ftimme
2012-03-30 2655f9315464236f2e41d2b6467d9cf97cb2b34b
server/plugins-available/nginx_plugin.inc.php
@@ -911,7 +911,7 @@
      if(file_exists($vhost_file)) copy($vhost_file,$vhost_file.'~');
      
      //* Write vhost file
      file_put_contents($vhost_file,$tpl->grab());
      file_put_contents($vhost_file,$this->nginx_merge_locations($tpl->grab()));
      $app->log('Writing the vhost file: '.$vhost_file,LOGLEVEL_DEBUG);
      unset($tpl);
@@ -1407,18 +1407,25 @@
         $default_php_fpm = true;
      }
      
      $app->uses("getconf");
      $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
      if($data['new']['php'] == 'no'){
         if(@is_file($pool_dir.$pool_name.'.conf')){
            unlink($pool_dir.$pool_name.'.conf');
            //$reload = true;
         }
         if($data['old']['php'] != 'no'){
            if(!$default_php_fpm){
               $app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
            } else {
               $app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
            }
         }
         //if($reload == true) $app->services->restartService('php-fpm','reload');
         return;
      }
      $app->uses("getconf");
      $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
      $app->load('tpl');
      $tpl = new tpl();
      $tpl->newTemplate('php_fpm_pool.conf.master');
@@ -1503,7 +1510,7 @@
         if ( @is_file($default_pool_dir.$pool_name.'.conf') ) {
               unlink($default_pool_dir.$pool_name.'.conf');
               $app->log('Removed PHP-FPM config file: '.$default_pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
               exec($conf['init_scripts'] . '/' . $web_config['php_fpm_init_script'] . ' reload');
               $app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
         }
      }
      $php_versions = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".$conf["server_id"]);
@@ -1514,15 +1521,17 @@
               if ( @is_file($php_version['php_fpm_pool_dir'].$pool_name.'.conf') ) {
                  unlink($php_version['php_fpm_pool_dir'].$pool_name.'.conf');
                  $app->log('Removed PHP-FPM config file: '.$php_version['php_fpm_pool_dir'].$pool_name.'.conf',LOGLEVEL_DEBUG);
                  exec($php_version['php_fpm_init_script'] . ' reload');
                  $app->services->restartService('php-fpm','reload:'.$php_version['php_fpm_init_script']);
               }
            }
         }
      }
      // Reload current PHP-FPM after all others
      sleep(1);
      if(!$default_php_fpm){
         sleep(1);
         exec($custom_php_fpm_init_script . ' reload');
         $app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
      } else {
         $app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
      }
      
      //$reload = true;
@@ -1565,7 +1574,7 @@
         if ( @is_file($default_pool_dir.$pool_name.'.conf') ) {
               unlink($default_pool_dir.$pool_name.'.conf');
               $app->log('Removed PHP-FPM config file: '.$default_pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
               exec($conf['init_scripts'] . '/' . $web_config['php_fpm_init_script'] . ' reload');
               $app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
         }
      }   
      $php_versions = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".$data['old']['server_id']);
@@ -1576,19 +1585,89 @@
               if ( @is_file($php_version['php_fpm_pool_dir'].$pool_name.'.conf') ) {
                  unlink($php_version['php_fpm_pool_dir'].$pool_name.'.conf');
                  $app->log('Removed PHP-FPM config file: '.$php_version['php_fpm_pool_dir'].$pool_name.'.conf',LOGLEVEL_DEBUG);
                  exec($php_version['php_fpm_init_script'] . ' reload');
                  $app->services->restartService('php-fpm','reload:'.$php_version['php_fpm_init_script']);
               }
            }
         }
      }
      
      // Reload current PHP-FPM after all others
      sleep(1);
      if(!$default_php_fpm){
         sleep(1);
         exec($custom_php_fpm_init_script . ' reload');
         $app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
      } else {
         $app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
      }
   }
   
   private function nginx_merge_locations($vhost_conf){
      $lines = explode("\n", $vhost_conf);
      if(is_array($lines) && !empty($lines)){
         $locations = array();
         $islocation = false;
         $linecount = sizeof($lines);
         for($i=0;$i<$linecount;$i++){
            $l = trim($lines[$i]);
            if(substr($l, 0, 8) == 'location' && !$islocation){
               $islocation = true;
               $level = 0;
               // Remove unnecessary whitespace
               $l = preg_replace('/\s\s+/', ' ', $l);
               $loc_parts = explode(' ', $l);
               // see http://wiki.nginx.org/HttpCoreModule#location
               if($loc_parts[1] == '=' || $loc_parts[1] == '~' || $loc_parts[1] == '~*' || $loc_parts[1] == '^~'){
                  $location = $loc_parts[1].' '.$loc_parts[2];
               } else {
                  $location = $loc_parts[1];
               }
               unset($loc_parts);
               if(!isset($locations[$location]['open_tag'])) $locations[$location]['open_tag'] = '        location '.$location.' {';
               if(!isset($locations[$location]['location'])) $locations[$location]['location'] = '';
               if(!isset($locations[$location]['end_tag'])) $locations[$location]['end_tag'] = '        }';
               if(!isset($locations[$location]['start_line'])) $locations[$location]['start_line'] = $i;
               unset($lines[$i]);
            } else {
               if($islocation){
                  if(strpos($l, '{') !== false){
                     $level += 1;
                  }
                  if(strpos($l, '}') !== false && $level > 0){
                     $level -= 1;
                     $locations[$location]['location'] .= $lines[$i]."\n";
                  } elseif(strpos($l, '}') !== false && $level == 0){
                     $islocation = false;
                  } else {
                     $locations[$location]['location'] .= $lines[$i]."\n";
                  }
                  unset($lines[$i]);
               }
            }
         }
         if(is_array($locations) && !empty($locations)){
            foreach($locations as $key => $val){
               $new_location = $val['open_tag']."\n".$val['location'].$val['end_tag'];
               $lines[$val['start_line']] = $new_location;
            }
         }
         ksort($lines);
         $vhost_conf = implode("\n", $lines);
      }
      return $vhost_conf;
   }
   function client_delete($event_name,$data) {
      global $app, $conf;