PageController.php 4.7 KB

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