/** * Add an element to the site menu * * @static * @access public * @param string [$ctl] Controller name * @param string [$name] Name that will be displayed * @return boolean */ public static function add_to_menu($ctl, $name) { try { $db =& Database::load(); $to_read['table'] = 'setting'; $to_read['columns'] = array('SETTING_ID'); $to_read['condition_columns'][':t'] = 'setting_type'; $to_read['condition_select_types'][':t'] = '='; $to_read['condition_values'][':t'] = 'site_menu'; $to_read['value_types'][':t'] = 'str'; $setting = $db->read($to_read); if (empty($setting)) { $setting = new Setting(); $setting->_name = 'Site Menu'; $setting->_type = 'site_menu'; $setting->_data = json_encode(array(array('ctl' => $ctl, 'name' => $name))); $setting->create(); } else { $setting = new Setting($setting[0]['SETTING_ID']); $data = json_decode($setting->_data, true); $data[] = array('ctl' => $ctl, 'name' => $name); $setting->_data = json_encode($data); $setting->update('_data', 'str'); } return true; } catch (Exception $e) { return false; } }
/** * 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()); } } }
/** * Create setting table * * @access private */ private function create_setting() { $sql = 'CREATE TABLE `' . $this->_db_prefix . 'setting` ( `SETTING_ID` int(11) unsigned NOT NULL AUTO_INCREMENT, `setting_name` text NOT NULL, `setting_type` tinytext NOT NULL, `setting_data` text COMMENT \'data generally stored as a json encoded array\', PRIMARY KEY (`SETTING_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;'; $create = $this->_db->prepare($sql); $create->execute(); if ($create->errorCode() != '00000') { throw new Exception('false create'); } else { $setting = new Setting(); $setting->_name = 'Share Buttons'; $setting->_type = 'share_buttons'; $setting->_data = '["facebook","twitter","google"]'; $setting->create(); $setting = new Setting(); $setting->_name = 'All Roles'; $setting->_type = 'all_roles'; $setting->_data = '[]'; $setting->create(); $setting = new Setting(); $setting->_name = 'Post'; $setting->_type = 'post'; $setting->_data = json_encode(array('media' => false)); $setting->create(); $setting = new Setting(); $setting->_name = 'Default Page'; $setting->_type = 'default_page'; $setting->_data = json_encode(array('type' => 'posts', 'view' => 'all')); $setting->create(); $setting = new Setting(); $setting->_name = 'Main Template'; $setting->_type = 'current_template'; $setting->_data = 'main'; $setting->create(); $setting = new Setting(); $setting->_name = 'Main template'; $setting->_type = 'template'; $setting->_data = '{"name":"Main Template","namespace":"main","files":["class.html.view.php","css/html5.css","css/mobile.css","css/style.css","html/footer_html5.php","html/footer_mobile.php","html/footer.php","html/header_html5.php","html/header_mobile.php","html/header.php","index.html"],"author":"Baptiste Langlade","url":"http://www.lynxpress.org"}'; $setting->create(); $setting = new Setting(); $setting->_name = 'Bobcat Template'; $setting->_type = 'template'; $setting->_data = '{"name":"Bobcat Template","namespace":"bobcat","author":"Baptiste Langlade","url":"http://www.lynxpress.org","files":["class.html.view.php","css/html5.css","css/mobile.css","css/style.css","css/index.html","html/footer_html5.php","html/footer_mobile.php","html/footer.php","html/header_html5.php","html/header_mobile.php","html/header.php","html/index.html","index.html"]}'; $setting->create(); } }
/** * 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); } } } }
/** * 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()); } } }
/** * Logout a user * * @access public */ public function logout() { $to_read['table'] = 'setting'; $to_read['columns'] = array('SETTING_ID'); $to_read['condition_columns'][':t'] = 'setting_type'; $to_read['condition_select_types'][':t'] = '='; $to_read['condition_values'][':t'] = 'user_' . VSession::user_id(); $to_read['value_types'][':t'] = 'str'; $res = $this->_db->read($to_read); if (empty($res)) { $setting = new Setting(); $setting->_name = 'User preferences for "' . VSession::username() . '"'; $setting->_type = 'user_' . VSession::user_id(); $setting->_data = json_encode(array('last_visit' => date('Y-m-d H:i:s'), 'timeline' => array())); $setting->create(); } else { $setting = new Setting($res[0]['SETTING_ID']); $setting->_data = json_decode($setting->_data, true); $data = $setting->_data; $data['last_visit'] = date('Y-m-d H:i:s'); $setting->_data = json_encode($data); $setting->update('_data', 'str'); } session_destroy(); header('Location: index.php?ns=session&ctl=login&loggedout=true'); }
/** * 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()); } } }
/** * Create a new role * * @access private */ private function create() { if (VPost::add_role(false) && $this->check_post_data()) { try { $new_role = new Setting(); $new_role->_name = strtolower(VPost::role()); $new_role->_type = 'role'; $new_role->_data = json_encode(array('dashboard' => false, 'post' => false, 'media' => false, 'album_photo' => false, 'comments' => false, 'delete_content' => false, 'settings' => false)); $new_role->create(); //update array containing all roles name $to_read['table'] = 'setting'; $to_read['columns'] = array('SETTING_ID'); $to_read['condition_columns'][':t'] = 'setting_type'; $to_read['condition_select_types'][':t'] = '='; $to_read['condition_values'][':t'] = 'all_roles'; $to_read['value_types'][':t'] = 'str'; $all_roles = $this->_db->read($to_read); //if the setting does't exist, we create it with the new role if (empty($all_roles)) { $all_roles = new Setting(); $all_roles->_name = 'All roles'; $all_roles->_type = 'all_roles'; $all_roles->_data = json_encode(array(strtolower(VPost::role()))); $all_roles->create(); } else { $all_roles = new Setting($all_roles[0]['SETTING_ID']); $roles = json_decode($all_roles->_data, true); array_push($roles, strtolower(VPost::role())); $all_roles->_data = json_encode($roles); $all_roles->update('_data', 'str'); } //end update $result = true; } catch (Exception $e) { $result = $e->getMessage(); } $this->_action_msg = ActionMessages::created($result); } }