PageController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Typemill\Models\Folder;
  4. use Typemill\Models\WriteCache;
  5. use Typemill\Models\WriteSitemap;
  6. use Typemill\Models\WriteYaml;
  7. use \Symfony\Component\Yaml\Yaml;
  8. use Typemill\Models\VersionCheck;
  9. use Typemill\Models\Helpers;
  10. use Typemill\Models\Markdown;
  11. use Typemill\Events\OnPagetreeLoaded;
  12. use Typemill\Events\OnBreadcrumbLoaded;
  13. use Typemill\Events\OnItemLoaded;
  14. use Typemill\Events\OnMarkdownLoaded;
  15. use Typemill\Events\OnContentArrayLoaded;
  16. use Typemill\Events\OnHtmlLoaded;
  17. use Typemill\Extensions\ParsedownExtension;
  18. class PageController extends Controller
  19. {
  20. public function index($request, $response, $args)
  21. {
  22. /* Initiate Variables */
  23. $structure = false;
  24. $contentHTML = false;
  25. $item = false;
  26. $breadcrumb = false;
  27. $description = '';
  28. $settings = $this->c->get('settings');
  29. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  30. $cache = new WriteCache();
  31. $uri = $request->getUri();
  32. $base_url = $uri->getBaseUrl();
  33. try
  34. {
  35. /* if the cached structure is still valid, use it */
  36. if($cache->validate('cache', 'lastCache.txt',600))
  37. {
  38. $structure = $this->getCachedStructure($cache);
  39. }
  40. else
  41. {
  42. /* if not, get a fresh structure of the content folder */
  43. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  44. /* if there is no structure at all, the content folder is probably empty */
  45. if(!$structure)
  46. {
  47. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  48. return $this->render($response, '/index.twig', array( 'content' => $content ));
  49. }
  50. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  51. {
  52. /* update sitemap */
  53. $sitemap = new WriteSitemap();
  54. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
  55. /* check and update the typemill-version in the user settings */
  56. $this->updateVersion($uri->getBaseUrl());
  57. }
  58. }
  59. /* dispatch event and let others manipulate the structure */
  60. $structure = $this->c->dispatcher->dispatch('onPagetreeLoaded', new OnPagetreeLoaded($structure))->getData();
  61. }
  62. catch (Exception $e)
  63. {
  64. echo $e->getMessage();
  65. exit(1);
  66. }
  67. /* if the user is on startpage */
  68. if(empty($args))
  69. {
  70. /* check, if there is an index-file in the root of the content folder */
  71. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  72. }
  73. else
  74. {
  75. /* get the request url */
  76. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  77. /* find the url in the content-item-tree and return the item-object for the file */
  78. $item = Folder::getItemForUrl($structure, $urlRel);
  79. /* if there is still no item, return a 404-page */
  80. if(!$item)
  81. {
  82. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  83. }
  84. /* get breadcrumb for page */
  85. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  86. $breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
  87. /* add the paging to the item */
  88. $item = Folder::getPagingForItem($structure, $item);
  89. $item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
  90. /* check if url is a folder. If so, check if there is an index-file in that folder */
  91. if($item->elementType == 'folder' && $item->index)
  92. {
  93. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  94. }
  95. elseif($item->elementType == 'file')
  96. {
  97. $filePath = $pathToContent . $item->path;
  98. }
  99. /* add the modified date for the file */
  100. $item->modified = isset($filePath) ? filemtime($filePath) : false;
  101. /* read the content of the file */
  102. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  103. }
  104. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
  105. /* initialize parsedown */
  106. $parsedown = new ParsedownExtension();
  107. /* set safe mode to escape javascript and html in markdown */
  108. $parsedown->setSafeMode(true);
  109. /* parse markdown-file to content-array */
  110. $contentArray = $parsedown->text($contentMD);
  111. $contentArray = $this->c->dispatcher->dispatch('onContentArrayLoaded', new OnContentArrayLoaded($contentArray))->getData();
  112. /* get the first image from content array */
  113. $firstImage = $this->getFirstImage($contentArray);
  114. /* parse markdown-content-array to content-string */
  115. $contentHTML = $parsedown->markup($contentArray);
  116. $contentHTML = $this->c->dispatcher->dispatch('onHtmlLoaded', new OnHtmlLoaded($contentHTML))->getData();
  117. /* extract the h1 headline*/
  118. $contentParts = explode("</h1>", $contentHTML);
  119. $title = isset($contentParts[0]) ? strip_tags($contentParts[0]) : $settings['title'];
  120. $contentHTML = isset($contentParts[1]) ? $contentParts[1] : $contentHTML;
  121. /* create excerpt from content */
  122. $excerpt = substr($contentHTML,0,500);
  123. /* create description from excerpt */
  124. $description = isset($excerpt) ? strip_tags($excerpt) : false;
  125. if($description)
  126. {
  127. $description = trim(preg_replace('/\s+/', ' ', $description));
  128. $description = substr($description, 0, 300);
  129. $lastSpace = strrpos($description, ' ');
  130. $description = substr($description, 0, $lastSpace);
  131. }
  132. /* get url and alt-tag for first image, if exists */
  133. if($firstImage)
  134. {
  135. preg_match('#\((.*?)\)#', $firstImage, $img_url);
  136. if($img_url[1])
  137. {
  138. preg_match('#\[(.*?)\]#', $firstImage, $img_alt);
  139. $firstImage = array('img_url' => $base_url . $img_url[1], 'img_alt' => $img_alt[1]);
  140. }
  141. }
  142. $route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
  143. return $this->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'title' => $title, 'description' => $description, 'base_url' => $base_url, 'image' => $firstImage ));
  144. }
  145. protected function getCachedStructure($cache)
  146. {
  147. return $cache->getCache('cache', 'structure.txt');
  148. }
  149. protected function getFreshStructure($pathToContent, $cache, $uri)
  150. {
  151. /* scan the content of the folder */
  152. $structure = Folder::scanFolder($pathToContent);
  153. /* if there is no content, render an empty page */
  154. if(count($structure) == 0)
  155. {
  156. return false;
  157. }
  158. /* create an array of object with the whole content of the folder */
  159. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  160. /* cache navigation */
  161. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  162. return $structure;
  163. }
  164. protected function updateVersion($baseUrl)
  165. {
  166. /* check the latest public typemill version */
  167. $version = new VersionCheck();
  168. $latestVersion = $version->checkVersion($baseUrl);
  169. if($latestVersion)
  170. {
  171. /* store latest version */
  172. \Typemill\Settings::updateSettings(array('latestVersion' => $latestVersion));
  173. }
  174. }
  175. protected function getFirstImage(array $contentBlocks)
  176. {
  177. foreach($contentBlocks as $block)
  178. {
  179. /* is it a paragraph? */
  180. if(isset($block['element']['name']) && $block['element']['name'] == 'p')
  181. {
  182. if(isset($block['element']['handler']['argument']) && substr($block['element']['handler']['argument'], 0, 2) == '![' )
  183. {
  184. return $block['element']['handler']['argument'];
  185. }
  186. }
  187. }
  188. return false;
  189. }
  190. }