protected static function requestField($map, $fieldName, CInputFilter $inputFilter, &$success) { $success = true; // Account for the fact that, with PHP, GET and POST (and cookie) fields arrive having "." replaced with "_" // in their names. $fieldName = CString::replace($fieldName, ".", "_"); $value; $hasField = false; if (CMap::hasKey($map, $fieldName)) { $value = $map[$fieldName]; if (isset($value)) { if (!is_cstring($value) && !is_cmap($value)) { // Should not happen in the known versions of PHP. assert('false', vs(isset($this), get_defined_vars())); $success = false; return $inputFilter->defaultValue(); } if (!self::$ms_treatEmptyRequestValuesAsAbsent) { $hasField = true; } else { if (is_cstring($value)) { $hasField = !CString::isEmpty($value); } else { $hasField = !CMap::isEmpty($value) && !(CMap::length($value) == 1 && CMap::hasKey($value, 0) && is_cstring($value[0]) && CString::isEmpty($value[0])); } } } } if (!$hasField) { $success = false; return $inputFilter->defaultValue(); } $inputFilterOrFilterCollection; if ($inputFilter->expectedType() != CInputFilter::CARRAY && $inputFilter->expectedType() != CInputFilter::CMAP) { $inputFilterOrFilterCollection = $inputFilter; } else { $inputFilterOrFilterCollection = $inputFilter->collectionInputFilters(); } // Recursively convert any PHP array that has sequential keys and for which CArray type is expected into a // CArray, while leaving PHP arrays for which CMap type is expected untouched. $value = self::recurseValueBeforeFiltering($value, $inputFilterOrFilterCollection, $success, 0); if (!$success) { return $inputFilter->defaultValue(); } return $inputFilter->filter($value, $success); }
/** * Determines if the URL in a specified string is valid. * * @param string $url The URL string to be looked into. * @param bool $ignoreProtocolAbsence **OPTIONAL. Default is** `false`. Tells whether the URL in the string may * still be considered valid even if it does not indicate any protocol. * * @return bool `true` if the URL in the string is valid, `false` otherwise. */ public static function isValid($url, $ignoreProtocolAbsence = false) { assert('is_cstring($url) && is_bool($ignoreProtocolAbsence)', vs(isset($this), get_defined_vars())); $parsedUrl = parse_url($url); if (!is_cmap($parsedUrl)) { return false; } if ($ignoreProtocolAbsence && !CMap::hasKey($parsedUrl, "scheme")) { // No protocol seems to be specified, try with the default one. $url = self::DEFAULT_PROTOCOL . "://{$url}"; $parsedUrl = parse_url($url); if (!is_cmap($parsedUrl)) { return false; } if (!CMap::hasKey($parsedUrl, "scheme")) { return false; } } if (is_cstring(filter_var($url, FILTER_VALIDATE_URL))) { return true; } else { if (CMap::hasKey($parsedUrl, "host")) { // The `filter_var` function could fail to recognize an IPv6 as the URL's host (enclosed in square // brackets), so, in case of a valid IPv6 being the host, replace it with an IPv4 and give the URL another // try. $host = $parsedUrl["host"]; if (CRegex::find($host, "/^\\[.*\\]\\z/")) { $host = CString::substr($host, 1, CString::length($host) - 2); if (CIp::isValidV6($host)) { // Should not influence the validity if the string is present anywhere else. $url = CString::replace($url, "[{$host}]", "127.0.0.1"); if (is_cstring(filter_var($url, FILTER_VALIDATE_URL)) && is_cmap(parse_url($url))) { return true; } } } } } return false; }
protected static function makeEnName($name) { $name = CString::replace($name, "/", ", "); $name = CString::replace($name, "_", " "); $name = CRegex::replace($name, "/[^A-Z0-9\\-+,]/i", " "); $name = CString::normSpacing($name); return $name; }
/** * Executes a command pipeline, e.g. "command1 | command2", and returns the output. * * Using this method instead of `execCommand` method ensures that, if a command in the pipeline fails, so does the * entire pipeline. * * @param string $commands The command pipeline to be executed. * @param reference $success **OPTIONAL. OUTPUT.** After the method is called with this parameter provided, the * parameter's value tells whether the pipeline was run successfully. * * @return CUStringObject The output of the command pipeline. */ public static function execCommandPipe($commands, &$success = null) { assert('is_cstring($commands)', vs(isset($this), get_defined_vars())); $commands = CString::replace($commands, "\"", "'"); $command = "bash -c \"set -o pipefail && {$commands}\""; return self::execCommand($command, $success); }
/** * Composes a URL query into a query string ready to be used as a part of a URL and returns it. * * Any characters that cannot be represented literally in a valid query string come out percent-encoded. The * resulting query string never starts with "?". * * Because the characters in field named and field values are stored in their literal representations, the * resulting query string is always normalized, with only those characters appearing percent-encoded that really * require it for the query string to be valid and with the hexadecimal letters in percent-encoded characters * appearing uppercased. Also, no duplicate fields are produced in the resulting query string (even if the object * was constructed from a query string with duplicate fields in it) and "=" is added after any field name that goes * without a value and is not followed by "=". * * @param bool $sortFields **OPTIONAL. Default is** `false`. Tells whether the fields in the query string should * appear sorted in the ascending order, case-insensitively, and with natural order comparison used for sorting. * * @return CUStringObject The query string. */ public function queryString($sortFields = false) { assert('is_bool($sortFields)', vs(isset($this), get_defined_vars())); if (!CMap::isEmpty($this->m_query)) { $useQuery = CMap::makeCopy($this->m_query); // Recursively convert any CArray into a CMap for `http_build_query` function to accept the query. $useQuery = self::recurseQueryValueBeforeComposingQs($useQuery, 0); // Compose a preliminary query string. $queryString = http_build_query($useQuery, "", self::$ms_fieldDelimiters[0], PHP_QUERY_RFC1738); if (!is_cstring($queryString)) { return ""; } // Break the string into fields. $fields = CString::split($queryString, self::$ms_fieldDelimiters[0]); // Adjust the result of `http_build_query` function. $len = CArray::length($fields); for ($i = 0; $i < $len; $i++) { if (CString::find($fields[$i], "=")) { // Revert excessive percent-encoding of the square brackets next to the identifiers of // multidimensional data. $fields[$i] = CRegex::replaceWithCallback($fields[$i], "/(?:%5B(?:[^%]++|%(?!5B|5D))*+%5D)+?=/i", function ($matches) { $value = $matches[0]; $value = CString::replace($value, "%5B", "["); $value = CString::replace($value, "%5D", "]"); return $value; }); // Remove redundant indexing next to the identifiers of simple arrays. $fields[$i] = CRegex::replaceWithCallback($fields[$i], "/^.+?=/", function ($matches) { return CRegex::replace($matches[0], "/\\[\\d+\\]/", "[]"); }); } } if ($sortFields) { // Normalize the order of fields. CArray::sortStringsNatCi($fields); } $queryString = CArray::join($fields, self::$ms_fieldDelimiters[0]); return $queryString; } else { return ""; } }