Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: PHP  |  size: 8.08 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * @author Camilo Zambrano
  5.  * @copyright 2011
  6.  */
  7.  
  8. abstract class Object {
  9.     private $operators = array();
  10.     function Object() {
  11.     }
  12.    
  13.     /**
  14.       * Returns an array with all the object subclass' attributes.
  15.       *
  16.       * @return array
  17.       */
  18.     static public function catchAttributes() {
  19.         return get_class_vars(get_called_class());
  20.     }
  21.    
  22.     /**
  23.       * Returns an array with all the object subclass' methods.
  24.       *
  25.       * @return array
  26.       */
  27.     static public function catchMethods() {
  28.         return get_class_methods(get_called_class());
  29.     }
  30.    
  31.     /**
  32.       * Determines if a variable is of a specific type.
  33.       *
  34.       * @param mixed $e
  35.       * @param string $type
  36.       * @return boolean
  37.       */
  38.     protected function is($e, $type) {
  39.         $valid   = array(
  40.             'array',
  41.             'bool',
  42.             'callable',
  43.             'double',
  44.             'float',
  45.             'int',
  46.             'integer',
  47.             'long',
  48.             'null',
  49.             'numeric',
  50.             'scalar',
  51.             'object',
  52.             'real',
  53.             'resource',
  54.             'string'
  55.         );
  56.         $correct = false;
  57.         if (in_array($type, $valid))
  58.             $func_name = "is_$type";
  59.         else if (get_class($e) == $type)
  60.             $correct = true;
  61.         else
  62.             return $correct;
  63.        
  64.         if ($correct == false && $func_name($e))
  65.             $correct = true;
  66.         return $correct;
  67.     }
  68.    
  69.     /**
  70.       * Creates a new operator usable only by the Instance of object subclass.
  71.       * Available only for PHP >= 5.3.
  72.       *
  73.       * @param string $name
  74.       * @param function $action
  75.       */
  76.     public function addNewOperator($name, $action) {
  77.         $this->operators[$name] = $action;
  78.     }
  79.    
  80.     /**
  81.       * Magic method that automatically calls the operator when it's
  82.       * called into the script.
  83.       *
  84.       * @param string $name
  85.       * @param array $arguments
  86.       */
  87.     public function __call($name, $arguments = NULL) {
  88.         foreach ($this->operators as $operator) {
  89.             call_user_func_array($operator, $arguments);
  90.         }
  91.     }
  92.    
  93.     /**
  94.      * Returns the value of $var_name QueryString, and encodes it
  95.      * if $safe is true (default).
  96.      *
  97.      * @return mixed
  98.      */
  99.     protected function QueryString() {
  100.                 $args = func_get_args();
  101.                 // If the datatype of the first parameter is int, then it's a permalink.
  102.                 $plink = (gettype($args[0]) == "integer") ? true : false;
  103.                 /* If the datatype of the second parameter ($safe)is not set then it's
  104.                 set to TRUE. */
  105.                 $args[1] = (!isset($args[1]) || empty($args[1])) ? true : $args[1];
  106.  
  107.                 if (PERMALINK == md5('permalink_true') && $plink) {
  108.                         if (isset($_SERVER["PATH_INFO"])) {
  109.                 $url = parse_url($_SERVER["PATH_INFO"], PHP_URL_PATH);
  110.                 $url = explode("/", $url);
  111.             } else {
  112.                 $url = Kernel::GetPathInfo();
  113.             }
  114.                         $urlen = count($url);
  115.                         if ($urlen == 2) {     
  116.                                 // If there's no class and method defined
  117.                                 return $url[$args[0]+1];
  118.                         } else if ( $urlen == 3 ) {
  119.                                 // If there's the class defined only.
  120.                                 return $url[$args[0]+2];
  121.                         } else if ( $urlen >= 4 ){
  122.                                 // If there's the class and method defined.
  123.                                 return $url[$args[0]+3];                       
  124.                         }
  125.                 } else {
  126.                         if (isset($_GET[$args[0]])) {
  127.                                 if ($safe == true)
  128.                                     return htmlentities($_GET[$args[0]], ENT_QUOTES, 'UTF-8');
  129.                                 else
  130.                                     return $_GET[$args[0]];
  131.                         } else {
  132.                                 return false;
  133.                         }
  134.                 }
  135.     }
  136.  
  137.     /**
  138.      * Creates a new cookie with name $name,
  139.      * value $value, lifetime $lifetime and if
  140.      * it is needed an extra cookiedata array
  141.      * $cookie_data created by the Kernel.
  142.      *
  143.      * @param string $name
  144.      * @param mixed $value
  145.      * @param int $lifetime
  146.      * @param mixed[optional] $cookie_data
  147.      */
  148.     protected function NewCookie($name, $val, $lifetime, $cookie_data = NULL) {
  149.         if ($this->CookieExists($name)) {
  150.             $this->DeleteCookie($name);
  151.         }
  152.         if (!$cookie_data) {
  153.             if (is_object($val) || is_array($val))
  154.                 $val = gzcompress(serialize($val), 9);
  155.             setcookie($name, $val, $lifetime);
  156.         } else {
  157.             if (($data = Kernel::GetSetting($cookie_data, $id)) != NULL) {
  158.                 if (is_object($val) || is_array($val))
  159.                     $val = gzcompress(serialize($val), 9);
  160.                 setcookie($name, $val, $lifetime, $data["path"], $data["domain"], $data["safe"], $data["httponly"]);
  161.             } else {
  162.                 Kernel::Error("No cookie data in configuration file.");
  163.             }
  164.         }
  165.     }
  166.     /**
  167.      * Returns the value of $var_name Cookie, and encodes it
  168.      * if $safe is true (default).
  169.      *
  170.      * @return mixed
  171.      */
  172.     protected function GetCookie($var_name, $isObject = false, $isArray = false, $safe = true) {
  173.         if (isset($_COOKIE[$var_name])) {
  174.             $value[0] = ($isObject && $isArray) ? false : $_COOKIE[$var_name];
  175.             $value[1] = ($isObject xor $isArray) ? unserialize(gzuncompress($_COOKIE[$var_name])) : $value[0];
  176.             if ($safe == true && !$isObject && !$isArray)
  177.                 return htmlentities($value[1], ENT_QUOTES);
  178.             else
  179.                 return $value[1];
  180.         } else {
  181.             return false;
  182.         }
  183.     }
  184.     /**
  185.      * Deletes the $var_name Cookie.
  186.      *
  187.      * @param string $var_name
  188.      */
  189.     protected function DeleteCookie($var_name) {
  190.         setcookie($var_name, "", time() - 3600);
  191.     }
  192.     /**
  193.      * Stores $value value in $sessvar session var.
  194.      *
  195.      * @param string $sessvar
  196.      * @param mixed $value
  197.      */
  198.     protected function Record($sessvar, $value) {
  199.             $_SESSION[$sessvar] = $value;
  200.     }
  201.     /**
  202.      * Returns $sessvar session var value.
  203.      *
  204.      * @param string $sessvar
  205.      */
  206.     protected function Get($sessvar) {
  207.         return (isset($_SESSION[$sessvar])) ? $_SESSION[$sessvar] : NULL;
  208.     }
  209.     /**
  210.      * Destroys $sessvar session var, if $sessvar is NULL, session
  211.      * is destroyed completely.
  212.      *
  213.      * @param string $sessvar
  214.      */
  215.     protected function Erase($sessvar = NULL) {
  216.         if ($sessvar == NULL)
  217.             session_destroy();
  218.         else {
  219.                         if($this->Check($sessvar))
  220.                                 unset($_SESSION[$sessvar]);
  221.         }
  222.     }
  223.     /**
  224.      * Returns the value of a sent-by-post var with $var_name index,
  225.      * and encodes it if $safe is true (default).
  226.      *
  227.      * @param mixed $var_name
  228.      * @param boolean $safe
  229.      * @return mixed
  230.      */
  231.     protected function Receive($var_name, $safe = true) {
  232.         if (isset($_POST[$var_name])) {
  233.             if ($safe == true && !is_array($_POST[$var_name]))
  234.                 return htmlentities($_POST[$var_name], ENT_QUOTES, 'UTF-8');
  235.             else
  236.                 return $_POST[$var_name];
  237.         } else {
  238.             return false;
  239.         }
  240.     }
  241.     /**
  242.      * Returns true if the session var exists and if it's not empty, false otherwise.
  243.      *
  244.      * @param string $sessvar
  245.      * @return boolean
  246.      */
  247.     protected function Check($sessvar) {
  248.         if (isset($_SESSION[$sessvar]) && !empty($_SESSION[$sessvar]))
  249.             return true;
  250.     }
  251.     /**
  252.      * Returns true if the cookie exists, false otherwise.
  253.      *
  254.      * @param string $var_name
  255.      * @return boolean
  256.      */
  257.     protected function CookieExists($var_name) {
  258.         if (isset($_COOKIE[$var_name]))
  259.             return true;
  260.         else
  261.             return false;
  262.     }
  263.         /**
  264.          * Redirecciona a la dirección contenida en $url.
  265.          * @param string $url
  266.          */
  267.         protected function Relocate($url) {
  268.                 echo "<script>location.href='$url'</script>";
  269.         }
  270.                
  271.         /**
  272.          * Envía los datos contenidos en $data a la dirección
  273.          * contenida en $url.
  274.          * @param string $url
  275.          * @param array $data
  276.          */
  277.         protected function Send($url, $data) {
  278.                 foreach ($data as $key => $value) {
  279.                         $this->Record($key,$value);
  280.                 }
  281.                 $this->Relocate($url);
  282.         }
  283. }
  284.  
  285. ?>