action.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. 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. /** RESOURCE **/
  146. //Récuperation d'une liste de resource
  147. case 'hackpoint_resource_search':
  148. Action::write(function(&$response){
  149. global $myUser,$_;
  150. require_once(__DIR__.SLASH.'Sketch.class.php');
  151. require_once(__DIR__.SLASH.'Resource.class.php');
  152. $sketch = Sketch::provide('sketch');
  153. if(!$sketch->state && $sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'read') ) throw new Exception("Sketch privé", 403);
  154. foreach(Resource::loadAll(array('sketch'=>$_['sketch']),array('sort')) as $resource){
  155. $row = $resource->toArray();
  156. $type = $resource->type();
  157. $row['type'] = $type;
  158. $response['rows'][] = $row;
  159. }
  160. });
  161. break;
  162. case 'hackpoint_resource_edit':
  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. require_once(__DIR__.SLASH.'Resource.class.php');
  168. $item = Resource::provide('id',1);
  169. $sketch = $item->join('sketch');
  170. if(!$sketch->state && $sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'read') ) throw new Exception("Sketch privé", 403);
  171. $response = $item->toHtml();
  172. $response['resourceType'] = $item->type;
  173. });
  174. break;
  175. //Sauveagrde du contenu d'une resource
  176. case 'hackpoint_resource_save_content':
  177. Action::write(function(&$response){
  178. global $myUser,$_;
  179. require_once(__DIR__.SLASH.'Sketch.class.php');
  180. require_once(__DIR__.SLASH.'Resource.class.php');
  181. $item = Resource::provide('id',1);
  182. $sketch = $item->join('sketch');
  183. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit')) throw new Exception("Permissions insuffisantes",403);
  184. $item->content = $_['content'];
  185. $item->save();
  186. });
  187. break;
  188. //Ajout ou modification d'élément resource
  189. case 'hackpoint_resource_save':
  190. Action::write(function(&$response){
  191. global $myUser,$_;
  192. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  193. require_once(__DIR__.SLASH.'Sketch.class.php');
  194. require_once(__DIR__.SLASH.'Resource.class.php');
  195. require_once(__DIR__.SLASH.'ResourceType.class.php');
  196. $item = Resource::provide('id',1);
  197. $sketch = $item->join('sketch');
  198. if( !is_object($sketch) || $sketch->id==0){
  199. $sketch = Sketch::getById($_['sketch']);
  200. }
  201. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'edit')) throw new Exception("Permissions insuffisantes",403);
  202. if(!isset($_['type']) && $item->id!=0) $_['type'] = $item->type;
  203. $type = ResourceType::types($_['type']);
  204. if(isset($_['label'])) $item->label = $_['label'];
  205. if($item->id==0){
  206. $item->label = $type['label'];
  207. $item->sort = 100;
  208. $item->type = $_['type'];
  209. if(isset($type['default'])) $item->content = $type['default'] ;
  210. $item->sketch = $_['sketch'];
  211. }
  212. $item->save();
  213. $response = $item->toArray();
  214. $response['type'] = $item->type();
  215. });
  216. break;
  217. case 'hackpoint_resource_sort':
  218. Action::write(function(&$response){
  219. global $myUser,$_;
  220. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  221. require_once(__DIR__.SLASH.'Sketch.class.php');
  222. require_once(__DIR__.SLASH.'Resource.class.php');
  223. require_once(__DIR__.SLASH.'ResourceType.class.php');
  224. foreach($_['sort'] as $sort=>$id){
  225. $resource = Resource::getById($id,1);
  226. $sketch = $resource->join('sketch');
  227. if($sketch->creator != $myUser->login) continue;
  228. $resource->sort = $sort;
  229. $resource->save();
  230. }
  231. });
  232. break;
  233. //Suppression d'élement resource
  234. case 'hackpoint_resource_delete':
  235. Action::write(function(&$response){
  236. global $myUser,$_;
  237. require_once(__DIR__.SLASH.'Sketch.class.php');
  238. require_once(__DIR__.SLASH.'Resource.class.php');
  239. $item = Resource::getById($_['id'],1);
  240. $sketch = $item->join('sketch');
  241. if($sketch->creator != $myUser->login && !$myUser->can('hackpoint',$sketch->id,'delete')) throw new Exception("Permissions insuffisantes",403);
  242. Resource::deleteById($_['id']);
  243. });
  244. break;
  245. //Suppression document
  246. case 'resource_delete_document':
  247. Action::write(function(&$response){
  248. global $myUser,$_;
  249. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  250. require_once(__DIR__.SLASH.'Resource.class.php');
  251. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  252. //Le premier argument est un namspace de sécurité
  253. //et assure que le fichier sera toujours cloisoné dans un contexte file/hackpoint/sketch
  254. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  255. File::delete('hackpoint'.SLASH.'sketch',$path);
  256. });
  257. break;
  258. case 'resource_add_document':
  259. Action::write(function(&$response){
  260. global $myUser,$_;
  261. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  262. require_once(__DIR__.SLASH.'Resource.class.php');
  263. $resource = Resource::provide();
  264. $folder = $resource->directory();
  265. if(!file_exists($folder)) mkdir($folder,0755,true);
  266. foreach ($_['files'] as $file) {
  267. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  268. $row = File::move(File::temp().$file['path'],str_replace(File::dir(),'',$folder).SLASH.$name);
  269. $row['url'] = 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode($file['name']));
  270. $row['oldPath'] = $file['path'];
  271. if(!in_array( getExt($file['name']), array('jpg','jpeg','png','bmp','svg'))){
  272. $row['icon'] = $file['icon'];//getExtIcon( getExt($file));
  273. }
  274. $response['files'][] = $row;
  275. }
  276. });
  277. break;
  278. //Download d'un fichier
  279. case 'hackpoint_download_file':
  280. Action::write(function(&$response){
  281. global $myUser,$_;
  282. $file = str_replace(array('..'),array(''),urldecode(base64_decode($_['file'])));
  283. $file = File::dir().'hackpoint'.SLASH.$file;
  284. File::downloadFile($file);
  285. });
  286. break;
  287. //Sauvegarde des configurations de hackpoint
  288. case 'hackpoint_setting_save':
  289. Action::write(function(&$response){
  290. global $myUser,$_,$conf;
  291. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  292. foreach(Configuration::setting('hackpoint') as $key=>$value){
  293. if(!is_array($value)) continue;
  294. $allowed[] = $key;
  295. }
  296. foreach ($_['fields'] as $key => $value) {
  297. if(in_array($key, $allowed))
  298. $conf->put($key,$value);
  299. }
  300. });
  301. break;
  302. }
  303. ?>