'README', 'extension' => 'md', 'codemirror' => array( 'smartIndent' => false, 'readOnly' => false, ), ); $types['arduino'] = array( 'label' => 'Source Arduino', 'extension' => 'ino', 'codemirror' => array( 'mode' => 'clike', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['clike'] = array( 'label' => 'Source C++/C', 'extension' => 'cpp', 'codemirror' => array( 'mode' => 'clike', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['shell'] = array( 'label' => 'Shell', 'extension' => 'sh', 'codemirror' => array( 'mode' => 'shell', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['php'] = array( 'label' => 'Source PHP', 'extension' => 'php', 'codemirror' => array( 'mode' => 'php', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['python'] = array( 'label' => 'Source Python', 'extension' => 'py', 'codemirror' => array( 'mode' => 'python', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['c'] = array( 'label' => 'Source C++', 'extension' => 'cpp', 'codemirror' => array( 'mode' => 'clike', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['java'] = array( 'label' => 'Source JAVA', 'extension' => 'java', 'codemirror' => array( 'mode' => 'java', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['css'] = array( 'label' => 'Feuille CSS', 'extension' => 'css', 'codemirror' => array( 'mode' => 'css', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['javascript'] = array( 'label' => 'Source Javascript', 'extension' => 'js', 'codemirror' => array( 'mode' => 'javascript', 'theme' => 'monokai', 'lineNumbers' => true, 'readOnly' => false, ), ); $types['image'] = array( 'label' => 'Image', 'upload' => array( 'url' => 'action.php?action=upload_resource', 'element' => '#resource p img:eq(0)', 'callback' => '$(\'#resource img:eq(0)\').attr(\'src\',r.url);', ), ); $types['files'] = array( 'label' => 'Set de fichiers', 'upload' => array( 'url' => 'action.php?action=upload_resource_file', 'element' => '#dropZoneFiles', 'callback' => 'search_file();', ), 'extension' => 'zip', ); $types['part'] = array( 'label' => 'Set de composants', 'extension' => 'part', ); return $types; } public static function fromFileImport($file, $sketch, $type) { $resource = new Resource(); $resource->sketch = $sketch->id; $stream = file_get_contents($file['tmp_name']); $resource->label = $file['name']; $resource->type = $type; switch ($resource->type) { case 'arduino': case 'php': case 'shell': case 'python': case 'c': case 'css': case 'javascript': case 'java': case 'readme': $resource->content = file_get_contents($file['tmp_name']); $enc = mb_detect_encoding($resource->content, 'UTF-8, ISO-8859-1, GBK'); if ($enc != 'UTF-8') { $resource->content = iconv($enc, 'utf-8', $resource->content); } $resource->save(); break; case 'files': $resource->save(); $path = SKETCH_PATH.'/'.$resource->id; if (!file_exists($path)) { mkdir($path); } $zip = new ZipArchive(); $res = $zip->open($file['tmp_name']); if ($res === true) { $zip->extractTo($path); $zip->close(); } break; case 'image': $resource->save(); $name = $resource->id.'.'.$ext; file_put_contents(SKETCH_PATH.$name, $stream); $resource->content = $name; $resource->save(); break; default: break; } } public static function fromImport($res, $sketch) { global $myUser; $resource = new Resource(); $resource->fromArray($res); $resource->id = null; $resource->sketch = $sketch->id; $stream = ''; switch ($resource->type) { case 'image': $stream = base64_decode($resource->content); $resource->content = ''; break; case 'part': $parts = $resource->content; $resource->content = ''; break; case 'files': $files = $resource->content; $resource->content = ''; break; default: break; } if (is_string($resource->content)) { $resource->content = htmlspecialchars_decode($resource->content); } $resource->save(); switch ($resource->type) { case 'image': $resource->content = $resource->id.'.png'; file_put_contents(SKETCH_PATH.$resource->content, $stream); $resource->save(); break; case 'part': foreach ($parts as $p): $part = new Part(); $part->fromArray($p['part']); $part->id = null; $stream = base64_decode($part->image); $part->owner = $myUser->id; $part->save(); $name = $part->id.'.png'; file_put_contents(PART_PATH.$name, $stream); $part->image = $name; $part->save(); $resourcePart = new ResourcePart(); $resourcePart->fromArray($p['resourcePart']); $resourcePart->id = null; $resourcePart->part = $part->id; $resourcePart->resource = $resource->id; $resourcePart->save(); endforeach; $resource->content = ''; $resource->save(); break; case 'files': $folder = SKETCH_PATH.'/'.$resource->id; if (!file_exists($folder)) { mkdir($folder); } foreach ($files as $file): $stream = base64_decode($file['stream']); file_put_contents($folder.'/'.$file['label'], $stream); endforeach; $resource->content = ''; $resource->save(); break; default: break; } } public static function toExport($resource) { $resource = $resource->toArray(); switch ($resource['type']) { case 'image': $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content'])); break; case 'part': $resource['content'] = array(); foreach (ResourcePart::loadAll(array('resource' => $resource['id'])) as $resourcePart): $part = $resourcePart->part_object; $part = $part->toArray(); $resourcePart = $resourcePart->toArray(); if ($part['image'] != '') { $part['image'] = base64_encode(file_get_contents(PART_PATH.$part['image'])); } $resource['content'][] = array('resourcePart' => $resourcePart, 'part' => $part); endforeach; break; case 'files': $resource['content'] = array(); $folder = SKETCH_PATH.'/'.$resource['id']; foreach (glob($folder.'/*') as $file): $resource['content'][] = array('label' => basename($file), 'stream' => base64_encode(file_get_contents($file))); endforeach; break; default: $resource['content'] = htmlspecialchars(SKETCH_PATH.$resource['content']); break; } return $resource; } public static function toHtml($resource, $sketch) { global $myUser; $response = array(); $response = $resource->toArray(); $type = self::get($resource->type); switch ($resource->type) { case 'image': $image = $response['content'] == '' ? 'img/default_image.png' : 'action.php?action=get_resource_image&id='.$response['id']; $response['content'] = ''; if ($myUser->id == $sketch->owner) { $response['upload'] = $type['upload']; } break; case 'files': if ($myUser->id == $sketch->owner) { $response['upload'] = $type['upload']; $response['content'] = '
Faites glisser vos fichiers ici
'; } $response['callback'] = 'init_file();'; $response['content'] .= ''; $response['content'] .= ''; $response['content'] .= ''; $response['content'] .= ''; $response['content'] .= ''; $response['content'] .= ''; $response['content'] .= '
Fichiers disponibles Télécharger
{{label}}'; if ($myUser->id == $sketch->owner) { $response['content'] .= '
'; } $response['content'] .= '
'; break; case 'part': $response['callback'] = 'init_part();'; $response['content'] = ''; if ($myUser->id == $sketch->owner) { $response['content'] .= ''; } $response['content'] .= ''; if ($myUser->id == $sketch->owner) { $response['content'] .= ''; } $response['content'] .= ''; $response['content'] .= ''; if ($myUser->id == $sketch->owner) { $response['content'] .= ''; } $response['content'] .= ''; $response['content'] .= '
Libellé
{{label}}
{{#price}}{{price}} €{{/price}}{{#brand}} {{brand}}{{/brand}}
'; break; } //for sources if (isset($type['codemirror'])) { $response['content'] = ''; $response['code'] = $type['codemirror']; if ($myUser->id != $sketch->owner) { $response['code']['readOnly'] = true; } } return $response; } public static function toFileStream($resource) { $type = self::get($resource->type); $file = (object) array('name' => slugify($resource->label), 'content' => ''); if (isset($type['extension'])) { $file->name .= '.'.$type['extension']; } switch ($resource->type) { case 'part': $file->content = '> '.strtoupper($resource->label).PHP_EOL; $file->content .= str_repeat('=', strlen($resource->label) + 2).PHP_EOL; foreach (ResourcePart::loadAll(array('resource' => $resource->id)) as $resourcePart): $part = $resourcePart->part_object; $file->content .= $part->label."\t"; if (isset($part->link) && !empty($part->link)) { $file->content .= $part->link."\t"; } if (isset($part->price) && !empty($part->price)) { $file->content .= $part->price.'€'; } $file->content .= PHP_EOL; endforeach; break; case 'files': global $myUser; $path = SKETCH_PATH.'/'.$resource->id; $filename = $resource->label.'-'.time().'.zip'; $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename; $zip = new ZipArchive(); if (file_exists($filepath)) { unlink($filepath); } $res = $zip->open($filepath, ZipArchive::CREATE); if ($res === true) { foreach (glob($path.'/*') as $f) { $zip->addFile($f, basename($f)); } $zip->close(); } $file->name = slugify($resource->label).'.zip'; $file->content = file_get_contents($filepath); break; case 'image': $ext = getExt($resource->content); $file->name = slugify($resource->label).'.'.$ext; if (file_exists(SKETCH_PATH.$resource->content)) { $file->content = file_get_contents(SKETCH_PATH.$resource->content); } break; default: $file->content = html_entity_decode($resource->content); break; } return $file; } }