3 * Table Definition for status_network
5 * Laconica - a distributed open-source microblogging tool
6 * Copyright (C) 2009, Control Yourself, 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('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/statusnet.ini';
58 $config['db']['table_status_network'] = $dbname;
60 self::$cache = new Memcache();
62 if (is_array($servers)) {
63 foreach($servers as $server) {
64 self::$cache->addServer($server);
67 self::$cache->addServer($servers);
70 self::$base = $dbname;
73 static function cacheKey($k, $v) {
74 return 'laconica:' . self::$base . ':status_network:'.$k.':'.$v;
77 static function memGet($k, $v)
79 $ck = self::cacheKey($k, $v);
81 $sn = self::$cache->get($ck);
84 $sn = self::staticGet($k, $v);
86 self::$cache->set($ck, $sn);
95 $keys = array('nickname', 'hostname', 'pathname');
96 foreach ($keys as $k) {
97 $ck = self::cacheKey($k, $this->$k);
98 self::$cache->delete($ck);
102 function update($orig=null)
104 if (is_object($orig)) {
105 $orig->decache(); # might be different keys
107 return parent::update($orig);
112 $this->decache(); # while we still have the values!
113 return parent::delete();
116 static function setupSite($servername, $pathname, $wildcard)
122 // XXX I18N, probably not crucial for hostnames
123 // XXX This probably needs a tune up
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', '');
130 $parts = explode('.', $servername);
131 $sn = self::memGet('nickname', strtolower($parts[0]));
134 $sn = self::memGet('hostname', strtolower($servername));
137 // Try for a no-www address
138 if (0 == strncasecmp($servername, 'www.', 4)) {
139 $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
145 if (!empty($sn->hostname) && 0 != strcasecmp($sn->hostname, $servername)) {
146 $sn->redirectToHostname();
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;
153 $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
155 $config['site']['name'] = $sn->sitename;
157 if (!empty($sn->theme)) {
158 $config['site']['theme'] = $sn->theme;
160 if (!empty($sn->logo)) {
161 $config['site']['logo'] = $sn->logo;
170 // Code partially mooked from http://www.richler.de/en/php-redirect/
171 // (C) 2006 by Heiko Richler http://www.richler.de/
174 function redirectToHostname()
176 $destination = 'http://'.$this->hostname;
177 $destination .= $_SERVER['REQUEST_URI'];
180 (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
182 $_SERVER['HTTP_HOST'].
183 $_SERVER['REQUEST_URI'].
184 $_SERVER['QUERY_STRING'];
185 if ($old == $destination) { // this would be a loop!
190 header('HTTP/1.1 301 Moved Permanently');
191 header("Location: $destination");
193 print "<a href='$destination'>$destination</a>\n";