/** * Create auth lib * @param users_collection users handle * @param bool disable auth */ function __construct($disable_auth = false) { $core = core::selfie(); $this->mod_users = core::module('users'); $this->_cookie_domain = '.' . @$_SERVER['HTTP_HOST']; $this->_cookie_httponly = $core->cfg('auth.cookie_httponly', false); $this->_autologin_UID = $this->mod_users->cfg('autologin_UID', 0); $this->_disable_auth = $disable_auth; if ($this->is_crawler()) { $this->_disable_auth = true; } if ($this->_disable_auth) { core::dprint('[AUTH] Sessions disabled'); } $this->users = $this->mod_users->get_users_handle(); $this->sessions = $this->mod_users->get_sessions_handle()->with_user_agent($core->cfg('auth.with_browser', true)); if ($this->_disable_auth) { $this->set_null_session(); return; } if ($this->_autologin_UID) { $this->set_uid_session($this->_autologin_UID); return; } }
/** * Load smarty3 * @param mixed $params * @return Smarty3 */ static function _init_smarty3($params) { $smarty = false; // check smarty autoload $status = class_exists('Smarty'); $file = loader::get_public() . loader::DIR_EXT . 'smarty3/Smarty.php'; if (!file_exists($file)) { throw new lib_exception('Smarty3 file not found'); } require $file; if (!class_exists('Smarty3', false)) { throw new lib_exception('Smarty3 class not found'); } $smarty = new Smarty3(); $smarty->debugging = isset($params['debugging']) ? $params['debugging'] : core::selfie()->cfg('debug_templates', false); $smarty->caching = isset($params['caching']) ? $params['caching'] : false; $smarty->cache_lifetime = isset($params['cache_lifetime']) ? $params['cache_lifetime'] : 120; $smarty->cache_locking = true; $smarty->compile_check = isset($params['compile_check']) ? $params['compile_check'] : true; $smarty->force_compile = isset($params['force_compile']) ? $params['force_compile'] : false; $smarty->merge_compiled_includes = false; $smarty->error_reporting = error_reporting() & ~E_NOTICE; $smarty->addPluginsDir(loader::get_public() . loader::DIR_EXT . 'smarty3/plugins'); // add asset compiler plugin $smarty->addPluginsDir(loader::get_public(loader::DIR_EXT . 'smarty-sacy')); core::dprint(array('[smarty3] dist:%s %s debugging: %s, caching: %s, force: %s, ttl: %d', $status ? 'composer' : 'old', Smarty3::SMARTY_VERSION, $smarty->debugging ? 'yes' : 'no', $smarty->caching ? 'yes' : 'no', $smarty->force_compile ? 'yes' : 'no', $smarty->cache_lifetime), core::E_RENDER); $template = core::selfie()->cfg('template'); self::$parser = $smarty; self::set_template($template); return $smarty; }
/** * Remember! * Assign current item in controller for comment linking! */ function run() { if (loader::in_ajax() !== true) { throw new controller_exception('Cant touch this ' . __METHOD__); return false; } core::dprint('run comment modify'); $pctl = core::modules()->get_router()->get_controller(); $user = core::lib('auth')->get_user(); /** * Parent item, must be assigned thru @see module_controller::set_current_item() * @var abs_collection_item */ $post = $pctl->get_current_item(); // var_dump(get_class($post), core::get_modules()->get_router()->get_name()); if (!$post) { throw new controller_exception('No item assigned'); } if (!$post->has_behavior('sat.commentable')) { throw new controller_exception('Not commentable'); } $comments = $post->behavior('sat.commentable')->get_attach_model(); //get_comments(); $request = core::lib('request'); $renderer = core::lib('renderer'); $user_id = core::lib('auth')->get_user()->id; $pid = (int) $request->post('pid', 0); $limit = core::selfie()->cfg('comment_interval', 60); $auth = core::lib('auth'); /** @var aregistry $sd */ $sd = $auth->get_current_session()->get_storage(); $time = $sd->comments_last_time; //$time = $comments->get_last_time($pid, $user_id); // disallow by interval if ($time && $time + $limit > time()) { $pctl->set_null_template(); $renderer->set_ajax_answer(array('status' => false, 'id' => 0, 'message' => vsprintf(i18n::T('sat\\comment_interval_restriction'), $time + $limit - time())))->ajax_flush(); // else core::get_instance()->set_message(array('content', 'comment_interval_restriction')); return; // exit } $sd->comments_last_time = time(); $username = functions::request_var('username', ''); $text = functions::request_var('text', ''); $api = functions::request_var('api'); $id = $comments->modify(array('user_ip' => core::lib('auth')->get_user_ip(true), 'user_id' => $user_id, 'ctype_id' => $post->get_ctype_id(), 'username' => $username, 'pid' => $pid, 'text' => $text, 'type' => functions::request_var('type', 0), 'tpid' => functions::request_var('tpid', 0), 'api' => $api)); $comment = $comments->get_item_by_id($id); if (!$comment) { throw new controller_exception('[ajax] Comment create failed'); } $comment->load_secondary(); $renderer->set_data('comment', $comment->render())->set_ajax_answer(array('status' => true, 'id' => $id))->set_ajax_message(i18n::T('sat\\comment_posted')); //->set_main_template('content/comment/view'); $renderer->ajax_flush('shared/comments/comment'); // alright, update counter return $id; }
function run() { $posts = core::module('sat')->get_news_handle()->set_order('created_at DESC')->set_limit(10)->where('active', true)->with_deps(array('category'))->load()->render(); $parser = tpl_loader::get_parser(true); $parser->assign('site', core::module('sat')->get_current_site()->render()); $parser->assign('posts', $posts); header('Content-Type: text/xml; charset=UTF-8'); // display appends smarty_debug echo $parser->fetch('partials/sat/news/xml.tpl'); core::selfie()->halt(); }
function __construct($title, $err_no = 0) { if (class_exists('loader', false) && !loader::_option(loader::OPTION_TESTING)) { $this->log_id = false; if ($err_no == self::CRITICAL) { echo "<h1 style='color:darkred'>Danger! {$title} </h1>"; } else { // override email if (class_exists('core', 0) && core::selfie()) { $this->bugs_email = core::selfie()->cfg('email', $this->bugs_email); } // log if logger available if ($this->logable && class_exists('core', 0) && ($libs = core::libs()) && $libs->is_registered('logger') && ($logger = core::lib('logger'))) { $this->log_id = $logger->error($title, $err_no, $this->getTraceAsString()); } } } parent::__construct($title, $err_no); self::$last_exception = $this; }
/** * Инициализируем класс (конструктор) * Вызывыем из дочернего класса * * @param string физ.путь до модуля */ public function __construct($root_dir = null, $params = null) { if (empty($root_dir)) { throw new module_exception('empty root, register ' . get_class($this)); } $this->root_dir = $root_dir; $this->classes_chroot = $root_dir . 'classes/'; $class = get_class($this); // if not core, get its instance if ($class != strtolower('core')) { $this->name = !isset($params['name']) ? substr($class, strlen(loader::CLASS_PREFIX)) : $params['name']; $this->core = core::selfie(); // alloc manager // todo: why clone? $this->manager = clone core::lib('manager'); } else { $this->name = $class; } if (!empty($params['alias'])) { // @todo trim $this->_aliases = explode(',', $params['alias']); } // append config|create $this->init_config($params, true); $this->ioc = new module_ioc($this->IOC_initialize(), $this); $this->construct_after(); $this->_register_ctypes(); }
/** * Block entry point * * @param array * module // module tag * action // block action * cache // seconds, cache data * other params * @param Smarty3 $smarty */ function smarty_function_satblock($params, &$smarty) { if (empty($params['action'])) { throw new block_exception('Bad action'); } $orig_params = $params; $orig_action = $params['action']; // module.action if (strpos($params['action'], '.') !== false) { $t = explode('.', $params['action']); $params['action'] = $t[1]; $params['module'] = $t[0]; } $action = @$params['action']; $module = @$params['module']; $cache = @$params['cache']; $cache_id = null; $cacher = null; $with_cache = false; $cached = false; $buffer = null; $core = core::selfie(); // unpack params to local scope // extract($params, EXTR_SKIP); /** * Cache block */ if (!empty($cache)) { /** @var cache $cacher_factory */ $cacher_factory = core::lib('cache'); $cacher = $cacher_factory->has_memory() ? $cacher_factory->get_memory_handle() : $cacher_factory->get_file_handle(); if ($cacher) { $cache_time = $cache; unset($params['cache']); $cache_id = 'block_' . md5(serialize($params)); $result = $cacher->get($cache_id, false); if (null !== $result) { core::dprint('..block cached "' . $orig_action . '" using ' . get_class($cacher), core::E_NOTICE); $buffer = $result; $cached = true; } $with_cache = true; } } if (!$cached) { try { if (empty($module)) { $module = 'core'; } if ($pmod = core::module($module)) { unset($params['action'], $params['module']); // Run block action $buffer = $pmod->run_block($action, $params); if ($with_cache) { $cacher->set($cache_id, $buffer, $cache_time); } } } catch (module_exception $e) { return '[block] module-error: ' . $e->getMessage(); } catch (block_exception $e) { return '[block] error: ' . $e->getMessage(); } } // debug block if (core::is_debug() && $core->cfg('debug_templates')) { $dparams = array(); foreach ($orig_params as $pk => $pv) { $dparams[] = sprintf('data-%s = "%s"' . PHP_EOL, $pk, $pv); } /** @var Smarty_Template_Source $source */ $source = $smarty->source; // @todo how to get current line? // $dparams []= sprintf('data-parent-template = "%s"' . PHP_EOL, $source->filepath); $dsparams = join(' ', $dparams); $buffer = <<<DBG <satblock class="sat-block" {$dsparams}> {$buffer} </satblock> DBG; } return $buffer; }
return array('api/editor/menu' => array(), 'api/editor/i18n' => array(), 'ctype/update' => array('template' => false, 'action' => function (module_controller $ctrl) { $request = $ctrl->get_request(); /** POST: ctype : sat.node field : title id : 4 content : data */ //@todo add some security checks $id = $request->post('id'); $field = $request->post('field'); $ctype = $request->post('ctype'); $content = $request->post('content'); $model = core::selfie()->get_ctype($ctype); if (!$model) { throw new controller_exception('bad ctype'); } $collection = $model->get_ctype_collection(); if (!$collection) { throw new controller_exception('No collection'); } // allow only text fields if (!$collection->has_field($field) || ($fieldOptions = $collection->field($field)) && $fieldOptions['type'] != 'text') { throw new controller_exception('bad field'); } $item = $collection->load_only_id($id); if (!$item) { throw new controller_exception('No item'); }
/** * @return mail_tpl_collection */ function get_mail_collection() { if (!isset($this->_mail_collection)) { $this->_mail_collection = core::selfie()->model('mail_tpl'); } return $this->_mail_collection; }
/** * Get attached post */ function get_parent() { if ($this->pid && !isset($this->_parent)) { // try container parent $this->_parent = $this->get_container()->get_parent(); if (!isset($this->_parent)) { if ($this->_parent = array_get(static::$_parents_cache, $this->ctype_id . '.' . $this->pid)) { } else { $core = core::selfie(); $ctype = $core->get_ctype($this->ctype_id, false); /* * dd($core->get_ctype($this->ctype_id, false)->get_model()); */ // has-ctype if ($ctype) { $this->_parent = $core->model($core->get_ctype($this->ctype_id, false)->get_model())->load_only_id($this->pid); } array_set(static::$_parents_cache, $this->ctype_id . '.' . $this->pid, $this->_parent); } } } return $this->_parent; }
$_COOKIE['vidz0xoid'] = $_POST['_sid']; $_REQUEST['with_ajax'] = 1; } require '../modules/core/loader.php'; loader::bootstrap(array(loader::OPTION_CORE_PARAMS => array('editor' => true))); // disable cache functions::headers_no_cache(); /** @var tf_editor $editor */ $editor = core::lib('editor'); if (!core::lib('auth')->logged_in()) { core::dprint('Please login!'); $editor->on_exception('Not logged in'); functions::redirect('/editor/in/'); return; } $core = core::selfie(); /** @var user_item */ $user = core::lib('auth')->get_user(); $path = @$_GET['req']; // allow ng-redirect if (strpos($core->request->uri(), '/editor/redirect') === 0) { // die('ng-redirect'); $path = '/editor/core/redirect/'; } try { core::module('users')->check_forged(); } catch (controller_exception $e) { $editor->on_exception($e->getMessage(), $e); } // parse request path $editor->dispatch($path, core::get_params());
/** * INIT0 - call right after create an instance of core * create basic stuff * @throws core_exception */ public function init0() { if ($this->initialized) { throw new core_exception('Already initialized'); } $this->initialized = self::IS_LOADING; self::dprint(array("core::init0 %s", loader::with_composer() ? '+composer' : ''), self::E_DEBUG2); // templates setup self::register_lib('tpl_parser', function () { return tpl_loader::factory(core::selfie()->cfg('lib_tpl_parser')); }); // renderer self::register_lib('renderer', function () { return 0 ? new \SatCMS\Modules\Core\Base\ObjectMock() : new tf_renderer(core::selfie()->cfg('template'), core::lib('tpl_parser')); }); // database setup (database-`mysql`) $this->configure_database($this->cfg('database')); // set default timezone $tz = $this->cfg('default_timezone'); date_default_timezone_set($tz ? $tz : 'Europe/Moscow'); // load core config $this->dyn_config = $this->model('config', array('render_by_key' => true))->load()->merge_with($this->config); // content-types $ctype_config = loader::get_docs() . 'ctypes.cfg'; $ctype_array = fs::file_exists($ctype_config) ? parse_ini_file($ctype_config, true) : array(); $this->_ctypes = $this->get_ctype_handle(); $this->_ctypes->from_array($ctype_array); // add libs self::register_lib('logger', function () { return tf_logger::get_instance()->enable(!core::get_instance()->cfg('disable_logs', false)); }); self::register_lib('manager', new tf_manager()); self::register_lib('request', new tf_request()); $modules_config = array(); if ('file' == $this->cfg('modules_config', '') && ($modules_config_file = loader::get_docs() . 'modules.cfg') && fs::file_exists($modules_config_file)) { $modules_config = parse_ini_file($modules_config_file, true); } else { try { $modules_config = $this->module('modules', array('key' => 'tag'))->as_array(); } catch (module_exception $e) { // misconfigured modules, some of modules not exists throw new core_exception($e->getMessage(), tf_exception::CRITICAL); } } // site init %domain% // config/%domain%/init.php $site_config = array(); $site_config_path = $this->cfg('site_config'); if (!empty($site_config_path)) { $host = @$_SERVER['HTTP_HOST']; if ('%domain%' == $site_config_path) { $site_config_path = strpos($host, 'www.') === 0 ? substr($host, 4) : $host; } $mod_config_file = loader::get_docs() . $site_config_path . '/init.php'; if ($site_config_path && file_exists($mod_config_file)) { $site_config = (include $mod_config_file); } } // import module config `mod_{module}` // allow overrides modules.cfg foreach ($this->config as $cfg_key => $cfg) { if (strpos($cfg_key, 'mod_') === 0) { $cfg_key = substr($cfg_key, 4); $modules_config[$cfg_key] = @$modules_config[$cfg_key] ?: array(); $modules_config[$cfg_key] = functions::array_merge_recursive_distinct($modules_config[$cfg_key], $cfg); } } // module manager self::$modules = new core_modules($modules_config, $site_config); // finish core init0 proccess parent::init0(); // check bans if (!$this->cfg('no_bans_check') && isset($_SERVER['REQUEST_URI']) && ($_uri = $_SERVER['REQUEST_URI']) && !empty($_uri)) { if ($this->get_bans_handle()->check_spam($_uri)) { throw new core_exception(i18n::T('you_are_banned'), tf_exception::CRITICAL); } } self::register_lib('auth', new tf_auth(loader::in_shell()))->start_session(); if (self::in_editor()) { // editor kickstart $this->lib('editor'); } register_shutdown_function(array($this, 'halt')); $this->initialized = true; }
/** * Pre out */ private function output_begin($ext_config = array()) { // query current module for info $core = core::selfie(); $module = core::modules()->get_router(); $controller = $module->get_controller(); // lang constants {$lang._module.value} // @deprecated, use smarty i18n modifier $this->set_data('lang', $core->get_langwords()); foreach ($this->import_cfg as $key) { if (false !== ($test = $core->cfg($key))) { $cfg[$key] = $test; } } $cfg['template_url'] = $this->template_url; $cfg['url'] = $_SERVER['REQUEST_URI']; $cfg['title'] = $this->get_page_title(); $cfg['domain_url'] = $module->get_router()->get_protocol() . $_SERVER['HTTP_HOST']; $cfg['static_url'] = $core->get_static_url(); $cfg['debug'] = core::is_debug(); $cfg['version'] = core::$version; $cfg['in_ajax'] = loader::in_ajax(); $cfg['token'] = $core->auth->token(); if ($rc = $core->get_renderable_config()) { $cfg = array_merge($cfg, $rc); } // set default title if empty if (empty($cfg['title'])) { $cfg['title'] = ''; } $cfg['module'] = $module->get_name(); $cfg['section'] = $controller->get_section_name(); $cfg['action'] = $controller->get_action_name(); // ridiculous translate stuff @todo rethink $cfg['action_title'] = $controller->get_title(); // mix configs if (!empty($ext_config)) { $cfg = array_merge($cfg, $ext_config); } if (core::in_editor()) { $cfg['editor'] = $core->cfg('editor', array()); } // user $this->render_user(); // render modules $this->render_modules(); // this go to template $this->set_data('req', core::get_params()->as_array())->set_data('return', $this->return)->set_data('current', $this->current)->set_data('config', $cfg); }
/** * @return abs_collection */ function get_ctype_collection() { return core::selfie()->model($this->model); }