PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/modules/template-editor/template-editor.Manage.php

https://github.com/KenBoyer/CompactCMS
PHP | 334 lines | 230 code | 33 blank | 71 comment | 23 complexity | ef456c03f28a3ea2b1dec8fb2319da04 MD5 | raw file
  1. <?php
  2. /* ************************************************************
  3. Copyright (C) 2008 - 2010 by Xander Groesbeek (CompactCMS.nl)
  4. Revision: CompactCMS - v 1.4.2
  5. This file is part of CompactCMS.
  6. CompactCMS is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. CompactCMS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. A reference to the original author of CompactCMS and its copyright
  15. should be clearly visible AT ALL TIMES for the user of the back-
  16. end. You are NOT allowed to remove any references to the original
  17. author, communicating the product to be your own, without written
  18. permission of the original copyright owner.
  19. You should have received a copy of the GNU General Public License
  20. along with CompactCMS. If not, see <http://www.gnu.org/licenses/>.
  21. > Contact me for any inquiries.
  22. > E: Xander@CompactCMS.nl
  23. > W: http://community.CompactCMS.nl/forum
  24. ************************************************************ */
  25. /* make sure no-one can run anything here if they didn't arrive through 'proper channels' */
  26. if(!defined("COMPACTCMS_CODE")) { define("COMPACTCMS_CODE", 1); } /*MARKER*/
  27. /*
  28. We're only processing form requests / actions here, no need to load the page content in sitemap.php, etc.
  29. */
  30. if (!defined('CCMS_PERFORM_MINIMAL_INIT')) { define('CCMS_PERFORM_MINIMAL_INIT', true); }
  31. // Define default location
  32. if (!defined('BASE_PATH'))
  33. {
  34. $base = str_replace('\\','/',dirname(dirname(dirname(dirname(dirname(__FILE__))))));
  35. define('BASE_PATH', $base);
  36. }
  37. // Include general configuration
  38. /*MARKER*/require_once(BASE_PATH . '/lib/sitemap.php');
  39. // security check done ASAP
  40. if(!checkAuth() || empty($_SESSION['rc1']) || empty($_SESSION['rc2']))
  41. {
  42. die("No external access to file");
  43. }
  44. $do = getGETparam4IdOrNumber('do');
  45. $status = getGETparam4IdOrNumber('status');
  46. $status_message = getGETparam4DisplayHTML('msg');
  47. // Set the default template
  48. $dir_temp = BASE_PATH . "/lib/templates/";
  49. $get_temp = getGETparam4FullFilePath('template', $template[0].'.tpl.html');
  50. $chstatus = is_writable_ex($dir_temp.$get_temp); // @dev: to test the error feedback on read-only on Win+UNIX: add '|| 1' here.
  51. // Check for filename
  52. if(!empty($get_temp))
  53. {
  54. if(@fopen($dir_temp.$get_temp, "r"))
  55. {
  56. $handle = fopen($dir_temp.$get_temp, "r");
  57. // PHP5+ Feature
  58. $contents = stream_get_contents($handle);
  59. if (0)
  60. {
  61. // PHP4 Compatibility
  62. $flen = filesize($dir_temp.$get_temp);
  63. if ($flen > 0)
  64. {
  65. $contents = @fread($handle, $flen);
  66. }
  67. }
  68. fclose($handle);
  69. $contents = str_replace("<br />", "<br>", $contents);
  70. }
  71. }
  72. if(!$perm->is_level_okay('manageTemplate', $_SESSION['ccms_userLevel']))
  73. {
  74. $chstatus = false; // templates are viewable but NOT WRITABLE when user doesn't have permission to manage these.
  75. }
  76. ?>
  77. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  78. <html>
  79. <head>
  80. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  81. <title>Template Editing module</title>
  82. <link rel="stylesheet" type="text/css" href="../../../../admin/img/styles/base.css,liquid.css,layout.css,sprite.css,last_minute_fixes.css" />
  83. <!--[if IE]>
  84. <link rel="stylesheet" type="text/css" href="../../../../admin/img/styles/ie.css" />
  85. <![endif]-->
  86. </head>
  87. <body>
  88. <div class="module" id="template-editor">
  89. <?php
  90. if(!$chstatus)
  91. {
  92. ?>
  93. <div class="span-25 last center-text error">
  94. <p class="ss_has_sprite"><span class="ss_sprite_16 ss_error">&#160;</span><?php echo $ccms['lang']['template']['nowrite']; ?></p>
  95. </div>
  96. <?php
  97. }
  98. ?>
  99. <div class="span-15 clear">
  100. <h1 class="editor"><?php echo $ccms['lang']['template']['manage']; ?></h1>
  101. </div>
  102. <div class="span-10 right last">
  103. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="changeTmp" method="get" class="right" accept-charset="utf-8">
  104. <label for="template"><?php echo $ccms['lang']['backend']['template'];?></label>
  105. <select class="text" onChange="document.getElementById('changeTmp').submit();" id="template" name="template">
  106. <?php
  107. for ($x = 0; $x < count($template); $x++)
  108. {
  109. ?>
  110. <optgroup label="<?php echo ucfirst($template[$x]); ?>">
  111. <option <?php echo ($get_temp == $template[$x].'.tpl.html') ? 'selected="selected"' : ''; ?> value="<?php echo $template[$x]; ?>.tpl.html"><?php echo ucfirst($template[$x]).': '.strtolower($ccms['lang']['backend']['template']); ?></option>
  112. <?php
  113. // Get CSS and other text-editable files which are part of the engine
  114. $cssfiles = array();
  115. if ($handle = opendir($dir_temp.$template[$x].'/'))
  116. {
  117. while (false !== ($file = readdir($handle)))
  118. {
  119. if ($file != "." && $file != "..")
  120. {
  121. switch (strtolower(substr($file, strrpos($file, '.') + 1)))
  122. {
  123. case 'css':
  124. case 'js':
  125. case 'php':
  126. case 'xml':
  127. case 'htm':
  128. case 'html':
  129. case 'txt':
  130. $cssfiles[$x][] = $file;
  131. break;
  132. default:
  133. // don't list image files and such
  134. break;
  135. }
  136. }
  137. }
  138. closedir($handle);
  139. }
  140. foreach ($cssfiles[$x] as $css)
  141. {
  142. ?>
  143. <option <?php echo ($get_temp == $template[$x].'/'.$css) ? 'selected="selected"' : ''; ?> value="<?php echo $template[$x].'/'.$css; ?>"><?php echo ucfirst($template[$x]).': '.$css; ?></option>
  144. <?php
  145. }
  146. ?>
  147. </optgroup>
  148. <?php
  149. }
  150. ?>
  151. </select>
  152. </form>
  153. </div>
  154. <hr class="space span-25 clear"/>
  155. <?php
  156. /*
  157. ??? ALWAYS saying 'settings saved' instead of the attached message in the old code? Must've been a bug...
  158. Changed to mimic the layout in the other files...
  159. */
  160. ?>
  161. <div class="center-text <?php echo $status; ?> span-25 clear">
  162. <?php
  163. if(!empty($status_message))
  164. {
  165. echo '<p class="ss_has_sprite"><span class="ss_sprite_16 '.($status == 'notice' ? 'ss_accept' : 'ss_error').'">&#160;</span>'.$status_message.'</p>';
  166. }
  167. ?>
  168. </div>
  169. <form action="template-editor.Process.php?template=<?php echo $get_temp; ?>&action=save-template" method="post" accept-charset="utf-8">
  170. <textarea id="content" name="content"><?php echo htmlspecialchars(trim($contents), ENT_COMPAT, 'UTF-8'); ?></textarea>
  171. <input type="hidden" name="template" value="<?php echo $get_temp; ?>" id="template" />
  172. <div class="right">
  173. <?php
  174. if($chstatus)
  175. {
  176. ?>
  177. <button type="submit" name="do" id="submit"><span class="ss_sprite_16 ss_disk">&#160;</span><?php echo $ccms['lang']['editor']['savebtn']; ?></button>
  178. <?php
  179. }
  180. ?>
  181. <a class="button" href="../../../index.php" onClick="return confirmation();" title="<?php echo $ccms['lang']['editor']['cancelbtn']; ?>"><span class="ss_sprite_16 ss_cross">&#160;</span><?php echo $ccms['lang']['editor']['cancelbtn']; ?></a>
  182. </div>
  183. </form>
  184. <?php
  185. if ($cfg['IN_DEVELOPMENT_ENVIRONMENT'])
  186. {
  187. ?>
  188. <textarea id="jslog" class="log span-25 last clear" readonly="readonly">
  189. </textarea>
  190. <?php
  191. }
  192. ?>
  193. </div>
  194. <?php
  195. // TODO: call edit_area_compressor.php only from the combiner: combine.inc.php when constructing the edit_area.js file for the first time.
  196. ?>
  197. <script type="text/javascript">
  198. function confirmation()
  199. {
  200. var answer = <?php echo (strpos($cfg['verify_alert'], 'X') !== false ? 'confirm("'.$ccms['lang']['editor']['confirmclose'].'")' : 'true'); ?>;
  201. if(answer)
  202. {
  203. return !close_mochaUI_window_or_goto_url("<?php echo makeAbsoluteURI($cfg['rootdir'] . 'admin/index.php'); ?>", 'sys-tmp_ccms');
  204. }
  205. return false;
  206. }
  207. <?php
  208. $js_files = array(
  209. $cfg['rootdir'] . 'lib/includes/js/the_goto_guy.js',
  210. $cfg['rootdir'] . 'lib/includes/js/mootools-core.js,mootools-more.js'
  211. );
  212. if ($cfg['USE_JS_DEVELOPMENT_SOURCES'])
  213. {
  214. $js_files[] = $cfg['rootdir'] . 'lib/includes/js/edit_area/edit_area_ccms.js';
  215. }
  216. else
  217. {
  218. $js_files[] = $cfg['rootdir'] . 'lib/includes/js/edit_area/edit_area_ccms.js';
  219. }
  220. /*
  221. be aware that the edit_area code assumes a 'onLoad' event will be triggered AFTER it is
  222. loaded, which is ONLY PROBABLY TRUE when the edit_area code is loaded by <script> tags
  223. in the page header or HTML itself and NOT TRUE when edit_Area itself is loaded through
  224. a lazyloader, like we do.
  225. Hence, we need to execute the edit_area onLoad event code manually at a time when we
  226. can be certain the edit_area code is really loaded.
  227. That's what the 'loaded' check and call in the code below is for.
  228. */
  229. $eaLanguage = $cfg['editarea_language'];
  230. $EAsyntax = cvt_extension2EAsyntax($dir_temp.$get_temp);
  231. $driver_code = <<<EOT42
  232. if (editAreaLoader.win != "loaded")
  233. {
  234. editAreaLoader.window_loaded();
  235. }
  236. /*
  237. resize event has the problem that it is triggered continually when in IE (and tests reveal it's similar in FF3)
  238. and we do NOT want to spend CPU cycles on repeated updates of the MUI window sizes all the time, so we follow the
  239. advice found here:
  240. http://mbccs.blogspot.com/2007/11/fixing-window-resize-event-in-ie.html
  241. http://mootools-users.660466.n2.nabble.com/Moo-Detecting-window-resize-td3713058.html
  242. */
  243. var resizeTimeout;
  244. var realResize = function(){
  245. //alert('template editor: resize event');
  246. };
  247. window.addEvent('resize', function(e){
  248. \$clear(resizeTimeout);
  249. resizeTimeout = realResize.delay(200, this);
  250. });
  251. // initialisation
  252. // make sure we only specify a /supported/ syntax; if we spec something else, edit_area will NOT show up!
  253. if (!editAreaLoader.init(
  254. {
  255. id: "content",
  256. start_highlight: true,
  257. allow_resize: 'both',
  258. //min_width: 400,
  259. //min_height: 125,
  260. allow_toggle: true,
  261. word_wrap: true,
  262. language: "$eaLanguage",
  263. syntax: "$EAsyntax",
  264. ignore_unsupported_syntax: true
  265. }))
  266. {
  267. alert('failed to start the EditArea JS control; error code: ' + (0 + editAreaLoader.error_code));
  268. }
  269. /*
  270. for (syn in editAreaLoader.load_syntax)
  271. {
  272. alert("syntax: " + syn);
  273. }
  274. */
  275. EOT42;
  276. echo generateJS4lazyloadDriver($js_files, $driver_code);
  277. ?>
  278. </script>
  279. <script type="text/javascript" src="../../../../lib/includes/js/lazyload/lazyload.js" charset="utf-8"></script>
  280. </body>
  281. </html>