Пример #1
0
 /**
  * Returns the last IP address in a comma separated list, subject to an optional exclusion list.
  *
  * @param string $csv Comma separated list of elements.
  * @param array $excludedIps Optional list of excluded IP addresses (or IP address ranges).
  * @return string Last (non-excluded) IP address in the list or an empty string if all given IPs are excluded.
  */
 public static function getLastIpFromList($csv, $excludedIps = null)
 {
     $p = strrpos($csv, ',');
     if ($p !== false) {
         $elements = explode(',', $csv);
         for ($i = count($elements); $i--;) {
             $element = trim(Common::sanitizeInputValue($elements[$i]));
             $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element));
             if (empty($excludedIps) || !in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps)) {
                 return $element;
             }
         }
         return '';
     }
     return trim(Common::sanitizeInputValue($csv));
 }
Пример #2
0
 /**
  * Returns a URL query string as an array.
  *
  * @param string $urlQuery The query string, eg, `'?param1=value1&param2=value2'`.
  * @return array eg, `array('param1' => 'value1', 'param2' => 'value2')`
  * @api
  */
 public static function getArrayFromQueryString($urlQuery)
 {
     if (strlen($urlQuery) == 0) {
         return array();
     }
     // TODO: this method should not use a cache. callers should instead have their own cache, configured through DI.
     //       one undesirable side effect of using a cache here, is that this method can now init the StaticContainer, which makes setting
     //       test environment for RequestCommand more complicated.
     $cache = \yii::$app->cache;
     $cacheKey = 'arrayFromQuery' . $urlQuery;
     if ($cache->exists($cacheKey)) {
         return $cache->get($cacheKey);
     }
     if ($urlQuery[0] == '?') {
         $urlQuery = substr($urlQuery, 1);
     }
     $separator = '&';
     $urlQuery = $separator . $urlQuery;
     //		$urlQuery = str_replace(array('%20'), ' ', $urlQuery);
     $referrerQuery = trim($urlQuery);
     $values = explode($separator, $referrerQuery);
     $nameToValue = array();
     foreach ($values as $value) {
         $pos = strpos($value, '=');
         if ($pos !== false) {
             $name = substr($value, 0, $pos);
             $value = substr($value, $pos + 1);
             if ($value === false) {
                 $value = '';
             }
         } else {
             $name = $value;
             $value = false;
         }
         if (!empty($name)) {
             $name = Common::sanitizeInputValue($name);
         }
         if (!empty($value)) {
             $value = Common::sanitizeInputValue($value);
         }
         // if array without indexes
         $count = 0;
         $tmp = preg_replace('/(\\[|%5b)(]|%5d)$/i', '', $name, -1, $count);
         if (!empty($tmp) && $count) {
             $name = $tmp;
             if (isset($nameToValue[$name]) == false || is_array($nameToValue[$name]) == false) {
                 $nameToValue[$name] = array();
             }
             array_push($nameToValue[$name], $value);
         } elseif (!empty($name)) {
             $nameToValue[$name] = $value;
         }
     }
     $cache->set($cacheKey, $nameToValue);
     return $nameToValue;
 }