コード例 #1
0
 /**
  * Constructs a new Zend_Service_Technorati instance
  * and setup character encoding.
  *
  * @param  string $apiKey  Your Technorati API key
  */
 public function __construct($apiKey)
 {
     Zend_EncodingProxy::setOutputEncoding('UTF-8');
     Zend_EncodingProxy::setInputEncoding('UTF-8');
     Zend_EncodingProxy::setInternalEncoding('UTF-8');
     $this->_apiKey = $apiKey;
 }
コード例 #2
0
 /**
  * Sets up character encoding, instantiates the HTTP client, and assigns the web service version.
  */
 public function __construct()
 {
     $this->set('version', '1.0');
     Zend_EncodingProxy::setOutputEncoding('UTF-8');
     Zend_EncodingProxy::setInputEncoding('UTF-8');
     Zend_EncodingProxy::setInternalEncoding('UTF-8');
 }
コード例 #3
0
 /**
  * Sets a new encoding to use
  *
  * @param string $encoding
  * @return Zend_Validate_StringLength
  */
 public function setEncoding($encoding = null)
 {
     if ($encoding !== null) {
         $orig = Zend_EncodingProxy::getInternalEncoding();
         $result = Zend_EncodingProxy::setInternalEncoding($encoding);
         if (!$result) {
             // require_once 'Zend/Validate/Exception.php';
             throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
         }
         Zend_EncodingProxy::setInternalEncoding($orig);
     }
     $this->_encoding = $encoding;
     return $this;
 }
コード例 #4
0
ファイル: Hostname.php プロジェクト: emagister/zendframework1
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if the $value is a valid hostname with respect to the current allow option
  *
  * @param  string $value
  * @throws Zend_Validate_Exception if a fatal error occurs for validation process
  * @return boolean
  */
 public function isValid($value)
 {
     if (!is_string($value)) {
         $this->_error(self::INVALID);
         return false;
     }
     $this->_setValue($value);
     // Check input against IP address schema
     if (preg_match('/^[0-9a-f:.]*$/i', $value) && $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
         if (!($this->_options['allow'] & self::ALLOW_IP)) {
             $this->_error(self::IP_ADDRESS_NOT_ALLOWED);
             return false;
         } else {
             return true;
         }
     }
     // RFC3986 3.2.2 states:
     //
     //     The rightmost domain label of a fully qualified domain name
     //     in DNS may be followed by a single "." and should be if it is
     //     necessary to distinguish between the complete domain name and
     //     some local domain.
     //
     // (see ZF-6363)
     // Local hostnames are allowed to be partitial (ending '.')
     if ($this->_options['allow'] & self::ALLOW_LOCAL) {
         if (substr($value, -1) === '.') {
             $value = substr($value, 0, -1);
             if (substr($value, -1) === '.') {
                 // Empty hostnames (ending '..') are not allowed
                 $this->_error(self::INVALID_LOCAL_NAME);
                 return false;
             }
         }
     }
     $domainParts = explode('.', $value);
     // Prevent partitial IP V4 adresses (ending '.')
     if (count($domainParts) == 4 && preg_match('/^[0-9.a-e:.]*$/i', $value) && $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
         $this->_error(self::INVALID_LOCAL_NAME);
     }
     // Check input against DNS hostname schema
     if (count($domainParts) > 1 && strlen($value) >= 4 && strlen($value) <= 254) {
         $status = false;
         $origenc = Zend_EncodingProxy::getInternalEncoding();
         Zend_EncodingProxy::setInternalEncoding('UTF-8');
         do {
             // First check TLD
             $matches = array();
             if (preg_match('/([^.]{2,10})$/i', end($domainParts), $matches) || end($domainParts) == 'ایران' || end($domainParts) == '中国' || end($domainParts) == '公司' || end($domainParts) == '网络') {
                 reset($domainParts);
                 // Hostname characters are: *(label dot)(label dot label); max 254 chars
                 // label: id-prefix [*ldh{61} id-prefix]; max 63 chars
                 // id-prefix: alpha / digit
                 // ldh: alpha / digit / dash
                 // Match TLD against known list
                 $this->_tld = strtolower($matches[1]);
                 if ($this->_options['tld']) {
                     if (!in_array($this->_tld, $this->_validTlds)) {
                         $this->_error(self::UNKNOWN_TLD);
                         $status = false;
                         break;
                     }
                 }
                 /**
                  * Match against IDN hostnames
                  * Note: Keep label regex short to avoid issues with long patterns when matching IDN hostnames
                  * @see Zend_Validate_Hostname_Interface
                  */
                 $regexChars = array(0 => '/^[a-z0-9\\x2d]{1,63}$/i');
                 if ($this->_options['idn'] && isset($this->_validIdns[strtoupper($this->_tld)])) {
                     if (is_string($this->_validIdns[strtoupper($this->_tld)])) {
                         $regexChars += (include $this->_validIdns[strtoupper($this->_tld)]);
                     } else {
                         $regexChars += $this->_validIdns[strtoupper($this->_tld)];
                     }
                 }
                 // Check each hostname part
                 $check = 0;
                 foreach ($domainParts as $domainPart) {
                     // Decode Punycode domainnames to IDN
                     if (strpos($domainPart, 'xn--') === 0) {
                         $domainPart = $this->decodePunycode(substr($domainPart, 4));
                         if ($domainPart === false) {
                             return false;
                         }
                     }
                     // Check dash (-) does not start, end or appear in 3rd and 4th positions
                     if (strpos($domainPart, '-') === 0 || strlen($domainPart) > 2 && strpos($domainPart, '-', 2) == 2 && strpos($domainPart, '-', 3) == 3 || strpos($domainPart, '-') === strlen($domainPart) - 1) {
                         $this->_error(self::INVALID_DASH);
                         $status = false;
                         break 2;
                     }
                     // Check each domain part
                     $checked = false;
                     foreach ($regexChars as $regexKey => $regexChar) {
                         $status = @preg_match($regexChar, $domainPart);
                         if ($status > 0) {
                             $length = 63;
                             if (array_key_exists(strtoupper($this->_tld), $this->_idnLength) && array_key_exists($regexKey, $this->_idnLength[strtoupper($this->_tld)])) {
                                 $length = $this->_idnLength[strtoupper($this->_tld)];
                             }
                             if (iconv_strlen($domainPart, 'UTF-8') > $length) {
                                 $this->_error(self::INVALID_HOSTNAME);
                             } else {
                                 $checked = true;
                                 break;
                             }
                         }
                     }
                     if ($checked) {
                         ++$check;
                     }
                 }
                 // If one of the labels doesn't match, the hostname is invalid
                 if ($check !== count($domainParts)) {
                     $this->_error(self::INVALID_HOSTNAME_SCHEMA);
                     $status = false;
                 }
             } else {
                 // Hostname not long enough
                 $this->_error(self::UNDECIPHERABLE_TLD);
                 $status = false;
             }
         } while (false);
         Zend_EncodingProxy::setInternalEncoding($origenc);
         // If the input passes as an Internet domain name, and domain names are allowed, then the hostname
         // passes validation
         if ($status && $this->_options['allow'] & self::ALLOW_DNS) {
             return true;
         }
     } else {
         if ($this->_options['allow'] & self::ALLOW_DNS) {
             $this->_error(self::INVALID_HOSTNAME);
         }
     }
     // Check for URI Syntax (RFC3986)
     if ($this->_options['allow'] & self::ALLOW_URI) {
         if (preg_match("/^([a-zA-Z0-9-._~!\$&\\'()*+,;=]|%[[:xdigit:]]{2}){1,254}\$/i", $value)) {
             return true;
         } else {
             $this->_error(self::INVALID_URI);
         }
     }
     // Check input against local network name schema; last chance to pass validation
     $regexLocal = '/^(([a-zA-Z0-9\\x2d]{1,63}\\x2e)*[a-zA-Z0-9\\x2d]{1,63}[\\x2e]{0,1}){1,254}$/';
     $status = @preg_match($regexLocal, $value);
     // If the input passes as a local network name, and local network names are allowed, then the
     // hostname passes validation
     $allowLocal = $this->_options['allow'] & self::ALLOW_LOCAL;
     if ($status && $allowLocal) {
         return true;
     }
     // If the input does not pass as a local network name, add a message
     if (!$status) {
         $this->_error(self::INVALID_LOCAL_NAME);
     }
     // If local network names are not allowed, add a message
     if ($status && !$allowLocal) {
         $this->_error(self::LOCAL_NAME_NOT_ALLOWED);
     }
     return false;
 }
コード例 #5
0
ファイル: Format.php プロジェクト: emagister/zendframework1
 /**
  * Parse date and split in named array fields
  *
  * @param   string  $date     Date string to parse
  * @param   array   $options  Options: format_type, fix_date, locale, date_format. See {@link setOptions()} for details.
  * @return  array             Possible array members: day, month, year, hour, minute, second, fixed, format
  */
 private static function _parseDate($date, $options)
 {
     if (!self::_getUniCodeSupport()) {
         trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE);
     }
     $options = self::_checkOptions($options) + self::$_options;
     $test = array('h', 'H', 'm', 's', 'y', 'Y', 'M', 'd', 'D', 'E', 'S', 'l', 'B', 'I', 'X', 'r', 'U', 'G', 'w', 'e', 'a', 'A', 'Z', 'z', 'v');
     $format = $options['date_format'];
     $number = $date;
     // working copy
     $result['date_format'] = $format;
     // save the format used to normalize $number (convenience)
     $result['locale'] = $options['locale'];
     // save the locale used to normalize $number (convenience)
     $oenc = Zend_EncodingProxy::getInternalEncoding();
     Zend_EncodingProxy::setInternalEncoding('UTF-8');
     $day = iconv_strpos($format, 'd');
     $month = iconv_strpos($format, 'M');
     $year = iconv_strpos($format, 'y');
     $hour = iconv_strpos($format, 'H');
     $min = iconv_strpos($format, 'm');
     $sec = iconv_strpos($format, 's');
     $am = null;
     if ($hour === false) {
         $hour = iconv_strpos($format, 'h');
     }
     if ($year === false) {
         $year = iconv_strpos($format, 'Y');
     }
     if ($day === false) {
         $day = iconv_strpos($format, 'E');
         if ($day === false) {
             $day = iconv_strpos($format, 'D');
         }
     }
     if ($day !== false) {
         $parse[$day] = 'd';
         if (!empty($options['locale']) && $options['locale'] !== 'root' && (!is_object($options['locale']) || (string) $options['locale'] !== 'root')) {
             // erase day string
             $daylist = Zend_Locale_Data::getList($options['locale'], 'day');
             foreach ($daylist as $key => $name) {
                 if (iconv_strpos($number, $name) !== false) {
                     $number = str_replace($name, "EEEE", $number);
                     break;
                 }
             }
         }
     }
     $position = false;
     if ($month !== false) {
         $parse[$month] = 'M';
         if (!empty($options['locale']) && $options['locale'] !== 'root' && (!is_object($options['locale']) || (string) $options['locale'] !== 'root')) {
             // prepare to convert month name to their numeric equivalents, if requested,
             // and we have a $options['locale']
             $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'], 'month'));
             if ($position === false) {
                 $position = self::_replaceMonth($number, Zend_Locale_Data::getList($options['locale'], 'month', array('gregorian', 'format', 'abbreviated')));
             }
         }
     }
     if ($year !== false) {
         $parse[$year] = 'y';
     }
     if ($hour !== false) {
         $parse[$hour] = 'H';
     }
     if ($min !== false) {
         $parse[$min] = 'm';
     }
     if ($sec !== false) {
         $parse[$sec] = 's';
     }
     if (empty($parse)) {
         Zend_EncodingProxy::setInternalEncoding($oenc);
         // require_once 'Zend/Locale/Exception.php';
         throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
     }
     ksort($parse);
     // get daytime
     if (iconv_strpos($format, 'a') !== false) {
         if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'am'))) !== false) {
             $am = true;
         } else {
             if (iconv_strpos(strtoupper($number), strtoupper(Zend_Locale_Data::getContent($options['locale'], 'pm'))) !== false) {
                 $am = false;
             }
         }
     }
     // split number parts
     $split = false;
     preg_match_all('/\\d+/u', $number, $splitted);
     if (count($splitted[0]) == 0) {
         Zend_EncodingProxy::setInternalEncoding($oenc);
         // require_once 'Zend/Locale/Exception.php';
         throw new Zend_Locale_Exception("No date part in '{$date}' found.");
     }
     if (count($splitted[0]) == 1) {
         $split = 0;
     }
     $cnt = 0;
     foreach ($parse as $key => $value) {
         switch ($value) {
             case 'd':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['day'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['day'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'M':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['month'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['month'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'y':
                 $length = 2;
                 if (iconv_substr($format, $year, 4) == 'yyyy' || iconv_substr($format, $year, 4) == 'YYYY') {
                     $length = 4;
                 }
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['year'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['year'] = iconv_substr($splitted[0][0], $split, $length);
                     $split += $length;
                 }
                 ++$cnt;
                 break;
             case 'H':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['hour'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['hour'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 'm':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['minute'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['minute'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
             case 's':
                 if ($split === false) {
                     if (count($splitted[0]) > $cnt) {
                         $result['second'] = $splitted[0][$cnt];
                     }
                 } else {
                     $result['second'] = iconv_substr($splitted[0][0], $split, 2);
                     $split += 2;
                 }
                 ++$cnt;
                 break;
         }
     }
     // AM/PM correction
     if ($hour !== false) {
         if ($am === true and $result['hour'] == 12) {
             $result['hour'] = 0;
         } else {
             if ($am === false and $result['hour'] != 12) {
                 $result['hour'] += 12;
             }
         }
     }
     if ($options['fix_date'] === true) {
         $result['fixed'] = 0;
         // nothing has been "fixed" by swapping date parts around (yet)
     }
     if ($day !== false) {
         // fix false month
         if (isset($result['day']) and isset($result['month'])) {
             if ($position !== false and (iconv_strpos($date, $result['day']) === false or isset($result['year']) and iconv_strpos($date, $result['year']) === false)) {
                 if ($options['fix_date'] !== true) {
                     Zend_EncodingProxy::setInternalEncoding($oenc);
                     // require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Unable to parse date '{$date}' using '" . $format . "' (false month, {$position}, {$month})");
                 }
                 $temp = $result['day'];
                 $result['day'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 1;
             }
         }
         // fix switched values d <> y
         if (isset($result['day']) and isset($result['year'])) {
             if ($result['day'] > 31) {
                 if ($options['fix_date'] !== true) {
                     Zend_EncodingProxy::setInternalEncoding($oenc);
                     // require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Unable to parse date '{$date}' using '" . $format . "' (d <> y)");
                 }
                 $temp = $result['year'];
                 $result['year'] = $result['day'];
                 $result['day'] = $temp;
                 $result['fixed'] = 2;
             }
         }
         // fix switched values M <> y
         if (isset($result['month']) and isset($result['year'])) {
             if ($result['month'] > 31) {
                 if ($options['fix_date'] !== true) {
                     Zend_EncodingProxy::setInternalEncoding($oenc);
                     // require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Unable to parse date '{$date}' using '" . $format . "' (M <> y)");
                 }
                 $temp = $result['year'];
                 $result['year'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 3;
             }
         }
         // fix switched values M <> d
         if (isset($result['month']) and isset($result['day'])) {
             if ($result['month'] > 12) {
                 if ($options['fix_date'] !== true || $result['month'] > 31) {
                     Zend_EncodingProxy::setInternalEncoding($oenc);
                     // require_once 'Zend/Locale/Exception.php';
                     throw new Zend_Locale_Exception("Unable to parse date '{$date}' using '" . $format . "' (M <> d)");
                 }
                 $temp = $result['day'];
                 $result['day'] = $result['month'];
                 $result['month'] = $temp;
                 $result['fixed'] = 4;
             }
         }
     }
     if (isset($result['year'])) {
         if (iconv_strlen($result['year']) == 2 && $result['year'] < 10 || (iconv_strpos($format, 'yy') !== false && iconv_strpos($format, 'yyyy') === false || iconv_strpos($format, 'YY') !== false && iconv_strpos($format, 'YYYY') === false)) {
             if ($result['year'] >= 0 && $result['year'] < 100) {
                 if ($result['year'] < 70) {
                     $result['year'] = (int) $result['year'] + 100;
                 }
                 $result['year'] = (int) $result['year'] + 1900;
             }
         }
     }
     Zend_EncodingProxy::setInternalEncoding($oenc);
     return $result;
 }
コード例 #6
0
ファイル: Client.php プロジェクト: emagister/zendframework1
 /**
  * Perform an XML-RPC request and return a response.
  *
  * @param Zend_XmlRpc_Request $request
  * @param null|Zend_XmlRpc_Response $response
  * @return void
  * @throws Zend_XmlRpc_Client_HttpException
  */
 public function doRequest($request, $response = null)
 {
     $this->_lastRequest = $request;
     Zend_EncodingProxy::setOutputEncoding('UTF-8');
     Zend_EncodingProxy::setInputEncoding('UTF-8');
     Zend_EncodingProxy::setInternalEncoding('UTF-8');
     $http = $this->getHttpClient();
     if ($http->getUri() === null) {
         $http->setUri($this->_serverAddress);
     }
     $http->setHeaders(array('Content-Type: text/xml; charset=utf-8', 'Accept: text/xml'));
     if ($http->getHeader('user-agent') === null) {
         $http->setHeaders(array('User-Agent: Zend_XmlRpc_Client'));
     }
     $xml = $this->_lastRequest->__toString();
     $http->setRawData($xml);
     $httpResponse = $http->request(Zend_Http_Client::POST);
     if (!$httpResponse->isSuccessful()) {
         /**
          * Exception thrown when an HTTP error occurs
          * @see Zend_XmlRpc_Client_HttpException
          */
         // require_once 'Zend/XmlRpc/Client/HttpException.php';
         throw new Zend_XmlRpc_Client_HttpException($httpResponse->getMessage(), $httpResponse->getStatus());
     }
     if ($response === null) {
         $response = new Zend_XmlRpc_Response();
     }
     $this->_lastResponse = $response;
     $this->_lastResponse->loadXml(trim($httpResponse->getBody()));
 }