/**
  * 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());
         }
     }
 }
 /**
  * Delete medias from database and on hard drive
  *
  * @access	private
  */
 private function delete()
 {
     if ($this->_user['delete_content'] && VPost::delete(false) && VPost::media_id()) {
         $results = array();
         $global_result = true;
         foreach (VPost::media_id() as $id) {
             try {
                 $media = new Media();
                 $media->_id = $id;
                 $media->read('_permalink');
                 $path = $media->_permalink;
                 $media->delete();
                 unset($media);
                 HandleMedia::delete(PATH . $path);
                 $this->_db->query('DELETE FROM `' . DB_PREFIX . 'comment` WHERE comment_rel_id = ' . $id . ' AND comment_rel_type = "media"');
                 if (VPost::type() == 'alien') {
                     $to_update['table'] = 'media';
                     $to_update['columns'] = array(':attach' => 'media_attachment');
                     $to_update['condition_columns'] = array(':ca' => 'media_attachment');
                     $to_update['column_values'] = array(':attach' => null, ':ca' => $id);
                     $to_update['value_types'] = array(':attach' => 'null', ':ca' => 'int');
                     $this->_db->update($to_update);
                 }
                 array_push($results, true);
             } catch (Exception $e) {
                 array_push($results, false);
             }
         }
         foreach ($results as $result) {
             if ($result !== true) {
                 $global_result = false;
             }
         }
         Session::monitor_activity('deleted ' . count(VPost::media_id()) . ' file(s)');
         $this->_action_msg = ActionMessages::deleted($global_result);
     } elseif ($this->_user['delete_content'] && VGet::action() == 'delete' && VGet::id()) {
         try {
             $media = new Media();
             $media->_id = VGet::id();
             $media->read('_permalink');
             $path = $media->_permalink;
             $media->delete();
             unset($media);
             HandleMedia::delete(PATH . $path);
             $this->_db->query('DELETE FROM `' . DB_PREFIX . 'comment` WHERE comment_rel_id = ' . VGet::id() . ' AND comment_rel_type = "media"');
             if (VGet::type() == 'alien') {
                 $to_update['table'] = 'media';
                 $to_update['columns'] = array(':attach' => 'media_attachment');
                 $to_update['condition_columns'] = array(':ca' => 'media_attachment');
                 $to_update['column_values'] = array(':attach' => null, ':ca' => VGet::id());
                 $to_update['value_types'] = array(':attach' => 'null', ':ca' => 'int');
                 $this->_db->update($to_update);
             }
             Session::monitor_activity('deleted a file');
             $result = true;
         } catch (Exception $e) {
             error_log($e->getMessage(), 0);
             $result = false;
         }
         $this->_action_msg = ActionMessages::deleted($result);
     } elseif (!$this->_user['delete_content'] && (VPost::delete(false) || VGet::action() == 'delete')) {
         $this->_action_msg = ActionMessages::action_no_perm();
     }
 }
 /**
  * Display form to add pictures to an album
  *
  * @static
  * @access	public
  * @param	string [$name] Album name
  * @param	integer [$id] Album id
  */
 public static function ma_upload($name, $id)
 {
     echo '<div id="edit_media">' . '<h3>Upload new pictures for "' . $name . '"</h3>' . '<input type="file" name="picture1" /><br/>' . '<input type="file" name="picture2" /><br/>' . '<input type="file" name="picture3" /><br/>' . '<input type="file" name="picture4" /><br/>' . '<input type="file" name="picture5" /><br/>' . '<input type="file" name="picture6" /><br/>' . '<input type="file" name="picture7" /><br/>' . '<input type="file" name="picture8" /><br/>' . '<input type="file" name="picture9" /><br/>' . '<input type="file" name="picture10" /><br/>' . '<br/>' . '<span class="indication">' . 'Please don\'t quit this page while uploading</span><br/>' . '<span class="indication">The maximum upload file size is set to ' . HandleMedia::max_upload() . 'MB</span><br/>' . '<input type="hidden" name="album_id" value="' . $id . '" />' . '<input class="submit button button_publish" type="submit" name="upload" value="Add Pictures" />' . '<h3>Upload a zip</h3>' . '<input type="file" name="zip" /><br/>' . '<br/>' . '<span class="indication">Your pictures has to be at the root of the zip (not in a sub-folder)</span><br/>' . '<input class="submit button button_publish" type="submit" name="upload_zip" value="Upload Zip" />' . '</div>';
 }
 /**
  * Delete files on hard drive and metadata in database
  *
  * @access	private
  */
 private function delete()
 {
     if (VPost::apply_action(false) && VPost::action() == 'delete' && $this->_user['delete_content']) {
         if (VPost::album_id()) {
             try {
                 foreach (VPost::album_id() as $id) {
                     $album = new Media();
                     $album->_id = $id;
                     $album->read('_permalink');
                     $to_read['table'] = 'media';
                     $to_read['columns'] = array('MEDIA_ID');
                     $to_read['condition_columns'][':id'] = 'media_album';
                     $to_read['condition_select_types'][':id'] = '=';
                     $to_read['condition_values'][':id'] = $id;
                     $to_read['value_types'][':id'] = 'int';
                     $ids = $this->_db->read($to_read);
                     if (!empty($ids)) {
                         foreach ($ids as $pid) {
                             $pic = new Media();
                             $pic->_id = $pid['MEDIA_ID'];
                             $pic->read('_permalink');
                             $permalink = $pic->_permalink;
                             HandleMedia::delete(PATH . $permalink);
                             $pic->delete();
                         }
                     }
                     $permalink = $album->_permalink;
                     HandleMedia::delete(PATH . $permalink . 'cover.png');
                     @rmdir(PATH . $permalink);
                     $album->delete();
                     $this->_db->query('DELETE FROM `' . DB_PREFIX . 'comment` WHERE comment_rel_id = ' . $id . ' AND comment_rel_type = "media"');
                 }
                 Session::monitor_activity('deleted ' . count(VPost::album_id()) . ' album(s)');
                 $result = true;
             } catch (Exception $e) {
                 $result = $e->getMessage();
             }
             $this->_action_msg = ActionMessages::deleted($result);
         }
     } elseif (VGet::action() == 'delete' && VGet::id() && $this->_user['delete_content']) {
         try {
             $pic = new Media();
             $pic->_id = VGet::id();
             $pic->read('_permalink');
             $permalink = $pic->_permalink;
             HandleMedia::delete(PATH . $permalink);
             $pic->delete();
             $this->_db->query('DELETE FROM `' . DB_PREFIX . 'comment` WHERE comment_rel_id = ' . VGet::id() . ' AND comment_rel_type = "media"');
             Session::monitor_activity('deleted a picture of an album');
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::deleted($result);
     } elseif (VPost::delete_pics(false)) {
         if (VPost::picture_id()) {
             try {
                 foreach (VPost::picture_id() as $id) {
                     $pic = new Media();
                     $pic->_id = $id;
                     $pic->read('_permalink');
                     $permalink = $pic->_permalink;
                     HandleMedia::delete(PATH . $permalink);
                     $pic->delete();
                 }
                 Session::monitor_activity('deleted ' . count(VPost::picture_id(array())) . ' picture(s) of an album');
                 $result = true;
             } catch (Exception $e) {
                 $result = $e->getMessage();
             }
         }
     } elseif ((VPost::apply_action(false) && VPost::action() == 'delete' || VGet::action() == 'delete' || VPost::delete_pics(false)) && !$this->_user['delete_content']) {
         $this->_action_msg = ActionMessages::action_no_perm();
     }
 }
 /**
  * Display form to add a template
  *
  * @static
  * @access	public
  */
 public static function nt_form()
 {
     echo '<h3>Add a template to your website</h3>' . '<div id="new_tpl">' . '<lable for="tpl">Upload a template archive:</label>&nbsp;&nbsp;&nbsp;&nbsp;<input id="tpl" type="file" name="tpl" required />' . '<input id="upload" class="button button_publish" type="submit" name="upload" value="Upload" /><br/>' . '<span class="indication">(The maximum upload file size is set to ' . Media::max_upload() . 'MB)</span>' . '</div>';
 }