Status_network.php 12 KB

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