Type.class.php 13 KB

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