/**
  * Move uploaded files to the right place and insert metadata in the database
  *
  * @access	private
  */
 private function create()
 {
     if (VPost::upload(false)) {
         try {
             $path = 'content/' . date('Y/m/');
             $img = new HandleMedia();
             $img->load_upload('file');
             $name = Helper::remove_accent($img->_name);
             $mime = $img->_mime;
             if (file_exists(PATH . $path . $name)) {
                 throw new Exception('The file "' . $name . '" already exists');
             }
             $img->save(PATH . $path . $name);
             if (substr($mime, 0, 5) == 'image') {
                 $img->thumb(150, 0);
                 $img->thumb(300, 0);
                 $img->thumb(1000, 0);
                 $this->_media->_status = 'draft';
             } elseif (substr($mime, 0, 5) == 'video') {
                 $this->_media->_status = 'publish';
             }
             $this->_media->_name = $name;
             $this->_media->_type = $mime;
             $this->_media->_author = $this->_user['user_id'];
             $this->_media->_allow_comment = 'closed';
             $this->_media->_permalink = $path . $name;
             $this->_media->_album = 0;
             $this->_media->create();
             Session::monitor_activity('has uploaded a file named: ' . $this->_media->_name);
             if (substr($mime, 0, 5) == 'video') {
                 header('Location: index.php?ns=media&ctl=manage&type=video');
             } else {
                 header('Location: index.php?ns=media&ctl=manage');
             }
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     } elseif (VPost::create_album(false) && $this->_user['album_photo']) {
         if (!VPost::name()) {
             $this->_action_msg = ActionMessages::custom_wrong('Album name missing');
         } else {
             try {
                 $name = VPost::name();
                 $path = 'content/albums/' . Helper::slug($name) . '/';
                 if (file_exists(PATH . $path)) {
                     throw new Exception('The album "' . $name . '" already exists');
                 }
                 $this->_media->_name = $name;
                 $this->_media->_type = 'album';
                 $this->_media->_author = $this->_user['user_id'];
                 $this->_media->_status = 'draft';
                 $this->_media->_permalink = $path;
                 $this->_media->_description = stripslashes(VPost::description());
                 $this->_media->_category = implode(',', VPost::cat(array()));
                 $this->_media->_allow_comment = VPost::allow_comment('closed');
                 $this->_media->_album = 0;
                 $img = new HandleMedia();
                 $img->load_upload('cover');
                 $img->save(PATH . $path . 'cover.png');
                 $img->thumb(150, 0);
                 $img->thumb(300, 0);
                 $img->thumb(1000, 0);
                 $this->_media->create();
                 Session::monitor_activity('created an album named: ' . $this->_media->_name);
                 header('Location: index.php?ns=media&ctl=albums&action=edit&id=' . $this->_media->_id);
             } catch (Exception $e) {
                 $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
             }
         }
     } elseif (VPost::link_alien(false)) {
         if (!VPost::name() || !VPost::embed_code()) {
             $this->_action_msg = ActionMessages::custom_wrong('There\'s missing informations');
         } else {
             try {
                 $this->_media->_name = VPost::name();
                 $this->_media->_type = 'alien';
                 $this->_media->_author = $this->_user['user_id'];
                 $this->_media->_status = 'draft';
                 $this->_media->_allow_comment = 'closed';
                 $this->_media->_permalink = Helper::slug(VPost::name());
                 $this->_media->_embed_code = VPost::embed_code();
                 $this->_media->_album = 0;
                 $this->_media->create();
                 Session::monitor_activity('linked a new video named: ' . $this->_media->_name);
                 header('Location: index.php?ns=media&ctl=manage&type=video');
             } catch (Exception $e) {
                 $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
             }
         }
     } elseif (VPost::register_video(false)) {
         try {
             if (!file_exists(PATH . VPost::url())) {
                 throw new Exception('Video not found');
             }
             if (!VPost::mime()) {
                 throw new Exception('Video mime type missing');
             }
             $this->_media->_name = VPost::name();
             $this->_media->_type = VPost::mime();
             $this->_media->_author = $this->_user['user_id'];
             $this->_media->_status = 'publish';
             $this->_media->_allow_comment = 'closed';
             $this->_media->_permalink = VPost::url();
             $this->_media->_album = 0;
             $this->_media->create();
             Session::monitor_activity('registered a new video named: ' . $this->_media->_name);
             header('Location: index.php?ns=media&ctl=manage&action=edit&type=video&id=' . $this->_media->_id);
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     }
 }
 /**
  * Add a new Plugin
  *
  * @access	private
  */
 private function create()
 {
     if (VPost::upload(false)) {
         try {
             $plg = VFiles::plg();
             if ($plg['error'] != 0) {
                 throw new Exception('No file uploaded');
             }
             $tmp = 'tmp/plg_' . md5_file($plg['tmp_name']) . '/';
             File::unzip($plg['tmp_name'], $tmp);
             $json = File::read($tmp . 'manifest.json');
             $conf = json_decode($json->_content, true);
             //check if manifest is complete
             if (!isset($conf['name']) || !isset($conf['namespace']) || !isset($conf['entry_point']) || !isset($conf['author']) || !isset($conf['url']) || !isset($conf['admin']) || !isset($conf['site']) || !isset($conf['library']) || !isset($conf['queries']) || !isset($conf['uninstall'])) {
                 throw new Exception('Invalid manifest!');
             }
             if (is_dir('includes/' . $conf['namespace']) || is_dir('library/' . $conf['namespace'])) {
                 throw new Exception('The namespace "' . $conf['namespace'] . '" is already taken');
             }
             //if one of files doesn't exists, an exception will be raised
             foreach ($conf['admin'] as $file) {
                 File::read($tmp . 'admin/' . $file);
             }
             //if one of files doesn't exists, an exception will be raised
             foreach ($conf['site'] as $file) {
                 if (file_exists(PATH . 'includes/' . $file)) {
                     throw new Exception('The file "' . $file . '" already exists in site directory');
                 }
                 File::read($tmp . 'site/' . $file);
             }
             //if one of files doesn't exists, an exception will be raised
             foreach ($conf['library'] as $file) {
                 File::read($tmp . 'library/' . $file);
             }
             foreach ($conf['admin'] as $file) {
                 File::move($tmp . 'admin/' . $file, 'includes/' . $conf['namespace'] . '/' . $file);
                 File::delete($tmp . 'admin/' . $file);
             }
             foreach ($conf['site'] as $file) {
                 File::move($tmp . 'site/' . $file, PATH . 'includes/' . $file);
                 File::delete($tmp . 'site/' . $file);
             }
             foreach ($conf['library'] as $file) {
                 File::move($tmp . 'library/' . $file, 'library/' . $conf['namespace'] . '/' . $file);
                 File::delete($tmp . 'library/' . $file);
             }
             if (isset($conf['css'])) {
                 foreach ($conf['css'] as $file) {
                     File::move($tmp . 'css/' . $file, PATH . 'css/' . $conf['namespace'] . '.css');
                     File::delete($tmp . 'css/' . $file);
                 }
             }
             foreach ($conf['queries'] as $query) {
                 $this->_db->query(str_replace('{{prefix}}', DB_PREFIX, $query));
             }
             File::delete($tmp . 'manifest.json');
             $setting = new Setting();
             $setting->_name = $conf['name'];
             $setting->_type = 'plugin';
             $setting->_data = json_encode($conf);
             $setting->create();
             header('Location: index.php?ns=plugins&ctl=manage');
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     }
 }
 /**
  * Move uploaded files in the associated album directory and insert metadata in the database
  *
  * @access	private
  */
 private function create()
 {
     if (VPost::upload(false) && !empty($_FILES)) {
         try {
             $album = new Media();
             $album->_id = VPost::album_id();
             $album->read('_name');
             $album->read('_permalink');
             $path = $album->_permalink;
             foreach (VFiles::all() as $key => $img) {
                 if (empty($img['name'])) {
                     continue;
                 }
                 $pic = new HandleMedia();
                 $pic->load_upload($key);
                 $name = Helper::remove_accent($pic->_name);
                 $mime = $pic->_mime;
                 if (substr($mime, 0, 5) == 'image') {
                     if (file_exists(PATH . $path . $name)) {
                         throw new Exception('The file "' . $name . '" already exists');
                     }
                     $pic->save(PATH . $path . $name);
                     $pic->thumb(150, 0);
                     $pic->thumb(300, 0);
                     $pic->thumb(1000, 0);
                     $picture = new Media();
                     $picture->_name = $name;
                     $picture->_type = $mime;
                     $picture->_author = $this->_user['user_id'];
                     $picture->_album = $album->_id;
                     $picture->_allow_comment = 'closed';
                     $picture->_permalink = $path . $name;
                     $picture->_status = 'publish';
                     $picture->create();
                 }
             }
             Session::monitor_activity('added new photos to ' . $album->_name);
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::created($result);
     } elseif (VPost::upload_zip() && !empty($_FILES)) {
         try {
             $album = new Media();
             $album->_id = VPost::album_id();
             $album->read('_name');
             $album->read('_permalink');
             $path = $album->_permalink;
             $tmp = 'tmp/albums/';
             if (empty($_FILES['zip']['tmp_name'])) {
                 throw new Exception('No archive uploaded!');
             }
             File::unzip($_FILES['zip']['tmp_name'], $tmp);
             $files = @scandir($tmp);
             if (empty($files)) {
                 throw new Exception('Your archive is empty!');
             }
             foreach ($files as $file) {
                 $finfo = new finfo(FILEINFO_MIME_TYPE);
                 $mime = $finfo->file($tmp . $file);
                 if ($mime == 'directory') {
                     continue;
                 }
                 $pic = new HandleMedia();
                 $pic->_mime = $mime;
                 $pic->load($tmp . $file);
                 $name = Helper::remove_accent($pic->_name);
                 if (substr($mime, 0, 5) == 'image') {
                     if (file_exists(PATH . $path . $name)) {
                         throw new Exception('The file "' . $name . '" already exists');
                     }
                     File::read($tmp . $file)->save(PATH . $path . $name);
                     $pic->_file = PATH . $path . $name;
                     $pic->thumb(150, 0);
                     $pic->thumb(300, 0);
                     $pic->thumb(1000, 0);
                     $picture = new Media();
                     $picture->_name = $name;
                     $picture->_type = $mime;
                     $picture->_author = $this->_user['user_id'];
                     $picture->_album = $album->_id;
                     $picture->_allow_comment = 'closed';
                     $picture->_permalink = $path . $name;
                     $picture->_status = 'publish';
                     $picture->create();
                     File::delete($tmp . $file);
                 }
             }
             Session::monitor_activity('added new photos to ' . $album->_name);
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::created($result);
     }
 }
 /**
  * Upload and move into place a new template
  *
  * @access	private
  */
 private function create()
 {
     if (VPost::upload(false)) {
         try {
             $tpl = VFiles::tpl();
             if ($tpl['error'] != 0) {
                 throw new Exception('No file uploaded');
             }
             $tmp = 'tmp/tpl_' . md5_file($tpl['tmp_name']) . '/';
             File::unzip($tpl['tmp_name'], $tmp);
             $json = File::read($tmp . 'manifest.json');
             $conf = json_decode($json->_content, true);
             //check if the manifest is complete
             if (!isset($conf['name']) || !isset($conf['author']) || !isset($conf['url']) || !isset($conf['namespace']) || !isset($conf['files'])) {
                 throw new Exception('Invalid manifest!');
             }
             if (is_dir(PATH . 'includes/templates/' . $conf['namespace'] . '/')) {
                 throw new Exception('Template already exist');
             }
             //if one of files doesn't exists, an exception will be raised
             foreach ($conf['files'] as $file) {
                 File::read($tmp . $file);
             }
             foreach ($conf['files'] as $file) {
                 File::move($tmp . $file, PATH . 'includes/templates/' . $conf['namespace'] . '/' . $file);
                 File::delete($tmp . $file);
             }
             File::delete($tmp . 'manifest.json');
             $setting = new Setting();
             $setting->_name = $conf['name'];
             $setting->_type = 'template';
             $setting->_data = json_encode($conf);
             $setting->create();
             header('Location: index.php?ns=templates&ctl=manage');
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     }
 }