PageController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Extensions\ParsedownExtension;
  11. class PageController extends Controller
  12. {
  13. public function index($request, $response, $args)
  14. {
  15. /* Initiate Variables */
  16. $structure = false;
  17. $contentHTML = false;
  18. $item = false;
  19. $breadcrumb = false;
  20. $description = '';
  21. $settings = $this->c->get('settings');
  22. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  23. $cache = new WriteCache();
  24. $uri = $request->getUri();
  25. $base_url = $uri->getBaseUrl();
  26. try
  27. {
  28. if($cache->validate('cache', 'lastCache.txt',600))
  29. {
  30. $structure = $this->getCachedStructure($cache);
  31. $cached = true;
  32. }
  33. else
  34. {
  35. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  36. $cached = false;
  37. if(!$structure)
  38. {
  39. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  40. $this->c->view->render($response, '/index.twig', [ 'content' => $content ]);
  41. }
  42. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  43. {
  44. /* update sitemap */
  45. $sitemap = new WriteSitemap();
  46. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
  47. /* check and update the typemill-version in the user settings */
  48. $this->updateVersion($uri->getBaseUrl());
  49. }
  50. }
  51. }
  52. catch (Exception $e)
  53. {
  54. echo $e->getMessage();
  55. exit(1);
  56. }
  57. /* if the user is on startpage */
  58. if(empty($args))
  59. {
  60. /* check, if there is an index-file in the root of the content folder */
  61. $contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
  62. }
  63. else
  64. {
  65. /* get the request url */
  66. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  67. /* find the url in the content-item-tree and return the item-object for the file */
  68. $item = Folder::getItemForUrl($structure, $urlRel);
  69. /*
  70. if(!$item && $cached)
  71. {
  72. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  73. $item = Folder::getItemForUrl($structure, $urlRel);
  74. }
  75. */
  76. if(!$item)
  77. {
  78. return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
  79. }
  80. /* get breadcrumb for page */
  81. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  82. /* add the paging to the item */
  83. $item = Folder::getPagingForItem($structure, $item);
  84. /* check if url is a folder. If so, check if there is an index-file for the folder */
  85. if($item->elementType == 'folder' && $item->index)
  86. {
  87. $filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
  88. }
  89. elseif($item->elementType == 'file')
  90. {
  91. $filePath = $pathToContent . $item->path;
  92. }
  93. /* read the content of the file */
  94. $contentMD = isset($filePath) ? file_get_contents($filePath) : false;
  95. }
  96. /* initialize parsedown */
  97. $Parsedown = new ParsedownExtension();
  98. /* parse markdown-file to html-string */
  99. $contentHTML = $Parsedown->text($contentMD);
  100. $excerpt = substr($contentHTML,0,200);
  101. $excerpt = explode("</h1>", $excerpt);
  102. $title = isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
  103. $description = isset($excerpt[1]) ? strip_tags($excerpt[1]) : false;
  104. $description = $description ? trim(preg_replace('/\s+/', ' ', $description)) : false;
  105. /*
  106. $timer['topiccontroller']=microtime(true);
  107. $timer['end topiccontroller']=microtime(true);
  108. Helpers::printTimer($timer);
  109. */
  110. $route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
  111. $this->c->view->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'title' => $title, 'description' => $description, 'base_url' => $base_url ));
  112. }
  113. protected function getCachedStructure($cache)
  114. {
  115. return $cache->getCache('cache', 'structure.txt');
  116. }
  117. protected function getFreshStructure($pathToContent, $cache, $uri)
  118. {
  119. /* scan the content of the folder */
  120. $structure = Folder::scanFolder($pathToContent);
  121. /* if there is no content, render an empty page */
  122. if(count($structure) == 0)
  123. {
  124. return false;
  125. }
  126. /* create an array of object with the whole content of the folder */
  127. $structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
  128. /* cache navigation */
  129. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  130. return $structure;
  131. }
  132. protected function updateVersion($baseUrl)
  133. {
  134. /* check the latest public typemill version */
  135. $version = new VersionCheck();
  136. $latestVersion = $version->checkVersion($baseUrl);
  137. if($latestVersion)
  138. {
  139. /* check, if user-settings exist */
  140. $yaml = new WriteYaml();
  141. $userSettings = $yaml->getYaml('settings', 'settings.yaml');
  142. if($userSettings)
  143. {
  144. /* if there is no version info in the settings or if the version info is outdated */
  145. if(!isset($userSettings['latestVersion']) || $userSettings['latestVersion'] != $latestVersion)
  146. {
  147. /* write the latest version into the user-settings */
  148. $userSettings['latestVersion'] = $latestVersion;
  149. $yaml->updateYaml('settings', 'settings.yaml', $userSettings);
  150. }
  151. }
  152. }
  153. }
  154. }