action.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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-warning';
  33. if($row['progress'] > 60) $row['progress-color'] = 'bg-info';
  34. if($row['progress'] > 75) $row['progress-color'] = '';
  35. if($row['progress'] > 90) $row['progress-color'] = 'bg-success';
  36. $row['created'] = relative_time($row['created']);
  37. $response['rows'][] = $row;
  38. }
  39. });
  40. break;
  41. //Ajout ou modification d'élément sketch
  42. case 'hackpoint_sketch_save':
  43. Action::write(function(&$response){
  44. global $myUser,$_;
  45. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  46. require_once(__DIR__.SLASH.'Sketch.class.php');
  47. $item = Sketch::getById($_['id']);
  48. if(isset($_['label'])) $item->label = $_['label'];
  49. $item->progress = 5;
  50. if(isset($_['state'])) $item->state = $_['state'] == 'true';
  51. if(isset($_['comment'])) $item->comment = $_['comment'];
  52. $item->save();
  53. });
  54. break;
  55. case 'hackpoint_sketch_save_cover':
  56. Action::write(function(&$response){
  57. global $myUser,$_;
  58. require_once(__DIR__.SLASH.'Sketch.class.php');
  59. if(!is_numeric($_['sketch'])) throw new Exception("Sketch non spécifié", 400);
  60. $sketch = Sketch::provide('sketch');
  61. if($myUser->login!= $sketch->creator) throw new Exception("Permission insuffisantes", 403);
  62. $folder = $sketch->directory();
  63. if(!file_exists($folder)) mkdir($folder,0755,true);
  64. $name = 'cover.jpg';
  65. $stream = preg_replace('|data\:image\/[^;]*;base64,|is','',$_['stream']);
  66. $filepath = $folder.SLASH.$name;
  67. $row = file_put_contents($filepath, base64_decode($stream));
  68. //Image::resize($filepath,150,150);
  69. $response['stream'] = 'data:image/jpg;base64,'.base64_encode(file_get_contents($filepath));
  70. });
  71. break;
  72. case 'hackpoint_sketch_progress_save':
  73. Action::write(function(&$response){
  74. global $myUser,$_;
  75. require_once(__DIR__.SLASH.'Sketch.class.php');
  76. $item = Sketch::getById($_['id']);
  77. if($myUser->login!=$item->creator) throw new Exception("Permissions insuffisantes",403);
  78. $item->progress = $_['progress'];
  79. $item->save();
  80. });
  81. break;
  82. case 'hackpoint_sketch_add':
  83. global $myUser,$_;
  84. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  85. require_once(__DIR__.SLASH.'Sketch.class.php');
  86. $sketch = new Sketch();
  87. $sketch->label = 'Sketch Sans titre';
  88. $sketch->state = false;
  89. $sketch->comment = 'Nouveau sketch sans commentaires';
  90. $sketch->save();
  91. require_once(__DIR__.SLASH.'Resource.class.php');
  92. $item = new Resource();
  93. $item->label = 'Documentation';
  94. $item->sort = 0;
  95. $item->type = 'readme';
  96. $item->content = '# Documentation';
  97. $item->sketch = $sketch->id;
  98. $item->save();
  99. header('location: index.php?module=hackpoint&page=sheet.sketch&id='.$sketch->id);
  100. break;
  101. //Suppression d'élement sketch
  102. case 'hackpoint_sketch_delete':
  103. Action::write(function(&$response){
  104. global $myUser,$_;
  105. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  106. require_once(__DIR__.SLASH.'Sketch.class.php');
  107. Sketch::removeById($_['id']);
  108. });
  109. break;
  110. //Sauvegarde des configurations de hackpoint
  111. case 'hackpoint_setting_save':
  112. Action::write(function(&$response){
  113. global $myUser,$_,$conf;
  114. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  115. foreach(Configuration::setting('hackpoint') as $key=>$value){
  116. if(!is_array($value)) continue;
  117. $allowed[] = $key;
  118. }
  119. foreach ($_['fields'] as $key => $value) {
  120. if(in_array($key, $allowed))
  121. $conf->put($key,$value);
  122. }
  123. });
  124. break;
  125. /** RESOURCE **/
  126. //Récuperation d'une liste de resource
  127. case 'hackpoint_resource_search':
  128. Action::write(function(&$response){
  129. global $myUser,$_;
  130. require_once(__DIR__.SLASH.'Sketch.class.php');
  131. require_once(__DIR__.SLASH.'Resource.class.php');
  132. $sketch = Sketch::provide('sketch');
  133. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Sketch privé", 403);
  134. foreach(Resource::loadAll(array('sketch'=>$_['sketch']),array('sort')) as $resource){
  135. $row = $resource->toArray();
  136. $type = $resource->type();
  137. $row['type'] = $type;
  138. $response['rows'][] = $row;
  139. }
  140. });
  141. break;
  142. case 'hackpoint_resource_edit':
  143. Action::write(function(&$response){
  144. global $myUser,$_;
  145. require_once(__DIR__.SLASH.'Sketch.class.php');
  146. require_once(__DIR__.SLASH.'Resource.class.php');
  147. require_once(__DIR__.SLASH.'Resource.class.php');
  148. $item = Resource::provide('id',1);
  149. $sketch = $item->join('sketch');
  150. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Sketch privé", 403);
  151. $response = $item->toHtml();
  152. $response['resourceType'] = $item->type;
  153. });
  154. break;
  155. //Sauveagrde du contenu d'une resource
  156. case 'hackpoint_resource_save_content':
  157. Action::write(function(&$response){
  158. global $myUser,$_;
  159. require_once(__DIR__.SLASH.'Sketch.class.php');
  160. require_once(__DIR__.SLASH.'Resource.class.php');
  161. $item = Resource::provide('id',1);
  162. $sketch = $item->join('sketch');
  163. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  164. $item->content = $_['content'];
  165. $item->save();
  166. });
  167. break;
  168. //Ajout ou modification d'élément resource
  169. case 'hackpoint_resource_save':
  170. Action::write(function(&$response){
  171. global $myUser,$_;
  172. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  173. require_once(__DIR__.SLASH.'Sketch.class.php');
  174. require_once(__DIR__.SLASH.'Resource.class.php');
  175. require_once(__DIR__.SLASH.'ResourceType.class.php');
  176. $item = Resource::provide('id',1);
  177. $sketch = $item->join('sketch');
  178. if( !is_object($sketch) || $sketch->id==0){
  179. $sketch = Sketch::getById($_['sketch']);
  180. }
  181. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  182. if(!isset($_['type']) && $item->id!=0) $_['type'] = $item->type;
  183. $type = ResourceType::types($_['type']);
  184. if(isset($_['label'])) $item->label = $_['label'];
  185. if($item->id==0){
  186. $item->label = $type['label'];
  187. $item->sort = 100;
  188. $item->type = $_['type'];
  189. if(isset($type['default'])) $item->content = $type['default'] ;
  190. $item->sketch = $_['sketch'];
  191. }
  192. $item->save();
  193. $response = $item->toArray();
  194. $response['type'] = $item->type();
  195. });
  196. break;
  197. case 'hackpoint_resource_sort':
  198. Action::write(function(&$response){
  199. global $myUser,$_;
  200. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  201. require_once(__DIR__.SLASH.'Sketch.class.php');
  202. require_once(__DIR__.SLASH.'Resource.class.php');
  203. require_once(__DIR__.SLASH.'ResourceType.class.php');
  204. foreach($_['sort'] as $sort=>$id){
  205. $resource = Resource::getById($id,1);
  206. $sketch = $resource->join('sketch');
  207. if($sketch->creator != $myUser->login) continue;
  208. $resource->sort = $sort;
  209. $resource->save();
  210. }
  211. });
  212. break;
  213. //Suppression d'élement resource
  214. case 'hackpoint_resource_delete':
  215. Action::write(function(&$response){
  216. global $myUser,$_;
  217. require_once(__DIR__.SLASH.'Sketch.class.php');
  218. require_once(__DIR__.SLASH.'Resource.class.php');
  219. $item = Resource::getById($_['id'],1);
  220. $sketch = $item->join('sketch');
  221. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  222. Resource::deleteById($_['id']);
  223. });
  224. break;
  225. //Sauvegarde des configurations de hackpoint
  226. case 'hackpoint_setting_save':
  227. Action::write(function(&$response){
  228. global $myUser,$_,$conf;
  229. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  230. foreach(Configuration::setting('hackpoint') as $key=>$value){
  231. if(!is_array($value)) continue;
  232. $allowed[] = $key;
  233. }
  234. foreach ($_['fields'] as $key => $value) {
  235. if(in_array($key, $allowed))
  236. $conf->put($key,$value);
  237. }
  238. });
  239. break;
  240. //Suppression document
  241. case 'resource_delete_document':
  242. Action::write(function(&$response){
  243. global $myUser,$_;
  244. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  245. require_once(__DIR__.SLASH.'Resource.class.php');
  246. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  247. //Le premier argument est un namspace de sécurité
  248. //et assure que le fichier sera toujours cloisoné dans un contexte file/hackpoint/sketch
  249. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  250. File::delete('hackpoint'.SLASH.'sketch',$path);
  251. });
  252. break;
  253. case 'resource_add_document':
  254. Action::write(function(&$response){
  255. global $myUser,$_;
  256. if(!$myUser->can('hackpoint','edit')) throw new Exception("Permissions insuffisantes",403);
  257. require_once(__DIR__.SLASH.'Resource.class.php');
  258. $resource = Resource::provide();
  259. $folder = $resource->directory();
  260. if(!file_exists($folder)) mkdir($folder,0755,true);
  261. foreach ($_['files'] as $file) {
  262. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  263. $row = File::move(File::temp().$file['path'],str_replace(File::dir(),'',$folder).SLASH.$name);
  264. $row['url'] = 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode($file['name']));
  265. $row['oldPath'] = $file['path'];
  266. if(!in_array( getExt($file['name']), array('jpg','jpeg','png','bmp','svg'))){
  267. $row['icon'] = $file['icon'];//getExtIcon( getExt($file));
  268. }
  269. $response['files'][] = $row;
  270. }
  271. });
  272. break;
  273. /** PART **/
  274. case 'hackpoint_part_search':
  275. Action::write(function(&$response){
  276. global $myUser,$_;
  277. require_once(__DIR__.SLASH.'Part.class.php');
  278. foreach(Part::loadAll(array('state'=>Part::ACTIVE)) as $part){
  279. $row = $part->toArray();
  280. $row['picture'] = $part->picture(true);
  281. $response['rows'][] = $row;
  282. }
  283. });
  284. break;
  285. //Récuperation d'une liste de part
  286. case 'hackpoint_resource_part_search':
  287. Action::write(function(&$response){
  288. global $myUser,$_;
  289. require_once(__DIR__.SLASH.'Sketch.class.php');
  290. require_once(__DIR__.SLASH.'Part.class.php');
  291. require_once(__DIR__.SLASH.'Resource.class.php');
  292. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  293. $item = Resource::provide('resource',1);
  294. $sketch = $item->join('sketch');
  295. if(!$sketch->state && $sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  296. foreach(ResourcePart::loadAll(array('resource'=>$_['resource']), null, null, array('*'),1) as $resourcepart){
  297. $part = $resourcepart->join('part');
  298. $row = $part->toArray();
  299. $row['picture'] = $part->picture(true);
  300. $row['id'] = $resourcepart->id;
  301. $row['part'] = $part->id;
  302. $response['rows'][] = $row;
  303. }
  304. });
  305. break;
  306. //Ajout ou modification d'élément part
  307. case 'hackpoint_part_save':
  308. Action::write(function(&$response){
  309. global $myUser,$_;
  310. require_once(__DIR__.SLASH.'Sketch.class.php');
  311. require_once(__DIR__.SLASH.'Part.class.php');
  312. require_once(__DIR__.SLASH.'Resource.class.php');
  313. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  314. $item = Resource::provide('resource',1);
  315. $sketch = $item->join('sketch');
  316. if($sketch->creator != $myUser->login) throw new Exception("Permissions insuffisantes",403);
  317. $part = Part::provide('part');
  318. $part->label = $_['label'];
  319. if(isset($_['price'])) $part->price = $_['price'];
  320. if(isset($_['link'])) $part->link = $_['link'];
  321. if(isset($_['brand'])) $part->brand = $_['brand'];
  322. $part->state = Part::ACTIVE;
  323. $part->save();
  324. if(isset($_['picture'])){
  325. $stream = base64_decode(preg_replace('|.*image/[^;]*;base64,|i','',$_['picture']));
  326. $dir = File::dir().'hackpoint'.SLASH.'part'.SLASH.$part->id;
  327. if(!file_exists($dir)) mkdir($dir,0755,true);
  328. file_put_contents($dir.SLASH.'cover.jpg', $stream);
  329. }
  330. $item = ResourcePart::provide();
  331. $item->part = $part->id;
  332. $item->resource = $_['resource'];
  333. $item->save();
  334. $response = $item->toArray();
  335. });
  336. break;
  337. //Suppression d'élement part
  338. case 'hackpoint_resource_part_delete':
  339. Action::write(function(&$response){
  340. global $myUser,$_;
  341. //if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  342. require_once(__DIR__.SLASH.'Sketch.class.php');
  343. require_once(__DIR__.SLASH.'Resource.class.php');
  344. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  345. require_once(__DIR__.SLASH.'Part.class.php');
  346. $resourcePart = ResourcePart::getById($_['id'],2);
  347. $resource = $resourcePart->join('resource');
  348. $sketch = $resource->join('sketch');
  349. if($sketch->creator!=$myUser->login) throw new Exception("Permissions insuffisantes",403);
  350. ResourcePart::deleteById($_['id']);
  351. });
  352. break;
  353. //Suppression d'élement part
  354. case 'hackpoint_part_delete':
  355. Action::write(function(&$response){
  356. global $myUser,$_;
  357. if(!$myUser->can('hackpoint','delete')) throw new Exception("Permissions insuffisantes",403);
  358. require_once(__DIR__.SLASH.'Part.class.php');
  359. Part::deleteById($_['id']);
  360. });
  361. break;
  362. //Download d'un fichier
  363. case 'hackpoint_download_file':
  364. Action::write(function(&$response){
  365. global $myUser,$_;
  366. $file = str_replace(array('..'),array(''),urldecode(base64_decode($_['file'])));
  367. $file = File::dir().'hackpoint'.SLASH.$file;
  368. File::downloadFile($file);
  369. });
  370. break;
  371. //Sauvegarde des configurations de hackpoint
  372. case 'hackpoint_setting_save':
  373. Action::write(function(&$response){
  374. global $myUser,$_,$conf;
  375. if(!$myUser->can('hackpoint','configure')) throw new Exception("Permissions insuffisantes",403);
  376. foreach(Configuration::setting('hackpoint') as $key=>$value){
  377. if(!is_array($value)) continue;
  378. $allowed[] = $key;
  379. }
  380. foreach ($_['fields'] as $key => $value) {
  381. if(in_array($key, $allowed))
  382. $conf->put($key,$value);
  383. }
  384. });
  385. break;
  386. case 'autocomplete_part':
  387. Action::write(function(&$response){
  388. require_once(__DIR__.SLASH.'Part.class.php');
  389. global $myUser,$_;
  390. if (!$myUser->connected()) throw new Exception("Error Processing Request", 1);
  391. new Exception("Vous devez être connecté!");
  392. $response['rows'] = array();
  393. $data = array("%".$_['keyword']."%",0);
  394. $parts = Part::staticQuery('SELECT * FROM {{table}} WHERE label LIKE ? AND state=? LIMIT 10',array("%".$_['keyword']."%",Part::ACTIVE),true);
  395. foreach($parts as $part){
  396. $response['rows'][] = array(
  397. 'name'=>html_entity_decode($part->label, ENT_QUOTES),
  398. 'id'=>$part->id,
  399. 'price'=>$part->price,
  400. 'brand'=>$part->brand,
  401. 'picture' => $part->picture(true)
  402. );
  403. }
  404. if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
  405. $list = json_decode(html_entity_decode($_['data']['before']),true);
  406. if(is_array($list)){
  407. foreach ($list as $key=>$value) {
  408. if(preg_match('/'.$_['keyword'].'/i', $value))
  409. array_unshift($response['rows'],array('name'=>$value,'id'=>$key));
  410. }
  411. }
  412. }
  413. });
  414. break;
  415. case 'get_part_by_id':
  416. Action::write(function(&$response){
  417. global $myUser,$_;
  418. require_once(__DIR__.SLASH.'Sketch.class.php');
  419. require_once(__DIR__.SLASH.'Resource.class.php');
  420. require_once(__DIR__.SLASH.'Part.class.php');
  421. $part = Part::getById($_['id'],1);
  422. $part = !$part ? new Part() : Part::getById($_['id']);
  423. $row = $part->toArray();
  424. $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
  425. if(isset($_['before']) && isset($_['before'])!=''){
  426. $list = json_decode(html_entity_decode($_['before']),true);
  427. if(is_array($list)){
  428. if(isset($list[$_['id']])) $row = array('label' => $list[$_['id']], 'id'=>$_['id']);
  429. }
  430. }
  431. $response['part'] = $row;
  432. });
  433. break;
  434. }
  435. ?>