action.php 19 KB

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