/** * Apply all of the callbacks for this filter. * * @param mixed $data * @param int $offset * @return mixed * @throws \TypeError */ public function applyCallbacks($data = null, int $offset = 0) { if ($offset === 0) { if (\is_null($data)) { return parent::applyCallbacks($data, 0); } elseif (!\is_array($data)) { throw new \TypeError(\sprintf('Expected an array of booleans (%s).', $this->index)); } if (!\is1DArray($data)) { throw new \TypeError(\sprintf('Expected a 1-dimensional array (%s).', $this->index)); } foreach ($data as $key => $val) { if (\is_array($val)) { throw new \TypeError(\sprintf('Unexpected array at index %s (%s).', $key, $this->index)); } if (\is_null($data) || $data === '') { $data[$key] = null; } else { $data[$key] = !empty($val); } } return parent::applyCallbacks($data, 0); } return parent::applyCallbacks($data, $offset); }
/** * Apply all of the callbacks for this filter. * * @param mixed $data * @param int $offset * @return mixed * @throws \TypeError */ public function applyCallbacks($data = null, int $offset = 0) { if ($offset === 0) { if (\is_null($data)) { return parent::applyCallbacks([], 0); } elseif (!\is_array($data)) { throw new \TypeError(\sprintf('Expected an array of string (%s).', $this->index)); } if (!\is1DArray($data)) { throw new \TypeError(\sprintf('Expected a 1-dimensional array (%s).', $this->index)); } foreach ($data as $key => $val) { if (\is_array($val)) { throw new \TypeError(\sprintf('Expected a string at index %s (%s).', $key, $this->index)); } if (\is_null($val)) { $data[$key] = ''; } elseif (\is_numeric($val)) { $data[$key] = (string) $val; } elseif (!\is_string($val)) { throw new \TypeError(\sprintf('Expected a string at index %s (%s).', $key, $this->index)); } } return parent::applyCallbacks($data, 0); } return parent::applyCallbacks($data, $offset); }
/** * This interrupts requests if all else fails. * @param string[] ...$args * @return void * @throws CustomPageNotFoundException */ public function routeNotFound(...$args) { if (!\is1DArray($args)) { throw new CustomPageNotFoundException(\__('Invalid arguments')); } $dirs = $args; $file = \array_pop($dirs); // First: Do we have a custom page at this endpoint? try { if ($this->serveCustomPage($file, $dirs)) { return; } } catch (CustomPageNotFoundException $ex) { $this->log('Custom page not found', LogLevel::INFO, ['cabin' => $this->cabin, 'dirs' => $dirs, 'exception' => \Airship\throwableToArray($ex), 'file' => $file]); } // Second: Is there a redirect at this endpoint? try { $path = \implode('/', $args); if ($this->pages->serveRedirect($path)) { return; } } catch (RedirectException $ex) { $this->log('Redirect missed', LogLevel::INFO); } \http_response_code(404); // Finally: Return a 4o4 $this->lens('404'); return; }
/** * Apply all of the callbacks for this filter. * * @param mixed $data * @param int $offset * @return mixed * @throws \TypeError */ public function applyCallbacks($data = null, int $offset = 0) { if ($offset === 0) { if (\is_null($data)) { return parent::applyCallbacks($data, 0); } elseif (!\is_array($data)) { throw new \TypeError(\sprintf('Expected an array of floats (%s).', $this->index)); } if (!\is1DArray($data)) { throw new \TypeError(\sprintf('Expected a 1-dimensional array (%s).', $this->index)); } foreach ($data as $key => $val) { if (\is_array($val)) { throw new \TypeError(\sprintf('Expected a float at index %s (%s).', $key, $this->index)); } if (\is_int($val) || \is_float($val)) { $data[$key] = (double) $val; } elseif (\is_null($val) || $val === '') { $data[$key] = (double) $this->default; } elseif (\is_string($val) && \is_numeric($val)) { $data[$key] = (double) $val; } else { throw new \TypeError(\sprintf('Expected a float at index %s (%s).', $key, $this->index)); } } return parent::applyCallbacks($data, 0); } return parent::applyCallbacks($data, $offset); }
/** * Channel constructor. * * @param object $parent (Continuum or Keyggdrasil) * @param string $name * @param array $config * @throws \TypeError */ public function __construct($parent, string $name, array $config = []) { if ($parent instanceof Keyggdrasil || $parent instanceof Continuum) { $this->parent = $parent; } if (!\is1DArray($config['urls'])) { throw new \TypeError(\trk('errors.type.expected_1d_array')); } // The channel should be signing responses at the application layer: $this->publicKey = new SignaturePublicKey(\Sodium\hex2bin($config['publickey'])); $this->name = $name; foreach (\array_values($config['urls']) as $index => $url) { if (!\is_string($url)) { throw new \TypeError(\trk('errors.type.expected_string_array', \gettype($url), $index)); } $this->urls[] = $url; } }
/** * Returns true if every member of an array is a 1-dimensional array. * * @param array $source * @param bool $allow1D Permit non-array children? * @param bool $constantTime Don't exit early * @return bool */ function is2DArray(array $source, bool $allow1D = false, bool $constantTime = false) : bool { $ret = !empty($source); foreach ($source as $row) { if (!\is_array($row)) { if ($allow1D) { continue; } if (!$constantTime) { return false; } $ret = false; } if (!\is1DArray($row)) { if (!$constantTime) { return false; } $ret = false; } } return $ret; }
/** * Fetch a single result -- useful for SELECT COUNT() queries * * @param string $statement * @param array $params * @return mixed * @throws DBAlert\QueryError * @throws \TypeError */ public function single(string $statement, array $params = []) { if (!\is1DArray($params)) { throw new \TypeError(\trk('errors.database.array_passed')); } $stmt = $this->pdo->prepare($statement); $stmt->execute($params); return $stmt->fetchColumn(0); }
/** * @route ajax/authors_blog_series */ public function getSeriesForAuthor() { $auth_bp = $this->blueprint('Author'); $blog_bp = $this->blueprint('Blog'); if (IDE_HACKS) { $db = \Airship\get_database(); $auth_bp = new Author($db); $blog_bp = new Blog($db); } if (empty($_POST['author'])) { \Airship\json_response(['status' => 'ERROR', 'message' => \__('No author selected.')]); } $authorId = (int) ($_POST['author'] ?? 0); if (!$this->isSuperUser()) { $authors = $auth_bp->getAuthorIdsForUser($this->getActiveUserId()); if (!\in_array($authorId, $authors)) { \Airship\json_response(['status' => 'ERROR', 'message' => \__('You do not have permission to access this author\'s posts.')]); } } $existing = $_POST['existing'] ?? []; if (!\is1DArray($existing)) { \Airship\json_response(['status' => 'ERROR', 'message' => \__('One-dimensional array expected')]); } foreach ($existing as $i => $e) { $existing[$i] = (int) $e; } $response = ['status' => 'OK']; if (!empty($_POST['add'])) { $add = (int) ($_POST['add'] ?? 0); $newSeries = $blog_bp->getSeries($add); if (!empty($newSeries)) { if ($newSeries['author'] === $authorId) { $existing[] = $add; $response['new_item'] = $this->getLensAsText('ajax/bridge_blog_series_item', ['item' => ['name' => $newSeries['name'], 'series' => $newSeries['seriesid'], 'data-id' => null]]); } } } $existing = $blog_bp->getAllSeriesParents($existing); $series = $blog_bp->getSeriesForAuthor($authorId, $existing); $response['options'] = $this->getLensAsText('ajax/bridge_blog_series_select_series', ['items' => $series]); \Airship\json_response($response); }
/** * We're going to view a page's history * * @route pages/{string}/history/view/{string} * @param string $cabin * @param string $uniqueId */ public function pageHistoryView(string $cabin, string $uniqueId) { $page = []; $version = []; $path = $this->determinePath($cabin); if (!\is1DArray($_GET)) { \Airship\redirect($this->airship_cabin_prefix . '/pages/' . \trim($cabin, '/')); } $cabins = $this->getCabinNamespaces(); if (!\in_array($cabin, $cabins)) { \Airship\redirect($this->airship_cabin_prefix); } $this->setTemplateExtraData($cabin); if (!$this->can('read')) { \Airship\redirect($this->airship_cabin_prefix); } try { $version = $this->pg->getPageVersionByUniqueId($uniqueId); if (!empty($version['metadata'])) { $version['metadata'] = \json_decode($version['metadata'], true); } $page = $this->pg->getPageById($version['page']); } catch (CustomPageNotFoundException $ex) { \Airship\redirect($this->airship_cabin_prefix . '/pages/' . \trim($cabin, '/')); } $prevUnique = $this->pg->getPrevVersionUniqueId((int) $version['page'], $version['versionid']); $nextUnique = $this->pg->getNextVersionUniqueId((int) $version['page'], $version['versionid']); $latestId = $this->pg->getLatestVersionId((int) $version['page']); $this->lens('pages/page_history_view', ['cabins' => $cabins, 'pageinfo' => $page, 'version' => $version, 'latestId' => $latestId, 'prev_url' => $prevUnique, 'next_url' => $nextUnique, 'dir' => $path, 'cabin' => $cabin, 'pathinfo' => \Airship\chunk($path)]); }