DotLibrary
[ class tree: DotLibrary ] [ index: DotLibrary ] [ all elements ]

Source for file Kernel.php

Documentation is available at Kernel.php

  1. <?php 
  2. /**
  3. * DotBoost Technologies Inc.
  4. * DotKernel Application Framework
  5. *
  6. @category   DotKernel
  7. @package    DotLibrary
  8. @copyright  Copyright (c) 2009 DotBoost  Technologies (http://www.dotboost.com)
  9. @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10. @version    $Id: Kernel.php 165 2010-07-02 07:25:12Z teo $
  11. */
  12.  
  13. /**
  14. * Bunch of miscelaneous  functions, used in all DotKernel Applications
  15. @category   DotKernel
  16. @package    DotLibrary
  17. @author     DotKernel Team <team@dotkernel.com>
  18. */
  19.  
  20. class Dot_Kernel
  21. {
  22.     /**
  23.      * Configuration instance
  24.      * @var Zend_config 
  25.      */
  26.     public $config;
  27.     /**
  28.      * Constant
  29.      * Dot Kernel version identification
  30.      * @var string 
  31.      */
  32.     const VERSION '1.2.0';    
  33.     /**
  34.      * Constructor
  35.      * @access public
  36.      * @return Dot_Kernel 
  37.      */
  38.     public function __construct()
  39.     {
  40.         $this->config = Zend_Registry::get('configuration');
  41.     }
  42.     /**
  43.      * End the execution of the application, by sending an 404 header and redirecting to home page
  44.      * @access public
  45.      * @return bool 
  46.      */
  47.     public function pageNotFound()
  48.     {       
  49.         // send the 404 header
  50.         header('HTTP/1.0 404 Not Found');
  51.         // redirect to 404 page
  52.         echo '<SCRIPT LANGUAGE=JAVASCRIPT> 
  53.                     function go()
  54.                     {
  55.                         window.location.href="'.$this->config->website->params->url.'" 
  56.                         }
  57.                         </SCRIPT>
  58.                     </HEAD>
  59.                     <BODY onLoad="go()">
  60.                     <!--
  61.                    - Unfortunately, Microsoft has added a clever new 
  62.                    - \"feature\" to Internet Explorer. If the text of
  63.                    - an error\'s message is \"too small\", specifically
  64.                    - less than 512 bytes, Internet Explorer returns
  65.                    - its own error message. You can turn that off,
  66.                    - but it\'s pretty tricky to find switch called
  67.                    - \"smart error messages\". That means, of course,
  68.                    - that short error messages are censored by default.
  69.                    - IIS always returns error messages that are long
  70.                   - enough to make Internet Explorer happy. The
  71.                    - workaround is pretty simple: pad the error
  72.                    - message with a big comment like this to push it
  73.                    - over the five hundred and twelve bytes minimum.
  74.                    - Of course, that\'s exactly what you\'re reading
  75.                    - right now.
  76.                    -->';
  77.         exit;
  78.     }
  79.     /**
  80.      * Return the user Ip , whatever the server are set
  81.      * @access public
  82.      * @static
  83.      * @return string 
  84.      */
  85.     public static function getUserIp()
  86.     {
  87.         if (isSet($_SERVER))
  88.         {
  89.             if (isSet($_SERVER['HTTP_X_FORWARDED_FOR']))
  90.             {
  91.                 $realIp $_SERVER['HTTP_X_FORWARDED_FOR'];
  92.             }
  93.             elseif (isSet($_SERVER['HTTP_CLIENT_IP']))
  94.             {
  95.                 $realIp $_SERVER['HTTP_CLIENT_IP'];
  96.             }
  97.             else
  98.             {
  99.                 $realIp $_SERVER['REMOTE_ADDR'];
  100.             }
  101.         }
  102.         else
  103.         {
  104.             if (getenv('HTTP_X_FORWARDED_FOR'))
  105.             {
  106.                 $realIp getenv('HTTP_X_FORWARDED_FOR');
  107.             }
  108.             elseif (getenv('HTTP_CLIENT_IP'))
  109.             {
  110.                 $realIp getenv('HTTP_CLIENT_IP');
  111.             }
  112.             else
  113.             {
  114.                 $realIp getenv('REMOTE_ADDR');
  115.             }
  116.         }
  117.         return $realIp;
  118.     }
  119.     /**
  120.      * Return the name of the browser icon based on User Agent
  121.      * @access public
  122.      * @static
  123.      * @param string $user 
  124.      * @return string 
  125.      */
  126.     public static function getBrowserIcon($agent)
  127.     {        
  128.         $xml new Zend_Config_Xml(CONFIGURATION_PATH.'/browser.xml');
  129.         $browser $xml->name->type->toArray();
  130.         foreach ($browser as $key => $val)
  131.         {            
  132.             if (stripos($agent,$val['uaBrowser']!== FALSE)
  133.             {
  134.                 return $val['uaIcon'];
  135.             }
  136.         }
  137.         return 'unknown';
  138.     }
  139.     /**
  140.      * Return the name of the OS icon based on User Agent
  141.      * @access public
  142.      * @static
  143.      * @param string $user 
  144.      * @return array 
  145.      */
  146.     public static function getOsIcon($agent)
  147.     {        
  148.         $xml new Zend_Config_Xml(CONFIGURATION_PATH.'/os.xml');
  149.         $os $xml->type->toArray();
  150.         foreach ($os as $major)
  151.         {
  152.             foreach ($major as $osArray)
  153.             {
  154.                 if(array_key_exists('identify'$osArray))
  155.                 {//there are minor version
  156.                     foreach ($osArray['identify'as $minor)
  157.                     {
  158.                         $uaStringArray explode('|',$minor['uaString']);
  159.                         foreach ($uaStringArray as $uaString)
  160.                         {                        
  161.                             if ((stripos($agent$uaString!== false))
  162.                             {
  163.                                 $operatingSystem array('icon'=>strtolower(str_replace(' ''_'$osArray['os']))'major'=>$osArray['os']'minor'=>$minor['osName']);
  164.                                 return $operatingSystem;
  165.                             }
  166.                         }                    
  167.                     }
  168.                 }
  169.                 else
  170.                 {//no minor version known for this os
  171.                     if ((stripos($agent$osArray['os']!== false))
  172.                     {
  173.                         $operatingSystem array('icon'=>strtolower(str_replace(' ''_'$osArray['os']))'major'=>$osArray['os']'minor'=>'');
  174.                         return $operatingSystem;
  175.                     }
  176.                 }
  177.                                     
  178.             }
  179.         }
  180.         return array('major'=>'''minor'=>'');
  181.     }
  182.     /**
  183.      * Return date formatted fancy
  184.      * @access public
  185.      * @static
  186.      * @param string $date 
  187.      * @param string $format - 'short', 'long'
  188.      * @return string 
  189.      */
  190.     public static function timeFormat($date$format='short')
  191.     {
  192.         $settings Zend_Registry::get('settings');
  193.         $times strtotime($date);
  194.         switch($format)
  195.         {
  196.             case 'long':
  197.                 $times strftime($settings->timeFormatLong,$times);
  198.             break;            
  199.             case 'short':
  200.             default:
  201.                 $times strftime($settings->timeFormatShort,$times);
  202.             break;
  203.         }
  204.         return $times;
  205.     }
  206.     /**
  207.      * Process that validate and filter the input/output data.
  208.      * Return valid and filtered data
  209.      * @access public
  210.      * @static
  211.      * @param Zend_Validate $validator 
  212.      * @param array $values 
  213.      * @return array 
  214.      */
  215.     public static function validateFilter($validator$values)
  216.     {
  217.         $data $error array();
  218.         $filter new Zend_Filter();
  219.         $filter->addFilter(new Zend_Filter_HtmlEntities());
  220.         $filter->addFilter(new Zend_Filter_StringTrim());
  221.         foreach ($values as $k=>$v)
  222.         {
  223.             if($validator->isValid($values[$k]))
  224.             {
  225.                 //filter the input     
  226.                 $data[$k$filter->filter($values[$k])
  227.             }
  228.             else
  229.             {
  230.                 foreach ($validator->getMessages(as $message)
  231.                 {
  232.                     //filter the output
  233.                     $error[$kstr_replace($values[$k]$filter->filter($values[$k])$message);
  234.                 }
  235.             }
  236.         }
  237.         return array('data'=>$data,'error'=>$error);
  238.     }
  239. }

Documentation generated on Wed, 21 Jul 2010 07:34:41 +0000 by phpDocumentor 1.4.3