action.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 && !$myUser->can('hackpoint',$sketch->id,'read') ) 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. /* permissions */
  55. case 'hackpoint_sketch_permission_save':
  56. Action::write(function(&$response){
  57. global $myUser,$_;
  58. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  59. require_once(__DIR__.SLASH.'Sketch.class.php');
  60. $permission = Permission::form();
  61. $permission->entity = 'hackpoint';
  62. $sketch = Sketch::getById($permission->uid);
  63. if($sketch->creator!=$myUser->login) throw new Exception("Vous ne pouvez pas définir une permission sur un sketch qui ne vous appartient pas");
  64. $permission->save();
  65. });
  66. break;
  67. //Suppression d'élement permission
  68. case 'hackpoint_sketch_permission_delete':
  69. Action::write(function(&$response){
  70. global $myUser,$_;
  71. require_once(__DIR__.SLASH.'Sketch.class.php');
  72. $permission = Permission::getById($_['id']);
  73. if($permission->entity != 'hackpoint') throw new Exception("Erreur de routage des permissions");
  74. $sketch = Sketch::getById($permission->uid);
  75. if($sketch->creator!=$myUser->login) throw new Exception("Vous ne pouvez pas définir une permission sur un sketch qui ne vous appartient pas");
  76. Permission::deleteById($permission->id);
  77. });
  78. break;
  79. case 'hackpoint_sketch_save_cover':
  80. Action::write(function(&$response){
  81. global $myUser,$_;
  82. require_once(__DIR__.SLASH.'Sketch.class.php');
  83. if(!is_numeric($_['sketch'])) throw new Exception("Sketch non spécifié", 400);
  84. $sketch = Sketch::provide('sketch');
  85. if($myUser->login!= $sketch->creator) throw new Exception("Permission insuffisantes", 403);
  86. $folder = $sketch->directory();
  87. if(!file_exists($folder)) mkdir($folder,0755,true);
  88. $name = 'cover.jpg';
  89. $stream = preg_replace('|data\:image\/[^;]*;base64,|is','',$_['stream']);
  90. $filepath = $folder.SLASH.$name;
  91. $row = file_put_contents($filepath, base64_decode($stream));
  92. //Image::resize($filepath,150,150);
  93. $response['stream'] = 'data:image/jpg;base64,'.base64_encode(file_get_contents($filepath));
  94. });
  95. break;
  96. case 'hackpoint_sketch_download':
  97. Action::write(function(&$response){
  98. global $myUser,$_;
  99. require_once(__DIR__.SLASH.'Sketch.class.php');
  100. if(!is_numeric($_['id'])) throw new Exception("Sketch non spécifié", 400);
  101. $sketch = Sketch::provide();
  102. if($myUser->login!= $sketch->creator && !$sketch->state) throw new Exception("Permission insuffisantes", 403);
  103. File::downloadStream($sketch->download(),$sketch->slug.' '.date('d-m-y H-i-s').'.zip');
  104. exit();
  105. });
  106. break;
  107. case 'hackpoint_sketch_progress_save':
  108. Action::write(function(&$response){
  109. global $myUser,$_;
  110. require_once(__DIR__.SLASH.'Sketch.class.php');
  111. $item = Sketch::getById($_['id']);
  112. if($myUser->login!=$item->creator) throw new Exception("Permissions insuffisantes",403);
  113. $item->progress = $_['progress'];
  114. $item->save();
  115. });
  116. break;
  117. case 'hackpoint_sketch_add':
  118. global $myUser,$_;
  119. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  120. require_once(__DIR__.SLASH.'Sketch.class.php');
  121. $sketch = new Sketch();
  122. $sketch->label = 'Sketch Sans titre';
  123. $sketch->state = false;
  124. $sketch->progress = 10;
  125. $sketch->comment = 'Nouveau sketch sans commentaires';
  126. $sketch->save();
  127. require_once(__DIR__.SLASH.'Resource.class.php');
  128. $item = new Resource();
  129. $item->label = 'Documentation';
  130. $item->sort = 0;
  131. $item->type = 'readme';
  132. $item->content = '# Documentation'.PHP_EOL.'Utilisez le bouton + en bas de la barre latérale pour ajouter des ressources...';
  133. $item->sketch = $sketch->id;
  134. $item->save();
  135. header('location: index.php?module=hackpoint&page=sheet.sketch&id='.$sketch->id);
  136. break;
  137. //Suppression d'élement sketch
  138. case 'hackpoint_sketch_delete':
  139. Action::write(function(&$response){
  140. global $myUser,$_;
  141. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  142. require_once(__DIR__.SLASH.'Sketch.class.php');
  143. Sketch::removeById($_['id']);
  144. });
  145. break;
  146. /** RESOURCE **/
  147. //Récuperation d'une liste de resource
  148. case 'hackpoint_resource_search':
  149. Action::write(function(&$response){
  150. global $myUser,$_;
  151. require_once(__DIR__.SLASH.'Sketch.class.php');
  152. require_once(__DIR__.SLASH.'Resource.class.php');
  153. $sketch = Sketch::provide('sketch');
  154. if(!$sketch->state && $sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'read') ) throw new Exception("Sketch privé", 403);
  155. foreach(Resource::loadAll(array('sketch'=>$_['sketch']),array('sort')) as $resource){
  156. $row = $resource->toArray();
  157. $type = $resource->type();
  158. $row['type'] = $type;
  159. $response['rows'][] = $row;
  160. }
  161. });
  162. break;
  163. case 'hackpoint_resource_edit':
  164. Action::write(function(&$response){
  165. global $myUser,$_;
  166. require_once(__DIR__.SLASH.'Sketch.class.php');
  167. require_once(__DIR__.SLASH.'Resource.class.php');
  168. require_once(__DIR__.SLASH.'Resource.class.php');
  169. $item = Resource::provide('id',1);
  170. $sketch = $item->join('sketch');
  171. if(!$sketch->state && $sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'read') ) throw new Exception("Sketch privé", 403);
  172. $response = $item->toHtml();
  173. $response['resourceType'] = $item->type;
  174. });
  175. break;
  176. //Sauveagrde du contenu d'une resource
  177. case 'hackpoint_resource_save_content':
  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. $item = Resource::provide('id',1);
  183. $sketch = $item->join('sketch');
  184. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit')) throw new Exception("Permissions insuffisantes",403);
  185. $item->content = $_['content'];
  186. $item->save();
  187. });
  188. break;
  189. //Ajout ou modification d'élément resource
  190. case 'hackpoint_resource_save':
  191. Action::write(function(&$response){
  192. global $myUser,$_;
  193. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  194. require_once(__DIR__.SLASH.'Sketch.class.php');
  195. require_once(__DIR__.SLASH.'Resource.class.php');
  196. require_once(__DIR__.SLASH.'ResourceType.class.php');
  197. $item = Resource::provide('id',1);
  198. $sketch = $item->join('sketch');
  199. if( !is_object($sketch) || $sketch->id==0){
  200. $sketch = Sketch::getById($_['sketch']);
  201. }
  202. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit')) throw new Exception("Permissions insuffisantes",403);
  203. if(!isset($_['type']) && $item->id!=0) $_['type'] = $item->type;
  204. $type = ResourceType::types($_['type']);
  205. if(isset($_['label'])) $item->label = $_['label'];
  206. if($item->id==0){
  207. $item->label = $type['label'];
  208. $item->sort = 100;
  209. $item->type = $_['type'];
  210. if(isset($type['default'])) $item->content = $type['default'] ;
  211. $item->sketch = $_['sketch'];
  212. }
  213. $item->save();
  214. $response = $item->toArray();
  215. $response['type'] = $item->type();
  216. });
  217. break;
  218. case 'hackpoint_resource_sort':
  219. Action::write(function(&$response){
  220. global $myUser,$_;
  221. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  222. require_once(__DIR__.SLASH.'Sketch.class.php');
  223. require_once(__DIR__.SLASH.'Resource.class.php');
  224. require_once(__DIR__.SLASH.'ResourceType.class.php');
  225. foreach($_['sort'] as $sort=>$id){
  226. $resource = Resource::getById($id,1);
  227. $sketch = $resource->join('sketch');
  228. if($sketch->creator != $myUser->login) continue;
  229. $resource->sort = $sort;
  230. $resource->save();
  231. }
  232. });
  233. break;
  234. //Suppression d'élement resource
  235. case 'hackpoint_resource_delete':
  236. Action::write(function(&$response){
  237. global $myUser,$_;
  238. require_once(__DIR__.SLASH.'Sketch.class.php');
  239. require_once(__DIR__.SLASH.'Resource.class.php');
  240. $item = Resource::getById($_['id'],1);
  241. $sketch = $item->join('sketch');
  242. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'delete')) throw new Exception("Permissions insuffisantes",403);
  243. Resource::deleteById($_['id']);
  244. });
  245. break;
  246. //Suppression document
  247. case 'resource_delete_document':
  248. Action::write(function(&$response){
  249. global $myUser,$_;
  250. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  251. require_once(__DIR__.SLASH.'Resource.class.php');
  252. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  253. //Le premier argument est un namspace de sécurité
  254. //et assure que le fichier sera toujours cloisoné dans un contexte file/hackpoint/sketch
  255. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  256. File::delete('hackpoint'.SLASH.'sketch',$path);
  257. });
  258. break;
  259. case 'resource_add_document':
  260. Action::write(function(&$response){
  261. global $myUser,$_;
  262. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  263. require_once(__DIR__.SLASH.'Resource.class.php');
  264. $resource = Resource::provide();
  265. $folder = $resource->directory();
  266. if(!file_exists($folder)) mkdir($folder,0755,true);
  267. foreach ($_['files'] as $file) {
  268. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  269. $row = File::move(File::temp().$file['path'],str_replace(File::dir(),'',$folder).SLASH.$name);
  270. $row['url'] = 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode($file['name']));
  271. $row['oldPath'] = $file['path'];
  272. if(!in_array( getExt($file['name']), array('jpg','jpeg','png','bmp','svg'))){
  273. $row['icon'] = $file['icon'];//getExtIcon( getExt($file));
  274. }
  275. $response['files'][] = $row;
  276. }
  277. });
  278. break;
  279. //Download d'un fichier
  280. case 'hackpoint_download_file':
  281. Action::write(function(&$response){
  282. global $myUser,$_;
  283. $file = str_replace(array('..'),array(''),urldecode(base64_decode($_['file'])));
  284. $file = File::dir().'hackpoint'.SLASH.$file;
  285. File::downloadFile($file);
  286. });
  287. break;
  288. //Sauvegarde des configurations de hackpoint
  289. case 'hackpoint_setting_save':
  290. Action::write(function(&$response){
  291. global $myUser,$_,$conf;
  292. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  293. foreach(Configuration::setting('hackpoint') as $key=>$value){
  294. if(!is_array($value)) continue;
  295. $allowed[] = $key;
  296. }
  297. foreach ($_['fields'] as $key => $value) {
  298. if(in_array($key, $allowed))
  299. $conf->put($key,$value);
  300. }
  301. });
  302. break;
  303. }
  304. ?>