public static function load($path = '') { if (!empty($path) and !in_array($path, self::$loaded)) { $path = !empty($path) ? '\\' . trim($path, '\\') : ''; $lang_class = '\\GCore' . $path . '\\Locales\\EnGb\\Lang'; $cutsom_lang = '\\GCore' . $path . '\\Locales\\' . Str::camilize(str_replace('-', '_', strtolower(Base::getConfig('site_language', 'en-gb')))) . '\\Lang'; if (class_exists($cutsom_lang)) { if (class_exists($lang_class)) { //load default language as well $lang_class_loaded = new \ReflectionClass($lang_class); self::$translations = array_merge((array) self::$translations, $lang_class_loaded->getConstants(), $lang_class_loaded->getStaticProperties()); self::$loaded[] = $path; } $lang_class = $cutsom_lang; } if (!class_exists($lang_class)) { return false; } $lang_class_loaded = new \ReflectionClass($lang_class); self::$translations = array_merge((array) self::$translations, $lang_class_loaded->getConstants(), $lang_class_loaded->getStaticProperties()); self::$loaded[] = $path; return true; } return false; }
function deleteAll($conditions, $params = array()) { if (!empty($this->cache) and Base::getConfig('cache') >= 1 and Base::getConfig('cache_query') >= 1) { $cache = Cache::getInstance($this->tablename . '.queries'); $cache->destroy(); } return parent::deleteAll($conditions, $params); }
public static function getInstance($domain = 'gcore', $params = array(), $engine = 'file') { $domain = !empty($domain) ? $domain : 'gcore'; $engine = func_num_args() > 2 ? $engine : Base::getConfig('cache_engine'); if (empty(self::$instances[$engine][$domain])) { $class = '\\GCore\\Libs\\CacheEngines\\' . Str::camilize($engine); self::$instances[$engine][$domain] = new $class($domain, $params); } return self::$instances[$engine][$domain]; }
public static function getInstance($options = array()) { if (!empty($options)) { $db_adapter_class = '\\GCore\\Libs\\DatabaseAdapters\\' . Str::camilize(Base::getConfig('db_adapter', 'pdo')); $new_object = new $db_adapter_class($options); return $new_object; } else { return false; } }
public static function _setOptions($options = array()) { if (empty($options)) { $options['user'] = Base::getConfig('db_user'); $options['pass'] = Base::getConfig('db_pass'); $options['name'] = Base::getConfig('db_name'); $options['host'] = Base::getConfig('db_host'); $options['type'] = Base::getConfig('db_type'); $options['prefix'] = Base::getConfig('db_prefix'); } return $options; }
public static function getInstance($options = array()) { if (!empty($options)) { $db_adapter_class = '\\GCore\\Libs\\DatabaseAdapters\\' . Str::camilize(Base::getConfig('db_adapter', 'pdo')); $new_object = new $db_adapter_class($options); $new_object->_initialize($options); //$db_processor_class = '\GCore\Libs\DatabaseProcessors\\'.Str::camilize($new_object->db_type); //$new_object->processor = new $db_processor_class(); return $new_object; } else { return false; } }
public static function login($credentials) { $session = Base::getSession(); $username_field = Base::getConfig('username_field', 'username'); if (isset($credentials[$username_field]) and isset($credentials['password'])) { $user_model = new \GCore\Admin\Models\User(); $user = $user_model->find('first', array('conditions' => array($username_field => $credentials[$username_field]))); if (!empty($user)) { $user_groups = Arr::getVal($user, array('GroupUser', '[n]', 'group_id'), self::get_public_groups()); $user_groups_paths = Arr::getVal($user, array('Group', '[n]', 'path'), array()); $user_inheritance = array(); foreach ($user_groups_paths as $user_groups_path) { $user_inheritance = array_merge($user_inheritance, array_filter(explode('.', $user_groups_path))); } $user_inheritance = array_unique($user_inheritance); $user = $user['User']; $password_correct = self::check_password($credentials['password'], $user['password']); if (!$password_correct) { $session->setFlash('error', l_('AUTHENTICATE_INCORRECT_LOGIN_CREDENTIALS')); return false; } if (!empty($user['activation'])) { $session->setFlash('error', l_('AUTHENTICATE_ACCOUNT_NOT_ACTIVATED')); return false; } if ($user['blocked'] == 1) { $session->setFlash('error', l_('AUTHENTICATE_ACCOUNT_BLOCKED')); return false; } //account is found and can login, insert session data $user_session = array(); $user_session['id'] = $user['id']; $user_session['name'] = $user['name']; $user_session['username'] = $user['username']; $user_session['email'] = $user['email']; $user_session['last_login'] = $user['last_visit']; $user_session['logged_in'] = 1; $user_session['groups'] = $user_groups; $user_session['inheritance'] = $user_inheritance; //get referer $referer = $session->get('_referer'); $session->restart(); $session->set('_referer', $referer); $session->set('user', array_merge($session->get('user', array()), $user_session)); if (Base::getConfig('session_handler', 'php') == 'database') { $session_model = new \GCore\Admin\Models\Session(); //$update = $session_model->updateAll(array('user_id' => $user['id'], 'site' => GCORE_SITE), array('session_id' => $session->get_id())); $insert_status = $session_model->save(array('session_id' => $session->get_id(), 'user_id' => $user['id'], 'site' => GCORE_SITE, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'last_activity' => time()), array('new' => true)); } //update last visit $user_model->updateAll(array('last_visit' => date('Y-m-d H:i:s', time())), array('id' => $user['id']), array('modified' => false)); //after login hook $hook_results = Event::trigger('on_after_user_login'); return true; } else { $session->setFlash('error', l_('AUTHENTICATE_INCORRECT_LOGIN_CREDENTIALS')); return false; } } else { return false; } }
protected function _destroy() { session_destroy(); $return = !session_id(); if ($return and isset($_COOKIE[session_name()])) { //remove any session presence in the cookies setcookie(session_name(), '', 0, Base::getConfig('cookie_path', ''), Base::getConfig('cookie_domain', '')); } return $return; }
function loadFields() { $cached = false; if (Base::getConfig('cache') >= 1 and Base::getConfig('cache_dbinfo') >= 1) { $cache = Cache::getInstance('db_tables_info', array('expiration' => Base::getConfig('dbinfo_cache_expiry', 43200)), 'file'); $this->table_fields = $cache->get($this->tablename . '.columns'); if (!empty($this->table_fields)) { $cached = true; } } if (empty($this->table_fields)) { $this->table_fields = $this->dbo->getTableColumns($this->tablename); } if (!$cached and Base::getConfig('cache') >= 1 and Base::getConfig('cache_dbinfo') >= 1) { $cache = Cache::getInstance('db_tables_info', array('expiration' => Base::getConfig('dbinfo_cache_expiry', 43200)), 'file'); $cache->set($this->tablename . '.columns', $this->table_fields); } }
function getTitle() { if (Base::getConfig('prepend_site_title', 1) == 1) { array_unshift($this->title, Base::getConfig('site_title')); } return '<title>' . implode(' - ', array_filter($this->title)) . '</title>'; }
public static function page($alias = '') { $page = \GCore\Controllers\Pages::_find($alias); if (!empty($page)) { Request::set('_Route.page', $page['Page']); //set page title if ((bool) Base::getConfig('append_page_title', 1) === true) { $doc = Document::getInstance(); $doc->setTitle($page['Page']['title']); } //pass more page params Request::set('_Route.index', $page['Page']['id']); if ($page['Page']['default'] == 1) { Request::set('_Route.default', 1); } $params = array('extension' => $page['Page']['extension'], 'controller' => $page['Page']['controller'], 'action' => $page['Page']['action'], 'params' => $page['Page']['params']); if (!empty($page['Page']['params'])) { foreach ($page['Page']['params'] as $k => $v) { $params[$k] = $v; } } return $params; } return array(); }
function output() { echo $this->buffer; if (Base::getConfig('debug', 0)) { \GCore\Loader::debug(); } }
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; }