PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Modules/Content.module.php

https://github.com/PeterJCLaw/SquidgyCMS
PHP | 265 lines | 201 code | 47 blank | 17 comment | 48 complexity | b5f60d6d1ae07c74438c6613baa8a067 MD5 | raw file
  1. <?php
  2. #name = Content
  3. #description = Allows site content to be created and edited
  4. #package = Core - required
  5. #type = content
  6. ###
  7. class AdminContent extends Admin {
  8. function AdminContent() {
  9. parent::__construct('Create or change content chunks made from html and site blocks', -1);
  10. }
  11. function printFormAdmin() {
  12. $chunk_req = empty($_GET['p']) ? False : urlencode($_GET['p']);
  13. $chunks = FileSystem::Filtered_File_List($this->data_root, '.chunk');
  14. natsort($chunks);
  15. if(!empty($chunk_req) && in_array($chunk_req, $chunks)) {
  16. $content = file_get_contents("$this->data_root/$chunk_req.chunk");
  17. $chunk_title = get_GEN_title($chunk_req);
  18. $chunk_id = $chunk_req;
  19. } else {
  20. $content = $chunk_title = "";
  21. $chunk_id = "new";
  22. }
  23. ?>
  24. <input type="hidden" name="chunk_id" id="chunk_id" value="<?php echo $chunk_id; ?>" />
  25. <table><tr>
  26. <th><label for="chunk_title" title="The title of the chunk">Chunk Title:</label></th>
  27. <td><input value="<?php echo $chunk_title; ?>" class="text" name="chunk_title" id="chunk_title" title="The title of the page" /></td>
  28. </tr><tr>
  29. <?php $this->print_select_cells($chunks, $chunk_req); ?>
  30. </tr></table>
  31. <?php
  32. $this->printTextarea($content, array('onkeydown' => 'monitor_changes(this.id);'));
  33. echo "\n <fieldset style=\"display: none;\">";
  34. echo "\n <legend>Live Preview</legend>";
  35. echo "\n <div id=\"content-preview\">", Content::SquidgyParser($content), "</div>";
  36. echo "\n </fieldset>";
  37. return;
  38. }
  39. function submit($content=0) {
  40. list($chunk_title, $chunk_id, $header_link) = array();
  41. extract($_POST, EXTR_IF_EXISTS);
  42. $error = "";
  43. if(empty($content))
  44. $error .= "\nNo content provided";
  45. if(strpos($content, '<?') !== FALSE)
  46. $error .= "\nInvalid content provided: PHP is not allowed";
  47. if(empty($chunk_title))
  48. $error .= "\nNo title provided";
  49. if(!empty($error)) //if there's an error then bail
  50. return $error;
  51. $old_chunk_id = urldecode($chunk_id);
  52. if($chunk_id == 'new') { //if its a new page
  53. $chunk_id = urlencode(get_next_id($this->data_root, ".chunk")."-$chunk_title");
  54. } elseif($this->new_id_needed($old_chunk_id, $chunk_title) && is_writable("$this->data_root/$chunk_id.chunk")) { //if we should and can change stuff
  55. $chunk_id = get_next_id($this->data_root, ".chunk")."-$chunk_title";
  56. $error = $this->change_something_in_all_chunks($old_chunk_id, $chunk_id);
  57. if(!empty($error)) //if there's an error then bail
  58. return $error;
  59. $error = $this->delete_file("$this->data_root/".urlencode($old_chunk_id).".chunk");
  60. $chunk_id = urlencode($chunk_id);
  61. }
  62. log_info('AdminContent', array('old_chunk_id' => $old_chunk_id, 'chunk_id' => $chunk_id));
  63. $header_link = "&p=$chunk_id";
  64. return FileSystem::file_put_contents("$this->data_root/".$chunk_id.".chunk", $content);
  65. }
  66. }
  67. class BlockContent extends Block {
  68. function BlockContent() {
  69. parent::__construct();
  70. }
  71. function replace_one($old, $new, $target) {
  72. $pos = strpos($target, $old);
  73. if($pos === FALSE)
  74. return $target;
  75. $before = substr($target, 0, $pos);
  76. $after = substr($target, $pos+strlen($old));
  77. return $before.$new.$after;
  78. }
  79. function getTemplate($n) {
  80. $Chunk = $this->Chunk($n);
  81. if($Chunk !== FALSE)
  82. return $Chunk;
  83. $themefile = "use the default theme one";
  84. $defaultFile = "Use the default SquidgyCMS one";
  85. foreach(array($themefile. $defaultFile) as $file) {
  86. if(is_readable($file))
  87. return file_get_contents($file);
  88. }
  89. return 'Foo[[Block::Content-Chunk]]GaFoo[[Block::Content-Chunk]]Gamm';
  90. }
  91. function ajax() {
  92. $content = $_REQUEST['content'];
  93. return Content::SquidgyParser($content);
  94. }
  95. function UseTemplate($args) {
  96. $template = $this->getTemplate($args['template']);
  97. if($template == FALSE)
  98. return FALSE;
  99. $chunks = explode(',',$args['chunks']);
  100. foreach($chunks as $chunk) {
  101. $template = $this->replace_one('[[Block::Content-Chunk]]', "[[Block::Content-Chunk::$chunk]]", $template);
  102. }
  103. return $template;
  104. }
  105. function Chunk($args) {
  106. list($chunk) = $args;
  107. $file = "$this->data_root/".$chunk.".chunk";
  108. if(!empty($chunk) && is_readable($file))
  109. return file_get_contents($file);
  110. return FALSE;
  111. }
  112. }
  113. class Content {
  114. /* parses the squidgyCMS block arguments from a string into an array, associative if applicable */
  115. function SquidgyParseArgs($argString) {
  116. if(empty($argString) || ereg('^\s+$', $argString) !== FALSE)
  117. return array();
  118. if( ereg('^\{.*\}$', $argString) !== FALSE )
  119. $argArray = explode('||', substr($argString, 1, -1)); //throw them into an array
  120. else
  121. return array($argString);
  122. if(strpos($argString, ':') !== FALSE) {
  123. foreach($argArray as $arg) {
  124. list($name, $value) = explode(':', $arg);
  125. $argDict[$name] = $value;
  126. }
  127. return $argDict;
  128. } else
  129. return $argArray;
  130. }
  131. /**
  132. * Wrapper for SquidgyParser that allows it to be easily applied to files.
  133. * It also allows for a subsection of the file to be parsed, either by index position or search string.
  134. * @param page_file The file to get the contents from.
  135. * @param start An indicator of the position to begin parsing at.
  136. * @param finish An indicator of the position to stop parsing at.
  137. */
  138. function SquidgyParserFile($page_file, $start = 0, $finish = 0) {
  139. $page = file_get_contents($page_file);
  140. $len = strlen($page);
  141. log_info('Parser', array('start' => $start, 'finish' => $finish));
  142. if(!empty($start) || !empty($finish)) { //then we need to shorten it
  143. if(!empty($start))
  144. $start_pos = strpos($page, $start);
  145. else
  146. $start_pos = -1;
  147. if(!empty($finish))
  148. $finish_pos = strpos($page, $finish);
  149. else
  150. $finish_pos = -1;
  151. log_info('Parser', array('start_pos' => $start_pos, 'finish_pos' => $finish_pos));
  152. if($start_pos >= $len || ($start_pos >= $finish_pos && $finish_pos != -1) || $start_pos === $finish_pos)
  153. return FALSE;
  154. if($finish_pos == -1)
  155. $page = substr($page, (int)$start_pos);
  156. elseif($start_pos == -1)
  157. $page = substr($page, 0, (int)$finish_pos);
  158. else
  159. $page = substr($page, (int)$start_pos, ((int)$finish_pos)-((int)$start_pos));
  160. }
  161. return Content::SquidgyParser($page);
  162. }
  163. /**
  164. * Parses SquidgyML, substituting the markup for the evaluated contents of the blocks.
  165. * @param page A strng containing the markup to parse.
  166. */
  167. function SquidgyParser($page) {
  168. $enabled_modules = Module::list_enabled(true);
  169. $len = strlen($page);
  170. $i = 0;
  171. while(!(strpos($page, '[[Block::') === FALSE || strpos($page, ']]') === FALSE) && $i<$len) { //keep going until you run out of custom bits
  172. $block_call = substr($page, strpos($page, '[[Block::')+9, (strpos($page, ']]') - strpos($page, '[[Block::') - 9) ); //grab the call from the source
  173. list($type) = explode("::", $block_call); //get the type
  174. list($module, $method) = explode("-", $type);
  175. $args = substr($block_call, strlen($type)+2); //grab the arguments
  176. $args = Content::SquidgyParseArgs($args); //turn them into a useful array
  177. log_info('Parser', array('block_call' => $block_call, 'i' => $i, 'type' => $type, 'args' => $args));
  178. $block_html = '';
  179. $module_path = Module::get_path($module);
  180. if($module_path !== FALSE && in_array($module, $enabled_modules)) {
  181. require_once($module_path);
  182. $block = "Block$module";
  183. if(class_exists($block)) {
  184. $block_obj = new $block();
  185. if(method_exists($block_obj, $method))
  186. $block_html = $block_obj->$method($args);
  187. else
  188. log_info("Block '$block' has no method '$method'");
  189. if(empty($block_html))
  190. log_info("Block method '${block}->$method' returned nothing");
  191. } else
  192. log_info("Module '$module' has no block '$block'");
  193. } else
  194. log_info("Module '$module' does not exist or is not enabled");
  195. $page = str_replace("[[Block::$block_call]]", $block_html, $page);
  196. $i+=9;
  197. }
  198. return $page;
  199. }
  200. function edit_URL($id=FALSE) {
  201. if(empty($id))
  202. $id = $GLOBALS['page_id'];
  203. if($id == 'admin' || empty($id))
  204. return FALSE;
  205. return "admin?p=$id#Content";
  206. }
  207. function get_file_from_id($id) {
  208. if($id === FALSE)
  209. return FALSE;
  210. $path = $GLOBALS['data_root'].'/'.$id.'.chunk';
  211. if(is_file($path) && is_readable($path))
  212. return $path;
  213. return FALSE;
  214. }
  215. function get_title_from_id($id) {
  216. return urldecode(substr($id, strpos($id, "-")+1));
  217. }
  218. }