public function run($function, array $params = array()) { $hooks = Utils::get('ThinHooks'); $res = null; if (Arrays::exists($function, $hooks)) { if (Arrays::exists('before', $hooks[$function])) { $action = $hooks[$function]['before']; if (is_callable($action, true, $before)) { $res = $before(); } } if (null === $res) { $res = ''; } $res .= call_user_func_array($function, $params); if (Arrays::exists('after', $hooks[$function])) { $action = $hooks[$function]['after']; if (is_callable($action, true, $after)) { $res .= $after(); } } return $res; } else { return call_user_func_array($function, $params); } }
/** * Static method for instantiating a singleton object. * * @return object */ public static final function instance() { $className = get_called_class(); if (!Arrays::exists(static::$instances[$className])) { static::$instances[$className] = new $className(func_get_args()); } return static::$instances[$className]; }
/** * @param string $name * @return mixed */ public static function get($name, $default = null) { if (Arrays::exists($name, $_COOKIE)) { if (isset($_COOKIE[$name])) { return $_COOKIE[$name]; } } return $default; }
public function isPost($except = []) { if (!empty($_POST) && !empty($except)) { foreach ($except as $key) { if (Arrays::exists($key, $_POST)) { unset($_POST[$key]); } } } return !empty($_POST); }
public static function flush($queue) { foreach (static::$flushers[$queue] as $flusher) { if (!Arrays::exists($queue, static::$queued)) { continue; } foreach (static::$queued[$queue] as $key => $payload) { array_unshift($payload, $key); call_user_func_array($flusher, $payload); } } }
private static function next($url, $data) { $tab = json_decode(file_get_contents($url), true); $data = array_merge($data, $tab['data']); if (Arrays::exists('next', $tab)) { $next = $tab['next']; if (isset($next)) { return static::next($next, $data); } } return $data; }
public function get($key) { if ($this->useCache && Arrays::exists($key, $this->cache)) { return $this->cache[$key]; } $v = dba_fetch($key, $this->dbHandler); // convert $value = $this->decode($v); // store if ($this->useCache) { $this->cache[$key] = $value; } return $value; }
public function __construct($config) { $session = session('web'); $session->setLanguage($config->getLanguage()); $this->_config = $config; if ($config->getLanguage() == Cms::getOption('default_language')) { $this->_needTranslate = false; } else { $file = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $config->getModule() . DS . 'languages' . DS . $config->getController() . ucFirst($config->getLanguage()) . '.php'; $this->_translation = new languageTranslation(); if (File::exists($file)) { $translation = (include $file); $this->_translation->populate(Arrays::exists($config->getAction(), $translation) ? $translation[$config->getAction()] : $translation); } } }
public static function getCategories() { $collection = array(); $articles = static::getAll(); foreach ($articles as $tmpArticle) { $article = static::getArticle($tmpArticle); $cat = $article->getCategory(); if (!Arrays::exists($cat, $collection)) { $collection[$cat] = 1; } else { $collection[$cat]++; } } ksort($collection); return $collection; }
function getNamedInterval($id = null) { # if $id name is null if ($id === null) { # return the latest interval foreach ($this->intervals as $id => $val) { } $ret = "{$id} = {$val}"; # else if there is a $id name } elseif (Arrays::exists($id, $this->intervals)) { # return the named interval $ret = "{$id} = {$this->intervals[$id]}"; } else { $ret = "{$id} = no value"; } return $ret; }
public function __construct(array $connection) { $required = array('host', 'port', 'secure', 'auth', 'login', 'password', 'debug'); foreach ($required as $field) { if (!Arrays::exists($field, $connection)) { throw new Exception($field . " is mandatory to use this class."); } } // set connection vars $this->host = $connection['host']; $this->port = $connection['port']; $this->secure = $connection['secure']; $this->auth = $connection['auth']; $this->user = $connection['login']; $this->pass = $connection['password']; // set debug mode $this->debugMode = $connection['debug']; }
/** * Add a row to the cart * * @param string|Array $id Unique ID of the item|Item formated as array|Array of items * @param string $name Name of the item * @param int $qty Item qty to add to the cart * @param float $price Price of one item * @param Array $options Array of additional options, such as 'size' or 'color' */ public function add($id, $name = null, $qty = null, $price = null, array $options = array()) { // If the first parameter is an array we need to call the add() function again if (Arrays::is($id)) { // And if it's not only an array, but a multidimensional array, we need to // recursively call the add function if ($this->isMulti($id)) { foreach ($id as $item) { $options = Arrays::exists('options', $item) ? $item['options'] : array(); $this->addRow($item['id'], $item['name'], $item['qty'], $item['price'], $options); } return; } $options = Arrays::exists('options', $id) ? $id['options'] : array(); return $this->addRow($id['id'], $id['name'], $id['qty'], $id['price'], $options); } return $this->addRow($id, $name, $qty, $price, $options); }
public static function auto($sentence, $source = 'fr', $target = 'en') { $key = sha1(serialize(func_get_args())); $res = Data::query('translation', 'key = ' . $key); if (count($res)) { $obj = current($res); return $obj->getSentence(); } $source = Inflector::lower($source); $target = Inflector::lower($target); $url = "http://api.mymemory.translated.net/get?q=" . urlencode($sentence) . "&langpair=" . urlencode($source) . "|" . urlencode($target); $res = dwn($url); $tab = json_decode($res, true); if (Arrays::exists('responseData', $tab)) { if (Arrays::exists('translatedText', $tab['responseData'])) { $translation = $tab['responseData']['translatedText']; $data = array('source' => $source, 'target' => $target, 'key' => $key, 'sentence' => $translation); Data::add('translation', $data); return $translation; } } return $sentence; }
function getVariableName() { $arrBacktrace = debug_backtrace(); //possible 'included' functions $arrInclude = array("include", "include_once", "require", "require_once"); //check for any included/required files. if found, get array of the last included file (they contain the right line numbers) $countBt = count($arrBacktrace); for ($i = $countBt - 1; $i >= 0; $i--) { $arrCurrent = $arrBacktrace[$i]; if (Arrays::exists("function", $arrCurrent) && (Arrays::inArray($arrCurrent["function"], $arrInclude) || 0 != strcasecmp($arrCurrent["function"], "dbug"))) { continue; } $arrFile = $arrCurrent; break; } if (isset($arrFile)) { $arrLines = file($arrFile["file"]); $code = $arrLines[$arrFile["line"] - 1]; //find call to dBug class preg_match('/\\bnew dBug\\s*\\(\\s*(.+)\\s*\\);/i', $code, $arrMatches); return $arrMatches[1]; } return ""; }
public function newInstance($id = null) { if (null === $id) { $obj = new self($this->_entity, $this->_table); $obj = $obj->map(); foreach ($obj->fields() as $field) { if (Arrays::is($obj->_datas['keys'])) { if (Arrays::in($field, $obj->_datas['keys'])) { if (Arrays::exists($field, $obj->_datas['configModel']['relationship'])) { $seg = $obj->_datas['configModel']['relationship'][$field]; $m = $obj->_datas['configModel']['relationship'][$field]; if (null !== $m) { $obj->_datas['foreignFields'][$field] = true; } } } } } return $obj; } else { $obj = new $class(); return $obj->find($id); } }
/** * Parse the url and set server parameters * * @access private * @return bool */ private function parseUrl() { $url = parse_url($this->socketIOUrl); $this->serverPath = $url['path']; $this->serverHost = $url['host']; $this->serverPort = isset($url['port']) ? $url['port'] : null; if (Arrays::exists('scheme', $url) && $url['scheme'] == 'https') { $this->serverHost = 'ssl://' . $this->serverHost; if (!$this->serverPort) { $this->serverPort = 443; } } return true; }
public static function exists($path) { return Arrays::exists($path, $_SESSION); }
public static function has($key) { return Arrays::exists($key, static::$objects); }
public static function mime($extension, $default = 'application/octet-stream') { Config::load('mimes'); $mimes = null !== config::get('mimes') ? config::get('mimes') : []; if (!Arrays::exists($extension, $mimes)) { return $default; } return Arrays::is($mimes[$extension]) ? Arrays::first($mimes[$extension]) : $mimes[$extension]; }
public static function configs($entity, $key, $value = null, $cb = null) { if (!strlen($entity)) { throw new Exception("An entity must be provided to use this method."); } if (!Arrays::exists($entity, static::$configs)) { self::$configs[$entity] = array(); } if (empty($value)) { if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } return isAke(self::$configs[$entity], $key, null); } if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } $reverse = strrev($key); $last = $reverse[0]; if ('s' == $last) { self::$configs[$entity][$key] = $value; } else { if (!Arrays::exists($key . 's', self::$configs[$entity])) { self::$configs[$entity][$key . 's'] = array(); } array_push(self::$configs[$entity][$key . 's'], $value); } return !is_callable($cb) ? true : $cb(); }
public function run($id, $args = array()) { $id = sha1($id . $this->_token); if (Arrays::exists($id, $this->values)) { return call_user_func_array($this->values[$id], $args); } return null; }
public function setGrammar($grammar, $replace) { if (!Arrays::exists($grammar, $this->_grammar)) { $this->_grammar[$grammar] = $replace; } return $this; }
public static function mergeOptions(array $array1, $array2 = null) { if (Arrays::is($array2)) { foreach ($array2 as $key => $val) { if (Arrays::is($array2[$key])) { $array1[$key] = Arrays::exists($key, $array1) && Arrays::isArray($array1[$key]) ? static::mergeOptions($array1[$key], $array2[$key]) : $array2[$key]; } else { $array1[$key] = $val; } } } return $array1; }
protected static function id($name, $attributes) { // If an ID has been explicitly specified in the attributes, we will // use that ID. Otherwise, we will look for an ID in the array of // label names so labels and their elements have the same ID. if (Arrays::exists('id', $attributes)) { return $attributes['id']; } if (Arrays::in($name, static::$labels)) { return $name; } }
/** * Check the version of the given property in the User-Agent. * Will return a float number. (eg. 2_0 will return 2.0, 4.3.1 will return 4.31) * * @param string $propertyName The name of the property. See self::getProperties() array * keys for all possible properties. * @param string $type Either self::VERSION_TYPE_STRING to get a string value or * self::VERSION_TYPE_FLOAT indicating a float value. This parameter * is optional and defaults to self::VERSION_TYPE_STRING. Passing an * invalid parameter will default to the this type as well. * * @return string|float The version of the property we are trying to extract. */ public function version($propertyName, $type = self::VERSION_TYPE_STRING) { if (empty($propertyName)) { return false; } //set the $type to the default if we don't recognize the type if ($type != self::VERSION_TYPE_STRING && $type != self::VERSION_TYPE_FLOAT) { $type = self::VERSION_TYPE_STRING; } $properties = self::getProperties(); // Check if the property exists in the properties array. if (Arrays::exists($propertyName, $properties)) { // Prepare the pattern to be matched. // Make sure we always deal with an array (string is converted). $properties[$propertyName] = (array) $properties[$propertyName]; foreach ($properties[$propertyName] as $propertyMatchString) { $propertyPattern = repl('[VER]', self::VER, $propertyMatchString); // Escape the special character which is the delimiter. $propertyPattern = repl('/', '\\/', $propertyPattern); // Identify and extract the version. preg_match('/' . $propertyPattern . '/is', $this->userAgent, $match); if (!empty($match[1])) { $version = $type == self::VERSION_TYPE_FLOAT ? $this->prepareVersionNo($match[1]) : $match[1]; return $version; } } } return false; }
/** * Get all of the messages from the container for a given key. * * <code> * // Echo all of the messages for the e-mail attribute * echo $messages->get('email'); * * // Format all of the messages for the e-mail attribute * echo $messages->get('email', '<p>:message</p>'); * </code> * * @param string $key * @param string $format * @return array */ public function get($key, $format = null) { $format = null === $format ? $this->format : $format; if (Arrays::exists($key, $this->messages)) { return $this->transform($this->messages[$key], $format); } return array(); }
/** * Determine if a given offset exists. * * @param string $key * @return bool */ public function offsetExists($key) { return Arrays::exists($key, $this->bindings); }
/** * Get the S3 response * * @return object | false */ public function getResponse() { $query = ''; if (sizeof($this->parameters) > 0) { $query = substr($this->uri, -1) !== '?' ? '?' : '&'; foreach ($this->parameters as $var => $value) { if ($value == null || $value == '') { $query .= $var . '&'; } else { $query .= $var . '=' . rawurlencode($value) . '&'; } } $query = substr($query, 0, -1); $this->uri .= $query; if (Arrays::exists('acl', $this->parameters) || Arrays::exists('location', $this->parameters) || Arrays::exists('torrent', $this->parameters) || Arrays::exists('website', $this->parameters) || Arrays::exists('logging', $this->parameters)) { $this->resource .= $query; } } $url = (S3::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri; //var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url); // Basic setup $curl = curl_init(); curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php'); if (S3::$useSSL) { // SSL Validation can now be optional for those with broken OpenSSL installations curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, S3::$useSSLValidation ? 1 : 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, S3::$useSSLValidation ? 1 : 0); if (S3::$sslKey !== null) { curl_setopt($curl, CURLOPT_SSLKEY, S3::$sslKey); } if (S3::$sslCert !== null) { curl_setopt($curl, CURLOPT_SSLCERT, S3::$sslCert); } if (S3::$sslCACert !== null) { curl_setopt($curl, CURLOPT_CAINFO, S3::$sslCACert); } } curl_setopt($curl, CURLOPT_URL, $url); if (S3::$proxy != null && isset(S3::$proxy['host'])) { curl_setopt($curl, CURLOPT_PROXY, S3::$proxy['host']); curl_setopt($curl, CURLOPT_PROXYTYPE, S3::$proxy['type']); if (isset(S3::$proxy['user'], S3::$proxy['pass']) && $proxy['user'] != null && $proxy['pass'] != null) { curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3::$proxy['user'], S3::$proxy['pass'])); } } // Headers $headers = array(); $amz = array(); foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } foreach ($this->headers as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } // Collect AMZ headers for signature foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $amz[] = strtolower($header) . ':' . $value; } } // AMZ headers must be sorted if (sizeof($amz) > 0) { sort($amz); $amz = "\n" . implode("\n", $amz); } else { $amz = ''; } if (S3::hasAuth()) { // Authorization string (CloudFront stringToSign should only contain a date) if ($this->headers['Host'] == 'cloudfront.amazonaws.com') { $headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Date']); } else { $headers[] = 'Authorization: ' . S3::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource); } } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback')); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Request types switch ($this->verb) { case 'GET': break; case 'PUT': case 'POST': // POST only used for CloudFront if ($this->fp !== false) { curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $this->fp); if ($this->size >= 0) { curl_setopt($curl, CURLOPT_INFILESIZE, $this->size); } } elseif ($this->data !== false) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); } else { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); } break; case 'HEAD': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); curl_setopt($curl, CURLOPT_NOBODY, true); break; case 'DELETE': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; default: break; } // Execute, grab errors if (curl_exec($curl)) { $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); } else { $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource); } @curl_close($curl); // Parse body into XML if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) { $this->response->body = simplexml_load_string($this->response->body); // Grab S3 errors if (!in_array($this->response->code, array(200, 204, 206)) && isset($this->response->body->Code, $this->response->body->Message)) { $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message); if (isset($this->response->body->Resource)) { $this->response->error['resource'] = (string) $this->response->body->Resource; } unset($this->response->body); } } // Clean up file resources if ($this->fp !== false && is_resource($this->fp)) { fclose($this->fp); } return $this->response; }
private function upload($field) { $bucket = container()->bucket(); if (Arrays::exists($field, $_FILES)) { $fileupload = $_FILES[$field]['tmp_name']; $fileuploadName = $_FILES[$field]['name']; if (strlen($fileuploadName)) { $tab = explode(".", $fileuploadName); $ext = Inflector::lower(Arrays::last($tab)); return $bucket->data(fgc($fileupload), $ext); } } return null; }
public static function configs($entity, $key, $value = null) { if (!strlen($entity)) { throw new Exception("An entity must be provided to use this method."); } if (!Arrays::exists($entity, static::$configs)) { self::$configs[$entity] = array(); } if (empty($value)) { if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } return isAke(self::$configs[$entity], $key, null); } if (!strlen($key)) { throw new Exception("A key must be provided to use this method."); } self::$configs[$entity][$key] = $value; }