subString() public static method

Binary-safe substr() implementation
public static subString ( string $str, integer $start, integer | null $length = null ) : string
$str string
$start integer
$length integer | null
return string
Exemplo n.º 1
0
 /**
  * @param string $mimeHeader
  * @return bool
  */
 protected function isViewable(string $mimeHeader) : bool
 {
     $pos = \strpos($mimeHeader, ';');
     if ($pos !== false) {
         $mimeHeader = Util::subString($mimeHeader, 0, $pos);
     }
     return \in_array($mimeHeader, $this->viewableMimeTypes);
 }
Exemplo n.º 2
0
 /**
  * Flatten an array into a DSN string and driver
  * 
  * @param array $dbConf
  * @param string $username
  * @param string $password
  * @return array [$dsn, $driver]
  * @throws DBAlert\DBException
  */
 public static function flattenDSN(array $dbConf, string $username = '', string $password = '') : array
 {
     switch ($dbConf['driver']) {
         case 'mysql':
             $dsn = $dbConf['driver'] . ':';
             if (Util::subString($dbConf['host'], 0, 5) === 'unix:') {
                 $dsn .= 'unix_socket=' . Util::subString($dbConf['host'], 5) . ';';
             } else {
                 $dsn .= 'host=' . $dbConf['host'] . ';';
             }
             if (!empty($dbConf['port'])) {
                 $dsn .= 'port=' . $dbConf['port'] . ';';
             }
             $dsn .= 'dbname=' . $dbConf['database'];
             return [$dsn, $dbConf['driver'], $dbConf['username'] ?? $username, $dbConf['password'] ?? $password];
         case 'pgsql':
             $dsn = $dbConf['driver'] . ':';
             if (isset($dbConf['host'])) {
                 if (Util::subString($dbConf['host'], 0, 5) === 'unix:') {
                     $dsn .= 'unix_socket=' . Util::subString($dbConf['host'], 5) . ';';
                 } else {
                     $dsn .= 'host=' . $dbConf['host'] . ';';
                 }
             }
             if (!empty($dbConf['port'])) {
                 $dsn .= 'port=' . $dbConf['port'] . ';';
             }
             $dsn .= 'dbname=' . $dbConf['database'];
             return [$dsn, $dbConf['driver'], $dbConf['username'] ?? $username, $dbConf['password'] ?? $password];
         case 'sqlite':
             $dsn = $dbConf['driver'] . ':';
             if (isset($dbConf['path'])) {
                 $dsn .= $dbConf['path'];
             } else {
                 $dsn .= ':memory:';
             }
             return [$dsn, $dbConf['driver'], null, null];
         default:
             throw new DBAlert\DBException(\trk('errors.database.not_implemented', (string) $dbConf['driver']));
     }
 }
Exemplo n.º 3
0
 /**
  * Generate, store, and return the index and token
  *
  * @param string $lockTo What URI endpoint this is valid for
  * @return array [string, string]
  */
 protected function generateToken(string $lockTo = '') : array
 {
     // Create a distinct index:
     do {
         $index = Base64UrlSafe::encode(\random_bytes(18));
     } while (isset($_SESSION[$this->sessionIndex][$index]));
     $token = Base64UrlSafe::encode(\random_bytes(33));
     $_SESSION[$this->sessionIndex][$index] = ['created' => \intval(\date('YmdHis')), 'uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'], 'token' => $token];
     if (!empty($lockTo)) {
         // Get rid of trailing slashes.
         if (\preg_match('#/$#', $lockTo)) {
             $lockTo = Util::subString($lockTo, 0, Util::stringLength($lockTo) - 1);
         }
         $_SESSION[$this->sessionIndex][$index]['lockto'] = $lockTo;
     }
     $this->recycleTokens();
     return [$index, $token];
 }
Exemplo n.º 4
0
 /**
  * Process data using the filter rules.
  *
  * @param mixed $data
  * @return mixed
  * @throws \TypeError
  */
 public function process($data = null)
 {
     if ($this->type === 'string') {
         if (\is_array($data)) {
             throw new \TypeError(\sprintf('Unexpected array for string filter (%s).', $this->index));
         }
         if (\is_string($data)) {
         } elseif (\is_object($data) && \method_exists($data, '__toString')) {
             $data = (string) $data;
         } elseif (\is_numeric($data)) {
             $data = (string) $data;
         } elseif (\is_null($data)) {
             $data = null;
         } else {
             throw new \TypeError(\sprintf('Expected a string (%s).', $this->index));
         }
     }
     if ($this->type === 'int') {
         if (\is_array($data)) {
             throw new \TypeError(\sprintf('Unexpected array for integer filter (%s).', $this->index));
         }
         if (\is_int($data) || \is_float($data)) {
             $data = (int) $data;
         } elseif (\is_null($data) || $data === '') {
             $data = null;
         } elseif (\is_string($data) && \preg_match('#^\\-?[0-9]+$#', $data)) {
             $data = (int) $data;
         } else {
             throw new \TypeError(\sprintf('Expected an integer (%s).', $this->index));
         }
     }
     if ($this->type === 'float') {
         if (\is_array($data)) {
             throw new \TypeError(\sprintf('Unexpected array for float filter (%s).', $this->index));
         }
         if (\is_int($data) || \is_float($data)) {
             $data = (double) $data;
         } elseif (\is_null($data) || $data === '') {
             $data = null;
         } elseif (\is_string($data) && \is_numeric($data)) {
             $data = (double) $data;
         } else {
             throw new \TypeError(\sprintf('Expected an integer or floating point number (%s).', $this->index));
         }
     }
     if ($this->type === 'array' || Util::subString($this->type, -2) === '[]') {
         if (\is_array($data)) {
             $data = (array) $data;
         } elseif (\is_null($data)) {
             $data = [];
         } else {
             throw new \TypeError(\sprintf('Expected an array (%s).', $this->index));
         }
     }
     if ($this->type === 'bool') {
         if (\is_array($data)) {
             throw new \TypeError(\sprintf('Unexpected array for boolean filter (%s).', $this->index));
         }
         $data = !empty($data);
     }
     $data = $this->applyCallbacks($data, 0);
     if ($data === null) {
         $data = $this->default;
     }
     // For type strictness:
     switch ($this->type) {
         case 'bool':
             return (bool) $data;
         case 'float':
             return (double) $data;
         case 'int':
             return (int) $data;
         case 'string':
             return (string) $data;
         default:
             return $data;
     }
 }
Exemplo n.º 5
0
    $extensions = \array_slice($argv, 1);
} else {
    $extensions = ['php', 'twig'];
}
$fileList = [];
$repository = 'https://github.com/paragonie/airship/blob/master/';
$cutoff = \strlen(\dirname(__DIR__) . '/src') + 1;
$dirs = [];
$allDirs = \Airship\list_all_files(\dirname(__DIR__) . '/src');
\sort($allDirs, \SORT_STRING & ~\SORT_FLAG_CASE);
foreach ($allDirs as $file) {
    $print = \trim(Util::subString($file, $cutoff), '/');
    $pieces = \explode('/', $print);
    $max = \count($pieces) - 1;
    $name = \array_pop($pieces);
    if (Util::subString($print, 0, 3) === 'tmp' || Util::subString($print, 0, 5) === 'files') {
        continue;
    }
    $currentDir = \implode('/', $pieces);
    if ($max > 0 && !isset($dirs[$currentDir])) {
        echo \str_repeat(' ', 3 * ($max - 1)) . '- [ ] ';
        echo $pieces[$max - 1];
        $dirs[$currentDir] = true;
        echo "\n";
    }
    if (\preg_match('/\\.([^.]+)$/', $name, $m)) {
        if (!\in_array($m[1], $extensions)) {
            continue;
        }
    } else {
        continue;