action.php 14 KB

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