action.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <?php
  2. require_once __DIR__.DIRECTORY_SEPARATOR."common.php";
  3. if (!isset($_['action'])) {
  4. throw new Exception('Action inexistante');
  5. }
  6. //Execution du code en fonction de l'action
  7. switch ($_['action']) {
  8. case 'login':
  9. global $myUser;
  10. try {
  11. $myUser = User::check($_['login'], $_['password']);
  12. if (!$myUser->connected()) {
  13. throw new Exception('Utilisateur inexistant');
  14. }
  15. $_SESSION['currentUser'] = serialize($myUser);
  16. } catch (Exception $e) {
  17. $_SESSION['error'] = $e->getMessage();
  18. }
  19. header('location: index.php');
  20. break;
  21. case 'logout':
  22. unset($_SESSION['currentUser']);
  23. session_destroy();
  24. header('location: index.php');
  25. break;
  26. case 'save_user':
  27. try {
  28. global $myUser;
  29. if (!$myUser->connected()) {
  30. throw new Exception("Permission refusée, seul un connecté peux faire ça");
  31. }
  32. if ($myUser->id!=$_['id']) {
  33. throw new Exception("Permission refusée, seul le propriétaire du compte peux faire ça");
  34. }
  35. if ($_['password']!=$_['confirmPassword']) {
  36. throw new Exception("Les deux mot de passe ne correspondent pas");
  37. }
  38. $myUser->password = User::password_encrypt($_['password']);
  39. $myUser->save();
  40. $_SESSION['success'] = "Mot de passe modifié avec succès";
  41. } catch (Exception $e) {
  42. $_SESSION['error'] = $e->getMessage();
  43. }
  44. header('location: account.php');
  45. break;
  46. // SKETCH
  47. case 'create_sketch':
  48. Action::write(function ($_, &$response) {
  49. global $myUser;
  50. if (!$myUser->connected()) {
  51. throw new Exception("Permission refusée, seul un connecté peux faire ça");
  52. }
  53. $sketch = new Sketch();
  54. $sketch->fromArray($_);
  55. $sketch->owner = $myUser->id;
  56. $sketch->save();
  57. $resource = new Resource();
  58. $resource->label = 'README';
  59. $resource->type = 'readme';
  60. $resource->sketch = $sketch->id;
  61. $resource->content = 'Décrivez votre projet ici...';
  62. $resource->sort = 0;
  63. $resource->save();
  64. $response = $sketch->toArray();
  65. });
  66. break;
  67. case 'export_sketch':
  68. global $myUser;
  69. $response = array();
  70. try {
  71. $sketch = Sketch::getById($_['id']);
  72. if (!$sketch->public && $myUser->id!=$sketch->owner) {
  73. throw new Exception('Ce sketch est privé');
  74. }
  75. $response['sketch'] = $sketch->toArray();
  76. $response['resources'] = array();
  77. foreach (Resource::loadAll(array('sketch'=>$_['id'])) as $resource) {
  78. $response['resources'][] = Type::toExport($resource);
  79. }
  80. } catch (Exception $e) {
  81. $response['error'] = $e->getMessage();
  82. }
  83. $response = gzdeflate(json_encode($response));
  84. if (!isset($_['api'])) {
  85. $filename = slugify($sketch->label).('.export.').date('d-m-Y_H-i').'.json';
  86. header("Content-Type: application/json");
  87. header("Content-Length: " . strlen($response));
  88. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  89. header('Cache-Control: no-store, no-cache, must-revalidate');
  90. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  91. header('Cache-Control: post-check=0, pre-check=0', false);
  92. header('Pragma: no-cache');
  93. header("Content-Disposition: attachment; filename=\"".$filename."\"");
  94. }
  95. echo $response;
  96. break;
  97. case 'import_sketch':
  98. Action::write(function ($_, &$response) {
  99. global $myUser;
  100. if (!$myUser->connected()) {
  101. throw new Exception("Permission refusée, seul un connecté peux faire ça");
  102. }
  103. if ($_['from'] == 'url') {
  104. if (!isset($_['url']) || empty($_['url'])) {
  105. throw new Exception("Adresse du sketch invalide");
  106. }
  107. $url = parse_url($_['url']);
  108. parse_str($url['query'], $parameters);
  109. if (!isset($parameters['id']) || empty($parameters['id']) || !is_numeric($parameters['id'])) {
  110. throw new Exception("ID du sketch invalide");
  111. }
  112. $contentPath = $url['scheme'].'://'.$url['host'].'/'.substr($url['path'], 0, -11).'/action.php?action=export_sketch&id='.$parameters['id'].'&api=true';
  113. } else {
  114. $ext = getExt($_FILES['file']['name']);
  115. if ($ext!='json') {
  116. throw new Exception('Extension JSON autorisée uniquement');
  117. }
  118. $contentPath = $_FILES['file']['tmp_name'];
  119. }
  120. $stream = false;
  121. try {
  122. $stream = @file_get_contents($contentPath);
  123. } catch (Exception $a) {
  124. }
  125. if ($stream === false) {
  126. throw new Exception("Impossible d'atteindre le contenu hackpoint : $contentPath ");
  127. }
  128. $stream = gzinflate($stream);
  129. if ($stream === false) {
  130. throw new Exception('Impossible de décompresser le sketch...');
  131. }
  132. $json = json_decode($stream, true);
  133. if ($json == false) {
  134. throw new Exception('Impossible de parser la réponse du hackpoint, json invalide :'.$stream);
  135. }
  136. if (isset($json['error'])) {
  137. throw new Exception($json['error']);
  138. }
  139. $sketch = new Sketch();
  140. $sketch->fromArray($json['sketch']);
  141. $sketch->id = null;
  142. $sketch->owner = $myUser->id;
  143. $sketch->public = 0;
  144. $sketch->label = $sketch->label .='-import-'.date('d/m/Y H:i');
  145. $sketch->save();
  146. foreach ($json['resources'] as $res) {
  147. Type::fromImport($res, $sketch);
  148. }
  149. });
  150. break;
  151. case 'search_sketch':
  152. Action::write(function ($_, &$response) {
  153. global $myUser;
  154. $sketchs = Sketch::loadAll(array('owner'=>$myUser->id));
  155. foreach ($sketchs as $sketch) {
  156. $sketch->label = html_entity_decode($sketch->label);
  157. $response['rows'][] = $sketch->toArray();
  158. }
  159. });
  160. break;
  161. case 'delete_sketch':
  162. Action::write(function ($_, &$response) {
  163. global $myUser;
  164. $sketch = Sketch::getById($_['id']);
  165. if ($myUser->id != $sketch->owner) {
  166. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  167. }
  168. Part::staticQuery("delete FROM ".ResourcePart::tableName()." WHERE resource IN(SELECT id FROM ".Resource::tableName()." WHERE sketch=".$_['id'].") ");
  169. foreach (Resource::loadAll(array('sketch'=>$_['id'])) as $resource):
  170. $resource->remove();
  171. endforeach;
  172. Sketch::deleteById($_['id']);
  173. });
  174. break;
  175. case 'save_sketch_title':
  176. Action::write(function ($_, &$response) {
  177. global $myUser;
  178. $sketch = Sketch::getById($_['id']);
  179. if ($myUser->id != $sketch->owner) {
  180. return;
  181. }
  182. $sketch->fromArray($_);
  183. $sketch->save();
  184. });
  185. break;
  186. case 'toggle_share_sketch':
  187. Action::write(function ($_, &$response) {
  188. global $myUser;
  189. $sketch = Sketch::getById($_['id']);
  190. if ($myUser->id != $sketch->owner) {
  191. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  192. }
  193. $sketch->public = $_['state'];
  194. $sketch->save();
  195. });
  196. break;
  197. case 'download_sketch':
  198. $sketch = Sketch::getByid($_['id']);
  199. $resources = Resource::loadAll(array('sketch'=>$sketch->id), 'sort');
  200. $filename = $sketch->slug.'-'.time().'.zip';
  201. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
  202. $zip = new ZipArchive;
  203. $res = $zip->open($filepath, ZipArchive::CREATE);
  204. if ($res === true) {
  205. foreach ($resources as $resource) {
  206. $type = Type::get($resource->type);
  207. $file = Type::toFileStream($resource);
  208. $zip->addFromString($file->name, $file->content);
  209. }
  210. $zip->close();
  211. }
  212. header("Content-Type: application/zip");
  213. header("Content-Length: " . filesize($filepath));
  214. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  215. header('Cache-Control: no-store, no-cache, must-revalidate');
  216. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  217. header('Cache-Control: post-check=0, pre-check=0', false);
  218. header('Pragma: no-cache');
  219. header("Content-Disposition: attachment; filename=\"".$filename."\"");
  220. readfile($filepath);
  221. unlink($filepath);
  222. break;
  223. // RESOURCES
  224. case 'upload_resource':
  225. Action::write(function ($_, &$response) {
  226. global $myUser;
  227. $resource = Resource::getByid($_['id']);
  228. $sketch = Sketch::getById($resource->sketch);
  229. $ext = getExt($_FILES['file']['name']);
  230. if ($myUser->id != $sketch->owner) {
  231. throw new Exception("Seul le propriétaire du sketch peux faire ça");
  232. }
  233. if (!in_array($ext, explode(',', ALLOWED_RESOURCE_IMAGE))) {
  234. throw new Exception('Extensions autorisées '.ALLOWED_RESOURCE_IMAGE);
  235. }
  236. if ($_FILES['file']['size']>ALLOWED_RESOURCE_SIZE) {
  237. throw new Exception('Taille maximum autorisée '.ALLOWED_RESOURCE_SIZE.' o');
  238. }
  239. $name = $resource->id.'.'.$ext;
  240. $path = SKETCH_PATH.$name;
  241. move_uploaded_file($_FILES['file']['tmp_name'], $path);
  242. $resource->content = $name;
  243. $resource->save();
  244. $response = array_merge(Type::get($resource->type));
  245. $response['url'] = $path.'?v='.time();
  246. });
  247. break;
  248. case 'upload_resource_file':
  249. Action::write(function ($_, &$response) {
  250. global $myUser;
  251. $resource = Resource::getByid($_['id']);
  252. $sketch = Sketch::getById($resource->sketch);
  253. $ext = getExt($_FILES['file']['name']);
  254. if ($myUser->id != $sketch->owner) {
  255. throw new Exception("Seul le propriétaire du sketch peux faire ça");
  256. }
  257. if (ALLOWED_RESOURCE_FILE!='' && !in_array($ext, explode(',', ALLOWED_RESOURCE_FILE))) {
  258. throw new Exception('Extensions autorisées '.ALLOWED_RESOURCE_FILE);
  259. }
  260. if ($_FILES['file']['size']>ALLOWED_RESOURCE_SIZE) {
  261. throw new Exception('Taille maximum autorisée '.ALLOWED_RESOURCE_SIZE.' o');
  262. }
  263. $name = $resource->id;
  264. $folder = SKETCH_PATH.$name;
  265. if (!file_exists($folder)) {
  266. mkdir($folder);
  267. }
  268. $path = $folder.'/'.$_FILES['file']['name'];
  269. move_uploaded_file($_FILES['file']['tmp_name'], $path);
  270. $response = array_merge(Type::get($resource->type));
  271. $response['url'] = $path.'?v='.time();
  272. });
  273. break;
  274. case 'search_resources':
  275. Action::write(function ($_, &$response) {
  276. if (!isset($_['id']) || !is_numeric($_['id'])) {
  277. throw new Exception("Sketch non spécifié");
  278. }
  279. $resources = Resource::loadAll(array('sketch'=>$_['id']), 'sort');
  280. foreach ($resources as $resource) {
  281. $resource->label = html_entity_decode($resource->label);
  282. $resource->content = null;
  283. $response['rows'][] = $resource->toArray();
  284. }
  285. });
  286. break;
  287. case 'import_resource':
  288. global $myUser;
  289. $sketch = Sketch::getByid($_['id']);
  290. if ($myUser->id != $sketch->owner) {
  291. return;
  292. }
  293. $ext = explode('.', $_FILES['file']['name']);
  294. $ext = strtolower(array_pop($ext));
  295. $types = Type::all();
  296. $type = 'readme';
  297. foreach ($types as $uid=>$tp) {
  298. if (isset($tp['extension']) && $ext == $tp['extension']) {
  299. $type = $uid;
  300. }
  301. }
  302. Type::fromFileImport($_FILES['file'], $sketch, $type);
  303. break;
  304. //COMPONENT
  305. case 'upload_component_image':
  306. global $myUser;
  307. $ext = explode('.', $_FILES['file']['name']);
  308. $ext = strtolower(array_pop($ext));
  309. if (!in_array($ext, explode(',', ALLOWED_RESOURCE_IMAGE))) {
  310. exit();
  311. }
  312. imageResize($_FILES['file']['tmp_name'], 100, 100);
  313. echo 'data:image/png;base64,'.base64_encode(file_get_contents($_FILES['file']['tmp_name']));
  314. break;
  315. case 'search_component':
  316. Action::write(function ($_, &$response) {
  317. global $myUser;
  318. $parts = Part::populate();
  319. foreach ($parts as $part) {
  320. $part->label = html_entity_decode($part->label);
  321. $part->link = html_entity_decode($part->link);
  322. if ($part->image=='') {
  323. $part->image = 'img/default_image.png';
  324. } else {
  325. $part->image = PART_PATH.html_entity_decode($part->image);
  326. }
  327. $response['rows'][] = $part->toArray();
  328. }
  329. });
  330. break;
  331. case 'delete_component':
  332. Action::write(function ($_, &$response) {
  333. global $myUser;
  334. $part = Part::getById($_['id']);
  335. if ($myUser->id!=$part->owner) {
  336. throw new Exception('Seul le propriétaire du composant peux faire ça');
  337. }
  338. $part->remove();
  339. });
  340. break;
  341. case 'save_component':
  342. Action::write(function ($_, &$response) {
  343. global $myUser;
  344. if (!$myUser->connected()) {
  345. throw new Exception("Permission refusée, seul un connecté peux faire ça");
  346. }
  347. $part = isset($_['id']) && is_numeric($_['id'])?Part::getById($_['id']):new Part();
  348. $part->fromArray($_);
  349. $part->owner = $myUser->id;
  350. $part->save();
  351. if (substr($part->image, 0, 10) == 'data:image') {
  352. $imageName = $part->id.'.png';
  353. base64_to_image($part->image, PART_PATH.$imageName);
  354. $part->image = $imageName;
  355. $part->save();
  356. }
  357. $response = $part->toArray();
  358. });
  359. break;
  360. case 'edit_component':
  361. Action::write(function ($_, &$response) {
  362. $part = isset($_['id'])? Part::getById($_['id']):new Part;
  363. $part->label = html_entity_decode($part->label);
  364. $part->link = html_entity_decode($part->link);
  365. $part->brand = html_entity_decode($part->brand);
  366. if ($part->image=='') {
  367. $part->image = 'img/default_image.png';
  368. } else {
  369. $part->image = PART_PATH.html_entity_decode($part->image);
  370. }
  371. $part->image.='?t='.time();
  372. $response = $part->toArray();
  373. });
  374. break;
  375. //RESOURCE
  376. case 'save_resource':
  377. Action::write(function ($_, &$response) {
  378. global $myUser;
  379. $sketch = Sketch::getById($_['sketch']);
  380. if ($myUser->id != $sketch->owner) {
  381. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  382. }
  383. $resource = isset($_['id']) && is_numeric($_['id']) && !empty($_['id']) ? Resource::getByid($_['id']) : new Resource();
  384. $resource->fromArray($_);
  385. $resource->sort = 10;
  386. if ($resource->type=='readme' && $resource->id==0) {
  387. $resource->sort = 0;
  388. }
  389. $resource->label = $resource->label == ''?'Sans titre '.date('d-m-Y H:i'):$resource->label;
  390. $resource->save();
  391. $response = $resource->toArray();
  392. });
  393. break;
  394. case 'save_resource_content':
  395. Action::write(function ($_, &$response) {
  396. global $myUser;
  397. $resource = Resource::getByid($_['id']);
  398. $sketch = Sketch::getById($resource->sketch);
  399. if ($myUser->id != $sketch->owner) {
  400. return;
  401. }
  402. $resource->fromArray($_);
  403. $resource->save();
  404. });
  405. break;
  406. case 'get_resource_file':
  407. global $myUser;
  408. $resource = Resource::getById($_['id']);
  409. $sketch =$resource->sketch_object;
  410. if ($myUser->id != $sketch->owner && !$sketch->public) {
  411. echo 'Désolé, vous n\'avez pas d\'accès à cette ressource...';
  412. return;
  413. }
  414. $filepath = SKETCH_PATH.$resource->id.'/'.$_['file'];
  415. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  416. $mime = finfo_file($finfo, $filepath);
  417. header('Content-Type: '.$mime);
  418. header("Content-Length: " . filesize($filepath));
  419. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  420. header('Cache-Control: no-store, no-cache, must-revalidate');
  421. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  422. header('Cache-Control: post-check=0, pre-check=0', false);
  423. header('Pragma: no-cache');
  424. header("Content-Disposition: attachment; filename=\"".basename($filepath)."\"");
  425. echo file_get_contents($filepath);
  426. finfo_close($finfo);
  427. break;
  428. case 'get_resource_image':
  429. global $myUser;
  430. $resource = Resource::getById($_['id']);
  431. $sketch =$resource->sketch_object;
  432. if ($myUser->id != $sketch->owner && !$sketch->public) {
  433. readfile('img/default_image.png');
  434. return;
  435. }
  436. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  437. $filepath = SKETCH_PATH.$resource->content;
  438. $mime = finfo_file($finfo, $filepath);
  439. header('Content-Type: '.$mime);
  440. readfile($filepath);
  441. break;
  442. case 'edit_resource':
  443. Action::write(function ($_, &$response) {
  444. $resource = Resource::getById($_['id']);
  445. global $myUser;
  446. $sketch = Sketch::getById($resource->sketch);
  447. $resource->label = html_entity_decode($resource->label);
  448. $response = Type::toHtml($resource, $sketch);
  449. });
  450. break;
  451. case 'delete_resource':
  452. Action::write(function ($_, &$response) {
  453. global $myUser;
  454. $resource = Resource::getById($_['id']);
  455. $sketch = Sketch::getById($resource->sketch);
  456. if ($myUser->id != $sketch->owner) {
  457. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  458. }
  459. Resource::getById($_['id']);
  460. $resource->remove();
  461. });
  462. break;
  463. /*FILES*/
  464. case 'search_file':
  465. Action::write(function ($_, &$response) {
  466. global $myUser;
  467. if (!isset($_['id']) || !is_numeric($_['id'])) {
  468. throw new Exception("Ressource non spécifiée");
  469. }
  470. $resource = Resource::getById($_['id']);
  471. $sketch = $resource->sketch_object;
  472. if ($myUser->id != $sketch->owner && !$sketch->public) {
  473. throw new Exception("Désolé, le sketch est privé");
  474. }
  475. foreach (glob(SKETCH_PATH.'/'.$_['id'].'/*') as $file):
  476. $icon = getExtIcon(getExt($file));
  477. $response['rows'][] = array('id'=>basename($file), 'icon'=>$icon, 'label'=>basename($file), 'resource'=>$resource->id);
  478. endforeach;
  479. });
  480. break;
  481. case 'download_file':
  482. global $myUser;
  483. $path = SKETCH_PATH.'/'.$_['resource'];
  484. $resource = Resource::getById($_['resource']);
  485. $sketch = $resource->sketch_object;
  486. if ($myUser->id != $sketch->owner && !$sketch->public) {
  487. throw new Exception("Permission refusée, le sketch est privé");
  488. }
  489. $filename = $resource->label.'-'.time().'.zip';
  490. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
  491. $zip = new ZipArchive;
  492. if (file_exists($filepath)) {
  493. unlink($filepath);
  494. }
  495. $res = $zip->open($filepath, ZipArchive::CREATE);
  496. if ($res === true) {
  497. foreach (glob($path.'/*') as $file) {
  498. $zip->addFile($file, basename($file));
  499. }
  500. $zip->close();
  501. }
  502. header("Content-Type: application/zip");
  503. header("Content-Length: " . filesize($filepath));
  504. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  505. header('Cache-Control: no-store, no-cache, must-revalidate');
  506. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  507. header('Cache-Control: post-check=0, pre-check=0', false);
  508. header('Pragma: no-cache');
  509. header("Content-Disposition: attachment; filename=\"".$filename."\"");
  510. readfile($filepath);
  511. unlink($filepath);
  512. break;
  513. case 'delete_file':
  514. Action::write(function ($_, &$response) {
  515. global $myUser;
  516. $path = SKETCH_PATH.'/'.$_['resource'].'/'.$_['id'];
  517. $resource = Resource::getById($_['resource']);
  518. $sketch = $resource->sketch_object;
  519. if ($myUser->id != $sketch->owner) {
  520. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  521. }
  522. if (file_exists($path)) {
  523. unlink($path);
  524. }
  525. });
  526. break;
  527. /*PART*/
  528. case 'search_part':
  529. Action::write(function ($_, &$response) {
  530. if (!isset($_['id']) || !is_numeric($_['id'])) {
  531. throw new Exception("Ressource non spécifiée");
  532. }
  533. $resourceParts = ResourcePart::loadAll(array('resource'=>$_['id']), 'sort');
  534. foreach ($resourceParts as $resourcePart):
  535. $part = $resourcePart->part_object;
  536. $part->label = html_entity_decode($part->label);
  537. $part->link = html_entity_decode($part->link);
  538. if ($part->image == '') {
  539. $part->image = 'img/default_image.png';
  540. } else {
  541. $part->image = PART_PATH.$part->image;
  542. }
  543. $line = $part->toArray();
  544. $line['id'] = $resourcePart->id;
  545. $response['rows'][] = $line;
  546. endforeach;
  547. });
  548. break;
  549. case 'delete_part':
  550. Action::write(function ($_, &$response) {
  551. global $myUser;
  552. $part = ResourcePart::getById($_['id']);
  553. $resource = $part->resource_object;
  554. $sketch = $resource->sketch_object;
  555. if ($myUser->id != $sketch->owner) {
  556. throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
  557. }
  558. ResourcePart::deleteById($_['id']);
  559. });
  560. break;
  561. case 'save_part':
  562. Action::write(function ($_, &$response) {
  563. global $myUser;
  564. $model = isset($_['model']) && is_numeric($_['model']) && !empty($_['model']) ? Part::getByid($_['model']) : new Part();
  565. if ($model->id==0) {
  566. $model->fromArray($_);
  567. $model->save();
  568. }
  569. $resourcePart = new ResourcePart();
  570. $resourcePart->fromArray($_);
  571. $resourcePart->part = $model->id;
  572. $resourcePart->save();
  573. });
  574. break;
  575. case 'autocomplete_part':
  576. global $myUser;
  577. $parts = Part::staticQuery("SELECT DISTINCT label,* FROM ".Part::tableName()." WHERE label LIKE ? GROUP BY label ORDER BY price ASC ", array('%'.$_['term'].'%'), true);
  578. $rows = array();
  579. levenshtein_deduplication($parts);
  580. foreach ($parts as $part):
  581. $part->image = PART_PATH.$part->image;
  582. $rows[] = array('label'=>$part->label.' ('.$part->price.'€)','value'=>$part->toArray());
  583. endforeach;
  584. echo json_encode($rows);
  585. break;
  586. default:
  587. break;
  588. }
  589. function levenshtein_deduplication(&$objs)
  590. {
  591. foreach ($objs as $obj1):
  592. foreach ($objs as $u=>$obj2):
  593. if ($obj1->id==$obj2->id) {
  594. continue;
  595. }
  596. if (levenshtein($obj1->label, $obj2->label) < 5) {
  597. unset($objs[$u]);
  598. }
  599. endforeach;
  600. endforeach;
  601. }