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