/** * Constructor. * * For example: * ```php * // moves the uploaded file * $upload = new HttpUpload("my_file"); * $file = $upload->move("/path/to/your/folder"); * echo "Your file was uploaded to: $file"; * ``` * * @param string $name Attribute name */ public function __construct($name) { // initializes errors $this->_errors[UPLOAD_ERR_INI_SIZE] = "The uploaded file exceeds the upload_max_filesize " . "directive in php.ini"; $this->_errors[UPLOAD_ERR_FORM_SIZE] = "The uploaded file exceeds the MAX_FILE_SIZE directive " . "that was specified in the HTML form"; $this->_errors[UPLOAD_ERR_PARTIAL] = "The uploaded file was only partially uploaded"; $this->_errors[UPLOAD_ERR_NO_FILE] = "No file was uploaded"; $this->_errors[UPLOAD_ERR_NO_TMP_DIR] = "Missing a temporary folder"; $this->_errors[UPLOAD_ERR_CANT_WRITE] = "Failed to write file to disk"; $this->_errors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped the file upload. PHP does not " . "provide a way to ascertain which extension caused the file upload to stop; examining " . "the list of loaded extensions with phpinfo() may help"; if (!Arr::exist($_FILES, $name)) { throw new HttpException("File key not found: {$name}"); } $this->_file = Arr::get($_FILES, $name); }
/** * Loads url contents. * * <p>Loads the contents of a URL and, optionally, the mimetype and the * charset of the page.</p> * * @param string $source A filename or a URL * @param string $charset Charset, autodetected (default is "") * @param string $mimetype Mime-type, autodetected (default is "") * * @return array */ private function _loadUrl($source, $charset = "", $mimetype = "") { $loadHeaders = strlen($mimetype) == 0 || strlen($charset) == 0; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $source); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, $loadHeaders); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); $result = curl_exec($ch); curl_close($ch); // loads mime-type and charse if ($loadHeaders) { $headers = null; $separator = "\r\n\r\n"; $pos = strpos($result, $separator); if ($pos !== false) { $headers = substr($result, 0, $pos); $content = substr($result, $pos + strlen($separator)); } $lines = explode("\r\n", $headers); foreach ($lines as $line) { $regexp = '@Content-Type:\\s*([\\w/+]+)(;\\s*charset=(\\S+))?@i'; if (preg_match($regexp, $line, $matches) > 0) { if (strlen($mimetype) == 0) { $mimetype = Arr::exist($matches, 1) ? $matches[1] : null; } if (strlen($charset) == 0) { $charset = Arr::exist($matches, 3) ? $matches[3] : null; } } } } else { $content = $result; } return array($content, $mimetype, $charset); }
/** * Does the request attribute exist? * * @param string $name HttpRequest attribute. * * @return boolean */ public function exist($name) { return Arr::exist($_REQUEST, $name); }
/** * Does the exist exist? * * @param string $name Request attribute. * * @return boolean */ public function existCookie($name) { return Arr::exist($_COOKIE, $name); }
/** * Fetches elements from an array. * * This function is especially suitable for getting optional arguments. * * For example: * ```php * function test($title, $message, $x, $y, $options) { * $args = Arr::fetch(func_get_args(), array( * // `title` is a required string * "title" => "string", * // `message` is an optional string and has a default value * "message" => array( * "type" => "string", * "default" => "Default message ..." * ), * // `x` is an optional string or number and has a default value * "x" => array( * "type" => "string|number", * "default" => 0 * ), * // `y` is an optional string or number and has a default value * "y" => array( * "type" => "string|number", * "default" => 0 * ), * // `options` is an optional array * "options" => array( * "type" => "array", * required => false * ) * ); * print_r($args); * } * // this throws an InvalidArgumentException, as 'title' is required. * test(120, 250); * ``` * * @param array $arr Array of mixed elements * @param array $descriptors Associative array of descriptors. * * @throws InvalidArgumentException * @return array */ public static function fetch($arr, $descriptors) { $args = new ArrArguments($arr); foreach ($descriptors as $name => $descriptor) { $types = array(); $default = null; $required = false; if (is_string($descriptor)) { $types = explode("|", $descriptor); } elseif (is_array($descriptor)) { $types = explode("|", Arr::get($descriptor, "type")); $default = Arr::get($descriptor, "default"); $required = Arr::get($descriptor, "required", !Arr::exist($descriptor, "default")); } $desc = new ArrArgumentsDescriptor($types, $default, $required); $args->registerDescriptor($name, $desc); } return $args->fetch(); }
/** * Does the session attribute exist? * * @param string $name Attribute name * * @return boolean */ public function existSession($name) { $this->_startSession(); return Arr::exist($_SESSION, $name); }
/** * Does the request attribute exist? * * @param string $name Request attribute. * * @return boolean */ public function exist($name) { HttpSession::start(); return Arr::exist($_SESSION, $name); }