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 */
52 static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
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;
60 if (class_exists('Memcache')) {
61 self::$cache = new Memcache();
63 if (is_array($servers)) {
64 foreach($servers as $server) {
65 self::$cache->addServer($server);
68 self::$cache->addServer($servers);
72 self::$base = $dbname;
75 static function cacheKey($k, $v) {
76 return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
79 static function memGet($k, $v)
82 return self::staticGet($k, $v);
85 $ck = self::cacheKey($k, $v);
87 $sn = self::$cache->get($ck);
90 $sn = self::staticGet($k, $v);
92 self::$cache->set($ck, $sn);
102 $keys = array('nickname', 'hostname', 'pathname');
103 foreach ($keys as $k) {
104 $ck = self::cacheKey($k, $this->$k);
105 self::$cache->delete($ck);
110 function update($orig=null)
112 if (is_object($orig)) {
113 $orig->decache(); # might be different keys
115 return parent::update($orig);
120 $this->decache(); # while we still have the values!
121 return parent::delete();
124 static function setupSite($servername, $pathname, $wildcard)
130 // XXX I18N, probably not crucial for hostnames
131 // XXX This probably needs a tune up
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', '');
138 $parts = explode('.', $servername);
139 $sn = self::memGet('nickname', strtolower($parts[0]));
142 $sn = self::memGet('hostname', strtolower($servername));
145 // Try for a no-www address
146 if (0 == strncasecmp($servername, 'www.', 4)) {
147 $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
153 if (!empty($sn->hostname) && 0 != strcasecmp($sn->hostname, $servername)) {
154 $sn->redirectToHostname();
156 $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
157 $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
158 $dbpass = $sn->dbpass;
159 $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
161 $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
163 $config['site']['name'] = $sn->sitename;
165 if (!empty($sn->theme)) {
166 $config['site']['theme'] = $sn->theme;
168 if (!empty($sn->logo)) {
169 $config['site']['logo'] = $sn->logo;
178 // Code partially mooked from http://www.richler.de/en/php-redirect/
179 // (C) 2006 by Heiko Richler http://www.richler.de/
182 function redirectToHostname()
184 $destination = 'http://'.$this->hostname;
185 $destination .= $_SERVER['REQUEST_URI'];
188 (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
190 $_SERVER['HTTP_HOST'].
191 $_SERVER['REQUEST_URI'].
192 $_SERVER['QUERY_STRING'];
193 if ($old == $destination) { // this would be a loop!
198 header('HTTP/1.1 301 Moved Permanently');
199 header("Location: $destination");
201 print "<a href='$destination'>$destination</a>\n";