Status_network.php 12 KB

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