action.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** COMMON **/
  5. case 'wiki_logo_download':
  6. $logoDir = File::dir().'wiki'.SLASH.'logo';
  7. $logo = $logoDir.SLASH.'logo.png';
  8. if(!file_exists($logoDir)) mkdir($logoDir,0755,true);
  9. if(!file_exists($logo)) copy(__DIR__.SLASH.'img'.SLASH.'logo.png', $logo);
  10. header('Content-Type: image/png');
  11. echo file_get_contents($logo);
  12. break;
  13. case 'wiki_logo_delete':
  14. Action::write(function(&$response){
  15. global $myUser,$_;
  16. if(!$myUser->can('wiki', 'configure')) throw new Exception("Permissions insuffisantes",403);
  17. foreach (glob(File::dir().'wiki'.SLASH."logo".SLASH."logo.*") as $filename)
  18. unlink($filename);
  19. });
  20. break;
  21. /** HOME **/
  22. case 'wiki_page_home':
  23. Action::write(function(&$response){
  24. global $myUser,$_;
  25. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  26. ob_start();
  27. require_once(__DIR__.SLASH.'page.home.php');
  28. $response['content'] = ob_get_clean();
  29. });
  30. break;
  31. case 'wiki_page_search':
  32. Action::write(function(&$response){
  33. global $myUser,$_;
  34. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  35. ob_start();
  36. require_once(__DIR__.SLASH.'page.search.php');
  37. $response['content'] = ob_get_clean();
  38. });
  39. break;
  40. case 'wiki_page_download':
  41. require_once(__DIR__.SLASH.'WikiPage.class.php');
  42. $workspace = WikiPage::workspace();
  43. $page = WikiPage::getById($_['page']);
  44. File::downloadFile($workspace.SLASH.wiki_os_encode($page->path),null,null,true);
  45. break;
  46. /** CATEGORY **/
  47. //Récuperation d'une liste de page
  48. case 'wiki_category_search':
  49. Action::write(function(&$response){
  50. global $myUser,$_;
  51. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  52. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  53. require_once(__DIR__.SLASH.'WikiPage.class.php');
  54. $workspace = WikiPage::workspace();
  55. if(!file_exists($workspace)) mkdir($workspace,0755,true);
  56. foreach(WikiCategory::loadAll() as $category){
  57. $response['rows'][] = $category;
  58. }
  59. });
  60. break;
  61. case 'wiki_category_edit':
  62. require_once(__DIR__.SLASH.'modal.category.php');
  63. break;
  64. case 'wiki_category_open':
  65. Action::write(function(&$response){
  66. global $myUser,$_;
  67. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  68. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  69. require_once(__DIR__.SLASH.'WikiPage.class.php');
  70. $category = WikiCategory::load(array('slug'=>$_['category']));
  71. $pages = $category->pages();
  72. $recents = WikiPage::loadAll(array('category'=>$category->id),array('updated DESC'),array('10'), array('*'), 1);
  73. ob_start();
  74. require_once(__DIR__.SLASH.'page.category.php');
  75. $response['content'] = ob_get_clean();
  76. $response['categorySlug'] = $category->slug;
  77. $response['pages'] = $pages;
  78. });
  79. break;
  80. case 'wiki_category_download':
  81. try{
  82. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  83. require_once(__DIR__.SLASH.'WikiPage.class.php');
  84. $workspace = WikiPage::workspace();
  85. $category = WikiCategory::getById($_['category']);
  86. $path = $workspace.SLASH.wiki_os_encode($category->path);
  87. $zipName = tempnam(sys_get_temp_dir(), "zip123");
  88. if (!extension_loaded('zip')) throw new Exception("L'extension ZIP est manquante");
  89. $zip = new ZipArchive();
  90. if (!$zip->open($zipName, ZIPARCHIVE::CREATE))
  91. throw new Exception ("Impossible de créer l'archive (problèmes de permissions ?");
  92. foreach(glob($path.SLASH.'*.md') as $file){
  93. $zip->addFromString(basename($file), file_get_contents($file));
  94. }
  95. $zip->close();
  96. $stream = file_get_contents($zipName);
  97. unlink($zipName);
  98. File::downloadStream($stream,$category->slug.'.zip');
  99. }catch(Exception $e){
  100. echo 'Erreur : '.$e->getMessage();
  101. }
  102. break;
  103. //Ajout ou modification d'élément page
  104. case 'wiki_category_save':
  105. Action::write(function(&$response){
  106. global $myUser,$_;
  107. if(!$myUser->can('wiki','edit')) throw new Exception("Permissions insuffisantes",403);
  108. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  109. require_once(__DIR__.SLASH.'WikiPage.class.php');
  110. $workspace = WikiPage::workspace();
  111. $item = isset($_['id']) && is_numeric($_['id']) ? WikiCategory::getById($_['id']) : new WikiCategory();
  112. $item->icon = $_['icon'];
  113. $item->color = $_['color'];
  114. if($item->id==0){
  115. $item->label = $_['label'];
  116. $item->slug = slugify($item->label);
  117. $item->path = WikiPage::path_from_label($item->label);
  118. $dir = $workspace.SLASH.wiki_os_encode($item->path);
  119. if(!file_exists($dir)) mkdir($dir,0755,true);
  120. }else{
  121. if($item->label!=$_['label']){
  122. $oldDir = $workspace.SLASH.wiki_os_encode($item->path);
  123. $item->label = $_['label'] ;
  124. $item->slug = slugify($item->label);
  125. $item->path = WikiPage::path_from_label($item->label);
  126. $newDir = $workspace.SLASH.wiki_os_encode($item->path);
  127. if(file_exists($newDir)) throw new Exception("Ce nom de catégorie est déja pris");
  128. rename($oldDir, $newDir);
  129. }
  130. }
  131. $item->save();
  132. $response = $item->toArray();
  133. });
  134. break;
  135. //Suppression d'élement page
  136. case 'wiki_category_delete':
  137. Action::write(function(&$response){
  138. global $myUser,$_;
  139. if(!$myUser->can('wiki','delete')) throw new Exception("Permissions insuffisantes",403);
  140. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  141. require_once(__DIR__.SLASH.'WikiPage.class.php');
  142. WikiPage::delete(array('category'=>$_['id']));
  143. WikiCategory::deleteById($_['id']);
  144. });
  145. break;
  146. /** PAGE **/
  147. //Ajout ou modification d'élément page
  148. case 'wiki_page_save':
  149. Action::write(function(&$response){
  150. global $myUser,$_;
  151. if(!$myUser->can('wiki','edit')) throw new Exception("Permissions insuffisantes",403);
  152. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  153. require_once(__DIR__.SLASH.'WikiPage.class.php');
  154. $page = WikiPage::provide();
  155. $page->content = html_entity_decode($_['content']);
  156. if($page->id == 0 && isset($_['category'])){
  157. $category = WikiCategory::getById($_['category']);
  158. $page->state = WikiPage::PUBLISHED;
  159. $page->category = $category->id;
  160. $page->label = 'Nouvelle page - '.date('d/m/y h:i:s');
  161. $page->path = $category->path.SLASH.WikiPage::path_from_label($page->label).'.md';
  162. $page->slug = slugify($page->label);
  163. } else {
  164. $category = WikiCategory::getById($page->category);
  165. if($page->label!=$_['label']){
  166. $oldPath = WikiPage::workspace().SLASH.wiki_os_encode($page->path);
  167. $page->label = $_['label'];
  168. $page->path = $category->path.SLASH.WikiPage::path_from_label($page->label).'.md';
  169. $page->slug = slugify($page->label);
  170. $newPath = WikiPage::workspace().SLASH.wiki_os_encode($page->path);
  171. if(file_exists($newPath)) throw new Exception("Ce nom de page pour cette catégorie est déja pris");
  172. unlink($oldPath);
  173. }
  174. }
  175. if(isset($page->content)) file_put_contents(WikiPage::workspace().SLASH.wiki_os_encode($page->path),$page->content);
  176. $page->save();
  177. ob_start();
  178. require_once(__DIR__.SLASH.'page.page.php');
  179. $response['content'] = ob_get_clean();
  180. $response['page'] = $page->toArray();
  181. $response['category'] = $category->toArray();
  182. });
  183. break;
  184. //Suppression d'élement page
  185. case 'wiki_page_delete':
  186. Action::write(function(&$response){
  187. global $myUser,$_;
  188. if(!$myUser->can('wiki','delete')) throw new Exception("Permissions insuffisantes",403);
  189. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  190. $page = WikiPage::getById($_['id']);
  191. $category = WikiCategory::getById($page->category);
  192. $response['category']= $category->toArray();
  193. WikiPage::deleteById($page->id);
  194. $path = WikiPage::workspace().SLASH.wiki_os_encode($page->path);
  195. if(file_exists($path)) unlink($path);
  196. });
  197. break;
  198. case 'wiki_page_open':
  199. Action::write(function(&$response){
  200. global $myUser,$_;
  201. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  202. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  203. require_once(__DIR__.SLASH.'WikiPage.class.php');
  204. $page = WikiPage::load(array('slug'=>$_['page']));
  205. if(!$page){
  206. $page = new WikiPage();
  207. $page->label = $_['page'];
  208. }
  209. $category = WikiCategory::load(array('slug'=>$_['category']));
  210. ob_start();
  211. require_once(__DIR__.SLASH.'page.page.php');
  212. $response['content'] = ob_get_clean();
  213. $response['categorySlug'] = $category->slug;
  214. if($page->id!=0) $response['pageSlug'] = $page->slug;
  215. });
  216. break;
  217. //Sauvegarde des configurations de wiki
  218. case 'wiki_setting_save':
  219. Action::write(function(&$response){
  220. global $myUser,$_,$conf;
  221. if(!$myUser->can('wiki','configure')) throw new Exception("Permissions insuffisantes",403);
  222. foreach(Configuration::setting('wiki') as $key=>$value){
  223. if(!is_array($value)) continue;
  224. $allowed[] = $key;
  225. }
  226. foreach ($_['fields'] as $key => $value)
  227. if(in_array($key, $allowed)) $conf->put($key,$value);
  228. if(!empty($_FILES['logo']) && $_FILES['logo']['size']!=0 ){
  229. $logo = File::upload('logo','wiki'.SLASH.'logo'.SLASH.'logo.{{ext}}', 1048576, array('jpg','png','jpeg'));
  230. Image::resize($logo['absolute'], 38, 38, false);
  231. Image::toPng($logo['absolute']);
  232. }
  233. });
  234. break;
  235. case 'wiki_file_upload':
  236. Action::write(function(&$response){
  237. global $myUser,$_,$conf;
  238. if(!$myUser->can('wiki','edit')) throw new Exception("Permissions insuffisantes",403);
  239. require_once(__DIR__.SLASH.'WikiPage.class.php');
  240. $uploads = WikiPage::uploads().SLASH;
  241. if(!file_exists($uploads)) mkdir($uploads,0755,true);
  242. $maxSize = $conf->get('wiki_max_size') * 1048576;
  243. $extensions = explode(',',str_replace(' ', '', $conf->get('wiki_ext')));
  244. $response['rows'] = array();
  245. if(!is_array($_FILES['file']['name'])){
  246. $_FILES['file']['name'] = array($_FILES['file']['name']);
  247. $_FILES['file']['size'] = array($_FILES['file']['size']);
  248. $_FILES['file']['tmp_name'] = array($_FILES['file']['tmp_name']);
  249. }
  250. for ($i=0; $i<count($_FILES['file']['name']); $i++) {
  251. $extension = getExt($_FILES['file']['name'][$i]);
  252. if($_FILES['file']['size'][$i] > $maxSize) throw new Exception("Taille du fichier ".$_FILES['file']['name'][$i]." trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)');
  253. if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier ".$_FILES['file']['name'][$i]." non permise, autorisé :".implode(', ',$extensions));
  254. $filePath = $uploads.wiki_os_encode($_FILES['file']['name'][$i]);
  255. $u = 0;
  256. while(file_exists($filePath)){
  257. $u++;
  258. $filePath = $uploads.$u.'_'.wiki_os_encode($_FILES['file']['name'][$i]);
  259. }
  260. rename($_FILES['file']['tmp_name'][$i],$filePath);
  261. $row = array(
  262. 'name'=>$_FILES['file']['name'][$i],
  263. 'relative'=>str_replace($uploads,'',$filePath),
  264. 'absolute'=>$filePath,
  265. );
  266. switch($extension){
  267. case 'jpg':
  268. case 'jpeg':
  269. case 'gif':
  270. case 'png':
  271. $row['tag'] = '!['.$row['name'].'](action.php?action=wiki_file_read&file='.base64_encode($row['relative']).')';
  272. break;
  273. default:
  274. $row['tag'] = '['.$row['name'].'](action.php?action=wiki_file_read&file='.base64_encode($row['relative']).')';
  275. break;
  276. }
  277. $response['rows'][] = $row;
  278. Log::put("Upload d'un élément : ".$filePath,'WIKI');
  279. }
  280. });
  281. break;
  282. case 'wiki_file_read':
  283. global $myUser,$_,$conf;
  284. if(!$myUser->can('wiki','read')) throw new Exception("Permissions insuffisantes",403);
  285. File::downloadFile('file/wiki/uploads/'.base64_decode($_['file']));
  286. break;
  287. case 'wiki_night_mode':
  288. Action::write(function(&$response){
  289. global $myUser,$_,$conf;
  290. $myUser->preference('wiki_night_mode', isset($_['nightmode']) && !empty($_['nightmode'])?true:false);
  291. $myUser->loadPreferences();
  292. });
  293. break;
  294. }
  295. ?>