code
stringlengths 31
1.39M
| docstring
stringlengths 23
16.8k
| func_name
stringlengths 1
126
| language
stringclasses 1
value | repo
stringlengths 7
63
| path
stringlengths 7
166
| url
stringlengths 50
220
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
protected function unserializeCookieValue($rawValue)
{
if (!is_string($rawValue)) {
return null;
}
$chunks = explode(':', $rawValue);
if (count($chunks) !== 3) {
return null;
}
list($userID, $authenticationTypeHandle, $token) = $chunks;
if (($userID = (int) $userID) < 1 || $authenticationTypeHandle === '') {
return null;
}
return new CookieValue($userID, $authenticationTypeHandle, $token);
}
|
@param string|mixed $rawValue
@return \Concrete\Core\User\PersistentAuthentication\CookieValue|null
|
unserializeCookieValue
|
php
|
concretecms/concretecms
|
concrete/src/User/PersistentAuthentication/CookieService.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PersistentAuthentication/CookieService.php
|
MIT
|
public function __construct($userID, $authenticationTypeHandle, $token)
{
$this->userID = $userID;
$this->authenticationTypeHandle = $authenticationTypeHandle;
$this->token = $token;
}
|
@param int $userID The user ID
@param string $authenticationTypeHandle the authentication type handle
@param string $token the unique token identifier
|
__construct
|
php
|
concretecms/concretecms
|
concrete/src/User/PersistentAuthentication/CookieValue.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PersistentAuthentication/CookieValue.php
|
MIT
|
public function getUserID()
{
return $this->userID;
}
|
Get the user ID.
@return int
|
getUserID
|
php
|
concretecms/concretecms
|
concrete/src/User/PersistentAuthentication/CookieValue.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PersistentAuthentication/CookieValue.php
|
MIT
|
public function getAuthenticationTypeHandle()
{
return $this->authenticationTypeHandle;
}
|
Get the handle of the authentication type.
@return string
|
getAuthenticationTypeHandle
|
php
|
concretecms/concretecms
|
concrete/src/User/PersistentAuthentication/CookieValue.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PersistentAuthentication/CookieValue.php
|
MIT
|
public function getToken()
{
return $this->token;
}
|
Get the unique token identifier.
@return string
|
getToken
|
php
|
concretecms/concretecms
|
concrete/src/User/PersistentAuthentication/CookieValue.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PersistentAuthentication/CookieValue.php
|
MIT
|
public function getFormattedMessageBody()
{
$msgBody = $this->getMessageBody();
$txt = Loader::helper('text');
$repliedPos = strpos($msgBody, $this->getMessageDelimiter());
if ($repliedPos > -1) {
$repliedText = substr($msgBody, $repliedPos);
$messageText = substr($msgBody, 0, $repliedPos);
$msgBody = nl2br($txt->entities($messageText)) . '<div class="ccm-profile-message-replied">' . nl2br($txt->entities($repliedText)) . '</div>';
$msgBody = str_replace($this->getMessageDelimiter(), '<hr />', $msgBody);
} else {
$msgBody = nl2br($txt->entities($msgBody));
}
return $msgBody;
}
|
Responsible for converting line breaks to br tags, perhaps running bbcode, as well as making the older replied-to messages gray.
|
getFormattedMessageBody
|
php
|
concretecms/concretecms
|
concrete/src/User/PrivateMessage/PrivateMessage.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PrivateMessage/PrivateMessage.php
|
MIT
|
public function getAttachments() {
return $this->attachments;
}
|
@return \Concrete\Core\Entity\File\File[]
|
getAttachments
|
php
|
concretecms/concretecms
|
concrete/src/User/PrivateMessage/PrivateMessage.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/User/PrivateMessage/PrivateMessage.php
|
MIT
|
public function __construct($ipAddress = null, $isHex = false)
{
if ($ipAddress !== null && $ipAddress !== '') {
$this->setIp($ipAddress, $isHex);
}
}
|
Builds the IPAddress object from the ip address string provided, or from a hexadecimal string
If no ip address is provided, it can be set later by running the setIp function.
@param string|null $ipAddress
@param bool $isHex
|
__construct
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function setIp($ipAddress, $isHex = false)
{
if ($isHex) {
$this->ipHex = $ipAddress;
} else {
//discard any IPv6 port
$ipAddress = preg_replace('/\[(.*?)\].*/', '$1', $ipAddress);
//discard any IPv4 port
if (strpos($ipAddress, '.') !== false) {
$ipAddress = preg_replace('/(.*?(?:\d{1,3}\.?){4}).*/', "$1", $ipAddress);
}
$this->ipHex = bin2hex(inet_pton($ipAddress));
}
return $this;
}
|
Sets the current IP Address.
@param string $ipAddress
@param bool $isHex
@return $this
|
setIp
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function getIp($format = self::FORMAT_HEX)
{
if ($this->ipHex === null) {
return null;
} elseif ($format === self::FORMAT_HEX) {
return $this->ipHex;
} elseif ($format === self::FORMAT_IP_STRING) {
return inet_ntop($this->hex2bin($this->ipHex));
}
throw new \Exception('Invalid IP format');
}
|
Returns the IPAddress string, null if no ip address has been set.
@param int $format Uses the IPAddress::FORMAT_* constants
@throws \Exception Throws an exception if the value is not null and no valid format constant is given
@return string|null
|
getIp
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
protected function isIpSet()
{
return $this->ipHex !== null;
}
|
@return bool Returns true if the IP address is set
|
isIpSet
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function hex2bin($hex)
{
if (!function_exists('hex2bin')) {
return pack("H*", $hex);
} else {
return hex2bin($hex);
}
}
|
Fallback function for those using < PHP 5.4
Decodes a hexadecimally encoded binary string.
@param string $hex
@return string Returns the binary representation of the given data
|
hex2bin
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function isLoopBack()
{
if (!$this->isIpSet()) {
throw new \Exception('No IP Set');
}
if ($this->isIPv4() && strpos($this->ipHex, '7f') === 0) {
return true; //IPv4 loopback 127.0.0.0/8
} elseif ($this->ipHex === '00000000000000000000000000000001'
|| $this->ipHex === '00000000000000000000ffff7f000001'
) {
return true; //IPv6 loopback ::1 or ::ffff:127.0.0.1
}
return false;
}
|
Used to check of the current IP is a loopback IP address.
@throws \Exception if no IP is set
@return bool returns true for loopback IP's, returns false if it is not a loopback IP
|
isLoopBack
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function isPrivate()
{
if (!$this->isIpSet()) {
throw new \Exception('No IP Set');
}
if (
($this->isIPv4() &&
(strpos($this->ipHex, '0a') === 0 //10.0.0.0/8
|| strpos($this->ipHex, 'ac1') === 0 //172.16.0.0/12
|| strpos($this->ipHex, 'c0a8') === 0 //192.168.0.0/16
)
)
||
($this->isIPv6() &&
(strpos($this->ipHex, 'fc') === 0 //fc00::/7
|| strpos($this->ipHex, 'fd') === 0 //fd00::/7
|| strpos($this->ipHex, 'ffff0a') === 20 //::ffff:10.0.0.0/8
|| strpos($this->ipHex, 'ffffac1') === 20 //::ffff:172.16.0.0/12
|| strpos($this->ipHex, 'ffffc0a8') === 20 //::ffff:192.168.0.0/16
)
)
) {
return true;
}
return false;
}
|
Returns true if the IP address belongs to a private network, false if it is not.
@return bool
@throws \Exception
|
isPrivate
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function isLinkLocal()
{
if (!$this->isIpSet()) {
throw new \Exception('No IP Set');
}
if (
($this->isIPv4() &&
strpos($this->ipHex, 'a9fe') === 0 //169.254.0.0/16
)
||
($this->isIPv6() &&
(strpos($this->ipHex, 'fe8') === 0 //fe80::/10 Link-Scope Unicast
|| strpos($this->ipHex, 'fe9') === 0 //fe80::/10 Link-Scope Unicast
|| strpos($this->ipHex, 'fea') === 0 //fe80::/10 Link-Scope Unicast
|| strpos($this->ipHex, 'feb') === 0 //fe80::/10 Link-Scope Unicast
|| strpos($this->ipHex, 'ffffa9fe') === 20 //::ffff:169.254.0.0/16
)
)
) {
return true;
}
return false;
}
|
Returns true if the IP is a Link-local address, false if it is not.
@return bool
@throws \Exception
|
isLinkLocal
|
php
|
concretecms/concretecms
|
concrete/src/Utility/IPAddress.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/IPAddress.php
|
MIT
|
public function get(array $array, $keys, $default = null)
{
$keys = $this->parseKeys($keys);
if (is_array($array) && $keys) {
$key = array_shift($keys);
if (array_key_exists($key, $array)) {
$value = $array[$key];
if (!$keys) {
return $value;
}
if (is_array($value)) {
return $this->get($value, $keys, $default);
}
}
}
return $default;
}
|
Fetches a value from an (multidimensional) array.
@param array $array
@param string|int|array $keys Either one key or multiple keys
@param mixedvar $default the value that is returned if key is not found
|
get
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Arrays.php
|
MIT
|
public function set(array $array, $keys, $value)
{
$keys = $this->parseKeys($keys);
if ($keys) {
$key = array_shift($keys);
// This is the last key we've shifted
if (!$keys) {
$array[$key] = $value;
} else {
// There are more keys so this should be an array
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = array();
}
$array[$key] = $this->set(
$array[$key], $keys, $value
);
}
}
return $array;
}
|
Sets a value in an (multidimensional) array, creating the arrays recursivly.
@param array $array
@param unknown_type $keys
@param unknown_type $value
|
set
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Arrays.php
|
MIT
|
private function parseKeys($keys)
{
if (is_string($keys)) {
if (strpos($keys, '[') !== false) {
$keys = str_replace(']', '', $keys);
$keys = explode('[', trim($keys, '['));
} else {
$keys = (array) $keys;
}
}
return $keys;
}
|
Turns the string keys into an array of keys.
@param string|array $keys
@return array
|
parseKeys
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Arrays.php
|
MIT
|
public function flatten(array $array)
{
$tmp = array();
foreach ($array as $a) {
if (is_array($a)) {
$tmp = array_merge($tmp, array_flat($a));
} else {
$tmp[] = $a;
}
}
return $tmp;
}
|
Takes a multidimensional array and flattens it.
@param array $array
@return array
|
flatten
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Arrays.php
|
MIT
|
public function subset($a, $b)
{
if (count(array_diff(array_merge($a, $b), $b)) == 0) {
return true;
} else {
return false;
}
}
|
Returns whether $a is a proper subset of $b.
|
subset
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Arrays.php
|
MIT
|
public function generateFromBase($string, $table, $key)
{
$foundRecord = false;
$db = Application::make(Connection::class);
$i = '';
$_string = '';
while ($foundRecord == false) {
$_string = $string . $i;
$cnt = $db->GetOne("select count(" . $key . ") as total from " . $table . " where " . $key . " = ?",
array($_string));
if ($cnt < 1) {
$foundRecord = true;
} else {
if ($i == '') {
$i = 0;
}
++$i;
}
}
return $_string;
}
|
Like generate() below, but simply appends an ever increasing number to what you provide
until it comes back as not found.
|
generateFromBase
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Identifier.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Identifier.php
|
MIT
|
public function generate($table, $key, $length = 12, $lowercase = false)
{
$foundHash = false;
$db = Application::make(Connection::class);
while ($foundHash == false) {
$string = $this->getString($length);
if ($lowercase) {
$string = strtolower($string);
}
$cnt = $db->GetOne("select count(" . $key . ") as total from " . $table . " where " . $key . " = ?",
array($string));
if ($cnt < 1) {
$foundHash = true;
}
}
return $string;
}
|
Generates a unique identifier for an item in a database table. Used, among other places, in generating
User hashes for email validation.
@param string $table
@param string $key
@param int $length
@param bool $lowercase
@return string
|
generate
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Identifier.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Identifier.php
|
MIT
|
public function getString($length = 12)
{
$size = ceil($length / 2);
try {
$bytes = random_bytes($size);
} catch (\Exception $e) {
die('Could not generate a random string.');
}
return substr(bin2hex($bytes), 0, $length);
}
|
Generate a cryptographically secure random string
@param int $length
@return string
|
getString
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Identifier.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Identifier.php
|
MIT
|
public function flexround($value)
{
return ($value === null || $value === '') ? null : (float) $value;
}
|
Rounds the value only out to its most significant digit.
@param string $value
@return float|null
|
flexround
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function trim($value)
{
$result = '';
$value = (string) $value;
if ($value !== '') {
// Temporarily remove leadin sign
$sign = $value[0];
if ($sign === '-' || $sign === '+') {
$value = substr($value, 1);
} else {
$sign = '';
}
if ($value !== '') {
// Remove initial zeroes
$value = ltrim($value, '0');
if ($value === '' || $value[0] === '.') {
$value = '0' . $value;
}
if (strpos($value, '.') !== false) {
// Remove trailing zeroes after the dot
$value = rtrim(rtrim($value, '0'), '.');
}
$result = $sign . $value;
}
}
return $result;
}
|
Remove superfluous zeroes from a string containing a number.
@param string $value (decimal separator is dot)
@return string
|
trim
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function isNumber($string)
{
return \Punic\Number::isNumeric($string);
}
|
Checks if a given string is valid representation of a number in the current locale.
@param string $string
@return bool
@example http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details
|
isNumber
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function isInteger($string)
{
return \Punic\Number::isInteger($string);
}
|
Checks if a given string is valid representation of an integer in the current locale.
@param int|float|string $string
@return bool
@example http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details
|
isInteger
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function format($number, $precision = null)
{
return \Punic\Number::format($number, $precision);
}
|
Format a number with grouped thousands and localized decimal point/thousands separator.
@param int|float|string $number The number being formatted
@param int|null $precision [default: null] The wanted precision; if null or not specified the complete localized number will be returned
@return string
@example http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details
|
format
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function unformat($string, $trim = true, $precision = null)
{
$result = null;
$number = \Punic\Number::unformat($string);
if (!is_null($number)) {
if (is_numeric($precision)) {
$number = round($number, $precision);
}
$result = $number;
}
return $result;
}
|
Parses a localized number representation and returns the number (or null if $string is not a valid number representation).
@param string $string The number representation to parse
@param bool $trim [default: true] Remove spaces and new lines at the start/end of $string?
@param int|null $precision [default: null] The wanted precision; if null or not specified the complete number will be returned
@return int|float|null
@example http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details
|
unformat
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function formatSize($size, $forceUnit = '')
{
if (!is_numeric($size)) {
return $size;
}
if (strlen($forceUnit) && array_search($forceUnit, ['bytes', 'KB', 'MB', 'GB', 'TB']) === false) {
$forceUnit = '';
}
if ($forceUnit === 'bytes' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
return t2(/*i18n %s is a number */'%s byte', '%s bytes', $size, $this->format($size, 0));
}
$size /= 1024;
if ($forceUnit === 'KB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
return t(/*i18n %s is a number, KB means Kilobyte */'%s KB', $this->format($size, 2));
}
$size /= 1024;
if ($forceUnit === 'MB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
return t(/*i18n %s is a number, MB means Megabyte */'%s MB', $this->format($size, 2));
}
$size /= 1024;
if ($forceUnit === 'GB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
return t(/*i18n %s is a number, GB means Gigabyte */'%s GB', $this->format($size, 2));
}
$size /= 1024;
return t(/*i18n %s is a number, TB means Terabyte */'%s TB', $this->format($size, 2));
}
|
Formats a size (measured in bytes, KB, MB, ...).
@param int|float|string $size The size to be formatted, in bytes
@param string $forceUnit = '' Set to 'bytes', 'KB', 'MB', 'GB' or 'TB' if you want to force the unit, leave empty to automatically determine the unit
@return string|mixed If $size is not numeric, the function returns $size (untouched), otherwise it returns the size with the correct unit (GB, MB, ...) and formatted following the locale rules
@example formatSize(0) returns '0 bytes'
@example formatSize(1) returns '1 byte'
@example formatSize(1000) returns '1,000 bytes'
@example formatSize(1024) returns '1.00 KB'
@example formatSize(1024, 'bytes') returns '1024 bytes'
@example formatSize(1024, 'GB') returns '0.00 GB'
@example formatSize(2000000) returns '1.91 MB'
@example formatSize(-5000) returns '-4.88 KB'
@example formatSize('hello') returns 'hello'
|
formatSize
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public function getBytes($val)
{
$val = trim($val);
if ($val !== '') {
$last = strtolower($val[strlen($val) - 1]);
if (!is_numeric($last)) {
$num = trim(substr($val, 0, -1));
switch ($last) {
case 'g':
$num *= 1024;
case 'm':
$num *= 1024;
case 'k':
$num *= 1024;
$val = $num;
break;
}
}
}
return $val;
}
|
Nice and elegant function for converting memory. Thanks to @lightness races in orbit on Stackoverflow.
@param string $val
@return int|string
|
getBytes
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Number.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Number.php
|
MIT
|
public static function encodePath($path)
{
if (mb_strpos($path, '/') !== false) {
$path = explode('/', $path);
$path = array_map('rawurlencode', $path);
$newPath = implode('/', $path);
} else {
if (is_null($path)) {
$newPath = null;
} else {
$newPath = rawurlencode($path);
}
}
$path = str_replace('%21', '!', $newPath);
return $path;
}
|
URL-encodes collection path.
@param string $path
@return string $path
|
encodePath
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public static function match($pattern, $value)
{
if ($pattern == $value) {
return true;
}
$pattern = preg_quote($pattern, '#');
if ($pattern !== '/') {
$pattern = str_replace('\*', '.*', $pattern) . '\z';
} else {
$pattern = '/$';
}
return (bool) preg_match('#^' . $pattern . '#', $value);
}
|
Determine if a given string matches a given pattern.
@param string $pattern
@param string $value
@return bool
|
match
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function slugSafeString($handle, $maxlength = 128)
{
$handle = preg_replace('/[^\\p{L}\\p{Nd}\-_]+/u', ' ', $handle); // remove unneeded chars
$handle = preg_replace('/[-\s]+/', '-', $handle); // convert spaces to hyphens
return trim(Utf8::substr($handle, 0, $maxlength), '-'); // trim to first $max_length chars
}
|
Remove unsafe characters for URL slug.
@param string $handle
@param int $maxlength = Max number of characters of the return value
@return string $handle
|
slugSafeString
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function sanitize($string, $max_length = 0, $allowed = '')
{
$text = trim(strip_tags($string, $allowed));
if ($max_length > 0) {
if (function_exists('mb_substr')) {
$text = mb_substr($text, 0, $max_length, APP_CHARSET);
} else {
$text = substr($text, 0, $max_length);
}
}
if ($text == null) {
return ""; // we need to explicitly return a string otherwise some DB functions might insert this as a ZERO.
}
return $text;
}
|
Strips tags and optionally reduces string to specified length.
@param string $string
@param int $max_length
@param string $allowed
@return string
|
sanitize
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function email($email)
{
$regex = '/[^a-zA-Z0-9_\.!#\$\&\'\*\+-?^`{|}~@]/i';
return preg_replace($regex, '', $email);
}
|
Leaves only characters that are valid in email addresses (RFC).
@param string $email
@return string
|
email
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function alphanum($string)
{
return preg_replace('/[^A-Za-z0-9]/', '', $string);
}
|
Leaves only characters that are alpha-numeric.
@param string $string
@return string
|
alphanum
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function entities($v)
{
return htmlentities($v, ENT_QUOTES, APP_CHARSET);
}
|
always use in place of htmlentites(), so it works with different languages.
@param string $v
@return string
|
entities
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function decodeEntities($v)
{
return html_entity_decode($v, ENT_QUOTES, APP_CHARSET);
}
|
Decodes html-encoded entities (for instance: from '>' to '>').
@param string $v
@return string
|
decodeEntities
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function specialchars($v)
{
return htmlspecialchars((string) $v, ENT_QUOTES, APP_CHARSET, false);
}
|
A concrete5 specific version of htmlspecialchars(). Double encoding is OFF, and the character set is set to your site's.
@param string $v
@return string
|
specialchars
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function camelcase($string)
{
return ConcreteObject::camelcase($string);
}
|
Takes a string and turns it into the CamelCase or StudlyCaps version.
@param string $string
@return string
|
camelcase
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function makenice($input)
{
$output = strip_tags($input);
$output = $this->autolink($output);
$output = nl2br($output);
return $output;
}
|
Runs a number of text functions, including autolink, nl2br, strip_tags. Assumes that you want simple
text comments but with a few niceties.
@param string $input
@return string $output
|
makenice
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function prettyStripTags($input, $allowedTags = null)
{
return str_replace(' ', ' ', strip_tags(str_replace('<', ' <', $input), $allowedTags));
}
|
Runs strip_tags but ensures that spaces are kept between the stripped tags.
@param $input
@param $allowedTags
|
prettyStripTags
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function autolink($input, $newWindow = false, $defaultProtocol = 'http://')
{
$target = $newWindow ? ' target="_blank"' : '';
$output = preg_replace_callback(
'/(http:\/\/|https:\/\/|(www\.))(([^\s<]{4,80})[^\s<]*)/',
function (array $matches) use ($target, $defaultProtocol) {
$protocol = strpos($matches[1], ':') ? $matches[1] : $defaultProtocol;
return "<a href=\"{$protocol}{$matches[2]}{$matches[3]}\"{$target} rel=\"nofollow\">{$matches[2]}{$matches[4]}</a>";
},
$input);
return $output;
}
|
Scans passed text and automatically hyperlinks any URL inside it.
@param string $input
@param bool $newWindow
@param string $defaultProtocol
@return string $output
|
autolink
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function fnmatch($pattern, $string)
{
$string = (string) $string;
if (!function_exists('fnmatch')) {
return preg_match(
"#^" . strtr(
preg_quote($pattern, '#'),
array('\*' => '.*', '\?' => '.', '\[' => '[', '\]' => ']')) . "$#i",
$string);
} else {
return fnmatch($pattern, $string);
}
}
|
A wrapper for PHP's fnmatch() function, which some installations don't have.
@param string $pattern
@param string $string
@return bool
|
fnmatch
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function uncamelcase($string)
{
return ConcreteObject::uncamelcase($string);
}
|
Takes a CamelCase string and turns it into camel_case.
See also laravel function snake_case which does similar but without bugs.
@param string $string
@return string
|
uncamelcase
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function unhandle($string)
{
$r1 = ucwords(str_replace(array('_', '-', '/'), ' ', $string));
return $r1;
}
|
Takes a handle-based string like "blah_blah" or "blah-blah" or "blah/blah" and turns it into "Blah Blah".
@param string $string
@return string $r1
|
unhandle
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function handle($handle, $leaveSlashes = false)
{
$handle = $this->sanitizeFileSystem($handle, $leaveSlashes);
return str_replace('-', '_', $handle);
}
|
Takes a string and turns it into a handle.
@param $handle
@param bool $leaveSlashes
@return string
|
handle
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function sanitizeFileSystem($handle)
{
return $this->urlify($handle, Config::get('concrete.seo.segment_max_length'), '', false);
}
|
@param string $handle
@return string $handle
|
sanitizeFileSystem
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function urlify($handle, $max_length = null, $locale = '', $removeExcludedWords = true)
{
$app = Application::getFacadeApplication();
$config = $app->make(Repository::class);
if ($max_length === null) {
$max_length = $config->get('concrete.seo.segment_max_length');
}
if ($config->get('concrete.seo.enable_slug_asciify')) {
$text = $this->asciify($handle, $locale);
} else {
$text = $handle;
}
$text = strtolower(str_replace(array("\r", "\n", "\t"), ' ', $text));
if ($removeExcludedWords) {
$excludeSeoWords = $config->get('concrete.seo.exclude_words');
if (is_string($excludeSeoWords)) {
if (strlen($excludeSeoWords)) {
$remove_list = explode(',', $excludeSeoWords);
$remove_list = array_map('trim', $remove_list);
$remove_list = array_filter($remove_list, 'strlen');
} else {
$remove_list = array();
}
} else {
$remove_list = \URLify::$remove_list;
}
if (count($remove_list)) {
$text = preg_replace('/\b(' . implode('|', $remove_list) . ')\b/i', '', $text);
}
}
$text = preg_replace('/[^-\w\s]/', '', $text); // remove unneeded chars
$text = str_replace('_', ' ', $text); // treat underscores as spaces
$text = preg_replace('/^\s+|\s+$/', '', $text); // trim leading/trailing spaces
$text = preg_replace('/[-\s]+/', '-', $text); // convert spaces to hyphens
$text = strtolower($text); // convert to lowercase
return trim(substr($text, 0, $max_length), '-'); // trim to first $maxlength chars
}
|
Takes text and returns it in the "lowercase-and-dashed-with-no-punctuation" format.
@param string $handle
@param int $max_length Max number of characters of the return value
@param string $locale Language code of the language rules that should be priorized
@param bool $removeExcludedWords Set to true to remove excluded words, false to allow them.
@return string
|
urlify
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function asciify($text, $locale = '')
{
if (!strlen($locale)) {
$locale = \Localization::activeLocale();
}
$language = substr($locale, 0, strcspn($locale, '_'));
$text = \URLify::downcode($text, $language);
if (preg_match('/[^\\t\\r\\n\\x20-\\x7e]/', $text)) {
if (function_exists('iconv')) {
$t = @iconv(APP_CHARSET, 'US-ASCII//IGNORE//TRANSLIT', $text);
if (is_string($t)) {
$text = $t;
}
}
$text = preg_replace('/[^\\t\\r\\n\\x20-\\x7e]/', '', $text);
}
return $text;
}
|
Takes text and converts it to an ASCII-only string (characters with code between 32 and 127, plus \t, \n and \r).
@param string $text The text to be converted.
@param string $locale ='' The locale for the string. If not specified we consider the current locale.
@return string
|
asciify
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function filterNonAlphaNum($val)
{
return preg_replace('/[^[:alnum:]]/', '', $val);
}
|
Strips out non-alpha-numeric characters.
@param string $val
@return string
|
filterNonAlphaNum
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function highlightSearch($value, $searchString)
{
if (strlen($value) < 1 || strlen($searchString) < 1) {
return $value;
}
preg_match_all("/$searchString+/i", $value, $matches);
if (is_array($matches[0]) && count($matches[0]) > 0) {
return str_replace($matches[0][0], '<em class="ccm-highlight-search">' . $matches[0][0] . '</em>', $value);
}
return $value;
}
|
Highlights a string within a string with the class ccm-highlight-search.
@param string $value
@param string $searchString
@return string
|
highlightSearch
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function formatXML($xml)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadXML($xml);
$dom->formatOutput = true;
return $dom->saveXml();
}
|
Formats a passed XML string nicely.
@param $xml
@return string
|
formatXML
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function appendXML(\SimpleXMLElement $root, \SimpleXMLElement $new)
{
$node = $root->addChild($new->getName(), (string) $new);
foreach ($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
foreach ($new->children() as $ch) {
$this->appendXML($node, $ch);
}
}
|
Appends a SimpleXMLElement to a SimpleXMLElement.
@param \SimpleXMLElement $root
@param \SimpleXMLElement $new
|
appendXML
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Text.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Text.php
|
MIT
|
public function shortenURL($strURL)
{
$file = Loader::helper('file');
$url = $file->getContents("http://tinyurl.com/api-create.php?url=".$strURL);
return $url;
}
|
Shortens a given url with the tiny url api.
@param string $strURL
@return string $url
|
shortenURL
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Url.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Url.php
|
MIT
|
public function getBool(?SimpleXMLElement $elementOrAttribute, ?bool $default = false): ?bool
{
/*
* $element['nonExistingAttribute'] returns null
* $element->nonExistingChildElement returns a SimpleXMLElement whose name is an empty string
* (yep! "isset($element->nonExistingChildElement)" return false, but "$element->nonExistingChildElement" is a SimpleXMLElement !)
*/
if ($elementOrAttribute === null || $elementOrAttribute->getName() === '') {
return $default;
}
return filter_var((string) $elementOrAttribute, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? $default;
}
|
Extract a boolean from an element or an attribute value.
@param bool|null $default what should be returned if the element/attribute does not exist, or if it does not contain a boolean representation ('true', 'yes', 'on', '1', 'false', 'no', '0', '')
@return bool|null returns NULL if and only if $default is null and the attribute/element doesn't exist (or it has an invalid value)
|
getBool
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Xml.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Xml.php
|
MIT
|
public function createChildElement(SimpleXMLElement $parentElement, string $childName, $value, int $flags = 0): SimpleXMLElement
{
$serialized = $this->serialize($value);
$hasCarriageReturns = str_contains($serialized, "\r");
if ($hasCarriageReturns && ($flags & static::FLAG_PRESERVE_CARRIAGE_RETURNS) === static::FLAG_PRESERVE_CARRIAGE_RETURNS) {
// We can't use CDATA if we want to preserve \r - see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends
$newElement = $parentElement->addChild($childName, $serialized);
} elseif ($this->shouldUseCData($serialized)) {
$newElement = $parentElement->addChild($childName);
$this->appendCData($newElement, $serialized);
} else {
if ($hasCarriageReturns) {
$serialized = strtr($serialized, ["\r\n" => "\n", "\r" => "\n"]);
}
$newElement = $parentElement->addChild($childName, $serialized);
}
return $newElement;
}
|
Create a new element with the specified value.
@param scalar|\DateTimeInterface|\Stringable $value
|
createChildElement
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Xml.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Xml.php
|
MIT
|
public function appendCData(SimpleXMLElement $element, string $value): void
{
$domElement = dom_import_simplexml($element);
$domElement->appendChild($domElement->ownerDocument->createCDataSection($value));
}
|
Append a new CDATA section to an existing element.
Please remark that \r\n and \r sequences will be converted to \n - see https://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends
|
appendCData
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Xml.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Xml.php
|
MIT
|
protected function shouldUseCData(string $value): bool
{
return strpbrk($value, "&<>") !== false; // '>' is not strictly required, only ']]>' is, but libxml2 escapes even '>' alone
}
|
Using a CDATA section is not mandatory: it'd be enough to call htmlentities(..., ENT_XML1).
But CDATA is much more readable, so let's use it when values contains characters that
must be escaped).
@see https://www.w3.org/TR/2008/REC-xml-20081126/#syntax
|
shouldUseCData
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Xml.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Xml.php
|
MIT
|
public function containsString($needle, $haystack = array(), $recurse = true)
{
/** @var \Concrete\Core\Utility\Service\Validation\Strings $stringHelper */
$stringHelper = Core::make('helper/validation/strings');
if (!$stringHelper->notempty($needle)) {
return false;
}
$arr = (!is_array($haystack)) ? array($haystack) : $haystack; // turn the string into an array
foreach ($arr as $item) {
if ($stringHelper->notempty($item) && strstr($item, $needle) !== false) {
return true;
} elseif ($recurse && is_array($item) && $this->containsString($needle, $item)) {
return true;
}
}
return false;
}
|
Returns true if any string in the "haystack" contains the "needle".
@param string $needle The string to search for
@param string|array $haystack An array of strings to be searched (Can also contain sub arrays)
@param bool $recurse Defaults to true, when enabled this function will check any arrays inside the haystack for
a containing value when false it will only check the first level
@return bool
|
containsString
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Arrays.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Arrays.php
|
MIT
|
public function integer($data, $min = null, $max = null)
{
if (is_int($data)) {
$result = true;
} elseif (is_string($data)) {
$iv = (string) (int) $data;
$result = $data === $iv;
} else {
$result = false;
}
if ($result === true && $min !== null && (int) $data < (int) $min) {
$result = false;
}
if ($result === true && $max !== null && (int) $data > (int) $max) {
$result = false;
}
return $result;
}
|
Tests whether the passed item is an integer.
Since this is frequently used by the form helper we're not checking whether the TYPE of data is an integer,
but whether the passed argument represents a valid text/string version of an integer.
@param mixed $data
@param int|null $min the minimum acceptable value of the integer (pass NULL to not check the minimum value)
@param int|null $max the maximum acceptable value of the integer (pass NULL to not check the maximum value)
@return bool
|
integer
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Numbers.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Numbers.php
|
MIT
|
public function number($data, $min = null, $max = null)
{
switch (gettype($data)) {
case 'integer':
case 'double':
$result = true;
break;
case 'string':
$result = preg_match('/^-?(\d+(\.\d*)?|\.\d+)$/', $data) ? true : false;
break;
default:
$result = false;
break;
}
if ($result === true && $min !== null && (float) $data < (float) $min) {
$result = false;
}
if ($result === true && $max !== null && (float) $data > (float) $max) {
$result = false;
}
return $result;
}
|
Tests whether the passed item is an integer or a floating point number.
Since this is frequently used by the form helper we're not checking whether the TYPE of data is an integer or a float,
but whether the passed argument represents a valid text/string version of an integer or a float.
@param mixed $data
@param int|float|null $min the minimum acceptable value of the number (pass NULL to not check the minimum value)
@param int|float|null $max the maximum acceptable value of the number (pass NULL to not check the maximum value)
@return bool
|
number
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Numbers.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Numbers.php
|
MIT
|
public function email($em, $testMXRecord = false, $strict = false)
{
$validator = $this->app->make(EmailValidator::class, ['testMXRecord' => $testMXRecord, 'strict' => $strict]);
return $validator->isValid($em);
}
|
@deprecated Use Concrete\Core\Validator\String\EmailValidator
@param string $em
@param bool $testMXRecord
@param bool $strict
@return bool
|
email
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function handle($handle)
{
return $this->notempty($handle) && !preg_match("/[^A-Za-z0-9\_]/", $handle);
}
|
Returns true if the passed string is a valid "handle" (e.g. only letters, numbers, or a _ symbol).
@param string $handle
@return bool
|
handle
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function notempty($field)
{
return is_string($field) && trim($field) !== '';
}
|
Returns false if the string is empty (including trim()).
@param string $field
@return bool
|
notempty
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function min($str, $length)
{
return $this->notempty($str) && strlen(trim($str)) >= $length;
}
|
Returns true on whether the passed string is larger or equal to the passed length.
@param string $str
@param int $length
@return bool
|
min
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function max($str, $length)
{
return $this->notempty($str) && strlen(trim($str)) <= $length;
}
|
Returns true on whether the passed is smaller or equal to the passed length.
@param string $str
@param int $length
@return bool
|
max
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function containsNumber($str)
{
if (!$this->notempty($str)) {
return 0;
}
return strlen(preg_replace('/([^0-9]*)/', '', $str));
}
|
Returns 0 if there are no numbers in the string, or returns the number of numbers in the string.
@param string $str
@return int
|
containsNumber
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function containsUpperCase($str)
{
if (!$this->notempty($str)) {
return 0;
}
return strlen(preg_replace('/([^A-Z]*)/', '', $str));
}
|
Returns 0 if there are no upper case letters in the string, or returns the number of upper case letters in the string.
@param string $str
@return int
|
containsUpperCase
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function containsLowerCase($str)
{
if (!$this->notempty($str)) {
return 0;
}
return strlen(preg_replace('/([^a-z]*)/', '', $str));
}
|
Returns 0 if there are no lower case letters in the string, or returns the number of lower case letters in the string.
@param string $str
@return int
|
containsLowerCase
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function containsSymbol($str)
{
if (!$this->notempty($str)) {
return 0;
}
return strlen(trim(preg_replace('/([a-zA-Z0-9]*)/', '', $str)));
}
|
Returns 0 if there are no symbols in the string, or returns the number of symbols in the string.
@param string $str
@return int
|
containsSymbol
|
php
|
concretecms/concretecms
|
concrete/src/Utility/Service/Validation/Strings.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Utility/Service/Validation/Strings.php
|
MIT
|
public function sanitizeString($string)
{
if (is_object($string) && method_exists($string, '__toString')) {
$string = (string) $string;
} elseif (is_scalar($string) || $string === null) {
$string = (string) $string;
} else {
return false;
}
$string = preg_replace('/<.*?>/ms', '', $string);
$string = preg_replace('/<.*/ms', '', $string);
return $string;
}
|
Remove everything between < and > (or from < to the end of the string).
@param string|mixed $string
@return string|false return false if $string is not a scalar (or not an object with a __toString method), the sanitized string otherwise
|
sanitizeString
|
php
|
concretecms/concretecms
|
concrete/src/Validation/SanitizeService.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/SanitizeService.php
|
MIT
|
public function getWord()
{
return $this->word;
}
|
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
getWord
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public function getID()
{
return $this->id;
}
|
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
getID
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public function setWord($word)
{
if ($word == false) {
return $this->delete();
}
if (is_object($this->entity)) {
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$this->entity->setWord($word);
$em->persist($this->entity);
$em->flush();
}
$this->word = $word;
}
|
@param $word
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
setWord
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public function init($id, $word)
{
if ($this->id && $this->word) {
throw new \Exception(t('Banned word already initialized.'));
}
$this->id = $id;
$this->word = $word;
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$repository = $em->getRepository(\Concrete\Core\Entity\Validation\BannedWord::class);
$this->entity = $repository->find($id);
}
|
@param $id
@param $word
@throws \Exception
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
init
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public function delete()
{
if (is_object($this->entity)) {
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$em->remove($this->entity);
$em->flush();
$this->entity = null;
}
}
|
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
delete
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public static function getByID($id)
{
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$repository = $em->getRepository(\Concrete\Core\Entity\Validation\BannedWord::class);
/** @var \Concrete\Core\Entity\Validation\BannedWord $word */
$word = $repository->find($id);
if (!is_object($word)) {
return false;
}
return new static($word->getID(), $word->getWord());
}
|
@param $id
@return BannedWord|false
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
getByID
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public static function getByWord($word)
{
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$repository = $em->getRepository(\Concrete\Core\Entity\Validation\BannedWord::class);
$word = strtolower($word);
/** @var \Concrete\Core\Entity\Validation\BannedWord $bw */
$bw = $repository->findOneBy(['bannedWord' => $word]);
if (!is_object($bw)) {
return false;
}
return new static($bw->getID(), $bw->getWord());
}
|
@param $word
@return BannedWord|false
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
getByWord
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public static function add($word)
{
if (!$word) {
return false;
}
$app = Application::getFacadeApplication();
/** @var EntityManagerInterface $em */
$em = $app->make(EntityManagerInterface::class);
$word = strtolower($word);
if ($bw = static::getByWord($word)) {
return $bw;
}
$bw = new \Concrete\Core\Entity\Validation\BannedWord();
$bw->setWord($word);
$em->persist($bw);
$em->flush();
return new static($bw->getID(), $bw->getWord());
}
|
@param $word
@return BannedWord|false
@deprecated Use \Concrete\Core\Entity\Validation\BannedWord instead
|
add
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/BannedWord.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/BannedWord.php
|
MIT
|
public function hasBannedWords(string $string)
{
$hasBannedWords = false;
$bannedWords = $this->getBannedWords();
foreach ($bannedWords as $bannedWord) {
if ($this->hasBannedWord($bannedWord, $string)) {
$hasBannedWords = true;
break;
}
}
return $hasBannedWords;
}
|
Check if the given sentence has the banned words.
@param string $string The sentence to check for
@return bool
|
hasBannedWords
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/Service.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/Service.php
|
MIT
|
public function hasBannedWord(string $bannedWord, string $sentence): bool
{
$hasBannedWord = false;
$escaped = preg_replace(['/(?!\*)\P{L}/u', '/\*/'], ['', '(\w+|)'], $bannedWord);
if (!empty($escaped)) {
$pattern = '/^' . $escaped . '$/ui';
$strings = $this->explodeString($sentence);
foreach ($strings as $chunk) {
if (preg_match($pattern, $chunk)) {
$hasBannedWord = true;
break;
}
}
}
return $hasBannedWord;
}
|
Check if the given sentence has the banned word.
@param string $bannedWord The banned word to search for, can contain * as a wildcard, case insensitive
@param string $sentence The string being searched, also known as the haystack
@return bool
|
hasBannedWord
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/Service.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/Service.php
|
MIT
|
public function explodeString(string $string): array
{
$result = preg_split('/((\p{P}*\s+\p{P}*)|(\p{P}))/u', $string);
if ($result === false) {
$result = [];
}
return $result;
}
|
Explode string by whitespaces, line separators, tabs, punctuations, etc.
@param string $string Original string to explode
@return string[] Array of words
|
explodeString
|
php
|
concretecms/concretecms
|
concrete/src/Validation/BannedWord/Service.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/BannedWord/Service.php
|
MIT
|
public function getErrorMessage()
{
$app = Application::getFacadeApplication();
$request = $app->make(Request::class);
if ($request->isXmlHttpRequest()) {
return t("Invalid token. Please reload the page and retry.");
} else {
return t("Invalid form token. Please reload this form and submit again.");
}
}
|
Get the error message to be shown to the users when a token is not valid.
@return string
|
getErrorMessage
|
php
|
concretecms/concretecms
|
concrete/src/Validation/CSRF/Token.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/CSRF/Token.php
|
MIT
|
public function output($action = '', $return = false)
{
$hash = $this->generate($action);
$token = '<input type="hidden" name="' . static::DEFAULT_TOKEN_NAME . '" value="' . $hash . '" />';
if (!$return) {
echo $token;
} else {
return $token;
}
}
|
Create the HTML code of a token.
@param string $action An optional identifier of the token
@param bool $return Set to true to return the generated code, false to print it out
@return string|void
|
output
|
php
|
concretecms/concretecms
|
concrete/src/Validation/CSRF/Token.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/CSRF/Token.php
|
MIT
|
public function generate($action = '', $time = null)
{
$app = Application::getFacadeApplication();
$u = $app->make(User::class);
$uID = $u->getUserID();
if (!$uID) {
$uID = 0;
}
if (!$time) {
$time = time();
}
$app = Application::getFacadeApplication();
$config = $app->make('config/database');
$hash = $time . ':' . md5($time . ':' . $uID . ':' . $action . ':' . $config->get('concrete.security.token.validation'));
return $hash;
}
|
Generates a token for a given action. This is a token in the form of time:hash, where hash is md5(time:userID:action:pepper).
@param string $action An optional identifier of the token
@param int $time The UNIX timestamp to be used to determine the token expiration
@return string
|
generate
|
php
|
concretecms/concretecms
|
concrete/src/Validation/CSRF/Token.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/CSRF/Token.php
|
MIT
|
public function getParameter($action = '')
{
$hash = $this->generate($action);
return static::DEFAULT_TOKEN_NAME . '=' . $hash;
}
|
Generate a token and return it as a query string variable (eg 'ccm_token=...').
@param string $action
@return string
|
getParameter
|
php
|
concretecms/concretecms
|
concrete/src/Validation/CSRF/Token.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/CSRF/Token.php
|
MIT
|
public function validate($action = '', $token = null)
{
$app = Application::getFacadeApplication();
if ($token == null) {
$request = $app->make(Request::class);
$token = $request->request->get(static::DEFAULT_TOKEN_NAME);
if ($token === null) {
$token = $request->query->get(static::DEFAULT_TOKEN_NAME);
}
}
if (is_string($token)) {
$parts = explode(':', $token);
if ($parts[0] && isset($parts[1])) {
$time = $parts[0];
$hash = $parts[1];
$compHash = $this->generate($action, $time);
$now = time();
if (substr($compHash, strpos($compHash, ':') + 1) == $hash) {
$diff = $now - $time;
//hash is only valid if $diff is less than VALID_HASH_TIME_RECORD
return $diff <= static::VALID_HASH_TIME_THRESHOLD;
} else {
$logger = $app->make('log/factory')->createLogger(Channels::CHANNEL_SECURITY);
$u = $app->make(User::class);
$logger->debug(t('Validation token did not match'), [
'uID' => $u->getUserID(),
'action' => $action,
'time' => $time,
]);
}
}
}
return false;
}
|
Validate a token against a given action.
Basically, we check the passed hash to see if:
a. the hash is valid. That means it computes in the time:action:pepper format
b. the time included next to the hash is within the threshold.
@param string $action The action that should be associated to the token
@param string $token The token to be validated (if empty we'll retrieve it from the current request)
@return bool
|
validate
|
php
|
concretecms/concretecms
|
concrete/src/Validation/CSRF/Token.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validation/CSRF/Token.php
|
MIT
|
public function setRequirementString($code, $message)
{
if (!$this->isTranslatableStringValueValid($message)) {
throw new InvalidArgumentException('Invalid translatable string value provided for Validator');
}
$this->translatable_requirements[$code] = $message;
}
|
{@inheritdoc}
@see \Concrete\Core\Validator\TranslatableValidatorInterface::setRequirementString()
|
setRequirementString
|
php
|
concretecms/concretecms
|
concrete/src/Validator/AbstractTranslatableValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/AbstractTranslatableValidator.php
|
MIT
|
public function setErrorString($code, $message)
{
if (!$this->isTranslatableStringValueValid($message)) {
throw new InvalidArgumentException('Invalid translatable string value provided for Validator');
}
$this->translatable_errors[$code] = $message;
}
|
{@inheritdoc}
@see \Concrete\Core\Validator\TranslatableValidatorInterface::setErrorString()
|
setErrorString
|
php
|
concretecms/concretecms
|
concrete/src/Validator/AbstractTranslatableValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/AbstractTranslatableValidator.php
|
MIT
|
public function getRequirementStrings()
{
$map = $this->translatable_requirements;
foreach ($map as $key => &$value) {
if ($value instanceof Closure) {
$value = $value($this, $key);
}
}
return $map;
}
|
{@inheritdoc}
@see \Concrete\Core\Validator\ValidatorInterface::getRequirementStrings()
|
getRequirementStrings
|
php
|
concretecms/concretecms
|
concrete/src/Validator/AbstractTranslatableValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/AbstractTranslatableValidator.php
|
MIT
|
protected function getErrorString($code, $value, $default = null)
{
if (array_key_exists($code, $this->translatable_errors)) {
$resolver = $this->translatable_errors[$code];
if ($resolver instanceof Closure) {
return $resolver($this, $code, $value);
} else {
return $resolver;
}
}
return $default;
}
|
Get an error string given a code and a passed value.
@param int $code
@param mixed $value
@param mixed $default
@return string|mixed Returns a string or $default
|
getErrorString
|
php
|
concretecms/concretecms
|
concrete/src/Validator/AbstractTranslatableValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/AbstractTranslatableValidator.php
|
MIT
|
protected function isTranslatableStringValueValid($value)
{
return $value instanceof Closure || is_string($value);
}
|
Check to see if $value a valid stand in for a translatable string.
@param \Closure|string|mixed $value
@return bool
|
isTranslatableStringValueValid
|
php
|
concretecms/concretecms
|
concrete/src/Validator/AbstractTranslatableValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/AbstractTranslatableValidator.php
|
MIT
|
public function __construct(Closure $validator_closure, Closure $requirements_closure)
{
$this->validator_closure = $validator_closure;
$this->requirements_closure = $requirements_closure;
}
|
ClosureValidator constructor.
@param \Closure $validator_closure function(ClosureValidator $validator, mixed $passed, \Concrete\Core\Error $error = null): bool
@param \Closure $requirements_closure function(ClosureValidator $validator): array
@example
|
__construct
|
php
|
concretecms/concretecms
|
concrete/src/Validator/ClosureValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/ClosureValidator.php
|
MIT
|
public function setValidatorClosure(Closure $validator_closure)
{
$this->validator_closure = $validator_closure;
}
|
Set the closure that handles validation.
@param \Closure $validator_closure
@example function(ClosureValidator $validator, mixed $passed, \Concrete\Core\Error $error = null): bool
|
setValidatorClosure
|
php
|
concretecms/concretecms
|
concrete/src/Validator/ClosureValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/ClosureValidator.php
|
MIT
|
public function setRequirementsClosure(Closure $requirements_closure)
{
$this->requirements_closure = $requirements_closure;
}
|
Set the closure that returns requirements.
@param \Closure $requirements_closure
@example function(ClosureValidator $validator): array
|
setRequirementsClosure
|
php
|
concretecms/concretecms
|
concrete/src/Validator/ClosureValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/ClosureValidator.php
|
MIT
|
public function getRequirementStrings()
{
$closure = $this->requirements_closure;
return $closure($this);
}
|
{@inheritdoc}
@see \Concrete\Core\Validator\ValidatorInterface::getRequirementStrings()
|
getRequirementStrings
|
php
|
concretecms/concretecms
|
concrete/src/Validator/ClosureValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/ClosureValidator.php
|
MIT
|
public function isValid($mixed, ?ArrayAccess $error = null)
{
$closure = $this->validator_closure;
return $closure($this, $mixed, $error);
}
|
{@inheritdoc}
@see \Concrete\Core\Validator\ValidatorInterface::isValid()
|
isValid
|
php
|
concretecms/concretecms
|
concrete/src/Validator/ClosureValidator.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/ClosureValidator.php
|
MIT
|
public function register()
{
$this->app->singleton('validator/password', function () {
$this->config = $this->app->make('config');
$manager = $this->app->make(ValidatorForSubjectManager::class);
$this->applyLengthValidators($manager);
$this->applyStringRequirementValidators($manager);
$this->applyRegexRequirements($manager);
$this->applyPasswordReuseValidator($manager);
return $manager;
});
}
|
{@inheritdoc}
@see \Concrete\Core\Foundation\Service\Provider::register()
|
register
|
php
|
concretecms/concretecms
|
concrete/src/Validator/PasswordValidatorServiceProvider.php
|
https://github.com/concretecms/concretecms/blob/master/concrete/src/Validator/PasswordValidatorServiceProvider.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.