action.php 14 KB

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