Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
DynamicContentSource.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Class for loading content from Dynamic Content Renderer
11  */
13 {
14  /**
15  * @var string folder into which content will be cached
16  */
17  public $cacheFolder;
18 
19  private $_modTimeCachePrefix = '__modTime__';
20 
21  /**
22  * Init Gentics Connector API
23  */
24  public function init()
25  {
26 
27  }
28 
29  /**
30  * Return array of attributes which will be requested from API
31  *
32  * @return array array of attributes
33  */
34  public function getRequestedAttributes()
35  {
36  $mainAttributes = array(
37  'contentid',
38  'name',
39  'filename',
40  'updatetimestamp',
41  'languagecode',
42  'mimetype',
43  'content'
44  );
45  if ($this->usePersonalisation) {
46  return array_merge($mainAttributes, $this->personalisationFields);
47  }
48  return $mainAttributes;
49  }
50 
51  /**
52  * Get content absolute path.
53  * Checking if we have newest version on content,
54  * if yes return, else download and store
55  *
56  * @param string $path content path
57  *
58  * @return \Content|mixed object contained file path, false if error
59  */
60  public function getContent($path)
61  {
62  if ($this->_haveNewestVersion($path)) {
63  return $this->_takeFromFileSystem($path);
64  } else {
65  return $this->_takeAndSaveFromApi($path);
66  }
67  }
68 
69  public function getStartPage($folderPath, $locale)
70  {
71  $localizedAttributeName = Yii::app()->getModule('contentSource')->localizedAttributeName. $locale;
72 
73  $folderObject = $this->getRepositoryApi()->getContentAttributes($folderPath, array($localizedAttributeName));
74  if($folderObject){
75  return $folderObject[$localizedAttributeName];
76  }
77  return false;
78  }
79 
80  /**
81  * This method takes content from API and save locally
82  *
83  * @param string $path content path
84  *
85  * @return bool|string
86  */
87  private function _takeAndSaveFromApi($path)
88  {
89  if ($cmsObject = $this->getRepositoryApi()->getCmsObjectCanBeExternal($path, $this->getRequestedAttributes())) {
90  $this->_createFile($path, $cmsObject['attributes']['content'], $cmsObject['attributes']['updatetimestamp']);
91  $content = new Content($this, $path);
92  unset($cmsObject['attributes']['content']);
93  $content->setAttributes($cmsObject['attributes']);
94  return $content;
95  }
96 
97  return false;
98  }
99 
100  /**
101  * Checking if we have latest version of file
102  *
103  * @param string $path content path
104  *
105  * @return bool
106  */
107  private function _haveNewestVersion($path)
108  {
109  $contentFile = $this->contentFile($path);
110  if (!file_exists($contentFile)) {
111  return false;
112  }
113 
114  if ($lastModificationTime = $this->getRepositoryApi()->getLastModificationTime($path)) {
115  return
116  Yii::app()->cache->get($this->_modTimeCachePrefix . $path) == $lastModificationTime ||
117  filemtime($this->contentFile($path)) == $lastModificationTime;
118  }
119 
120  return false;
121  }
122 
123  /**
124  * Take existed file from filesystem
125  *
126  * @param string $path content path
127  *
128  * @return bool|string
129  */
130  private function _takeFromFileSystem($path)
131  {
132  $contentFile = $this->contentFile($path);
133  if ($this->fileAccessible($contentFile, $this->cacheFolder)) {
134  $content = new Content($this, $path);
135  return $content;
136  } else {
137  return false;
138  }
139  }
140 
141 
142  /**
143  * Create file,and save its last modification date
144  *
145  * @param string $path string relative path
146  * @param string $content string file content
147  * @param int $time mixed last modification time
148  *
149  * @throws Exception
150  * @return string string absolute path
151  *
152  */
153  private function _createFile($path, $content, $time)
154  {
155  $contentFile = $this->contentFile($path);
156  if (file_exists($contentFile) && !$this->fileAccessible($contentFile, $this->cacheFolder)) {
157  return false;
158  }
159 
160  $dirPath = pathinfo($contentFile, PATHINFO_DIRNAME);
161  if (!file_exists($dirPath)) {
162  if (!@mkdir($dirPath, 0777, true)) {
163  throw new Exception("Can't create dir $dirPath");
164  }
165  }
166  if (!file_put_contents($contentFile, $content)) {
167  throw new Exception("Can't create file $contentFile");
168  }
169  if (!touch($contentFile, $time)) {
170  throw new Exception("Can't touch() file $contentFile");
171  }
172  if (!chmod($contentFile, 0666)) {
173  throw new Exception("Can't chmod() file $contentFile");
174  }
175 
176  if (!$this->fileAccessible($contentFile, $this->cacheFolder)) {
177  unlink($contentFile);
178  return false;
179  }
180 
181  Yii::app()->cache->set($this->_modTimeCachePrefix . $path, $time, Yii::app()->getModule('contentSource')->cacheTime);
182 
183  return $contentFile;
184  }
185 
186  /**
187  * Get content file location
188  *
189  * @param string $path content path
190  *
191  * @return string
192  */
193  public function contentFile($path)
194  {
195  return $this->cacheFolder . $path;
196  }
197 
198 
199 }