static function get() { $get = $_GET; if ($get['_json']) { $get = \Grithin\Arrays::merge($get['_json'], json_decode((string) $get['_json'], true)); } return $get; }
function __construct($options = []) { $this->options = Arrays::merge(['input_timezone' => 'UTC', 'target_timezone' => 'UTC'], $options); }
/** @param options { mimic:<return paths prefixes with @:dir>, prefix:<prefix returned file paths with>, filter:<function to filter returned paths>, ghost:<don't error on non-existent>} */ static function scan($dir, $options = []) { if (!$options['prefix'] && $options['mimic']) { $options['prefix'] = $dir; } if (isset($options['maxDepth']) && $options['maxDepth'] == 0) { return []; } $realPath = realpath($dir); if (!$realPath) { if ($options['ghost']) { return []; } else { Debug::toss('No such directory'); } } $realPath .= '/'; $files = array(); foreach (scandir($realPath) as $v) { if ($v != '.' && $v != '..') { if (is_dir($realPath . $v)) { $newOptions = array_merge($options, ['prefix' => $options['prefix'] . $v . '/']); if (isset($newOptions['maxDepth'])) { $newOptions['maxDepth']--; } $newFiles = self::scan($realPath . $v, $newOptions); $files = Arrays::merge($files, $newFiles); } else { if (!$options['filter'] || $options['filter']($options['prefix'] . $v)) { $files[] = $options['prefix'] . $v; } } } } return $files; }
protected function setRequestOptions($url, $method, $vars, $files = null) { $purl = parse_url($url); if ($purl['scheme'] == 'https') { curl_setopt($this->request, CURLOPT_PORT, empty($purl['port']) ? 443 : $purl['port']); if ($this->validate_ssl) { curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, true); } elseif ($this->validate_ssl_host) { curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 0); } else { curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 2); } } $method = strtoupper($method); switch ($method) { case 'HEAD': curl_setopt($this->request, CURLOPT_NOBODY, true); break; case 'GET': curl_setopt($this->request, CURLOPT_HTTPGET, true); break; case 'PUT': curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, "PUT"); $toPost = true; break; case 'POST': curl_setopt($this->request, CURLOPT_POST, true); $toPost = true; break; default: curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method); } curl_setopt($this->request, CURLOPT_URL, $url); if ($files || !empty($vars)) { if (!$toPost) { throw new \InvalidArgumentException('POST-vars may only be set for a POST or PUT Request.'); } if (!is_array($vars)) { curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars); } elseif ($files) { foreach ($files as &$file) { if ($file[0] != '@') { $file = '@' . $file; } } unset($file); curl_setopt($this->request, CURLOPT_POSTFIELDS, Arrays::merge($files, $vars)); } else { curl_setopt($this->request, CURLOPT_POSTFIELDS, Http::buildQuery($vars)); } } elseif ($toPost) { throw new \InvalidArgumentException('POST-vars must be set for a POST-Request.'); } # Set some default CURL options curl_setopt($this->request, CURLOPT_HEADER, true); curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent); curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout); if ($this->cookie_file) { curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file); curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file); } /* relative paths fix, see requestFix if ($this->follow_redirects){ curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true); } */ if ($this->referrer) { curl_setopt($this->request, CURLOPT_REFERER, $this->referrer); } # Set any custom CURL options foreach ($this->options as $option => $value) { curl_setopt($this->request, $option, $value); } }
function checkUniqueKeys($value, $table, $type, $id = null) { $indices = $this->db->indices($table); foreach ($indices as $name => $key) { if ($key['unique']) { if ($name != 'PRIMARY') { foreach ($key['columns'] as $column) { if (!isset($this->input->in[$column]) || $this->input->in[$column] === null) { //null indices can overlap continue 2; } } $where = Arrays::extract($key['columns'], $this->input->in); if ($type != 'create' && $id) { $where['id?<>'] = $id; } if ($this->db->check($table, $where)) { Debug::toss(['type' => 'record_not_unique', 'detail' => $key['columns']], 'InputException'); } } } } }
function values($table_name = '', $input = null) { if ($input === null) { $input = $this->input->in; } $columns = $this->column_names($table_name); $values = \Grithin\Arrays::extract($columns, $input, $v = [], false); return $values; }
/** @param additional additional fields to merge with where on insert */ protected function id($table, $where, $additional = null) { $sql = $this->select($table, $where, 'id'); $id = $this->value($sql); if (!$id) { if ($additional) { $where = Arrays::merge($where, $additional); } $id = $this->insert($table, $where); } return $id; }
/** @param mimes array of either whole mimes "part/part", or the last part of the mime "part" @note To get mime: $mime = \Grithin\File::mime($_FILES[$name]['tmp_name']); */ function mime($v, $mimes) { $mimes = Arrays::toArray($mimes); foreach ($mimes as $matchMime) { if (preg_match('@' . preg_quote($matchMime) . '$@', $v)) { return $v; } } self::error(); }
static function specialSyntaxKeys($string) { if (preg_match('@^([^\\[]+)((\\[[^\\]]*\\])+)$@', $string, $match)) { //match[1] = array name, match[2] = all keys //get names of all keys preg_match_all('@\\[([^\\]]*)\\]@', $match[2], $matches); //add array name to beginning of keys list array_unshift($matches[1], $match[1]); //clear out empty key items Arrays::remove($matches[1], '', true); return $matches[1]; } }
function apply_rules($field, $rules) { try { $value = Arrays::got($this->input, $field); } catch (\Exception $e) { # Field wasn't found $value = null; } foreach ($rules as $rule) { # handle continuity if ($rule['flags']['continuity'] && $this->field_errors($field)) { Debug::toss(['type' => 'continuity']); } elseif ($rule['flags']['full_continuity'] && $this->errors) { Debug::toss(['type' => 'continuity']); } # resolve and try function $fn = $this->resolve_fn($rule['fn_path']); $params = array_merge([$value], $rule['params']); # including `input` and `output` as referenced arrays doubles the time of a do-nothing callback $params[] = ['field' => $field, 'instance' => $this]; try { $value = call_user_func_array($fn, $params); if ($rule['flags']['not']) { Debug::toss(['type' => 'not']); } } catch (\Exception $e) { $error = self::parse_exception($e); if ($rule['flags']['not'] && $error['type'] != 'not') { # potentially, the not flag caused the Error continue; } if (!$rule['flags']['optional']) { $error['rule'] = $rule; $this->error($error, $field); } if ($rule['flags']['break']) { $error['type'] = 'break'; Debug::toss($error); } if ($rule['flags']['break_all']) { $error['type'] = 'break_all'; Debug::toss($error); } } } return $value; }
static function getAttributes($tag, $attributes) { $attributes = Arrays::toArray($attributes); $collected = array(); foreach ($attributes as $attribute) { preg_match('@' . $attribute . '=([\'"]).+?\\1@i', $tag, $match); if ($match) { $collected[] = $match[0]; } } return $collected; }
static function explode($separator, $string) { $array = explode($separator, $string); Arrays::remove($array); return array_values($array); }
static function prefixWithRule($rule, $rules = []) { $rules = \Grithin\Arrays::toArray($rules); array_unshift($rules, $rule); return $rules; }