ContentController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Psr\Container\ContainerInterface;
  6. use Typemill\Models\Validation;
  7. use Typemill\Models\Folder;
  8. use Typemill\Models\Write;
  9. use Typemill\Models\WriteCache;
  10. use Typemill\Models\WriteYaml;
  11. abstract class ContentController
  12. {
  13. # holds the pimple container
  14. protected $c;
  15. # holds the params from request
  16. protected $params;
  17. # holds the slim-uri-object
  18. protected $uri;
  19. # holds the errors to output in frontend
  20. protected $errors = false;
  21. # holds a write object to write files
  22. protected $write;
  23. # holds the structure of content folder as a serialized array of objects
  24. protected $structure;
  25. # holds the name of the structure-file with drafts for author environment
  26. protected $structureDraftName;
  27. # holds the name of the structure-file without drafts for live site
  28. protected $structureLiveName;
  29. # holds informations about the homepage
  30. protected $homepage;
  31. # hold the page-item as an object
  32. protected $item;
  33. # hold the breadcrumb as an object
  34. protected $breadcrumb;
  35. # holds the path to the requested file
  36. protected $path = false;
  37. # holds the content of the page
  38. protected $content;
  39. public function __construct(ContainerInterface $c)
  40. {
  41. $this->c = $c;
  42. $this->settings = $this->c->get('settings');
  43. $this->structureLiveName = 'structure.txt';
  44. $this->structureDraftName = 'structure-draft.txt';
  45. }
  46. # admin ui rendering
  47. protected function render($response, $route, $data)
  48. {
  49. if(isset($_SESSION['old']))
  50. {
  51. unset($_SESSION['old']);
  52. }
  53. $response = $response->withoutHeader('Server');
  54. $response = $response->withoutHeader('X-Powered-By');
  55. if($this->c->request->getUri()->getScheme() == 'https')
  56. {
  57. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  58. }
  59. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  60. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  61. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  62. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  63. return $this->c->view->render($response, $route, $data);
  64. }
  65. protected function render404($response, $data = NULL)
  66. {
  67. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  68. }
  69. protected function renderIntern404($response, $data = NULL)
  70. {
  71. return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
  72. }
  73. protected function getValidator()
  74. {
  75. return new Validation();
  76. }
  77. protected function validateEditorInput()
  78. {
  79. $validate = new Validation();
  80. $vResult = $validate->editorInput($this->params);
  81. if(is_array($vResult))
  82. {
  83. $message = reset($vResult);
  84. $this->errors = ['errors' => $vResult];
  85. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  86. return false;
  87. }
  88. return true;
  89. }
  90. protected function validateBlockInput()
  91. {
  92. $validate = new Validation();
  93. $vResult = $validate->blockInput($this->params);
  94. if(is_array($vResult))
  95. {
  96. $message = reset($vResult);
  97. $this->errors = ['errors' => $vResult];
  98. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  99. return false;
  100. }
  101. return true;
  102. }
  103. protected function validateNavigationSort()
  104. {
  105. $validate = new Validation();
  106. $vResult = $validate->navigationSort($this->params);
  107. if(is_array($vResult))
  108. {
  109. $message = reset($vResult);
  110. $this->errors = ['errors' => $vResult];
  111. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  112. return false;
  113. }
  114. return true;
  115. }
  116. protected function validateNaviItem()
  117. {
  118. $validate = new Validation();
  119. $vResult = $validate->navigationItem($this->params);
  120. if(is_array($vResult))
  121. {
  122. $message = reset($vResult);
  123. $this->errors = ['errors' => $vResult];
  124. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  125. return false;
  126. }
  127. return true;
  128. }
  129. protected function validateBaseNaviItem()
  130. {
  131. $validate = new Validation();
  132. $vResult = $validate->navigationBaseItem($this->params);
  133. if(is_array($vResult))
  134. {
  135. $message = reset($vResult);
  136. $this->errors = ['errors' => $vResult];
  137. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  138. return false;
  139. }
  140. return true;
  141. }
  142. protected function setStructure($draft = false, $cache = true)
  143. {
  144. # set initial structure to false
  145. $structure = false;
  146. # name of structure-file for draft or live
  147. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  148. # set variables and objects
  149. $this->write = new writeCache();
  150. # check, if cached structure is still valid
  151. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  152. {
  153. # get the cached structure
  154. $structure = $this->write->getCache('cache', $filename);
  155. }
  156. # if no structure was found or cache is deactivated
  157. if(!$structure)
  158. {
  159. # scan the content of the folder
  160. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  161. # if there is content, then get the content details
  162. if(count($structure) > 0)
  163. {
  164. # get the extended structure files with changes like navigation title or hidden pages
  165. $yaml = new writeYaml();
  166. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  167. # create an array of object with the whole content of the folder and changes from extended file
  168. $structure = Folder::getFolderContentDetails($structure, $extended, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  169. }
  170. # cache navigation
  171. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  172. }
  173. $this->structure = $structure;
  174. return true;
  175. }
  176. protected function renameExtended($item, $newFolder)
  177. {
  178. # get the extended structure files with changes like navigation title or hidden pages
  179. $yaml = new writeYaml();
  180. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  181. if(isset($extended[$item->urlRelWoF]))
  182. {
  183. $newUrl = $newFolder->urlRelWoF . '/' . $item->slug;
  184. $entry = $extended[$item->urlRelWoF];
  185. unset($extended[$item->urlRelWoF]);
  186. $extended[$newUrl] = $entry;
  187. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  188. }
  189. return true;
  190. }
  191. protected function deleteFromExtended()
  192. {
  193. # get the extended structure files with changes like navigation title or hidden pages
  194. $yaml = new writeYaml();
  195. $extended = $yaml->getYaml('cache', 'structure-extended.yaml');
  196. if($this->item->elementType == "file" && isset($extended[$this->item->urlRelWoF]))
  197. {
  198. unset($extended[$this->item->urlRelWoF]);
  199. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  200. }
  201. if($this->item->elementType == "folder")
  202. {
  203. $changed = false;
  204. # delete all entries with that folder url
  205. foreach($extended as $url => $entries)
  206. {
  207. if( strpos($url, $this->item->urlRelWoF) !== false )
  208. {
  209. $changed = true;
  210. unset($extended[$url]);
  211. }
  212. }
  213. if($changed)
  214. {
  215. $yaml->updateYaml('cache', 'structure-extended.yaml', $extended);
  216. }
  217. }
  218. }
  219. protected function setHomepage()
  220. {
  221. $contentFolder = Folder::scanFolderFlat($this->settings['rootPath'] . $this->settings['contentFolder']);
  222. if(in_array('index.md', $contentFolder))
  223. {
  224. $md = true;
  225. $status = 'published';
  226. }
  227. if(in_array('index.txt', $contentFolder))
  228. {
  229. $txt = true;
  230. $status = 'unpublished';
  231. }
  232. if(isset($txt) && isset($md))
  233. {
  234. $status = 'modified';
  235. }
  236. $active = false;
  237. if($this->params['url'] == '/' || $this->params['url'] == $this->uri->getBasePath() )
  238. {
  239. $active = 'active';
  240. }
  241. $this->homepage = ['status' => $status, 'active' => $active];
  242. }
  243. protected function setItem()
  244. {
  245. # search for the url in the structure
  246. $item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBasePath());
  247. if($item)
  248. {
  249. $this->item = $item;
  250. return true;
  251. }
  252. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  253. return false;
  254. }
  255. # determine if you want to write to published file (md) or to draft (txt)
  256. protected function setItemPath($fileType)
  257. {
  258. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  259. }
  260. protected function setPublishStatus()
  261. {
  262. $this->item->published = false;
  263. $this->item->drafted = false;
  264. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  265. {
  266. $this->item->published = true;
  267. # add file-type in case it is a folder
  268. $this->item->fileType = "md";
  269. }
  270. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  271. {
  272. $this->item->drafted = true;
  273. # add file-type in case it is a folder
  274. $this->item->fileType = "txt";
  275. }
  276. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  277. {
  278. # set txt as default for a folder, so that we can create an index.txt for a folder.
  279. $this->item->fileType = "txt";
  280. }
  281. }
  282. protected function deleteContentFiles($fileTypes, $folder = false)
  283. {
  284. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  285. foreach($fileTypes as $fileType)
  286. {
  287. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  288. {
  289. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  290. }
  291. }
  292. if($this->errors)
  293. {
  294. return false;
  295. }
  296. return true;
  297. }
  298. protected function deleteContentFolder()
  299. {
  300. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  301. $path = $basePath . $this->item->path;
  302. if(file_exists($path))
  303. {
  304. $files = array_diff(scandir($path), array('.', '..'));
  305. # check if there are published pages or folders inside, then stop the operation
  306. foreach ($files as $file)
  307. {
  308. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  309. {
  310. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  311. }
  312. if(substr($file, -3) == '.md' && $file != 'index.md')
  313. {
  314. $this->errors = ['message' => 'Please unpublish all pages in the folder first.'];
  315. }
  316. }
  317. if(!$this->errors)
  318. {
  319. foreach ($files as $file)
  320. {
  321. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  322. }
  323. return rmdir($path);
  324. }
  325. # delete all files from the extended file
  326. $this->deleteFromExtended();
  327. }
  328. return false;
  329. }
  330. protected function setContent()
  331. {
  332. # if the file exists
  333. if($this->item->published OR $this->item->drafted)
  334. {
  335. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  336. if($this->item->fileType == 'txt')
  337. {
  338. # decode the json-draft to an array
  339. $content = json_decode($content);
  340. }
  341. }
  342. elseif($this->item->elementType == "folder")
  343. {
  344. $content = '';
  345. }
  346. else
  347. {
  348. $this->errors = ['errors' => ['message' => 'requested file not found']];
  349. return false;
  350. }
  351. $this->content = $content;
  352. return true;
  353. }
  354. }