PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/libraries/SZ_Parser.php

https://bitbucket.org/seezoo/seezoo/
PHP | 247 lines | 171 code | 35 blank | 41 comment | 29 complexity | fd1e430c05690a47b3c2bf3eee347f4c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * =========================================================
  4. *Extended CodeIgniter builtin Parser Class
  5. *
  6. * @add menthod parse_static
  7. * do template parse static files
  8. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  9. * =========================================================
  10. */
  11. class SZ_Parser extends CI_Parser
  12. {
  13. // temporary delimiters
  14. var $l_delim_temp;
  15. var $r_delim_temp;
  16. var $static_methods;
  17. // vurtual static dirs
  18. var $virtual_dirs = array();
  19. var $virtual_dir_count = 0;
  20. var $virtual_currnet_path = '';
  21. function SZ_Parser()
  22. {
  23. $this->l_delim_temp = $this->l_delim;
  24. $this->r_delim_temp = $this->r_delim;
  25. $this->static_methods = new StaticV();
  26. }
  27. // add method parse static
  28. // template parse for static file(html or php)
  29. function parse_static($template, $data, $return = FALSE)
  30. {
  31. // override delimiters property
  32. $this->set_delimiters('<!--\s?\{\$', '\}\s?-->');
  33. $CI =& get_instance();
  34. // get template data by "Loader::file" method.
  35. $template = $CI->load->file($template, TRUE);
  36. // same parse method
  37. if ($template == '')
  38. {
  39. return FALSE;
  40. }
  41. foreach ($data as $key => $val)
  42. {
  43. if (is_array($val))
  44. {
  45. $template = $this->_parse_pair($key, $val, $template);
  46. }
  47. else
  48. {
  49. $template = $this->_parse_single_statics($key, (string)$val, $template);
  50. }
  51. }
  52. // additional, simple function replace
  53. $template = $this->_parse_simple_function($template);
  54. // get relative virtual path
  55. $uri = trim($CI->uri->uri_string(), '/');
  56. if (strpos($uri, '/') !== FALSE)
  57. {
  58. $this->virtual_dirs = explode('/', $uri);
  59. array_pop($this->virtual_dirs);
  60. $this->virtual_dir_count = count($this->virtual_dirs);
  61. }
  62. // parse filepath to static
  63. $template = preg_replace_callback(
  64. '/(src|href|action)=[\'"]([^"\'<!>]+)["\']/',
  65. array($this, '_correct_static_path'),
  66. $template
  67. );
  68. if ($return == FALSE)
  69. {
  70. $CI->output->append_output($template);
  71. }
  72. $this->_reset_delimiter();
  73. return $template;
  74. }
  75. // add method parse vers
  76. // string parse for static file(html or php)
  77. function parse_vars($template, $data, $return = FALSE)
  78. {
  79. // override delimiters property
  80. $this->set_delimiters('<!--\s?\{\$', '\}\s?-->');
  81. $CI =& get_instance();
  82. // same parse method
  83. if ($template == '')
  84. {
  85. return FALSE;
  86. }
  87. foreach ($data as $key => $val)
  88. {
  89. if (is_array($val))
  90. {
  91. $template = $this->_parse_pair($key, $val, $template);
  92. }
  93. else
  94. {
  95. $template = $this->_parse_single_statics($key, (string)$val, $template);
  96. }
  97. }
  98. // additional, simple function replace
  99. $template = $this->_parse_simple_function($template);
  100. // not implement to evil code executes like include, require...
  101. if ($return == FALSE)
  102. {
  103. $CI->output->append_output($template);
  104. }
  105. $this->_reset_delimiter();
  106. return $template;
  107. }
  108. function _reset_delimiter()
  109. {
  110. $this->set_delimiters($this->l_delim_temp, $this->r_delim_temp);
  111. }
  112. /**
  113. * replace by regular exceptions
  114. * @param $key
  115. * @param $val
  116. * @param $string
  117. */
  118. function _parse_single_statics($key, $val, $string)
  119. {
  120. return preg_replace("|" . $this->l_delim.$key.$this->r_delim . "|", $val, $string);
  121. }
  122. /**
  123. * prase variable to simple function
  124. * @param string
  125. */
  126. function _parse_simple_function($string)
  127. {
  128. if (preg_match_all("|" . $this->l_delim . '([0-9a-zA-Z_]+)' . $this->r_delim . "|", $string, $match, PREG_SET_ORDER))
  129. {
  130. foreach ($match as $value)
  131. {
  132. if (method_exists($this->static_methods, $value[1]))
  133. {
  134. $ret = $this->static_methods->{$value[1]}();
  135. $string = str_replace($value[0], $ret, $string);
  136. }
  137. }
  138. }
  139. return $string;
  140. }
  141. /**
  142. * static replace callback method
  143. * @param array $v
  144. */
  145. function _correct_static_path($v)
  146. {
  147. $type = $v[1];
  148. $path = $v[2];
  149. if ( !preg_match('/\.html?$|\.php$/', $path) )
  150. {
  151. $prefix = 'statics/';
  152. }
  153. else
  154. {
  155. $path = preg_replace('/\.html?$|\.php$/', '', $path);
  156. $prefix = '';
  157. }
  158. // correct traversal path if path includes "../"
  159. if (strpos($path, '../') !== FALSE)
  160. {
  161. $level = substr_count($path, '../');
  162. if ($this->virtual_dir_count > $level)
  163. {
  164. $rel_path_array = array_slice(
  165. $this->virtual_dirs,
  166. 0,
  167. $this->virtual_dir_count - $level
  168. );
  169. $prefix .= implode('/', $rel_path_array) . '/';
  170. }
  171. else
  172. {
  173. $prefix .= implode('/', $this->virtual_dirs) . '/';
  174. }
  175. $path = str_replace('../', '', $path);
  176. }
  177. else if ($this->virtual_dir_count > 0)
  178. {
  179. $prefix .= implode('/', $this->virtual_dirs) . '/';
  180. }
  181. // kill current directory relative path
  182. str_replace('./', '', $path);
  183. if (preg_match('/^http/', $path) && strpos($path, 'statics/') === FALSE)
  184. {
  185. $p = file_link();
  186. if (defined('MOVE_ORIGIN_SITE') && MOVE_ORIGIN_SITE != '')
  187. {
  188. $from = MOVE_ORIGIN_SITE;
  189. }
  190. else
  191. {
  192. $from = file_link();
  193. }
  194. return $type .'="' . str_replace($from, $p . $prefix, $path) . '"';
  195. }
  196. else if (strpos($path, 'statics/') === FALSE)
  197. {
  198. if ( preg_match('/^javascript:.+/', $path) && $type === 'href')
  199. {
  200. return $type . '="' . $path . '"';
  201. }
  202. else
  203. {
  204. return $type . '="' . file_link() . $prefix . substr($path, strrpos($path, 'statics/')) . '"';
  205. }
  206. }
  207. else
  208. {
  209. return $type . '="' . file_link() . $prefix . $path . '"';
  210. }
  211. }
  212. }