/** * @param CM_Http_Request_Abstract $request * @param float|null $weight */ public function setConversion(CM_Http_Request_Abstract $request, $weight = null) { if ($request->isBotCrawler()) { return; } $this->_setConversion(new CM_Splittest_Fixture($request), $weight); }
/** * @param CM_Http_Request_Abstract $request */ public function addFromRequest(CM_Http_Request_Abstract $request) { $useragent = $request->getUserAgent(); if ('' !== $useragent) { $this->add($useragent); } }
public static function createFromRequest(CM_Http_Request_Abstract $request, CM_Site_Abstract $site, CM_Service_Manager $serviceManager) { if ($request->getPathPart(0) === 'ajax') { $request = clone $request; $request->popPathPart(0); $request->popPathLanguage(); return new self($request, $site, $serviceManager); } return null; }
public static function createFromRequest(CM_Http_Request_Abstract $request, CM_Site_Abstract $site, CM_Service_Manager $serviceManager) { if ($request->getPathPart(0) === 'upload' && $request instanceof CM_Http_Request_Post) { $request = clone $request; $request->popPathPart(0); $request->popPathLanguage(); $request->setBodyEncoding(CM_Http_Request_Post::ENCODING_NONE); return new self($request, $site, $serviceManager); } return null; }
public function getServerId(CM_Http_Request_Abstract $request) { $ipAddress = long2ip($request->getIp()); $servers = CM_Stream_Video::_getConfig()->servers; foreach ($servers as $serverId => $server) { if ($server['publicIp'] == $ipAddress || $server['privateIp'] == $ipAddress) { return (int) $serverId; } } throw new CM_Exception_Invalid('No video server with ipAddress `' . $ipAddress . '` found'); }
/** * @param CM_Http_Request_Abstract $request */ public function __construct(CM_Http_Request_Abstract $request) { $this->_request = $request; $headerList = []; foreach ($request->getHeaders() as $key => $header) { if (substr($key, 0, 8) !== 'content-') { $headerList['HTTP_' . str_replace('-', '_', strtoupper($key))] = $header; } } $this->_parser = new Jenssegers\Agent\Agent($headerList); $this->_headerList = $headerList; }
public static function createFromRequest(CM_Http_Request_Abstract $request, CM_Site_Abstract $site, CM_Service_Manager $serviceManager) { if ($request->hasPathPrefix('/serviceworker-')) { $request = clone $request; $request->setPath(str_replace('-', '/', $request->getPath())); $request->popPathPart(0); $request->popPathLanguage(); $deployVersion = $request->popPathPart(0); return new self($request, $site, $serviceManager); } return null; }
public function testGetRecordContext() { $user = CMTest_TH::createUser(); $httpRequest = CM_Http_Request_Abstract::factory('post', '/foo?bar=1&baz=quux&viewInfoList=fooBar', ['bar' => 'baz', 'host' => 'foo.bar:8080'], ['http_referer' => 'http://bar/baz', 'http_user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', 'foo' => 'quux'], '{"foo" : "bar", "quux" : "baz"}'); $clientId = $httpRequest->getClientId(); $computerInfo = new CM_Log_Context_ComputerInfo('www.example.com', 'v7.0.1'); $exception = new CM_Exception_Invalid('Bad', null, ['foo' => 'bar']); $context = new CM_Log_Context(); $context->setExtra(['bar' => 'baz', 'baz' => 'quux']); $context->setUser($user); $context->setException($exception); $context->setComputerInfo($computerInfo); $context->setHttpRequest($httpRequest); $contextFormatter = new CM_Log_ContextFormatter_Cargomedia('appName'); $formattedContext = $contextFormatter->formatContext($context); $this->assertSame('www.example.com', $formattedContext['computerInfo']['fqdn']); $this->assertSame('v7.0.1', $formattedContext['computerInfo']['phpVersion']); $this->assertSame('/foo?bar=1&baz=quux&viewInfoList=fooBar', $formattedContext['httpRequest']['uri']); $this->assertSame(join("\n", ['{', ' "bar": "1",', ' "baz": "quux",', ' "foo": "bar",', ' "quux": "baz"', '}']), $formattedContext['httpRequest']['query']); $this->assertSame('POST', $formattedContext['httpRequest']['method']); $this->assertSame('http://bar/baz', $formattedContext['httpRequest']['referer']); $this->assertSame('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_10)', $formattedContext['httpRequest']['useragent']); $this->assertSame('foo.bar', $formattedContext['httpRequest']['hostname']); $this->assertSame(['id' => $user->getId(), 'displayName' => 'user' . $user->getId()], $formattedContext['appName']['user']); $this->assertSame($clientId, $formattedContext['appName']['client']['id']); $this->assertSame('baz', $formattedContext['appName']['bar']); $this->assertSame('quux', $formattedContext['appName']['baz']); $this->assertSame('CM_Exception_Invalid', $formattedContext['exception']['type']); $this->assertSame('Bad', $formattedContext['exception']['message']); $this->assertArrayHasKey('stack', $formattedContext['exception']); $this->assertInternalType('string', $formattedContext['exception']['stack']); $this->assertSame(['foo' => "'bar'"], $formattedContext['exception']['metaInfo']); $this->assertRegExp('/library\\/CM\\/Log\\/ContextFormatter\\/CargomediaTest\\.php\\(\\d+\\)/', $formattedContext['exception']['stack']); }
public function testGetVersion() { $clientDeviceDetector = new CM_Http_ClientDevice(CM_Http_Request_Abstract::factory('get', '/foo', $this->_nonMobileHeaders)); $this->assertInstanceOf('CM_Http_ClientDevice', $clientDeviceDetector); $this->assertFalse($clientDeviceDetector->getVersion('Android')); $clientDeviceDetector = new CM_Http_ClientDevice(CM_Http_Request_Abstract::factory('post', '/bar', $this->_mobileHeaders)); $this->assertSame('5.0.2', $clientDeviceDetector->getVersion('Android')); }
public function prepare(CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) { if (CM_Http_Request_Abstract::hasInstance()) { $ip = CM_Http_Request_Abstract::getInstance()->getIp(); if ($locationGuess = CM_Model_Location::findByIp($ip)) { $this->getField('location')->setValue($locationGuess); } } }
/** * @expectedException CM_Exception_FormFieldValidation */ public function testValidate() { $formField = new CM_FormField_Birthdate(['name' => 'foo', 'minAge' => 18, 'maxAge' => 30]); $request = CM_Http_Request_Abstract::factory('get', '/foo'); $response = CM_Http_Response_Abstract::factory($request, $this->getServiceManager()); $environment = new CM_Frontend_Environment(); $value = $formField->validate($environment, array('year' => 1995, 'month' => 1, 'day' => 1)); $this->assertEquals(new DateTime('1995-01-01'), $value); $formField->validate($environment, array('year' => 2005, 'month' => 1, 'day' => 1)); }
protected function _getSuggestions($term, array $options, CM_Frontend_Render $render) { $ip = CM_Http_Request_Abstract::getInstance()->getIp(); $locations = new CM_Paging_Location_Suggestions($term, $options['levelMin'], $options['levelMax'], CM_Model_Location::findByIp($ip), $options['scopeLocation']); $locations->setPage(1, 15); $out = array(); foreach ($locations as $location) { $out[] = $this->getSuggestion($location, $render); } return $out; }
public function testWriting() { $collection = 'cm_event_log'; $level = CM_Log_Logger::DEBUG; $message = 'foo'; $ttl = 30; $user = CMTest_TH::createUser(); $httpRequest = CM_Http_Request_Abstract::factory('post', '/foo?bar=1&baz=quux', ['bar' => 'baz'], ['foo' => 'quux'], '{"bar":"2", "quux":"baz"}'); $clientId = $httpRequest->getClientId(); $computerInfo = new CM_Log_Context_ComputerInfo('www.example.com', 'v7.0.1'); $mongoClient = $this->getServiceManager()->getMongoDb(); $this->assertSame(0, $mongoClient->count($collection)); $mongoClient->createIndex($collection, ['expireAt' => 1], ['expireAfterSeconds' => 0]); $recordContext = new CM_Log_Context(); $recordContext->setExtra(['bar' => ['baz' => 'quux']]); $recordContext->setUser($user); $recordContext->setHttpRequest($httpRequest); $recordContext->setComputerInfo($computerInfo); $record = new CM_Log_Record($level, $message, $recordContext); $encoder = $this->mockObject(CM_Log_Encoder_MongoDb::class); /** @var CM_Log_Encoder_MongoDb $encoder */ $handler = new CM_Log_Handler_MongoDb($mongoClient, $encoder, $collection, $ttl, ['w' => 0], $level); $this->callProtectedMethod($handler, '_writeRecord', [$record]); $this->assertSame(1, $mongoClient->count($collection)); $savedRecord = $mongoClient->findOne($collection); $this->assertSame($level, $savedRecord['level']); $this->assertSame($message, $savedRecord['message']); /** @var MongoDate $createdAt */ $createdAt = $savedRecord['createdAt']; /** @var MongoDate $expireAt */ $expireAt = $savedRecord['expireAt']; $this->assertInstanceOf('MongoDate', $createdAt); $this->assertInstanceOf('MongoDate', $expireAt); $this->assertSame($ttl, $expireAt->sec - $createdAt->sec); $context = $savedRecord['context']; $this->assertSame(['id' => $user->getId(), 'name' => $user->getDisplayName()], $context['user']); $this->assertSame('POST', $context['httpRequest']['method']); $this->assertSame('/foo?bar=1&baz=quux', $context['httpRequest']['uri']); $this->assertSame(['bar' => '2', 'baz' => 'quux', 'quux' => 'baz'], $context['httpRequest']['query']); $this->assertSame(['bar' => 'baz'], $context['httpRequest']['headers']); $this->assertSame(['foo' => 'quux'], $context['httpRequest']['server']); $this->assertSame($clientId, $context['httpRequest']['clientId']); $this->assertSame('www.example.com', $context['computerInfo']['fqdn']); $this->assertSame('v7.0.1', $context['computerInfo']['phpVersion']); $this->assertSame(['bar' => ['baz' => 'quux'], 'type' => CM_Log_Handler_MongoDb::DEFAULT_TYPE], $context['extra']); $this->assertSame('{"bar":"2", "quux":"baz"}', $context['httpRequest']['body']); }
/** * @param string $uri * @param array|null $headers OPTIONAL * @param array|null $server * @param CM_Model_User|null $viewer */ public function __construct($uri, array $headers = null, array $server = null, CM_Model_User $viewer = null) { if (null !== $headers) { $this->_headers = array_change_key_case($headers); } if (null !== $server) { $this->_server = array_change_key_case($server); } $this->setUri($uri); if ($sessionId = $this->getCookie('sessionId')) { if ($this->_session = CM_Session::findById($sessionId)) { $this->_session->start(); } } if ($viewer) { $this->_viewer = $viewer; } self::$_instance = $this; }
/** * @param string $uri * @param array|null $headers OPTIONAL * @param array|null $server * @param CM_Model_User|null $viewer * @throws CM_Exception * @throws CM_Exception_Invalid */ public function __construct($uri, array $headers = null, array $server = null, CM_Model_User $viewer = null) { if (null !== $headers) { $this->_headers = array_change_key_case($headers); } if (null !== $server) { foreach ($server as &$serverValue) { if (is_string($serverValue)) { $serverValue = CM_Util::sanitizeUtf($serverValue); } } $this->_server = array_change_key_case($server); } $uri = CM_Util::sanitizeUtf((string) $uri); $this->setUri($uri); if ($sessionId = $this->getCookie('sessionId')) { $this->setSession(CM_Session::findById($sessionId)); } if ($viewer) { $this->_viewer = $viewer; } self::$_instance = $this; }
/** * @return array */ protected function _getDefaultMetaInfo() { $metaInfo = array(); if ($fqdn = CM_Util::getFqdn()) { $metaInfo['fqdn'] = $fqdn; } if (CM_Http_Request_Abstract::hasInstance()) { $request = CM_Http_Request_Abstract::getInstance(); $metaInfo['uri'] = $request->getUri(); if ($viewer = $request->getViewer()) { $metaInfo['userId'] = $viewer->getId(); } if ($ip = $request->getIp()) { $metaInfo['ip'] = $ip; } if ($request->hasHeader('Referer')) { $metaInfo['referer'] = $request->getHeader('Referer'); } if ($request->hasHeader('User-Agent')) { $metaInfo['useragent'] = $request->getHeader('User-Agent'); } } return $metaInfo; }
public function testProcessTrackingViewer() { $viewer = $this->getMock('CM_Model_User', array('getIdRaw', 'getVisible', 'getLanguage')); $viewer->expects($this->any())->method('getIdRaw')->will($this->returnValue(array('id' => '1'))); $viewer->expects($this->any())->method('getVisible')->will($this->returnValue(false)); $viewer->expects($this->any())->method('getLanguage')->will($this->returnValue(null)); /** @var CM_Model_User $viewer */ $response = CMTest_TH::createResponsePage('/mock5', null, $viewer); $response->setServiceManager($this->_getServiceManager('ga123', 'km123')); $response->process(); $html = $response->getContent(); $this->assertContains('ga("create", "ga123"', $html); $this->assertContains('ga("send", "pageview", "\\/mock5")', $html); $this->assertContains('var _kmq = _kmq || [];', $html); $this->assertContains("var _kmk = _kmk || 'km123';", $html); $clientId = CM_Http_Request_Abstract::getInstance()->getClientId(); $this->assertContains("_kmq.push(['identify', 'Guest {$clientId}']);", $html); $this->assertContains("_kmq.push(['identify', '1']);", $html); $this->assertContains("_kmq.push(['alias', 'Guest {$clientId}', '1']);", $html); }
/** * @param CM_Http_Request_Abstract $request * @return bool * @throws CM_Exception */ public function match(CM_Http_Request_Abstract $request) { return $this->isUrlMatch($request->getHost(), $request->getPath()); }
/** * @param CM_Http_Request_Abstract $request * @throws CM_Exception_Invalid * @return string|null */ protected function _processPageLoop(CM_Http_Request_Abstract $request) { $count = 0; $paths = array($request->getPath()); while (false === ($html = $this->_processPage($request))) { $paths[] = $request->getPath(); if ($count++ > 10) { throw new CM_Exception_Invalid('Page dispatch loop detected (' . implode(' -> ', $paths) . ').'); } } return $html; }
public static function match(CM_Http_Request_Abstract $request) { return $request->getPathPart(0) === 'emailtracking'; }
public static function match(CM_Http_Request_Abstract $request) { return $request->getPathPart(0) === 'layout'; }
public function trackPageView(CM_Frontend_Environment $environment, $path) { if ($viewer = $environment->getViewer()) { $this->setUserId($viewer->getId()); } if (CM_Http_Request_Abstract::hasInstance()) { $this->setRequestClientId(CM_Http_Request_Abstract::getInstance()->getClientId()); } }
/** * @param CM_Http_Request_Abstract $request * @return boolean */ public function match(CM_Http_Request_Abstract $request) { $urlRequest = $request->getHost(); $urlSite = $this->getHost(); return 0 === strpos(preg_replace('/^www\\./', '', $urlRequest), preg_replace('/^www\\./', '', $urlSite)); }
/** * @param CM_Http_Request_Abstract $request * @param CM_Http_Response_Page_ProcessingResult $result * @throws CM_Exception * @throws Exception * @return boolean */ private function _processPage(CM_Http_Request_Abstract $request, CM_Http_Response_Page_ProcessingResult $result) { return $this->_runWithCatching(function () use($request, $result) { $path = CM_Util::link($request->getPath(), $request->getQuery()); $result->addPath($path); $this->getSite()->rewrite($request); $pageParams = CM_Params::factory($request->getQuery(), true); try { $className = CM_Page_Abstract::getClassnameByPath($this->getRender(), $request->getPath()); $page = CM_Page_Abstract::factory($className, $pageParams); } catch (CM_Exception $ex) { throw new CM_Exception_Nonexistent('Cannot load page `' . $request->getPath() . '`: ' . $ex->getMessage()); } $result->addPage($page); $environment = $this->getRender()->getEnvironment(); $page->prepareResponse($environment, $this); if ($this->getRedirectUrl()) { $request->setUri($this->getRedirectUrl()); return true; } if ($page->getCanTrackPageView()) { $this->getRender()->getServiceManager()->getTrackings()->trackPageView($environment, $result->getPathTracking()); } $result->setHtml($this->_renderPage($page)); return true; }, function (CM_Exception $ex, array $errorOptions) use($request) { $this->getRender()->getGlobalResponse()->clear(); /** @var CM_Page_Abstract $errorPage */ $errorPage = $errorOptions['errorPage']; $request->setPath($errorPage::getPath()); $request->setQuery(array()); return false; }); }
/** * @param string $streamName * @param string $clientKey * @return boolean */ public static function rpc_unsubscribe($streamName, $clientKey) { $adapter = self::getInstance()->getAdapter(); $adapter->getServerId(CM_Http_Request_Abstract::getInstance()); $adapter->unsubscribe($streamName, $clientKey); return true; }
public function testFactory() { $this->assertInstanceOf('CM_Http_Request_Get', CM_Http_Request_Abstract::factory('GET', '/test')); $this->assertInstanceOf('CM_Http_Request_Post', CM_Http_Request_Abstract::factory('POST', '/test')); }
/** * @param bool $needed OPTIONAL Throw an CM_Exception_AuthRequired if not authenticated * @return CM_Model_User|null * @throws CM_Exception_AuthRequired */ public function getViewer($needed = false) { return $this->_request->getViewer($needed); }
/** * @param string $uri * @param array|null $headers * @param array|null $server * @param string|null $body */ public function __construct($uri, array $headers = null, array $server = null, $body = null) { parent::__construct($uri, $headers, $server); $this->_body = (string) $body; }
public static function match(CM_Http_Request_Abstract $request) { return 0 === stripos($request->getPath(), '/resource-'); }
/** * @param CM_Http_Request_Abstract $request * @param CM_Model_User $user */ public static function setUserForRequestClient(CM_Http_Request_Abstract $request, CM_Model_User $user) { $requestClientId = $request->getClientId(); $userId = $user->getId(); CM_Db_Db::updateIgnore('cm_splittestVariation_fixture', array('userId' => $userId), array('requestClientId' => $requestClientId)); }