/** * Get response status header string */ public static function getHeader($scheme = 'HTTP/1.1', $code = 200) { $scheme = strtoupper(Sanitize::toText($scheme)); $code = Sanitize::toNumber($code); $info = self::getString($code); return $scheme . ' ' . $code . ' ' . $info; }
/** * Look for timezone offset value saved in cookie by JS (in seconds) */ public function lookupJsCookie($name = '') { $name = Sanitize::toKey($name); $value = Utils::getValue(@$_COOKIE[$name], null); if (is_numeric($value)) { $this->setTimezoneOffset(intval($value)); } }
/** * Add a new flash message to the list for $key */ public function setFlash($key = '', $class = '', $message = '') { if (!empty($key) && is_string($key)) { $class = Sanitize::toText($class); $message = Sanitize::toText($message); $key = $this->fkey . '.' . $key; $list = $this->session->get($key, array()); $list[] = array('class' => $class, 'message' => $message, 'addtime' => time()); $this->session->set($key, $list); } }
/** * Get a value for a key in _ENV, if set, or default */ public function getEnv($key = '', $default = null) { $key = Sanitize::toKey($key); if (!empty($key) && array_key_exists($key, $_ENV)) { return $_ENV[$key]; } return $default; }
/** * Delete an entry for a dot-notated key string */ public function delete($key = '') { $key = Sanitize::toKey($key); if (!empty($key) && $this->started === true) { $path = trim($this->container . '.' . $key, '.'); $list = explode('.', $path); $last = array_pop($list); $data =& $_SESSION; foreach ($list as $step) { if (!isset($data[$step])) { return; // gone } $data =& $data[$step]; } if (isset($data[$last])) { // need to reference the last key for unset() to work $data[$last] = null; unset($data[$last]); } } }
/** * Gets an existing key, or return a default value. */ public function delete($key = '') { $key = Sanitize::toKey($key); if (!empty($key)) { $list = explode('.', $key); $last = array_pop($list); $data =& $this->_data; foreach ($list as $step) { if (!isset($data[$step])) { return; } $data =& $data[$step]; } if (isset($data[$last])) { $data[$last] = null; unset($data[$last]); } } }
/** * Recursive filter for input data arrays */ private function _filter($value = null) { if (is_numeric($value)) { return $value + 0; } if (is_string($value)) { $value = trim($value); if (!empty($this->_encoding)) { $value = mb_convert_encoding($value, $this->_encoding, $this->_encoding); } if (get_magic_quotes_gpc()) { $value = stripslashes($value); } return Sanitize::toType($value); } if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = $this->_filter($v); } } return $value; }
/** * Adds a new item node to the XML DOM, for when creating RSS feeds. */ public function item($pairs = array()) { $this->parent('channel')->node('item'); foreach ($pairs as $key => $value) { $key = Sanitize::toSlug($key); if (empty($key) || is_numeric($key)) { continue; } $this->node($key)->value($value)->parent(); } return $this; }
/** * Cleans a path and removes the doc root from it */ private function _relativePath($path = '') { $path = Sanitize::toPath($path); $root = Sanitize::toPath($_SERVER['DOCUMENT_ROOT']); foreach (explode('/', $root) as $dir) { $path = str_replace($dir . '/', '', $path); } return '/' . $path; }
/** * Set fulltext column/s index */ public function addFulltextKey($name = '', $columns = array()) { $name = Sanitize::toKey($name); $columns = array_values($columns); if (!empty($name) && !empty($columns)) { $this->indexes[$name] = "FULLTEXT " . $name . " (`" . implode("`,`", $columns) . "`)"; } return $this; }
/** * Get the item mime/content-type string */ public function getInfo() { $time = $this->getTimestamps(); $title = Sanitize::toTitle($this->_name); $title = Sanitize::toCaps($title); return array('path' => $this->_path, 'parent' => $this->_parent, 'extension' => $this->_extension, 'name' => $this->_name, 'filename' => $this->getFileName(), 'type' => $this->getType(), 'perms' => $this->getPermissions(), 'owner' => $this->getOwner(), 'mimetype' => $this->getMimeType(), 'category' => $this->getCategory(), 'title' => $title, 'created' => $time['created'], 'modified' => $time['modified'], 'accessed' => $time['accessed'], 'writable' => is_writable($this->_path)); }
/** * Convert a request path string into routing params */ public function parse() { $this->resetActions(); $this->resetParams(); $this->setArea($this->_default_area); $this->setController($this->_default_controller); $this->addAction('init-action', false); $path = Utils::getValue($this->_path_request, '/'); $path = Sanitize::toPath(@parse_url($path, PHP_URL_PATH)); $path = str_replace(Server::getBasePath(), '', $path); $route = explode('/', trim($path, '/')); if (!empty($route[0]) && $this->areaExists($route[0])) { $this->setArea(array_shift($route)); } if (!empty($route[0])) { $this->setController(array_shift($route)); } if (!empty($route[0])) { $this->addAction(array_shift($route)); } if (!empty($route)) { $this->_params = array_values($route); } if (count($this->_actions) === 1) { $this->addAction($this->_default_action); } }
/** * Get a cookie value, or default fallback */ public function get($default = '', $decrypt = false) { if ($this->exists()) { $value = trim($_COOKIE[$this->_name]); if ($decrypt === true) { $value = $this->decrypt($value); } return Sanitize::toType($value); } return $default; }
/** * Send redirect response */ public function redirect($location = '', $code = 302, $delay = 1) { $current = Server::getUrl(); $location = Sanitize::toUrl($location); $path1 = Sanitize::toPath(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $path2 = Sanitize::toPath(parse_url($location, PHP_URL_PATH)); $code = is_numeric($code) ? intval($code) : 302; if (Validate::isExternal($location) || $path1 !== $path2) { $this->flushHeaders(); $this->flushContents(); $this->setText($code, ''); $this->setHeader('Location', $location, true); $this->setHeader('Connection', 'close', true); $this->send($delay); } throw new Exception('Redirect aborted, from (' . $current . ') to (' . $location . ').'); }
/** * Adds an entry to the list of breadcrumb links data */ public function addCrumb($name = '', $link = '', $title = '', $params = array()) { $key = Sanitize::toKey($name); $name = Sanitize::toName($name); $link = Sanitize::toPath($link); $title = Sanitize::toTitle($title); if (!empty($key) && !empty($link)) { $crumbs = $this->get('crumbs', array()); $crumbs[$key] = array_merge(array('name' => $name, 'link' => $link, 'title' => $title), $params); $this->set('crumbs', $crumbs); } }
/** * Adds col BETWEEN( min AND max ) to the WHERE clause */ public function between($column = '', $min = null, $max = null, $next = '') { if (!empty($column) && !is_null($min) && !is_null($max)) { $column = $this->_clause($column); $values = array($min, $max); $next = strtoupper(trim($next)); $keys = array(); foreach ($values as $value) { $key = ':val' . $this->_num; $keys[] = $key; $this->_data['query'][$key] = Sanitize::toString($value); $this->_num++; } $this->_maps['where'][$column] = $this->_clause('(%s BETWEEN ' . implode(' AND ', $keys) . ') ' . $next); } return $this; }
/** * Get folder items list (recursive) */ public function getRecursiveList() { $path = $this->getPath(); $output = array(); if (is_dir($path)) { $dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); $items = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST); foreach ($items as $item) { $output[] = Sanitize::toPath($item->getRealPath()); } } return $output; }
/** * Removes an injected object instance if available */ public function removeObject($name = '') { $name = Sanitize::toAlnum($name); if (!empty($name) && array_key_exists($name, $this->_objects)) { unset($this->_objects[$name]); } }
/** * Copy existing file to another location */ public function copy($newpath = '') { $path = $this->getPath(); $newpath = Sanitize::toPath($newpath); $parent = dirname($newpath); $output = false; if (is_file($path) && !empty($newpath)) { if (is_dir($parent) || mkdir($parent, 0777, true)) { $strin = fopen($path, "rb"); $strout = fopen($newpath, "wb"); $output = stream_copy_to_stream($strin, $strout); fclose($strin); fclose($strout); } } return $output; }
/** * Save final image */ public function save($file = '', $quality = 80) { $file = Sanitize::toPath($file); $folder = dirname($file); $saved = false; if (!empty($file) && $this->img_source !== null) { if (is_dir($folder) || mkdir($folder, 0777, true)) { @imagealphablending($this->img_source, false); @imagesavealpha($this->img_source, true); if ($this->img_type === IMAGETYPE_JPEG) { $saved = @imagejpeg($this->img_source, $file, $quality); } if ($this->img_type === IMAGETYPE_GIF) { $saved = @imagegif($this->img_source, $file); } if ($this->img_type === IMAGETYPE_PNG) { $saved = @imagepng($this->img_source, $file); } } } @imagedestroy($this->img_source); $this->img_source = null; return $saved; }
/** * Checks if a SERVER key is available, or use default value */ private static function _server($key = '', $default = '') { $key = strtoupper(trim($key)); $default = trim($default); if (array_key_exists($key, $_SERVER)) { return Sanitize::toText($_SERVER[$key]); } return $default; }
/** * Parse FORM encoded data */ private function _parseForm() { if (!empty($this->boundary)) { $chunks = @preg_split('/[\\-]+' . $this->boundary . '(\\-\\-)?/', $this->input, -1, PREG_SPLIT_NO_EMPTY); $request = array(); $files = array(); $nd = 0; $nf = 0; if (is_array($chunks)) { foreach ($chunks as $index => $chunk) { $chunk = ltrim($chunk, "-\r\n\t\\s "); $lines = explode("\r\n", $chunk); $levels = ''; $name = ''; $file = ''; $type = ''; $value = ''; $path = ''; $copy = false; // skip empty chunks if (empty($chunk) || empty($lines)) { continue; } // extract name/filename if (strpos($lines[0], 'Content-Disposition') !== false) { $line = $this->_line(array_shift($lines)); $name = Utils::getValue(@$line['name'], '', true); $file = Utils::getValue(@$line['filename'], '', true); } // extract content-type if (strpos($lines[0], 'Content-Type') !== false) { $line = $this->_line(array_shift($lines)); $type = Utils::getValue(@$line['content'], '', true); } // rebuild value $value = trim(implode("\r\n", $lines)); // FILES data if (!empty($type)) { // check if file extension is in skip list if (in_array(Sanitize::toExtension($file), $this->skip)) { continue; } // move file data to temporary file on server if (!empty($value)) { $path = str_replace('\\', '/', sys_get_temp_dir() . '/php' . substr(sha1(rand()), 0, 6)); $copy = file_put_contents($path, $value); } // extract multi-level array structure from the property name if (preg_match('/(\\[.*?\\])$/', $name, $tmp)) { $name = str_replace($tmp[1], '', $name); $levels = preg_replace('/\\[\\]/', '[' . $nf . ']', $tmp[1]); } // build final array keys to be parsed $files[$name . '[name]' . $levels] = $file; $files[$name . '[type]' . $levels] = $type; $files[$name . '[tmp_name]' . $levels] = $path; $files[$name . '[error]' . $levels] = !empty($copy) ? 0 : UPLOAD_ERR_NO_FILE; $files[$name . '[size]' . $levels] = !empty($copy) ? filesize($path) : 0; $nf++; } else { $name = preg_replace('/\\[\\]/', '[' . $nd . ']', $name); $request[$name] = $value; $nd++; } } // finalize arrays $_REQUEST = array_merge($_GET, $this->_data($request)); $_FILES = $this->_data($files); return true; } } return false; }
/** * Get the HTTP protocol version */ public static function getProtocol() { $value = Utils::getValue(@$_SERVER['SERVER_PROTOCOL'], '', true); $value = Sanitize::toTitle($value); return $value; }
/** * Decodes string data for a column */ public function decodeType($row = array(), $column = '') { if (!empty($column) && array_key_exists($column, $row)) { $row[$column] = Sanitize::toType($row[$column]); } return $row; }