14 const OBJ_TYPE_FOLDER = 10002;
15 const OBJ_TYPE_PAGE = 10007;
16 const OBJ_TYPE_BINARY = 10008;
23 public $failover_urls = array();
25 public $contentRepositorySearch;
29 public $binaryContent;
39 public $contentExternalHost;
44 public $additionalRequestParameters = array();
49 public $additionalAttributes = array();
54 public $additionalSortingRules = array();
64 return $this->getUrl() . $this->contentRepositorySearch;
74 return $this->getUrl() . $this->binaryContent;
84 return $this->getUrl() . $this->navigation;
94 return $this->getUrl() . $this->activePath;
104 return $this->getUrl() . $this->autosuggest;
114 return $this->getUrl() . $this->search;
122 if (isset($_GET[
'lang']) && is_string($_GET[
'lang'])) {
123 Yii::app()->language = $_GET[
'lang'];
127 protected function getUrl(){
130 if(($failover_URL = Yii::app()->cache->get(
'failover_URL'))!==
false){
131 $this->failover_urls[] = $this->url;
132 $this->url = $failover_URL;
148 if (!$cmsObject && !preg_match(
'/.+\.(php|html)$/', $path)) {
149 $externalPath = (!empty($this->contentExternalHost) ? $this->contentExternalHost : Yii::app()->request->getHostInfo()) . $path;
165 $cacheId =
'updateTMSTMP_'.md5($path);
166 $updateTMSTMP =
false;
167 if(!($updateTMSTMP = Yii::app()->cache->get($cacheId)) || (isset($_GET[
'refresh']) && $_GET[
'refresh']==
'true')){
170 $updateTMSTMP = $attributes[
'updatetimestamp'];
171 Yii::app()->cache->set($cacheId, $updateTMSTMP, Yii::app()->getModule(
'contentSource')->cacheTime);
174 return $updateTMSTMP;
187 $cmsObject = $this->
getCmsObject($path, array_filter($attributes,
function ($el) {
188 return $el !=
'content';
190 return $cmsObject[
'attributes'];
205 'filter' =>
"object.url==\"$path\"",
211 $cmsObject = self::_prepareResponse($response);
215 if (in_array(
'content', $attributes) && $cmsObject[
'obj_type'] == self::OBJ_TYPE_BINARY) {
232 'filter' =>
"object.url==\"$path\"",
250 if (!empty($this->additionalRequestParameters[$type])) {
251 foreach ($this->additionalRequestParameters[$type] as $name => $val) {
252 $params[$name] = !empty($params[$name]) ? $params[$name] .
' AND ' . $val : $params[$name];
268 private function _request($url, $params, $attributes = array())
271 array_walk($attributes,
function ($item) use (&$attr) {
272 $attr[] =
"attributes=$item";
275 $url = $url .
'?' . http_build_query($params);
276 foreach($this->additionalSortingRules as $addRuleK => $addRuleV){
277 if( strpos($url,$addRuleK)){
278 $url .=
'&sorting='.$addRuleV;
281 $url .=
'&' . implode(
'&', $attr).
'&' . implode(
'&attributes=', $this->additionalAttributes);
282 $url = rtrim($url,
'&');
283 Yii::trace($url,
'Api requests(start)');
284 $start = microtime(
true);
285 $ctx=stream_context_create(array(
'http'=>
291 @$content = file_get_contents($url,
false, $ctx);
292 $failover_attempt = 0;
293 while(($content ==
false || strpos($content,
'DatasourceException')!==
false) && ($failover_attempt < count($this->failover_urls))){
294 $this->url = $this->failover_urls[$failover_attempt];
295 $exploded_url = explode(
'://',$url);
296 $exploded_url = explode(
'/',$exploded_url[1]);
297 $exploded_url[0] = $this->url;
298 $url = implode(
'/', $exploded_url);
299 @$content = file_get_contents($url,
false, $ctx);
300 if($content !==
false){
301 Yii::app()->cache->set(
'failover_URL', $this->url, Yii::app()->getModule(
'contentSource')->cacheTime);
306 if ($content ==
false) {
307 throw new Exception(
'Repository API error: could not complete API request.');
309 Yii::trace($url,
'Api requests(end). Time s.: ' . round(microtime(
true) - $start, 4));
322 public function request($params, $attributes = array())
376 return $this->_request($this->
getSearchUrl(), $params, $attributes);
387 private static function _prepareResponse($response)
389 $responseData = unserialize($response);
390 if (@$responseData[
'status'] ==
'ok') {
391 unset($responseData[
'status']);
392 if (count($responseData) > 0) {
393 return array_shift($responseData);
397 Yii::log(print_r($responseData,
true), CLogger::LEVEL_TRACE);