Status_network.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Table Definition for status_network
  4. *
  5. * StatusNet - the distributed open-source microblogging tool
  6. * Copyright (C) 2009, StatusNet, Inc.
  7. *
  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.
  12. *
  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.
  17. *
  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/>.
  20. */
  21. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  22. class Status_network extends Safe_DataObject
  23. {
  24. ###START_AUTOCODE
  25. /* the code below is auto generated do not remove the above tag */
  26. public $__table = 'status_network'; // table name
  27. public $site_id; // int(4) primary_key not_null
  28. public $nickname; // varchar(64) unique_key not_null
  29. public $hostname; // varchar(255) unique_key
  30. public $pathname; // varchar(255) unique_key
  31. public $dbhost; // varchar(255)
  32. public $dbuser; // varchar(255)
  33. public $dbpass; // varchar(255)
  34. public $dbname; // varchar(255)
  35. public $sitename; // varchar(255)
  36. public $theme; // varchar(255)
  37. public $logo; // varchar(255)
  38. public $created; // datetime() not_null
  39. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  40. /* Static get */
  41. static function getKV($k,$v=NULL) {
  42. // TODO: This must probably be turned into a non-static call
  43. $i = DB_DataObject::staticGet('Status_network',$k,$v);
  44. // Don't use local process cache; if we're fetching multiple
  45. // times it's because we're reloading it in a long-running
  46. // process; we need a fresh copy!
  47. global $_DB_DATAOBJECT;
  48. unset($_DB_DATAOBJECT['CACHE']['status_network']);
  49. return $i;
  50. }
  51. /* the code above is auto generated do not remove the tag below */
  52. ###END_AUTOCODE
  53. // XXX: made public so Status_network_tag can eff with it
  54. public static $cache = null;
  55. public static $cacheInitialized = false;
  56. static $base = null;
  57. static $wildcard = null;
  58. /**
  59. * @param string $dbhost
  60. * @param string $dbuser
  61. * @param string $dbpass
  62. * @param string $dbname
  63. * @param array $servers memcached servers to use for caching config info
  64. */
  65. static function setupDB($dbhost, $dbuser, $dbpass, $dbname, array $servers)
  66. {
  67. global $config;
  68. $config['db']['database_'.$dbname] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
  69. $config['db']['ini_'.$dbname] = INSTALLDIR.'/classes/status_network.ini';
  70. foreach (array('status_network', 'status_network_tag', 'unavailable_status_network') as $table) {
  71. $config['db']['table_'.$table] = $dbname;
  72. }
  73. if (class_exists('Memcache')) {
  74. self::$cache = new Memcache();
  75. // If we're a parent command-line process we need
  76. // to be able to close out the connection after
  77. // forking, so disable persistence.
  78. //
  79. // We'll turn it back on again the second time
  80. // through which will either be in a child process,
  81. // or a single-process script which is switching
  82. // configurations.
  83. $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
  84. if (!is_array($servers)) {
  85. $servers = array($servers);
  86. }
  87. foreach($servers as $server) {
  88. $parts = explode(':', $server);
  89. $server = $parts[0];
  90. if (count($parts) > 1) {
  91. $port = $parts[1];
  92. } else {
  93. $port = 11211;
  94. }
  95. self::$cache->addServer($server, $port, $persist);
  96. }
  97. self::$cacheInitialized = true;
  98. }
  99. self::$base = $dbname;
  100. }
  101. static function cacheKey($k, $v) {
  102. return 'gnusocial:' . self::$base . ':status_network:'.$k.':'.$v;
  103. }
  104. static function memGet($k, $v)
  105. {
  106. if (!self::$cache) {
  107. return self::getKV($k, $v);
  108. }
  109. $ck = self::cacheKey($k, $v);
  110. $sn = self::$cache->get($ck);
  111. if (empty($sn)) {
  112. $sn = self::getKV($k, $v);
  113. if (!empty($sn)) {
  114. self::$cache->set($ck, clone($sn));
  115. }
  116. }
  117. return $sn;
  118. }
  119. function decache()
  120. {
  121. if (self::$cache) {
  122. $keys = array('nickname', 'hostname', 'pathname');
  123. foreach ($keys as $k) {
  124. $ck = self::cacheKey($k, $this->$k);
  125. self::$cache->delete($ck);
  126. }
  127. }
  128. }
  129. function update($dataObject=false)
  130. {
  131. if (is_object($dataObject)) {
  132. $dataObject->decache(); # might be different keys
  133. }
  134. return parent::update($dataObject);
  135. }
  136. /**
  137. * DB_DataObject doesn't allow updating keys (even non-primary)
  138. */
  139. function updateKeys(&$orig)
  140. {
  141. $this->_connect();
  142. foreach (array('hostname', 'pathname') as $k) {
  143. if (strcmp($this->$k, $orig->$k) != 0) {
  144. $parts[] = $k . ' = ' . $this->_quote($this->$k);
  145. }
  146. }
  147. if (count($parts) == 0) {
  148. // No changes
  149. return true;
  150. }
  151. $toupdate = implode(', ', $parts);
  152. $table = common_database_tablename($this->tableName());
  153. $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
  154. ' WHERE nickname = ' . $this->_quote($this->nickname);
  155. $orig->decache();
  156. $result = $this->query($qry);
  157. $this->decache();
  158. return $result;
  159. }
  160. function delete($useWhere=false)
  161. {
  162. $this->decache(); # while we still have the values!
  163. return parent::delete($useWhere);
  164. }
  165. /**
  166. * @param string $servername hostname
  167. * @param string $wildcard hostname suffix to match wildcard config
  168. * @return mixed Status_network or null
  169. */
  170. static function getFromHostname($servername, $wildcard)
  171. {
  172. $sn = null;
  173. if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
  174. // special case for exact match
  175. if (0 == strcasecmp($servername, $wildcard)) {
  176. $sn = self::memGet('nickname', '');
  177. } else {
  178. $parts = explode('.', $servername);
  179. $sn = self::memGet('nickname', strtolower($parts[0]));
  180. }
  181. } else {
  182. $sn = self::memGet('hostname', strtolower($servername));
  183. if (empty($sn)) {
  184. // Try for a no-www address
  185. if (0 == strncasecmp($servername, 'www.', 4)) {
  186. $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
  187. }
  188. }
  189. }
  190. return $sn;
  191. }
  192. /**
  193. * @param string $servername hostname
  194. * @param string $pathname URL base path
  195. * @param string $wildcard hostname suffix to match wildcard config
  196. */
  197. static function setupSite($servername, $pathname, $wildcard)
  198. {
  199. global $config;
  200. $sn = null;
  201. // XXX I18N, probably not crucial for hostnames
  202. // XXX This probably needs a tune up
  203. $sn = self::getFromHostname($servername, $wildcard);
  204. if (!empty($sn)) {
  205. // Redirect to the right URL
  206. if (!empty($sn->hostname) &&
  207. empty($_SERVER['HTTPS']) &&
  208. 0 != strcasecmp($sn->hostname, $servername)) {
  209. $sn->redirectTo('http://'.$sn->hostname.$_SERVER['REQUEST_URI']);
  210. } else if (!empty($_SERVER['HTTPS']) &&
  211. 0 != strcasecmp($sn->hostname, $servername) &&
  212. 0 != strcasecmp($sn->nickname.'.'.$wildcard, $servername)) {
  213. $sn->redirectTo('https://'.$sn->nickname.'.'.$wildcard.$_SERVER['REQUEST_URI']);
  214. }
  215. $dbhost = (empty($sn->dbhost)) ? 'localhost' : $sn->dbhost;
  216. $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser;
  217. $dbpass = $sn->dbpass;
  218. $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname;
  219. $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname";
  220. $config['site']['name'] = $sn->sitename;
  221. $config['site']['nickname'] = $sn->nickname;
  222. self::$wildcard = $wildcard;
  223. $config['site']['wildcard'] =& self::$wildcard;
  224. if (!empty($sn->hostname)) {
  225. $config['site']['server'] = $sn->hostname;
  226. }
  227. if (!empty($sn->theme)) {
  228. $config['site']['theme'] = $sn->theme;
  229. }
  230. if (!empty($sn->logo)) {
  231. $config['site']['logo'] = $sn->logo;
  232. }
  233. return $sn;
  234. } else {
  235. return null;
  236. }
  237. }
  238. // Code partially mooked from http://www.richler.de/en/php-redirect/
  239. // (C) 2006 by Heiko Richler http://www.richler.de/
  240. // LGPL
  241. function redirectTo($destination)
  242. {
  243. $old = 'http'.
  244. (($_SERVER['HTTPS'] == 'on') ? 'S' : '').
  245. '://'.
  246. $_SERVER['HTTP_HOST'].
  247. $_SERVER['REQUEST_URI'].
  248. $_SERVER['QUERY_STRING'];
  249. if ($old == $destination) { // this would be a loop!
  250. // error_log(...) ?
  251. return false;
  252. }
  253. header('HTTP/1.1 301 Moved Permanently');
  254. header("Location: $destination");
  255. print "<a href='$destination'>$destination</a>\n";
  256. exit;
  257. }
  258. function getServerName()
  259. {
  260. if (!empty($this->hostname)) {
  261. return $this->hostname;
  262. } else {
  263. return $this->nickname . '.' . self::$wildcard;
  264. }
  265. }
  266. /**
  267. * Return site meta-info tags as an array
  268. * @return array of strings
  269. */
  270. function getTags()
  271. {
  272. return Status_network_tag::getTags($this->site_id);
  273. }
  274. /**
  275. * Save a given set of tags
  276. * @param array tags
  277. * @fixme only add/remove differentials
  278. */
  279. function setTags(array $tags)
  280. {
  281. $this->clearTags();
  282. foreach ($tags as $tag) {
  283. if (!empty($tag)) {
  284. $snt = new Status_network_tag();
  285. $snt->site_id = $this->site_id;
  286. $snt->tag = $tag;
  287. $snt->created = common_sql_now();
  288. $id = $snt->insert();
  289. if (!$id) {
  290. // TRANS: Exception thrown when a tag cannot be saved.
  291. throw new Exception(_("Unable to save tag."));
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297. function clearTags()
  298. {
  299. $tag = new Status_network_tag();
  300. $tag->site_id = $this->site_id;
  301. if ($tag->find()) {
  302. while($tag->fetch()) {
  303. $tag->delete();
  304. }
  305. }
  306. $tag->free();
  307. }
  308. /**
  309. * Check if this site record has a particular meta-info tag attached.
  310. * @param string $tag
  311. * @return bool
  312. */
  313. function hasTag($tag)
  314. {
  315. return in_array($tag, $this->getTags());
  316. }
  317. }