Type.class.php 12 KB

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