/** * @method OC_Shorty_Type::validate * @brief Validates a given value against a type specific re gular expression * Validates a given value according to the claimed type of the value. * Validation is done by matching the value against a type s pecific regular expression. * @param mixed value: Value to be verified according to the specified type * @param OC_Shorty_Type::type type: Type the value is said to belong to, important for verification * @param bool strict: Flag indicating if the verification s hould be done strict, that is if an exception should be thrown in case of a failure * @return mixed|NULL The value itself in case of a positive validation, NULL or an exception in case of a failure, depending on the flag indication strict mode * @throws error Indicating a failed validation in case of s trict mode * @access public * @author Christian Reiner */ static function validate ( $value, $type, $strict=FALSE ) { switch ( $type ) { case self::ID: if ( preg_match ( '/^'.self::$RX['SHORTY_ID'].'$/i', $value ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::STATUS: if ( in_array($value,OC_Shorty_Type::$STATUS) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::SORTKEY: if ( array_key_exists ( trim($value), self::$SORTING ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::SORTVAL: if ( in_array ( trim($value), self::$SORTING ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::JSON: if ( NULL !== json_decode($value) ) return $value; elseif ( '' === $value ) return '{}'; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::STRING: if ( preg_match ( '/^.*$/x', str_replace("\n","\\n",$value) ) ) return str_replace("\n","\\n",$value); elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::URL: $pattern = '/^'.self::$RX['URL_SCHEME'].'\:\/\/([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*'.self::$RX['DOMAIN_NAME'].'(\:'.self::$RX['NUMBER'].')*(\/($|.+)?)*$/i'; if ( parse_url($value) && preg_match ( $pattern, OC_Shorty_Tools::idnToASCII($value) ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::PATH: $pattern = '/^'.self::$RX['PATH'].'$/'; if ( preg_match ( $pattern, $value ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::INTEGER: if ( preg_match ( '/^'.self::$RX['NUMBER'].'$/', $value ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::FLOAT: if ( preg_match ( '/^'.self::$RX['NUMBER'].'(\.'.self::$RX['NUMBER'].')?$/', $value ) ) return $value; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::TIMESTAMP: if ( preg_match ( '/^'.self::$RX['TIMESTAMP'].'$/', $value ) ) return $value; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::DATE: if (FALSE!==($time=strtotime($value))) return $time; elseif ( ! $strict) return NULL; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); case self::BOOLEAN: if ( OC_Shorty_Tools::toBoolean(trim($value),$strict) ) return TRUE; elseif ( ! $strict) return FALSE; throw new OC_Shorty_Exception ( "invalid value '%s' for type '%s'", array( ((CL<strlen($value))?$value:substr($value,0,(CL-3)).'…'),$type) ); } // switch $type throw new OC_Shorty_Exception ( "unknown request argument type '%s'", array($type) ); } // function validate
static function enrichMetaDataCurl ( $url, &$meta ) { // to fetch meta data we rely on curl being installed if ( ! function_exists('curl_init') ) return; // try to retrieve the meta data $handle = curl_init ( ); curl_setopt ( $handle, CURLOPT_URL, OC_Shorty_Tools::idnToASCII($url) ); curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt ( $handle, CURLOPT_MAXREDIRS, 10 ); if ( FALSE!==($page=curl_exec($handle)) ) { // try to extract title from page preg_match ( "/<head[^>]*>.*<title>(.*)<\/title>.*<\/head>/si", $page, $match ); // $meta['title'] = isset($match[1]) ? htmlspecialchars_decode ( trim($match[1]) ) : ''; $meta['title'] = isset($match[1]) ? html_entity_decode ( trim($match[1]), ENT_COMPAT, 'UTF-8' ) : ''; $meta['staticon'] = self::selectIcon ( 'state', TRUE ); // final url after a possible redirection $meta['final'] = curl_getinfo ( $handle, CURLINFO_EFFECTIVE_URL ); // try to extract favicon from page preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match ); if (1<sizeof($match)) { // the specified uri might be an url, an absolute or a relative path // we have to turn it into an url to be able to display it out of context $favicon = htmlspecialchars_decode ( $match[2] ); // test for an url if (parse_url($favicon,PHP_URL_SCHEME)) { $meta['favicon'] = $favicon; } // test for an absolute path elseif ( 0===strpos(parse_url($favicon,PHP_URL_PATH),'/') ) { $url_token = parse_url($meta['final']); $meta['favicon'] = sprintf( '%s://%s/%s', $url_token['scheme'], $url_token['host'], $favicon ); } // so it appears to be a relative path else { $url_token = parse_url($meta['final']); $meta['favicon'] = sprintf( '%s://%s%s%s', $url_token['scheme'], $url_token['host'], dirname($url_token['path']), $favicon ); } } $meta['mimetype'] = preg_replace ( '/^([^;]+);.*/i', '$1', curl_getinfo($handle,CURLINFO_CONTENT_TYPE) ); $meta['mimicon'] = self::selectIcon ( 'mimetype', $meta['mimetype'] ); $meta['code'] = curl_getinfo ( $handle, CURLINFO_HTTP_CODE ); $meta['status'] = OC_Shorty_L10n::t ( self::selectCode('status',$meta['code']) ); $meta['explanation'] = OC_Shorty_L10n::t ( self::selectCode('explanation',$meta['code']) ); } // if curl_close ( $handle ); // that's it ! } // function enrichMetaDataCurl