]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Status_network.php
Fix for regression introduced with my last update to the
[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 DB_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 $nickname;                        // varchar(64)  primary_key not_null
31     public $hostname;                        // varchar(255)  unique_key
32     public $pathname;                        // varchar(255)  unique_key
33     public $dbhost;                          // varchar(255)
34     public $dbuser;                          // varchar(255)
35     public $dbpass;                          // varchar(255)
36     public $dbname;                          // varchar(255)
37     public $sitename;                        // varchar(255)
38     public $theme;                           // varchar(255)
39     public $logo;                            // varchar(255)
40     public $created;                         // datetime()   not_null
41     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
42     public $tags;                            // text
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 $base = null;
61     static $wildcard = null;
62
63     /**
64      * @param string $dbhost
65      * @param string $dbuser
66      * @param string $dbpass
67      * @param string $dbname
68      * @param array $servers memcached servers to use for caching config info
69      */
70     static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
71     {
72         global $config;
73
74         $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
75         $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
76         $config['db']['table_status_network'] = $dbname;
77
78         if (class_exists('Memcache')) {
79             self::$cache = new Memcache();
80
81             // Can't close persistent connections, making forking painful.
82             //
83             // @fixme only do this in *parent* CLI processes.
84             // single-process and child-processes *should* use persistent.
85             $persist = php_sapi_name() != 'cli';
86             if (is_array($servers)) {
87                 foreach($servers as $server) {
88                     self::$cache->addServer($server, 11211, $persist);
89                 }
90             } else {
91                 self::$cache->addServer($servers, 11211, $persist);
92             }
93         }
94
95         self::$base = $dbname;
96     }
97
98     static function cacheKey($k, $v) {
99         return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
100     }
101
102     static function memGet($k, $v)
103     {
104         if (!self::$cache) {
105             return self::staticGet($k, $v);
106         }
107
108         $ck = self::cacheKey($k, $v);
109
110         $sn = self::$cache->get($ck);
111
112         if (empty($sn)) {
113             $sn = self::staticGet($k, $v);
114             if (!empty($sn)) {
115                 self::$cache->set($ck, clone($sn));
116             }
117         }
118
119         return $sn;
120     }
121
122     function decache()
123     {
124         if (self::$cache) {
125             $keys = array('nickname', 'hostname', 'pathname');
126             foreach ($keys as $k) {
127                 $ck = self::cacheKey($k, $this->$k);
128                 self::$cache->delete($ck);
129             }
130         }
131     }
132
133     function update($orig=null)
134     {
135         if (is_object($orig)) {
136             $orig->decache(); # might be different keys
137         }
138         return parent::update($orig);
139     }
140
141     function delete()
142     {
143         $this->decache(); # while we still have the values!
144         return parent::delete();
145     }
146
147     /**
148      * @param string $servername hostname
149      * @param string $pathname URL base path
150      * @param string $wildcard hostname suffix to match wildcard config
151      */
152     static function setupSite($servername, $pathname, $wildcard)
153     {
154         global $config;
155
156         $sn = null;
157
158         // XXX I18N, probably not crucial for hostnames
159         // XXX This probably needs a tune up
160
161         if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
162             // special case for exact match
163             if (0 == strcasecmp($servername, $wildcard)) {
164                 $sn = self::memGet('nickname', '');
165             } else {
166                 $parts = explode('.', $servername);
167                 $sn = self::memGet('nickname', strtolower($parts[0]));
168             }
169         } else {
170             $sn = self::memGet('hostname', strtolower($servername));
171
172             if (empty($sn)) {
173                 // Try for a no-www address
174                 if (0 == strncasecmp($servername, 'www.', 4)) {
175                     $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
176                 }
177             }
178         }
179
180         if (!empty($sn)) {
181
182             // Redirect to the right URL
183
184             if (!empty($sn->hostname) &&
185                 empty($_SERVER['HTTPS']) &&
186                 0 != strcasecmp($sn->hostname, $servername)) {
187                 $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
188             } else if (!empty($_SERVER['HTTPS']) &&
189                        0 != strcasecmp($sn->hostname, $servername) &&
190                        0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
191                 $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
192             }
193
194             $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
195             $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
196             $dbpass = $sn->dbpass;
197             $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
198
199             $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
200
201             $config['site']['name'] = $sn->sitename;
202             $config['site']['nickname'] = $sn->nickname;
203
204             self::$wildcard = $wildcard;
205
206             $config['site']['wildcard'] =& self::$wildcard;
207
208             if (!empty($sn->hostname)) {
209                 $config['site']['server'] = $sn->hostname;
210             }
211
212             if (!empty($sn->theme)) {
213                 $config['site']['theme'] = $sn->theme;
214             }
215             if (!empty($sn->logo)) {
216                 $config['site']['logo'] = $sn->logo;
217             }
218
219             return $sn;
220         } else {
221             return null;
222         }
223     }
224
225     // Code partially mooked from http://www.richler.de/en/php-redirect/
226     // (C) 2006 by Heiko Richler  http://www.richler.de/
227     // LGPL
228
229     function redirectTo($destination)
230     {
231         $old = 'http'.
232           (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
233           '://'.
234           $_SERVER['HTTP_HOST'].
235           $_SERVER['REQUEST_URI'].
236           $_SERVER['QUERY_STRING'];
237         if ($old == $destination) { // this would be a loop!
238             // error_log(...) ?
239             return false;
240         }
241
242         header('HTTP/1.1 301 Moved Permanently');
243         header("Location: $destination");
244
245         print "<a href='$destination'>$destination</a>\n";
246
247         exit;
248     }
249
250     function getServerName()
251     {
252         if (!empty($this->hostname)) {
253             return $this->hostname;
254         } else {
255             return $this->nickname . '.' . self::$wildcard;
256         }
257     }
258
259     /**
260      * Return site meta-info tags as an array
261      * @return array of strings
262      */
263     function getTags()
264     {
265         return array_filter(explode("|", strval($this->tags)));
266     }
267
268     /**
269      * Check if this site record has a particular meta-info tag attached.
270      * @param string $tag
271      * @return bool
272      */
273     function hasTag($tag)
274     {
275         return in_array($tag, $this->getTags());
276     }
277 }