PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/dryden/ui/templateparser.class.php

https://github.com/raiman264/zpanelx
PHP | 392 lines | 376 code | 2 blank | 14 comment | 0 complexity | 417e9028d0b1bab1a5d98e46940d4b87 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, CC-BY-SA-4.0, GPL-3.0
  1. <?php
  2. /**
  3. * The template parser class.
  4. * @package zpanelx
  5. * @subpackage dryden -> ui
  6. * @version 1.0.0
  7. * @author Bobby Allen (ballen@bobbyallen.me)
  8. * @copyright ZPanel Project (http://www.zpanelcp.com/)
  9. * @link http://www.zpanelcp.com/
  10. * @license GPL (http://www.gnu.org/licenses/gpl.html)
  11. */
  12. class ui_templateparser
  13. {
  14. /**
  15. * Array of all the functions allowed by ZPanelX Template system with the pattern to identify them
  16. * @author Sam Mottley (smottley@zpanelcp.com)
  17. */
  18. static public $Functions = array(
  19. 'PHPTags' => "/(<\?php)|(<\?)|(\?>)/is",
  20. 'TemplateClass' => "/<# ([\w*_*]*) #>/is",
  21. 'FunctionEcho' => "/<@\s([\w*]*)\s@>/is",
  22. 'Lanuage' => "/<:\s([^>\]\"<\?>]*)\s:>/is",
  23. 'EndLoop' => "/<%\s(endloop)\s%>/is",
  24. 'Loop' => "/<%\sloop\s(\w*)\s%>/is",
  25. 'EchoLoop' => "/<&\s([\w*]*)\s&>/is",
  26. 'If' => " /<%\sif\s([\w*-*_*]*)\s%>/is",
  27. 'Else' => "/<%\s(else)\s%>/is",
  28. 'EndIf' => "/<%\s(endif)\s%>/is"
  29. );
  30. /**
  31. * Location of the cached files relative to root
  32. * @author Sam Mottley (smottley@zpanelcp.com)
  33. */
  34. static public $storageLocation = 'etc/tmp/storage/';
  35. /**
  36. * 1 in X change on page load to check for old cached files for deletetion
  37. * @author Sam Mottley (smottley@zpanelcp.com)
  38. */
  39. static public $deleteCacheChance = 40;
  40. /**
  41. * use eval or file cache for the template loading
  42. * @author Sam Mottley (smottley@zpanelcp.com)
  43. * @var 1=file cache 0=eval
  44. */
  45. static public $evalOrCache = 1;
  46. /**
  47. * Runs though the functions array and loads the relivent function compiler
  48. * @author Sam Mottley (smottley@zpanelcp.com)
  49. */
  50. static function CompileFunctions($data)
  51. {
  52. $temp = $data;
  53. runtime_hook::Execute('OnBeforeTemplateProcessor');
  54. foreach (ui_templateparser::$Functions as $Tag => $pattern) {
  55. $temp = call_user_func_array('ui_templateparser::Compile' . $Tag, array($pattern, $temp));
  56. }
  57. runtime_hook::Execute('OnAfterTemplateProcessor');
  58. return $temp;
  59. }
  60. /**
  61. * Removes any php tags
  62. * @author Sam Mottley (smottley@zpanelcp.com)
  63. */
  64. static private function CompilePHPTags($value, $data)
  65. {
  66. $match = null;
  67. preg_match_all($value, $data, $match);
  68. if (($match) && (!empty($match[1]))) {
  69. $i = 0;
  70. foreach ($match[1] as $string) {
  71. if ($match[0][$i] == '?>') {
  72. $data = str_replace($match[0][$i], ']', $data);
  73. } else {
  74. $data = str_replace($match[0][$i], 'PHP execution is not permitted! Caught: [', $data);
  75. }
  76. $i++;
  77. }
  78. }
  79. return $data;
  80. }
  81. /**
  82. * Compiles ZPanelX loop template tags into valid PHP
  83. * @author Sam Mottley (smottley@zpanelcp.com)
  84. */
  85. static private function CompileLoop($value, $data)
  86. {
  87. $match = null;
  88. preg_match_all($value, $data, $match);
  89. if (($match) && (!empty($match[1]))) {
  90. $i = 0;
  91. foreach ($match[1] as $function) {
  92. $data = str_replace($match[0][$i], '<?php foreach(module_controller::get' . $function . '() as $key => $value){ ?>', $data);
  93. $i++;
  94. }
  95. }
  96. return $data;
  97. }
  98. /**
  99. * Compiles ZPanelX echo loop template tags into valid PHP
  100. * @author Sam Mottley (smottley@zpanelcp.com)
  101. */
  102. static private function CompileEchoLoop($value, $data)
  103. {
  104. $match = null;
  105. preg_match_all($value, $data, $match);
  106. if (($match) && (!empty($match[1]))) {
  107. $i = 0;
  108. foreach ($match[1] as $key) {
  109. $data = str_replace($match[0][$i], '<?php echo $value[\'' . $key . '\']; ?>', $data);
  110. $i++;
  111. }
  112. }
  113. return $data;
  114. }
  115. /**
  116. * Compiles ZPanelX end loop template tags into valid PHP
  117. * @author Sam Mottley (smottley@zpanelcp.com)
  118. */
  119. static private function CompileEndLoop($value, $data)
  120. {
  121. $match = null;
  122. preg_match_all($value, $data, $match);
  123. if (($match) && (!empty($match[1]))) {
  124. $i = 0;
  125. foreach ($match[1] as $string) {
  126. $data = str_replace($match[0][$i], '<?php } ?>', $data);
  127. $i++;
  128. }
  129. }
  130. return $data;
  131. }
  132. /**
  133. * Compiles ZPanelX if template tags into valid PHP
  134. * @author Sam Mottley (smottley@zpanelcp.com)
  135. */
  136. static private function CompileIf($value, $data)
  137. {
  138. $match = null;
  139. preg_match_all($value, $data, $match);
  140. if (($match) && (!empty($match[1]))) {
  141. $i = 0;
  142. foreach ($match[1] as $function) {
  143. $data = str_replace($match[0][$i], '<?php if(module_controller::get' . $function . '()){ ?>', $data);
  144. $i++;
  145. }
  146. }
  147. return $data;
  148. }
  149. /**
  150. * Compiles ZPanelX end if template tags into valid PHP
  151. * @author Sam Mottley (smottley@zpanelcp.com)
  152. */
  153. static private function CompileEndIf($value, $data)
  154. {
  155. $match = null;
  156. preg_match_all($value, $data, $match);
  157. if ($match) {
  158. $i = 0;
  159. foreach ($match[1] as $string) {
  160. $data = str_replace($match[0][$i], '<?php } ?>', $data);
  161. $i++;
  162. }
  163. }
  164. return $data;
  165. }
  166. /**
  167. * Compiles ZPanelX lanuage template tags into valid PHP
  168. * @author Sam Mottley (smottley@zpanelcp.com)
  169. */
  170. static private function CompileLanuage($value, $data)
  171. {
  172. $match = null;
  173. preg_match_all($value, $data, $match);
  174. if ($match) {
  175. $i = 0;
  176. foreach ($match[1] as $string) {
  177. $output = ui_language::translate(addslashes($string));
  178. $data = str_replace($match[0][$i], "<?php echo ui_language::translate('" . addslashes($string) . "');?>", $data);
  179. $i++;
  180. }
  181. }
  182. return $data;
  183. }
  184. /**
  185. * Compiles ZPanelX function each template tags into valid PHP
  186. * @author Sam Mottley (smottley@zpanelcp.com)
  187. */
  188. static private function CompileFunctionEcho($value, $data)
  189. {
  190. $match = null;
  191. preg_match_all($value, $data, $match);
  192. if ($match) {
  193. $i = 0;
  194. foreach ($match[1] as $classes) {
  195. $method_name = "get" . $classes;
  196. $output = module_controller::$method_name();
  197. $data = str_replace($match[0][$i], "<?php echo module_controller::" . $method_name . "(); ?>", $data);
  198. $i++;
  199. }
  200. }
  201. return $data;
  202. }
  203. /**
  204. * Compiles ZPanelX end if template tags into valid PHP
  205. * @author Sam Mottley (smottley@zpanelcp.com)
  206. */
  207. static private function CompileElse($value, $data)
  208. {
  209. $match = null;
  210. preg_match_all($value, $data, $match);
  211. if ($match) {
  212. $i = 0;
  213. foreach ($match[1] as $string) {
  214. $data = str_replace($match[0][$i], '<?php }else{ ?>', $data);
  215. $i++;
  216. }
  217. }
  218. return $data;
  219. }
  220. /**
  221. * Compiles ZPanelX template class tag into valid PHP
  222. * @author Sam Mottley (smottley@zpanelcp.com)
  223. */
  224. static private function CompileTemplateClass($value, $data)
  225. {
  226. $match = null;
  227. preg_match_all($value, $data, $match);
  228. if ($match) {
  229. $i = 0;
  230. foreach ($match[1] as $classes) {
  231. if (class_exists('' . $classes . '')) {
  232. $moduleTemplate = call_user_func(array($classes, 'Template'));
  233. $codeToInsert = ui_templateparser::CompileFunctions($moduleTemplate);
  234. $data = str_replace($match[0][$i], $codeToInsert, $data);
  235. }
  236. $i++;
  237. }
  238. }
  239. return $data;
  240. }
  241. /**
  242. * Check the cache file for presents and valid/upto date Data
  243. * @author Sam Mottley (smottley@zpanelcp.com)
  244. */
  245. static function CheckFileCache($phpCode)
  246. {
  247. $currentCode = $phpCode;
  248. //Get the users current theme
  249. $userDetails = ctrl_users::GetUserDetail();
  250. $userTheme = $userDetails['usertheme'];
  251. //The location of the cached php
  252. if (isset($_GET['module'])) {
  253. $location = ui_templateparser::$storageLocation . $userTheme . '/' . md5(fs_protector::SanitiseFolderName($_GET['module'])) . '.cache';
  254. } else {
  255. $location = ui_templateparser::$storageLocation . $userTheme . '/' . md5('index') . '.cache';
  256. }
  257. //check folder exists (First load of 10.1.0 and on new theme)
  258. $dirname = dirname($location);
  259. if (!is_dir($dirname)) {
  260. mkdir($dirname, 0755, true);
  261. }
  262. //check file exsists if not make, insert code else just get current code
  263. if (file_exists($location)) {
  264. $content = file_get_contents($location);
  265. } else {
  266. $handle = fopen($location, 'w');
  267. file_put_contents($location, $phpCode, LOCK_EX);
  268. $content = $phpCode;
  269. }
  270. //check the file content is the same as the generated content then return file location
  271. if ($currentCode == $content) {
  272. return $location;
  273. } else {
  274. file_put_contents($location, $phpCode, LOCK_EX);
  275. return $location;
  276. }
  277. }
  278. /**
  279. * Check the cache for very old files and clear them
  280. * @var deathAfter the number of seconds old a cache file can be deafult 7 days
  281. * @author Sam Mottley (smottley@zpanelcp.com)
  282. */
  283. static function clearOldCache($deathAfter = '604800')
  284. {
  285. //dont check every time!
  286. $rollDice = rand(1, ui_templateparser::$deleteCacheChance);
  287. //1 in X chance of checking all files
  288. if ($rollDice == 1) {
  289. //get all files and folders in storage location
  290. $contents = glob(ui_templateparser::$storageLocation . "*");
  291. //loop each file and folder
  292. foreach ($contents as $content) {
  293. //Is folder
  294. if (is_dir($content)) {
  295. $cacheFilesArray = glob($content . "/*");
  296. foreach ($cacheFilesArray as $cacheFile) {
  297. if (!is_dir($cacheFile)) {
  298. $time = time() - $deathAfter;
  299. if (filemtime($cacheFile) <= $time) {
  300. //Delete cache files
  301. $path = pathinfo($cacheFile);
  302. chdir($path['dirname']);
  303. unlink($path['filename'] . '.cache');
  304. }
  305. }
  306. }
  307. }
  308. }
  309. chdir('/');
  310. }
  311. }
  312. /**
  313. * Set the root of the temp path location
  314. * @author Sam Mottley (smottley@zpanelcp.com)
  315. */
  316. static public function setLocation()
  317. {
  318. self::$storageLocation = ctrl_options::GetSystemOption('zpanel_root') . self::$storageLocation;
  319. }
  320. /**
  321. * Run the php code and return it
  322. * @var code the php code
  323. * @author Sam Mottley (smottley@zpanelcp.com)
  324. */
  325. static function runPHP($template_code)
  326. {
  327. //load from DB or set a default
  328. $evalOrCache = ui_templateparser::$evalOrCache;
  329. //selected eval or file cache
  330. if ($evalOrCache == 1) {
  331. $fileLocation = ui_templateparser::CheckFileCache($template_code);
  332. return include($fileLocation);
  333. } else {
  334. return eval('?>' . $template_code);
  335. }
  336. }
  337. /**
  338. * All br tags to be used using the zpanel br tag loader
  339. * @author Sam Mottley (smottley@zpanelcp.com)
  340. */
  341. static function allowBr($templateCode)
  342. {
  343. return str_replace("ZP(br)", '<br/>', $templateCode);
  344. }
  345. /**
  346. * Loads in the template content and parses it to compute the place holder content.
  347. * @author Bobby Allen (ballen@bobbyallen.me)
  348. * @param string $template_path The full path to the system template (or user template).
  349. * @return sting The processed template HTML.
  350. */
  351. static function Generate($template_path)
  352. {
  353. self::setLocation();
  354. $template_raw = file_get_contents($template_path . "/master.ztml");
  355. $template_code = ui_templateparser::allowBr(ui_templateparser::CompileFunctions($template_raw));
  356. ui_templateparser::clearOldCache();
  357. return ui_templateparser::runPHP($template_code);
  358. }
  359. }
  360. ?>