public function __construct($use_cache = FALSE, $cache_lifetime = NULL) { $this->use_cache = $use_cache; $this->cache_lifetime = $cache_lifetime; $this->cache = new Cache(); $this->setReturnTransfer(TRUE); $this->setUserAgent(Kohana::user_agent() or TRUE); }
/** * Force a download of a file to the user's browser. This function is * binary-safe and will work with any MIME type that Kohana is aware of. * * @param string a file path or file name * @param mixed data to be sent if the filename does not exist * @param string suggested filename to display in the download * @return void */ public static function force($filename = NULL, $data = NULL, $nicename = NULL) { if (empty($filename)) { return FALSE; } if (is_file($filename)) { // Get the real path $filepath = str_replace('\\', '/', realpath($filename)); // Set filesize $filesize = filesize($filepath); // Get filename $filename = substr(strrchr('/' . $filepath, '/'), 1); // Get extension $extension = strtolower(substr(strrchr($filepath, '.'), 1)); } else { // Get filesize $filesize = strlen($data); // Make sure the filename does not have directory info $filename = substr(strrchr('/' . $filename, '/'), 1); // Get extension $extension = strtolower(substr(strrchr($filename, '.'), 1)); } // Get the mime type of the file $mime = Kohana::config('mimes.' . $extension); if (empty($mime)) { // Set a default mime if none was found $mime = array('application/octet-stream'); } // Generate the server headers header('Content-Type: ' . $mime[0]); header('Content-Disposition: attachment; filename="' . (empty($nicename) ? $filename : $nicename) . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . sprintf('%d', $filesize)); // More caching prevention header('Expires: 0'); if (Kohana::user_agent('browser') === 'Internet Explorer') { // Send IE headers header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } else { // Send normal headers header('Pragma: no-cache'); } // Clear the output buffer Kohana::close_buffers(FALSE); if (isset($filepath)) { // Open the file $handle = fopen($filepath, 'rb'); // Send the file data fpassthru($handle); // Close the file fclose($handle); } else { // Send the file data echo $data; } }
/** * Allows a model to be loaded by filename. */ public function add_view($type, $id) { $this->item_type = $type; $this->item_id = $id; $this->ip = Input::instance()->ip_address(); $this->referrer = request::referrer(); $this->user_agent = Kohana::user_agent(); $this->save(); }
public static function check_spam() { $comment = array('comment_type' => 'comment', 'comment_author' => Event::$data->author, 'comment_author_email' => Event::$data->email, 'comment_author_url' => Event::$data->url, 'comment_content' => Event::$data->content, 'referrer' => request::referrer(), 'user_ip' => Event::$data->ip, 'user_agent' => Kohana::user_agent(), 'blog' => url::base(TRUE, 'http')); $result = self::send_request('comment', $comment); if ($result === 'true') { Event::$data->approved = 'no'; Event::$data->save(); } }
public function setup() { Input::instance()->ip_address = "1.1.1.1"; Kohana::$user_agent = "Akismet_Helper_Test"; $root = ORM::factory("item", 1); $this->_comment = comment::create($root, user::guest(), "This is a comment", "John Doe", "*****@*****.**", "http://gallery2.org"); foreach ($this->_comment->list_fields("comments") as $name => $field) { if (strpos($name, "server_") === 0) { $this->_comment->{$name} = substr($name, strlen("server_")); } } $this->_comment->save(); module::set_var("akismet", "api_key", "TEST_KEY"); }
public static function browser_language() { if (language::$browser_language === NULL) { language::$browser_language = ''; $browser_languages = Kohana::user_agent('languages'); foreach ($browser_languages as $language) { if (strlen($language) == 2 and array_key_exists($language, language::$available_languages)) { language::$browser_language = strtolower($language); break; } } } return language::$browser_language; }
function fancyupload_modify_session_config() { if (stripos(Kohana::user_agent(), 'Shockwave Flash') !== FALSE) { // Check that the session isn't attempting to validate the user agent $validate = Kohana::config('session.validate'); $key = array_search('user_agent', $validate); if ($key !== FALSE) { unset($validate[$key]); Kohana::config('session.validate', $validate); } // Ensure that the session key won't be regenerated Kohana::config_set('session.regenerate', 0); } }
/** * Sets up the PHP environment. Adds error/exception handling, output * buffering, and adds an auto-loading method for loading classes. * * This method is run immediately when this file is loaded, and is * benchmarked as environment_setup. * * For security, this function also destroys the $_REQUEST global variable. * Using the proper global (GET, POST, COOKIE, etc) is inherently more secure. * The recommended way to fetch a global variable is using the Input library. * @see http://www.php.net/globals * * @return void */ public static function setup() { static $run; // This function can only be run once if ($run === TRUE) { return; } // Start the environment setup benchmark Benchmark::start(SYSTEM_BENCHMARK . '_environment_setup'); // Define Kohana error constant define('E_KOHANA', 42); // Define 404 error constant define('E_PAGE_NOT_FOUND', 43); // Define database error constant define('E_DATABASE_ERROR', 44); if (self::$cache_lifetime = self::config('core.internal_cache')) { // Load cached configuration and language files self::$internal_cache['configuration'] = self::cache('configuration', self::$cache_lifetime); self::$internal_cache['language'] = self::cache('language', self::$cache_lifetime); // Load cached file paths self::$internal_cache['find_file_paths'] = self::cache('find_file_paths', self::$cache_lifetime); // Enable cache saving Event::add('system.shutdown', array(__CLASS__, 'internal_cache_save')); } // Disable notices and "strict" errors $ER = error_reporting(~E_NOTICE & ~E_STRICT); // Set the user agent self::$user_agent = trim($_SERVER['HTTP_USER_AGENT']); if (function_exists('date_default_timezone_set')) { $timezone = self::config('locale.timezone'); // Set default timezone, due to increased validation of date settings // which cause massive amounts of E_NOTICEs to be generated in PHP 5.2+ date_default_timezone_set(empty($timezone) ? 'UTC' : $timezone); } // Restore error reporting error_reporting($ER); // Start output buffering ob_start(array(__CLASS__, 'output_buffer')); // Save buffering level self::$buffer_level = ob_get_level(); // Set autoloader spl_autoload_register(array('Kohana', 'auto_load')); // Set error handler set_error_handler(array('Kohana', 'exception_handler')); // Set exception handler set_exception_handler(array('Kohana', 'exception_handler')); // Send default text/html UTF-8 header header('Content-Type: text/html; charset=UTF-8'); // Load locales $locales = self::config('locale.language'); // Make first locale UTF-8 $locales[0] .= '.UTF-8'; // Set locale information self::$locale = setlocale(LC_ALL, $locales); if (self::$configuration['core']['log_threshold'] > 0) { // Set the log directory self::log_directory(self::$configuration['core']['log_directory']); // Enable log writing at shutdown register_shutdown_function(array(__CLASS__, 'log_save')); } // Enable Kohana routing Event::add('system.routing', array('Router', 'find_uri')); Event::add('system.routing', array('Router', 'setup')); // Enable Kohana controller initialization Event::add('system.execute', array('Kohana', 'instance')); // Enable Kohana 404 pages Event::add('system.404', array('Kohana', 'show_404')); // Enable Kohana output handling Event::add('system.shutdown', array('Kohana', 'shutdown')); if (self::config('core.enable_hooks') === TRUE) { // Find all the hook files $hooks = self::list_files('hooks', TRUE); foreach ($hooks as $file) { // Load the hook include $file; } } // Setup is complete, prevent it from being run again $run = TRUE; // Stop the environment setup routine Benchmark::stop(SYSTEM_BENCHMARK . '_environment_setup'); }
/** * Download a file to the user's browser. This function is * binary-safe and will work with any MIME type that Kohana is aware of. * * @param string a file path or file name, or data * @param string suggested filename to display in the download * @return void */ public static function download($filedata, $filename = NULL) { // file if (is_file($filedata)) { $filepath = str_replace('\\', '/', realpath($filedata)); $filesize = filesize($filepath); $filename = substr(strrchr('/' . $filepath, '/'), 1); // Make sure the filename does not have directory info $extension = strtolower(substr(strrchr($filepath, '.'), 1)); } else { $filesize = strlen($filedata); $extension = strtolower(substr(strrchr($filename, '.'), 1)); } // Mimetype $mimes = Kohana::config('mimes.' . $extension); $mime = $mimes[0]; if (empty($mime)) { $mime = array('application/octet-stream'); } // headers // Generate the server headers header('Content-Type: ' . $mime[0]); header('Content-Disposition: attachment; filename="' . (empty($nicename) ? $filename : $nicename) . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . sprintf('%d', $filesize)); // More caching prevention header('Expires: 0'); if (Kohana::user_agent('browser') === 'Internet Explorer') { // Send IE headers header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } else { // Send normal headers header('Pragma: no-cache'); } // output if (isset($filepath)) { $handle = fopen($filepath, 'rb'); fpassthru($handle); fclose($handle); } else { echo $filedata; } }
public static function add_stat($type, $id) { if (Kohana::user_agent('is_browser')) { ORM::factory('statistic')->add_view($type, $id); } }
/** * Demonstrates the User_Agent library. */ function user_agent() { foreach (array('agent', 'browser', 'version') as $key) { echo $key . ': ' . Kohana::user_agent($key) . '<br/>' . "\n"; } echo "<br/><br/>\n"; echo 'done in {execution_time} seconds'; }
public function teardown() { Input::instance()->ip_address = $this->_ip_address; Kohana::$user_agent = $this->_user_agent; }
* This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ // If var/database.php doesn't exist, then we assume that the Gallery is not properly installed // and send users to the installer. if (!file_exists(VARPATH . "database.php")) { url::redirect(url::abs_file("installer")); } Event::add("system.ready", array("I18n", "instance")); Event::add("system.ready", array("module", "load_modules")); Event::add("system.ready", array("gallery", "ready")); Event::add("system.post_routing", array("url", "parse_url")); Event::add("system.post_routing", array("gallery", "maintenance_mode")); Event::add("system.shutdown", array("gallery", "shutdown")); // @todo once we convert to Kohana 2.4 this doesn't have to be here set_error_handler(array("gallery_error", "error_handler")); // Override the cookie if we have a session id in the URL. // @todo This should probably be an event callback $input = Input::instance(); if ($g3sid = $input->post("g3sid", $input->get("g3sid"))) { $_COOKIE["g3sid"] = $g3sid; } if ($user_agent = $input->post("user_agent", $input->get("user_agent"))) { Kohana::$user_agent = $user_agent; }
/** * Retrieves current user agent information: * keys: browser, version, platform, mobile, robot, referrer, languages, charsets * tests: is_browser, is_mobile, is_robot, accept_lang, accept_charset * * @param string key or test name * @param string used with "accept" tests: user_agent(accept_lang, en) * @return array languages and charsets * @return string all other keys * @return boolean all tests */ public static function user_agent($key = 'agent', $compare = NULL) { static $info; // Return the raw string if ($key === 'agent') { return Kohana::$user_agent; } if ($info === NULL) { // Parse the user agent and extract basic information $agents = Kohana::config('user_agents'); foreach ($agents as $type => $data) { foreach ($data as $agent => $name) { if (stripos(Kohana::$user_agent, $agent) !== FALSE) { if ($type === 'browser' and preg_match('|' . preg_quote($agent) . '[^0-9.]*+([0-9.][0-9.a-z]*)|i', Kohana::$user_agent, $match)) { // Set the browser version $info['version'] = $match[1]; } // Set the agent name $info[$type] = $name; break; } } } } if (empty($info[$key])) { switch ($key) { case 'is_robot': case 'is_browser': case 'is_mobile': // A boolean result $return = !empty($info[substr($key, 3)]); break; case 'languages': $return = array(); if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { if (preg_match_all('/[-a-z]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])), $matches)) { // Found a result $return = $matches[0]; } } break; case 'charsets': $return = array(); if (!empty($_SERVER['HTTP_ACCEPT_CHARSET'])) { if (preg_match_all('/[-a-z0-9]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])), $matches)) { // Found a result $return = $matches[0]; } } break; case 'referrer': if (!empty($_SERVER['HTTP_REFERER'])) { // Found a result $return = trim($_SERVER['HTTP_REFERER']); } break; } // Cache the return value isset($return) and $info[$key] = $return; } if (!empty($compare)) { // The comparison must always be lowercase $compare = strtolower($compare); switch ($key) { case 'accept_lang': // Check if the lange is accepted return in_array($compare, Kohana::user_agent('languages')); break; case 'accept_charset': // Check if the charset is accepted return in_array($compare, Kohana::user_agent('charsets')); break; default: // Invalid comparison return FALSE; break; } } // Return the key, if set return isset($info[$key]) ? $info[$key] : NULL; }