PageController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 Typemill\Models\WriteMeta;
  8. use \Symfony\Component\Yaml\Yaml;
  9. use Typemill\Models\VersionCheck;
  10. use Typemill\Models\Helpers;
  11. use Typemill\Models\Markdown;
  12. use Typemill\Events\OnPagetreeLoaded;
  13. use Typemill\Events\OnBreadcrumbLoaded;
  14. use Typemill\Events\OnItemLoaded;
  15. use Typemill\Events\OnOriginalLoaded;
  16. use Typemill\Events\OnMetaLoaded;
  17. use Typemill\Events\OnMarkdownLoaded;
  18. use Typemill\Events\OnContentArrayLoaded;
  19. use Typemill\Events\OnHtmlLoaded;
  20. use Typemill\Extensions\ParsedownExtension;
  21. class PageController extends Controller
  22. {
  23. public function index($request, $response, $args)
  24. {
  25. /* Initiate Variables */
  26. $structure = false;
  27. $contentHTML = false;
  28. $item = false;
  29. $home = false;
  30. $breadcrumb = false;
  31. $settings = $this->c->get('settings');
  32. $pathToContent = $settings['rootPath'] . $settings['contentFolder'];
  33. $cache = new WriteCache();
  34. $uri = $request->getUri();
  35. $base_url = $uri->getBaseUrl();
  36. try
  37. {
  38. /* if the cached structure is still valid, use it */
  39. if($cache->validate('cache', 'lastCache.txt', 600))
  40. {
  41. $structure = $this->getCachedStructure($cache);
  42. }
  43. if(!isset($structure) OR !$structure)
  44. {
  45. /* if not, get a fresh structure of the content folder */
  46. $structure = $this->getFreshStructure($pathToContent, $cache, $uri);
  47. /* if there is no structure at all, the content folder is probably empty */
  48. if(!$structure)
  49. {
  50. $content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
  51. return $this->render($response, '/index.twig', array( 'content' => $content ));
  52. }
  53. elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
  54. {
  55. /* update sitemap */
  56. $sitemap = new WriteSitemap();
  57. $sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $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. # get the cached navigation here (structure without hidden files )
  69. $navigation = $cache->getCache('cache', 'navigation.txt');
  70. if(!$navigation)
  71. {
  72. # use the structure as navigation if there is no difference
  73. $navigation = $structure;
  74. }
  75. # if the user is on startpage
  76. if(empty($args))
  77. {
  78. $home = true;
  79. $item = Folder::getItemForUrl($navigation, $uri->getBasePath(), $uri->getBasePath());
  80. $urlRel = $uri->getBasePath();
  81. }
  82. else
  83. {
  84. /* get the request url */
  85. $urlRel = $uri->getBasePath() . '/' . $args['params'];
  86. /* find the url in the content-item-tree and return the item-object for the file */
  87. $item = Folder::getItemForUrl($structure, $urlRel, $uri->getBasePath());
  88. /* if there is still no item, return a 404-page */
  89. if(!$item)
  90. {
  91. return $this->render404($response, array( 'navigation' => $navigation, 'settings' => $settings, 'base_url' => $base_url ));
  92. }
  93. /* get breadcrumb for page */
  94. $breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
  95. $breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
  96. # set pages active for navigation again
  97. Folder::getBreadcrumb($structure, $item->keyPathArray);
  98. /* add the paging to the item */
  99. $item = Folder::getPagingForItem($navigation, $item);
  100. }
  101. # dispatch the item
  102. $item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
  103. # set the filepath
  104. $filePath = $pathToContent . $item->path;
  105. # check if url is a folder and add index.md
  106. if($item->elementType == 'folder')
  107. {
  108. $filePath = $filePath . DIRECTORY_SEPARATOR . 'index.md';
  109. # use navigation instead of structure to get
  110. $item = Folder::getItemForUrl($navigation, $urlRel, $uri->getBasePath());
  111. }
  112. # read the content of the file
  113. $contentMD = file_exists($filePath) ? file_get_contents($filePath) : false;
  114. # dispatch the original content without plugin-manipulations for case anyone wants to use it
  115. $this->c->dispatcher->dispatch('onOriginalLoaded', new OnOriginalLoaded($contentMD));
  116. # get meta-Information
  117. $writeMeta = new WriteMeta();
  118. # makes sure that you always have the full meta with title, description and all the rest.
  119. $metatabs = $writeMeta->completePageMeta($contentMD, $settings, $item);
  120. # dispatch meta
  121. $metatabs = $this->c->dispatcher->dispatch('onMetaLoaded', new OnMetaLoaded($metatabs))->getData();
  122. # dispatch content
  123. $contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
  124. $itemUrl = isset($item->urlRel) ? $item->urlRel : false;
  125. /* initialize parsedown */
  126. $parsedown = new ParsedownExtension($settings['headlineanchors']);
  127. /* set safe mode to escape javascript and html in markdown */
  128. $parsedown->setSafeMode(true);
  129. /* parse markdown-file to content-array */
  130. $contentArray = $parsedown->text($contentMD, $itemUrl);
  131. $contentArray = $this->c->dispatcher->dispatch('onContentArrayLoaded', new OnContentArrayLoaded($contentArray))->getData();
  132. /* get the first image from content array */
  133. $firstImage = $this->getFirstImage($contentArray);
  134. /* parse markdown-content-array to content-string */
  135. $contentHTML = $parsedown->markup($contentArray, $itemUrl);
  136. $contentHTML = $this->c->dispatcher->dispatch('onHtmlLoaded', new OnHtmlLoaded($contentHTML))->getData();
  137. /* extract the h1 headline*/
  138. $contentParts = explode("</h1>", $contentHTML);
  139. $title = isset($contentParts[0]) ? strip_tags($contentParts[0]) : $settings['title'];
  140. $contentHTML = isset($contentParts[1]) ? $contentParts[1] : $contentHTML;
  141. /* get url and alt-tag for first image, if exists */
  142. if($firstImage)
  143. {
  144. preg_match('#\((.*?)\)#', $firstImage, $img_url);
  145. if($img_url[1])
  146. {
  147. preg_match('#\[(.*?)\]#', $firstImage, $img_alt);
  148. $firstImage = array('img_url' => $base_url . '/' . $img_url[1], 'img_alt' => $img_alt[1]);
  149. }
  150. }
  151. $theme = $settings['theme'];
  152. $route = empty($args) && isset($settings['themes'][$theme]['cover']) ? '/cover.twig' : '/index.twig';
  153. # check if there is a custom theme css
  154. $customcss = $writeMeta->checkFile('cache', $theme . '-custom.css');
  155. if($customcss)
  156. {
  157. $this->c->assets->addCSS($base_url . '/cache/' . $theme . '-custom.css');
  158. }
  159. $logo = false;
  160. if(isset($settings['logo']) && $settings['logo'] != '')
  161. {
  162. $logo = 'media/files/' . $settings['logo'];
  163. }
  164. $favicon = false;
  165. if(isset($settings['favicon']) && $settings['favicon'] != '')
  166. {
  167. $favicon = true;
  168. }
  169. return $this->render($response, $route, [
  170. 'home' => $home,
  171. 'navigation' => $navigation,
  172. 'title' => $title,
  173. 'content' => $contentHTML,
  174. 'item' => $item,
  175. 'breadcrumb' => $breadcrumb,
  176. 'settings' => $settings,
  177. 'metatabs' => $metatabs,
  178. 'base_url' => $base_url,
  179. 'image' => $firstImage,
  180. 'logo' => $logo,
  181. 'favicon' => $favicon
  182. ]);
  183. }
  184. protected function getCachedStructure($cache)
  185. {
  186. return $cache->getCache('cache', 'structure.txt');
  187. }
  188. protected function getFreshStructure($pathToContent, $cache, $uri)
  189. {
  190. /* scan the content of the folder */
  191. $structure = Folder::scanFolder($pathToContent);
  192. /* if there is no content, render an empty page */
  193. if(count($structure) == 0)
  194. {
  195. return false;
  196. }
  197. # get the extended structure files with changes like navigation title or hidden pages
  198. $yaml = new writeYaml();
  199. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  200. # create an array of object with the whole content of the folder
  201. $structure = Folder::getFolderContentDetails($structure, $extended, $uri->getBaseUrl(), $uri->getBasePath());
  202. # cache structure
  203. $cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
  204. if($extended && $this->containsHiddenPages($extended))
  205. {
  206. # generate the navigation (delete empty pages)
  207. $navigation = $this->createNavigationFromStructure($structure);
  208. # cache navigation
  209. $cache->updateCache('cache', 'navigation.txt', false, $navigation);
  210. }
  211. else
  212. {
  213. # make sure no separate navigation file is set
  214. $cache->deleteFileWithPath('cache' . DIRECTORY_SEPARATOR . 'navigation.txt');
  215. }
  216. # load and return the cached structure, because might be manipulated with navigation....
  217. return $this->getCachedStructure($cache);
  218. }
  219. protected function containsHiddenPages($extended)
  220. {
  221. foreach($extended as $element)
  222. {
  223. if(isset($element['hide']) && $element['hide'] === true)
  224. {
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. protected function createNavigationFromStructure($navigation)
  231. {
  232. foreach ($navigation as $key => $element)
  233. {
  234. if($element->hide === true)
  235. {
  236. unset($navigation[$key]);
  237. }
  238. elseif(isset($element->folderContent))
  239. {
  240. $navigation[$key]->folderContent = $this->createNavigationFromStructure($element->folderContent);
  241. }
  242. }
  243. return $navigation;
  244. }
  245. # not in use, stored the latest version in user settings, but that does not make sense because checkd on the fly with api in admin
  246. protected function updateVersion($baseUrl)
  247. {
  248. /* check the latest public typemill version */
  249. $version = new VersionCheck();
  250. $latestVersion = $version->checkVersion($baseUrl);
  251. if($latestVersion)
  252. {
  253. /* store latest version */
  254. \Typemill\Settings::updateSettings(array('latestVersion' => $latestVersion));
  255. }
  256. }
  257. protected function getFirstImage(array $contentBlocks)
  258. {
  259. foreach($contentBlocks as $block)
  260. {
  261. /* is it a paragraph? */
  262. if(isset($block['name']) && $block['name'] == 'p')
  263. {
  264. if(isset($block['handler']['argument']) && substr($block['handler']['argument'], 0, 2) == '![' )
  265. {
  266. return $block['handler']['argument'];
  267. }
  268. }
  269. }
  270. return false;
  271. }
  272. }