Type.class.php 11 KB

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