Type.class.php 13 KB

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