PageController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $cached = true;
  39. }
  40. else
  41. {
  42. /* if not, get a fresh structure of the content folder */
  43. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  44. $cached = false;
  45. /* if there is no structure at all, the content folder is probably empty */
  46. if(!$structure)
  47. {
  48. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  49. $this->render($response, '/index.twig', array( 'content' => $content ));
  50. }
  51. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  52. {
  53. /* update sitemap */
  54. $sitemap = new WriteSitemap();
  55. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
  56. /* check and update the typemill-version in the user settings */
  57. $this->updateVersion($uri->getBaseUrl());
  58. }
  59. }
  60. /* dispatch event and let others manipulate the structure */
  61. $structure = $this->c->dispatcher->dispatch('onPagetreeLoaded', new OnPagetreeLoaded($structure))->getData();
  62. }
  63. catch (Exception $e)
  64. {
  65. echo $e->getMessage();
  66. exit(1);
  67. }
  68. /* if the user is on startpage */
  69. if(empty($args))
  70. {
  71. /* check, if there is an index-file in the root of the content folder */
  72. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  73. }
  74. else
  75. {
  76. /* get the request url */
  77. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  78. /* find the url in the content-item-tree and return the item-object for the file */
  79. $item = Folder::getItemForUrl($structure, $urlRel);
  80. /* if structure is cached and there is no item
  81. if($cached && !$item)
  82. {
  83. /* get a fresh structure and search for the item again
  84. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  85. $item = Folder::getItemForUrl($structure, $urlRel);
  86. }
  87. /* if there is still no item, return a 404-page */
  88. if(!$item)
  89. {
  90. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  91. }
  92. /* get breadcrumb for page */
  93. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  94. $breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
  95. /* add the paging to the item */
  96. $item = Folder::getPagingForItem($structure, $item);
  97. $item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
  98. /* check if url is a folder. If so, check if there is an index-file in that folder */
  99. if($item->elementType == 'folder' && $item->index)
  100. {
  101. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  102. }
  103. elseif($item->elementType == 'file')
  104. {
  105. $filePath = $pathToContent . $item->path;
  106. }
  107. /* read the content of the file */
  108. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  109. }
  110. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
  111. /* initialize parsedown */
  112. $parsedown = new ParsedownExtension();
  113. /* parse markdown-file to content-array */
  114. $contentArray = $parsedown->text($contentMD);
  115. $contentArray = $this->c->dispatcher->dispatch('onContentArrayLoaded', new OnContentArrayLoaded($contentArray))->getData();
  116. /* get the first image from content array */
  117. $firstImage = $this->getFirstImage($contentArray);
  118. /* parse markdown-content-array to content-string */
  119. $contentHTML = $parsedown->markup($contentArray);
  120. $contentHTML = $this->c->dispatcher->dispatch('onHtmlLoaded', new OnHtmlLoaded($contentHTML))->getData();
  121. /* create excerpt from content */
  122. $excerpt = substr($contentHTML,0,320);
  123. $excerpt = explode("</h1>", $excerpt);
  124. /* extract title from excerpt */
  125. $title = isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
  126. /* create description from excerpt */
  127. $description = isset($excerpt[1]) ? strip_tags($excerpt[1]) : false;
  128. if($description)
  129. {
  130. $description = trim(preg_replace('/\s+/', ' ', $description));
  131. $lastSpace = strrpos($description, ' ');
  132. $description = substr($description, 0, $lastSpace);
  133. }
  134. /* get url and alt-tag for first image, if exists */
  135. if($firstImage)
  136. {
  137. preg_match('#\((.*?)\)#', $firstImage, $img_url);
  138. if($img_url[1])
  139. {
  140. preg_match('#\[(.*?)\]#', $firstImage, $img_alt);
  141. $firstImage = array('img_url' => $base_url . $img_url[1], 'img_alt' => $img_alt[1]);
  142. }
  143. }
  144. /*
  145. $timer['topiccontroller']=microtime(true);
  146. $timer['end topiccontroller']=microtime(true);
  147. Helpers::printTimer($timer);
  148. */
  149. $route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
  150. $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 ));
  151. }
  152. protected function getCachedStructure($cache)
  153. {
  154. return $cache->getCache('cache', 'structure.txt');
  155. }
  156. protected function getFreshStructure($pathToContent, $cache, $uri)
  157. {
  158. /* scan the content of the folder */
  159. $structure = Folder::scanFolder($pathToContent);
  160. /* if there is no content, render an empty page */
  161. if(count($structure) == 0)
  162. {
  163. return false;
  164. }
  165. /* create an array of object with the whole content of the folder */
  166. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  167. /* cache navigation */
  168. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  169. return $structure;
  170. }
  171. protected function updateVersion($baseUrl)
  172. {
  173. /* check the latest public typemill version */
  174. $version = new VersionCheck();
  175. $latestVersion = $version->checkVersion($baseUrl);
  176. if($latestVersion)
  177. {
  178. /* check, if user-settings exist */
  179. $yaml = new WriteYaml();
  180. $userSettings = $yaml->getYaml('settings', 'settings.yaml');
  181. if($userSettings)
  182. {
  183. /* if there is no version info in the settings or if the version info is outdated */
  184. if(!isset($userSettings['latestVersion']) || $userSettings['latestVersion'] != $latestVersion)
  185. {
  186. /* write the latest version into the user-settings */
  187. $userSettings['latestVersion'] = $latestVersion;
  188. $yaml->updateYaml('settings', 'settings.yaml', $userSettings);
  189. }
  190. }
  191. }
  192. }
  193. protected function getFirstImage(array $contentBlocks)
  194. {
  195. foreach($contentBlocks as $block)
  196. {
  197. if($block['element']['name'] == 'p' && substr($block['element']['text'], 0, 2) == '![' )
  198. {
  199. return $block['element']['text'];
  200. }
  201. }
  202. return false;
  203. }
  204. }