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 Safe_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
45 function staticGet($k,$v=NULL) {
46 $i = DB_DataObject::staticGet('Status_network',$k,$v);
48 // Don't use local process cache; if we're fetching multiple
49 // times it's because we're reloading it in a long-running
50 // process; we need a fresh copy!
51 global $_DB_DATAOBJECT;
52 unset($_DB_DATAOBJECT['CACHE']['status_network']);
56 /* the code above is auto generated do not remove the tag below */
60 static $cacheInitialized = false;
62 static $wildcard = null;
65 * @param string $dbhost
66 * @param string $dbuser
67 * @param string $dbpass
68 * @param string $dbname
69 * @param array $servers memcached servers to use for caching config info
71 static function setupDB($dbhost, $dbuser, $dbpass, $dbname, $servers)
75 $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
76 $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
77 $config['db']['table_status_network'] = $dbname;
79 if (class_exists('Memcache')) {
80 self::$cache = new Memcache();
82 // If we're a parent command-line process we need
83 // to be able to close out the connection after
84 // forking, so disable persistence.
86 // We'll turn it back on again the second time
87 // through which will either be in a child process,
88 // or a single-process script which is switching
90 $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
91 if (is_array($servers)) {
92 foreach($servers as $server) {
93 self::$cache->addServer($server, 11211, $persist);
96 self::$cache->addServer($servers, 11211, $persist);
98 self::$cacheInitialized = true;
101 self::$base = $dbname;
104 static function cacheKey($k, $v) {
105 return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
108 static function memGet($k, $v)
111 return self::staticGet($k, $v);
114 $ck = self::cacheKey($k, $v);
116 $sn = self::$cache->get($ck);
119 $sn = self::staticGet($k, $v);
121 self::$cache->set($ck, clone($sn));
131 $keys = array('nickname', 'hostname', 'pathname');
132 foreach ($keys as $k) {
133 $ck = self::cacheKey($k, $this->$k);
134 self::$cache->delete($ck);
139 function update($orig=null)
141 if (is_object($orig)) {
142 $orig->decache(); # might be different keys
144 return parent::update($orig);
149 $this->decache(); # while we still have the values!
150 return parent::delete();
154 * @param string $servername hostname
155 * @param string $pathname URL base path
156 * @param string $wildcard hostname suffix to match wildcard config
158 static function setupSite($servername, $pathname, $wildcard)
164 // XXX I18N, probably not crucial for hostnames
165 // XXX This probably needs a tune up
167 if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
168 // special case for exact match
169 if (0 == strcasecmp($servername, $wildcard)) {
170 $sn = self::memGet('nickname', '');
172 $parts = explode('.', $servername);
173 $sn = self::memGet('nickname', strtolower($parts[0]));
176 $sn = self::memGet('hostname', strtolower($servername));
179 // Try for a no-www address
180 if (0 == strncasecmp($servername, 'www.', 4)) {
181 $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
188 // Redirect to the right URL
190 if (!empty($sn->hostname) &&
191 empty($_SERVER['HTTPS']) &&
192 0 != strcasecmp($sn->hostname, $servername)) {
193 $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
194 } else if (!empty($_SERVER['HTTPS']) &&
195 0 != strcasecmp($sn->hostname, $servername) &&
196 0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
197 $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
200 $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
201 $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
202 $dbpass = $sn->dbpass;
203 $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
205 $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
207 $config['site']['name'] = $sn->sitename;
208 $config['site']['nickname'] = $sn->nickname;
210 self::$wildcard = $wildcard;
212 $config['site']['wildcard'] =& self::$wildcard;
214 if (!empty($sn->hostname)) {
215 $config['site']['server'] = $sn->hostname;
218 if (!empty($sn->theme)) {
219 $config['site']['theme'] = $sn->theme;
221 if (!empty($sn->logo)) {
222 $config['site']['logo'] = $sn->logo;
231 // Code partially mooked from http://www.richler.de/en/php-redirect/
232 // (C) 2006 by Heiko Richler http://www.richler.de/
235 function redirectTo($destination)
238 (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
240 $_SERVER['HTTP_HOST'].
241 $_SERVER['REQUEST_URI'].
242 $_SERVER['QUERY_STRING'];
243 if ($old == $destination) { // this would be a loop!
248 header('HTTP/1.1 301 Moved Permanently');
249 header("Location: $destination");
251 print "<a href='$destination'>$destination</a>\n";
256 function getServerName()
258 if (!empty($this->hostname)) {
259 return $this->hostname;
261 return $this->nickname . '.' . self::$wildcard;
266 * Return site meta-info tags as an array
267 * @return array of strings
271 return array_filter(explode("|", strval($this->tags)));
275 * Check if this site record has a particular meta-info tag attached.
279 function hasTag($tag)
281 return in_array($tag, $this->getTags());