tbrehm
2010-08-09 cd568a72940b3ea6766a83c1437ceb4bc3f575b7
server/plugins-available/apache2_plugin.inc.php
@@ -415,9 +415,9 @@
            }
            else {
               exec("cp /usr/local/ispconfig/server/conf/index/standard_index.html_".substr(escapeshellcmd($conf["language"]),0,2)." ".escapeshellcmd($data["new"]["document_root"])."/web/index.html");
               exec("cp /usr/local/ispconfig/server/conf/index/favicon.ico ".escapeshellcmd($data["new"]["document_root"])."/web/");
               exec("cp /usr/local/ispconfig/server/conf/index/robots.txt ".escapeshellcmd($data["new"]["document_root"])."/web/");
               exec("cp /usr/local/ispconfig/server/conf/index/.htaccess ".escapeshellcmd($data["new"]["document_root"])."/web/");
               if(is_file('/usr/local/ispconfig/server/conf/index/favicon.ico')) exec("cp /usr/local/ispconfig/server/conf/index/favicon.ico ".escapeshellcmd($data["new"]["document_root"])."/web/");
               if(is_file('/usr/local/ispconfig/server/conf/index/robots.txt')) exec("cp /usr/local/ispconfig/server/conf/index/robots.txt ".escapeshellcmd($data["new"]["document_root"])."/web/");
               if(is_file('/usr/local/ispconfig/server/conf/index/.htaccess')) exec("cp /usr/local/ispconfig/server/conf/index/.htaccess ".escapeshellcmd($data["new"]["document_root"])."/web/");
            }
         }
         exec("chmod -R a+r ".escapeshellcmd($data["new"]["document_root"])."/web/");
@@ -543,7 +543,30 @@
      $this->_exec("chown $username:$groupname ".escapeshellcmd($data["new"]["document_root"])."/log/error.log");
      // Create the vhost config file
      //* Write the custom php.ini file, if custom_php_ini filed is not empty
      $custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$data["new"]["system_user"];
      if(!is_dir($web_config['website_basedir'].'/conf')) mkdir($web_config['website_basedir'].'/conf');
      if(trim($data["new"]['custom_php_ini']) != '') {
         $has_custom_php_ini = true;
         if(!is_dir($custom_php_ini_dir)) mkdir($custom_php_ini_dir);
         $php_ini_content = '';
         if($data["new"]['php'] == 'mod') {
            $master_php_ini_path = $web_config['php_ini_path_apache'];
         } else {
            $master_php_ini_path = $web_config['php_ini_path_cgi'];
         }
         if($master_php_ini_path != '' && substr($master_php_ini_path,-7) == 'php.ini' && is_file($master_php_ini_path)) {
            $php_ini_content .= file_get_contents($master_php_ini_path)."\n";
         }
         $php_ini_content .= trim($data["new"]['custom_php_ini']);
         file_put_contents($custom_php_ini_dir.'/php.ini',$php_ini_content);
      } else {
         $has_custom_php_ini = false;
         if(is_file($custom_php_ini_dir.'/php.ini')) unlink($custom_php_ini_dir.'/php.ini');
      }
      //* Create the vhost config file
      $app->load('tpl');
      $tpl = new tpl();
@@ -557,6 +580,8 @@
      $vhost_data["allow_override"] = ($data["new"]["allow_override"] == '')?'All':$data["new"]["allow_override"];
      $vhost_data["php_open_basedir"] = ($data["new"]["php_open_basedir"] == '')?$data["new"]["document_root"]:$data["new"]["php_open_basedir"];
      $vhost_data["ssl_domain"] = $data["new"]["ssl_domain"];
      $vhost_data["has_custom_php_ini"] = $has_custom_php_ini;
      $vhost_data["custom_php_ini_dir"] = escapeshellcmd($custom_php_ini_dir);
      // Check if a SSL cert exists
      $ssl_dir = $data["new"]["document_root"]."/ssl";
@@ -582,9 +607,11 @@
      $rewrite_rules = array();
      if($data["new"]["redirect_type"] != '') {
         if(substr($data["new"]["redirect_path"],-1) != '/') $data["new"]["redirect_path"] .= '/';
         /* Disabled path extension
         if($data["new"]["redirect_type"] == 'no' && substr($data["new"]["redirect_path"],0,4) != 'http') {
            $data["new"]["redirect_path"] = $data["new"]["document_root"]."/web".realpath($data["new"]["redirect_path"]).'/';
         }
         */
         $rewrite_rules[] = array(   'rewrite_domain'    => $data["new"]["domain"],
               'rewrite_type'       => ($data["new"]["redirect_type"] == 'no')?'':'['.$data["new"]["redirect_type"].']',
@@ -633,9 +660,11 @@
            // Rewriting
            if($alias["redirect_type"] != '') {
               if(substr($data["new"]["redirect_path"],-1) != '/') $data["new"]["redirect_path"] .= '/';
               /* Disabled the path extension
               if($data["new"]["redirect_type"] == 'no' && substr($data["new"]["redirect_path"],0,4) != 'http') {
                  $data["new"]["redirect_path"] = $data["new"]["document_root"]."/web".realpath($data["new"]["redirect_path"]).'/';
               }
               */
               $rewrite_rules[] = array(   'rewrite_domain'    => $alias["domain"],
                     'rewrite_type'       => ($alias["redirect_type"] == 'no')?'':'['.$alias["redirect_type"].']',
                     'rewrite_target'    => $alias["redirect_path"]);
@@ -705,8 +734,12 @@
         $fcgi_tpl = new tpl();
         $fcgi_tpl->newTemplate("php-fcgi-starter.master");
         $fcgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
         if($has_custom_php_ini) {
            $fcgi_tpl->setVar('php_ini_path',escapeshellcmd($custom_php_ini_dir));
         } else {
            $fcgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
         }
         $fcgi_tpl->setVar('document_root',escapeshellcmd($data["new"]["document_root"]));
         $fcgi_tpl->setVar('php_fcgi_children',escapeshellcmd($fastcgi_config["fastcgi_children"]));
         $fcgi_tpl->setVar('php_fcgi_max_requests',escapeshellcmd($fastcgi_config["fastcgi_max_requests"]));
@@ -768,6 +801,13 @@
         //$cgi_tpl->setVar('open_basedir', "/var/www/" . $data["new"]["domain"]);
         $cgi_tpl->setVar('php_cgi_bin',$cgi_config["cgi_bin"]);
         $cgi_tpl->setVar('security_level',$web_config["security_level"]);
         $cgi_tpl->setVar('has_custom_php_ini',$has_custom_php_ini);
         if($has_custom_php_ini) {
            $cgi_tpl->setVar('php_ini_path',escapeshellcmd($custom_php_ini_dir));
         } else {
            $cgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
         }
         $cgi_starter_script = escapeshellcmd($cgi_starter_path.$cgi_config["cgi_starter_script"]);
         file_put_contents($cgi_starter_script,$cgi_tpl->grab());
@@ -785,6 +825,10 @@
      }
      $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"].'/'.$data["new"]["domain"].'.vhost');
      //* Make a backup copy of vhost file
      copy($vhost_file,$vhost_file.'~');
      //* Write vhost file
      file_put_contents($vhost_file,$tpl->grab());
      $app->log("Writing the vhost file: $vhost_file",LOGLEVEL_DEBUG);
      unset($tpl);
@@ -834,14 +878,41 @@
            unset($htp_file);
         }
      }
      if($apache_chrooted) {
         $app->services->restartServiceDelayed('httpd','restart');
      } else {
         // request a httpd reload when all records have been processed
         $app->services->restartServiceDelayed('httpd','reload');
      //* Create awstats configuration
      if($data["new"]["stats_type"] == 'awstats' && $data["new"]["type"] == "vhost") {
         $this->awstats_update($data,$web_config);
      }
      if($web_config['check_apache_config'] == 'y') {
         //* Test if apache starts with the new configuration file
         $apache_online_status_before_restart = $this->_checkTcp('localhost',80);
         $app->log("Apache status is: ".$apache_online_status_before_restart,LOGLEVEL_DEBUG);
         $app->services->restartService('httpd','restart');
         //* Check if apache restarted successfully if it was online before
         $apache_online_status_after_restart = $this->_checkTcp('localhost',80);
         $app->log("Apache online status after restart is: ".$apache_online_status_after_restart,LOGLEVEL_DEBUG);
         if($apache_online_status_before_restart && !$apache_online_status_after_restart) {
            $app->log("Apache did not restart after the configuration change for website ".$data["new"]["domain"].' Reverting the configuration. Saved not working config as '.$vhost_file.'.err',LOGLEVEL_WARN);
            copy($vhost_file,$vhost_file.'.err');
            copy($vhost_file.'~',$vhost_file);
            $app->services->restartService('httpd','restart');
         }
      } else {
         //* We do not check the apache config after changes (is faster)
         if($apache_chrooted) {
            $app->services->restartServiceDelayed('httpd','restart');
         } else {
            // request a httpd reload when all records have been processed
            $app->services->restartServiceDelayed('httpd','reload');
         }
      }
      // Remove the backup copy of the config file.
      unlink($vhost_file.'~');
      //* Unset action to clean it for next processed vhost.
      $this->action = '';
@@ -938,6 +1009,11 @@
         $command .= ' '.$data["old"]["system_user"];
         exec($command);
         if($apache_chrooted) $this->_exec("chroot ".escapeshellcmd($web_config['website_basedir'])." ".$command);
         //* Remove the awstats configuration file
         if($data["old"]["stats_type"] == 'awstats') {
            $this->awstats_delete($data,$web_config);
         }
      }
   }
@@ -987,9 +1063,11 @@
         /*
          * Get additional informations
         */
         $sitedata = $app->db->queryOneRecord("SELECT document_root, domain FROM web_domain WHERE domain_id = " . $data['new']['parent_domain_id']);
         $sitedata = $app->db->queryOneRecord("SELECT document_root, domain, system_user, system_group FROM web_domain WHERE domain_id = " . $data['new']['parent_domain_id']);
         $documentRoot = $sitedata['document_root'];
         $domain = $sitedata['domain'];
         $user = $sitedata['system_user'];
         $group = $sitedata['system_group'];
         /* Check if this is a chrooted setup */
         if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
@@ -1008,21 +1086,36 @@
         }
         /*
          *  The webdav folder (not the root!) has to be owned by the apache-user
          * The webdav - Root needs the group/user as owner and the apache as read and write
         */
         exec('chown ' . escapeshellcmd($web_config['user']) . ':' . escapeshellcmd($web_config['group']) . ' ' . $documentRoot . '/webdav/' . $data['new']['dir'] . ' -R');
         $this->_exec("chown " . $user . ':' . $group . ' ' . escapeshellcmd($documentRoot . '/webdav/'));
         $this->_exec("chmod 770 " . escapeshellcmd($documentRoot . '/webdav/'));
         /*
          * Next step is to update the password - file
          * The webdav folder (not the webdav-root!) needs the same (not in ONE step, because the
          * pwd-files are owned by root)
         */
         $this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['new']['dir'] . '.htdigest', $data['new']['username'], $data['new']['dir'], $data['new']['password']);
         $this->_exec("chown " . $user . ':' . $group . ' ' . escapeshellcmd($documentRoot . '/webdav/'. $data['new']['dir'] . ' -R'));
         $this->_exec("chmod 770 " . escapeshellcmd($documentRoot . '/webdav/' . $data['new']['dir'] . ' -R'));
         /*
          * if the user is active, we have to write/update the password - file
          * if the user is inactive, we have to inactivate the user by removing the user from the file
         */
         if ($data['new']['active'] == 'y') {
            $this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['new']['dir'] . '.htdigest', $data['new']['username'], $data['new']['dir'], $data['new']['password']);
         }
         else {
            /* empty pwd removes the user! */
            $this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['new']['dir'] . '.htdigest', $data['new']['username'], $data['new']['dir'], '');
         }
         /*
          * Next step, patch the vhost - file
         */
         $vhost_file = escapeshellcmd($web_config["vhost_conf_dir"] . '/' . $domain . '.vhost');
         $this->_patchVhostWebdav($vhost_file, $documentRoot . '/webdav');
         /*
          * Last, restart apache
         */
@@ -1045,7 +1138,7 @@
         /*
          * We dont't want to destroy any (transfer)-Data. So we do NOT delete any dir.
          * So the only thing, we have to do, is to delete the user from the password-file
          */
         */
         $this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['old']['dir'] . '.htdigest', $data['old']['username'], $data['old']['dir'], '');
      }
   }
@@ -1053,17 +1146,17 @@
   /**
    * This function writes the htdigest - files used by webdav and digest
    * more info: see http://riceball.com/d/node/424
    * @author Oliver Vogel
    * @param string $filename The name of the digest-file
    * @param string $username The name of the webdav-user
    * @param string $authname The name of the realm
    * @param string $pwd      The password of the user
    * @param string $pwd      The password-hash of the user
    */
   private function _writeHtDigestFile($filename, $username, $authname, $pwd ) {
   private function _writeHtDigestFile($filename, $username, $authname, $pwdhash ) {
      $changed = false;
      $in = fopen($filename, 'r');
      $output = '';
      /*
       * read line by line and search for the username and authname
      */
@@ -1074,10 +1167,9 @@
            /*
             * found the user. delete or change it?
            */
            if ($pwd != '') {
               $tmp[2] = md5($username . ':' . $authname . ':' .$pwd);
               $output .= $tmp[0] . ':' . $tmp[1] . ':' . $tmp[2] . "\n";
            }
            if ($pwdhash != '') {
               $output .= $tmp[0] . ':' . $tmp[1] . ':' . $pwdhash . "\n";
               }
            $changed = true;
         }
         else {
@@ -1088,7 +1180,7 @@
       * if we didn't change anything, we have to add the new user at the end of the file
      */
      if (!$changed) {
         $output .= $username . ':' . $authname . ':' . md5($username . ':' . $authname . ':' . $pwd) . "\n";
         $output .= $username . ':' . $authname . ':' . $pwdhash . "\n";
      }
      fclose($in);
@@ -1176,6 +1268,40 @@
      file_put_contents($fileName, $output);
   }
   //* Update the awstats configuration file
   private function awstats_update ($data,$web_config) {
      global $app;
      $awstats_conf_dir = $web_config['awstats_conf_dir'];
      if(!@is_file($awstats_conf_dir."/awstats.".$data["new"]["domain"].".conf") || ($data["old"]["domain"] != '' && $data["new"]["domain"] != $data["old"]["domain"])) {
         if ( @is_file($awstats_conf_dir."/awstats.".$data["old"]["domain"].".conf") ) {
            unlink($awstats_conf_dir."/awstats.".$data["old"]["domain"].".conf");
         }
         $content = '';
         $content .= "Include \"".$awstats_conf_dir."/awstats.conf\"\n";
         $content .= "LogFile=\"/var/log/ispconfig/httpd/".$data["new"]["domain"]."/access.log\"\n";
         $content .= "SiteDomain=\"".$data["new"]["domain"]."\"\n";
         $content .= "HostAliases=\"www.".$data["new"]["domain"]."  localhost 127.0.0.1\"\n";
         file_put_contents($awstats_conf_dir.'/awstats.'.$data["new"]["domain"].'.conf',$content);
         $app->log("Created awstats config file: ".$awstats_conf_dir.'/awstats.'.$data["new"]["domain"].'.conf',LOGLEVEL_DEBUG);
      }
   }
   //* Delete the awstats configuration file
   private function awstats_delete ($data,$web_config) {
      global $app;
      $awstats_conf_dir = $web_config['awstats_conf_dir'];
      if ( @is_file($awstats_conf_dir."/awstats.".$data["old"]["domain"].".conf") ) {
         unlink($awstats_conf_dir."/awstats.".$data["old"]["domain"].".conf");
         $app->log("Removed awstats config file: ".$awstats_conf_dir.'/awstats.'.$data["old"]["domain"].'.conf',LOGLEVEL_DEBUG);
      }
   }
   //* Wrapper for exec function for easier debugging
   private function _exec($command) {
@@ -1183,6 +1309,18 @@
      $app->log("exec: ".$command,LOGLEVEL_DEBUG);
      exec($command);
   }
   private function _checkTcp ($host,$port) {
      $fp = @fsockopen ($host, $port, $errno, $errstr, 2);
      if ($fp) {
         fclose($fp);
         return true;
      } else {
         return false;
      }
   }
} // end class