3 * Table Definition for status_network
5 * StatusNet - the distributed open-source microblogging tool
6 * Copyright (C) 2009, StatusNet, Inc.
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.
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.
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/>.
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
24 class Status_network extends DB_DataObject
27 /* the code below is auto generated do not remove the above tag */
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
44 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Status_network',$k,$v); }
46 /* the code above is auto generated do not remove the tag below */
51 static $wildcard = null;
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
60 static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
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;
68 if (class_exists('Memcache')) {
69 self::$cache = new Memcache();
71 // Can't close persistent connections, making forking painful.
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);
81 self::$cache->addServer($servers, 11211, $persist);
85 self::$base = $dbname;
88 static function cacheKey($k, $v) {
89 return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
92 static function memGet($k, $v)
95 return self::staticGet($k, $v);
98 $ck = self::cacheKey($k, $v);
100 $sn = self::$cache->get($ck);
103 $sn = self::staticGet($k, $v);
105 self::$cache->set($ck, clone($sn));
115 $keys = array('nickname', 'hostname', 'pathname');
116 foreach ($keys as $k) {
117 $ck = self::cacheKey($k, $this->$k);
118 self::$cache->delete($ck);
123 function update($orig=null)
125 if (is_object($orig)) {
126 $orig->decache(); # might be different keys
128 return parent::update($orig);
133 $this->decache(); # while we still have the values!
134 return parent::delete();
138 * @param string $servername hostname
139 * @param string $pathname URL base path
140 * @param string $wildcard hostname suffix to match wildcard config
142 static function setupSite($servername, $pathname, $wildcard)
148 // XXX I18N, probably not crucial for hostnames
149 // XXX This probably needs a tune up
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', '');
156 $parts = explode('.', $servername);
157 $sn = self::memGet('nickname', strtolower($parts[0]));
160 $sn = self::memGet('hostname', strtolower($servername));
163 // Try for a no-www address
164 if (0 == strncasecmp($servername, 'www.', 4)) {
165 $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
172 // Redirect to the right URL
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']);
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;
189 $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
191 $config['site']['name'] = $sn->sitename;
192 $config['site']['nickname'] = $sn->nickname;
194 self::$wildcard = $wildcard;
196 $config['site']['wildcard'] =& self::$wildcard;
198 if (!empty($sn->hostname)) {
199 $config['site']['server'] = $sn->hostname;
202 if (!empty($sn->theme)) {
203 $config['site']['theme'] = $sn->theme;
205 if (!empty($sn->logo)) {
206 $config['site']['logo'] = $sn->logo;
215 // Code partially mooked from http://www.richler.de/en/php-redirect/
216 // (C) 2006 by Heiko Richler http://www.richler.de/
219 function redirectTo($destination)
222 (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
224 $_SERVER['HTTP_HOST'].
225 $_SERVER['REQUEST_URI'].
226 $_SERVER['QUERY_STRING'];
227 if ($old == $destination) { // this would be a loop!
232 header('HTTP/1.1 301 Moved Permanently');
233 header("Location: $destination");
235 print "<a href='$destination'>$destination</a>\n";
240 function getServerName()
242 if (!empty($this->hostname)) {
243 return $this->hostname;
245 return $this->nickname . '.' . self::$wildcard;