1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Sites;
13
14 use ICanBoogie\ActiveRecord\ActiveRecordException;
15 use Icybee\Modules\Users\User;
16 use ICanBoogie\HTTP\Request;
17
18 19 20
21 class Model extends \ICanBoogie\ActiveRecord\Model
22 {
23 24 25 26 27 28
29 public function save(array $properties, $key=null, array $options=array())
30 {
31 if (isset($properties['path']))
32 {
33 $path = trim($properties['path'], '/');
34
35 if ($path)
36 {
37 $path = '/' . $path;
38 }
39
40 $properties['path'] = $path;
41 }
42
43 if (!$key && empty($properties['created_at']))
44 {
45 $properties['created_at'] = gmdate('Y-m-d H:i:s');
46 }
47
48 if (empty($properties['updated_at']))
49 {
50 $properties['updated_at'] = gmdate('Y-m-d H:i:s');
51 }
52
53 return parent::save($properties, $key, $options);
54 }
55
56 static private $cached_sites;
57
58 59 60 61 62 63 64 65 66 67 68 69 70
71 static public function find_by_request(Request $request, User $user=null)
72 {
73 global $core;
74
75 $sites = self::$cached_sites;
76
77 if ($sites === null)
78 {
79 $sites = $core->vars['cached_sites'];
80 }
81
82 if (!$sites)
83 {
84 self::$cached_sites = array();
85
86 try
87 {
88 self::$cached_sites = $sites = $core->models['sites']->all;
89
90 $core->vars['cached_sites'] = $sites;
91 }
92 catch (ActiveRecordException $e)
93 {
94 throw $e;
95 }
96 catch (\Exception $e)
97 {
98 return self::get_default_site();
99 }
100 }
101
102 $path = $request->path;
103 $parts = array_reverse(explode('.', $request->headers['Host']));
104
105 $tld = null;
106 $domain = null;
107 $subdomain = null;
108
109 if (isset($parts[0]))
110 {
111 $tld = $parts[0];
112 }
113
114 if (isset($parts[1]))
115 {
116 $domain = $parts[1];
117 }
118
119 if (isset($parts[2]))
120 {
121 $subdomain = implode('.', array_slice($parts, 2));
122 }
123
124 $match = null;
125 $match_score = -1;
126
127 foreach ($sites as $site)
128 {
129 $score = 0;
130
131
132
133
134
135 if ($site->status != Site::STATUS_OK && $user && $user->is_guest)
136 {
137 continue;
138 }
139
140 if ($site->tld)
141 {
142 $score += ($site->tld == $tld) ? 1000 : -1000;
143 }
144
145 if ($site->domain)
146 {
147 $score += ($site->domain == $domain) ? 100 : -100;
148 }
149
150 if ($site->subdomain)
151 {
152 $score += ($site->subdomain == $subdomain || (!$site->subdomain && $subdomain == 'www')) ? 10 : -10;
153 }
154
155 $site_path = $site->path;
156
157 if ($site_path)
158 {
159 $score += ($path == $site_path || preg_match('#^' . $site_path . '/#', $path)) ? 1 : -1;
160 }
161 else if ($path == '/')
162 {
163 $score += 1;
164 }
165
166 if ($score > $match_score)
167 {
168 $match = $site;
169 $match_score = $score;
170 }
171 }
172
173 return $match ? $match : self::get_default_site();
174 }
175
176 static private $default_site;
177
178 179 180 181 182
183 static private function get_default_site()
184 {
185 global $core;
186
187 if (self::$default_site === null)
188 {
189 self::$default_site = Site::from
190 (
191 array
192 (
193 'title' => 'Undefined',
194 'language' => $core->language,
195 'timezone' => $core->timezone,
196 'status' => Site::STATUS_OK
197 )
198 );
199 }
200
201 return self::$default_site;
202 }
203 }