/** * Чтение массива из кеша * @param string $cache_file кешируемый файл * @param int $mytime время жизни кеш файла, отличное от глобальных настроек * @return mixed массив, если файл кеширован и не требует обновления, * false - если необходимо обновить/создать */ public function read($cache_file, $mytime = false) { $cache_file = validpath($cache_file); if (!$cache_file || !$this->state) { return false; } $this->cache_files[] = $cache_file; $f = ROOT . 'include/cache/' . $cache_file . '.html'; $my_array = array(); if ($this->m) { $my_array = $this->m->get($cache_file); } elseif (file_exists($f)) { $contents = file_get_contents($f); $my_array = unserialize($contents); } if (!$my_array) { return false; } $array = (array) $my_array[0]; $time = $my_array['!_cache_time_']; $delay = $mytime ? $mytime : $this->time * 3600; if ($time < time() - $delay) { if ($this->m) { $this->m->delete($cache_file); } return false; } $this->pop_readed(); // Ибо уже кешировано return $array; }
/** * Выборка файлов(для АЦ) * @param string $path путь к дирректории * @param string $name имя дирректории * @param string $folder выбранная дирректория * @param array $apaths разрешённые дирректории внутри основной * @param bool $deny_delete запретить удалять? * @return null */ public function filechooser($path, $name, $folder = null, $apaths = null, $deny_delete = false) { $ajax = globals::g('ajax'); if (!validfolder($name, $path)) { return; } lang::o()->get('admin/filechooser'); $npath = ($path ? $path . '/' : '') . $name; if ($apaths) { $apaths = (array) $apaths; } if ($folder) { $folder = preg_replace('/(?:^|^(.*)\\/)[^\\/]+\\/\\.\\.\\/$/siu', '\\1', $folder); $folder = rtrim($folder, '/'); $folder = validpath($folder . '/', false, $apaths); $folder = rtrim($folder, '/'); if ($folder) { $npath .= "/" . rtrim($folder, '/'); } } $rows = file::o()->open_folder($npath, false, '^.+(\\.[a-z]+)?$'); if ($rows === false) { return; } file::o()->sort($npath, $rows); $arr = array(); foreach ($rows as $row) { $f = ROOT . $npath . '/' . $row; if (!$folder && $apaths && !validpath($row . '/', false, $apaths)) { continue; } if ($row == 'index.html') { continue; } $arr[$row] = array(is_dir($f), is_writable($f), filesize($f), filemtime($f)); } tpl::o()->assign('id', $name); tpl::o()->assign('files', $arr); tpl::o()->assign('deny_add', false); if ($folder) { tpl::o()->assign('parent', $folder . '/'); } elseif ($apaths) { tpl::o()->assign('deny_modify', true); } tpl::o()->assign('deny_delete', (bool) $deny_delete); if ($ajax) { ok(true); } tpl::o()->display('admin/filechooser.tpl'); }
/** * Удаление файла/папки * @param string $name имя языка * @param string $f2d имя файла/папки * @return null */ public function delete_file($name, $f2d) { $f2d = validpath($f2d); file::o()->unlink_folder(LANGUAGES_PATH . '/' . $name . '/' . $f2d); log_add('deleted_language_file', 'admin', array($f2d, $name)); }
/** * Модификация шаблона * @param string $f файл шаблона(относительно дирректории, с расширением) * @param string $what что заменять? (рег. выражение без делимиттеров) * @param string $with чем заменять? Чтобы добавить, достаточно дописать * $0 в нужное место, ибо работает рег. выражение в любом случае. * !Использовать только вставки вида ${номер группы}, для рег. выражений. * @param bool $regexp рег. выражение? * @param string $folder дирректория шаблонов * @return bool|int 2, если все шаблоны успешно изменены, true, если стандартный шаблон * успешно изменён * @note CSS, JS не нужно модифицировать, проще подцепить свой, * ибо всё переопределяемо. * @see join_js() * @see join_css() */ public function modify_template($f, $what, $with = "", $regexp = false, $folder = null) { if (!$what) { return false; } $f = validpath($f); $ft = file::o()->get_filetype($f); if (!$f || $ft != 'tpl' && $ft != 'xtpl') { return false; } if (!$regexp && !$this->insert) { $what = mpc($what); $what = preg_replace('/\\s+/s', '\\s+', $what); // не учитываем пробелы $regexp = true; } if (!$folder) { $b = 0; $dir = file::o()->open_folder(THEMES_PATH, true); $c = count($dir); $cb = true; for ($i = 0; $i < $c; $i++) { $cur = $dir[$i]; $r = $this->modify_template($f, $what, $with, $regexp, $cur); if ($cur == config::o()->v('default_style')) { $cb = $r; } $b += $r; } return !$cb ? false : $b == $c + $cb; } if ($this->insert) { $what = "<!-- inserted by plugin at time " . time() . ". begin-->\n" . $what . "\n<!-- inserted by plugin. end-->"; } elseif (!$this->comment) { $with = "<!-- " . crc32($what) . " replaced by plugin at time " . time() . ". begin-->\n" . $with . "\n<!-- replaced by plugin. end-->"; } // для обратной замены $p = THEMES_PATH . '/' . $folder . '/' . TEMPLATES_PATH . '/' . $f; if (!file_exists(ROOT . $p)) { return true; } // ибо может наследовать $ftpl = file_get_contents(ROOT . $p); if (!$this->replaced[$p]) { $this->replaced[$p] = array(); } if (!$this->insert) { $this->tmp_with = $with; $this->treplaced = array(); $ftpl = preg_replace_callback('/' . $what . '/siu', array($this, "replace_callback"), $ftpl); $this->replaced[$p][] = $this->treplaced; } else { if ($this->insert == 1) { $ftpl = $what . $ftpl; } else { $ftpl = $ftpl . $what; } $this->replaced[$p][] = array(array("", $what)); } return file::o()->write_file($ftpl, $p); }
/** * Сохранение стат. страницы * @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 $f2d имя файла/папки * @return null * @throws EngineException */ public function delete_file($name, $f2d) { $f2d = validpath($f2d, false, styles_man::$spaths); if (preg_match('/^\\/*(' . implode('|', array_map('mpc', styles_man::$spaths)) . ')\\/*$/siu', trim($f2d))) { throw new EngineException(); } file::o()->unlink_folder(THEMES_PATH . '/' . $name . '/' . $f2d); log_add('deleted_style_file', 'admin', array($f2d, $name)); }
/** * Добавление смайлов * @param string $f путь к файлу/дирректории * @return null * @throws EngineException */ protected function add($f = null) { $f = rtrim(validpath($f), '/'); $path = config::o()->v('smilies_folder') . ($f ? '/' . $f : ''); if (is_dir(ROOT . $path)) { $r = file::o()->open_folder($path, false, '^.*\\.(' . implode('|', array_map('mpc', $this->allowed_types)) . ')$'); $nr = array(); foreach ($r as $k => $v) { $k = ($f ? $f . '/' : '') . $v; if (db::o()->p($k)->count_rows('smilies', 'image = ?')) { continue; } $nr[$k] = $this->get_smilie_name($v); } tpl::o()->assign('smilies', $nr); } elseif (file_exists(ROOT . $path) && in_array(file::o()->get_filetype($path), $this->allowed_types)) { tpl::o()->assign('smilies', array($f => $this->get_smilie_name($f))); } else { throw new EngineException(); } tpl::o()->display('admin/smilies/add.tpl'); }
function tryfind() { switch (PLATFORM) { case "windows": $cacls = relpath("dumping/cacls.bat"); if ($cacls !== false) { $c = file($cacls); if (preg_match("/cacls.exe (.*)\\\\data \\/E/", $c[1], $matches)) { $try = "{$matches['1']}\\PostgreSQL\\bin"; if (validpath("{$try}")) { return $try; } } } $cd = getcwd(); while ($p = strrpos("{$cd}", "\\")) { $cd = substr($cd, 0, $p); if (validpath("{$cd}\\PostgreSQL\\bin")) { return "{$cd}\\PostgreSQL\\bin"; } } if (validpath("C:\\Cubit\\PostgreSQL\\bin")) { return "C:\\Cubit\\PostgreSQL\\bin"; } break; case "linux": $cd = getcwd(); while ($p = strrpos("{$cd}", "/")) { $cd = substr($cd, 0, $p); if (validpath("{$cd}/pgsql/bin")) { return "{$cd}/pgsql/bin"; } } $paths = array("/usr/local/cubit/pgsql/bin", "/usr/bin", "/usr/local/bin", "/usr/local/pgsql/bin", "/var/lib/pgsql/bin", "/var/pgsql/bin"); foreach ($paths as $p) { if (validpath("{$p}")) { return $p; } } break; } return false; }