/** * Parse calendar * * @param AbstractController $controller * @param Application $application * @return void */ public static function parse(AbstractController $controller, Application $application) { if (!$_POST && $controller->hasView() && $controller instanceof \Phire\Content\Controller\IndexController) { $body = $controller->response()->getBody(); // Parse any calendar placeholders $calendars = []; $calendarIds = []; preg_match_all('/\\[\\{calendar.*\\}\\]/', $body, $calendars); if (isset($calendars[0]) && isset($calendars[0][0])) { foreach ($calendars[0] as $calendar) { $id = substr($calendar, strpos($calendar, '[{calendar_') + 11); if (strpos($id, '_') !== false) { $id = substr($id, 0, strpos($id, '_')); $replace = '[{calendar_' . $id . '_time}]'; $time = true; } else { $id = substr($id, 0, strpos($id, '}]')); $replace = '[{calendar_' . $id . '}]'; $time = false; } $calendarIds[] = ['id' => $id, 'replace' => $replace, 'time' => $time]; } } if (count($calendarIds) > 0) { $sess = $application->services()->get('session'); $roleId = isset($sess->user) ? $sess->user->role_id : null; foreach ($calendarIds as $cal) { $calendar = new Model\Calendar(['user_role_id' => $roleId, 'weekdays' => $application->module('phire-calendar')['weekdays'], 'range' => $application->module('phire-calendar')['range'], 'range_format' => $application->module('phire-calendar')['range_format'], 'day_format' => $application->module('phire-calendar')['day_format'], 'force_list' => $application->module('phire-calendar')['force_list'], 'force_list_mobile' => $application->module('phire-calendar')['force_list_mobile'], 'show_all' => $application->module('phire-calendar')['show_all'], 'date' => $controller->request()->getQuery('date')]); $rendered = $calendar->getById($cal['id'], $cal['time']); $body = str_replace($cal['replace'], $rendered, $body); } $controller->response()->setBody($body); } } }
/** * Set the tag template * * @param AbstractController $controller * @param Application $application * @return void */ public static function setTemplate(AbstractController $controller, Application $application) { if ($application->isRegistered('phire-templates') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView() && $controller->view()->isStream()) { if (null !== $controller->view()->tag_title) { $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag ' . $controller->view()->tag_title]); if (!isset($template->id)) { $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag']); } } else { $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag']); } if (isset($template->id)) { if (isset($template->id)) { $device = \Phire\Templates\Event\Template::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && $template->device != $device) { $childTemplate = \Phire\Templates\Table\Templates::findBy(['parent_id' => $template->id, 'device' => $device]); if (isset($childTemplate->id)) { $tmpl = $childTemplate->template; } else { $tmpl = $template->template; } } else { $tmpl = $template->template; } $controller->view()->setTemplate(\Phire\Templates\Event\Template::parse($tmpl)); } } } else { if ($application->isRegistered('phire-themes') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView() && $controller->view()->isFile()) { $theme = \Phire\Themes\Table\Themes::findBy(['active' => 1]); if (isset($theme->id)) { $template = null; $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/'; if (null !== $controller->view()->tag_slug) { $tagSlug = 'tag-' . str_replace('/', '-', $controller->view()->tag_slug); if (file_exists($themePath . $tagSlug . '.phtml') || file_exists($themePath . $tagSlug . '.php')) { $template = file_exists($themePath . $tagSlug . '.phtml') ? $tagSlug . '.phtml' : $tagSlug . '.php'; } else { if (file_exists($themePath . 'tag.phtml') || file_exists($themePath . 'tag.php')) { $template = file_exists($themePath . 'tag.phtml') ? 'tag.phtml' : 'tag.php'; } } } else { if (file_exists($themePath . 'tag.phtml') || file_exists($themePath . 'tag.php')) { $template = file_exists($themePath . 'tag.phtml') ? 'tag.phtml' : 'tag.php'; } } if (null !== $template) { $device = \Phire\Themes\Event\Theme::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && file_exists($themePath . $device . '/' . $template)) { $template = $device . '/' . $template; } $controller->view()->setTemplate($themePath . $template); } } } } }
/** * Set the search template * * @param AbstractController $controller * @param Application $application * @return void */ public static function setTemplate(AbstractController $controller, Application $application) { if ($application->isRegistered('phire-templates') && $controller instanceof \Phire\Search\Controller\IndexController && $controller->hasView()) { $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Search']); if (isset($template->id)) { if (isset($template->id)) { $device = \Phire\Templates\Event\Template::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && $template->device != $device) { $childTemplate = \Phire\Templates\Table\Templates::findBy(['parent_id' => $template->id, 'device' => $device]); if (isset($childTemplate->id)) { $tmpl = $childTemplate->template; } else { $tmpl = $template->template; } } else { $tmpl = $template->template; } $controller->view()->setTemplate(\Phire\Templates\Event\Template::parse($tmpl)); } } } else { if ($application->isRegistered('phire-themes') && $controller instanceof \Phire\Search\Controller\IndexController && $controller->hasView()) { $theme = \Phire\Themes\Table\Themes::findBy(['active' => 1]); if (isset($theme->id)) { $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/'; if (file_exists($themePath . 'search.phtml') || file_exists($themePath . 'search.php')) { $template = file_exists($themePath . 'search.phtml') ? 'search.phtml' : 'search.php'; $device = \Phire\Themes\Event\Theme::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && file_exists($themePath . $device . '/' . $template)) { $template = $device . '/' . $template; } $controller->view()->setTemplate($themePath . $template); } } } } }
/** * Get all category values for the form object * * @param AbstractController $controller * @param Application $application * @return void */ public static function parseCategories(AbstractController $controller, Application $application) { if ($controller->hasView() && ($controller instanceof \Phire\Categories\Controller\IndexController || $controller instanceof \Phire\Content\Controller\IndexController)) { $body = $controller->response()->getBody(); $category = new Model\Category(); $category->show_total = $application->module('phire-categories')['show_total']; $category->filters = $application->module('phire-categories')['filters']; $category->datetime_formats = $application->module('phire-categories')['datetime_formats']; $catIds = self::parseCategoryIds($body); $catParentIds = self::parseParentCategoryIds($body); if (count($catIds) > 0) { foreach ($catIds as $key => $value) { $category->getById($value['id']); $categoryName = 'category_' . $value['id']; if (isset($value['limit']) && $value['limit'] > 0 && $category->hasPages($value['limit'])) { $limit = $value['limit']; $pages = null; } else { if ($category->pagination > 0 && $category->hasPages($category->pagination)) { $limit = $category->pagination; $pages = new \Pop\Paginator\Paginator($category->getCount(), $limit); $pages->useInput(true); } else { $limit = null; $pages = null; } } if (null !== $pages) { $controller->view()->pages = $pages; } $controller->view()->{$categoryName} = $category->getItems($limit, $controller->request()->getQuery('page')); } } if (count($catParentIds) > 0) { foreach ($catParentIds as $key => $value) { if (isset($value['limit']) && $value['limit'] > 0) { $limit = $value['limit']; $categoryName = 'categories_' . $value['id'] . '_' . $limit; } else { $limit = null; $categoryName = 'categories_' . $value['id']; } $controller->view()->{$categoryName} = $category->getCategoryChildren($value['id'], $limit); } } $controller->view()->setTemplate($body); $body = $controller->view()->render(); $controller->response()->setBody($body); } }
/** * Set the template for the content * * @param AbstractController $controller * @param Application $application * @return void */ public static function setTemplate(AbstractController $controller, Application $application) { $template = null; if ($application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView()) { if (is_numeric($controller->getTemplate())) { if ($controller->getTemplate() == -1) { $template = Table\Templates::findBy(['name' => 'Error']); } else { if ($controller->getTemplate() == -2) { $template = Table\Templates::findBy(['name' => 'Date']); } else { $template = Table\Templates::findById((int) $controller->getTemplate()); } } } } if (null !== $template && isset($template->id)) { if (isset($template->id)) { $device = self::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && $template->device != $device) { $childTemplate = Table\Templates::findBy(['parent_id' => $template->id, 'device' => $device]); if (isset($childTemplate->id)) { $tmpl = $childTemplate->template; } else { $tmpl = $template->template; } } else { $tmpl = $template->template; } $controller->view()->setTemplate(self::parse($tmpl)); } } }
/** * Set the template for the content * * @param AbstractController $controller * @param Application $application * @return void */ public static function setTemplate(AbstractController $controller, Application $application) { $template = null; $themePath = null; $parentThemePath = null; $realThemePath = null; $theme = Table\Themes::findBy(['active' => 1]); if (isset($theme->id)) { $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/'; if (null !== $theme->parent_id) { $parentTheme = Table\Themes::findById($theme->parent_id); if (isset($parentTheme->id)) { $parentThemePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $parentTheme->folder . '/'; } } } if ($application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView()) { if (null !== $controller->getTemplate()) { if (isset($theme->id)) { $controller->view()->themePath = $themePath; $controller->view()->parentThemePath = $parentThemePath; if ($controller->getTemplate() == -1 && (file_exists($themePath . 'error.phtml') || file_exists($themePath . 'error.php'))) { $template = file_exists($themePath . 'error.phtml') ? 'error.phtml' : 'error.php'; } else { if ($controller->getTemplate() == -2 && (file_exists($themePath . 'date.phtml') || file_exists($themePath . 'date.php'))) { $template = file_exists($themePath . 'date.phtml') ? 'date.phtml' : 'date.php'; } else { if (file_exists($themePath . $controller->getTemplate())) { $template = $controller->getTemplate(); } } } $realThemePath = $themePath; if (null === $template && null !== $parentThemePath) { if ($controller->getTemplate() == -1 && (file_exists($parentThemePath . 'error.phtml') || file_exists($parentThemePath . 'error.php'))) { $template = file_exists($parentThemePath . 'error.phtml') ? 'error.phtml' : 'error.php'; } else { if ($controller->getTemplate() == -2 && (file_exists($parentThemePath . 'date.phtml') || file_exists($parentThemePath . 'date.php'))) { $template = file_exists($parentThemePath . 'date.phtml') ? 'date.phtml' : 'date.php'; } else { if (file_exists($parentThemePath . $controller->getTemplate())) { $template = $controller->getTemplate(); } } } $realThemePath = $parentThemePath; } if (null !== $template && null !== $realThemePath) { $device = self::getDevice($controller->request()->getQuery('mobile')); if (null !== $device && file_exists($realThemePath . $device . '/' . $template)) { $template = $device . '/' . $template; } $controller->view()->setTemplate($realThemePath . $template); } } } } }
/** * Login and track session * * @param AbstractController $controller * @param Application $application * @return void */ public static function login(AbstractController $controller, Application $application) { $sess = $application->getService('session'); $userUri = APP_URI; $key = 'user'; if (isset($sess->member) && $application->isRegistered('phire-members')) { $key = 'member'; $memberAdmin = new \Phire\Members\Model\MembersAdmin(); $memberAdmin->getByRoleId($sess->member->role_id); if (isset($memberAdmin->uri)) { $userUri = $memberAdmin->uri; } } $path = BASE_PATH . $userUri; if ($path == '') { $path = '/'; } $cookie = Cookie::getInstance(['path' => $path]); $cookie->delete('phire_session_timeout'); $cookie->delete('phire_session_path'); // If login, validate and start new session if ($controller->request()->isPost() && substr($controller->request()->getRequestUri(), -6) == '/login') { // If the user successfully logged in if (isset($sess[$key])) { $config = Table\UserSessionConfig::findById($sess[$key]->role_id); $data = Table\UserSessionData::findById($sess[$key]->id); if (isset($config->role_id)) { if (!self::validate($config, $sess[$key], $data)) { if (isset($data->user_id)) { $data->failed_attempts++; $data->save(); } else { $data = new Table\UserSessionData(['user_id' => $sess[$key]->id, 'logins' => null, 'failed_attempts' => 1]); $data->save(); } if (isset($config->role_id) && (int) $config->log_type > 0 && null !== $config->log_emails) { self::log($config, $sess[$key], false); } $sess->kill(); Response::redirect(BASE_PATH . $userUri . '/login?failed=' . $data->failed_attempts); exit; } else { if (isset($data->user_id)) { $limit = (int) $application->module('phire-sessions')['login_limit']; $logins = unserialize($data->logins); if ($limit > 0 && count($logins) >= $limit) { reset($logins); unset($logins[key($logins)]); } $logins[time()] = ['ua' => $_SERVER['HTTP_USER_AGENT'], 'ip' => $_SERVER['REMOTE_ADDR']]; $data->total_logins++; $data->failed_attempts = 0; $data->logins = serialize($logins); $data->save(); } else { $data = new Table\UserSessionData(['user_id' => $sess[$key]->id, 'logins' => serialize([time() => ['ua' => $_SERVER['HTTP_USER_AGENT'], 'ip' => $_SERVER['REMOTE_ADDR']]]), 'total_logins' => 1, 'failed_attempts' => 0]); $data->save(); } } $expire = (int) $config->session_expiration > 0 ? (int) $config->session_expiration : null; $timeout = (int) $config->timeout_warning; } else { $expire = null; $timeout = false; } $lastLogin = null; $lastIp = null; // Check for the last login $data = Table\UserSessionData::findById($sess[$key]->id); if (isset($data->user_id)) { $logins = null !== $data->logins ? unserialize($data->logins) : []; if (count($logins) > 1) { $keys = array_keys($logins); $timestamp = isset($keys[count($keys) - 2]) ? $keys[count($keys) - 2] : null; if (null !== $timestamp && isset($logins[$timestamp])) { $lastLogin = $timestamp; $lastIp = $logins[$timestamp]['ip']; } } } // Clear old sessions $clear = (int) $application->module('phire-sessions')['clear_sessions']; if ($clear > 0) { $clear = time() - $clear; $sql = Table\UserSessions::sql(); $sql->delete()->where(['start <= :start']); Table\UserSessions::execute((string) $sql, ['start' => $clear]); } $session = new Table\UserSessions(['user_id' => $sess[$key]->id, 'ip' => $_SERVER['REMOTE_ADDR'], 'ua' => $_SERVER['HTTP_USER_AGENT'], 'start' => time()]); $session->save(); $sess[$key]->session = new \ArrayObject(['id' => $session->id, 'start' => $session->start, 'last' => $session->start, 'expire' => $expire, 'timeout' => $timeout, 'last_login' => $lastLogin, 'last_ip' => $lastIp], \ArrayObject::ARRAY_AS_PROPS); if (isset($config->role_id) && (int) $config->log_type > 0 && null !== $config->log_emails) { self::log($config, $sess[$key], true); } // Else, if the user login failed } else { if (null !== $controller->view()->form && $controller->view()->form !== false && null !== $controller->view()->form->username) { $user = \Phire\Table\Users::findBy(['username' => $controller->view()->form->username]); $config = Table\UserSessionConfig::findById($user->role_id); if (isset($user->id)) { $data = Table\UserSessionData::findById($user->id); if (isset($data->user_id)) { $data->failed_attempts++; $data->save(); } else { $data = new Table\UserSessionData(['user_id' => $user->id, 'logins' => null, 'failed_attempts' => 1]); $data->save(); } if (isset($config->role_id) && (int) $config->log_type > 0 && null !== $config->log_emails) { self::log($config, $user, false); } } } } // Check existing session } else { if (isset($sess[$key]) && isset($sess[$key]->session)) { if (!isset(Table\UserSessions::findById((int) $sess[$key]->session->id)->id) || null !== $sess[$key]->session->expire && time() - $sess[$key]->session->last >= $sess[$key]->session->expire) { $session = Table\UserSessions::findById((int) $sess[$key]->session->id); if (isset($session->id)) { $session->delete(); } $sess->kill(); Response::redirect(BASE_PATH . $userUri . '/login?expired=1'); exit; } else { if ($sess[$key]->session->timeout && null !== $sess[$key]->session->expire) { $cookie->set('phire_session_timeout', $sess[$key]->session->expire - 30); $cookie->set('phire_session_path', BASE_PATH . $userUri); } $sess[$key]->session->last = time(); } } } }