public function testSort() { $users = $this->users; $users = a::sort($users, 'username', 'asc'); $first = a::first($users); $last = a::last($users); $this->assertEquals('mary', $first['username']); $this->assertEquals('peter', $last['username']); }
<?php // detect handler from 1) query parameter ("?handler="), or fallback to 2) http referer $handler_name = get('handler') ?: a::last(array_filter(explode('/', server::get('http_referer')))); $handler_path = c::get('root.content') . '/smart-submit/' . $handler_name . '.php'; // check honeypot if (get('smart-submit-honeypot') && intval(get('smart-submit-honeypot')) < 2) { die('{"error":"' . (l::get('smart-submit-alarm') ?: 'Anti-spam alarm. Please try again.') . '"}'); } if (file_exists($handler_path)) { require $handler_path; } else { die('{"error":"' . (l::get('smart-submit-missing-handler') ?: 'Error - handler not found. Please contact web site administrator for details.') . '"}'); }
/** * Parses the hashed value from a cookie * and tries to extract the value * * @param string $hash * @return mixed */ protected static function parse($string) { // extract hash and value $parts = str::split($string, '+'); $hash = a::first($parts); $value = a::last($parts); // if the hash or the value is missing at all return null if (empty($hash) or empty($value)) { return null; } // compare the extracted hash with the hashed value if ($hash !== static::hash($value)) { return null; } return $value; }
/** * Parses a string by a set of available methods * * Available methods: * - json * - xml * - url * - query * - php * * @param string $string * @param string $mode * @return string */ static function parse($string, $mode = 'json') { if (is_array($string)) { return $string; } switch ($mode) { case 'json': $result = (array) @json_decode($string, true); break; case 'xml': $result = x::parse($string); break; case 'url': $result = (array) @parse_url($string); break; case 'query': if (url::has_query($string)) { $string = self::split($string, '?'); $string = a::last($string); } @parse_str($string, $result); break; case 'php': $result = @unserialize($string); break; default: $result = $string; break; } return $result; }
/** * Returns one of the started instance * * @param string $id * @return object */ public static function instance($id = null) { return is_null($id) ? a::last(static::$connections) : a::get(static::$connections, $id); }
/** * Get the database table name. * * @return string */ public function table() { if (empty($this->table)) { $class = a::last(Str::split(get_class($this), '\\')); return str_replace('\\', '_', Str::snakecase(Str::plural($class))); } return $this->table; }
function dispatchContent() { $meta = array(); $langSupport = c::get('lang.support'); $translated = c::get('lang.translated'); foreach ($this->contents() as $key => $content) { // split filenames (already without extension) by . $parts = explode('.', $content->name); $countParts = count($parts); $lastPart = a::last($parts); $firstPart = a::first($parts); // home.txt if ($countParts == 1) { // files without a language code // are considered to be the default language file $content->languageCode = c::get('lang.default'); // keep the entire name for the template (i.e. home) $content->template = $content->name; // home.en.txt // myfile.jpg.txt // article.video.txt } else { if ($countParts == 2) { // check for a matching file by the entire name $file = $this->find($content->name); // myfile.jpg.txt if ($file) { // change the filetype $content->type = 'meta'; // files without a language code // are considered to be the default language file $content->languageCode = c::get('lang.default'); $file->meta[$content->languageCode] = $content->variables; // add this to the meta array $meta[] = $file; // home.en.txt // article.video.txt } else { // check for a valid language extension // home.en.txt if ($langSupport && in_array($lastPart, c::get('lang.available', array()))) { // use the first part for the template name (i.e. home) $content->template = $firstPart; // add the language code $content->languageCode = $lastPart; // plain content file with crazy name // article.video.txt } else { // files without a language code // are considered to be the default language file $content->languageCode = c::get('lang.default'); // use the entire name for the template (i.e. home) $content->template = $content->name; } } // myfile.jpg.de.txt // article.video.de.txt // something more absurd } else { if ($countParts > 2) { // check for a valid language extension // myfile.jpg.de.txt // article.video.de.txt if ($langSupport && in_array($lastPart, c::get('lang.available', array()))) { // name without the last part / language code $name = implode('.', array_slice($parts, 0, -1)); // check for a matching file by the new name $file = $this->find($name); // add the language code $content->languageCode = $lastPart; // myfile.jpg.de.txt if ($file) { // change the filetype $content->type = 'meta'; $file->meta[$content->languageCode] = $content->variables; // add this to the meta array $meta[] = $file; // article.video.de.txt } else { // use the already prepared name for the template (i.e. article.video) $content->template = $name; } // something more absurd // article.video.whatever.txt // myfile.something.jpg.txt // or an invalid language code } else { // check for a matching file by the new name $file = $this->find($content->name); // files without a language code // are considered to be the default language file $content->languageCode = c::get('lang.default'); if ($file) { $content->type = 'meta'; $file->meta[$content->languageCode] = $content->variables; // add this to the meta array $meta[] = $file; } else { // use the entire name for the template (i.e. article.video.whatever) $content->template = $content->name; } } } } } } foreach ($meta as $m) { if ($langSupport) { $variables = (array) a::get($m->meta, c::get('lang.default')); if ($translated) { $translation = (array) a::get($m->meta, c::get('lang.current')); $variables = !empty($translation) ? array_merge($variables, $translation) : $variables; } } else { $variables = (array) @a::first($m->meta); } // merge the variables with the file object $m->_ = array_merge($m->_, $variables); } }
/** * Shortcut to attach a where clause with an AND operator. * Check out the where() method docs for additional info. * * @param list * @return object */ public function andWhere() { $args = func_get_args(); $mode = a::last($args); // if there's a where clause mode attribute attached… if (in_array($mode, array('AND', 'OR'))) { // remove that from the list of arguments array_pop($args); } // make sure to always attach the AND mode indicator $args[] = 'AND'; call_user_func_array(array($this, 'where'), func_get_args()); return $this; }
function last() { return a::last($this->_); }
/** * Builder for where and having clauses * * @param array $args Arguments, see where() description * @param string $current Current value (like $this->where) * @return string */ protected function filterQuery($args, $current) { $mode = a::last($args); $result = ''; // if there's a where clause mode attribute attached… if (in_array($mode, array('AND', 'OR'))) { // remove that from the list of arguments array_pop($args); } else { $mode = 'AND'; } switch (count($args)) { case 1: if (is_null($args[0])) { return $current; // ->where('username like "myuser"'); } else { if (is_string($args[0])) { // simply add the entire string to the where clause // escaping or using bindings has to be done before calling this method $result = $args[0]; // ->where(array('username' => 'myuser')); } else { if (is_array($args[0])) { $sql = new SQL($this->database, $this); // simple array mode (AND operator) $result = $sql->values($this->table, $args[0], ' AND ', true, true); } else { if (is_callable($args[0])) { $query = clone $this; call_user_func($args[0], $query); $result = '(' . $query->where . ')'; } } } } break; case 2: // ->where('username like :username', array('username' => 'myuser')) if (is_string($args[0]) && is_array($args[1])) { // prepared where clause $result = $args[0]; // store the bindings $this->bindings($args[1]); // ->where('username like ?', 'myuser') } else { if (is_string($args[0]) && is_string($args[1])) { // prepared where clause $result = $args[0]; // store the bindings $this->bindings(array($args[1])); } } break; case 3: // ->where('username', 'like', 'myuser'); if (is_string($args[0]) && is_string($args[1])) { // validate column $sql = new SQL($this->database, $this); list($table, $column) = $sql->splitIdentifier($this->table, $args[0]); if (!$this->database->validateColumn($table, $column)) { throw new Error('Invalid column ' . $args[0]); } $key = $sql->combineIdentifier($table, $column); // ->where('username', 'in', array('myuser', 'myotheruser')); if (is_array($args[2])) { $predicate = trim(strtoupper($args[1])); if (!in_array($predicate, array('IN', 'NOT IN'))) { throw new Error('Invalid predicate ' . $predicate); } // build a list of bound values $values = array(); $bindings = array(); foreach ($args[2] as $value) { $valueBinding = sql::generateBindingName('value'); $bindings[$valueBinding] = $value; $values[] = $valueBinding; } // add that to the where clause in parenthesis $result = $key . ' ' . $predicate . ' (' . implode(', ', $values) . ')'; $this->bindings($bindings); // ->where('username', 'like', 'myuser'); } else { $predicate = trim(strtoupper($args[1])); if (!in_array($predicate, array('=', '>=', '>', '<=', '<', '<>', '!=', '<=>', 'IS', 'IS NOT', 'BETWEEN', 'NOT BETWEEN', 'LIKE', 'NOT LIKE', 'SOUNDS LIKE', 'REGEXP', 'NOT REGEXP'))) { throw new Error('Invalid predicate/operator ' . $predicate); } $valueBinding = sql::generateBindingName('value'); $bindings[$valueBinding] = $args[2]; $result = $key . ' ' . $predicate . ' ' . $valueBinding; $this->bindings($bindings); } } break; } // attach the where clause if (!empty($current)) { return $current . ' ' . $mode . ' ' . $result; } else { return $result; } }
/** * Finds pages by it's unique URI * * @param mixed $uri Either a single URI string or an array of URIs * @param string $use The field, which should be used (uid or slug) * @return mixed Either a Page object, a Pages object for multiple pages or null if nothing could be found */ public function findByURI() { $args = func_get_args(); if (count($args) == 0) { return false; } else { if (count($args) > 1) { $collection = new Children($this->page); foreach ($args as $uri) { $page = $this->findByURI($uri); if ($page) { $collection->data[$page->id()] = $page; } } return $collection; } else { // get the first argument and remove slashes $uri = trim($args[0], '/'); $array = str::split($uri, '/'); $obj = $this; $page = false; foreach ($array as $p) { $next = $obj->findBy('slug', $p); if (!$next) { break; } $page = $next; $obj = $next->children(); } return ($page and $page->slug() != a::last($array)) ? false : $page; } } }
function last() { $path = self::$path ? self::$path : self::path(); return a::last($path); }
* @var array */ return array('auth' => function () { if (!user::current() || !user::current()->isAdmin()) { redirect::to('plugin/comments/wizard'); } }, 'installed' => function () { if ($this->isInstalled()) { redirect::home(); } }, 'userCanCreate' => function () { $route = plugin('comments')->route(); $hash = a::first($route->arguments()); $page = site()->index()->findBy('hash', $hash); return $page instanceof Page && $page->isVisible(); }, 'userCanRead' => function () { $route = plugin('comments')->route(); $hash = a::first($route->arguments()); $page = site()->index()->findBy('hash', $hash); return $page instanceof Page && $page->isVisible(); }, 'userCanUpdate' => function () { $route = plugin('comments')->route(); $id = a::last($route->arguments()); $comment = comment::find($id); return $comment instanceof Comment && $comment->currentUserCan('update'); }, 'userCanDelete' => function () { $route = plugin('comments')->route(); $id = a::last($route->arguments()); $comment = comment::find($id); return $comment instanceof Comment && $comment->currentUserCan('delete'); });