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