constructUserAgentHeader($applicationName, $applicationVersion, $attributes); } private function constructUserAgentHeader($applicationName, $applicationVersion, $attributes) { $userAgent = $this->quoteApplicationName($applicationName) . '/' . $this->quoteApplicationVersion($applicationVersion); $userAgent .= ' ('; $userAgent .= 'Language=PHP/' . phpversion(); $userAgent .= '; '; $userAgent .= 'Platform=' . php_uname('s') . '/' . php_uname('m') . '/' . php_uname('r'); $userAgent .= '; '; $userAgent .= 'MWSClientVersion=' . self::MWS_CLIENT_VERSION; foreach ($attributes as $key => $value) { if (empty($value)) { throw new InvalidArgumentException("value for $key cannot be null or empty"); } $userAgent .= '; ' . $this->quoteAttributeName($key) . '=' . $this->quoteAttributeValue($value); } $userAgent .= ')'; return $userAgent; } /** * Collapse multiple whitespace characters into a single ' ' and backslash escape '\', * and '/' characters from a string. * @param $s * @return string */ private function quoteApplicationName($s) { $quotedString = $this->collapseWhitespace($s); $quotedString = preg_replace('/\\\\/', '\\\\\\\\', $quotedString); $quotedString = preg_replace('/\//', '\\/', $quotedString); return $quotedString; } /** * Collapse multiple whitespace characters into a single ' ' and backslash escape '\', * and '(' characters from a string. * * @param $s * @return string */ private function quoteApplicationVersion($s) { $quotedString = $this->collapseWhitespace($s); $quotedString = preg_replace('/\\\\/', '\\\\\\\\', $quotedString); $quotedString = preg_replace('/\\(/', '\\(', $quotedString); return $quotedString; } /** * Collapse multiple whitespace characters into a single ' ' character. * @param $s * @return string */ private function collapseWhitespace($s) { return preg_replace('/ {2,}|\s/', ' ', $s); } /** * Collapse multiple whitespace characters into a single ' ' and backslash escape '\', * and '=' characters from a string. * * @param $s * @return unknown_type */ private function quoteAttributeName($s) { $quotedString = $this->collapseWhitespace($s); $quotedString = preg_replace('/\\\\/', '\\\\\\\\', $quotedString); $quotedString = preg_replace('/\\=/', '\\=', $quotedString); return $quotedString; } /** * Collapse multiple whitespace characters into a single ' ' and backslash escape ';', '\', * and ')' characters from a string. * * @param $s * @return unknown_type */ private function quoteAttributeValue($s) { $quotedString = $this->collapseWhitespace($s); $quotedString = preg_replace('/\\\\/', '\\\\\\\\', $quotedString); $quotedString = preg_replace('/\\;/', '\\;', $quotedString); $quotedString = preg_replace('/\\)/', '\\)', $quotedString); return $quotedString; } }