/** * Log a message to the logger * * @param string $message The message to log * @param string $category[optional] The message category (default "main") * @param integer $level[optional] The loglevel */ public static function log($message, $category = 'main', $level = 1) { Caspar::ping(); if (!self::$_logging_enabled) { return false; } if (self::$_loglevel > $level) { return false; } if (self::$_cli_log_to_screen_in_debug_mode && Caspar::isCLI() && Caspar::isDebugMode() && class_exists('TBGCliCommand')) { TBGCliCommand::cli_echo(mb_strtoupper(self::getLevelName($level)), 'white', 'bold'); TBGCliCommand::cli_echo(" [{$category}] ", 'green', 'bold'); TBGCliCommand::cli_echo("{$message}\n"); } if (self::$_logonajaxcalls || !(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] == '')) { if (self::$_logfile !== null) { file_put_contents(self::$_logfile, mb_strtoupper(self::getLevelName($level)) . " [{$category}] {$message}\n", FILE_APPEND); } $time_msg = ($load_time = Caspar::getLoadtime()) >= 1 ? round($load_time, 2) . ' seconds' : round($load_time * 1000, 3) . ' ms'; self::$_entries[] = array('category' => $category, 'time' => $time_msg, 'message' => $message, 'level' => $level); self::$_categorized_entries[$category][] = array('time' => $time_msg, 'message' => $message, 'level' => $level); } }
/** * Sanitize a string * * @param string $string The string to sanitize * * @return string the sanitized string */ protected function __sanitize_string($string) { try { $charset = class_exists('Caspar') ? Caspar::getI18n()->getCharset() : 'utf-8'; } catch (Exception $e) { $charset = 'utf-8'; } return htmlspecialchars($string, ENT_QUOTES, $charset); }
public function hasTranslatedTemplate($template, $is_component = false) { if (mb_strpos($template, '/')) { $templateinfo = explode('/', $template); $module = $templateinfo[0]; $templatefile = $is_component ? '_' . $templateinfo[1] . '.inc.php' : $templateinfo[1] . '.' . Caspar::getRequest()->getRequestedFormat() . '.php'; } else { $module = Caspar::getRouting()->getCurrentRouteModule(); $templatefile = $is_component ? '_' . $template . '.inc.php' : $template . '.' . Caspar::getRequest()->getRequestedFormat() . '.php'; } if (file_exists(CASPAR_MODULES_PATH . $module . DS . 'i18n' . DS . $this->_language . DS . 'templates' . DS . $templatefile)) { return CASPAR_MODULES_PATH . $module . DS . 'i18n' . DS . $this->_language . DS . 'templates' . DS . $templatefile; } elseif (file_exists(CASPAR_CORE_PATH . 'i18n' . DS . $this->getCurrentLanguage() . DS . 'templates' . DS . $module . DS . $templatefile)) { return CASPAR_CORE_PATH . 'i18n' . DS . $this->getCurrentLanguage() . DS . 'templates' . DS . $module . DS . $templatefile; } return false; }
/** * Deletes a cookie on the client * * @param $key string the cookie key to delete * * @return bool */ public function deleteCookie($key, $base_path = null, $domain = null) { $domain = $domain !== null ? $domain : '.' . Caspar::getDefaultCookieDomain(); $this->setCookie($key, '', NOW - 36000, $base_path, $domain); return true; }
/** * Forward the user with HTTP status code 403 and an (optional) message * based on a boolean check * * @param boolean $condition * @param string $message[optional] The message */ public function forward403unless($condition, $message = null) { if (!$condition) { $message = $message === null ? Caspar::getI18n()->__("You are not allowed to access to this page") : $message; $this->getResponse()->setHttpStatus(403); $this->message = $message; $this->getResponse()->setTemplate('main/forbidden'); } }
/** * Take a raw password and convert it to the hashed format * * @param string $password * * @return hashed password */ public static function hashPassword($password, $salt = null) { $salt = $salt !== null ? $salt : Caspar::getSalt(); return crypt($password, '$2a$07$' . $salt . '$'); }
/** * Generate a url based on a route * * @param string $name The route key * @param array $params key=>value pairs of route parameters * @param boolean $relative Whether to generate an url relative to web root or an absolute * * @return string */ public function generate($name, $params = array(), $relative = true, $querydiv = '/', $divider = '/', $equals = '/') { if (mb_substr($name, 0, 1) == '@') { $name = mb_substr($name, 1); $details = explode('?', $name); $name = array_shift($details); if (count($details)) { $param_details = array_shift($details); $param_details = explode('&', $param_details); foreach ($param_details as $detail) { $param_detail = explode('=', $detail); if (count($param_detail) > 1) { $params[$param_detail[0]] = $param_detail[1]; } } } } if (!isset($this->routes[$name])) { Logging::log("The route '{$name}' does not exist", 'routing', Logging::LEVEL_FATAL); throw new \Exception("The route '{$name}' does not exist"); } list($url, $regexp, $names, $names_hash, $action, $module, $defaults, $csrf_enabled) = $this->routes[$name]; $defaults = array('action' => $action, 'module' => $module); // all params must be given foreach ($names as $tmp) { if (!isset($params[$tmp]) && !isset($defaults[$tmp])) { throw new \Exception(sprintf('Route named "%s" have a mandatory "%s" parameter', $name, $tmp)); } } $params = self::arrayDeepMerge($defaults, $params); if ($csrf_enabled) { $params['csrf_token'] = Caspar::generateCSRFtoken(); } $real_url = preg_replace_callback('/\\/\\:([^\\/]+)/', function ($matches) use($params) { return '/' . $params[$matches[1]]; }, $url); // we add all other params if * if (mb_strpos($real_url, '*')) { $tmp = array(); foreach ($params as $key => $value) { if (isset($names_hash[$key]) || isset($defaults[$key])) { continue; } if (is_array($value)) { foreach ($value as $k => $v) { if (is_array($v)) { foreach ($v as $vk => $vv) { if (is_array($vv)) { foreach ($vv as $vvk => $vvv) { $tmp[] = "{$key}[{$k}][{$vk}][{$vvk}]" . $equals . urlencode($vvv); } } else { $tmp[] = "{$key}[{$k}][{$vk}]" . $equals . urlencode($vv); } } } else { $tmp[] = "{$key}[{$k}]" . $equals . urlencode($v); } } } else { $tmp[] = urlencode($key) . $equals . urlencode($value); } } $tmp = implode($divider, $tmp); if (mb_strlen($tmp) > 0) { $tmp = $querydiv . $tmp; } $real_url = preg_replace('/\\/\\*(\\/|$)/', "{$tmp}\$1", $real_url); } // strip off last divider character if (mb_strlen($real_url) > 1) { $real_url = rtrim($real_url, $divider); } if (!$relative) { return Caspar::getBaseUrl() . $real_url; } return $real_url; }
/** * Returns the user object * * @return User */ protected function getUser() { return Caspar::getUser(); }