ElggRewriteTester::createHtaccess PHP Method

createHtaccess() public method

Create Elgg's .htaccess file or confirm that it exists
public createHtaccess ( string $url ) : boolean
$url string URL of rewrite test
return boolean
    public function createHtaccess($url)
    {
        $root = Directory\Local::root();
        $file = $root->getFile(".htaccess");
        if ($file->exists()) {
            // check that this is the Elgg .htaccess
            $data = $file->getContents();
            if ($data === FALSE) {
                // don't have permission to read the file
                $this->htaccessIssue = 'read_permission';
                return FALSE;
            }
            if (strpos($data, 'Elgg') === FALSE) {
                $this->htaccessIssue = 'non_elgg_htaccess';
                return FALSE;
            } else {
                // check if this is an old Elgg htaccess
                if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') == FALSE) {
                    $this->htaccessIssue = 'old_elgg_htaccess';
                    return FALSE;
                }
                return TRUE;
            }
        }
        if (!is_writable($root->getPath())) {
            $this->htaccessIssue = 'write_permission';
            return FALSE;
        }
        // create the .htaccess file
        $result = copy(\Elgg\Application::elggDir()->getPath("install/config/htaccess.dist"), $file->getPath());
        if (!$result) {
            $this->htaccessIssue = 'cannot_copy';
            return FALSE;
        }
        // does default RewriteBase work already?
        if (!$this->runRewriteTest($url)) {
            //try to rewrite to guessed subdirectory
            if ($subdir = $this->guessSubdirectory($url)) {
                $contents = $file->getContents();
                $contents = preg_replace("/#RewriteBase \\/(\r?\n)/", "RewriteBase {$subdir}\$1", $contents);
                if ($contents) {
                    $file->putContents($contents);
                }
            }
        }
        return TRUE;
    }

Usage Example

コード例 #1
0
ファイル: ElggInstaller.php プロジェクト: nogsus/Elgg
 /**
  * A batch install of Elgg
  *
  * All required parameters must be passed in as an associative array. See
  * $requiredParams for a list of them. This creates the necessary files,
  * loads the database, configures the site settings, and creates the admin
  * account. If it fails, an exception is thrown. It does not check any of
  * the requirements as the multiple step web installer does.
  *
  * If the settings.php file exists, it will use that rather than the parameters
  * passed to this function.
  *
  * @param array $params         Array of key value pairs
  * @param bool  $createHtaccess Should .htaccess be created
  *
  * @return void
  * @throws InstallationException
  */
 public function batchInstall(array $params, $createHtaccess = FALSE)
 {
     global $CONFIG;
     restore_error_handler();
     restore_exception_handler();
     $defaults = array('dbhost' => 'localhost', 'dbprefix' => 'elgg_', 'path' => $CONFIG->path, 'language' => 'en', 'siteaccess' => ACCESS_PUBLIC);
     $params = array_merge($defaults, $params);
     $requiredParams = array('dbuser', 'dbpassword', 'dbname', 'sitename', 'wwwroot', 'dataroot', 'displayname', 'email', 'username', 'password');
     foreach ($requiredParams as $key) {
         if (!array_key_exists($key, $params)) {
             $msg = elgg_echo('install:error:requiredfield', array($key));
             throw new InstallationException($msg);
         }
     }
     // password is passed in once
     $params['password1'] = $params['password2'] = $params['password'];
     if ($createHtaccess) {
         $rewriteTester = new ElggRewriteTester();
         if (!$rewriteTester->createHtaccess($CONFIG->path)) {
             throw new InstallationException(elgg_echo('install:error:htaccess'));
         }
     }
     $this->setInstallStatus();
     if (!$this->status['config']) {
         if (!$this->createSettingsFile($params)) {
             throw new InstallationException(elgg_echo('install:error:settings'));
         }
     }
     if (!$this->connectToDatabase()) {
         throw new InstallationException(elgg_echo('install:error:databasesettings'));
     }
     if (!$this->status['database']) {
         if (!$this->installDatabase()) {
             throw new InstallationException(elgg_echo('install:error:cannotloadtables'));
         }
     }
     // load remaining core libraries
     $this->finishBootstraping('settings');
     if (!$this->saveSiteSettings($params)) {
         throw new InstallationException(elgg_echo('install:error:savesitesettings'));
     }
     if (!$this->createAdminAccount($params)) {
         throw new InstallationException(elgg_echo('install:admin:cannot_create'));
     }
 }