action.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. /** SKETCH **/
  5. //Récuperation d'une liste de sketch
  6. case 'hackpoint_sketch_search':
  7. Action::write(function(&$response){
  8. global $myUser,$_;
  9. require_once(__DIR__.SLASH.'Sketch.class.php');
  10. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  11. $query = 'SELECT * FROM '.Sketch::tableName().' WHERE 1';
  12. $data = array();
  13. //Recherche simple
  14. if(!empty($_['filters']['keyword'])){
  15. $query .= ' AND label LIKE ?';
  16. $data[] = '%'.$_['filters']['keyword'].'%';
  17. }
  18. //Recherche avancée
  19. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label'),$query,$data);
  20. //Tri des colonnes
  21. if(isset($_['sort'])) sort_secure_query($_['sort'],array('label'),$query,$data);
  22. //Pagination
  23. $response['pagination'] = Sketch::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$data);
  24. $sketchs = Sketch::staticQuery($query,$data,true,0);
  25. foreach($sketchs as $sketch){
  26. if(!$sketch->state && $sketch->creator != $myUser->login) continue;
  27. $row = $sketch->toArray();
  28. $row['comment'] = truncate($row['comment'],65);
  29. $row['picture'] = $sketch->picture();
  30. $row['progress-color'] = 'bg-danger';
  31. if($row['progress'] > 30) $row['progress-color'] = 'bg-warning';
  32. if($row['progress'] > 45) $row['progress-color'] = 'bg-info';
  33. if($row['progress'] > 65) $row['progress-color'] = '';
  34. if($row['progress'] > 85) $row['progress-color'] = 'bg-success';
  35. $row['created'] = relative_time($row['created']);
  36. $response['rows'][] = $row;
  37. }
  38. });
  39. break;
  40. //Ajout ou modification d'élément sketch
  41. case 'hackpoint_sketch_save':
  42. Action::write(function(&$response){
  43. global $myUser,$_;
  44. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  45. require_once(__DIR__.SLASH.'Sketch.class.php');
  46. $item = Sketch::getById($_['id']);
  47. if(isset($_['label'])) $item->label = $_['label'];
  48. if($item->id==0) $item->progress = 5;
  49. if(isset($_['state'])) $item->state = $_['state'] == 'true';
  50. if(isset($_['comment'])) $item->comment = $_['comment'];
  51. $item->save();
  52. });
  53. break;
  54. case 'hackpoint_sketch_permission_save':
  55. Action::write(function(&$response){
  56. global $myUser,$_;
  57. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  58. require_once(__DIR__.SLASH.'Sketch.class.php');
  59. $permission = Permission::form();
  60. $permission->entity = 'hackpoint';
  61. $sketch = Sketch::getById($permission->uid);
  62. if($sketch->creator!=$myUser->login) throw new Exception("Vous ne pouvez pas définir une permission sur un sketch qui ne vous appartient pas");
  63. $permission->save();
  64. });
  65. break;
  66. //Suppression d'élement permission
  67. case 'hackpoint_sketch_permission_delete':
  68. Action::write(function(&$response){
  69. global $myUser,$_;
  70. require_once(__DIR__.SLASH.'Sketch.class.php');
  71. $permission = Permission::getById($_['id']);
  72. if($permission->entity != 'hackpoint') throw new Exception("Erreur de routage des permissions");
  73. $sketch = Sketch::getById($permission->uid);
  74. if($sketch->creator!=$myUser->login) throw new Exception("Vous ne pouvez pas définir une permission sur un sketch qui ne vous appartient pas");
  75. Permission::deleteById($permission->id);
  76. });
  77. break;
  78. case 'hackpoint_sketch_save_cover':
  79. Action::write(function(&$response){
  80. global $myUser,$_;
  81. require_once(__DIR__.SLASH.'Sketch.class.php');
  82. if(!is_numeric($_['sketch'])) throw new Exception("Sketch non spécifié", 400);
  83. $sketch = Sketch::provide('sketch');
  84. if($myUser->login!= $sketch->creator) throw new Exception("Permission insuffisantes", 403);
  85. $folder = $sketch->directory();
  86. if(!file_exists($folder)) mkdir($folder,0755,true);
  87. $name = 'cover.jpg';
  88. $stream = preg_replace('|data\:image\/[^;]*;base64,|is','',$_['stream']);
  89. $filepath = $folder.SLASH.$name;
  90. $row = file_put_contents($filepath, base64_decode($stream));
  91. //Image::resize($filepath,150,150);
  92. $response['stream'] = 'data:image/jpg;base64,'.base64_encode(file_get_contents($filepath));
  93. });
  94. break;
  95. case 'hackpoint_sketch_download':
  96. Action::write(function(&$response){
  97. global $myUser,$_;
  98. require_once(__DIR__.SLASH.'Sketch.class.php');
  99. if(!is_numeric($_['id'])) throw new Exception("Sketch non spécifié", 400);
  100. $sketch = Sketch::provide();
  101. if($myUser->login!= $sketch->creator && !$sketch->state) throw new Exception("Permission insuffisantes", 403);
  102. File::downloadStream($sketch->download(),$sketch->slug.' '.date('d-m-y H-i-s').'.zip');
  103. exit();
  104. });
  105. break;
  106. case 'hackpoint_sketch_progress_save':
  107. Action::write(function(&$response){
  108. global $myUser,$_;
  109. require_once(__DIR__.SLASH.'Sketch.class.php');
  110. $item = Sketch::getById($_['id']);
  111. if($myUser->login!=$item->creator) throw new Exception("Permissions insuffisantes",403);
  112. $item->progress = $_['progress'];
  113. $item->save();
  114. });
  115. break;
  116. case 'hackpoint_sketch_add':
  117. global $myUser,$_;
  118. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  119. require_once(__DIR__.SLASH.'Sketch.class.php');
  120. $sketch = new Sketch();
  121. $sketch->label = 'Sketch Sans titre';
  122. $sketch->state = false;
  123. $sketch->progress = 10;
  124. $sketch->comment = 'Nouveau sketch sans commentaires';
  125. $sketch->save();
  126. require_once(__DIR__.SLASH.'Resource.class.php');
  127. $item = new Resource();
  128. $item->label = 'Documentation';
  129. $item->sort = 0;
  130. $item->type = 'readme';
  131. $item->content = '# Documentation'.PHP_EOL.'Utilisez le bouton + en bas de la barre latérale pour ajouter des ressources...';
  132. $item->sketch = $sketch->id;
  133. $item->save();
  134. header('location: index.php?module=hackpoint&page=sheet.sketch&id='.$sketch->id);
  135. break;
  136. //Suppression d'élement sketch
  137. case 'hackpoint_sketch_delete':
  138. Action::write(function(&$response){
  139. global $myUser,$_;
  140. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  141. require_once(__DIR__.SLASH.'Sketch.class.php');
  142. Sketch::removeById($_['id']);
  143. });
  144. break;
  145. //Sauvegarde des configurations de hackpoint
  146. case 'hackpoint_setting_save':
  147. Action::write(function(&$response){
  148. global $myUser,$_,$conf;
  149. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  150. foreach(Configuration::setting('hackpoint') as $key=>$value){
  151. if(!is_array($value)) continue;
  152. $allowed[] = $key;
  153. }
  154. foreach ($_['fields'] as $key => $value) {
  155. if(in_array($key, $allowed))
  156. $conf->put($key,$value);
  157. }
  158. });
  159. break;
  160. /** RESOURCE **/
  161. //Récuperation d'une liste de resource
  162. case 'hackpoint_resource_search':
  163. Action::write(function(&$response){
  164. global $myUser,$_;
  165. require_once(__DIR__.SLASH.'Sketch.class.php');
  166. require_once(__DIR__.SLASH.'Resource.class.php');
  167. $sketch = Sketch::provide('sketch');
  168. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Sketch privé", 403);
  169. foreach(Resource::loadAll(array('sketch'=>$_['sketch']),array('sort')) as $resource){
  170. $row = $resource->toArray();
  171. $type = $resource->type();
  172. $row['type'] = $type;
  173. $response['rows'][] = $row;
  174. }
  175. });
  176. break;
  177. case 'hackpoint_resource_edit':
  178. Action::write(function(&$response){
  179. global $myUser,$_;
  180. require_once(__DIR__.SLASH.'Sketch.class.php');
  181. require_once(__DIR__.SLASH.'Resource.class.php');
  182. require_once(__DIR__.SLASH.'Resource.class.php');
  183. $item = Resource::provide('id',1);
  184. $sketch = $item->join('sketch');
  185. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Sketch privé", 403);
  186. $response = $item->toHtml();
  187. $response['resourceType'] = $item->type;
  188. });
  189. break;
  190. //Sauveagrde du contenu d'une resource
  191. case 'hackpoint_resource_save_content':
  192. Action::write(function(&$response){
  193. global $myUser,$_;
  194. require_once(__DIR__.SLASH.'Sketch.class.php');
  195. require_once(__DIR__.SLASH.'Resource.class.php');
  196. $item = Resource::provide('id',1);
  197. $sketch = $item->join('sketch');
  198. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  199. $item->content = $_['content'];
  200. $item->save();
  201. });
  202. break;
  203. //Ajout ou modification d'élément resource
  204. case 'hackpoint_resource_save':
  205. Action::write(function(&$response){
  206. global $myUser,$_;
  207. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  208. require_once(__DIR__.SLASH.'Sketch.class.php');
  209. require_once(__DIR__.SLASH.'Resource.class.php');
  210. require_once(__DIR__.SLASH.'ResourceType.class.php');
  211. $item = Resource::provide('id',1);
  212. $sketch = $item->join('sketch');
  213. if( !is_object($sketch) || $sketch->id==0){
  214. $sketch = Sketch::getById($_['sketch']);
  215. }
  216. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  217. if(!isset($_['type']) && $item->id!=0) $_['type'] = $item->type;
  218. $type = ResourceType::types($_['type']);
  219. if(isset($_['label'])) $item->label = $_['label'];
  220. if($item->id==0){
  221. $item->label = $type['label'];
  222. $item->sort = 100;
  223. $item->type = $_['type'];
  224. if(isset($type['default'])) $item->content = $type['default'] ;
  225. $item->sketch = $_['sketch'];
  226. }
  227. $item->save();
  228. $response = $item->toArray();
  229. $response['type'] = $item->type();
  230. });
  231. break;
  232. case 'hackpoint_resource_sort':
  233. Action::write(function(&$response){
  234. global $myUser,$_;
  235. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  236. require_once(__DIR__.SLASH.'Sketch.class.php');
  237. require_once(__DIR__.SLASH.'Resource.class.php');
  238. require_once(__DIR__.SLASH.'ResourceType.class.php');
  239. foreach($_['sort'] as $sort=>$id){
  240. $resource = Resource::getById($id,1);
  241. $sketch = $resource->join('sketch');
  242. if($sketch->creator != $myUser->login) continue;
  243. $resource->sort = $sort;
  244. $resource->save();
  245. }
  246. });
  247. break;
  248. //Suppression d'élement resource
  249. case 'hackpoint_resource_delete':
  250. Action::write(function(&$response){
  251. global $myUser,$_;
  252. require_once(__DIR__.SLASH.'Sketch.class.php');
  253. require_once(__DIR__.SLASH.'Resource.class.php');
  254. $item = Resource::getById($_['id'],1);
  255. $sketch = $item->join('sketch');
  256. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  257. Resource::deleteById($_['id']);
  258. });
  259. break;
  260. //Sauvegarde des configurations de hackpoint
  261. case 'hackpoint_setting_save':
  262. Action::write(function(&$response){
  263. global $myUser,$_,$conf;
  264. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  265. foreach(Configuration::setting('hackpoint') as $key=>$value){
  266. if(!is_array($value)) continue;
  267. $allowed[] = $key;
  268. }
  269. foreach ($_['fields'] as $key => $value) {
  270. if(in_array($key, $allowed))
  271. $conf->put($key,$value);
  272. }
  273. });
  274. break;
  275. //Suppression document
  276. case 'resource_delete_document':
  277. Action::write(function(&$response){
  278. global $myUser,$_;
  279. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  280. require_once(__DIR__.SLASH.'Resource.class.php');
  281. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  282. //Le premier argument est un namspace de sécurité
  283. //et assure que le fichier sera toujours cloisoné dans un contexte file/hackpoint/sketch
  284. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  285. File::delete('hackpoint'.SLASH.'sketch',$path);
  286. });
  287. break;
  288. case 'resource_add_document':
  289. Action::write(function(&$response){
  290. global $myUser,$_;
  291. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  292. require_once(__DIR__.SLASH.'Resource.class.php');
  293. $resource = Resource::provide();
  294. $folder = $resource->directory();
  295. if(!file_exists($folder)) mkdir($folder,0755,true);
  296. foreach ($_['files'] as $file) {
  297. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  298. $row = File::move(File::temp().$file['path'],str_replace(File::dir(),'',$folder).SLASH.$name);
  299. $row['url'] = 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode($file['name']));
  300. $row['oldPath'] = $file['path'];
  301. if(!in_array( getExt($file['name']), array('jpg','jpeg','png','bmp','svg'))){
  302. $row['icon'] = $file['icon'];//getExtIcon( getExt($file));
  303. }
  304. $response['files'][] = $row;
  305. }
  306. });
  307. break;
  308. //Download d'un fichier
  309. case 'hackpoint_download_file':
  310. Action::write(function(&$response){
  311. global $myUser,$_;
  312. $file = str_replace(array('..'),array(''),urldecode(base64_decode($_['file'])));
  313. $file = File::dir().'hackpoint'.SLASH.$file;
  314. File::downloadFile($file);
  315. });
  316. break;
  317. //Sauvegarde des configurations de hackpoint
  318. case 'hackpoint_setting_save':
  319. Action::write(function(&$response){
  320. global $myUser,$_,$conf;
  321. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  322. foreach(Configuration::setting('hackpoint') as $key=>$value){
  323. if(!is_array($value)) continue;
  324. $allowed[] = $key;
  325. }
  326. foreach ($_['fields'] as $key => $value) {
  327. if(in_array($key, $allowed))
  328. $conf->put($key,$value);
  329. }
  330. });
  331. break;
  332. }
  333. ?>