1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Sites;
13
14 use ICanBoogie\ActiveRecord\CreatedAtProperty;
15 use ICanBoogie\ActiveRecord\UpdatedAtProperty;
16 use ICanBoogie\Debug;
17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 class Site extends \ICanBoogie\ActiveRecord
36 {
37 const SITEID = 'siteid';
38 const SUBDOMAIN = 'subdomain';
39 const DOMAIN = 'domain';
40 const PATH = 'path';
41 const TLD = 'tld';
42 const TITLE = 'title';
43 const ADMIN_TITLE = 'admin_title';
44 const LANGUAGE = 'language';
45 const TIMEZONE = 'tmezone';
46 const NATIVEID = 'nativeid';
47 const STATUS = 'status';
48 const CREATED_AT = 'created_at';
49 const UPDATED_AT = 'updated_at';
50
51 const BASE = '/protected/';
52
53 const STATUS_OK = 200;
54 const STATUS_UNAUTHORIZED = 401;
55 const STATUS_NOT_FOUND = 404;
56 const STATUS_UNAVAILABLE = 503;
57
58 public $siteid;
59 public $path = '';
60 public $tld = '';
61 public $domain = '';
62 public $subdomain = '';
63 public $title;
64 public $admin_title = '';
65 public $weight = 0;
66 public $language = '';
67 public $nativeid = 0;
68 public $timezone = '';
69 public $email = '';
70 public $status = 0;
71
72 use CreatedAtProperty;
73 use UpdatedAtProperty;
74
75 76 77 78 79
80 public function __construct($model='sites')
81 {
82 parent::__construct($model);
83 }
84
85 86 87
88 public function save()
89 {
90 global $core;
91
92 unset($core->vars['cached_sites']);
93
94 return parent::save();
95 }
96
97 98 99 100 101
102 protected function get_url()
103 {
104 $parts = explode('.', $_SERVER['SERVER_NAME']);
105 $parts = array_reverse($parts);
106
107 if ($this->tld)
108 {
109 $parts[0] = $this->tld;
110 }
111
112 if ($this->domain)
113 {
114 $parts[1] = $this->domain;
115 }
116
117 if ($this->subdomain)
118 {
119 $parts[2] = $this->subdomain;
120 }
121 else if (empty($parts[2]))
122 {
123
124 unset($parts[2]);
125 }
126
127 return 'http://' . implode('.', array_reverse($parts)) . $this->path;
128 }
129
130 131 132
133 protected function lazy_get_templates()
134 {
135 $templates = array();
136 $root = \ICanBoogie\DOCUMENT_ROOT;
137
138 $models = array('default', 'all');
139
140 foreach ($models as $model)
141 {
142 $path = self::BASE . $model . '/templates';
143
144 if (!is_dir($root . $path))
145 {
146 continue;
147 }
148
149 $dh = opendir($root . $path);
150
151 if (!$dh)
152 {
153 Debug::trigger('Unable to open directory %path', array('%path' => $path));
154
155 continue;
156 }
157
158 while (($file = readdir($dh)) !== false)
159 {
160 if ($file{0} == '.')
161 {
162 continue;
163 }
164
165 $pos = strrpos($file, '.');
166
167 if (!$pos)
168 {
169 continue;
170 }
171
172 $templates[$file] = $file;
173 }
174
175 closedir($dh);
176 }
177
178 sort($templates);
179
180 return $templates;
181 }
182
183 protected function lazy_get_partial_templates()
184 {
185 $templates = array();
186 $root = \ICanBoogie\DOCUMENT_ROOT;
187
188 $models = array('default', 'all');
189
190 foreach ($models as $model)
191 {
192 $path = self::BASE . $model . '/templates/partials';
193
194 if (!is_dir($root . $path))
195 {
196 continue;
197 }
198
199 $dh = opendir($root . $path);
200
201 if (!$dh)
202 {
203 Debug::trigger('Unable to open directory %path', array('%path' => $path));
204
205 continue;
206 }
207
208 while (($file = readdir($dh)) !== false)
209 {
210 if ($file{0} == '.')
211 {
212 continue;
213 }
214
215 $pos = strrpos($file, '.');
216
217 if (!$pos)
218 {
219 continue;
220 }
221
222 $id = preg_replace('#\.(php|html)$#', '', $file);
223 $templates[$id] = $root . $path . '/' . $file;
224 }
225
226 closedir($dh);
227 }
228
229 return $templates;
230 }
231
232 233 234 235 236
237
238 public function resolve_path($relative)
239 {
240 $root = $_SERVER['DOCUMENT_ROOT'];
241
242 $try = self::BASE . 'default/' . $relative;
243
244 if (file_exists($root . $try))
245 {
246 return $try;
247 }
248
249 $try = self::BASE . 'all/' . $relative;
250
251 if (file_exists($root . $try))
252 {
253 return $try;
254 }
255 }
256
257 protected function lazy_get_native()
258 {
259 $native_id = $this->nativeid;
260
261 return $native_id ? $this->model[$native_id] : $this;
262 }
263
264 265 266 267 268
269 protected function lazy_get_translations()
270 {
271 if ($this->nativeid)
272 {
273 return $this->model->where('siteid != ? AND (siteid = ? OR nativeid = ?)', $this->siteid, $this->nativeid, $this->nativeid)->order('language')->all;
274 }
275 else
276 {
277 return $this->model->where('nativeid = ?', $this->siteid)->order('language')->all;
278 }
279 }
280
281 private $_server_name;
282
283 protected function get_server_name()
284 {
285 if ($this->_server_name)
286 {
287 return $this->_server_name;
288 }
289
290 $parts = explode('.', $_SERVER['SERVER_NAME']);
291 $parts = array_reverse($parts);
292
293 if (count($parts) > 3)
294 {
295 $parts[2] = implode('.', array_reverse(array_slice($parts, 2)));
296 }
297
298 if ($this->tld)
299 {
300 $parts[0] = $this->tld;
301 }
302
303 if ($this->domain)
304 {
305 $parts[1] = $this->domain;
306 }
307
308 if ($this->subdomain)
309 {
310 $parts[2] = $this->subdomain;
311 }
312
313 return $this->_server_name = new ServerName(array($parts[2], $parts[1], $parts[0]));
314 }
315
316 protected function set_server_name($server_name)
317 {
318 if (!($server_name instanceof ServerName))
319 {
320 $server_name = new ServerName($server_name);
321 }
322
323 $this->subdomain = $server_name->subdomain;
324 $this->domain = $server_name->domain;
325 $this->tld = $server_name->tld;
326
327 $this->_server_name = $server_name;
328 }
329 }