Changeset 11691

Show
Ignore:
Timestamp:
08/23/07 10:22:24 (1 year ago)
Author:
piotras
Message:

Create configuration file.
Try to find if apache is listening on host's port

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midgard/data/midgard_setup_vhost.php

    r11655 r11691  
    44 
    55require_once 'midgard_setup_globals.php'; 
     6require_once 'midgard_setup_vhost_config.php'; 
    67 
    78class midgard_setup_vhost  
    89{ 
    9      
    1010    public $name; 
    1111    public $port; 
     
    1414    private $midgard_host; 
    1515    private $config; 
     16 
     17    private $host_dir; 
     18    private $blob_dir; 
     19    private $root_file; 
     20    private $root_file_mmp; 
     21    private $cache_dir; 
     22    private $midcom_cache_dir; 
     23    private $config_file_path; 
     24    private $midgard_httpd_conf; 
     25    private $midgard_httpd_dir; 
     26 
     27    private $midcom_rewrite_condition; 
    1628     
    17     public function __construct(midgard_setup_config &$config) 
    18     { 
    19         $this->name = "localhost"; 
    20         $this->port = 80; 
    21         $this->prefix = ""; 
    22         $this->config = $config; 
    23     } 
    24  
    25     static public function enable_apache_module() 
    26     { 
    27         $module_enable = FALSE; 
    28  
    29         echo MIDGARD_SETUP_APACHE_CONF; 
     29    public function __construct(midgard_setup_config &$config, $name, $port = "80", $prefix = "") 
     30    { 
     31        $this->name = $name; 
     32        $this->port = $port; 
     33        $this->prefix = $prefix; 
     34        $this->config = &$config; 
     35 
     36        if(MIDGARD_SETUP_APACHE_LIBEXEC_PATH == "") 
     37            throw new Exception("MIDGARD_SETUP_APACHE_LIBEXEC_PATH is empty"); 
     38 
     39        /* DocumentRoot */ 
     40        $this->host_dir = MIDGARD_SETUP_DIRECTORY_VAR."/lib/midgard/vhosts/"; 
     41        $this->host_dir .= $this->name."/".$this->port; 
     42         
     43        /* Blobdir */ 
     44        $this->blob_dir = MIDGARD_SETUP_DIRECTORY_VAR."/lib/midgard/blobs/"; 
     45        $this->blob_dir .= $this->config->midgard_config->database; 
     46 
     47        /* RootFile(s) */ 
     48        $this->root_file = MIDGARD_SETUP_APACHE_LIBEXEC_PATH."/midgard-root-nommp.php"; 
     49        $this->root_file_mmp = MIDGARD_SETUP_APACHE_LIBEXEC_PATH."/midgard-root-nommp.php"; 
     50 
     51        /* Cache dir */ 
     52        $this->cache_dir = MIDGARD_SETUP_DIRECTORY_VAR."/cache/midgard/"; 
     53        $this->cache_dir .= $this->config->midgard_config->database; 
     54 
     55        /* Config file location */ 
     56        $this->config_file_path = MIDGARD_SETUP_DIRECTORY_ETC."/midgard/apache/vhosts/"; 
     57        $this->config_file_path .= $this->name."_".$this->port; 
     58        
     59        /* midgard's httpd.conf directory and file location */ 
     60        $this->midgard_httpd_conf = MIDGARD_SETUP_DIRECTORY_ETC."/midgard/apache/httpd.conf"; 
     61        $this->midgard_httpd_dir = MIDGARD_SETUP_DIRECTORY_ETC."/midgard/apache";         
     62 
     63        /* Ugly but acceptable atm IMO */ 
     64        $this->midcom_rewrite_condition = " 
     65# Uncomment if you want to redirect all midcom-admin requests to 
     66# secured host. Keep in mind that mod_rewrite module is mandatory 
     67# and should be loaded in Apache configuration. 
     68# More docs about configuration may be found at: 
     69# http://www.midgard-project.org/midcom-permalink-ebfd755b5fc58087bc4f5771585c63eb 
     70# --- SSL rewrite Start --- 
     71#RewriteEngine On 
     72#RewriteCond %{REQUEST_URI} !^/midcom-admin.* 
     73#RewriteCond %{REQUEST_URI} !^/midcom-static.* 
     74#RewriteCond %{REQUEST_URI} !\.(gif|jpg|css|ico)$ 
     75#RewriteRule /(.*)$ http://{$this->name}/$1 
     76 
     77#RewriteEngine On 
     78#RewriteCond %{REQUEST_URI} ^/midcom-admin.* 
     79#RewriteRule /(.*)$ https://{$this->name}/$1 
     80# --- SSL rewrite End --- "; 
     81    } 
     82 
     83    static private function directive_exists($directive, $filepath) 
     84    { 
     85        if(!is_file($filepath)) 
     86            return FALSE; 
     87         
     88        if(!$content = file($filepath)) 
     89            return FALSE; 
     90         
     91        $directives = array(); 
     92 
     93        $length = strlen($directive); 
     94 
     95        foreach($content as $line) 
     96        { 
     97            $exists = strstr($line, $directive); 
     98            if($exists != FALSE && strlen($line) > $length) 
     99            { 
     100                $listen = substr_compare($line, $directive, 1, $length); 
     101                if($listen > -1) 
     102                    $directives[] = $line; 
     103            } 
     104        } 
     105 
     106        if(count($directives) > 0) 
     107            return $directives; 
     108 
     109        return FALSE; 
     110    } 
     111 
     112    static private function param_exists_in_directives($param, $directives) 
     113    { 
     114        if($directives) 
     115        { 
     116            foreach($directives as $line) 
     117            { 
     118                $chunks = explode(" ", $line); 
     119                 
     120                foreach($chunks as $piece) 
     121                { 
     122                    $piece = trim($piece); 
     123                    if($param == $piece) 
     124                        return TRUE; 
     125                } 
     126            } 
     127        } 
     128 
     129        return FALSE; 
     130    } 
     131 
     132    private function apache_is_listening($port) 
     133    { 
     134        $directives = self::directive_exists("Listen", MIDGARD_SETUP_APACHE_CONF); 
     135        
     136        if(self::param_exists_in_directives($port, $directives)) 
     137            return TRUE; 
     138 
     139        $included = self::directive_exists("Include", MIDGARD_SETUP_APACHE_CONF); 
     140         
     141        $included_directives = array(); 
     142 
     143        if($included != FALSE) 
     144        { 
     145            foreach($included as $line) 
     146            { 
     147                if(substr($line, 0, 7) == "Include") 
     148                { 
     149                    $filepath = trim(substr($line, 7)); 
     150                    if(is_file($filepath)) 
     151                    {  
     152                        $new_array = self::directive_exists("Listen", $filepath); 
     153                        if(is_array($new_array)) 
     154                        { 
     155                            $included_directives =  
     156                                array_merge($included_directives, $new_array); 
     157                        } 
     158                    } 
     159                } 
     160            } 
     161        } 
     162         
     163        if(self::param_exists_in_directives($port, $included_directives)) 
     164            return TRUE; 
     165 
     166        $directives = self::directive_exists("Listen", $this->midgard_httpd_conf); 
     167 
     168        if(self::param_exists_in_directives($port, $directives)) 
     169            return TRUE; 
     170 
     171        return FALSE; 
     172    } 
     173     
     174    private function create_midgard_httpd_conf() 
     175    { 
     176        $load_module = MIDGARD_SETUP_APACHE_LIBEXEC_PATH."/midgard-apache2.so"; 
     177        $conf_dir = $this->midgard_httpd_dir; 
     178        $port = $this->port; 
     179 
     180        if(!is_file($this->midgard_httpd_conf))  
     181        { 
     182             
     183            $content = "LoadModule midgard_module {$load_module} \n"; 
     184            $content .= "\n"; 
     185 
     186            /* Check if apache is listening already on this port */ 
     187            if(!$this->apache_is_listening($port)) 
     188            { 
     189                $content .= "Listen {$port} \n"; 
     190                $content .= "NameVirtualHost *:{$port}\n"; 
     191                $content .= "\n"; 
     192            } 
     193             
     194            $content .= "Include {$conf_dir}/vhosts/[^\.#]*\n"; 
     195             
     196            if(!file_put_contents($this->midgard_httpd_conf, $content)) 
     197                return FALSE; 
     198                 
     199            return TRUE; 
     200        }    
     201 
     202        return TRUE; 
     203    } 
     204 
     205    public function enable_apache_module() 
     206    { 
     207        $module_enable = FALSE;         
    30208 
    31209        if(!$content = file(MIDGARD_SETUP_APACHE_CONF)) 
     
    35213        } 
    36214        
     215        $this->create_midgard_httpd_conf(); 
     216 
    37217        /* check if midgard's httpd.conf is included */ 
    38218        foreach($content as $line) 
     
    67247    } 
    68248 
    69     public function create_config() 
    70     { 
    71  
     249    public function create_configuration($location = NULL) 
     250    { 
     251        $cnf = new midgard_setup_vhost_config($this->name, $this->port); 
     252         
     253        /* Add directives */ 
     254        $cnf->add_directive("DocumentRoot", array($this->host_dir)); 
     255         
     256        /* Midgard directives */ 
     257        $cnf->add_directive("\n# MIDGARD settings \n", array()); 
     258        $cnf->add_directive("MidgardEnige", array("on")); 
     259        $cnf->add_directive("MidgardBlobDir", array($this->blob_dir)); 
     260        $cnf->add_directive("#MidgardRootfile", array($this->root_file)); 
     261        $cnf->add_directive("MidgardRootFile", array($this->root_file_mmp)); 
     262        $cnf->add_directive("MidgardPageCacheDir", array($this->cache_dir)); 
     263        $cnf->add_directive("MidgardDefaultRealm", array("Midgard")); 
     264        $cnf->add_directive("MidgardDatabase",  
     265            array ( 
     266                $this->config->midgard_config->database,  
     267                $this->config->midgard_config->dbuser, 
     268                $this->config->midgard_config->dbpass ));  
     269 
     270        /* TODO , add <Directory ${MIDCOM_STATIC_DIR}> Allow from all */ 
     271 
     272        /* PHP settings */ 
     273        $cnf->add_directive("\n# PHP settings \n", array()); 
     274        $cnf->add_directive("php_admin_flag", array("file_uploads", "On")); 
     275        $cnf->add_directive("php_flag", array("magic_quotes_gpc", "Off")); 
     276        $cnf->add_directive("php_value", array("memory_limit", "20M")); 
     277        $cnf->add_directive("php_value", array("post_max_size","50M")); 
     278        $cnf->add_directive("php_value", array("upload_max_filesize","50M")); 
     279 
     280        $cnf->add_directive($this->midcom_rewrite_condition, array()); 
     281 
     282        /* Add directories */ 
     283        $cnf->add_directory($this->host_dir,  
     284            array("Options +SymLinksIfOwnerMatch", "Allow from all")); 
     285        $cnf->add_directory($this->cache_dir, array("Allow from all")); 
     286 
     287        $midcom_static_dir = $this->host_dir."/midcom-static"; 
     288        $cnf->add_directory($midcom_static_dir,  
     289            array ( 
     290                "Options -Indexes", 
     291                "<FilesMatch \"\.(php)$\">", 
     292                "Deny from all", 
     293                "</FilesMatch>" )); 
     294 
     295        /* Add locations */ 
     296         
     297        $content = $cnf->get_configuration();        
     298         
     299        if($location == NULL) 
     300            $config_file = $this->config_file_path; 
     301        else  
     302            $config_file = $location; 
     303 
     304        if(!file_put_contents($this->config_file_path, $content)) 
     305            return FALSE; 
     306             
     307        return TRUE;         
    72308    } 
    73309 
     
    102338        chgrp($cache_dir, MIDGARD_SETUP_APACHE_GROUP); 
    103339 
    104         $db_cache_dir = $cache_dir.$this->config->midgard_config->database
     340        $db_cache_dir = $this->cache_dir
    105341        if(!is_dir($db_cache_dir)) 
    106342            mkdir($db_cache_dir, 0771, TRUE); 
     
    120356 
    121357        /* vhost individual directories */ 
    122         $host_root_dir = MIDGARD_SETUP_DIRECTORY_VAR."/lib/midgard/vhosts/"; 
    123         $host_root_dir .= $this->name."/".$this->port; 
    124         if(!is_dir($host_root_dir)) 
    125             mkdir($host_root_dir, 0711, TRUE); 
     358        if(!is_dir($this->host_dir)) 
     359            mkdir($this->host_dir, 0711, TRUE); 
    126360         
    127361        /* Change the group of the DocumentRoot (to be able to restrict permissions) */ 
    128         chgrp($host_root_dir, MIDGARD_SETUP_APACHE_GROUP); 
    129         chmod($host_root_dir, 0550); 
     362        chgrp($this->host_dir, MIDGARD_SETUP_APACHE_GROUP); 
     363        chmod($this->host_dir, 0550); 
    130364 
    131365        /* TODO */