/**
  * Class constructor
  *
  * @access	public
  */
 public function __construct()
 {
     parent::__construct();
     $this->_since = VGet::since(date('Y-m-d'));
     $this->_url = 'cache/timeline/' . $this->_since . '.json';
     if ($this->check_cache() === false) {
         $this->_content = array('title' => WS_NAME, 'url' => WS_URL, 'content' => array());
         $this->get_content();
         $cache = new File();
         $cache->_content = json_encode($this->_content);
         $cache->save($this->_url);
     } else {
         $cache = File::read($this->_url);
         $this->_content = json_decode($cache->_content, true);
     }
 }
 /**
  * Class constructor
  *
  * @access	public
  */
 public function __construct()
 {
     parent::__construct();
     try {
         $this->_slug = VGet::slug();
         if (empty($this->_slug)) {
             throw new Exception('No permalink found!');
         }
         $this->_url = 'cache/post/' . $this->_slug . '.json';
         if ($this->check_cache() === false) {
             $this->get_content();
             $cache = new File();
             $cache->_content = json_encode($this->_content);
             $cache->save($this->_url);
         } else {
             $cache = File::read($this->_url);
             $this->_content = json_decode($cache->_content, true);
         }
     } catch (Exception $e) {
         $this->_content = array('message' => $e->getMessage());
     }
 }
 /**
  * Install a template from github
  *
  * @access	private
  */
 private function create()
 {
     if (VGet::action() == 'install' && VGet::user() && VGet::repo() && VGet::download()) {
         try {
             $curl = new Curl('https://api.github.com/repos/' . VGet::user() . '/' . VGet::repo() . '/downloads');
             $downloads = json_decode($curl->_content, true);
             if (empty($downloads)) {
                 throw new Exception('Archive doesn\'t exist on Github');
             }
             if (isset($downloads['message'])) {
                 throw new Exception($downloads['message']);
             }
             $url = null;
             foreach ($downloads as $download) {
                 if ($download['name'] == VGet::download()) {
                     if ($download['content_type'] != 'application/zip') {
                         throw new Exception('Invalid archive type! (.zip only)');
                     } else {
                         $url = $download['html_url'];
                     }
                 }
             }
             unset($curl);
             $curl = new Curl($url);
             $zip = new File();
             $zip->_content = $curl->_content;
             $zip->save('tmp/template.zip');
             $tmp = 'tmp/tpl_' . md5_file('tmp/template.zip') . '/';
             File::unzip('tmp/template.zip', $tmp);
             File::delete('tmp/template.zip');
             $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();
             $this->_action_msg = ActionMessages::custom_good('Template "' . $setting->_name . '" installed');
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
         }
     }
 }
 /**
  * Build cache
  *
  * Exceptions of the exist method continue here
  * So cache generation is stopped if its not a landing page or
  * when we are on an album comments page and also when a form
  * has been submitted
  *
  * @access	public
  * @param	string [$action] Can be "s" or "e" (meaning start or end)
  */
 public function build($action)
 {
     if (self::ACTIVATED === false) {
         return false;
     }
     if ((!empty($this->_post) || count($this->_get) > 1) && !in_array(VGet::ctl(), array('search', 'albums'))) {
         return true;
     } elseif (VGet::ctl() == 'albums' && VGet::comments()) {
         return true;
     }
     if ($action == 's') {
         ob_start();
     } elseif ($action == 'e') {
         $content = ob_get_contents();
         ob_end_flush();
         $cache = new File();
         $cache->_content = $content;
         $cache->save($this->_url);
     } else {
         throw new Exception('Unknown cache command');
     }
 }
 /**
  * Update lynxpress
  *
  * @access	private
  */
 private function update()
 {
     if (VPost::update()) {
         try {
             if (Helper::check_update() === false) {
                 throw new Exception('No update available!');
             }
             //make a backup of the database first, with an email sent to webmaster with the whole dump
             $bk = new Backup();
             $bk->save('backup/dump-' . date('Y-m-d-H:i:s') . '.sql');
             $html = new File();
             $html->_content = '<!--The Lynx is not here!-->';
             $html->save('backup/index.html');
             $mail = new Mail(WS_EMAIL, 'Databse dump made before update at ' . date('Y-m-d H:i:s'), $bk->_sql);
             $mail->send();
             //end backup
             //retrieve json manifest from the server
             $manifest = new Curl('http://update.lynxpress.org/manifest.json');
             $manifest = json_decode($manifest->_content, true);
             //retrieve zip with all files inside
             $curl_zip = new Curl('http://versions.lynxpress.org/Lynxpress-' . $manifest['version'] . '.zip');
             if ($curl_zip->_content == '<!--The Lynx is not here!-->') {
                 throw new Exception('Can\'t retrieve lynxpress archive');
             }
             $zip = new File();
             $zip->_content = $curl_zip->_content;
             $zip->save('tmp/update.zip');
             unset($zip);
             unset($curl_zip);
             File::unzip('tmp/update.zip', 'tmp/update/');
             File::delete('tmp/update.zip');
             //check if all files are readable
             foreach ($manifest['src'] as $src) {
                 File::read('tmp/update/Lynxpress-' . $manifest['version'] . '/' . $src);
             }
             //replace all files registered in the manifest
             foreach ($manifest['src'] as $key => $src) {
                 File::read('tmp/update/Lynxpress-' . $manifest['version'] . '/' . $src)->save($manifest['dest'][$key]);
                 File::delete('tmp/update/Lynxpress-' . $manifest['version'] . '/' . $src);
             }
             //execute special queries
             foreach ($manifest['queries'] as $query) {
                 $this->_db->query(str_replace('{{prefix}}', DB_PREFIX, $query));
             }
             //remove files
             foreach ($manifest['remove'] as $file) {
                 File::delete($file, false);
             }
             $config = File::read(PATH . 'config.php');
             $config->_content = str_replace('(\'WS_VERSION\', \'' . WS_VERSION . '\')', '(\'WS_VERSION\', \'' . $manifest['version'] . '\')', $config->_content);
             $config->save();
             unset($config);
             $config = File::read(PATH . 'config.sample.php');
             $config->_content = str_replace('(\'WS_VERSION\', \'' . WS_VERSION . '\')', '(\'WS_VERSION\', \'' . $manifest['version'] . '\')', $config->_content);
             $config->save();
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::ws_update($result);
     }
 }
 /**
  * Delete a template
  *
  * Current and main template will raise an error
  *
  * @access	private
  */
 private function delete()
 {
     if (VPost::delete(false) && VPost::tpl_id() && $this->_user['delete_content']) {
         try {
             $tpl = new Setting(VPost::tpl_id());
             $tpl->_data = json_decode($tpl->_data, true);
             if ($tpl->_data['namespace'] == $this->_setting->_data) {
                 throw new Exception('Template currently used, action aborted');
             }
             if ($tpl->_data['namespace'] == 'main' || $tpl->_data['namespace'] == 'bobcat') {
                 throw new Exception('Default template can\'t be deleted, action aborted');
             }
             foreach ($tpl->_data['files'] as $file) {
                 File::delete(PATH . 'includes/templates/' . $tpl->_data['namespace'] . '/' . $file);
             }
             $tpl->delete();
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::template_deleted($result);
     } elseif (VPost::delete(false) && !$this->_user['delete_content']) {
         $this->_action_msg = ActionMessages::action_no_perm();
     }
 }
 /**
  * Save dump to a sql file
  *
  * @access	public
  * @param	string [$path]
  */
 public function save($path = 'dump.sql')
 {
     $dump = new File();
     $dump->_content = $this->_sql;
     $dump->save($path);
 }
 /**
  * Install a plugin from github
  *
  * @access	private
  */
 private function create()
 {
     if (VGet::action() == 'install' && VGet::user() && VGet::repo() && VGet::download()) {
         try {
             $curl = new Curl('https://api.github.com/repos/' . VGet::user() . '/' . VGet::repo() . '/downloads');
             $downloads = json_decode($curl->_content, true);
             if (empty($downloads)) {
                 throw new Exception('Archive doesn\'t exist on Github');
             }
             if (isset($downloads['message'])) {
                 throw new Exception($downloads['message']);
             }
             $url = null;
             foreach ($downloads as $download) {
                 if ($download['name'] == VGet::download()) {
                     if ($download['content_type'] != 'application/zip') {
                         throw new Exception('Invalid archive type! (.zip only)');
                     } else {
                         $url = $download['html_url'];
                     }
                 }
             }
             unset($curl);
             $curl = new Curl($url);
             $zip = new File();
             $zip->_content = $curl->_content;
             $zip->save('tmp/plugin.zip');
             $tmp = 'tmp/plg_' . md5_file('tmp/plugin.zip') . '/';
             File::unzip('tmp/plugin.zip', $tmp);
             File::delete('tmp/plugin.zip');
             $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();
             $this->_action_msg = ActionMessages::custom_good('Plugin "' . $setting->_name . '" installed');
         } catch (Exception $e) {
             $this->_action_msg = ActionMessages::custom_wrong($e->getMessage());
             //remove files
             foreach ($conf['admin'] as $file) {
                 File::delete($tmp . 'admin/' . $file, false);
             }
             foreach ($conf['site'] as $file) {
                 File::delete($tmp . 'site/' . $file, false);
             }
             foreach ($conf['library'] as $file) {
                 File::delete($tmp . 'library/' . $file, false);
             }
         }
     }
 }
 /**
  * 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);
     }
 }
 /**
  * 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());
         }
     }
 }
 /**
  * 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());
         }
     }
 }
 /**
  * Delete a plugin
  *
  * @access	private
  */
 private function delete()
 {
     if (VPost::delete(false) && VPost::plg_id() && $this->_user['delete_content']) {
         try {
             $plg = new Setting(VPost::plg_id());
             $plg->_data = json_decode($plg->_data, true);
             foreach ($plg->_data['admin'] as $file) {
                 File::delete('includes/' . $plg->_data['namespace'] . '/' . $file);
             }
             foreach ($plg->_data['site'] as $file) {
                 File::delete(PATH . 'includes/' . $file);
             }
             foreach ($plg->_data['library'] as $file) {
                 File::delete('library/' . $plg->_data['namespace'] . '/' . $file);
             }
             foreach ($plg->_data['uninstall'] as $query) {
                 $this->_db->query(str_replace('{{prefix}}', DB_PREFIX, $query));
             }
             File::delete(PATH . 'css/' . $plg->_data['namespace'] . '.css', false);
             $plg->delete();
             $result = true;
         } catch (Exception $e) {
             $result = $e->getMessage();
         }
         $this->_action_msg = ActionMessages::deleted($result);
     } elseif (VPost::delete(false) && !$this->_user['delete_content']) {
         $this->_action_msg = ActionMessages::action_no_perm();
     }
 }