public static function method($name, $definition = null) { if (is_array($name)) { foreach ($name as $method_name => $method_definition) { if (is_callable($method_definition)) { $method_definition = ['validate' => $method_definition]; } if (empty($method_definition['validate']) || !is_callable($method_definition['validate'])) { continue; } $method_definition['key'] = "core.check.error.{$method_name}"; $method_definition['message'] = Filter::with("core.check.message.{$method_definition['key']}", @$method_definition['message'] ?: 'Field not valid.'); static::$methods[$method_name] = (object) $method_definition; } } else { if (is_callable($definition)) { $definition = ['validate' => $definition]; } if (empty($definition['validate']) || !is_callable($definition['validate'])) { return; } $methods['key'] = "core.check.error.{$name}"; $methods['message'] = Filter::with("core.check.message.{$methods['key']}", @$methods['message'] ?: 'Field not valid.'); static::$methods[$name] = (object) $definition; } }
public static function exec($query, $params = []) { $query = Filter::with('core.sql.query', $query); $statement = static::prepare($query); Event::trigger('core.sql.query', $query, $params, !!$statement); static::$last_exec_success = $statement && $statement->execute($params); return $statement; }
public static function buildProjector() { // Build Projector if ($projection_fields = Filter::with(["api." . get_called_class() . ".getProjectionFields", "api.resource.getProjectionFields"], Request::get('fields'))) { $projection_fields = preg_split('~\\s*,\\s*~', $projection_fields); // Ensure primary key presence array_unshift($projection_fields, static::$PKEY); // Remove duplicate entries $projection_fields = array_unique($projection_fields); $projector = function ($element) use($projection_fields) { // An unique placehodler to handle `field_get` errors. $unique_placeholder = '§@°§___CO(' . time() . ')RE___§°@§'; // Get an array field by dot.notation $field_get = function ($x, $path, $default = null) { $current = (array) $x; $p = strtok($path, '.'); while (isset($current[$p])) { if (!($p = strtok('.'))) { return $current; } $current = (array) $current[$p]; } return $default; }; // Set an array field pointed by dot.notation $field_set = function (&$x, $path, $value) { $current = $x; $p = strtok($path, '.'); while ($p !== false) { $p = strtok('.'); if (!isset($current[$p])) { $current[$p] = []; } $current = (array) $current[$p]; } return $current; }; $element = (object) $element; $obj = []; foreach ($projection_fields as $field_path) { if (isset($element->{$field_path})) { $obj[$field_path] = $element->{$field_path}; } else { if (($value = $field_get($element, $field_path, $unique_placeholder)) !== $unique_placeholder) { var_dump($value, $element, $field_path, $unique_placeholder); $field_set($obj, $field_path, $value); } } } return (object) $obj; }; } else { $projector = function ($x) { return $x; }; } return $projector; }
public static function viaJavaScript($url, $parent = false) { if ($link = Filter::with('core.redirect', $url)) { Response::type('text/html'); Response::add('<script>' . ($parent ? 'parent.' : '') . 'location.href="', addslashes($link), '"</script>'); Response::send(); exit; } }
public static function fromSQL($resource, $sql, $params = []) { $count = SQL::value(preg_replace(['(SELECT(.+)FROM)i', '(LIMIT\\s+\\d+\\s+)i', '(OFFSET\\s+\\d+\\s+)i'], ['SELECT COUNT(1) FROM', '', ''], $sql . ' '), $params, 0); $page = Filter::with(["api.{$resource}.page", "api.page"], max(1, Request::get('page', 1))); $limit = Filter::with(["api.{$resource}.limit", "api.limit"], max(1, Request::get('limit', 10))); $page = min($page, ceil($count / $limit)); $offset = max(0, $page - 1); $sql = "{$sql} LIMIT {$limit} OFFSET {$offset}"; return static::wrap($resource, SQL::each($sql, $params), $page, $limit, $count); }
/** * Returns the remote UserAgent * * @return string */ public static function UA() { return Filter::with('core.request.UA', strtolower(empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'])); }
/** * Returns the current request URI. * * @param boolean $relative If true, trim the URI relative to the application index.php script. * * @return string */ public static function URI($relative = true) { // On some web server configurations PHP_SELF is not populated. $self = filter_input(INPUT_SERVER, 'SCRIPT_NAME') ?: filter_input(INPUT_SERVER, 'PHP_SELF'); // Search REQUEST_URI in $_SERVER $serv_uri = filter_input(INPUT_SERVER, 'PATH_INFO') ?: (filter_input(INPUT_SERVER, 'ORIG_PATH_INFO') ?: filter_input(INPUT_SERVER, 'REQUEST_URI')); $uri = strtok($serv_uri, '?'); $uri = $uri == $self ? '/' : $uri; // Add a filter here, for URL rewriting $uri = Filter::with('core.request.URI', $uri); $uri = rtrim($uri, '/'); if ($relative) { $base = rtrim(dirname($self), '/'); $uri = str_replace($base, '', $uri); } return $uri ?: '/'; }
/** * Render view when casted to a string * @return string The rendered view */ public function __toString() { return Filter::with('core.view', static::$handler->render($this->options['template'], $this->options['data'])); }
/** * Run one of the mapped callbacks to a passed HTTP Method. * @param array $args The arguments to be passed to the callback * @param string $method The HTTP Method requested. * @return array The callback response. */ public function run(array $args, $method = 'get') { $method = strtolower($method); $append_echoed_text = Options::get('core.route.append_echoed_text', true); static::trigger('start', $this, $args, $method); // Call direct befores if ($this->befores) { // Reverse befores order foreach (array_reverse($this->befores) as $mw) { static::trigger('before', $this, $mw); Event::trigger('core.route.before', $this, $mw); ob_start(); $mw_result = call_user_func($mw); $raw_echoed = ob_get_clean(); if ($append_echoed_text) { Response::add($raw_echoed); } if (false === $mw_result) { return ['']; } else { Response::add($mw_result); } } } $callback = is_array($this->callback) && isset($this->callback[$method]) ? $this->callback[$method] : $this->callback; if (is_callable($callback) || is_a($callback, "View")) { Response::type(Options::get('core.route.response_default_type', Response::TYPE_HTML)); ob_start(); if (is_a($callback, "View")) { // Get the rendered view $view_results = (string) $callback; } else { $view_results = call_user_func_array($callback, $args); } $raw_echoed = ob_get_clean(); if ($append_echoed_text) { Response::add($raw_echoed); } Response::add($view_results); } // Apply afters if ($this->afters) { foreach ($this->afters as $mw) { static::trigger('after', $this, $mw); Event::trigger('core.route.after', $this, $mw); ob_start(); $mw_result = call_user_func($mw); $raw_echoed = ob_get_clean(); if ($append_echoed_text) { Response::add($raw_echoed); } if (false === $mw_result) { return ['']; } else { Response::add($mw_result); } } } static::trigger('end', $this, $args, $method); Event::trigger('core.route.end', $this); return [Filter::with('core.route.response', Response::body())]; }
public static function body($setBody = null) { if ($setBody) { static::$payload = [$setBody]; } return Filter::with('core.response.body', is_array(static::$payload) ? implode('', static::$payload) : static::$payload); }
public function build() { return \Filter::with('core.email.source', $this->head() . "\r\n" . $this->body()); }
public function exec($query, $params = [], $pdo_params = []) { if (!$this->connection()) { return false; } if (false == is_array($params)) { $params = (array) $params; } $query = Filter::with('core.sql.query', $query); if ($statement = $this->prepare($query, $pdo_params)) { SQL::trigger('query', $query, $params, (bool) $statement); Event::trigger('core.sql.query', $query, $params, (bool) $statement); foreach ($params as $key => $val) { $type = PDO::PARAM_STR; if (is_bool($val)) { $type = PDO::PARAM_BOOL; } elseif (is_null($val)) { $type = PDO::PARAM_NULL; } elseif (is_int($val)) { $type = PDO::PARAM_INT; } // bindValue need a 1-based numeric parameter $statement->bindValue(is_numeric($key) ? $key + 1 : ':' . $key, $val, $type); } } else { $error = $this->connection['pdo']->errorInfo(); SQL::trigger('error', $error[2], $query, $params, $error); Event::trigger('core.sql.error', $error[2], $query, $params, $error); return false; } $this->last_exec_success = $statement && $statement->execute(); return $statement; }