/** * Validates a URL * * @param mixed $value The value to validate * @return String|NULL Any errors encountered */ protected function process($value) { if (!is_string($value)) { return "URL must be a string"; } if (\r8\str\contains(" ", $value)) { return "URL must not contain spaces"; } if (\r8\str\contains("\t", $value)) { return "URL must not contain tabs"; } if (\r8\str\contains("\n", $value) || \r8\str\contains("\r", $value)) { return "URL must not contain line breaks"; } if (preg_match('/[^a-z0-9' . preg_quote('$-_.+!*\'(),{}|\\^~[]`<>#%";/?:@&=', '/') . ']/i', $value)) { return "URL contains invalid characters"; } if ($this->flags & self::ALLOW_RELATIVE) { $parsed = @parse_url($value); if ($parsed === FALSE) { return "URL is not valid"; } } else { if (!filter_var($value, FILTER_VALIDATE_URL)) { return "URL is not valid"; } } }
/** * Parses a string into a SQL expression and it's alias * * @param String $name The SQL string to parse * @return array Returns an array where the first element is the * SQL expression and the second is the alias */ public static function parseSQLAlias($string) { $string = (string) $string; // If there is no obvious alias, take an easy out if (!\r8\str\contains(" AS ", $string)) { $alias = null; } else { if (!\r8\str\contains("`", $string)) { list($string, $alias) = explode(" AS ", $string, 2); $alias = trim($alias); } else { $parser = new \r8\Quoter(); list($string, $alias) = $parser->clearQuotes()->setQuote("`")->parse($string)->setIncludeQuoted(FALSE)->explode(" AS "); $alias = trim($alias); } } $string = trim($string); if (\r8\IsEmpty($string)) { $string = null; } $alias = \r8\str\stripW($alias); if (\r8\IsEmpty($alias)) { $alias = null; } return array($string, $alias); }
public function testContains() { $this->assertTrue(\r8\str\contains(' In ', 'Check In this string')); $this->assertFalse(\r8\str\contains('not in', 'Check In this string')); $this->assertTrue(\r8\str\contains(' In ', 'Check In this string', TRUE)); $this->assertFalse(\r8\str\contains('not in', 'Check In this string', TRUE)); $this->assertTrue(\r8\str\contains(' in ', 'Check In this string', TRUE)); $this->assertFalse(\r8\str\contains('not in', 'Check In this string', TRUE)); $this->assertTrue(\r8\str\contains(' In ', 'Check In this string', FALSE)); $this->assertFalse(\r8\str\contains('not in', 'Check In this string', FALSE)); $this->assertFalse(\r8\str\contains(' in ', 'Check In this string', FALSE)); $this->assertFalse(\r8\str\contains('not in', 'Check In this string', FALSE)); }
/** * Validates an e-mail address * * @param mixed $value The value to validate * @return String|NULL Any errors encountered */ protected function process($value) { $value = (string) $value; if (\r8\isEmpty($value)) { return "Email Address must not be empty"; } $atCount = substr_count($value, "@"); if ($atCount == 0) { return "Email Address must contain an 'at' (@) symbol"; } if ($atCount > 1) { return "Email Address must only contain one 'at' (@) symbol"; } if (\r8\str\contains(" ", $value)) { return "Email Address must not contain spaces"; } if (\r8\str\contains("\n", $value) || \r8\str\contains("\r", $value)) { return "Email Address must not contain line breaks"; } if (\r8\str\contains("\t", $value)) { return "Email Address must not contain tabs"; } if (preg_match('/\\.\\.+/', $value)) { return "Email Address must not contain repeated periods"; } if (preg_match('/[^a-z0-9' . preg_quote('!#$%&\'*+-/=?^_`{|}~@.[]', '/') . ']/i', $value)) { return "Email Address contains invalid characters"; } if (\r8\str\endsWith($value, ".")) { return "Email Address must not end with a period"; } list($local, $domain) = explode("@", $value); if (\r8\str\startsWith($local, ".")) { return "Email Address must not start with a period"; } // This is hard to describe to a user, so just give them a vague description if (\r8\str\endsWith($local, ".")) { return "Email Address is not valid"; } if (strlen($local) > 64 || strlen($domain) > 255) { return "Email Address is too long"; } $regex = '/' . '^' . '[\\w!#$%&\'*+\\/=?^`{|}~.-]+' . '@' . '(?:[a-z\\d][a-z\\d-]*(?:\\.[a-z\\d][a-z\\d-]*)?)+' . '\\.(?:[a-z][a-z\\d-]+)' . '$' . '/iD'; // Do a final regex to match the basic form if (!preg_match($regex, $value)) { return "Email Address is not valid"; } }
/** * Validates the given value * * @param mixed $value The value to validate * @return String|NULL Any errors encountered */ protected function process($value) { if (is_bool($value) || is_int($value) || is_float($value) || is_null($value)) { return null; } if (!is_string($value)) { return "Must be a string"; } if (\r8\str\contains(' ', $value)) { return "Must not contain any spaces"; } if (\r8\str\contains("\t", $value)) { return "Must not contain any tabs"; } if (\r8\str\contains("\n", $value) || \r8\str\contains("\r", $value)) { return "Must not contain any new lines"; } }
/** * Finds every occurance of the given needle in the haystack and returns their offsets * * @param String $needle The string being searched for * @param String $haystack The string that you trying to find the needle in * @param Boolean $ignoreCase Whether the search should be case sensitive * @return object Returns an array containing the found offsets. If the needle is not contained in the haystack, an empty array is returned */ function offsets($needle, $haystack, $ignoreCase = TRUE) { $ignoreCase = (bool) $ignoreCase; $needle = (string) $needle; $haystack = (string) $haystack; if (empty($needle)) { throw new \r8\Exception\Argument(0, 'needle', 'Must not be empty'); } if (!\r8\str\contains($needle, $haystack, $ignoreCase)) { return array(); } $count = $ignoreCase ? \r8\str\substr_icount($haystack, $needle) : substr_count($haystack, $needle); $found = array(); $offset = 0; $length = strlen($needle); for ($i = 0; $i < $count; $i++) { $found[] = $ignoreCase ? stripos($haystack, $needle, $offset) : strpos($haystack, $needle, $offset); $offset = end($found) + $length; } return $found; }
/** * Returns the mime type of this file * * @return String */ public function getMimeType() { $this->requirePath(); $finfo = finfo_open(\FILEINFO_MIME); if ($finfo === FALSE) { throw new \r8\Exception\Extension("Unable to open finfo database"); } $result = finfo_file($finfo, $this->getPath()); finfo_close($finfo); if (\r8\str\contains(" ", $result)) { $result = strstr($result, " ", TRUE); } $result = rtrim($result, ";"); return $result; }
/** * Parses a query string into an array * * @param String $query The query string to parser * @return Array Returns the parsed string as an array */ public function parse($query) { $query = (string) $query; // Grab everything after the starting delimiter if (\r8\str\contains($this->startDelim, $query)) { $query = substr($query, strpos($query, $this->startDelim) + 1); } // Cut off everything after the ending delimiter if (\r8\str\contains($this->endDelim, $query)) { $query = substr($query, 0, strpos($query, $this->endDelim)); } // Split the query into its pairs $query = explode($this->outerDelim, $query); $result = array(); // Loop through each pair foreach ($query as $pair) { // Skip over empty pairs if (\r8\isEmpty($pair)) { continue; } // split the pair up into its key and value if (\r8\str\contains($this->innerDelim, $pair)) { list($key, $value) = explode($this->innerDelim, $pair, 2); } else { list($key, $value) = array($pair, ""); } // if the key is empty, do nothing with it if (\r8\isEmpty($key, \r8\ALLOW_SPACES)) { continue; } // Apply the filters to the key and value $key = $this->keyFilter->filter($key); $value = $this->valueFilter->filter($value); // parse the list of keys into an array $key = $this->parseKey($key); // Add the branch to the result array \r8\ary\branch($result, $value, $key); } return $result; }
/** * Parses a string into the scheme, userinfo, host and port * * @param String $base The base of the url * @return \r8\URL Returns a self reference */ public function setBase($base) { $base = (string) $base; if (\r8\str\contains("://", $base)) { $this->setScheme(strstr($base, "://", TRUE)); $base = substr(strstr($base, "://", FALSE), 3); } else { $this->clearScheme(); } if (\r8\str\contains("@", $base)) { $this->setUserInfo(strstr($base, "@", TRUE)); $base = substr(strstr($base, "@", FALSE), 1); } else { $this->clearUserInfo(); } $this->setHostAndPort($base); return $this; }
/** * Analyzes the html and text body and returns a boundary string that doesn't * exist in either. * * This will return the same boundary every time it is called, unless the text * or HTML is changed to include the boundary * * @return String A thirty character long string */ public function getBoundary() { // If the boundary has been generated and the string and HTML don't contain // it, then there is no need to create another while (!isset($this->boundary) || \r8\str\contains($this->boundary, $this->text) || \r8\str\contains($this->boundary, $this->html)) { // The string "=_" can never appear in the quoted printable character // encoding format (or base64, for that matter), which ensures that // the generated boundary won't show up in any encoded data $this->boundary = "=_" . substr(md5(microtime() . time()), 0, 26) . "_="; } return $this->boundary; }