123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
- class Status_network extends Safe_DataObject
- {
-
-
- public $__table = 'status_network';
- public $site_id;
- public $nickname;
- public $hostname;
- public $pathname;
- public $dbhost;
- public $dbuser;
- public $dbpass;
- public $dbname;
- public $sitename;
- public $theme;
- public $logo;
- public $created;
- public $modified;
-
- static function getKV($k,$v=NULL) {
-
- $i = DB_DataObject::staticGet('Status_network',$k,$v);
-
-
-
- global $_DB_DATAOBJECT;
- unset($_DB_DATAOBJECT['CACHE']['status_network']);
- return $i;
- }
-
-
-
- public static $cache = null;
- public static $cacheInitialized = false;
- static $base = null;
- static $wildcard = null;
-
- static function setupDB($dbhost, $dbuser, $dbpass, $dbname, array $servers)
- {
- global $config;
- $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
- $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
- foreach (array('status_network', 'status_network_tag', 'unavailable_status_network') as $table) {
- $config['db']['table_'.$table] = $dbname;
- }
- if (class_exists('Memcache')) {
- self::$cache = new Memcache();
-
-
-
-
-
-
-
-
- $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
- if (!is_array($servers)) {
- $servers = array($servers);
- }
- foreach($servers as $server) {
- $parts = explode(':', $server);
- $server = $parts[0];
- if (count($parts) > 1) {
- $port = $parts[1];
- } else {
- $port = 11211;
- }
- self::$cache->addServer($server, $port, $persist);
- }
- self::$cacheInitialized = true;
- }
- self::$base = $dbname;
- }
- static function cacheKey($k, $v) {
- return 'gnusocial:' . self::$base . ':status_network:'.$k.':'.$v;
- }
- static function memGet($k, $v)
- {
- if (!self::$cache) {
- return self::getKV($k, $v);
- }
- $ck = self::cacheKey($k, $v);
- $sn = self::$cache->get($ck);
- if (empty($sn)) {
- $sn = self::getKV($k, $v);
- if (!empty($sn)) {
- self::$cache->set($ck, clone($sn));
- }
- }
- return $sn;
- }
- function decache()
- {
- if (self::$cache) {
- $keys = array('nickname', 'hostname', 'pathname');
- foreach ($keys as $k) {
- $ck = self::cacheKey($k, $this->$k);
- self::$cache->delete($ck);
- }
- }
- }
- function update($dataObject=false)
- {
- if (is_object($dataObject)) {
- $dataObject->decache();
- }
- return parent::update($dataObject);
- }
-
- function updateKeys(&$orig)
- {
- $this->_connect();
- foreach (array('hostname', 'pathname') as $k) {
- if (strcmp($this->$k, $orig->$k) != 0) {
- $parts[] = $k . ' = ' . $this->_quote($this->$k);
- }
- }
- if (count($parts) == 0) {
-
- return true;
- }
- $toupdate = implode(', ', $parts);
- $table = common_database_tablename($this->tableName());
- $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
- ' WHERE nickname = ' . $this->_quote($this->nickname);
- $orig->decache();
- $result = $this->query($qry);
- $this->decache();
- return $result;
- }
- function delete($useWhere=false)
- {
- $this->decache();
- return parent::delete($useWhere);
- }
-
- static function getFromHostname($servername, $wildcard)
- {
- $sn = null;
- if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
-
- if (0 == strcasecmp($servername, $wildcard)) {
- $sn = self::memGet('nickname', '');
- } else {
- $parts = explode('.', $servername);
- $sn = self::memGet('nickname', strtolower($parts[0]));
- }
- } else {
- $sn = self::memGet('hostname', strtolower($servername));
- if (empty($sn)) {
-
- if (0 == strncasecmp($servername, 'www.', 4)) {
- $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
- }
- }
- }
- return $sn;
- }
-
- static function setupSite($servername, $pathname, $wildcard)
- {
- global $config;
- $sn = null;
-
-
- $sn = self::getFromHostname($servername, $wildcard);
- if (!empty($sn)) {
-
- if (!empty($sn->hostname) &&
- empty($_SERVER['HTTPS']) &&
- 0 != strcasecmp($sn->hostname, $servername)) {
- $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
- } else if (!empty($_SERVER['HTTPS']) &&
- 0 != strcasecmp($sn->hostname, $servername) &&
- 0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
- $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
- }
- $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
- $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
- $dbpass = $sn->dbpass;
- $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
- $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
- $config['site']['name'] = $sn->sitename;
- $config['site']['nickname'] = $sn->nickname;
- self::$wildcard = $wildcard;
- $config['site']['wildcard'] =& self::$wildcard;
- if (!empty($sn->hostname)) {
- $config['site']['server'] = $sn->hostname;
- }
- if (!empty($sn->theme)) {
- $config['site']['theme'] = $sn->theme;
- }
- if (!empty($sn->logo)) {
- $config['site']['logo'] = $sn->logo;
- }
- return $sn;
- } else {
- return null;
- }
- }
-
-
-
- function redirectTo($destination)
- {
- $old = 'http'.
- (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
- '://'.
- $_SERVER['HTTP_HOST'].
- $_SERVER['REQUEST_URI'].
- $_SERVER['QUERY_STRING'];
- if ($old == $destination) {
-
- return false;
- }
- header('HTTP/1.1 301 Moved Permanently');
- header("Location: $destination");
- print "<a href='$destination'>$destination</a>\n";
- exit;
- }
- function getServerName()
- {
- if (!empty($this->hostname)) {
- return $this->hostname;
- } else {
- return $this->nickname . '.' . self::$wildcard;
- }
- }
-
- function getTags()
- {
- return Status_network_tag::getTags($this->site_id);
- }
-
- function setTags(array $tags)
- {
- $this->clearTags();
- foreach ($tags as $tag) {
- if (!empty($tag)) {
- $snt = new Status_network_tag();
- $snt->site_id = $this->site_id;
- $snt->tag = $tag;
- $snt->created = common_sql_now();
- $id = $snt->insert();
- if (!$id) {
-
- throw new Exception(_("Unable to save tag."));
- }
- }
- }
- return true;
- }
- function clearTags()
- {
- $tag = new Status_network_tag();
- $tag->site_id = $this->site_id;
- if ($tag->find()) {
- while($tag->fetch()) {
- $tag->delete();
- }
- }
- $tag->free();
- }
-
- function hasTag($tag)
- {
- return in_array($tag, $this->getTags());
- }
- }
|