ContentController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. # holds informations about the homepage
  29. protected $homepage;
  30. # hold the page-item as an object
  31. protected $item;
  32. # hold the breadcrumb as an object
  33. protected $breadcrumb;
  34. # holds the path to the requested file
  35. protected $path = false;
  36. # holds the content of the page
  37. protected $content;
  38. public function __construct(ContainerInterface $c)
  39. {
  40. $this->c = $c;
  41. $this->settings = $this->c->get('settings');
  42. $this->structureLiveName = 'structure.txt';
  43. $this->structureDraftName = 'structure-draft.txt';
  44. }
  45. protected function render($response, $route, $data)
  46. {
  47. if(isset($_SESSION['old']))
  48. {
  49. unset($_SESSION['old']);
  50. }
  51. $response = $response->withoutHeader('Server');
  52. $response = $response->withoutHeader('X-Powered-By');
  53. if($this->c->request->getUri()->getScheme() == 'https')
  54. {
  55. $response = $response->withAddedHeader('Strict-Transport-Security', 'max-age=63072000');
  56. }
  57. $response = $response->withAddedHeader('X-Content-Type-Options', 'nosniff');
  58. $response = $response->withAddedHeader('X-Frame-Options', 'SAMEORIGIN');
  59. $response = $response->withAddedHeader('X-XSS-Protection', '1;mode=block');
  60. $response = $response->withAddedHeader('Referrer-Policy', 'no-referrer-when-downgrade');
  61. return $this->c->view->render($response, $route, $data);
  62. }
  63. protected function render404($response, $data = NULL)
  64. {
  65. return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
  66. }
  67. protected function renderIntern404($response, $data = NULL)
  68. {
  69. return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
  70. }
  71. protected function validateEditorInput()
  72. {
  73. $validate = new Validation();
  74. $vResult = $validate->editorInput($this->params);
  75. if(is_array($vResult))
  76. {
  77. $message = reset($vResult);
  78. $this->errors = ['errors' => $vResult];
  79. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  80. return false;
  81. }
  82. return true;
  83. }
  84. protected function validateBlockInput()
  85. {
  86. $validate = new Validation();
  87. $vResult = $validate->blockInput($this->params);
  88. if(is_array($vResult))
  89. {
  90. $message = reset($vResult);
  91. $this->errors = ['errors' => $vResult];
  92. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  93. return false;
  94. }
  95. return true;
  96. }
  97. protected function validateNavigationSort()
  98. {
  99. $validate = new Validation();
  100. $vResult = $validate->navigationSort($this->params);
  101. if(is_array($vResult))
  102. {
  103. $message = reset($vResult);
  104. $this->errors = ['errors' => $vResult];
  105. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  106. return false;
  107. }
  108. return true;
  109. }
  110. protected function validateNaviItem()
  111. {
  112. $validate = new Validation();
  113. $vResult = $validate->navigationItem($this->params);
  114. if(is_array($vResult))
  115. {
  116. $message = reset($vResult);
  117. $this->errors = ['errors' => $vResult];
  118. if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
  119. return false;
  120. }
  121. return true;
  122. }
  123. protected function setStructure($draft = false, $cache = true)
  124. {
  125. # set initial structure to false
  126. $structure = false;
  127. # name of structure-file for draft or live
  128. $filename = $draft ? $this->structureDraftName : $this->structureLiveName;
  129. # set variables and objects
  130. $this->write = new writeCache();
  131. # check, if cached structure is still valid
  132. if($cache && $this->write->validate('cache', 'lastCache.txt', 600))
  133. {
  134. # get the cached structure
  135. $structure = $this->write->getCache('cache', $filename);
  136. }
  137. # if no structure was found or cache is deactivated
  138. if(!$structure)
  139. {
  140. # scan the content of the folder
  141. $structure = Folder::scanFolder($this->settings['rootPath'] . $this->settings['contentFolder'], $draft);
  142. # if there is content, then get the content details
  143. if(count($structure) > 0)
  144. {
  145. # create an array of object with the whole content of the folder
  146. $structure = Folder::getFolderContentDetails($structure, $this->uri->getBaseUrl(), $this->uri->getBasePath());
  147. }
  148. # cache navigation
  149. $this->write->updateCache('cache', $filename, 'lastCache.txt', $structure);
  150. }
  151. $this->structure = $structure;
  152. return true;
  153. }
  154. protected function setHomepage()
  155. {
  156. $contentFolder = Folder::scanFolderFlat($this->settings['rootPath'] . $this->settings['contentFolder']);
  157. if(in_array('index.md', $contentFolder))
  158. {
  159. $md = true;
  160. $status = 'published';
  161. }
  162. if(in_array('index.txt', $contentFolder))
  163. {
  164. $txt = true;
  165. $status = 'unpublished';
  166. }
  167. if(isset($txt) && isset($md))
  168. {
  169. $status = 'modified';
  170. }
  171. $active = false;
  172. if($this->params['url'] == '/' || $this->params['url'] == $this->uri->getBasePath() )
  173. {
  174. $active = 'active';
  175. }
  176. $this->homepage = ['status' => $status, 'active' => $active];
  177. }
  178. protected function setItem()
  179. {
  180. # if it is the homepage
  181. if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
  182. {
  183. $item = new \stdClass;
  184. $item->elementType = 'folder';
  185. $item->path = '';
  186. $item->urlRel = '/';
  187. }
  188. else
  189. {
  190. # search for the url in the structure
  191. $item = Folder::getItemForUrl($this->structure, $this->params['url']);
  192. }
  193. if($item)
  194. {
  195. if($item->elementType == 'file')
  196. {
  197. $pathParts = explode('.', $item->path);
  198. $fileType = array_pop($pathParts);
  199. $pathWithoutType = implode('.', $pathParts);
  200. $item->pathWithoutType = $pathWithoutType;
  201. }
  202. elseif($item->elementType == 'folder')
  203. {
  204. $item->pathWithoutItem = $item->path;
  205. $item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
  206. $item->pathWithoutType = $item->path;
  207. }
  208. $this->item = $item;
  209. return true;
  210. }
  211. $this->errors = ['errors' => ['message' => 'requested page-url not found']];
  212. return false;
  213. }
  214. # determine if you want to write to published file (md) or to draft (txt)
  215. protected function setItemPath($fileType)
  216. {
  217. $this->path = $this->item->pathWithoutType . '.' . $fileType;
  218. }
  219. protected function setPublishStatus()
  220. {
  221. $this->item->published = false;
  222. $this->item->drafted = false;
  223. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.md'))
  224. {
  225. $this->item->published = true;
  226. # add file-type in case it is a folder
  227. $this->item->fileType = "md";
  228. }
  229. if(file_exists($this->settings['rootPath'] . $this->settings['contentFolder'] . $this->item->pathWithoutType . '.txt'))
  230. {
  231. $this->item->drafted = true;
  232. # add file-type in case it is a folder
  233. $this->item->fileType = "txt";
  234. }
  235. if(!$this->item->drafted && !$this->item->published && $this->item->elementType == "folder")
  236. {
  237. # set txt as default for a folder, so that we can create an index.txt for a folder.
  238. $this->item->fileType = "txt";
  239. }
  240. }
  241. protected function deleteContentFiles($fileTypes, $folder = false)
  242. {
  243. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  244. foreach($fileTypes as $fileType)
  245. {
  246. if(file_exists($basePath . $this->item->pathWithoutType . '.' . $fileType) && !unlink($basePath . $this->item->pathWithoutType . '.' . $fileType) )
  247. {
  248. $this->errors = ['message' => 'We could not delete the file, please check, if the file is writable.'];
  249. }
  250. }
  251. if($this->errors)
  252. {
  253. return false;
  254. }
  255. return true;
  256. }
  257. protected function deleteContentFolder()
  258. {
  259. $basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
  260. $path = $basePath . $this->item->pathWithoutItem;
  261. if(file_exists($path))
  262. {
  263. $files = array_diff(scandir($path), array('.', '..'));
  264. # check if there are folders first, then stop the operation
  265. foreach ($files as $file)
  266. {
  267. if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
  268. {
  269. $this->errors = ['message' => 'Please delete the sub-folder first.'];
  270. }
  271. }
  272. if(!$this->errors)
  273. {
  274. foreach ($files as $file)
  275. {
  276. unlink(realpath($path) . DIRECTORY_SEPARATOR . $file);
  277. }
  278. return rmdir($path);
  279. }
  280. }
  281. return false;
  282. }
  283. protected function setContent()
  284. {
  285. # if the file exists
  286. if($this->item->published OR $this->item->drafted)
  287. {
  288. $content = $this->write->getFile($this->settings['contentFolder'], $this->path);
  289. if($this->item->fileType == 'txt')
  290. {
  291. # decode the json-draft to an array
  292. $content = json_decode($content);
  293. }
  294. }
  295. elseif($this->item->elementType == "folder")
  296. {
  297. $content = '';
  298. }
  299. else
  300. {
  301. $this->errors = ['errors' => ['message' => 'requested file not found']];
  302. return false;
  303. }
  304. $this->content = $content;
  305. return true;
  306. }
  307. }