PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/templates.php

https://github.com/MilkZoft/zan
PHP | 466 lines | 444 code | 22 blank | 0 comment | 39 complexity | e2b9ba0b79ea302226ecb7839807b869 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_Templates extends ZP_Load
  6. {
  7. public $themePath;
  8. public $themeRoute;
  9. private $CSS = null;
  10. private $topCSS = array();
  11. private $bottomCSS = array();
  12. private $js = null;
  13. private $topJS = array();
  14. private $bottomJS = array();
  15. private $theme = null;
  16. private $title;
  17. private $meta;
  18. private $vars = array();
  19. private $ignoredSegments = array();
  20. public function CSS($CSS = null, $application = null, $print = false, $top = false)
  21. {
  22. if ($top) {
  23. $arrayCSS = &$this->topCSS;
  24. } else {
  25. $arrayCSS = &$this->bottomCSS;
  26. }
  27. if (file_exists($CSS)) {
  28. if ($print) {
  29. print '<link rel="stylesheet" href="'. _get("webURL") .'/'. $CSS .'" type="text/css" />';
  30. } else {
  31. array_push($arrayCSS, $CSS);
  32. }
  33. }
  34. if ($CSS === "bootstrap") {
  35. if ($print) {
  36. print '<link rel="stylesheet" href="'. path("vendors/css/frameworks/bootstrap/css/bootstrap.min.css", "zan") .'" type="text/css" />';
  37. } else {
  38. array_push($arrayCSS, CORE_PATH ."/vendors/css/frameworks/bootstrap/css/bootstrap.min.css");
  39. }
  40. $this->js("bootstrap");
  41. } elseif ($CSS === "prettyphoto") {
  42. if ($print) {
  43. print '<link rel="stylesheet" href="'. path("vendors/js/lightbox/prettyphoto/css/prettyPhoto.css", "zan") .'" type="text/css" />';
  44. } else {
  45. array_push($arrayCSS, CORE_PATH ."/vendors/js/lightbox/prettyphoto/css/prettyPhoto.css");
  46. }
  47. } elseif ($CSS === "codemirror") {
  48. if ($print) {
  49. print '<link rel="stylesheet" href="'. path("vendors/js/codemirror/codemirror.css", "zan") .'" type="text/css" />';
  50. } else {
  51. array_push($arrayCSS, CORE_PATH ."/vendors/js/codemirror/codemirror.css");
  52. }
  53. } elseif ($CSS === "filedrag") {
  54. if ($print) {
  55. print '<link rel="stylesheet" href="'. path("vendors/js/files/uploader/styles.css", "zan") .'" type="text/css" />';
  56. } else {
  57. array_push($arrayCSS, CORE_PATH ."/vendors/js/files/uploader/styles.css");
  58. }
  59. }
  60. $file = is_null($application) ? "www/lib/css/$CSS.css" : "www/applications/$application/views/css/$CSS.css";
  61. if (file_exists($file)) {
  62. if ($print) {
  63. print '<link rel="stylesheet" href="'. _get("webURL") .'/'. $file .'" type="text/css" />';
  64. } else {
  65. array_push($arrayCSS, $file);
  66. }
  67. }
  68. }
  69. public function exists($template, $view = false)
  70. {
  71. if (!$view) {
  72. if (file_exists("www/lib/themes/$this->theme/$template.php")) {
  73. return true;
  74. }
  75. } elseif (file_exists("www/applications/$view/views/$template.php")) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. public function getCSS()
  81. {
  82. return $this->getScript("css");
  83. }
  84. public function getJs()
  85. {
  86. return $this->getScript("js");
  87. }
  88. private function getScript($ext)
  89. {
  90. if ($ext === "css") {
  91. $scripts = array_merge($this->topCSS, $this->bottomCSS);
  92. } elseif ($ext === "js") {
  93. $scripts = array_merge($this->topJS, $this->bottomJS);
  94. } else {
  95. return null;
  96. }
  97. if (count($scripts) > 0) {
  98. if (_get("environment") < 3 or !_get("optimization")) {
  99. array_walk($scripts, create_function('&$val', '$val = "'. _get("webURL") .'/$val";'));
  100. if ($ext === "css") {
  101. return '<link rel="stylesheet" href="'. implode('" type="text/css" /><link rel="stylesheet" href="', $scripts) .'" type="text/css" />';
  102. } else {
  103. return '<script type="text/javascript" src="'. implode('"></script><script type="text/javascript" src="', $scripts) .'"></script>';
  104. }
  105. } else {
  106. $filename = CACHE_DIR .'/'. $ext .'/'. md5(implode(':', $scripts)) .'.'. $ext;
  107. if (!is_file($filename)) {
  108. $contents = "";
  109. foreach ($scripts as $file) {
  110. $contents .= @file_get_contents($file) . "\n";
  111. }
  112. $contents = compress($contents, $ext);
  113. file_put_contents($filename, $contents, LOCK_EX);
  114. }
  115. if ($ext === "css") {
  116. return '<link rel="stylesheet" href="'. _get("webURL") .'/'. $filename .'" type="text/css" />';
  117. } else {
  118. return '<script type="text/javascript" src="'. _get("webURL") .'/'. $filename .'"></script>';
  119. }
  120. }
  121. }
  122. }
  123. public function getThemes($theme)
  124. {
  125. $path = "www/lib/themes/";
  126. $dir = dir($path);
  127. $options = false;
  128. $i = 0;
  129. while ($element = $dir->read()) {
  130. $directory = $path . $element . SH;
  131. if ($element !== ".." and $element !== "." and is_dir($directory) and $element !== "cpanel") {
  132. if ($element === $theme) {
  133. $options[$i]["value"] = $element;
  134. $options[$i]["option"] = $element;
  135. $options[$i]["selected"] = true;
  136. } else {
  137. $options[$i]["value"] = $element;
  138. $options[$i]["option"] = $element;
  139. $options[$i]["selected"] = false;
  140. }
  141. $i++;
  142. }
  143. }
  144. $dir->close();
  145. return $options;
  146. }
  147. public function getTitle()
  148. {
  149. return (is_null($this->title)) ? _get("webName") ." - ". _get("webSlogan") : encode($this->title);
  150. }
  151. public function getMeta()
  152. {
  153. return (is_null($this->meta) ? "" : ltrim($this->meta));
  154. }
  155. public function isTheme()
  156. {
  157. $this->path = (!is_null($this->theme)) ? "www/lib/themes/$this->theme" : false;
  158. $this->directory = @dir($this->path);
  159. return ($this->directory) ? true : false;
  160. }
  161. public function js($js, $application = null, $getJs = false, $top = false)
  162. {
  163. if ($top) {
  164. $arrayJS = &$this->topJS;
  165. } else {
  166. $arrayJS = &$this->bottomJS;
  167. }
  168. if ($js === "prettyphoto") {
  169. $this->CSS("prettyphoto");
  170. if ($getJs) {
  171. return '<script type="text/javascript" src="'. path("vendors/js/lightbox/prettyphoto/js/jquery.prettyphoto.js", "zan") .'"></script>';
  172. } else {
  173. array_push($arrayJS, CORE_PATH .'/vendors/js/lightbox/prettyphoto/js/jquery.prettyphoto.js');
  174. }
  175. } elseif ($js === "jquery") {
  176. if ($getJs) {
  177. return '<script type="text/javascript" src="'. path("vendors/js/jquery/jquery.js", "zan") .'"></script>';
  178. } else {
  179. array_push($arrayJS, CORE_PATH .'/vendors/js/jquery/jquery.js');
  180. }
  181. } elseif (preg_match('/^jquery\.(.+)\.js$/i', $js, $matches)) {
  182. $plugin_name = trim($matches[1]);
  183. if (file_exists(CORE_PATH ."/vendors/js/jquery/$plugin_name/")) {
  184. $this->css(CORE_PATH ."/vendors/js/jquery/$plugin_name/$plugin_name.css");
  185. if ($getJs) {
  186. return '<script type="text/javascript" src="'. path("vendors/js/jquery/$plugin_name/$js", "zan") .'"></script>';
  187. } else {
  188. array_push($arrayJS, CORE_PATH ."/vendors/js/jquery/$plugin_name/$js");
  189. }
  190. } elseif ($getJs) {
  191. return '<script type="text/javascript" src="'. path("vendors/js/jquery/$js", "zan") .'"></script>';
  192. } else {
  193. array_push($arrayJS, CORE_PATH ."/vendors/js/jquery/$js");
  194. }
  195. } elseif ($js === "filedrag") {
  196. $this->CSS("filedrag");
  197. if ($getJs) {
  198. return '<script type="text/javascript" src="'. path("vendors/js/files/uploader/filedrag.js", "zan") .'"></script>';
  199. } else {
  200. array_push($arrayJS, CORE_PATH .'/vendors/js/files/uploader/filedrag.js');
  201. }
  202. } elseif ($js === "ckeditor") {
  203. if ($getJs) {
  204. $js = '<script type="text/javascript" src="'. path("vendors/js/editors/ckeditor/ckeditor.js", "zan") .'"></script>';
  205. if ($application === "full") {
  206. $js .= "<script type=\"text/javascript\">
  207. CKEDITOR.config.extraPlugins = 'codemirror,insertpre,doksoft_image,doksoft_preview,doksoft_resize';
  208. CKEDITOR.config.insertpre_class = 'prettyprint';
  209. CKEDITOR.config.insertpre_style = 'background-color:#F8F8F8;border:1px solid #DDD;padding:10px;';
  210. CKEDITOR.config.filebrowserImageUploadUrl = '". path("vendors/js/editors/ckeditor/plugins/doksoft_uploader/uploader.php?type=Images", "zan") ."';
  211. CKEDITOR.config.filebrowserImageThumbsUploadUrl = '". path("vendors/js/editors/ckeditor/plugins/doksoft_uploader/uploader.php?type=Images&makeThumb=true", "zan") ."';
  212. CKEDITOR.config.filebrowserImageResizeUploadUrl = '". path("vendors/js/editors/ckeditor/plugins/doksoft_uploader/uploader.php?type=Images&resize=true", "zan") ."';
  213. CKEDITOR.replace('editor', {
  214. toolbar: [
  215. {name:'group1', items:['Bold','Italic','Underline','StrikeThrough','PasteFromWord']},
  216. {name:'group2', items:['Format']},
  217. {name:'group3', items:['Outdent','Indent','NumberedList','BulletedList','Blockquote','PageBreak']},
  218. {name:'group4', items:['Image','Link','Unlink','InsertPre','Source','doksoft_image', 'doksoft_preview', 'doksoft_resize']}
  219. ]
  220. });
  221. </script>";
  222. } else {
  223. $js .= "<script type=\"text/javascript\">
  224. CKEDITOR.config.insertpre_style = 'background-color:#F8F8F8;border:1px solid #DDD;padding:10px;';
  225. CKEDITOR.config.insertpre_class = 'prettyprint';
  226. CKEDITOR.replace('editor', {
  227. toolbar: [
  228. {name:'group1', items:['Bold','Italic','Underline','StrikeThrough','PasteFromWord']},
  229. {name:'group2', items:['Outdent','Indent','NumberedList','BulletedList','Blockquote']},
  230. {name:'group3', items:['Image','Link','Unlink','InsertPre']}
  231. ]
  232. });
  233. </script>";
  234. }
  235. return $js;
  236. } else {
  237. array_push($arrayJS, CORE_PATH .'/vendors/js/editors/ckeditor/ckeditor.js');
  238. }
  239. } elseif ($js === "lesscss") {
  240. if ($getJs) {
  241. return '<script type="text/javascript" src="'. path("vendors/js/less/less.js", "zan") .'"></script>';
  242. } else {
  243. array_push($arrayJS, CORE_PATH .'/vendors/js/less/less.js');
  244. }
  245. } elseif ($js === "angular") {
  246. if ($getJs) {
  247. return '<script type="text/javascript" src="'. path("vendors/js/angular/angular-1.0.1.min.js", "zan") .'"></script>';
  248. } else {
  249. array_push($arrayJS, CORE_PATH .'/vendors/js/angular/angular-1.0.1.min.js');
  250. }
  251. } elseif ($js === "bootstrap") {
  252. if ($getJs) {
  253. return '<script type="text/javascript" src="'. path("vendors/css/frameworks/bootstrap/js/bootstrap.min.js", "zan") .'"></script>';
  254. } else {
  255. array_push($arrayJS, CORE_PATH .'/vendors/css/frameworks/bootstrap/js/bootstrap.min.js');
  256. }
  257. } elseif ($js === "codemirror") {
  258. if ($getJs) {
  259. $js = '<script type="text/javascript" src="'. path("vendors/js/codemirror/codemirror.js", "zan") .'"></script>';
  260. if (is_null($application)) {
  261. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/util/loadmode.js", "zan") .'"></script>';
  262. } elseif ($application === "php") {
  263. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/htmlmixed.js", "zan") .'"></script>';
  264. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/xml.js", "zan") .'"></script>';
  265. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/javascript.js", "zan") .'"></script>';
  266. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/css.js", "zan") .'"></script>';
  267. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/clike.js", "zan") .'"></script>';
  268. $js .= '<script type="text/javascript" src="'. path("vendors/js/codemirror/mode/php.js", "zan") .'"></script>';
  269. }
  270. return $js;
  271. } else {
  272. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/codemirror.js');
  273. if (is_null($application)) {
  274. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/util/loadmode.js');
  275. } elseif ($application === "php") {
  276. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/htmlmixed.js');
  277. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/xml.js');
  278. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/javascript.js');
  279. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/css.js');
  280. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/clike.js');
  281. array_push($arrayJS, CORE_PATH .'/vendors/js/codemirror/mode/php.js');
  282. }
  283. }
  284. } elseif (file_exists($js)) {
  285. if ($getJs) {
  286. return '<script type="text/javascript" src="'. _get("webURL") .'/'. $js .'"></script>';
  287. } else {
  288. array_push($arrayJS, $js);
  289. }
  290. } elseif (file_exists(path($js, "zan"))) {
  291. if ($getJs) {
  292. return '<script type="text/javascript" src="'. path($js, "zan") .'"></script>';
  293. } else {
  294. array_push($arrayJS, CORE_PATH .'/'. $js);
  295. }
  296. } elseif (file_exists("www/applications/$application/views/js/$js")) {
  297. if ($getJs) {
  298. $filename = "www/applications/$application/views/js/$js";
  299. return '<script type="text/javascript" src="'. _get("webURL") .'/'. $filename .'"></script>';
  300. } else {
  301. array_push($arrayJS, "www/applications/$application/views/js/$js");
  302. }
  303. } elseif (file_exists("www/applications/$application/views/js/$js.js")) {
  304. if ($getJs) {
  305. $filename = "www/applications/$application/views/js/$js.js";
  306. return '<script type="text/javascript" src="'. _get("webURL") .'/'. $filename .'"></script>';
  307. } else {
  308. array_push($arrayJS, "www/applications/$application/views/js/$js.js");
  309. }
  310. } else {
  311. return false;
  312. }
  313. }
  314. public function load($template, $direct = false)
  315. {
  316. if (is_array($this->vars)) {
  317. $key = array_keys($this->vars);
  318. $size = sizeof($key);
  319. for ($i = 0; $i < $size; $i++) {
  320. $$key[$i] = $this->vars[$key[$i]];
  321. }
  322. }
  323. if ($direct) {
  324. if (is_array($template)) {
  325. $count = count($template);
  326. if ($count === 1) {
  327. include $template[0];
  328. } elseif ($count === 2) {
  329. include $template[0];
  330. include $template[1];
  331. } elseif ($count === 3) {
  332. include $template[0];
  333. include $template[1];
  334. include $template[2];
  335. } else {
  336. include $template[0];
  337. include $template[1];
  338. include $template[2];
  339. include $template[3];
  340. }
  341. } else {
  342. if (!file_exists($template)) {
  343. getException("Error 404: Theme Not Found: ". $template);
  344. }
  345. include $template;
  346. }
  347. } else {
  348. $templateName = $template;
  349. $template = "www/lib/themes/$this->theme/$templateName.php";
  350. $minTemplate = "www/lib/themes/$this->theme/min/$templateName.php";
  351. if (_get("minifyCode") and file_exists($minTemplate)) {
  352. $template = $minTemplate;
  353. }
  354. if (!file_exists($template)) {
  355. getException("Error 404: Theme Not Found: ". $template);
  356. }
  357. include $template;
  358. }
  359. }
  360. public function theme($theme = null)
  361. {
  362. $this->theme = (is_null($theme)) ? _get("webTheme") : $theme;
  363. $this->themeRoute = "www/lib/themes/$this->theme";
  364. $this->themePath = (_get("environment") == 1) ? _get("webURL") ."/$this->themeRoute" : getCDN() ."/$this->themeRoute";
  365. if (!$this->isTheme()) {
  366. die("You need to create a valid theme");
  367. }
  368. }
  369. public function themeCSS($theme = null, $min = true)
  370. {
  371. $style = ($min) ? "style.min.css" : "style.css";
  372. return '<link rel="stylesheet" href="'. $this->themePath .'/css/'. $style .'" type="text/css">';
  373. }
  374. public function title($title = null) {
  375. $this->helper("string");
  376. if (!is_null($title)) {
  377. $title = stripslashes($title) ." - ". _get("webName");
  378. }
  379. $this->title = is_null($title) ? _get("webName") ." - ". _get("webSlogan") : $title;
  380. $this->meta("title", $this->title);
  381. }
  382. public function meta($tag, $value)
  383. {
  384. switch ($tag) {
  385. case "title":
  386. $value = stripslashes($value);
  387. $this->meta .= "<meta name=\"$tag\" content=\"$value\" />";
  388. break;
  389. case "language":
  390. $this->meta .= "<meta http-equiv=\"content-language\" content=\"$value\" />";
  391. break;
  392. case "description":
  393. $value = preg_replace("/\r\n+/", " ", strip_tags($value));
  394. $value = str_replace('"', "", $value);
  395. if (strlen($value) > 250) {
  396. $abstract = stripslashes(substr($value, 0, strrpos(substr($value, 0, 100), " ")));
  397. $value = stripslashes(substr($value, 0, strrpos(substr($value, 0, 250), " ")));
  398. } else {
  399. $abstract = $value;
  400. }
  401. $this->meta .= "<meta name=\"abstract\" content=\"". $abstract ."\" />";
  402. default:
  403. $this->meta .= "<meta name=\"$tag\" content=\"$value\" />";
  404. break;
  405. }
  406. }
  407. public function vars($vars)
  408. {
  409. $this->vars = $vars;
  410. }
  411. }