/** * Построение плагина * @param array $data массив данных * @return null * @throws EngineException */ public function build($data) { lang::o()->get('admin/plugins'); $data_params = array("plugin", "version", "author", "name", "descr", "comp", "comp_min", "comp_max"); extract(rex($data, $data_params)); if (!validword($plugin)) { throw new EngineException("plugins_invalid_name"); } $settings = modsettings::o()->make($data); $defaults = modsettings::o()->save(modsettings::nocache_id, $data); $vars = array($plugin, var_export((string) $version, true), var_export((string) $author, true), var_export((string) $name, true), var_export((string) $descr, true), var_export((string) $comp, true), var_export((string) $comp_min, true), var_export((string) $comp_max, true), var_export((array) $settings, true), $plugin, var_export((array) $defaults, true)); $contents = @file_get_contents(ROOT . self::plugin_template); $contents = vsprintf($contents, $vars); /* @var $uploader uploader */ $uploader = n("uploader"); $uploader->download_headers($contents, 'plugin.' . $plugin . '.php', 'text/plain'); }
/** * Добавление модуля/класса/переменных в список разрешённых * @param string $what что добавляем * @param string $type тип(modules|acp_modules|acp_pages|classes) * @return bool|allowed false, если не добавили, иначе - $this */ public function add($what, $type = "modules") { switch ($type) { case 'modules': case 'acp_modules': case 'acp_pages': $v =& $this->allowed[$type]; break; case 'classes': $v =& $this->{$type}; break; default: return false; } if (!validword($what)) { return false; } $v[] = $what; return $this; }
/** * Сохранение стат. страницы * @param array $data массив данных * @return null * @throws EngineException */ public function save($data) { $admin_file = globals::g('admin_file'); $cols = array('url', 'title', 'content', 'type'); $update = rex($data, $cols); $id = (int) $data['id']; if (!validword($update['url'])) { throw new EngineException('static_empty_url'); } if (!$update['title']) { throw new EngineException('static_empty_title'); } if ($update['type'] == 'html') { $update['content'] = $data['html']; } elseif ($update['type'] == 'tpl') { $update['content'] = $data['tpl']; if (!validpath($update['content']) || !tpl::o()->template_exists($update['content'])) { throw new EngineException('static_tpl_not_exists'); } } if (!$update['content']) { throw new EngineException('static_empty_content'); } try { plugins::o()->pass_data(array("update" => &$update, "id" => $id), true)->run_hook('admin_static_save'); } catch (PReturn $e) { return $e->r(); } if (!$id) { db::o()->insert($update, 'static'); log_add('added_static', 'admin', $data['url']); } else { db::o()->p($id)->update($update, 'static', 'WHERE id=? LIMIT 1'); log_add('changed_static', 'admin', $data['url']); } furl::o()->location($admin_file); }
/** * Клонирование языкового пакета * @param string $name имя языка * @param string $newname новое имя языка * @return null * @throws EngineException */ public function copy($name, $newname) { lang::o()->get('admin/languages'); if (!validword($newname)) { throw new EngineException('languages_invalid_new_name'); } file::o()->copy_folder(LANGUAGES_PATH . '/' . $name, LANGUAGES_PATH . '/' . $newname); log_add('copied_language', 'admin', array($newname, $name)); }
/** * Сохранение категории * @param array $data массив данных категории * @param array $type тип категории * @return null * @throws EngineException */ public function save($type, $data) { $admin_file = globals::g('admin_file'); $cols = array('parent_id', 'name', 'transl_name', 'descr', 'post_allow', 'pattern'); if ($data['id']) { $id = (int) $data['id']; } $update = rex($data, $cols); $update['type'] = $type; if (!$update['name'] || !$update['transl_name']) { throw new EngineException("cats_invalid_input"); } if (!validword($update['transl_name'])) { throw new EngineException("cats_invalid_transl_name"); } $update['pattern'] = (int) $update['pattern']; if (!$this->cats->get($update['parent_id'])) { $update['parent_id'] = 0; } else { $update['parent_id'] = (int) $update['parent_id']; } $update['post_allow'] = (bool) $update['post_allow']; try { plugins::o()->pass_data(array("update" => &$update, "id" => $id), true)->run_hook('admin_cats_save'); } catch (PReturn $e) { return $e->r(); } if ($id) { db::o()->p($id)->update($update, 'categories', 'WHERE id=? LIMIT 1'); log_add('changed_cat', 'admin', $id); } else { db::o()->insert($update, 'categories'); log_add('added_cat', 'admin'); } db::o()->query('ALTER TABLE `categories` ORDER BY `sort`'); cache::o()->remove('categories'); furl::o()->location($admin_file); }
/** * Переопределение класса * @param string $original имя базового класса * @param string $new имя нового класса * @return plugins $this */ public function redefine_class($original, $new) { if (!$this->state || !$this->current_plugin) { return $this; } if (!validword($new)) { // подгружается апосле return $this; } $this->redefined[$original] = $new; $this->inc_redefined[$original][] = $this->current_plugin; return $this; }
/** * Клонирование темы * @param string $name имя темы * @param string $newname новое имя темы * @return null * @throws EngineException */ public function copy($name, $newname) { lang::o()->get('admin/styles'); if (!validword($newname)) { throw new EngineException('styles_invalid_new_name'); } file::o()->copy_folder(THEMES_PATH . '/' . $name, THEMES_PATH . '/' . $newname); log_add('copied_style', 'admin', array($newname, $name)); }
/** * Создание алиаса к классу * @param string $original имя оригинального класса * @param string $alias имя алиаса * @return bool true, если успешно создали алиас */ function class_alias($original, $alias) { if (!class_exists($original, false)) { return false; } $p = ROOT . CLASS_ALIASES; $f = file_get_contents($p); $string = "if (!class_exists('" . $alias . "') && class_exists('" . $original . "')){class " . $alias . " extends " . $original . "{}}\n"; if (mb_strpos($f, $string) === false) { if (class_exists($alias)) { return true; } if (!!validword($alias)) { return false; } $f = mb_substr($f, 0, mb_strlen($f) - 2); $f .= $string . '?>'; } else { return true; } file_put_contents($p, $f); load_aliases(); return true; }
/** * Построение настроек из формы * @param array $data массив данных * @return array массив настроек */ public function make($data) { $data_params = array('params' => 'mparam', 'key' => 'keytype', 'val' => 'valtype', 'limit' => 'keylimit', 'enum' => 'enumvals'); extract(rex($data, $data_params)); if (!$params) { return; } $params = (array) $params; $key = (array) $key; $val = (array) $val; $limit = (array) $limit; $enum = (array) $enum; $r = array(); foreach ($params as $k => $param) { if (!$param || !validword($param)) { continue; } $v = $val[$k]; $l = (int) $limit[$k]; $e = trim($enum[$k]); $k = $key[$k]; $a = ""; switch ($k) { case "unlimited": $a = "[]"; case "simple": break; case "limited": if (!$l || $l < 2) { $a = false; } else { $a = "[" . $l . "]"; } break; case "string": case "integer": $a = "[" . $k . "]"; break; default: $a = false; break; } if ($a === false) { continue; } $param = $param . $a; $a = ""; switch ($v) { case "string": case "integer": case "text": $a = $v; break; case "enum": if (!preg_match('/^([a-z0-9\\-\\_]+(;|$))+$/si', $e)) { $a = false; } else { $a = $v . '[' . $e . ']'; } } if ($a === false) { continue; } $r[$param] = $a; } return $r; }
/** * Сохранение типов файлов * @param array $data массив данных * @return null * @throws EngineException */ public function save($data) { $admin_file = globals::g('admin_file'); $oname = $data['old_name']; $cols = array('name', 'image', 'types', 'MIMES', 'max_filesize', 'max_width', 'max_height', 'makes_preview', 'allowed'); $update = rex($data, $cols); $update['makes_preview'] = (bool) $update['makes_preview']; $update['allowed'] = (bool) $update['allowed']; $update['max_filesize'] = (int) $update['max_filesize']; $update['max_width'] = (int) $update['max_width']; $update['max_height'] = (int) $update['max_height']; if (!validword($update['name'])) { throw new EngineException('allowedft_invalid_name'); } if (!$update['max_filesize']) { throw new EngineException('allowedft_invalid_filesize'); } if (!$update['types']) { throw new EngineException('allowedft_invalid_types'); } try { plugins::o()->pass_data(array("update" => &$update, "oname" => $oname), true)->run_hook('admin_allowedft_save'); } catch (PReturn $e) { return $e->r(); } if ($oname) { db::o()->p($oname)->update($update, 'allowed_ft', 'WHERE name=? LIMIT 1'); } else { db::o()->insert($update, 'allowed_ft'); log_add('added_filetype', 'admin', $update['name']); } furl::o()->location($admin_file); }
/** * Установка цвета темы * @param string $color имя цвета * @return string установленный цвет */ public function set_color($color) { $vars = $this->init_cfg(null, null); $def = strtolower($vars['default_name']); $defc = strtolower($vars['color_default']); $color = strtolower($color); $allowed = strtolower($vars['allowed_colors']); if ($color == strtolower($def)) { $color_path = ""; } else { if (!checkpos($allowed, $color, "|")) { if (checkpos($allowed, $defc, "|")) { $color = $defc; } else { return $this->set_color($def); } } if (!validword($color)) { $color_path = ""; } else { $color_path = COLORS_PATH . "/" . $color . "/"; } } $this->assign('color_path', $color_path); globals::s('color_path', $color_path); return $color; }
/** * Поиск в файле, содержащем массив * @param array $r массив файлов(ключи) и подсвеченных результатов(значения) * @param array $c контент * @param string $what что ищем * @param int $where где * @param string $nf полный путь до файла включительно * @param string $wf путь до папки, в кот. расположен файл * @return null */ protected function search_infiles_a(&$r, $c, $what, $where, $nf, $wf) { $tmp = array(); $b = $where == -1; if ($b) { $where = 0; } $keys = array(); foreach ($c as $k => $v) { if (!$b && !validword($k)) { continue; } if (is_null($this->infiles_replace)) { $v = display::o()->html_encode($v); $k = display::o()->html_encode($k); } if ($where == 0 && !preg_match($what, $v)) { continue; } if ($where == 1 && !preg_match($what, $k)) { continue; } if ($where == 2 && !preg_match($what, $v) && !preg_match($what, $k)) { continue; } if ($where < 0 || $where > 2) { continue; } if (is_null($this->infiles_replace)) { if ($where == 0 || $where == 2) { $this->highlight_text($v, $what, false); } if ($where == 1 || $where == 2) { $this->highlight_text($k, $what, false); } } else { $ok = $k; if ($where == 0 || $where == 2) { $v = preg_replace($what, $this->infiles_replace, $v); } if ($where == 1 || $where == 2) { $k = preg_replace($what, $this->infiles_replace, $k); } if ($ok != $k) { $keys[$k] = $ok; } } $tmp[$k] = $v; } if (!$tmp) { return; } if (!is_null($this->infiles_replace) && $this->infiles_replace_cb) { call_user_func($this->infiles_replace_cb, $nf, $tmp, $keys); } $r[$wf] = $tmp; }
/** * Получение настроек блока * @param string $file файл * @return array массив настроек */ public function get_settings($file) { if (!$this->state) { return; } if ($this->settings[$file]) { return $this->settings[$file]; } if (!validword($file)) { return; } $r = db::o()->p($file)->query('SELECT id,settings FROM blocks WHERE file=? LIMIT 1'); list($id, $settings) = db::o()->fetch_row($r); if (!$id) { return; } $settings = unserialize($settings); $object = plugins::o()->get_module($file, true); if (!$object || !isset($object->settings)) { return; } modsettings::o()->change_type('blocks')->parse($id, $object, $settings); $this->settings[$file] = $object->settings; return $this->settings[$file]; }
/** * Построение паттерна * @param array $pattern массив данных * @return array массив паттерна * @throws EngineException */ protected function build_pattern($pattern) { $c = count($pattern['name']); $obj = array(); foreach ($pattern as $type => $e) { if (!is_array($e) || count($e) != $c) { throw new EngineException('patterns_invalid_data'); } for ($i = 0; $i < $c; $i++) { $obj[$i][$type] = $e[$i]; } } $pattern = array(); foreach ($obj as $i => $e) { $type = $e['type']; if (!$type || !in_array($type, $this->types)) { continue; } if (!$e['name']) { continue; } if ((!$e['rname'] || !validword($e['rname'])) && $type != 'html') { $e['rname'] = display::o()->translite($e['name']); } if (!$e['formdata']) { continue; } $c = false; $size = (int) $e['size']; unset($e['size']); switch ($type) { case 'select': case 'radio': if (!$e['values']) { $c = true; break; } unset($e['html']); break; case 'html': if (!$e['html']) { $c = true; break; } unset($e['rname']); unset($e['descr']); unset($e['values']); break; case "input": if ($size) { $e['size'] = $size; } default: unset($e['html']); unset($e['values']); break; } if (!$c) { $pattern[] = $e; } } return $pattern; }
/** * Инициализация инсталляции * @return null */ protected function show_license() { $f = lang::o()->v('install_license'); if (!$f || !validword($f)) { $f = 'LICENSE_EN'; } tpl::o()->assign('license', file_get_contents(ROOT . $f)); tpl::o()->display('license'); }
/** * Сохранение доп. полей * @param array $data массив данных * @return null * @throws EngineException */ public function save($data) { $admin_file = globals::g('admin_file'); $oname = $data['old_field']; $values = (array) $data['values']; $keys = (array) $data['keys']; $cols = array('field', 'name', 'allowed', 'descr', 'type', 'show_register', 'show_profile'); $update = rex($data, $cols); $update['show_register'] = (bool) $update['show_register']; $update['show_profile'] = (bool) $update['show_profile']; if (!validword($update['field'])) { throw new EngineException('userfields_empty_field'); } if (!$update['name']) { throw new EngineException('userfields_empty_name'); } /* @var $uf userfields */ $uf = n("userfields"); $ct = $uf->get_var('types', $update['type']); if (is_null($ct)) { throw new EngineException('userfields_empty_type'); } if ($ct) { $allowed =& $update['allowed']; if ($ct == 2) { $allowed = array(); $cv = count($values); if ($cv == count($keys) && $cv >= 2) { for ($i = 0; $i < $cv; $i++) { $key = $keys[$i]; $value = $values[$i]; if (!validword($key, 'latin', 1) && !is_numeric($key)) { continue; } if (!$value) { continue; } $allowed[$key] = $value; } $allowed = serialize($allowed); } } if (!$allowed) { throw new EngineException('userfields_empty_allowed'); } } try { plugins::o()->pass_data(array('update' => &$update, 'oname' => $oname), true)->run_hook('admin_userfields_save'); } catch (PReturn $e) { return $e->r(); } if ($oname) { db::o()->p($oname)->update($update, 'users_fields', 'WHERE field=? LIMIT 1'); } else { db::o()->insert($update, 'users_fields'); log_add('added_userfield', 'admin', $update['field']); } cache::o()->remove('userfields'); furl::o()->location($admin_file); }