array( 'header' => "Accept: text/html\r\n" . "Connection: Close\r\n" . "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64)\r\n", ) ); stream_context_get_default($defaults); if (isset($url['host']) && $url['host'] !== gethostbyname($url['host'])) { $url = "$url[scheme]://$url[host]$url[port]$path"; $headers = get_headers($url); if (is_array($headers)) { return $headers; } } return false; } /** * Add protocol prefix if necessary (and possible) * * @param string $url * 2010-06-02 ms */ public static function autoPrefixUrl($url, $prefix = null) { if ($prefix === null) { $prefix = 'http://'; } if (($pos = strpos($url, '.')) !== false) { if (strpos(substr($url, 0, $pos), '//') === false) { $url = $prefix . $url; } } return $url; } /** * Encode strings with base64_encode and also * replace chars base64 uses that would mess up the url. * * Do not use this for querystrings. Those will escape automatically. * This is only useful for named or passed params. * * @param string $string Unsafe string * @return string Encoded string * 2012-10-23 ms */ public static function urlEncode($string) { return str_replace(array('/', '='), array('-', '_'), base64_encode($string)); } /** * Decode strings with base64_encode and also * replace back chars base64 uses that would mess up the url. * * Do not use this for querystrings. Those will escape automatically. * This is only useful for named or passed params. * * @param string $string Safe string * @return string Decoded string * 2012-10-23 ms */ public static function urlDecode($string) { return base64_decode(str_replace(array('-', '_'), array('/', '='), $string)); } /** * Returns true only if all values are true. * //TODO: maybe move to bootstrap? * * @param array $array * @return boolean Result * 2011-11-02 ms */ public static function logicalAnd($array) { if (empty($array)) { return false; } foreach ($array as $result) { if (!$result) { return false; } } return true; } /** * Returns true if at least one value is true. * //TODO: maybe move to bootstrap? * * @param array $array * @return boolean Result * * 2011-11-02 ms */ public static function logicalOr($array) { foreach ($array as $result) { if ($result) { return true; } } return false; } /** * On non-transaction db connections it will return a deep array of bools instead of bool. * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too. * * @param array * @return boolean * 2012-10-12 ms */ public static function isValidSaveAll($array) { if (empty($array)) { return false; } $ret = true; foreach ($array as $key => $val) { if (is_array($val)) { $ret = $ret & Utility::logicalAnd($val); } else { $ret = $ret & $val; } } return (bool)$ret; } /** * Convenience function for automatic casting in form methods etc. * //TODO: maybe move to bootstrap? * * @param mixed $value * @param string $type * @return safe value for DB query, or NULL if type was not a valid one * 2008-12-12 ms */ public static function typeCast($value, $type) { switch ($type) { case 'int': $value = (int)$value; break; case 'float': $value = (float)$value; break; case 'double': $value = (double)$value; break; case 'array': $value = (array )$value; break; case 'bool': $value = (bool)$value; break; case 'string': $value = (string )$value; break; default: return null; } return $value; } /** * Trim recursivly * * 2009-07-07 ms */ public static function trimDeep($value) { $value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value); return $value; } /** * h() recursivly * * 2009-07-07 ms */ public static function specialcharsDeep($value) { $value = is_array($value) ? array_map('self::specialcharsDeep', $value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); return $value; } /** * Removes all except A-Z,a-z,0-9 and allowedChars (allowedChars array) recursivly * * 2009-07-07 ms */ public static function paranoidDeep($value) { $value = is_array($value) ? array_map('self::paranoidDeep', $value) : Sanatize::paranoid($value, $this->allowedChars); return $value; } /** * Transfers/removes all < > from text (remove TRUE/FALSE) * * 2009-07-07 ms */ public static function htmlDeep($value) { $value = is_array($value) ? array_map('self::htmlDeep', $value) : Sanatize::html($value, $this->removeChars); return $value; } /** * main deep method * * 2009-07-07 ms */ public static function deep($function, $value) { $value = is_array($value) ? array_map('self::' . $function, $value) : $function($value); return $value; } /** * Flattens an array. * * @param array $array to flatten * @param boolean $perserveKeys * @return array * 2011-07-02 ms */ public static function arrayFlatten($array, $preserveKeys = false) { if ($preserveKeys) { return self::_arrayFlatten($array); } if (!$array) { return array(); } $result = array(); foreach ($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, self::arrayFlatten($value)); } else { $result[$key] = $value; } } return $result; } /** * Flatten an array and preserve the keys * * @return array */ protected static function _arrayFlatten($a, $f = array()) { if (!$a) { return array(); } foreach ($a as $k => $v) { if (is_array($v)) { $f = self::_arrayFlatten($v, $f); } else { $f[$k] = $v; } } return $f; } /** * Similar to array_shift but on the keys of the array * like array_shift() only for keys and not values * * @param array $keyValuePairs * @return string $key * 2011-01-22 ms */ public static function arrayShiftKeys(&$array) { foreach ($array as $key => $value) { unset($array[$key]); return $key; } } protected static $_counterStartTime; /** * returns microtime as float value * (to be subtracted right away) * * @return float * 2009-07-07 ms */ public static function microtime($precision = 8) { return round(microtime(true), $precision); } /** * @return void * 2009-07-07 ms */ public static function startClock() { self::$_counterStartTime = self::microtime(); } /** * @return float * 2009-07-07 ms */ public static function returnElapsedTime($precision = 8, $restartClock = false) { $startTime = self::$_counterStartTime; if ($restartClock) { self::startClock(); } return self::calcElapsedTime($startTime, self::microtime(), $precision); } /** * Returns microtime as float value * (to be subtracted right away) * * @return float * 2009-07-07 ms */ public static function calcElapsedTime($start, $end, $precision = 8) { $elapsed = $end - $start; return round($elapsed, $precision); } }