Beispiel #1
0
 /**
  * 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 = nil)
 {
     static $info;
     // Return the raw string
     if ($key === 'agent') {
         return Eight::$user_agent;
     }
     if ($info === nil) {
         // Parse the user agent and extract basic information
         $agents = Eight::config('user_agents');
         foreach ($agents as $type => $data) {
             foreach ($data as $agent => $name) {
                 if (stripos(Eight::$user_agent, $agent) !== NO) {
                     if ($type === 'browser' and preg_match('|' . preg_quote($agent) . '[^0-9.]*+([0-9.][0-9.a-z]*)|i', Eight::$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, Eight::user_agent('languages'));
                 break;
             case 'accept_charset':
                 // Check if the charset is accepted
                 return in_array($compare, Eight::user_agent('charsets'));
                 break;
             default:
                 // Invalid comparison
                 return NO;
                 break;
         }
     }
     // Return the key, if set
     return isset($info[$key]) ? $info[$key] : nil;
 }
Beispiel #2
0
 /**
  * Force a download of a file to the user's browser. This function is
  * binary-safe and will work with any MIME type that Eight 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
  */
 public static function download($filename = nil, $data = nil, $nicename = nil)
 {
     if (empty($filename)) {
         return NO;
     }
     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 = Eight::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 (Eight::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
     Eight::close_buffers(NO);
     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;
     }
 }
Beispiel #3
0
 /**
  * Demonstrates the User_Agent library.
  */
 function user_agent()
 {
     foreach (array('agent', 'browser', 'version') as $key) {
         echo $key . ': ' . Eight::user_agent($key) . '<br/>' . "\n";
     }
     echo "<br/><br/>\n";
     echo 'done in {execution_time} seconds';
 }