]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/installer.php
don't forget to add qstring to static paths
[quix0rs-gnu-social.git] / lib / installer.php
index 441f7266063e932ef83f84369be20ebcebf5d506..871b479a77a1d387382f3decdf2625dcdbd5ff7b 100644 (file)
@@ -42,9 +42,9 @@
 abstract class Installer
 {
     /** Web site info */
-    public $sitename, $server, $path, $fancy;
+    public $sitename, $server, $path, $fancy, $siteProfile;
     /** DB info */
-    public $host, $dbname, $dbtype, $username, $password, $db;
+    public $host, $database, $dbtype, $username, $password, $db;
     /** Administrator info */
     public $adminNick, $adminPass, $adminEmail, $adminUpdates;
     /** Should we skip writing the configuration file? */
@@ -73,7 +73,7 @@ abstract class Installer
         error_reporting($old);
         return $ok;
     }
-    
+
     /**
      * Check if all is ready for installation
      *
@@ -185,7 +185,7 @@ abstract class Installer
     /**
      * Basic validation on the database paramters
      * Side effects: error output if not valid
-     * 
+     *
      * @return boolean success
      */
     function validateDb()
@@ -218,7 +218,7 @@ abstract class Installer
     /**
      * Basic validation on the administrator user paramters
      * Side effects: error output if not valid
-     * 
+     *
      * @return boolean success
      */
     function validateAdmin()
@@ -236,7 +236,7 @@ abstract class Installer
         }
         // @fixme hardcoded list; should use User::allowed_nickname()
         // if/when it's safe to have loaded the infrastructure here
-        $blacklist = array('main', 'admin', 'twitter', 'settings', 'rsd.xml', 'favorited', 'featured', 'favoritedrss', 'featuredrss', 'rss', 'getfile', 'api', 'groups', 'group', 'peopletag', 'tag', 'user', 'message', 'conversation', 'bookmarklet', 'notice', 'attachment', 'search', 'index.php', 'doc', 'opensearch', 'robots.txt', 'xd_receiver.html', 'facebook');
+        $blacklist = array('main', 'panel', 'twitter', 'settings', 'rsd.xml', 'favorited', 'featured', 'favoritedrss', 'featuredrss', 'rss', 'getfile', 'api', 'groups', 'group', 'peopletag', 'tag', 'user', 'message', 'conversation', 'bookmarklet', 'notice', 'attachment', 'search', 'index.php', 'doc', 'opensearch', 'robots.txt', 'xd_receiver.html', 'facebook');
         if (in_array($this->adminNick, $blacklist)) {
             $this->updateStatus('The user nickname "' . htmlspecialchars($this->adminNick) .
                          '" is reserved.', true);
@@ -251,10 +251,29 @@ abstract class Installer
         return !$fail;
     }
 
+    /**
+     * Make sure a site profile was selected
+     *
+     * @return type boolean success
+     */
+    function validateSiteProfile()
+    {
+        $fail = false;
+
+        $sprofile = $this->siteProfile;
+
+        if (empty($sprofile))  {
+            $this->updateStatus("No site profile selected.", true);
+            $fail = true;
+        }
+
+        return !$fail;
+    }
+
     /**
      * Set up the database with the appropriate function for the selected type...
      * Saves database info into $this->db.
-     * 
+     *
      * @fixme escape things in the connection string in case we have a funny pass etc
      * @return mixed array of database connection params on success, false on failure
      */
@@ -316,7 +335,7 @@ abstract class Installer
      * Open a connection to the database.
      *
      * @param <type> $dsn
-     * @return <type> 
+     * @return <type>
      */
     function connectDatabase($dsn)
     {
@@ -341,6 +360,7 @@ abstract class Installer
             }
             $schema->ensureTable($name, $def);
         }
+        return true;
     }
 
     /**
@@ -383,7 +403,7 @@ abstract class Installer
      * Write a stock configuration file.
      *
      * @return boolean success
-     * 
+     *
      * @fixme escape variables in output in case we have funny chars, apostrophes etc
      */
     function writeConf()
@@ -393,7 +413,7 @@ abstract class Installer
             'server' => $this->server,
             'path' => $this->path,
             'db_database' => $this->db['database'],
-            'db_type' => $this->db['type'],
+            'db_type' => $this->db['type']
         ));
 
         // assemble configuration file in a string
@@ -424,6 +444,41 @@ abstract class Installer
         return $res;
     }
 
+    /**
+     * Write the site profile. We do this after creating the initial user
+     * in case the site profile is set to single user. This gets around the
+     * 'chicken-and-egg' problem of the system requiring a valid user for
+     * single user mode, before the intial user is actually created. Yeah,
+     * we should probably do this in smarter way.
+     *
+     * @return int res number of bytes written
+     */
+    function writeSiteProfile()
+    {
+        $vals = $this->phpVals(array(
+            'site_profile' => $this->siteProfile,
+            'nickname' => $this->adminNick
+        ));
+
+        $cfg =
+        // site profile
+        "\$config['site']['profile'] = {$vals['site_profile']};\n";
+
+        if ($this->siteProfile == "singleuser") {
+            $cfg .= "\$config['singleuser']['nickname'] = {$vals['nickname']};\n\n";
+        } else {
+            $cfg .= "\n";
+        }
+
+        // Normalize line endings for Windows servers
+        $cfg = str_replace("\n", PHP_EOL, $cfg);
+
+        // write configuration file out to install directory
+        $res = file_put_contents(INSTALLDIR.'/config.php', $cfg, FILE_APPEND);
+
+        return $res;
+    }
+
     /**
      * Install schema into the database
      *
@@ -441,11 +496,12 @@ abstract class Installer
             if (!mb_strlen($stmt)) {
                 continue;
             }
-            $res = $conn->execute($stmt);
-            if (DB::isError($res)) {
-                $error = $result->getMessage();
+            try {
+                $res = $conn->simpleQuery($stmt);
+            } catch (Exception $e) {
+                $error = $e->getMessage();
                 $this->updateStatus("ERROR ($error) for SQL '$stmt'");
-                return $res;
+                return false;
             }
         }
         return true;
@@ -458,9 +514,6 @@ abstract class Installer
      */
     function registerInitialUser()
     {
-        define('STATUSNET', true);
-        define('LACONICA', true); // compatibility
-
         require_once INSTALLDIR . '/lib/common.php';
 
         $data = array('nickname' => $this->adminNick,
@@ -480,7 +533,7 @@ abstract class Installer
         $user->grantRole('owner');
         $user->grantRole('moderator');
         $user->grantRole('administrator');
-        
+
         // Attempt to do a remote subscribe to update@status.net
         // Will fail if instance is on a private network.
 
@@ -500,9 +553,9 @@ abstract class Installer
     /**
      * The beef of the installer!
      * Create database, config file, and admin user.
-     * 
+     *
      * Prerequisites: validation of input data.
-     * 
+     *
      * @return boolean success
      */
     function doInstall()
@@ -510,7 +563,9 @@ abstract class Installer
         $this->updateStatus("Initializing...");
         ini_set('display_errors', 1);
         error_reporting(E_ALL);
-        define('STATUSNET', 1);
+        if (!defined('STATUSNET')) {
+            define('STATUSNET', 1);
+        }
         require_once INSTALLDIR . '/lib/framework.php';
         StatusNet::initDefaults($this->server, $this->path);
 
@@ -526,6 +581,9 @@ abstract class Installer
             return false;
         }
 
+        // Make sure we can write to the file twice
+        $oldUmask = umask(000); 
+
         if (!$this->skipConfig) {
             $this->updateStatus("Writing config file...");
             $res = $this->writeConf();
@@ -551,6 +609,21 @@ abstract class Installer
             }
         }
 
+        if (!$this->skipConfig) {
+            $this->updateStatus("Setting site profile...");
+            $res = $this->writeSiteProfile();
+
+            if (!$res) {
+                $this->updateStatus("Can't write to config file.", true);
+                return false;
+            }
+        }
+
+        // Restore original umask
+        umask($oldUmask);
+        // Set permissions back to something decent
+        chmod(INSTALLDIR.'/config.php', 0644);
+        
         /*
             TODO https needs to be considered
         */