action.php 13 KB

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