/**
  * Retrieve users
  *
  * @access	private
  */
 private function get_content()
 {
     try {
         $to_read['table'] = $this->_sql_table;
         $to_read['columns'] = array('USER_ID');
         if (VGet::author()) {
             $to_read['condition_columns'][':p'] = 'user_publicname';
             $to_read['condition_select_types'][':p'] = '=';
             $to_read['condition_values'][':p'] = VGet::author();
             $to_read['value_types'][':p'] = 'str';
         }
         $this->_content = $this->_db->read($to_read);
         if (!empty($this->_content)) {
             foreach ($this->_content as &$user) {
                 $user = new User($user['USER_ID']);
                 $a = $user->_avatar;
                 if (!empty($a)) {
                     $m = new Media();
                     $m->_id = $user->_avatar;
                     $m->read('_permalink');
                     $dirname = dirname($m->_permalink) . '/';
                     $filename = basename($m->_permalink);
                     $user->_avatar = $dirname . '150-' . $filename;
                 }
             }
         }
     } catch (Exception $e) {
         @error_log($e->getMessage() . ' file: ' . __FILE__ . '; line: ' . __LINE__, 1, WS_EMAIL);
         header('Location: 404.php');
     }
 }
 /**
  * Retrieve some recent comments pending to be approved
  *
  * @access	private
  */
 private function get_recent_comments()
 {
     if ($this->_user['comments']) {
         try {
             $to_read['table'] = 'comment';
             $to_read['columns'] = array('COMMENT_ID');
             $to_read['condition_columns'][':status'] = 'comment_status';
             $to_read['condition_select_types'][':status'] = '=';
             $to_read['condition_values'][':status'] = 'pending';
             $to_read['value_types'][':status'] = 'str';
             $to_read['order'] = array('comment_date', 'DESC');
             $to_read['limit'] = array(0, 3);
             $this->_comments = $this->_db->read($to_read);
             if (!empty($this->_comments)) {
                 foreach ($this->_comments as &$comment) {
                     $comment = new Comment($comment['COMMENT_ID']);
                     if ($comment->_rel_type == 'post') {
                         $post = new Post();
                         $post->_id = $comment->_rel_id;
                         $post->read('_title');
                         $post->read('_permalink');
                         $comment->_rel_title = $post->_title;
                         $comment->_rel_permalink = 'ctl=posts&news=' . $post->_permalink;
                     } elseif ($comment->_rel_type == 'media') {
                         $media = new Media();
                         $media->_id = $comment->_rel_id;
                         $media->read('_name');
                         $comment->_rel_title = $media->_name;
                         $comment->_rel_permalink = 'ctl=albums&album=' . $media->_id;
                     }
                 }
             }
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     }
 }
 /**
  * Transform user avatar in permalink
  *
  * @access	private
  */
 private function build_avatar()
 {
     $a = $this->_profile->_avatar;
     if (!empty($a)) {
         try {
             $m = new Media($this->_profile->_avatar);
             $m->read('_permalink');
             $dirname = dirname($m->_permalink) . '/';
             $filename = basename($m->_permalink);
             $this->_profile->_avatar = $dirname . '150-' . $filename;
         } catch (Exception $e) {
             $this->_profile->_avatar = 0;
         }
     }
 }
 /**
  * Retrieve comments from database in function of the status, the type or via a search
  *
  * @access	private
  */
 private function get_comments()
 {
     try {
         $to_read['table'] = 'comment';
         $to_read['columns'] = array('COMMENT_ID');
         if (VGet::action() == 'by_type' && VGet::id() && VGet::type() && VGet::comment_status()) {
             $to_read['condition_columns'][':id'] = 'comment_rel_ID';
             $to_read['condition_select_types'][':id'] = '=';
             $to_read['condition_values'][':id'] = VGet::id();
             $to_read['value_types'][':id'] = 'int';
             $to_read['condition_types'][':status'] = 'AND';
             $to_read['condition_columns'][':status'] = 'comment_status';
             $to_read['condition_select_types'][':status'] = '=';
             $to_read['condition_values'][':status'] = $this->_status;
             $to_read['value_types'][':status'] = 'str';
         } elseif (VPost::search_button(false) || VGet::search()) {
             $to_read['condition_columns']['group'][':content'] = 'comment_content';
             $to_read['condition_select_types'][':content'] = 'LIKE';
             $to_read['condition_values'][':content'] = '%' . $this->_search . '%';
             $to_read['value_types'][':content'] = 'str';
             $to_read['condition_types'][':name'] = 'OR';
             $to_read['condition_columns']['group'][':name'] = 'comment_name';
             $to_read['condition_select_types'][':name'] = 'LIKE';
             $to_read['condition_values'][':name'] = '%' . $this->_search . '%';
             $to_read['value_types'][':name'] = 'str';
             $to_read['condition_types'][':email'] = 'OR';
             $to_read['condition_columns']['group'][':email'] = 'comment_email';
             $to_read['condition_select_types'][':email'] = 'LIKE';
             $to_read['condition_values'][':email'] = '%' . $this->_search . '%';
             $to_read['value_types'][':email'] = 'str';
             $to_read['condition_types'][':status'] = 'AND';
             $to_read['condition_columns'][':status'] = 'comment_status';
             $to_read['condition_select_types'][':status'] = '=';
             $to_read['condition_values'][':status'] = $this->_status;
             $to_read['value_types'][':status'] = 'str';
         } elseif (VGet::action() == 'edit' && VGet::comment_id()) {
             $to_read['condition_columns'][':id'] = 'COMMENT_ID';
             $to_read['condition_select_types'][':id'] = '=';
             $to_read['condition_values'][':id'] = VGet::comment_id();
             $to_read['value_types'][':id'] = 'int';
         } else {
             $to_read['condition_columns'][':status'] = 'comment_status';
             $to_read['condition_select_types'][':status'] = '=';
             $to_read['condition_values'][':status'] = $this->_status;
             $to_read['value_types'][':status'] = 'str';
         }
         //pass $to_read by parameter to have same conditions
         $this->get_pagination($to_read);
         $to_read['order'] = array('comment_date', 'desc');
         $to_read['limit'] = array($this->_limit_start, parent::ITEMS);
         $this->_content = $this->_db->read($to_read);
         if (!empty($this->_content)) {
             foreach ($this->_content as &$comment) {
                 $comment = new Comment($comment['COMMENT_ID']);
                 if ($comment->_rel_type == 'post') {
                     $post = new Post();
                     $post->_id = $comment->_rel_id;
                     $post->read('_title');
                     $post->read('_permalink');
                     $comment->_rel_title = $post->_title;
                     $comment->_rel_permalink = $post->_permalink;
                 } elseif ($comment->_rel_type == 'media') {
                     $media = new Media();
                     $media->_id = $comment->_rel_id;
                     $media->read('_name');
                     $comment->_rel_title = $media->_name;
                     $comment->_rel_permalink = $media->_id;
                 }
             }
         } elseif (empty($this->_content) && VGet::action() == 'edit') {
             $this->_content[0] = new Comment();
             throw new Exception('Invalid comment!');
         }
     } 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();
     }
 }
 /**
  * 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();
     }
 }
 /**
  * Retrieve video metadatas from database
  *
  * @access	private
  */
 private function get_content()
 {
     try {
         $to_read['table'] = $this->_sql_table;
         $to_read['columns'] = array('MEDIA_ID');
         $to_read['condition_columns'][':t'] = 'media_type';
         $to_read['condition_select_types'][':t'] = 'LIKE';
         $to_read['condition_values'][':t'] = 'video%';
         $to_read['value_types'][':t'] = 'str';
         $to_read['condition_types'][':s'] = 'AND';
         $to_read['condition_columns'][':s'] = 'media_status';
         $to_read['condition_select_types'][':s'] = '=';
         $to_read['condition_values'][':s'] = 'publish';
         $to_read['value_types'][':s'] = 'str';
         if (VGet::cat(false)) {
             $to_read['condition_types'][':cat'] = 'AND';
             $to_read['condition_columns'][':cat'] = 'media_category';
             $to_read['condition_select_types'][':cat'] = 'LIKE';
             $to_read['condition_values'][':cat'] = '%' . VGet::cat() . '%';
             $to_read['value_types'][':cat'] = 'str';
         }
         $this->_content = $this->_db->read($to_read);
         if (!empty($this->_content)) {
             foreach ($this->_content as &$media) {
                 $media = new Media($media['MEDIA_ID']);
                 //retrieve user public name
                 $user = new User();
                 $user->_id = $media->_author;
                 $user->read('_publicname');
                 $media->_author_publicname = $user->_publicname;
                 $attached = $media->_attachment;
                 if (!empty($attached)) {
                     $attach = new Media();
                     $attach->_id = $media->_attachment;
                     $attach->read('_embed_code');
                     $media->_embed_code = $attach->_embed_code;
                 }
             }
         }
     } catch (Exception $e) {
         @error_log($e->getMessage() . ' file: ' . __FILE__ . '; line: ' . __LINE__, 1, WS_EMAIL);
         header('Location: 404.php');
     }
 }