ContentController.php 12 KB

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