Type.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * Declare available resource types.
  4. *
  5. * @author valentin carruesco
  6. *
  7. * @category Core
  8. *
  9. * @license copyright
  10. */
  11. class Type
  12. {
  13. public static function get($uid)
  14. {
  15. $t = self::all();
  16. return $t[$uid];
  17. }
  18. public static function all()
  19. {
  20. $types = array();
  21. $types['readme'] = array(
  22. 'label' => 'README',
  23. 'extension' => 'md',
  24. 'codemirror' => array(
  25. 'smartIndent' => false,
  26. 'readOnly' => false,
  27. ),
  28. );
  29. $types['arduino'] = array(
  30. 'label' => 'Source Arduino',
  31. 'extension' => 'ino',
  32. 'codemirror' => array(
  33. 'mode' => 'clike',
  34. 'theme' => 'monokai',
  35. 'lineNumbers' => true,
  36. 'readOnly' => false,
  37. ),
  38. );
  39. $types['clike'] = array(
  40. 'label' => 'Source C++/C',
  41. 'extension' => 'cpp',
  42. 'codemirror' => array(
  43. 'mode' => 'clike',
  44. 'theme' => 'monokai',
  45. 'lineNumbers' => true,
  46. 'readOnly' => false,
  47. ),
  48. );
  49. $types['shell'] = array(
  50. 'label' => 'Shell',
  51. 'extension' => 'sh',
  52. 'codemirror' => array(
  53. 'mode' => 'shell',
  54. 'theme' => 'monokai',
  55. 'lineNumbers' => true,
  56. 'readOnly' => false,
  57. ),
  58. );
  59. $types['php'] = array(
  60. 'label' => 'Source PHP',
  61. 'extension' => 'php',
  62. 'codemirror' => array(
  63. 'mode' => 'php',
  64. 'theme' => 'monokai',
  65. 'lineNumbers' => true,
  66. 'readOnly' => false,
  67. ),
  68. );
  69. $types['python'] = array(
  70. 'label' => 'Source Python',
  71. 'extension' => 'py',
  72. 'codemirror' => array(
  73. 'mode' => 'python',
  74. 'theme' => 'monokai',
  75. 'lineNumbers' => true,
  76. 'readOnly' => false,
  77. ),
  78. );
  79. $types['c'] = array(
  80. 'label' => 'Source C++',
  81. 'extension' => 'cpp',
  82. 'codemirror' => array(
  83. 'mode' => 'clike',
  84. 'theme' => 'monokai',
  85. 'lineNumbers' => true,
  86. 'readOnly' => false,
  87. ),
  88. );
  89. $types['java'] = array(
  90. 'label' => 'Source JAVA',
  91. 'extension' => 'java',
  92. 'codemirror' => array(
  93. 'mode' => 'java',
  94. 'theme' => 'monokai',
  95. 'lineNumbers' => true,
  96. 'readOnly' => false,
  97. ),
  98. );
  99. $types['css'] = array(
  100. 'label' => 'Feuille CSS',
  101. 'extension' => 'css',
  102. 'codemirror' => array(
  103. 'mode' => 'css',
  104. 'theme' => 'monokai',
  105. 'lineNumbers' => true,
  106. 'readOnly' => false,
  107. ),
  108. );
  109. $types['javascript'] = array(
  110. 'label' => 'Source Javascript',
  111. 'extension' => 'js',
  112. 'codemirror' => array(
  113. 'mode' => 'javascript',
  114. 'theme' => 'monokai',
  115. 'lineNumbers' => true,
  116. 'readOnly' => false,
  117. ),
  118. );
  119. $types['image'] = array(
  120. 'label' => 'Image',
  121. 'upload' => array(
  122. 'url' => 'action.php?action=upload_resource',
  123. 'element' => '#resource p img:eq(0)',
  124. 'callback' => '$(\'#resource img:eq(0)\').attr(\'src\',r.url);',
  125. ),
  126. );
  127. $types['files'] = array(
  128. 'label' => 'Set de fichiers',
  129. 'upload' => array(
  130. 'url' => 'action.php?action=upload_resource_file',
  131. 'element' => '#dropZoneFiles',
  132. 'callback' => 'search_file();',
  133. ),
  134. 'extension' => 'zip',
  135. );
  136. $types['part'] = array(
  137. 'label' => 'Set de composants',
  138. 'extension' => 'part',
  139. );
  140. return $types;
  141. }
  142. public static function fromFileImport($file, $sketch, $type)
  143. {
  144. $resource = new Resource();
  145. $resource->sketch = $sketch->id;
  146. $stream = file_get_contents($file['tmp_name']);
  147. $resource->label = $file['name'];
  148. $resource->type = $type;
  149. switch ($resource->type) {
  150. case 'arduino':
  151. case 'php':
  152. case 'shell':
  153. case 'python':
  154. case 'c':
  155. case 'css':
  156. case 'javascript':
  157. case 'java':
  158. case 'readme':
  159. $resource->content = file_get_contents($file['tmp_name']);
  160. $enc = mb_detect_encoding($resource->content, 'UTF-8, ISO-8859-1, GBK');
  161. if ($enc != 'UTF-8') {
  162. $resource->content = iconv($enc, 'utf-8', $resource->content);
  163. }
  164. $resource->save();
  165. break;
  166. case 'files':
  167. $resource->save();
  168. $path = SKETCH_PATH.'/'.$resource->id;
  169. if (!file_exists($path)) {
  170. mkdir($path);
  171. }
  172. $zip = new ZipArchive();
  173. $res = $zip->open($file['tmp_name']);
  174. if ($res === true) {
  175. $zip->extractTo($path);
  176. $zip->close();
  177. }
  178. break;
  179. case 'image':
  180. $resource->save();
  181. $name = $resource->id.'.'.$ext;
  182. file_put_contents(SKETCH_PATH.$name, $stream);
  183. $resource->content = $name;
  184. $resource->save();
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. public static function fromImport($res, $sketch)
  191. {
  192. global $myUser;
  193. $resource = new Resource();
  194. $resource->fromArray($res);
  195. $resource->id = null;
  196. $resource->sketch = $sketch->id;
  197. $stream = '';
  198. switch ($resource->type) {
  199. case 'image':
  200. $stream = base64_decode($resource->content);
  201. $resource->content = '';
  202. break;
  203. case 'part':
  204. $parts = $resource->content;
  205. $resource->content = '';
  206. break;
  207. case 'files':
  208. $files = $resource->content;
  209. $resource->content = '';
  210. break;
  211. default:
  212. break;
  213. }
  214. if (is_string($resource->content)) {
  215. $resource->content = htmlspecialchars_decode($resource->content);
  216. }
  217. $resource->save();
  218. switch ($resource->type) {
  219. case 'image':
  220. $resource->content = $resource->id.'.png';
  221. file_put_contents(SKETCH_PATH.$resource->content, $stream);
  222. $resource->save();
  223. break;
  224. case 'part':
  225. foreach ($parts as $p):
  226. $part = new Part();
  227. $part->fromArray($p['part']);
  228. $part->id = null;
  229. $stream = base64_decode($part->image);
  230. $part->owner = $myUser->id;
  231. $part->save();
  232. $name = $part->id.'.png';
  233. file_put_contents(PART_PATH.$name, $stream);
  234. $part->image = $name;
  235. $part->save();
  236. $resourcePart = new ResourcePart();
  237. $resourcePart->fromArray($p['resourcePart']);
  238. $resourcePart->id = null;
  239. $resourcePart->part = $part->id;
  240. $resourcePart->resource = $resource->id;
  241. $resourcePart->save();
  242. endforeach;
  243. $resource->content = '';
  244. $resource->save();
  245. break;
  246. case 'files':
  247. $folder = SKETCH_PATH.'/'.$resource->id;
  248. if (!file_exists($folder)) {
  249. mkdir($folder);
  250. }
  251. foreach ($files as $file):
  252. $stream = base64_decode($file['stream']);
  253. file_put_contents($folder.'/'.$file['label'], $stream);
  254. endforeach;
  255. $resource->content = '';
  256. $resource->save();
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. public static function toExport($resource)
  263. {
  264. $resource = $resource->toArray();
  265. switch ($resource['type']) {
  266. case 'image':
  267. $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
  268. break;
  269. case 'part':
  270. $resource['content'] = array();
  271. foreach (ResourcePart::loadAll(array('resource' => $resource['id'])) as $resourcePart):
  272. $part = $resourcePart->part_object;
  273. $part = $part->toArray();
  274. $resourcePart = $resourcePart->toArray();
  275. if ($part['image'] != '') {
  276. $part['image'] = base64_encode(file_get_contents(PART_PATH.$part['image']));
  277. }
  278. $resource['content'][] = array('resourcePart' => $resourcePart, 'part' => $part);
  279. endforeach;
  280. break;
  281. case 'files':
  282. $resource['content'] = array();
  283. $folder = SKETCH_PATH.'/'.$resource['id'];
  284. foreach (glob($folder.'/*') as $file):
  285. $resource['content'][] = array('label' => basename($file), 'stream' => base64_encode(file_get_contents($file)));
  286. endforeach;
  287. break;
  288. default:
  289. $resource['content'] = htmlspecialchars(SKETCH_PATH.$resource['content']);
  290. break;
  291. }
  292. return $resource;
  293. }
  294. public static function toHtml($resource, $sketch)
  295. {
  296. global $myUser;
  297. $response = array();
  298. $response = $resource->toArray();
  299. $type = self::get($resource->type);
  300. switch ($resource->type) {
  301. case 'image':
  302. $image = $response['content'] == '' ? 'img/default_image.png' : 'action.php?action=get_resource_image&id='.$response['id'];
  303. $response['content'] = '<img style="width:100%;height:auto;" class="dropzone" src="'.$image.'" />';
  304. if ($myUser->id == $sketch->owner) {
  305. $response['upload'] = $type['upload'];
  306. }
  307. break;
  308. case 'files':
  309. if ($myUser->id == $sketch->owner) {
  310. $response['upload'] = $type['upload'];
  311. $response['content'] = '<div id="dropZoneFiles"><i class="fa fa-file-text-o"></i> Faites glisser vos fichiers ici</div>';
  312. }
  313. $response['callback'] = 'init_file();';
  314. $response['content'] .= '<table class="table table-stripped table-bordered" id="files"><thead>
  315. <tr>
  316. <th>Fichiers disponibles <a class="btn btn-primary" href="action.php?action=download_file&resource='.$resource->id.'" style="float:right;" title="Télécharger les fichiers"><i class="fa fa-file-zip-o"></i> Télécharger</a></th>';
  317. $response['content'] .= '<th style="width:50px;"></th>';
  318. $response['content'] .= '</tr></thead><tbody>';
  319. $response['content'] .= '<tr style="display:none" data-id="{{id}}">
  320. <td ><a href="action.php?action=get_resource_file&id={{resource}}&file={{label}}"><i class="fa {{icon}}"></i> {{label}}</a></td>';
  321. $response['content'] .= '<td>';
  322. if ($myUser->id == $sketch->owner) {
  323. $response['content'] .= '<div class="btn btn-danger" onclick="delete_file(this);"><i class="fa fa-times" ></i></div>';
  324. }
  325. $response['content'] .= '</td>';
  326. $response['content'] .= '</tr>';
  327. $response['content'] .= '</tbody></table>';
  328. break;
  329. case 'part':
  330. $response['callback'] = 'init_part();';
  331. $response['content'] = '<table class="table table-stripped table-bordered" id="parts"><thead>
  332. <tr>
  333. <th>Libellé</th>
  334. <!--<th>Lien</th>
  335. <th>Prix</th>-->';
  336. if ($myUser->id == $sketch->owner) {
  337. $response['content'] .= '<th></th>';
  338. }
  339. $response['content'] .= '</tr>';
  340. if ($myUser->id == $sketch->owner) {
  341. $response['content'] .= '<tr id="partForm" data-action="save_part" data-id="">
  342. <td><input type="text" id="label" class="form-control"></td>
  343. <!--<td><input type="url" id="link" class="form-control"></td>
  344. <td><input type="text" id="price" class="form-control input-mini"></td>-->
  345. <td><div class="btn btn-success" onclick="save_part();"><i class="fa fa-plus"></i></div></td>
  346. </tr>';
  347. }
  348. $response['content'] .= '</thead><tbody>';
  349. $response['content'] .= '<tr style="display:none" data-id="{{id}}">
  350. <td ><a href="{{link}}"><div class="componentImage"><img src="{{image}}"/></div> {{label}}</a> {{#price}}<code>{{price}} €</code>{{/price}}{{#brand}} <small>{{brand}}</small>{{/brand}}</td>';
  351. if ($myUser->id == $sketch->owner) {
  352. $response['content'] .= '<td><div class="btn btn-danger" onclick="delete_part(this);"><i class="fa fa-times" ></i></div></td>';
  353. }
  354. $response['content'] .= '</tr>';
  355. $response['content'] .= '</tbody></table>';
  356. break;
  357. }
  358. //for sources
  359. if (isset($type['codemirror'])) {
  360. $response['content'] = '<textarea>'.$response['content'].'</textarea>';
  361. $response['code'] = $type['codemirror'];
  362. if ($myUser->id != $sketch->owner) {
  363. $response['code']['readOnly'] = true;
  364. }
  365. }
  366. return $response;
  367. }
  368. public static function toFileStream($resource)
  369. {
  370. $type = self::get($resource->type);
  371. $file = (object) array('name' => slugify($resource->label), 'content' => '');
  372. if (isset($type['extension'])) {
  373. $file->name .= '.'.$type['extension'];
  374. }
  375. switch ($resource->type) {
  376. case 'part':
  377. $file->content = '> '.strtoupper($resource->label).PHP_EOL;
  378. $file->content .= str_repeat('=', strlen($resource->label) + 2).PHP_EOL;
  379. foreach (ResourcePart::loadAll(array('resource' => $resource->id)) as $resourcePart):
  380. $part = $resourcePart->part_object;
  381. $file->content .= $part->label."\t";
  382. if (isset($part->link) && !empty($part->link)) {
  383. $file->content .= $part->link."\t";
  384. }
  385. if (isset($part->price) && !empty($part->price)) {
  386. $file->content .= $part->price.'€';
  387. }
  388. $file->content .= PHP_EOL;
  389. endforeach;
  390. break;
  391. case 'files':
  392. global $myUser;
  393. $path = SKETCH_PATH.'/'.$resource->id;
  394. $filename = $resource->label.'-'.time().'.zip';
  395. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
  396. $zip = new ZipArchive();
  397. if (file_exists($filepath)) {
  398. unlink($filepath);
  399. }
  400. $res = $zip->open($filepath, ZipArchive::CREATE);
  401. if ($res === true) {
  402. foreach (glob($path.'/*') as $f) {
  403. $zip->addFile($f, basename($f));
  404. }
  405. $zip->close();
  406. }
  407. $file->name = slugify($resource->label).'.zip';
  408. $file->content = file_get_contents($filepath);
  409. break;
  410. case 'image':
  411. $ext = getExt($resource->content);
  412. $file->name = slugify($resource->label).'.'.$ext;
  413. if (file_exists(SKETCH_PATH.$resource->content)) {
  414. $file->content = file_get_contents(SKETCH_PATH.$resource->content);
  415. }
  416. break;
  417. default:
  418. $file->content = html_entity_decode($resource->content);
  419. break;
  420. }
  421. return $file;
  422. }
  423. }