123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- <?php
- abstract class Installer
- {
-
- public $sitename, $server, $path, $fancy, $siteProfile, $ssl;
-
- public $host, $database, $dbtype, $username, $password, $db;
-
- public $avatarDir, $fileDir;
-
- public $adminNick, $adminPass, $adminEmail;
-
- public $skipConfig = false;
- public static $dbModules = array(
- 'mysql' => array(
- 'name' => 'MariaDB (or MySQL 5.5+)',
- 'check_module' => 'mysqli',
- 'scheme' => 'mysqli',
- ),
- );
-
- private function haveIncludeFile($filename) {
- $old = error_reporting(error_reporting() & ~E_WARNING);
- $ok = include_once($filename);
- error_reporting($old);
- return $ok;
- }
-
- function checkPrereqs()
- {
- $pass = true;
- $config = INSTALLDIR.'/config.php';
- if (!$this->skipConfig && file_exists($config)) {
- if (!is_writable($config) || filesize($config) > 0) {
- if (filesize($config) == 0) {
- $this->warning('Config file "config.php" already exists and is empty, but is not writable.');
- } else {
- $this->warning('Config file "config.php" already exists.');
- }
- $pass = false;
- }
- }
- if (version_compare(PHP_VERSION, '5.5.0', '<')) {
- $this->warning('Require PHP version 5.5.0 or greater.');
- $pass = false;
- }
- $reqs = array('gd', 'curl', 'intl', 'json',
- 'xmlwriter', 'mbstring', 'xml', 'dom', 'simplexml');
- foreach ($reqs as $req) {
- if (!$this->checkExtension($req)) {
- $this->warning(sprintf('Cannot load required extension: <code>%s</code>', $req));
- $pass = false;
- }
- }
-
- $missingExtensions = array();
- foreach (self::$dbModules as $type => $info) {
- if (!$this->checkExtension($info['check_module'])) {
- $missingExtensions[] = $info['check_module'];
- }
- }
- if (count($missingExtensions) == count(self::$dbModules)) {
- $req = implode(', ', $missingExtensions);
- $this->warning(sprintf('Cannot find a database extension. You need at least one of %s.', $req));
- $pass = false;
- }
-
- if (!$this->skipConfig && !is_writable(INSTALLDIR)) {
- $this->warning(sprintf('Cannot write config file to: <code>%s</code></p>', INSTALLDIR),
- sprintf('On your server, try this command: <code>chmod a+w %s</code>', INSTALLDIR));
- $pass = false;
- }
-
-
- if (!$this->skipConfig) {
- define('GNUSOCIAL', true);
- define('STATUSNET', true);
- require_once INSTALLDIR . '/lib/language.php';
- $_server=$this->server; $_path=$this->path;
- require_once INSTALLDIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'default.php';
- $fileSubdirs = [empty($this->avatarDir) ? $default['avatar']['dir'] : $this->avatarDir,
- empty($this->fileDir) ? $default['attachments']['dir'] : $this->fileDir];
- unset($default);
- foreach ($fileSubdirs as $fileFullPath) {
- if (!file_exists($fileFullPath)) {
- $pass = $pass && mkdir($fileFullPath);
- } elseif (!is_dir($fileFullPath)) {
- $this->warning(sprintf('GNU social expected a directory but found something else on this path: %s', $fileFullPath),
- 'Either make sure it goes to a directory or remove it and a directory will be created.');
- $pass = false;
- } elseif (!is_writable($fileFullPath)) {
- $this->warning(sprintf('Cannot write to %s directory: <code>%s</code>', $fileSubdir, $fileFullPath),
- sprintf('On your server, try this command: <code>chmod a+w %s</code>', $fileFullPath));
- $pass = false;
- }
- }
- }
- return $pass;
- }
-
- function checkExtension($name)
- {
- if (extension_loaded($name)) {
- return true;
- } elseif (function_exists('dl') && ini_get('enable_dl') && !ini_get('safe_mode')) {
-
-
- $soname = $name . '.' . PHP_SHLIB_SUFFIX;
- if (PHP_SHLIB_SUFFIX == 'dll') {
- $soname = "php_" . $soname;
- }
- return @dl($soname);
- } else {
- return false;
- }
- }
-
- function validateDb()
- {
- $fail = false;
- if (empty($this->host)) {
- $this->updateStatus("No hostname specified.", true);
- $fail = true;
- }
- if (empty($this->database)) {
- $this->updateStatus("No database specified.", true);
- $fail = true;
- }
- if (empty($this->username)) {
- $this->updateStatus("No username specified.", true);
- $fail = true;
- }
- if (empty($this->sitename)) {
- $this->updateStatus("No sitename specified.", true);
- $fail = true;
- }
- return !$fail;
- }
-
- function validateAdmin()
- {
- $fail = false;
- if (empty($this->adminNick)) {
- $this->updateStatus("No initial user nickname specified.", true);
- $fail = true;
- }
- if ($this->adminNick && !preg_match('/^[0-9a-z]{1,64}$/', $this->adminNick)) {
- $this->updateStatus('The user nickname "' . htmlspecialchars($this->adminNick) .
- '" is invalid; should be plain letters and numbers no longer than 64 characters.', true);
- $fail = true;
- }
-
-
- $blacklist = array('main', 'panel', 'twitter', 'settings', 'rsd.xml', 'favorited', 'featured', 'favoritedrss', 'featuredrss', 'rss', 'getfile', 'api', 'groups', 'group', 'peopletag', 'tag', 'user', 'message', 'conversation', 'notice', 'attachment', 'search', 'index.php', 'doc', 'opensearch', 'robots.txt', 'xd_receiver.html', 'facebook', 'activity');
- if (in_array($this->adminNick, $blacklist)) {
- $this->updateStatus('The user nickname "' . htmlspecialchars($this->adminNick) .
- '" is reserved.', true);
- $fail = true;
- }
- if (empty($this->adminPass)) {
- $this->updateStatus("No initial user password specified.", true);
- $fail = true;
- }
- return !$fail;
- }
-
- function validateSiteProfile()
- {
- if (empty($this->siteProfile)) {
- $this->updateStatus("No site profile selected.", true);
- return false;
- }
- return true;
- }
-
- function setupDatabase()
- {
- if ($this->db) {
- throw new Exception("Bad order of operations: DB already set up.");
- }
- $this->updateStatus("Starting installation...");
- if (empty($this->password)) {
- $auth = '';
- } else {
- $auth = ":$this->password";
- }
- $scheme = self::$dbModules[$this->dbtype]['scheme'];
- $dsn = "{$scheme}://{$this->username}{$auth}@{$this->host}/{$this->database}";
- $this->updateStatus("Checking database...");
- $conn = $this->connectDatabase($dsn);
- if (!$conn instanceof DB_common) {
-
- throw new Exception('Cannot connect to database: ' . $conn->getMessage());
- }
-
- if ($this->dbtype == 'mysql') {
-
-
-
-
- } else if ($this->dbtype == 'pgsql') {
- $record = $conn->getRow('SHOW server_encoding');
- if ($record->server_encoding != 'UTF8') {
- $this->updateStatus("GNU social requires UTF8 character encoding. Your database is ". htmlentities($record->server_encoding));
- return false;
- }
- }
- $res = $this->updateStatus("Creating database tables...");
- if (!$this->createCoreTables($conn)) {
- $this->updateStatus("Error creating tables.", true);
- return false;
- }
- foreach (array('sms_carrier' => 'SMS carrier',
- 'notice_source' => 'notice source',
- 'foreign_services' => 'foreign service')
- as $scr => $name) {
- $this->updateStatus(sprintf("Adding %s data to database...", $name));
- $res = $this->runDbScript($scr.'.sql', $conn);
- if ($res === false) {
- $this->updateStatus(sprintf("Can't run %s script.", $name), true);
- return false;
- }
- }
- $db = array('type' => $this->dbtype, 'database' => $dsn);
- return $db;
- }
-
- function connectDatabase($dsn)
- {
- global $_DB;
- return $_DB->connect($dsn);
- }
-
- function createCoreTables(DB_common $conn)
- {
- $schema = Schema::get($conn);
- $tableDefs = $this->getCoreSchema();
- foreach ($tableDefs as $name => $def) {
- if (defined('DEBUG_INSTALLER')) {
- echo " $name ";
- }
- $schema->ensureTable($name, $def);
- }
- return true;
- }
-
- function getCoreSchema()
- {
- $schema = array();
- include INSTALLDIR . '/db/core.php';
- return $schema;
- }
-
- function phpVal($val)
- {
- return var_export($val, true);
- }
-
- function phpVals($map)
- {
- return array_map(array($this, 'phpVal'), $map);
- }
-
- function writeConf()
- {
- $vals = $this->phpVals(array(
- 'sitename' => $this->sitename,
- 'server' => $this->server,
- 'path' => $this->path,
- 'ssl' => in_array($this->ssl, array('never', 'always'))
- ? $this->ssl
- : 'never',
- 'db_database' => $this->db['database'],
- 'db_type' => $this->db['type']
- ));
-
- $cfg = "<?php\n".
- "if (!defined('GNUSOCIAL')) { exit(1); }\n\n".
-
- "\$config['site']['name'] = {$vals['sitename']};\n\n".
-
- "\$config['site']['server'] = {$vals['server']};\n".
- "\$config['site']['path'] = {$vals['path']}; \n\n".
- "\$config['site']['ssl'] = {$vals['ssl']}; \n\n".
-
- ($this->fancy ? "\$config['site']['fancy'] = true;\n\n":'').
-
- "\$config['db']['database'] = {$vals['db_database']};\n\n".
- ($this->db['type'] == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n":'').
- "\$config['db']['type'] = {$vals['db_type']};\n\n".
- "// Uncomment below for better performance. Just remember you must run\n".
- "// php scripts/checkschema.php whenever your enabled plugins change!\n".
- "//\$config['db']['schemacheck'] = 'script';\n\n";
-
- $cfg = str_replace("\n", PHP_EOL, $cfg);
-
- $res = file_put_contents(INSTALLDIR.'/config.php', $cfg);
- return $res;
- }
-
- function writeSiteProfile()
- {
- $vals = $this->phpVals(array(
- 'site_profile' => $this->siteProfile,
- 'nickname' => $this->adminNick
- ));
- $cfg =
-
- "\$config['site']['profile'] = {$vals['site_profile']};\n";
- if ($this->siteProfile == "singleuser") {
- $cfg .= "\$config['singleuser']['nickname'] = {$vals['nickname']};\n\n";
- } else {
- $cfg .= "\n";
- }
-
- $cfg = str_replace("\n", PHP_EOL, $cfg);
-
- $res = file_put_contents(INSTALLDIR.'/config.php', $cfg, FILE_APPEND);
- return $res;
- }
-
- function runDbScript($filename, DB_common $conn)
- {
- $sql = trim(file_get_contents(INSTALLDIR . '/db/' . $filename));
- $stmts = explode(';', $sql);
- foreach ($stmts as $stmt) {
- $stmt = trim($stmt);
- if (!mb_strlen($stmt)) {
- continue;
- }
- try {
- $res = $conn->simpleQuery($stmt);
- } catch (Exception $e) {
- $error = $e->getMessage();
- $this->updateStatus("ERROR ($error) for SQL '$stmt'");
- return false;
- }
- }
- return true;
- }
-
- function registerInitialUser()
- {
-
-
- $server = $this->server;
- require_once INSTALLDIR . '/lib/common.php';
- $data = array('nickname' => $this->adminNick,
- 'password' => $this->adminPass,
- 'fullname' => $this->adminNick);
- if ($this->adminEmail) {
- $data['email'] = $this->adminEmail;
- }
- try {
- $user = User::register($data, true);
- } catch (Exception $e) {
- return false;
- }
-
- $user->grantRole('owner');
- $user->grantRole('moderator');
- $user->grantRole('administrator');
- return true;
- }
-
- function doInstall()
- {
- global $config;
- $this->updateStatus("Initializing...");
- ini_set('display_errors', 1);
- error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE);
- if (!defined('GNUSOCIAL')) {
- define('GNUSOCIAL', true);
- }
- if (!defined('STATUSNET')) {
- define('STATUSNET', true);
- }
- require_once INSTALLDIR . '/lib/framework.php';
- GNUsocial::initDefaults($this->server, $this->path);
- if ($this->siteProfile == "singleuser") {
-
- $config['singleuser']['enabled'] = true;
- }
- try {
- $this->db = $this->setupDatabase();
- if (!$this->db) {
-
- return false;
- }
- } catch (Exception $e) {
-
- $this->updateStatus("Database error: " . $e->getMessage(), true);
- return false;
- }
- if (!$this->skipConfig) {
-
- $oldUmask = umask(000);
- $this->updateStatus("Writing config file...");
- $res = $this->writeConf();
- if (!$res) {
- $this->updateStatus("Can't write config file.", true);
- return false;
- }
- }
- if (!empty($this->adminNick)) {
-
- if ($this->registerInitialUser()) {
- $this->updateStatus(
- "An initial user with the administrator role has been created."
- );
- } else {
- $this->updateStatus(
- "Could not create initial user account.",
- true
- );
- return false;
- }
- }
- if (!$this->skipConfig) {
- $this->updateStatus("Setting site profile...");
- $res = $this->writeSiteProfile();
- if (!$res) {
- $this->updateStatus("Can't write to config file.", true);
- return false;
- }
-
- umask($oldUmask);
-
- chmod(INSTALLDIR.'/config.php', 0644);
- }
-
- $scheme = $this->ssl === 'always' ? 'https' : 'http';
- $link = "{$scheme}://{$this->server}/{$this->path}";
- $this->updateStatus("GNU social has been installed at $link");
- $this->updateStatus(
- '<strong>DONE!</strong> You can visit your <a href="'.htmlspecialchars($link).'">new GNU social site</a> (log in as "'.htmlspecialchars($this->adminNick).'"). If this is your first GNU social install, make your experience the best possible by visiting our resource site to join the <a href="https://gnu.io/social/resources/">mailing list or IRC</a>. <a href="'.htmlspecialchars($link).'/doc/faq">FAQ is found here</a>.'
- );
- return true;
- }
-
- abstract function warning($message, $submessage='');
-
- abstract function updateStatus($status, $error=false);
- }
|