Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Parser Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Parser |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/parser.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | class CI_Parser { |
| 50 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 51 | /** |
Dumk0 | 05e6f1e | 2013-04-17 12:58:34 +0300 | [diff] [blame] | 52 | * Left delimiter character for pseudo vars |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 53 | * |
| 54 | * @var string |
| 55 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 56 | public $l_delim = '{'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 57 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 58 | /** |
Dumk0 | 05e6f1e | 2013-04-17 12:58:34 +0300 | [diff] [blame] | 59 | * Right delimiter character for pseudo vars |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 60 | * |
| 61 | * @var string |
| 62 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 63 | public $r_delim = '}'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 64 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 65 | /** |
| 66 | * Reference to CodeIgniter instance |
| 67 | * |
| 68 | * @var object |
| 69 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 70 | protected $CI; |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 71 | |
Andrey Andreev | fe367a9 | 2012-11-12 21:05:45 +0200 | [diff] [blame] | 72 | // -------------------------------------------------------------------- |
| 73 | |
| 74 | /** |
| 75 | * Class constructor |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function __construct() |
| 80 | { |
| 81 | $this->CI =& get_instance(); |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 82 | log_message('info', 'Parser Class Initialized'); |
Andrey Andreev | fe367a9 | 2012-11-12 21:05:45 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 88 | * Parse a template |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 90 | * Parses pseudo-variables contained in the specified template view, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | * replacing them with the data in the second param |
| 92 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | * @param string |
| 94 | * @param array |
| 95 | * @param bool |
| 96 | * @return string |
| 97 | */ |
Greg Aker | 857c6f5 | 2010-11-11 17:28:49 -0600 | [diff] [blame] | 98 | public function parse($template, $data, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 100 | $template = $this->CI->load->view($template, $data, TRUE); |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 101 | |
| 102 | return $this->_parse($template, $data, $return); |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 108 | * Parse a String |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 109 | * |
| 110 | * Parses pseudo-variables contained in the specified string, |
| 111 | * replacing them with the data in the second param |
| 112 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 113 | * @param string |
| 114 | * @param array |
| 115 | * @param bool |
| 116 | * @return string |
| 117 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 118 | public function parse_string($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 119 | { |
| 120 | return $this->_parse($template, $data, $return); |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 126 | * Parse a template |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 127 | * |
| 128 | * Parses pseudo-variables contained in the specified template, |
| 129 | * replacing them with the data in the second param |
| 130 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 131 | * @param string |
| 132 | * @param array |
| 133 | * @param bool |
| 134 | * @return string |
| 135 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 136 | protected function _parse($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 137 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 138 | if ($template === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
| 140 | return FALSE; |
| 141 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 142 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 143 | $replace = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | foreach ($data as $key => $val) |
| 145 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 146 | $replace = array_merge( |
| 147 | $replace, |
| 148 | is_array($val) |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 149 | ? $this->_parse_pair($key, $val, $template) |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 150 | : $this->_parse_single($key, (string) $val, $template) |
| 151 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 153 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 154 | unset($data); |
| 155 | $template = strtr($template, $replace); |
| 156 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 157 | if ($return === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 159 | $this->CI->output->append_output($template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | return $template; |
| 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 168 | * Set the left/right variable delimiters |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | * @param string |
| 171 | * @param string |
| 172 | * @return void |
| 173 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 174 | public function set_delimiters($l = '{', $r = '}') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
| 176 | $this->l_delim = $l; |
| 177 | $this->r_delim = $r; |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 183 | * Parse a single key/value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | * @param string |
| 186 | * @param string |
| 187 | * @param string |
| 188 | * @return string |
| 189 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 190 | protected function _parse_single($key, $val, $string) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 192 | return array($this->l_delim.$key.$this->r_delim => (string) $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 198 | * Parse a tag pair |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | * |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 200 | * Parses tag pairs: {some_tag} string... {/some_tag} |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | * @param string |
| 203 | * @param array |
| 204 | * @param string |
| 205 | * @return string |
| 206 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 207 | protected function _parse_pair($variable, $data, $string) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 209 | $replace = array(); |
| 210 | preg_match_all( |
| 211 | '#'.preg_quote($this->l_delim.$variable.$this->r_delim).'(.+?)'.preg_quote($this->l_delim.'/'.$variable.$this->r_delim).'#s', |
| 212 | $string, |
| 213 | $matches, |
| 214 | PREG_SET_ORDER |
| 215 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 217 | foreach ($matches as $match) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | { |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 219 | $str = ''; |
| 220 | foreach ($data as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 222 | $temp = array(); |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 223 | foreach ($row as $key => $val) |
| 224 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 225 | if (is_array($val)) |
| 226 | { |
Andrey Andreev | 47c21c6 | 2014-08-28 01:01:32 +0300 | [diff] [blame] | 227 | $pair = $this->_parse_pair($key, $val, $match[1]); |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 228 | if ( ! empty($pair)) |
| 229 | { |
| 230 | $temp = array_merge($temp, $pair); |
| 231 | } |
| 232 | |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | $temp[$this->l_delim.$key.$this->r_delim] = $val; |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 239 | $str .= strtr($match[1], $temp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 242 | $replace[$match[0]] = $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 245 | return $replace; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | } |