ContentController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Interop\Container\ContainerInterface;
  6. use Typemill\Models\Validation;
  7. use Typemill\Models\Folder;
  8. use Typemill\Models\Write;
  9. use Typemill\Models\WriteCache;
  10. abstract class ContentController
  11. {
  12. # holds the pimple container
  13. protected $c;
  14. # holds the params from request
  15. protected $params;
  16. # holds the slim-uri-object
  17. protected $uri;
  18. # holds the errors to output in frontend
  19. protected $errors = false;
  20. # holds a write object to write files
  21. protected $write;
  22. # holds the structure of content folder as a serialized array of objects
  23. protected $structure;
  24. # holds the name of the structure-file with drafts for author environment
  25. protected $structureDraftName;
  26. # holds the name of the structure-file without drafts for live site
  27. protected $structureLiveName;
  28. # hold the page-item as an object
  29. protected $item;
  30. # hold the breadcrumb as an object
  31. protected $breadcrumb;
  32. # holds the path to the requested file
  33. protected $path = false;
  34. # holds the content of the page
  35. protected $content;
  36. public function __construct(ContainerInterface $c)
  37. {
  38. $this->c = $c;
  39. $this->settings = $this->c->get('settings');
  40. $this->structureLiveName = 'structure.txt';
  41. $this->structureDraftName = 'structure-draft.txt';
  42. }
  43. protected function render($response, $route, $data)
  44. {
  45. if(isset($_SESSION['old']))
  46. {
  47. unset($_SESSION['old']);
  48. }
  49. if($this->c->request->getUri()->getScheme() == 'https')
  50. {
  51. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  52. }
  53. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  54. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  55. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  56. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  57. return $this->c->view->render($response, $route, $data);
  58. }
  59. protected function render404($response, $data = NULL)
  60. {
  61. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  62. }
  63. protected function renderIntern404($response, $data = NULL)
  64. {
  65. return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
  66. }
  67. protected function validateEditorInput()
  68. {
  69. $validate = new Validation();
  70. $vResult = $validate->editorInput($this->params);
  71. if(is_array($vResult))
  72. {
  73. $message = reset($vResult);
  74. $this->errors = ['errors' => $vResult];
  75. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  76. return false;
  77. }
  78. return true;
  79. }
  80. protected function validateBlockInput()
  81. {
  82. $validate = new Validation();
  83. $vResult = $validate->blockInput($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 validateNavigationSort()
  94. {
  95. $validate = new Validation();
  96. $vResult = $validate->navigationSort($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 validateNaviItem()
  107. {
  108. $validate = new Validation();
  109. $vResult = $validate->navigationItem($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 setStructure($draft = false, $cache = true)
  120. {
  121. # set initial structure to false
  122. $structure = false;
  123. # name of structure-file for draft or live
  124. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  125. # set variables and objects
  126. $this->write = new writeCache();
  127. # check, if cached structure is still valid
  128. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  129. {
  130. # get the cached structure
  131. $structure = $this->write->getCache('cache', $filename);
  132. }
  133. # if no structure was found or cache is deactivated
  134. if(!$structure)
  135. {
  136. # scan the content of the folder
  137. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  138. # if there is content, then get the content details
  139. if(count($structure) > 0)
  140. {
  141. # create an array of object with the whole content of the folder
  142. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  143. }
  144. # cache navigation
  145. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  146. }
  147. $this->structure = $structure;
  148. return true;
  149. }
  150. protected function setItem()
  151. {
  152. # if it is the homepage
  153. if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
  154. {
  155. $item = new \stdClass;
  156. $item->elementType = 'folder';
  157. $item->path = '';
  158. $item->urlRel = '/';
  159. }
  160. else
  161. {
  162. # search for the url in the structure
  163. $item = Folder::getItemForUrl($this->structure, $this->params['url']);
  164. }
  165. if($item)
  166. {
  167. if($item->elementType == 'file')
  168. {
  169. $pathParts = explode('.', $item->path);
  170. $fileType = array_pop($pathParts);
  171. $pathWithoutType = implode('.', $pathParts);
  172. $item->pathWithoutType = $pathWithoutType;
  173. }
  174. elseif($item->elementType == 'folder')
  175. {
  176. $item->pathWithoutItem = $item->path;
  177. $item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
  178. $item->pathWithoutType = $item->path;
  179. }
  180. $this->item = $item;
  181. return true;
  182. }
  183. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  184. return false;
  185. }
  186. # determine if you want to write to published file (md) or to draft (txt)
  187. protected function setItemPath($fileType)
  188. {
  189. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  190. }
  191. protected function setPublishStatus()
  192. {
  193. $this->item->published = false;
  194. $this->item->drafted = false;
  195. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  196. {
  197. $this->item->published = true;
  198. # add file-type in case it is a folder
  199. $this->item->fileType = "md";
  200. }
  201. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  202. {
  203. $this->item->drafted = true;
  204. # add file-type in case it is a folder
  205. $this->item->fileType = "txt";
  206. }
  207. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  208. {
  209. # set txt as default for a folder, so that we can create an index.txt for a folder.
  210. $this->item->fileType = "txt";
  211. }
  212. }
  213. protected function deleteContentFiles($fileTypes, $folder = false)
  214. {
  215. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  216. foreach($fileTypes as $fileType)
  217. {
  218. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  219. {
  220. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  221. }
  222. }
  223. if($this->errors)
  224. {
  225. return false;
  226. }
  227. return true;
  228. }
  229. protected function deleteContentFolder()
  230. {
  231. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  232. $path = $basePath . $this->item->pathWithoutItem;
  233. if(file_exists($path))
  234. {
  235. $files = array_diff(scandir($path), array('.', '..'));
  236. # check if there are folders first, then stop the operation
  237. foreach ($files as $file)
  238. {
  239. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  240. {
  241. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  242. }
  243. }
  244. if(!$this->errors)
  245. {
  246. foreach ($files as $file)
  247. {
  248. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  249. }
  250. return rmdir($path);
  251. }
  252. }
  253. return false;
  254. }
  255. protected function setContent()
  256. {
  257. # if the file exists
  258. if($this->item->published OR $this->item->drafted)
  259. {
  260. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  261. if($this->item->fileType == 'txt')
  262. {
  263. # decode the json-draft to an array
  264. $content = json_decode($content);
  265. }
  266. }
  267. elseif($this->item->elementType == "folder")
  268. {
  269. $content = '';
  270. }
  271. else
  272. {
  273. $this->errors = ['errors' => ['message' => 'requested file not found']];
  274. return false;
  275. }
  276. $this->content = $content;
  277. return true;
  278. }
  279. }