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