]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Status_network.php
Merge branch 'testing' into 0.9.x
[quix0rs-gnu-social.git] / classes / Status_network.php
1 <?php
2 /**
3  * Table Definition for status_network
4  *
5  * StatusNet - the distributed open-source microblogging tool
6  * Copyright (C) 2009, StatusNet, Inc.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23
24 class Status_network extends Safe_DataObject
25 {
26     ###START_AUTOCODE
27     /* the code below is auto generated do not remove the above tag */
28
29     public $__table = 'status_network';                  // table name
30     public $site_id;                         // int(4) primary_key not_null
31     public $nickname;                        // varchar(64)   unique_key not_null
32     public $hostname;                        // varchar(255)  unique_key
33     public $pathname;                        // varchar(255)  unique_key
34     public $dbhost;                          // varchar(255)
35     public $dbuser;                          // varchar(255)
36     public $dbpass;                          // varchar(255)
37     public $dbname;                          // varchar(255)
38     public $sitename;                        // varchar(255)
39     public $theme;                           // varchar(255)
40     public $logo;                            // varchar(255)
41     public $created;                         // datetime()   not_null
42     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
43
44     /* Static get */
45     function staticGet($k,$v=NULL) {
46         $i = DB_DataObject::staticGet('Status_network',$k,$v);
47
48         // Don't use local process cache; if we're fetching multiple
49         // times it's because we're reloading it in a long-running
50         // process; we need a fresh copy!
51         global $_DB_DATAOBJECT;
52         unset($_DB_DATAOBJECT['CACHE']['status_network']);
53         return $i;
54     }
55
56     /* the code above is auto generated do not remove the tag below */
57     ###END_AUTOCODE
58
59     static $cache = null;
60     static $cacheInitialized = false;
61     static $base = null;
62     static $wildcard = null;
63
64     /**
65      * @param string $dbhost
66      * @param string $dbuser
67      * @param string $dbpass
68      * @param string $dbname
69      * @param array $servers memcached servers to use for caching config info
70      */
71     static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
72     {
73         global $config;
74
75         $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
76         $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
77         $config['db']['table_status_network'] = $dbname;
78
79         if (class_exists('Memcache')) {
80             self::$cache = new Memcache();
81
82             // If we're a parent command-line process we need
83             // to be able to close out the connection after
84             // forking, so disable persistence.
85             //
86             // We'll turn it back on again the second time
87             // through which will either be in a child process,
88             // or a single-process script which is switching
89             // configurations.
90             $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
91             if (is_array($servers)) {
92                 foreach($servers as $server) {
93                     self::$cache->addServer($server, 11211, $persist);
94                 }
95             } else {
96                 self::$cache->addServer($servers, 11211, $persist);
97             }
98             self::$cacheInitialized = true;
99         }
100
101         self::$base = $dbname;
102     }
103
104     static function cacheKey($k, $v) {
105         return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
106     }
107
108     static function memGet($k, $v)
109     {
110         if (!self::$cache) {
111             return self::staticGet($k, $v);
112         }
113
114         $ck = self::cacheKey($k, $v);
115
116         $sn = self::$cache->get($ck);
117
118         if (empty($sn)) {
119             $sn = self::staticGet($k, $v);
120             if (!empty($sn)) {
121                 self::$cache->set($ck, clone($sn));
122             }
123         }
124
125         return $sn;
126     }
127
128     function decache()
129     {
130         if (self::$cache) {
131             $keys = array('nickname', 'hostname', 'pathname');
132             foreach ($keys as $k) {
133                 $ck = self::cacheKey($k, $this->$k);
134                 self::$cache->delete($ck);
135             }
136         }
137     }
138
139     function update($orig=null)
140     {
141         if (is_object($orig)) {
142             $orig->decache(); # might be different keys
143         }
144         return parent::update($orig);
145     }
146
147     /**
148      * DB_DataObject doesn't allow updating keys (even non-primary)
149      */
150     function updateKeys(&$orig)
151     {
152         $this->_connect();
153         foreach (array('hostname', 'pathname') as $k) {
154             if (strcmp($this->$k, $orig->$k) != 0) {
155                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
156             }
157         }
158         if (count($parts) == 0) {
159             // No changes
160             return true;
161         }
162
163         $toupdate = implode(', ', $parts);
164
165         $table = common_database_tablename($this->tableName());
166         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
167             ' WHERE nickname = ' . $this->_quote($this->nickname);
168         $orig->decache();
169         $result = $this->query($qry);
170         if ($result) {
171             $this->encache();
172         }
173         return $result;
174     }
175     
176     function delete()
177     {
178         $this->decache(); # while we still have the values!
179         return parent::delete();
180     }
181     
182     /**
183      * @param string $servername hostname
184      * @param string $wildcard hostname suffix to match wildcard config
185      * @return mixed Status_network or null
186      */
187     static function getFromHostname($servername, $wildcard)
188     {
189         $sn = null;
190         if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
191             // special case for exact match
192             if (0 == strcasecmp($servername, $wildcard)) {
193                 $sn = self::memGet('nickname', '');
194             } else {
195                 $parts = explode('.', $servername);
196                 $sn = self::memGet('nickname', strtolower($parts[0]));
197             }
198         } else {
199             $sn = self::memGet('hostname', strtolower($servername));
200
201             if (empty($sn)) {
202                 // Try for a no-www address
203                 if (0 == strncasecmp($servername, 'www.', 4)) {
204                     $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
205                 }
206             }
207         }
208         return $sn;
209     }
210
211     /**
212      * @param string $servername hostname
213      * @param string $pathname URL base path
214      * @param string $wildcard hostname suffix to match wildcard config
215      */
216     static function setupSite($servername, $pathname, $wildcard)
217     {
218         global $config;
219
220         $sn = null;
221
222         // XXX I18N, probably not crucial for hostnames
223         // XXX This probably needs a tune up
224         $sn = self::getFromHostname($servername, $wildcard);
225
226         if (!empty($sn)) {
227
228             // Redirect to the right URL
229
230             if (!empty($sn->hostname) &&
231                 empty($_SERVER['HTTPS']) &&
232                 0 != strcasecmp($sn->hostname, $servername)) {
233                 $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
234             } else if (!empty($_SERVER['HTTPS']) &&
235                        0 != strcasecmp($sn->hostname, $servername) &&
236                        0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
237                 $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
238             }
239
240             $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
241             $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
242             $dbpass = $sn->dbpass;
243             $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
244
245             $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
246
247             $config['site']['name'] = $sn->sitename;
248             $config['site']['nickname'] = $sn->nickname;
249
250             self::$wildcard = $wildcard;
251
252             $config['site']['wildcard'] =& self::$wildcard;
253
254             if (!empty($sn->hostname)) {
255                 $config['site']['server'] = $sn->hostname;
256             }
257
258             if (!empty($sn->theme)) {
259                 $config['site']['theme'] = $sn->theme;
260             }
261             if (!empty($sn->logo)) {
262                 $config['site']['logo'] = $sn->logo;
263             }
264
265             return $sn;
266         } else {
267             return null;
268         }
269     }
270
271     // Code partially mooked from http://www.richler.de/en/php-redirect/
272     // (C) 2006 by Heiko Richler  http://www.richler.de/
273     // LGPL
274
275     function redirectTo($destination)
276     {
277         $old = 'http'.
278           (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
279           '://'.
280           $_SERVER['HTTP_HOST'].
281           $_SERVER['REQUEST_URI'].
282           $_SERVER['QUERY_STRING'];
283         if ($old == $destination) { // this would be a loop!
284             // error_log(...) ?
285             return false;
286         }
287
288         header('HTTP/1.1 301 Moved Permanently');
289         header("Location: $destination");
290
291         print "<a href='$destination'>$destination</a>\n";
292
293         exit;
294     }
295
296     function getServerName()
297     {
298         if (!empty($this->hostname)) {
299             return $this->hostname;
300         } else {
301             return $this->nickname . '.' . self::$wildcard;
302         }
303     }
304
305     /**
306      * Return site meta-info tags as an array
307      * @return array of strings
308      */
309     function getTags()
310     {
311         $result = array();
312         
313         $tags = new Status_network_tag();
314         $tags->site_id = $this->site_id;
315         if ($tags->find()) {
316             while ($tags->fetch()) {
317                 $result[] = $tags->tag;
318             }
319         }
320
321         // XXX : for backwards compatibility
322         if (empty($result)) {
323             return explode('|', $this->tags);
324         }
325         
326         return $result;
327     }
328
329     /**
330      * Save a given set of tags
331      * @param array tags
332      */
333     function setTags($tags)
334     {
335         $this->clearTags();
336         foreach ($tags as $tag) {
337             if (!empty($tag)) {
338                 $snt = new Status_network_tag();
339                 $snt->site_id = $this->site_id;
340                 $snt->tag = $tag;
341                 $snt->created = common_sql_now();
342                 
343                 $id = $snt->insert();
344                 if (!$id) {
345                     // TRANS: Exception thrown when a tag cannot be saved.
346                     throw new Exception(_("Unable to save tag."));
347                 }
348             }
349         }
350
351         return true;
352     }
353
354     function clearTags()
355     {
356         $tag = new Status_network_tag();
357         $tag->site_id = $this->site_id;
358
359         if ($tag->find()) {
360             while($tag->fetch()) {
361                 $tag->delete();
362             }
363         }
364
365         $tag->free();
366     }
367     
368     /**
369      * Check if this site record has a particular meta-info tag attached.
370      * @param string $tag
371      * @return bool
372      */
373     function hasTag($tag)
374     {
375         return in_array($tag, $this->getTags());
376     }
377 }