action.php 15 KB

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