domainstatusnetworkinstaller.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Installer class for domain-based multi-homing systems
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category DomainStatusNetwork
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Installer class for domain-based multi-homing systems
  37. *
  38. * @category DomainStatusNetwork
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class DomainStatusNetworkInstaller extends Installer
  46. {
  47. protected $domain = null;
  48. protected $rootname = null;
  49. protected $sitedb = null;
  50. protected $rootpass = null;
  51. protected $nickname = null;
  52. protected $sn = null;
  53. public $verbose = false;
  54. function __construct($domain)
  55. {
  56. $this->domain = $domain;
  57. }
  58. /**
  59. * Go for it!
  60. * @return boolean success
  61. */
  62. function main()
  63. {
  64. // We don't check prereqs. Check 'em before setting up a
  65. // multi-home system, kthxbi
  66. if ($this->prepare()) {
  67. return $this->handle();
  68. } else {
  69. $this->showHelp();
  70. return false;
  71. }
  72. }
  73. /**
  74. * Get our input parameters...
  75. * @return boolean success
  76. */
  77. function prepare()
  78. {
  79. $config = $this->getConfig();
  80. $this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
  81. // XXX make this configurable
  82. $this->sitename = sprintf('The %s Status Network', $this->domain);
  83. $this->server = $this->nickname.'.'.$config['WILDCARD'];
  84. $this->path = null;
  85. $this->fancy = true;
  86. $datanick = $this->databaseize($this->nickname);
  87. $this->host = $config['DBHOSTNAME'];
  88. $this->database = $datanick.$config['DBBASE'];
  89. $this->dbtype = 'mysql'; // XXX: support others... someday
  90. $this->username = $datanick.$config['USERBASE'];
  91. // Max size for MySQL
  92. if (strlen($this->username) > 16) {
  93. $this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
  94. }
  95. $pwgen = $config['PWDGEN'];
  96. $password = `$pwgen`;
  97. $this->password = trim($password);
  98. // For setting up the database
  99. $this->rootname = $config['ADMIN'];
  100. $this->rootpass = $config['ADMINPASS'];
  101. $this->sitehost = $config['SITEDBHOSTNAME'];
  102. $this->sitedb = $config['SITEDB'];
  103. $tagstr = $config['TAGS'];
  104. if (!empty($tagstr)) {
  105. $this->tags = preg_split('/[\s,]+/', $tagstr);
  106. } else {
  107. $this->tags = array();
  108. }
  109. // Explicitly empty
  110. $this->adminNick = null;
  111. $this->adminPass = null;
  112. $this->adminEmail = null;
  113. /** Should we skip writing the configuration file? */
  114. $this->skipConfig = true;
  115. if (!$this->validateDb()) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. function handle()
  121. {
  122. return $this->doInstall();
  123. }
  124. function setupDatabase()
  125. {
  126. $this->updateStatus('Creating database...');
  127. $this->createDatabase();
  128. parent::setupDatabase();
  129. $this->updateStatus('Creating file directories...');
  130. $this->createDirectories();
  131. $this->updateStatus('Saving status network...');
  132. $this->saveStatusNetwork();
  133. $this->updateStatus('Checking schema for plugins...');
  134. $this->checkSchema();
  135. }
  136. function saveStatusNetwork()
  137. {
  138. Status_network::setupDB($this->sitehost,
  139. $this->rootname,
  140. $this->rootpass,
  141. $this->sitedb, array());
  142. $sn = new Status_network();
  143. $sn->nickname = $this->nickname;
  144. $sn->dbhost = $this->host;
  145. $sn->dbuser = $this->username;
  146. $sn->dbpass = $this->password;
  147. $sn->dbname = $this->database;
  148. $sn->sitename = $this->sitename;
  149. $sn->created = common_sql_now();
  150. $result = $sn->insert();
  151. if (!$result) {
  152. throw new ServerException("Could not create status_network: " . print_r($sn, true));
  153. }
  154. // Re-fetch; stupid auto-increment integer isn't working
  155. $sn = Status_network::getKV('nickname', $sn->nickname);
  156. if (empty($sn)) {
  157. throw new ServerException("Created {$this->nickname} status_network and could not find it again.");
  158. }
  159. // Set default tags
  160. $tags = $this->tags;
  161. // Add domain tag
  162. $tags[] = 'domain='.$this->domain;
  163. $sn->setTags($tags);
  164. $this->sn = $sn;
  165. }
  166. function checkSchema()
  167. {
  168. $config = $this->getConfig();
  169. Status_network::$wildcard = $config['WILDCARD'];
  170. GNUsocial::switchSite($this->nickname);
  171. // We need to initialize the schema_version stuff to make later setup easier
  172. $schema = array();
  173. require INSTALLDIR.'/db/core.php';
  174. $tableDefs = $schema;
  175. $schema = Schema::get();
  176. $schemaUpdater = new SchemaUpdater($schema);
  177. foreach ($tableDefs as $table => $def) {
  178. $schemaUpdater->register($table, $def);
  179. }
  180. $schemaUpdater->checkSchema();
  181. Event::handle('CheckSchema');
  182. }
  183. function getStatusNetwork()
  184. {
  185. return $this->sn;
  186. }
  187. function createDirectories()
  188. {
  189. $config = $this->getConfig();
  190. foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
  191. $base = $config[$key];
  192. $dirname = $base.'/'.$this->nickname;
  193. // Make sure our bits are set
  194. $mask = umask(0);
  195. mkdir($dirname, 0770, true);
  196. umask($mask);
  197. // If you set the setuid bit on your base dirs this should be
  198. // unnecessary, but just in case. You must be root for this
  199. // to work.
  200. if (array_key_exists('WEBUSER', $config)) {
  201. chown($dirname, $config['WEBUSER']);
  202. }
  203. if (array_key_exists('WEBGROUP', $config)) {
  204. chgrp($dirname, $config['WEBGROUP']);
  205. }
  206. }
  207. }
  208. function createDatabase()
  209. {
  210. // Create the New DB
  211. /* FIXME
  212. * Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli instead.
  213. */
  214. $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
  215. if (!$res) {
  216. throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
  217. }
  218. mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
  219. $return = mysql_select_db($this->database, $res);
  220. if (!$return) {
  221. throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
  222. }
  223. foreach (array('localhost', '%') as $src) {
  224. mysql_query("GRANT ALL ON " .
  225. mysql_real_escape_string($this->database).".* TO '" .
  226. $this->username . "'@'".$src."' ".
  227. "IDENTIFIED BY '".$this->password."'", $res);
  228. }
  229. mysql_close($res);
  230. }
  231. function getConfig()
  232. {
  233. static $config;
  234. $cfg_file = "/etc/statusnet/setup.cfg";
  235. if (empty($config)) {
  236. $result = parse_ini_file($cfg_file);
  237. $config = array();
  238. foreach ($result as $key => $value) {
  239. $key = str_replace('export ', '', $key);
  240. $config[$key] = $value;
  241. }
  242. }
  243. return $config;
  244. }
  245. function showHelp()
  246. {
  247. }
  248. function warning($message, $submessage='')
  249. {
  250. print $this->html2text($message) . "\n";
  251. if ($submessage != '') {
  252. print " " . $this->html2text($submessage) . "\n";
  253. }
  254. print "\n";
  255. }
  256. function updateStatus($status, $error=false)
  257. {
  258. if ($this->verbose || $error) {
  259. if ($error) {
  260. print "ERROR: ";
  261. }
  262. print $this->html2text($status);
  263. print "\n";
  264. }
  265. }
  266. private function html2text($html)
  267. {
  268. // break out any links for text legibility
  269. $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
  270. '\2 &lt;\1&gt;',
  271. $html);
  272. return common_strip_html($breakout);
  273. }
  274. function databaseize($nickname)
  275. {
  276. $nickname = str_replace('-', '_', $nickname);
  277. return $nickname;
  278. }
  279. }