function logout() { $session = Base::getSession(); $referer = $session->get('_referer'); $result = $session->restart(); $session->set('_referer', $referer); self::set_public_user(); return $result; }
public static function getSystemMessages() { $session = Base::getSession(); $types = $session->getFlash(); return \GCore\Helpers\Message::render($types); }
public static function authorized($path, $action = 'access', $owner_id = null, $user_id = null) { $user = Base::getUser(); //owner admin access if ((int) $user['id'] === 1) { return true; } //login/logout can be always accessed if (($path == '\\GCore\\Admin\\Controllers\\Users' or $path == '\\GCore\\Controllers\\Users') and ($action == 'login' or $action == 'logout')) { return true; } $groups = Authenticate::get_user_groups($user_id); if (!empty($owner_id) and $owner_id == $user['id']) { $groups[] = 'owner'; } $return = false; //build search branches based on current loaded class if (is_array($path)) { $tests = $path; } else { $branches = explode("\\", $path); $tests = array(); for ($i = 0; $i = count($branches) - 1; $i++) { $tests[] = implode("\\", $branches); array_pop($branches); } } //check cache $cache = (bool) Base::getConfig('cache_permissions'); if ($cache === true) { $session = Base::getSession(); $cached_permissions = $session->get('acos_permissions.' . $user['id'], array()); if (in_array('owner', $groups)) { $cache_key = md5(serialize($tests) . $action . $owner_id); } else { $cache_key = md5(serialize($tests) . $action); } if (array_key_exists($cache_key, $cached_permissions)) { $return = $cached_permissions[$cache_key]; goto end; } } $paths_key = md5(serialize($tests)); if (!isset(self::$lookups[$paths_key])) { $Acl_model = new \GCore\Admin\Models\Acl(); $acls = $Acl_model->find('all', array('conditions' => array('aco' => $tests, 'enabled' => 1), 'order' => array('aco DESC'))); self::$lookups[$paths_key] = $acls; } else { $acls = self::$lookups[$paths_key]; } if (empty($acls)) { //no ACL results found matching this ACO $return = false; goto end; } foreach ($acls as $k => $acl) { $p_action = $action; if (!empty($acl['Acl']['rules'][$p_action])) { //main action rules found, goto permissions check } elseif (!empty($acl['Acl']['rules']['access'])) { //main action not found, but access action found, let's use it $p_action = 'access'; } else { //neither the main action nor the default one found under this path, or maybe no permissions set, go to the next one. continue; } //check groups action's rules $result = self::check_rules($acl['Acl']['rules'][$p_action], $groups); if (!is_null($result)) { $return = $result; goto end; } //looks like all permissions in this path are not set or inheriting, go to next path continue; } //we looped all pathes with no matches, return denied $return = false; end: //store into cache if ($cache === true) { $session = Base::getSession(); $cached_permissions = $session->get('acos_permissions.' . $user['id'], array()); if (in_array('owner', $groups)) { $cache_key = md5(serialize($tests) . $action . $owner_id); } else { $cache_key = md5(serialize($tests) . $action); } $cached_permissions[$cache_key] = $return; $session->set('acos_permissions.' . $user['id'], $cached_permissions); } else { $session = Base::getSession(); $session->set('acos_permissions.' . $user['id'], array()); } return $return; }
function _paginate() { $model_class = !empty($this->paginate_model) ? $this->paginate_model : null; if (empty($model_class)) { $alias = $this->get_main_model(); if (!empty($alias)) { $model_class = $this->{$alias}; } else { return; } } $prefix = ''; if (!empty($this->paginate_prefix)) { $prefix = '.' . $this->paginate_prefix; } //check if we should process pagination if (!empty($model_class) and in_array('\\GCore\\Helpers\\Paginator', $this->helpers) or in_array('\\GCore\\Helpers\\Paginator', array_keys($this->helpers))) { $session = Base::getSession(); $k = array_search('\\GCore\\Helpers\\Paginator', $this->helpers); if ($k !== false) { unset($this->helpers[$k]); } $page = Request::data(\GCore\Libs\Base::getConfig('page_url_param_name', 'page'), $session->get(get_class($this) . $prefix . '.' . $model_class->alias . '.page', 1)); $page = $page < 1 ? 1 : $page; $active_limit = !empty($model_class->page_limit) ? $model_class->page_limit : Base::getConfig('list_limit', 30); $limit = Request::data('limit', $session->get(get_class($this) . $prefix . '.' . $model_class->alias . '.limit', $active_limit)); if ($limit == 0 or $limit > Base::getConfig('max_list_limit', 1000)) { $limit = Base::getConfig('max_list_limit', 1000); } if (!empty($model_class->limit)) { $limit = $model_class->limit; } if (!empty($model_class->page)) { $page = $model_class->page; } $offset = ($page - 1) * (int) $limit; $total = !empty($this->paginate_total) ? $this->paginate_total : $model_class->find('count', array('cache' => true)); $bad_page = false; if ($offset >= $total) { //$page = ceil($total/$limit); $bad_page = true; $page = ceil($total / $limit); $offset = $limit * ($page - 1); } $page = $page < 1 ? 1 : $page; $offset = $offset < 0 ? 0 : $offset; $this->helpers['\\GCore\\Helpers\\Paginator']['limit'] = $limit; $this->helpers['\\GCore\\Helpers\\Paginator']['page'] = $page; $this->helpers['\\GCore\\Helpers\\Paginator']['offset'] = $offset; $this->helpers['\\GCore\\Helpers\\Paginator']['page_param'] = \GCore\Libs\Base::getConfig('page_url_param_name', 'page'); if (!$bad_page) { $session->set(get_class($this) . $prefix . '.' . $model_class->alias . '.page', $page); } $session->set(get_class($this) . $prefix . '.' . $model_class->alias . '.limit', $limit); $this->helpers['\\GCore\\Helpers\\Paginator']['total'] = $total; //page (limit and offset) should be set after the count query $model_class->page = $page; $model_class->page_limit = $limit; } }
public static function send($to = array(), $subject = '', $body = '', $attachments = array(), $other = array()) { if (!class_exists('PHPMailer')) { require_once \GCore\C::get('GCORE_FRONT_PATH') . 'vendors' . DS . 'phpmailer' . DS . 'PHPMailerAutoload.php'; } $mail = new \PHPMailer(); $mail->CharSet = 'utf-8'; //get recipients foreach ((array) $to as $address) { $mail->AddAddress(trim($address)); } //subject $mail->Subject = $subject; //reply to $reply_name = !empty($other['reply_name']) ? $other['reply_name'] : Base::getConfig('mail_reply_name'); $reply_email = !empty($other['reply_email']) ? $other['reply_email'] : Base::getConfig('mail_reply_email'); if (!empty($reply_name) and !empty($reply_email)) { $mail->AddReplyTo($reply_email, $reply_name); } //from $from_name = !empty($other['from_name']) ? $other['from_name'] : Base::getConfig('mail_from_name'); $from_email = !empty($other['from_email']) ? $other['from_email'] : Base::getConfig('mail_from_email'); $mail->SetFrom($from_email, $from_name); //set custom headers if (!empty($other['custom'])) { foreach ($other['custom'] as $k => $v) { $mail->addCustomHeader($k . ': ' . $v); } } //set CC and BCC if (!empty($other['cc'])) { foreach ($other['cc'] as $k => $cc) { $mail->AddCC($cc); } } if (!empty($other['bcc'])) { foreach ($other['bcc'] as $k => $bcc) { $mail->AddBCC($bcc); } } if ((bool) Base::getConfig('smtp', 0) === true or Base::getConfig('mail_method', 'phpmail') == 'smtp') { $mail->IsSMTP(); if (Base::getConfig('smtp_username') and Base::getConfig('smtp_password')) { $mail->SMTPAuth = true; } if (Base::getConfig('smtp_secure')) { $mail->SMTPSecure = Base::getConfig('smtp_secure'); } $mail->Host = Base::getConfig('smtp_host'); $mail->Port = Base::getConfig('smtp_port'); $mail->Username = Base::getConfig('smtp_username'); $mail->Password = Base::getConfig('smtp_password'); } else { if (Base::getConfig('mail_method', 'phpmail') == 'sendmail') { $mail->IsSendmail(); } } if (!isset($other['type']) or $other['type'] == 'html') { $mail->AltBody = strip_tags($body); //'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically //$body = nl2br($body); //$mail->MsgHTML($body); $mail->Body = $body; $mail->IsHTML(true); } else { $mail->Body = $body; $mail->IsHTML(false); } $mail->SMTPDebug = (int) Base::getConfig('smtp_debug', 0); //attachments foreach ((array) $attachments as $attachment) { if (is_array($attachment) and !empty($attachment['path'])) { $attachment = array_merge(array('name' => basename($attachment['path']), 'type' => 'application/octet-stream', 'encoding' => 'base64'), $attachment); $mail->AddAttachment($attachment['path'], $attachment['name'], $attachment['encoding'], $attachment['type']); } else { $mail->AddAttachment($attachment); } } if (!$mail->Send()) { $session = Base::getSession(); $session->setFlash('warning', 'Mailer Error: ' . $mail->ErrorInfo); return false; } return true; }
function dispatch($content_only = false, $check_perm = true) { Event::trigger('on_before_dispatch', $this); $session = Base::getSession(); reset: //if no action set, set it to index if (strlen(trim($this->action)) == 0) { $this->action = 'index'; } //set admin path $site = ''; if ($this->site == 'admin') { $site = '\\Admin'; } //load the extension class $controller = !empty($this->controller) ? '\\Controllers\\' . Str::camilize($this->controller) : '\\' . Str::camilize($this->extension); $extension = !empty($this->extension) ? '\\Extensions\\' . Str::camilize($this->extension) : ''; $classname = '\\GCore' . $site . $extension . $controller; $this->tvout = strlen(Request::data('tvout', null)) > 0 ? Request::data('tvout') : $this->tvout; //set referer if (!$content_only) { if (!($this->controller == 'users' and ($this->action == 'login' or $this->action == 'logout' or $this->action == 'register')) and (!empty($this->extension) or !empty($this->controller)) and $this->tvout == 'index') { $session->set('_referer', Url::current()); } else { //$session->set('_referer', 'index.php'); } } //check permissions if ($check_perm and !Authorize::authorized($classname, $this->action)) { if ($content_only) { return; } $this->redirect(r_('index.php?cont=users&act=login')); } //if the extension class not found or the action function not found then load an error if (!class_exists($classname) or !in_array($this->action, get_class_methods($classname)) and !in_array('__call', get_class_methods($classname)) or substr($this->action, 0, 1) == '_') { $this->controller = 'errors'; $this->action = 'e404'; //reset the controller $classname = '\\GCore\\Controllers\\Errors'; \GCore\Libs\Env::e404(); //we need the rendered content only if ($content_only) { return; } } //load language file if (!empty($extension)) { Lang::load($site . $extension); } //set theme $doc = Document::getInstance($this->site, $this->thread); $doc->theme = 'bootstrap3'; //'gcoreui';//'semantic1'; $theme = \GCore\Helpers\Theme::getInstance(); // in gcore app, bootstrap should be always loaded first with jquery //load class and run the action ${$classname} = new $classname($this->site, $this->thread); ob_start(); $continue = ${$classname}->_initialize(); //check and read cache if (!empty(${$classname}->cache)) { if (!is_array(${$classname}->cache)) { ${$classname}->cache = array(); } if (empty(${$classname}->cache['time'])) { ${$classname}->cache['time'] = Base::getConfig('app_cache_expiry', 900); } if (empty(${$classname}->cache['title'])) { ${$classname}->cache['title'] = File::makeSafe($classname . '_' . $this->action); } else { ${$classname}->cache['title'] = File::makeSafe(${$classname}->cache['title']); } if (empty(${$classname}->cache['key'])) { ${$classname}->cache['key'] = 'cached_view'; } else { ${$classname}->cache['key'] = 'cached_view_' . ${$classname}->cache['key']; } $cache = Cache::getInstance(${$classname}->cache['title'], array('expiration' => ${$classname}->cache['time'])); $cached_view = $cache->get(${$classname}->cache['key']); $cached = false; if (!empty($cached_view)) { $cached = true; $continue = false; echo $cached_view; } } if ($continue !== false) { ${$classname}->{$this->action}(); if ($this->reset === true) { $this->reset = false; goto reset; } //initialize and render view $view = new View(); $view->initialize(${$classname}); $view->renderView($this->action); } //get the action output buffer $this->buffer = ob_get_clean(); //check and save cache if (!empty(${$classname}->cache) and !$cached) { $cache = Cache::getInstance(${$classname}->cache['title'], array('expiration' => ${$classname}->cache['time'])); $cache->set(${$classname}->cache['key'], $this->buffer); } //finalize ob_start(); ${$classname}->_finalize(); $this->buffer .= ob_get_clean(); //now load the theme files //$theme = \GCore\Helpers\Theme::getInstance(); if ($this->tvout != 'ajax' and $doc->theme == 'bootstrap3') { $this->buffer = '<div class="gbs3">' . $this->buffer . '</div>'; } Event::trigger('on_after_dispatch'); }