ContentController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. $this->errors = ['errors' => $vResult];
  74. return false;
  75. }
  76. return true;
  77. }
  78. protected function validateNavigationSort()
  79. {
  80. $validate = new Validation();
  81. $vResult = $validate->navigationSort($this->params);
  82. if(is_array($vResult))
  83. {
  84. $this->errors = ['errors' => $vResult];
  85. return false;
  86. }
  87. return true;
  88. }
  89. protected function validateNaviItem()
  90. {
  91. $validate = new Validation();
  92. $vResult = $validate->navigationItem($this->params);
  93. if(is_array($vResult))
  94. {
  95. $this->errors = ['errors' => $vResult];
  96. return false;
  97. }
  98. return true;
  99. }
  100. protected function setStructure($draft = false, $cache = true)
  101. {
  102. # set initial structure to false
  103. $structure = false;
  104. # name of structure-file for draft or live
  105. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  106. # set variables and objects
  107. $this->write = new writeCache();
  108. # check, if cached structure is still valid
  109. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  110. {
  111. # get the cached structure
  112. $structure = $this->write->getCache('cache', $filename);
  113. }
  114. # if no structure was found or cache is deactivated
  115. if(!$structure)
  116. {
  117. # scan the content of the folder
  118. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  119. # if there is content, then get the content details
  120. if(count($structure) > 0)
  121. {
  122. # create an array of object with the whole content of the folder
  123. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  124. }
  125. # cache navigation
  126. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  127. }
  128. $this->structure = $structure;
  129. return true;
  130. }
  131. protected function setItem()
  132. {
  133. # if it is the homepage
  134. if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
  135. {
  136. $item = new \stdClass;
  137. $item->elementType = 'folder';
  138. $item->path = '';
  139. $item->urlRel = '/';
  140. }
  141. else
  142. {
  143. # search for the url in the structure
  144. $item = Folder::getItemForUrl($this->structure, $this->params['url']);
  145. }
  146. if($item)
  147. {
  148. if($item->elementType == 'file')
  149. {
  150. $pathParts = explode('.', $item->path);
  151. $fileType = array_pop($pathParts);
  152. $pathWithoutType = implode('.', $pathParts);
  153. $item->pathWithoutType = $pathWithoutType;
  154. }
  155. elseif($item->elementType == 'folder')
  156. {
  157. $item->pathWithoutItem = $item->path;
  158. $item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
  159. $item->pathWithoutType = $item->path;
  160. }
  161. $this->item = $item;
  162. return true;
  163. }
  164. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  165. return false;
  166. }
  167. # determine if you want to write to published file (md) or to draft (txt)
  168. protected function setItemPath($fileType)
  169. {
  170. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  171. }
  172. protected function setPublishStatus()
  173. {
  174. $this->item->published = false;
  175. $this->item->drafted = false;
  176. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  177. {
  178. $this->item->published = true;
  179. # add file-type in case it is a folder
  180. $this->item->fileType = "md";
  181. }
  182. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  183. {
  184. $this->item->drafted = true;
  185. # add file-type in case it is a folder
  186. $this->item->fileType = "txt";
  187. }
  188. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  189. {
  190. # set txt as default for a folder, so that we can create an index.txt for a folder.
  191. $this->item->fileType = "txt";
  192. }
  193. }
  194. protected function deleteContentFiles($fileTypes, $folder = false)
  195. {
  196. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  197. foreach($fileTypes as $fileType)
  198. {
  199. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  200. {
  201. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  202. }
  203. }
  204. if($this->errors)
  205. {
  206. return false;
  207. }
  208. return true;
  209. }
  210. protected function deleteContentFolder()
  211. {
  212. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  213. $path = $basePath . $this->item->pathWithoutItem;
  214. if(file_exists($path))
  215. {
  216. $files = array_diff(scandir($path), array('.', '..'));
  217. # check if there are folders first, then stop the operation
  218. foreach ($files as $file)
  219. {
  220. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  221. {
  222. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  223. }
  224. }
  225. if(!$this->errors)
  226. {
  227. foreach ($files as $file)
  228. {
  229. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  230. }
  231. return rmdir($path);
  232. }
  233. }
  234. return false;
  235. }
  236. protected function setContent()
  237. {
  238. # if the file exists
  239. if($this->item->published OR $this->item->drafted)
  240. {
  241. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  242. if($this->item->fileType == 'txt')
  243. {
  244. # decode the json-draft to an array
  245. $content = json_decode($content);
  246. }
  247. }
  248. elseif($this->item->elementType == "folder")
  249. {
  250. $content = '';
  251. }
  252. else
  253. {
  254. $this->errors = ['errors' => ['message' => 'requested file not found']];
  255. return false;
  256. }
  257. $this->content = $content;
  258. return true;
  259. }
  260. }