/** * Convenience method for removing property value * @return NULL * @param $key string **/ function offsetunset($key) { if (Base::instance()->visible($this, $key)) { unset($this->{$key}); } else { $this->clear($key); } }
/** * Return weather data based on specified latitude/longitude * @return array|FALSE * @param $latitude float * @param $longitude float **/ function weather($latitude, $longitude) { $fw = Base::instance(); $web = Web::instance(); $query = array('lat' => $latitude, 'lon' => $longitude); $req = $web->request('http://api.openweathermap.org/data/2.5/weather?' . http_build_query($query)); return ($req = $web->request('http://api.openweathermap.org/data/2.5/weather?' . http_build_query($query))) ? json_decode($req['body'], TRUE) : FALSE; }
/** * This construct method was taken from medoo db framework (medoo.in) * * @param array $config configuration */ public function __construct(array $config = array()) { $fw = Base::instance(); $config || ($config = $fw->get('DATABASE')); $this->config = $config + $this->config; if (!$this->config['charset'] && ($charset = $fw->get('ENCODING'))) { $this->config['charset'] = $charset; } $commands = array(); if (isset($this->config['port']) && is_int($this->config['port'] * 1)) { $port = $this->config['port']; } $type = strtolower($this->config['type']); $is_port = isset($port); switch ($type) { case 'mariadb': $type = 'mysql'; case 'mysql': if ($this->config['socket']) { $dsn = $type . ':unix_socket=' . $this->config['socket'] . ';dbname=' . $this->config['name']; } else { $dsn = $type . ':host=' . $this->config['server'] . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->config['name']; } // Make MySQL using standard quoted identifier $commands[] = 'SET SQL_MODE=ANSI_QUOTES'; break; case 'pgsql': $dsn = $type . ':host=' . $this->config['server'] . ($is_port ? ';port=' . $port : '') . ';dbname=' . $this->config['name']; break; case 'sybase': $dsn = 'dblib:host=' . $this->config['server'] . ($is_port ? ':' . $port : '') . ';dbname=' . $this->config['name']; break; case 'oracle': $dbname = $this->config['server'] ? '//' . $this->config['server'] . ($is_port ? ':' . $port : ':1521') . '/' . $this->config['name'] : $this->config['name']; $dsn = 'oci:dbname=' . $dbname . ($this->config['charset'] ? ';charset=' . $this->config['charset'] : ''); break; case 'mssql': $dsn = strstr(PHP_OS, 'WIN') ? 'sqlsrv:server=' . $this->config['server'] . ($is_port ? ',' . $port : '') . ';database=' . $this->config['name'] : 'dblib:host=' . $this->config['server'] . ($is_port ? ':' . $port : '') . ';dbname=' . $this->config['name']; // Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting $commands[] = 'SET QUOTED_IDENTIFIER ON'; break; case 'sqlite': $dsn = $type . ':' . $this->config['file']; $this->config['username'] = null; $this->config['password'] = null; break; } if (!isset($dsn)) { throw new Exception('No database type given :(', 1); } if (in_array($type, explode(' ', 'mariadb mysql pgsql sybase mssql')) && $this->config['charset']) { $commands[] = "SET NAMES '" . $this->config['charset'] . "'"; } $this->pdo = new PDO($dsn, $this->config['username'], $this->config['password'], $this->config['option']); foreach ($commands as $value) { $this->pdo->exec($value); } }
/** * Generate map * @return string **/ function dump() { $fw = Base::instance(); $web = Web::instance(); $out = ''; return ($req = $web->request(self::URL_Static . '?' . array_reduce($this->query, function ($out, $item) { return $out .= ($out ? '&' : '') . urlencode($item[0]) . '=' . urlencode($item[1]); }))) && $req['body'] ? $req['body'] : FALSE; }
/** * Evaluate condition and save test result * @return object * @param $cond bool * @param $text string **/ function expect($cond, $text = NULL) { $out = (bool) $cond; if ($this->level == $out || $this->level == self::FLAG_Both) { $data = array('status' => $out, 'text' => $text, 'source' => NULL); foreach (debug_backtrace() as $frame) { if (isset($frame['file'])) { $data['source'] = Base::instance()->fixslashes($frame['file']) . ':' . $frame['line']; break; } } $this->data[] = $data; } if (!$out && $this->passed) { $this->passed = FALSE; } return $this; }
/** * Receive ping, check if local page is pingback-enabled, verify * source contents, and return XML-RPC response * @return string * @param $func callback * @param $path string **/ function listen($func, $path = NULL) { $fw = Base::instance(); if (PHP_SAPI != 'cli') { header('X-Powered-By: ' . $fw->get('PACKAGE')); header('Content-Type: application/xml; ' . 'charset=' . ($charset = $fw->get('ENCODING'))); } if (!$path) { $path = $fw->get('BASE'); } $web = Web::instance(); $args = xmlrpc_decode_request($fw->get('BODY'), $method, $charset); $options = array('encoding' => $charset); if ($method == 'pingback.ping' && isset($args[0], $args[1])) { list($source, $permalink) = $args; $doc = new DOMDocument('1.0', $fw->get('ENCODING')); // Check local page if pingback-enabled $parts = parse_url($permalink); if ((empty($parts['scheme']) || $parts['host'] == $fw->get('HOST')) && preg_match('/^' . preg_quote($path, '/') . '/' . ($fw->get('CASELESS') ? 'i' : ''), $parts['path']) && $this->enabled($permalink)) { // Check source $parts = parse_url($source); if ((empty($parts['scheme']) || $parts['host'] == $fw->get('HOST')) && ($req = $web->request($source)) && $doc->loadhtml($req['body'])) { $links = $doc->getelementsbytagname('a'); foreach ($links as $link) { if ($link->getattribute('href') == $permalink) { call_user_func_array($func, array($source, $req['body'])); // Success die(xmlrpc_encode_request(NULL, $source, $options)); } } // No link to local page die(xmlrpc_encode_request(NULL, 0x11, $options)); } // Source failure die(xmlrpc_encode_request(NULL, 0x10, $options)); } // Doesn't exist (or not pingback-enabled) die(xmlrpc_encode_request(NULL, 0x21, $options)); } // Access denied die(xmlrpc_encode_request(NULL, 0x31, $options)); }
/** * Wrap-up * @return NULL **/ function __destruct() { if (is_resource($this->data)) { imagedestroy($this->data); $fw = Base::instance(); $path = $fw->get('TEMP') . $fw->hash($fw->get('ROOT') . $fw->get('BASE')) . '.' . $fw->hash($this->file); if ($glob = @glob($path . '*.png', GLOB_NOSORT)) { foreach ($glob as $match) { if (preg_match('/-(\\d+)\\.png/', $match)) { @unlink($match); } } } } }
/** * Instantiate class * @param $onsuspect callback **/ function __construct($onsuspect = NULL) { session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'cleanup')); register_shutdown_function('session_commit'); @session_start(); $fw = Base::instance(); $headers = $fw->get('HEADERS'); if (($ip = $this->ip()) && $ip != $fw->get('IP') || ($agent = $this->agent()) && (!isset($headers['User-Agent']) || $agent != $headers['User-Agent'])) { if (isset($onsuspect)) { $fw->call($onsuspect, array($this)); } else { session_destroy(); $fw->error(403); } } $csrf = $fw->hash($fw->get('ROOT') . $fw->get('BASE')) . '.' . $fw->hash(mt_rand()); $jar = $fw->get('JAR'); if (Cache::instance()->exists(($this->sid = session_id()) . '.@', $data)) { $data['csrf'] = $csrf; Cache::instance()->set($this->sid . '.@', $data, $jar['expire'] ? $jar['expire'] - time() : 0); } }
/** * Instantiate class * @param $host string * @param $port int * @param $scheme string * @param $user string * @param $pw string **/ function __construct($host, $port, $scheme, $user, $pw) { $this->headers = array('MIME-Version' => '1.0', 'Content-Type' => 'text/plain; ' . 'charset=' . Base::instance()->get('ENCODING')); $this->host = $host; if (strtolower($this->scheme = strtolower($scheme)) == 'ssl') { $this->host = 'ssl://' . $host; } $this->port = $port; $this->user = $user; $this->pw = $pw; }
/** * Return TRUE if user agent is a Web bot * @return bool * @param $agent string **/ function isbot($agent = NULL) { if (!isset($agent)) { $agent = Base::instance()->get('AGENT'); } return (bool) preg_match('/(' . self::UA_Bot . ')/i', $agent); }
/** * Instantiate class * @return void * @param $key string **/ function __construct($key = 'basket') { $this->key = $key; @session_start(); Base::instance()->sync('SESSION'); $this->reset(); }
/** * Translate emoji tokens to Unicode font-supported symbols * @return string * @param $str string **/ function emojify($str) { $map = array(':(' => '\\u2639', ':)' => '\\u263a', '<3' => '\\u2665', ':D' => '\\u1f603', 'XD' => '\\u1f606', ';)' => '\\u1f609', ':P' => '\\u1f60b', ':,' => '\\u1f60f', ':/' => '\\u1f623', '8O' => '\\u1f632') + Base::instance()->get('EMOJI'); return $this->translate(str_replace(array_keys($map), array_values($map), $str)); }
/** * Perform validation * We can use method in Base class or any method with full name call but * you need to define a message to override default message * Be aware when use a method */ protected function performValidate($key, &$value) { if (!($filter = $this->_schema['filter'][$key])) { return true; } $validation = Validation::instance(); $moe = Base::instance(); $field = $this->_properties['fields'][$key]; foreach ($filter as $func => $param) { if (is_numeric($func)) { $func = $param; $args = array(); } else { $args = [$param]; } $function = $func; if (method_exists($validation, $func)) { $func = array($validation, $func); } elseif (method_exists($this, $func) || preg_match('/^(' . self::Magic . ')/', $func)) { $func = array($this, $func); } elseif (method_exists($moe, $func)) { $func = array($moe, $func); } array_unshift($args, $value); if (false === ($result = $moe->call($func, $args))) { $this->_messages[] = $validation->message($function, $field, $value, $param); return false; } else { is_bool($result) || ($value = $result); } } return true; }
/** * Convert characters to HTML entities * @return string * @param $str string **/ function esc($str) { if (!$this->special) { $this->special = array('...' => '…', '(tm)' => '™', '(r)' => '®', '(c)' => '©'); } foreach ($this->special as $key => $val) { $str = preg_replace('/' . preg_quote($key, '/') . '/i', $val, $str); } return htmlspecialchars($str, ENT_COMPAT, Base::instance()->get('ENCODING'), FALSE); }
/** * Create sandbox for template execution * @return string * @param $hive array **/ protected function sandbox(array $hive = NULL) { $this->level++; $fw = Base::instance(); $implicit = false; if ($hive === null) { $implicit = true; $hive = $fw->hive(); } if ($this->level < 2 || $implicit) { if ($fw->get('ESCAPE')) { $hive = $this->esc($hive); } if (isset($hive['ALIASES'])) { $hive['ALIASES'] = $fw->build($hive['ALIASES']); } } unset($fw, $implicit); extract($hive); unset($hive); ob_start(); require $this->view; $this->level--; return ob_get_clean(); }
/** * Return a URL/filesystem-friendly version of string * @return string * @param $text string **/ function slug($text) { return trim(strtolower(preg_replace('/([^\\pL\\pN])+/u', '-', trim(strtr(str_replace('\'', '', $text), array('Ǎ' => 'A', 'А' => 'A', 'Ā' => 'A', 'Ă' => 'A', 'Ą' => 'A', 'Å' => 'A', 'Ǻ' => 'A', 'Ä' => 'Ae', 'Á' => 'A', 'À' => 'A', 'Ã' => 'A', 'Â' => 'A', 'Æ' => 'AE', 'Ǽ' => 'AE', 'Б' => 'B', 'Ç' => 'C', 'Ć' => 'C', 'Ĉ' => 'C', 'Č' => 'C', 'Ċ' => 'C', 'Ц' => 'C', 'Ч' => 'Ch', 'Ð' => 'Dj', 'Đ' => 'Dj', 'Ď' => 'Dj', 'Д' => 'Dj', 'É' => 'E', 'Ę' => 'E', 'Ё' => 'E', 'Ė' => 'E', 'Ê' => 'E', 'Ě' => 'E', 'Ē' => 'E', 'È' => 'E', 'Е' => 'E', 'Э' => 'E', 'Ë' => 'E', 'Ĕ' => 'E', 'Ф' => 'F', 'Г' => 'G', 'Ģ' => 'G', 'Ġ' => 'G', 'Ĝ' => 'G', 'Ğ' => 'G', 'Х' => 'H', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ï' => 'I', 'Ĭ' => 'I', 'İ' => 'I', 'Į' => 'I', 'Ī' => 'I', 'Í' => 'I', 'Ì' => 'I', 'И' => 'I', 'Ǐ' => 'I', 'Ĩ' => 'I', 'Î' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Й' => 'J', 'Я' => 'Ja', 'Ю' => 'Ju', 'К' => 'K', 'Ķ' => 'K', 'Ĺ' => 'L', 'Л' => 'L', 'Ł' => 'L', 'Ŀ' => 'L', 'Ļ' => 'L', 'Ľ' => 'L', 'М' => 'M', 'Н' => 'N', 'Ń' => 'N', 'Ñ' => 'N', 'Ņ' => 'N', 'Ň' => 'N', 'Ō' => 'O', 'О' => 'O', 'Ǿ' => 'O', 'Ǒ' => 'O', 'Ơ' => 'O', 'Ŏ' => 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ö' => 'Oe', 'Õ' => 'O', 'Ó' => 'O', 'Ò' => 'O', 'Ô' => 'O', 'Œ' => 'OE', 'П' => 'P', 'Ŗ' => 'R', 'Р' => 'R', 'Ř' => 'R', 'Ŕ' => 'R', 'Ŝ' => 'S', 'Ş' => 'S', 'Š' => 'S', 'Ș' => 'S', 'Ś' => 'S', 'С' => 'S', 'Ш' => 'Sh', 'Щ' => 'Shch', 'Ť' => 'T', 'Ŧ' => 'T', 'Ţ' => 'T', 'Ț' => 'T', 'Т' => 'T', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U', 'Ū' => 'U', 'Ǜ' => 'U', 'Ǚ' => 'U', 'Ù' => 'U', 'Ú' => 'U', 'Ü' => 'Ue', 'Ǘ' => 'U', 'Ǖ' => 'U', 'У' => 'U', 'Ư' => 'U', 'Ǔ' => 'U', 'Û' => 'U', 'В' => 'V', 'Ŵ' => 'W', 'Ы' => 'Y', 'Ŷ' => 'Y', 'Ý' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'З' => 'Z', 'Ż' => 'Z', 'Ž' => 'Z', 'Ж' => 'Zh', 'á' => 'a', 'ă' => 'a', 'â' => 'a', 'à' => 'a', 'ā' => 'a', 'ǻ' => 'a', 'å' => 'a', 'ä' => 'ae', 'ą' => 'a', 'ǎ' => 'a', 'ã' => 'a', 'а' => 'a', 'ª' => 'a', 'æ' => 'ae', 'ǽ' => 'ae', 'б' => 'b', 'č' => 'c', 'ç' => 'c', 'ц' => 'c', 'ċ' => 'c', 'ĉ' => 'c', 'ć' => 'c', 'ч' => 'ch', 'ð' => 'dj', 'ď' => 'dj', 'д' => 'dj', 'đ' => 'dj', 'э' => 'e', 'é' => 'e', 'ё' => 'e', 'ë' => 'e', 'ê' => 'e', 'е' => 'e', 'ĕ' => 'e', 'è' => 'e', 'ę' => 'e', 'ě' => 'e', 'ė' => 'e', 'ē' => 'e', 'ƒ' => 'f', 'ф' => 'f', 'ġ' => 'g', 'ĝ' => 'g', 'ğ' => 'g', 'г' => 'g', 'ģ' => 'g', 'х' => 'h', 'ĥ' => 'h', 'ħ' => 'h', 'ǐ' => 'i', 'ĭ' => 'i', 'и' => 'i', 'ī' => 'i', 'ĩ' => 'i', 'į' => 'i', 'ı' => 'i', 'ì' => 'i', 'î' => 'i', 'í' => 'i', 'ï' => 'i', 'ij' => 'ij', 'ĵ' => 'j', 'й' => 'j', 'я' => 'ja', 'ю' => 'ju', 'ķ' => 'k', 'к' => 'k', 'ľ' => 'l', 'ł' => 'l', 'ŀ' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'л' => 'l', 'м' => 'm', 'ņ' => 'n', 'ñ' => 'n', 'ń' => 'n', 'н' => 'n', 'ň' => 'n', 'ʼn' => 'n', 'ó' => 'o', 'ò' => 'o', 'ǒ' => 'o', 'ő' => 'o', 'о' => 'o', 'ō' => 'o', 'º' => 'o', 'ơ' => 'o', 'ŏ' => 'o', 'ô' => 'o', 'ö' => 'oe', 'õ' => 'o', 'ø' => 'o', 'ǿ' => 'o', 'œ' => 'oe', 'п' => 'p', 'р' => 'r', 'ř' => 'r', 'ŕ' => 'r', 'ŗ' => 'r', 'ſ' => 's', 'ŝ' => 's', 'ș' => 's', 'š' => 's', 'ś' => 's', 'с' => 's', 'ş' => 's', 'ш' => 'sh', 'щ' => 'shch', 'ß' => 'ss', 'ţ' => 't', 'т' => 't', 'ŧ' => 't', 'ť' => 't', 'ț' => 't', 'у' => 'u', 'ǘ' => 'u', 'ŭ' => 'u', 'û' => 'u', 'ú' => 'u', 'ų' => 'u', 'ù' => 'u', 'ű' => 'u', 'ů' => 'u', 'ư' => 'u', 'ū' => 'u', 'ǚ' => 'u', 'ǜ' => 'u', 'ǔ' => 'u', 'ǖ' => 'u', 'ũ' => 'u', 'ü' => 'ue', 'в' => 'v', 'ŵ' => 'w', 'ы' => 'y', 'ÿ' => 'y', 'ý' => 'y', 'ŷ' => 'y', 'ź' => 'z', 'ž' => 'z', 'з' => 'z', 'ż' => 'z', 'ж' => 'zh') + Base::instance()->get('DIACRITICS'))))), '-'); }
/** * Initiate OpenID authentication sequence; Return FALSE on failure * or redirect to OpenID provider URL * @return bool * @param $proxy string * @param $attr array * @param $reqd string|array **/ function auth($proxy = NULL, $attr = array(), array $reqd = NULL) { $fw = Base::instance(); $root = $fw->get('SCHEME') . '://' . $fw->get('HOST'); if (empty($this->args['trust_root'])) { $this->args['trust_root'] = $root . $fw->get('BASE') . '/'; } if (empty($this->args['return_to'])) { $this->args['return_to'] = $root . $_SERVER['REQUEST_URI']; } $this->args['mode'] = 'checkid_setup'; if ($this->url = $this->discover($proxy)) { if ($attr) { $this->args['ns.ax'] = 'http://openid.net/srv/ax/1.0'; $this->args['ax.mode'] = 'fetch_request'; foreach ($attr as $key => $val) { $this->args['ax.type.' . $key] = $val; } $this->args['ax.required'] = is_string($reqd) ? $reqd : implode(',', $reqd); } $var = array(); foreach ($this->args as $key => $val) { $var['openid.' . $key] = $val; } $fw->reroute($this->url . '?' . http_build_query($var)); } return FALSE; }