action.php 19 KB

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