PageController.php 7.3 KB

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