<?php
/**
* @author Camilo Zambrano
* @copyright 2011
*/
abstract class Object {
private $operators = array();
function Object() {
}
/**
* Returns an array with all the object subclass' attributes.
*
* @return array
*/
static public function catchAttributes() {
return get_class_vars(get_called_class());
}
/**
* Returns an array with all the object subclass' methods.
*
* @return array
*/
static public function catchMethods() {
return get_class_methods(get_called_class());
}
/**
* Determines if a variable is of a specific type.
*
* @param mixed $e
* @param string $type
* @return boolean
*/
protected function is($e, $type) {
$valid = array(
'array',
'bool',
'callable',
'double',
'float',
'int',
'integer',
'long',
'null',
'numeric',
'scalar',
'object',
'real',
'resource',
'string'
);
$correct = false;
if (in_array($type, $valid))
$func_name = "is_$type";
else if (get_class($e) == $type)
$correct = true;
else
return $correct;
if ($correct == false && $func_name($e))
$correct = true;
return $correct;
}
/**
* Creates a new operator usable only by the Instance of object subclass.
* Available only for PHP >= 5.3.
*
* @param string $name
* @param function $action
*/
public function addNewOperator($name, $action) {
$this->operators[$name] = $action;
}
/**
* Magic method that automatically calls the operator when it's
* called into the script.
*
* @param string $name
* @param array $arguments
*/
public function __call($name, $arguments = NULL) {
foreach ($this->operators as $operator) {
call_user_func_array($operator, $arguments);
}
}
/**
* Returns the value of $var_name QueryString, and encodes it
* if $safe is true (default).
*
* @return mixed
*/
protected function QueryString() {
$args = func_get_args();
// If the datatype of the first parameter is int, then it's a permalink.
$plink = (gettype($args[0]) == "integer") ? true : false;
/* If the datatype of the second parameter ($safe)is not set then it's
set to TRUE. */
$args[1] = (!isset($args[1]) || empty($args[1])) ? true : $args[1];
if (PERMALINK == md5('permalink_true') && $plink) {
if (isset($_SERVER["PATH_INFO"])) {
$url = parse_url($_SERVER["PATH_INFO"], PHP_URL_PATH);
$url = explode("/", $url);
} else {
$url = Kernel::GetPathInfo();
}
$urlen = count($url);
if ($urlen == 2) {
// If there's no class and method defined
return $url[$args[0]+1];
} else if ( $urlen == 3 ) {
// If there's the class defined only.
return $url[$args[0]+2];
} else if ( $urlen >= 4 ){
// If there's the class and method defined.
return $url[$args[0]+3];
}
} else {
if (isset($_GET[$args[0]])) {
if ($safe == true)
return htmlentities($_GET[$args[0]], ENT_QUOTES, 'UTF-8');
else
return $_GET[$args[0]];
} else {
return false;
}
}
}
/**
* Creates a new cookie with name $name,
* value $value, lifetime $lifetime and if
* it is needed an extra cookiedata array
* $cookie_data created by the Kernel.
*
* @param string $name
* @param mixed $value
* @param int $lifetime
* @param mixed[optional] $cookie_data
*/
protected function NewCookie($name, $val, $lifetime, $cookie_data = NULL) {
if ($this->CookieExists($name)) {
$this->DeleteCookie($name);
}
if (!$cookie_data) {
if (is_object($val) || is_array($val))
$val = gzcompress(serialize($val), 9);
setcookie($name, $val, $lifetime);
} else {
if (($data = Kernel::GetSetting($cookie_data, $id)) != NULL) {
if (is_object($val) || is_array($val))
$val = gzcompress(serialize($val), 9);
setcookie($name, $val, $lifetime, $data["path"], $data["domain"], $data["safe"], $data["httponly"]);
} else {
Kernel::Error("No cookie data in configuration file.");
}
}
}
/**
* Returns the value of $var_name Cookie, and encodes it
* if $safe is true (default).
*
* @return mixed
*/
protected function GetCookie($var_name, $isObject = false, $isArray = false, $safe = true) {
if (isset($_COOKIE[$var_name])) {
$value[0] = ($isObject && $isArray) ? false : $_COOKIE[$var_name];
$value[1] = ($isObject xor $isArray) ? unserialize(gzuncompress($_COOKIE[$var_name])) : $value[0];
if ($safe == true && !$isObject && !$isArray)
return htmlentities($value[1], ENT_QUOTES);
else
return $value[1];
} else {
return false;
}
}
/**
* Deletes the $var_name Cookie.
*
* @param string $var_name
*/
protected function DeleteCookie($var_name) {
setcookie($var_name, "", time() - 3600);
}
/**
* Stores $value value in $sessvar session var.
*
* @param string $sessvar
* @param mixed $value
*/
protected function Record($sessvar, $value) {
$_SESSION[$sessvar] = $value;
}
/**
* Returns $sessvar session var value.
*
* @param string $sessvar
*/
protected function Get($sessvar) {
return (isset($_SESSION[$sessvar])) ? $_SESSION[$sessvar] : NULL;
}
/**
* Destroys $sessvar session var, if $sessvar is NULL, session
* is destroyed completely.
*
* @param string $sessvar
*/
protected function Erase($sessvar = NULL) {
if ($sessvar == NULL)
session_destroy();
else {
if($this->Check($sessvar))
unset($_SESSION[$sessvar]);
}
}
/**
* Returns the value of a sent-by-post var with $var_name index,
* and encodes it if $safe is true (default).
*
* @param mixed $var_name
* @param boolean $safe
* @return mixed
*/
protected function Receive($var_name, $safe = true) {
if (isset($_POST[$var_name])) {
if ($safe == true && !is_array($_POST[$var_name]))
return htmlentities($_POST[$var_name], ENT_QUOTES, 'UTF-8');
else
return $_POST[$var_name];
} else {
return false;
}
}
/**
* Returns true if the session var exists and if it's not empty, false otherwise.
*
* @param string $sessvar
* @return boolean
*/
protected function Check($sessvar) {
if (isset($_SESSION[$sessvar]) && !empty($_SESSION[$sessvar]))
return true;
}
/**
* Returns true if the cookie exists, false otherwise.
*
* @param string $var_name
* @return boolean
*/
protected function CookieExists($var_name) {
if (isset($_COOKIE[$var_name]))
return true;
else
return false;
}
/**
* Redirecciona a la dirección contenida en $url.
* @param string $url
*/
protected function Relocate($url) {
echo "<script>location.href='$url'</script>";
}
/**
* Envía los datos contenidos en $data a la dirección
* contenida en $url.
* @param string $url
* @param array $data
*/
protected function Send($url, $data) {
foreach ($data as $key => $value) {
$this->Record($key,$value);
}
$this->Relocate($url);
}
}
?>