14 const OBJ_TYPE_FOLDER = 10002;
15 const OBJ_TYPE_PAGE = 10007;
16 const OBJ_TYPE_BINARY = 10008;
26 public $failover_urls = array();
31 public $contentRepositorySearch;
36 public $binaryContent;
66 public $contentExternalHost;
71 public $additionalRequestParameters = array();
76 public $additionalAttributes = array();
81 public $additionalSortingRules = array();
90 return $this->getUrl() . $this->contentRepositorySearch;
100 return $this->getUrl() . $this->binaryContent;
110 return $this->getUrl() . $this->navigation;
120 return $this->getUrl() . $this->activePath;
130 return $this->getUrl() . $this->autosuggest;
140 return $this->getUrl() . $this->search;
148 if (isset($_GET[
'lang']) && is_string($_GET[
'lang'])) {
149 Yii::app()->language = $_GET[
'lang'];
153 protected function getUrl()
157 if ($failover_URL = Yii::app()->cache->get(
'failover_URL',
false)) {
158 $this->failover_urls[] = $this->url;
159 $this->url = $failover_URL;
175 if (!$cmsObject && !preg_match(
'/.+\.(php|html)$/', $path)) {
176 $externalPath = (!empty($this->contentExternalHost) ? $this->contentExternalHost : Yii::app()->request->getHostInfo()) . $path;
192 $cacheId =
'updateTMSTMP_'.md5($path);
193 $updateTMSTMP =
false;
194 if (!($updateTMSTMP = Yii::app()->cache->get($cacheId,
false))
195 || (isset($_GET[
'refresh']) && $_GET[
'refresh']==
'true')) {
198 $updateTMSTMP = $attributes[
'updatetimestamp'];
199 Yii::app()->cache->set($cacheId, $updateTMSTMP, Yii::app()->getModule(
'contentSource')->cacheTime);
202 return $updateTMSTMP;
215 $cmsObject = $this->
getCmsObject($path, array_filter($attributes,
function ($el) {
216 return $el !=
'content';
218 return $cmsObject[
'attributes'];
233 'filter' =>
"object.url==\"$path\"",
239 $cmsObject = self::_prepareResponse($response);
243 if (in_array(
'content', $attributes) && $cmsObject[
'obj_type'] == self::OBJ_TYPE_BINARY) {
260 'filter' =>
"object.url==\"$path\"",
278 if (!empty($this->additionalRequestParameters[$type])) {
279 foreach ($this->additionalRequestParameters[$type] as $name => $val) {
280 $params[$name] = !empty($params[$name]) ? $params[$name] .
' AND ' . $val : $params[$name];
296 if (empty($url) || $url=
"") {
297 $url = $this->clearCache;
300 $url = $url .
'?contentid=' . $contentid;
302 @$content = file_get_contents($url,
false);
316 if (isset($this->additionalAttributes[$type])) {
317 $attributes = $this->additionalAttributes[$type];
319 if ($attributes && $attributes = explode(
',',$attributes)) {
320 $attributes = array_map(
'trim',$attributes);
321 $attributes = array_unique($attributes);
322 $attributes =
'&attributes=' . implode(
'&attributes=', $attributes);
336 if (strpos($url, $this->navigation)) {
337 $type =
'navigation';
338 } elseif(strpos($url, $this->binaryContent)) {
340 } elseif(strpos($url, $this->contentRepositorySearch)
341 || strpos($url, $this->search)) {
358 private function _request($url, $params, $attributes = array(), $timeout =
"")
361 array_walk($attributes,
function ($item) use (&$attr) {
362 $attr[] =
"attributes=$item";
365 $url = $url .
'?' . http_build_query($params);
366 foreach($this->additionalSortingRules as $addRuleK => $addRuleV) {
367 if( strpos($url,$addRuleK)) {
368 $url .=
'&sorting='.$addRuleV;
374 $url .=
'&' . implode(
'&', $attr);
380 Yii::trace($url,
'Api requests(start)');
381 $start = microtime(
true);
383 if (!isset( $timeout ) || $timeout ==
"") {
384 $ctx=stream_context_create(array(
'http'=>
392 @$content = file_get_contents($url,
false, $ctx);
394 $failover_attempt = 0;
396 while (($content ==
false || strpos($content,
'DatasourceException')!==
false) && ($failover_attempt < count($this->failover_urls))) {
397 if (isset( $this->failover_urls[$failover_attempt])) {
398 $this->url = $this->failover_urls[$failover_attempt];
399 $exploded_url = explode(
'://',$url);
400 $exploded_url = explode(
'/',$exploded_url[1]);
401 $exploded_url[0] = $this->url;
402 $url = implode(
'/', $exploded_url);
403 @$content = file_get_contents($url,
false, $ctx);
406 if ($content !==
false) {
407 Yii::app()->cache->set(
'failover_URL', $this->url, Yii::app()->getModule(
'contentSource')->cacheTime);
412 if ($content ==
false) {
413 throw new Exception(
'Repository API error: could not complete API request.');
415 Yii::trace($url,
'Api requests(end). Time s.: ' . round(microtime(
true) - $start, 4));
429 public function request($params, $attributes = array(), $timeout =
"")
445 return $this->_request($this->
getNavigationUrl(), $params, $attributes, $timeout);
459 return $this->_request($this->
getActivePathUrl(), $params, $attributes, $timeout);
473 return $this->_request($this->
getAutosuggestUrl(), $params, $attributes, $timeout);
485 public function requestSearch($params, $attributes = array(), $timeout =
"")
487 return $this->_request($this->
getSearchUrl(), $params, $attributes, $timeout);
498 private static function _prepareResponse($response)
500 $responseData = unserialize($response);
501 if (@$responseData[
'status'] ==
'ok') {
502 unset($responseData[
'status']);
503 if (count($responseData) > 0) {
504 return array_shift($responseData);
508 Yii::log(print_r($responseData,
true), CLogger::LEVEL_TRACE);