Sketch.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Define a sketch.
  4. * @author Idleman
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class Sketch extends Entity{
  9. public $id,$label,$state,$comment,$slug,$progress;
  10. protected $TABLE_NAME = 'hackpoint_sketch';
  11. public $fields =
  12. array(
  13. 'id' => 'key',
  14. 'label' => 'string',
  15. 'state' => 'string',
  16. 'comment' => 'longstring',
  17. 'progress' => 'int',
  18. 'slug' => 'string'
  19. );
  20. public $links = array(
  21. );
  22. public function picture($stream = false){
  23. $folder = $this->directory();
  24. if(!file_exists($folder)) mkdir($folder,0755,true);
  25. $picture = $folder.SLASH.'cover.jpg';
  26. if(!file_exists($picture)){
  27. copy(__DIR__.SLASH.'img'.SLASH.'default-sketch.jpg',$picture);
  28. }
  29. if($stream) return file_get_contents($picture);
  30. return 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$this->id.SLASH.'cover.jpg');
  31. }
  32. public function download(){
  33. require_once(__DIR__.SLASH.'ResourceType.class.php');
  34. $stream = "Fonctionnalité en cours d'implémentation, désolé :p";
  35. $folder = $this->directory();//todo
  36. $filename = $this->slug.'-'.time().'.zip';
  37. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
  38. $zip = new ZipArchive;
  39. $res = $zip->open($filepath, ZipArchive::CREATE);
  40. if ($res === TRUE) {
  41. $mainReadme = $this->toFile();
  42. //ajout du md principal
  43. $zip->addFromString($mainReadme['name'], $mainReadme['content']);
  44. //ajout de l'image de couverture du sketch
  45. $zip->addFromString('cover.jpg', $this->picture(true));
  46. foreach($this->resources() as $resource){
  47. $type = ResourceType::types($resource->type);
  48. $file = $type['class']::toFile($resource);
  49. $zip->addFromString($file['name'], $file['content']);
  50. }
  51. $zip->close();
  52. }
  53. $stream = file_get_contents($filepath);
  54. unlink($filepath);
  55. return $stream;
  56. }
  57. //Export vers un fichier brut
  58. public function toFile(){
  59. $content = '# '.$this->label.PHP_EOL.PHP_EOL;
  60. $content .= '``'.$this->comment.'``'.PHP_EOL.PHP_EOL;
  61. $content .= '* Visibilité : '.($this->state?'Public':'Privé').PHP_EOL;
  62. $content .= '* Créé par '.$this->creator.' le '.strtolower(relative_time($this->created)).PHP_EOL;
  63. $content .= '* Modifié par '.$this->updater.' le '.strtolower(relative_time($this->updated)).PHP_EOL;
  64. return array(
  65. 'name'=> 'README-SKETCH.md',
  66. 'content' => html_entity_decode($content)
  67. );
  68. }
  69. public function resources(){
  70. require_once(__DIR__.SLASH.'Resource.class.php');
  71. return Resource::loadAll(array('sketch'=>$this->id));
  72. }
  73. public function directory(){
  74. return File::dir().'hackpoint'.SLASH.'sketch'.SLASH.$this->id;
  75. }
  76. public function save()
  77. {
  78. if ($this->id == 0) {
  79. $this->slug = slugify($this->label);
  80. }
  81. parent::save();
  82. }
  83. public static function removeById($id){
  84. require_once(__DIR__.SLASH.'Resource.class.php');
  85. $sketch = self::getById($id);
  86. self::deleteById($sketch->id);
  87. Resource::delete(array('sketch'=>$sketch->id));
  88. if(file_exists($sketch->directory())) delete_folder_tree($sketch->directory(),true);
  89. }
  90. }
  91. ?>