]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Status_network.php
96212c465775804105067a1f3b028939d564dbda
[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
52     static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
53     {
54         global $config;
55
56         $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
57         $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
58         $config['db']['table_status_network'] = $dbname;
59
60         if (class_exists('Memcache')) {
61             self::$cache = new Memcache();
62
63             if (is_array($servers)) {
64                 foreach($servers as $server) {
65                     self::$cache->addServer($server);
66                 }
67             } else {
68                 self::$cache->addServer($servers);
69             }
70         }
71
72         self::$base = $dbname;
73     }
74
75     static function cacheKey($k, $v) {
76         return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
77     }
78
79     static function memGet($k, $v)
80     {
81         if (!self::$cache) {
82             return self::staticGet($k, $v);
83         }
84
85         $ck = self::cacheKey($k, $v);
86
87         $sn = self::$cache->get($ck);
88
89         if (empty($sn)) {
90             $sn = self::staticGet($k, $v);
91             if (!empty($sn)) {
92                 self::$cache->set($ck, $sn);
93             }
94         }
95
96         return $sn;
97     }
98
99     function decache()
100     {
101         if (self::$cache) {
102             $keys = array('nickname', 'hostname', 'pathname');
103             foreach ($keys as $k) {
104                 $ck = self::cacheKey($k, $this->$k);
105                 self::$cache->delete($ck);
106             }
107         }
108     }
109
110     function update($orig=null)
111     {
112         if (is_object($orig)) {
113             $orig->decache(); # might be different keys
114         }
115         return parent::update($orig);
116     }
117
118     function delete()
119     {
120         $this->decache(); # while we still have the values!
121         return parent::delete();
122     }
123
124     static function setupSite($servername, $pathname, $wildcard)
125     {
126         global $config;
127
128         $sn = null;
129
130         // XXX I18N, probably not crucial for hostnames
131         // XXX This probably needs a tune up
132
133         if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
134             // special case for exact match
135             if (0 == strcasecmp($servername, $wildcard)) {
136                 $sn = self::memGet('nickname', '');
137             } else {
138                 $parts = explode('.', $servername);
139                 $sn = self::memGet('nickname', strtolower($parts[0]));
140             }
141         } else {
142             $sn = self::memGet('hostname', strtolower($servername));
143
144             if (empty($sn)) {
145                 // Try for a no-www address
146                 if (0 == strncasecmp($servername, 'www.', 4)) {
147                     $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
148                 }
149             }
150         }
151
152         if (!empty($sn)) {
153
154             // Redirect to the right URL
155
156             if (!empty($sn->hostname) &&
157                 empty($_SERVER['HTTPS']) &&
158                 0 != strcasecmp($sn->hostname, $servername)) {
159                 $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
160             } else if (!empty($_SERVER['HTTPS']) &&
161                        0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
162                 $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
163             }
164
165             $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
166             $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
167             $dbpass = $sn->dbpass;
168             $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
169
170             $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
171
172             $config['site']['name'] = $sn->sitename;
173
174             if (!empty($sn->theme)) {
175                 $config['site']['theme'] = $sn->theme;
176             }
177             if (!empty($sn->logo)) {
178                 $config['site']['logo'] = $sn->logo;
179             }
180
181             return $sn;
182         } else {
183             return null;
184         }
185     }
186
187     // Code partially mooked from http://www.richler.de/en/php-redirect/
188     // (C) 2006 by Heiko Richler  http://www.richler.de/
189     // LGPL
190
191     function redirectTo($destination)
192     {
193         $old = 'http'.
194           (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
195           '://'.
196           $_SERVER['HTTP_HOST'].
197           $_SERVER['REQUEST_URI'].
198           $_SERVER['QUERY_STRING'];
199         if ($old == $destination) { // this would be a loop!
200             // error_log(...) ?
201             return false;
202         }
203
204         header('HTTP/1.1 301 Moved Permanently');
205         header("Location: $destination");
206
207         print "<a href='$destination'>$destination</a>\n";
208
209         exit;
210     }
211 }