/** * Install a new template * * @param integer $id_theme Theme ID * @param string $name Theme name (theme folder name) * @return array Array of errors */ public function install_tpl($id_theme, $name) { $error = array(); // check if already installed if ($this->exists($id_theme, $name)) { $error[] = array('error' => '_ALREADY_INSTALLED', 'label' => $name); } else { // check if template file exists if (file_exists('themes/' . $name . '_install.php')) { // load template installer (SQL instructions) require_once 'themes/' . $name . '_install.php'; // install $result = $this->db->single_exec($sql); if ($result[1]) { // refactory permissions on templates table $perm = new Permission_model(); $perm->refactory_table($_SESSION['xuid'], 1, 'templates'); return $result[0]; } else { $error[] = array('error' => '_TEMPLATE_NOT_INSTALLED', 'label' => $name); } } else { $error[] = array('error' => '_TEMPLATE_INSTALLER_NOT_FOUND', 'label' => $name); } } return $error; }
/** * Get User permission level on a table * * @static * @param integer $id_who User ID * @param string $what Privilege type * @return integer Permission level */ public static function get_ulevel($id_area, $id_who, $what) { $mod = new Permission_model(); $level = $mod->get_upriv($id_area, $id_who, $what); return $level; }
/** * Delete language * * @access private * @param array $_post _POST array * @return void */ private function deleting($_post) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'languages', $_post['id'], 4); if (is_null($msg)) { // action $mod = new Language_model(); $result = $mod->delete_lang($_post['id']); // set message $msg = AdmUtils_helper::set_msg($result); // clear useless permissions if ($result[1]) { $perm = new Permission_model(); $perm->deleting_by_what('languages', $_post['id']); // set what update $msg->update[] = array('element' => 'tdown', 'url' => BASE_URL . 'languages', 'title' => null); } } $this->response($msg); }
/** * Save article * * @param string $bid * @return void */ public function update($bid) { // load dictionaries $this->dict->get_words(); // get article id $mod = new Article_model(); $item = $mod->get_by_bid($bid); // check permission AdmUtils_helper::chklevel($_SESSION['xuid'], 'articles', $item->id, 2); // only if there are differences if ($item->content != $_POST['content']) { // tinymce $post = array('bid' => $bid, 'id_area' => $item->id_area, 'lang' => $item->lang, 'code_context' => $item->code_context, 'id_page' => $item->id_page, 'date_in' => time(), 'xkeys' => $item->xkeys, 'name' => $item->name, 'content' => $_POST['content'], 'excerpt' => 0, 'author' => $_SESSION['mail'], 'module' => $item->module, 'param' => $item->param, 'id_editor' => $_SESSION['xuid'], 'xon' => AUTOREFRESH); // insert new article's version $result = $mod->insert($post); if ($result[1]) { // add permission $perm = new Permission_model(); // privs permissions $array[] = array('action' => 'insert', 'id_what' => $result[0], 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('articles', $array, $item->id_area); } // set message X4Utils_helper::set_msg($result); echo $_SESSION['msg']; unset($_SESSION['msg']); } else { echo ''; } }
/** * Perform the uninstall * * @access private * @param array $_post _POST array * @return void */ private function uninstalling($_post) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'themes', $_post['id'], 4); if (is_null($msg)) { // do action $mod = new Theme_model(); $result = $mod->uninstall($_post['id'], $_post['name']); // check the result if (is_array($result)) { $this->notice(false, '_theme_not_uninstalled'); die; //X4Utils_helper::set_error($result, '_theme_not_uninstalled'); } else { // uninstalled // set message $msg = AdmUtils_helper::set_msg(true); // clear useless permissions $perm = new Permission_model(); if ($result) { $perm->deleting_by_what('themes', $_post['id']); } $msg->update[] = array('element' => 'tdown', 'url' => BASE_URL . 'themes', 'title' => null); } } $this->response($msg); }
/** * Duplicate an area for another language (secret method) * If you need to add another language to an area you can call this script * /admin/pages/duplicate_area_lang/ID_AREA/OLD_LANG/NEW_LANG * * @param integer $id_area Area ID * @param string $old_lang Old language to copy * @param string $new_lang New language to set * @return string */ public function duplicate_area_lang($id_area, $old_lang, $new_lang) { // Comment the next row to enable the method die('Operation disabled!'); $mod = new Page_model(); // duplicate $res = $mod->duplicate_area_lang($id_area, $old_lang, $new_lang); if ($res[1]) { // refactory permissions $mod = new Permission_model(); $mod->refactory($_SESSION['xuid']); echo '<h1>CONGRATULATIONS!</h1>'; echo '<p>The changes on the database are applied.</p>'; // print instructions for manual changes echo '<p>Follow this instructions to perform manual changes.</p> <ul> <li>Install the following modules: ' . implode(', ', $res[0]) . ' and configure them if needed</li> </ul> <p>Done!</p> <p>NOTE: this operation acts on the pages and articles of the CMS, if you use plugins you have to check if you need to duplicate contents.</p>'; } else { echo '<h1>WARNING!</h1>'; echo '<p>Something went wrong, changes are not applied.</p>'; } die; }
/** * Delete article's version * * @access private * @param integer $id article ID * @param string $bid BID code * @return void */ private function deleting_version($id) { $msg = null; // check permissions $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'articles', $id, 4); if (is_null($msg)) { // do action $mod = new Article_model(); $obj = $mod->get_by_id($id, 'articles', 'id_area, lang, bid'); $result = $mod->delete($id); // clear useless permissions if ($result[1]) { $perm = new Permission_model(); $perm->deleting_by_what('articles', $id); } // set message $msg = AdmUtils_helper::set_msg($result); // set what update if ($result[1]) { $msg->update[] = array('element' => 'topic', 'url' => BASE_URL . 'articles/history/' . $obj->id_area . '/' . $obj->lang . '/' . $obj->bid, 'title' => null); } } $this->response($msg); }
/** * Uninstalling template * * @access private * @param array $_post _POST array * @return void */ private function uninstalling($_post) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'templates', $_post['id'], 4); if (is_null($msg)) { // do action $mod = new Template_model(); $result = $mod->uninstall($_post['id']); if (is_array($result)) { // set error $msg = AdmUtils_helper::set_msg(false, '', $this->dict->get_word('_template_not_uninstalled')); } else { // set message $msg = AdmUtils_helper::set_msg($result); if ($result) { // clear useless permissions $perm = new Permission_model(); $perm->deleting_by_what('templates', $_post['id']); $theme = $mod->get_var($_post['id_theme'], 'themes', 'name'); $msg->update[] = array('element' => 'tdown', 'url' => BASE_URL . 'templates/index/' . $_post['id_theme'] . '/' . $theme, 'title' => null); } } } $this->response($msg); }
/** * Perform login * * @access private * @param array $_post _POST array * @return void */ private function do_login($_post) { // check failure counter if ($_SESSION['failed'] < 5) { // fields to set in sessions $fields = array('mail' => 'mail', 'username' => 'username', 'id' => 'xuid', 'lang' => 'lang', 'last_in' => 'last_in', 'level' => 'level'); // conditions $conditions = array('id_area' => 1, 'username' => $_post['username']); // remember me $conditions['password'] = isset($_post['hpwd']) && $_post['password'] == '12345678' ? $_post['hpwd'] : X4Utils_helper::hashing($_post['password']); // log in $login = X4Auth_helper::log_in('users', $conditions, $fields, true, true); if ($login) { // post login operations $_SESSION['site'] = SITE; $_SESSION['id_area'] = 1; // admin AREA ID // set cookie for remember me if (isset($_post['remember_me'])) { setcookie(COOKIE . '_login', $conditions['username'] . '-' . $conditions['password'], time() + 2592000, '/', $_SERVER['HTTP_HOST']); } // refactory permissions $mod = new Permission_model(); $mod->refactory($_SESSION['xuid']); // log if (LOGS) { $mod = new X4Auth_model('users'); $mod->logger($_SESSION['xuid'], 1, 'users', 'log in'); } // redirect header('Location: ' . $this->site->site->domain . '/' . $_SESSION['lang'] . '/admin'); die; } else { // increase failure counter $_SESSION['failed']++; if (LOGS) { $mod = new X4Auth_model('users'); $mod->logger(0, 1, 'users', 'log in failed for ' . $_post['username']); } } } // redirect header('Location: ' . BASE_URL . 'login'); die; }
/** * Duplicate an area for another language (secret method) * If you need to add another language to an area you can call this script * /admin/modules/duplicate_area_lang/ID_AREA/OLD_LANG/NEW_LANG * * @param integer $id_area Area ID * @param string $old_lang Old language to copy * @param string $new_lang New language to set * @return string */ public function duplicate_area_lang($id_area, $old_lang, $new_lang) { // Comment the next row to enable the method die('Operation disabled!'); $mod = new X4Plugin_model(); // duplicate $res = $mod->duplicate_modules_lang($id_area, $old_lang, $new_lang); if ($res) { // refactory permissions $mod = new Permission_model(); $mod->refactory($_SESSION['xuid']); echo '<h1>CONGRATULATIONS!</h1>'; echo '<p>The changes on the database are applied.</p>'; } else { echo '<h1>WARNING!</h1>'; echo '<p>Something went wrong, changes are not applied.</p>'; } die; }
/** * Install a plugin * * @param integer area ID * @param string plugin name (is the same name of the folder) * @return mixed integer if all runs fine, else an array of error strings */ public function install($id_area, $name) { $error = array(); if (!$this->exists($name, $id_area)) { if (file_exists(PATH . 'plugins/' . $name . '/install.php')) { // area name, required with some installer $area = $this->get_by_id($id_area, 'areas', 'name'); // load installer require_once PATH . 'plugins/' . $name . '/install.php'; // check requirements $error = $this->check_required($required, $id_area, 0); // check area requirements if (isset($area_limit) && !in_array($area->name, $area_limit)) { $error[] = array('error' => array('_incompatible_area'), 'label' => implode(', ', $area_limit)); } // check compatibility if (!isset($compatibility) || !$this->compatibility($compatibility)) { $error[] = array('error' => array('_incompatible_plugin'), 'label' => $name); } if (empty($error)) { // global queries if (!$this->exists($name, $id_area, 1)) { foreach ($sql0 as $i) { $result = $this->db->single_exec($i); } } // area dipendent queries foreach ($sql1 as $i) { $result = $this->db->single_exec($i); } if ($result[1]) { // initialize Mongo DB autoincrement index if (isset($sql2)) { $model = $sql2['model']; $mod = new $model(); $res = $mod->insert($sql2['index'], 'indexes'); } $perm = new Permission_model(); $perm->refactory($_SESSION['xuid']); // return an integer if installation run fine return $result[0]; } else { $error[] = array('error' => array('_plugin_not_installed'), 'label' => $name); } } } else { $error[] = array('error' => array('_missing_plugin_installer'), 'label' => $name); } } else { $error[] = array('error' => array('_already_installed'), 'label' => $name); } // return an array if happen an error return $error; }
/** * Register Edited image * * @access private * @param integer $id File ID (if 0 then is a new item) * @param array $_post _POST array * @return void */ private function saving($id_file, $_post) { $msg = null; // check permissions $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'files', $id_file, 2); if (is_null($msg)) { $ko = _MSG_ERROR; // check if set asnew $asnew = intval(isset($_post['asnew'])); $mod = new File_model(); $file = $mod->get_by_id($id_file); if ($file) { switch ($file->xtype) { case 0: // images $path = APATH . 'files/filemanager/img/'; $rotation = intval($_post['rotate']); $rotation = $rotation ? 360 - $rotation : 0; if ($asnew) { // save a new file // set the new name $final_name = X4Files_helper::get_final_name($path, $file->name); $chk = X4Files_helper::create_cropped($path . $file->name, $path . $final_name, array($_post['width'], $_post['height']), array($_post['xcoord'], $_post['ycoord']), true); if ($chk) { $post = array(); $post[] = array('id_area' => $file->id_area, 'xtype' => $file->xtype, 'category' => $file->category, 'subcategory' => $file->subcategory, 'name' => $final_name, 'alt' => $file->alt, 'xon' => 1); // insert $result = $mod->insert_file($post); // create permissions if ($result[1]) { $id = $result[0]; $perm = new Permission_model(); // privs permissions $array[] = array('action' => 'insert', 'id_what' => $id, 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('files', $array, $file->id_area); if ($rotation) { sleep(1); $res = X4Files_helper::rotate($path . $final_name, $path . $final_name, $rotation); } } } else { $result = array($_post['id'], intval($chk)); } } else { // replace old $chk = X4Files_helper::create_cropped($path . $file->name, $path . $file->name, array($_post['width'], $_post['height']), array($_post['xcoord'], $_post['ycoord']), true); if ($chk && $rotation) { sleep(1); $res = X4Files_helper::rotate($path . $file->name, $path . $file->name, $rotation); } $result = array($_post['id'], intval($chk)); $id = $file->id; } break; case 1: // generic text file $path = APATH . 'files/filemanager/files/'; $txt = $_post['content']; $res = file_put_contents($path . $file->name, $txt); $id = $id_file; $result = array($id, intval($res)); break; case 2: // video file // get the command, if exists $ffmpeg = str_replace(NL, '', $this->command_exist('ffmpeg')); if (!empty($ffmpeg)) { $file_name = $file->name; $mimes = array('video/quicktime' => 'mov', 'video/mp4' => 'mp4', 'video/webm' => 'webm', 'video/ogg' => 'ogv', 'application/ogg' => 'ogv', 'video/x-flv' => 'flv', 'video/avi' => 'avi', 'application/vnd.adobe.flash.movie' => 'swf', 'application/x-shockwave-flash' => 'swf'); if (isset($_post['capture'])) { // we have to extract a frame $vpath = APATH . 'files/filemanager/media/'; $ipath = APATH . 'files/filemanager/img/'; $file_name = str_replace($mimes[$_post['old_format']], 'jpg', $file_name); // set the new name $final_name = X4Files_helper::get_final_name($ipath, $file_name); //ffmpeg -i video_file -an -ss 27.888237 -vframes 1 -s 320x240 -f image2 image_file $chk = shell_exec($ffmpeg . ' -i ' . $vpath . $file->name . ' -an -ss ' . $_post['sec'] . ' -vframes 1 -s ' . $_post['iwidth'] . 'x' . $_post['iheight'] . ' -f image2 ' . $ipath . $final_name . ' 2>&1'); if ($chk && file_exists($ipath . $final_name)) { chmod($ipath . $final_name, 0777); $post = array(); $post[] = array('id_area' => $file->id_area, 'xtype' => 0, 'category' => $file->category, 'subcategory' => $file->subcategory, 'name' => $final_name, 'alt' => $file->alt, 'xon' => 1); // insert $result = $mod->insert_file($post); // create permissions if ($result[1]) { $id = $result[0]; $perm = new Permission_model(); // privs permissions $array[] = array('action' => 'insert', 'id_what' => $id, 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('files', $array, $file->id_area); } } } else { // is a video conversion $path = APATH . 'files/filemanager/media/'; $new_format = $new_size = 0; if ($_post['old_width'] != $_post['width'] || $_post['old_height'] != $_post['height']) { $new_size = 1; } // if new format is a new file if ($_post['old_format'] != $_post['format']) { $new_format = 1; $file_name = str_replace($mimes[$_post['old_format']], $mimes[$_post['format']], $file_name); } if ($asnew || $new_format) { // save a new file // set the new name $final_name = X4Files_helper::get_final_name($path, $file_name); if ($new_size) { $chk = shell_exec($ffmpeg . ' -i ' . $path . $file->name . ' -vf scale=' . $_post['width'] . ':' . $_post['height'] . ' ' . $path . $final_name . ' 2>&1'); } else { // -c:a copy $chk = shell_exec($ffmpeg . ' -i ' . $path . $file->name . ' ' . $path . $final_name . ' 2>&1'); } if ($chk) { chmod($path . $final_name, 0777); $post = array(); $post[] = array('id_area' => $file->id_area, 'xtype' => $file->xtype, 'category' => $file->category, 'subcategory' => $file->subcategory, 'name' => $final_name, 'alt' => $file->alt, 'xon' => 1); // insert $result = $mod->insert_file($post); // create permissions if ($result[1]) { $id = $result[0]; $perm = new Permission_model(); // privs permissions $array[] = array('action' => 'insert', 'id_what' => $id, 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('files', $array, $file->id_area); } } } else { // replace old if ($new_size) { $chk = shell_exec($ffmpeg . ' -i ' . $path . $file->name . ' -vf scale=' . $_post['width'] . ':' . $_post['height'] . ' ' . $path . $file->name . ' 2>&1'); } else { $chk = 1; } $result = array($_post['id'], intval($chk)); $id = $result[0]; } } } else { // ffmpeg not available $result = array(0, 0); $ko = _FFMPEG_NOT_FOUND; } break; case 3: // template $path = APATH . 'files/filemanager/template/'; if (extension_loaded('php5-tidy')) { // clean the code $tidy = tidy_parse_string($_post['content']); $tidy->cleanRepair(); $html = $tidy->html(); } else { $html = $_post['content']; } $res = file_put_contents($path . $file->name, $html); $id = $id_file; $result = array($id, intval($res)); break; } // set message $msg = AdmUtils_helper::set_msg($result, _MSG_OK, $ko); // set what update if ($result[1]) { $msg->update[] = array('element' => 'topic', 'url' => BASE_URL . 'files/editor/' . $id, 'title' => null); } } else { // file not found // set message $msg = AdmUtils_helper::set_msg(array(0, 0)); } } $this->response($msg); }
/** * Delete category * * @access private * @param integer $id Category ID * @param object $obj Category Obj * @return void */ private function deleting($id, $obj) { $msg = null; // check permissions $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'categories', $id, 4); if (is_null($msg)) { // do action $mod = new Category_model(); $result = $mod->delete($id); // set message $msg = AdmUtils_helper::set_msg($result); // clear useless permissions if ($result[1]) { $perm = new Permission_model(); $perm->deleting_by_what('categories', $id); // set what update $msg->update[] = array('element' => 'topic', 'url' => BASE_URL . 'categories/index/' . $obj->id_area . '/' . $obj->lang . '/' . $obj->tag, 'title' => null); } } $this->response($msg); }
/** * Register page's composition * Use _POST data * * @param integer item id (if 0 then is a new item) * @param array _POST array * @return void */ public function compositing() { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'pages', $_POST['id_page'], 3); if (is_null($msg)) { // handle _POST $sections = array(); $post = array('id_area' => $_POST['id_area'], 'id_page' => $_POST['id_page'], 'xon' => 1); // handle _POST for each section for ($i = 1; $i <= $_POST['snum']; $i++) { $post['progressive'] = $i; // delete first comma $articles = substr($_POST['sort' . $i], 0, 1) == ',' ? substr($_POST['sort' . $i], 1) : $_POST['sort' . $i]; $post['articles'] = str_replace(',', '|', $articles); $sections[] = $post; } // register composition $mod = new Section_model(); $result = $mod->compose($sections); APC && apc_delete(SITE . 'sections' . $post['id_page']); // set message $this->dict->get_words(); $msg = AdmUtils_helper::set_msg($result); // add permissions on new sections if ($result[1]) { $msg->update[] = array('element' => 'topic', 'url' => BASE_URL . 'sections/compose/' . $post['id_page'], 'title' => null); if (is_array($result[0]) && !empty($result[0])) { $perm = new Permission_model(); $array = array(); foreach ($result[0] as $i) { $array[] = array('action' => 'insert', 'id_what' => $i, 'id_user' => $_SESSION['xuid'], 'level' => 4); } $result = $perm->pexec('sections', $array, $_POST['id_area']); } } } $this->response($msg); }
/** * Perform the importing of words * * @access private * @param array $_post _POST array * @return void */ private function importing($_post) { $msg = null; // check permission $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], '_key_import', 0, 4); if (is_null($msg)) { // get key list($lang, $area, $what) = explode('-', $_post['what']); // handle _post $post = array('lang' => $_post['lang'], 'area' => $_post['area'], 'what' => $what, 'xon' => 1); // set the translator X4Core_core::auto_load('google_translate_library'); $translator = new GoogleTranslate($lang, $post['lang']); // get words to import $dict = new Dictionary_model(); if ($what == 'ALL') { // import all sections in an area $sections = $dict->get_sections($lang, $area); $result = true; foreach ($sections as $s) { // get words in section $words = $dict->get_words_to_import($lang, $area, $s->what, $post['lang'], $post['area']); if (!empty($words)) { $post['what'] = $s->what; // import foreach ($words as $i) { $post['xkey'] = $i->xkey; // try to translate if ($lang != $post['lang']) { $value = $translator->translate($i->xval); } else { $value = $i->xval; } // set the word $post['xval'] = $value; // insert $result = $dict->insert($post); // add permission if ($result[1]) { $amod = new Area_model(); $id_area = $amod->get_area_id($_post['area']); $perm = new Permission_model(); $array[] = array('action' => 'insert', 'id_what' => $result[0], 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('dictionary', $array, $id_area); } } } } // set what for redirect $what = 'global'; } else { // import only one section $words = $dict->get_words_to_import($lang, $area, $what, $post['lang'], $post['area']); $result = true; // import foreach ($words as $i) { $post['xkey'] = $i->xkey; // try to translate if ($lang != $post['lang']) { $value = $translator->translate($i->xval); } else { $value = $i->xval; } // set the word $post['xval'] = $value; // insert $result = $dict->insert($post); // add permission if ($result[1]) { $amod = new Area_model(); $id_area = $amod->get_area_id($_post['area']); $perm = new Permission_model(); $array[] = array('action' => 'insert', 'id_what' => $result[0], 'id_user' => $_SESSION['xuid'], 'level' => 4); $res = $perm->pexec('dictionary', $array, $id_area); } } } $msg = AdmUtils_helper::set_msg($result); // set what update if ($result[1]) { $msg->update[] = array('element' => 'tdown', 'url' => BASE_URL . 'dictionary/keys/' . $post['lang'] . '/' . $post['area'] . '/' . $what, 'title' => null); } } $this->response($msg); }
/** * Delete area * * @access private * @param integer $id Area ID * @param string $name Area name * @return void */ private function deleting($id, $name) { $msg = null; // check permissions $msg = AdmUtils_helper::chk_priv_level($_SESSION['xuid'], 'areas', $id, 4); if (is_null($msg)) { // action $area = new Area_model(); $result = $area->delete_area($id, $name); // set message $msg = AdmUtils_helper::set_msg($result); // clear useless permissions if ($result[1]) { $perm = new Permission_model(); $perm->deleting_by_what('areas', $id); // set what update $msg->update[] = array('element' => 'topic', 'url' => BASE_URL . 'areas/index/1', 'title' => null); } } $this->response($msg); }