private void CopyInheritedHandlers()
        {
            if (_configurationWrapper.IsServerConfigurationPath())
            {
                return;
            }

            var list = new HandlerElement[_handlersCollection.Count];
            ((ICollection)_handlersCollection).CopyTo(list, 0);

            _handlersCollection.Clear();

            foreach (var handler in list)
            {
                _handlersCollection.AddCopy(handler);
            }
        }
        private void Initialize()
        {
            if (!IsFastCgiInstalled())
            {
                // If FastCGI is not installed on IIS then bail out as there is no point to continue
                _registrationType = PHPRegistrationType.NoneNoFastCgi;
                return;
            }

            // Get the handlers collection
            var handlersSection = _configurationWrapper.GetHandlersSection();
            _handlersCollection = handlersSection.Handlers;

            // Get the Default document collection
            var defaultDocumentSection = _configurationWrapper.GetDefaultDocumentSection();
            _defaultDocumentCollection = defaultDocumentSection.Files;

            // Get the FastCgi application collection
            var appHostConfig = _configurationWrapper.GetAppHostConfiguration();
            var fastCgiSection = (FastCgiSection)appHostConfig.GetSection("system.webServer/fastCgi", typeof(FastCgiSection));
            _fastCgiApplicationCollection = fastCgiSection.Applications;

            // Assume by default that PHP is not registered
            _registrationType = PHPRegistrationType.None;

            // Find the currently active PHP handler and FastCGI application
            var handler = _handlersCollection.GetActiveHandler("*.php");
            if (handler != null)
            {
                if (String.Equals(handler.Modules, "FastCgiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.FastCgi;
                }
                else if (String.Equals(handler.Modules, "CgiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.Cgi;
                }
                else if (String.Equals(handler.Modules, "IsapiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.Isapi;
                }

                if (_registrationType == PHPRegistrationType.FastCgi)
                {
                    ApplicationElement fastCgiApplication = _fastCgiApplicationCollection.GetApplication(handler.Executable, handler.Arguments);
                    if (fastCgiApplication != null)
                    {
                        _currentPhpHandler = handler;
                        _currentFastCgiApplication = fastCgiApplication;
                        _phpIniFilePath = GetPHPIniFilePath();
                        if (String.IsNullOrEmpty(_phpIniFilePath))
                        {
                            throw new FileNotFoundException(String.Format(Resources.CannotFindPhpIniForExecutableError, handler.Executable));
                        }
                        _phpDirectory = GetPHPDirectory();
                    }
                    else
                    {
                        _registrationType = PHPRegistrationType.None;
                    }
                }
            }
        }
        private void Initialize()
        {
            // Get the handlers collection
            ManagementConfiguration config = _managementUnit.Configuration;
            HandlersSection handlersSection = (HandlersSection)config.GetSection("system.webServer/handlers", typeof(HandlersSection));
            _handlersCollection = handlersSection.Handlers;

            // Get the Default document collection
            DefaultDocumentSection defaultDocumentSection = (DefaultDocumentSection)config.GetSection("system.webServer/defaultDocument", typeof(DefaultDocumentSection));
            _defaultDocumentCollection = defaultDocumentSection.Files;

            // Get the FastCgi application collection
            Configuration appHostConfig = _managementUnit.ServerManager.GetApplicationHostConfiguration();
            FastCgiSection fastCgiSection = (FastCgiSection)appHostConfig.GetSection("system.webServer/fastCgi", typeof(FastCgiSection));
            _fastCgiApplicationCollection = fastCgiSection.Applications;

            // Assume by default that PHP is not registered
            _registrationType = PHPRegistrationType.None;

            // Find the currently active PHP handler and FastCGI application
            HandlerElement handler = _handlersCollection.GetActiveHandler("*.php");
            if (handler != null)
            {
                if (String.Equals(handler.Modules, "FastCgiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.FastCgi;
                }
                else if (String.Equals(handler.Modules, "CgiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.Cgi;
                }
                else if (String.Equals(handler.Modules, "IsapiModule", StringComparison.OrdinalIgnoreCase))
                {
                    _registrationType = PHPRegistrationType.Isapi;
                }

                if (_registrationType == PHPRegistrationType.FastCgi)
                {
                    ApplicationElement fastCgiApplication = _fastCgiApplicationCollection.GetApplication(handler.Executable, handler.Arguments);
                    if (fastCgiApplication != null)
                    {
                        _currentPHPHandler = handler;
                        _currentFastCgiApplication = fastCgiApplication;
                        _phpIniFilePath = GetPHPIniFilePath();
                        _phpDirectory = GetPHPDirectory();
                    }
                    else
                    {
                        _registrationType = PHPRegistrationType.None;
                    }
                }
            }
        }
        private void CopyInheritedHandlers()
        {
            if (_managementUnit.ConfigurationPath.PathType == ConfigurationPathType.Server)
            {
                return;
            }

            HandlerElement[] list = new HandlerElement[_handlersCollection.Count];
            ((ICollection)_handlersCollection).CopyTo(list, 0);

            _handlersCollection.Clear();

            foreach (HandlerElement handler in list)
            {
                _handlersCollection.AddCopy(handler);
            }
        }
        public void SelectPHPHandler(string name)
        {
            HandlerElement handler = _handlersCollection[name];
            if (handler != null && _handlersCollection.IndexOf(handler) > 0)
            {
                CopyInheritedHandlers();
                MoveHandlerOnTop(_handlersCollection, name);

                _managementUnit.Update();

                // Update the references to current php handler and application
                ApplicationElement fastCgiApplication = _fastCgiApplicationCollection.GetApplication(handler.ScriptProcessor, "");
                _currentFastCgiApplication = fastCgiApplication;
                _currentPHPHandler = handler;
            }
        }
        public void RegisterPHPWithIIS(string path)
        {
            string phpexePath = Environment.ExpandEnvironmentVariables(path);
            bool iisUpdateHappened = false;
            
            if (!String.Equals(Path.GetFileName(phpexePath), "php-cgi.exe", StringComparison.OrdinalIgnoreCase) &&
                !String.Equals(Path.GetFileName(phpexePath), "php.exe", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("The provided php executable path is invalid", phpexePath);
            }

            // Check for existence of php executable in the specified directory
            if (!File.Exists(phpexePath))
            {
                throw new FileNotFoundException("php-cgi.exe and php.exe do not exist");
            }

            // Check for existense of php extensions directory
            string phpDir = Path.GetDirectoryName(phpexePath);
            string extDir = Path.Combine(phpDir, "ext");
            if (!Directory.Exists(extDir))
            {
                throw new DirectoryNotFoundException("ext directory does not exist in " + phpDir);
            }
            
            // Check for existence of php.ini file. If it does not exist then copy php.ini-recommended
            // or php.ini-production to it
            string phpiniPath = Path.Combine(phpDir, "php.ini");
            if (!File.Exists(phpiniPath))
            {
                string phpiniRecommendedPath = Path.Combine(phpDir, "php.ini-recommended");
                string phpiniProductionPath = Path.Combine(phpDir, "php.ini-production");
                if (File.Exists(phpiniRecommendedPath))
                {
                    File.Copy(phpiniRecommendedPath, phpiniPath);
                }
                else if (File.Exists(phpiniProductionPath))
                {
                    File.Copy(phpiniProductionPath, phpiniPath);
                }
                else
                {
                    throw new FileNotFoundException("php.ini and php.ini recommended do not exist in " + phpDir);
                }
            }

            ApplicationElement fastCgiApplication = _fastCgiApplicationCollection.GetApplication(phpexePath, "");
            
            // Create a FastCGI application if it does not exist
            if (fastCgiApplication == null)
            {
                fastCgiApplication = _fastCgiApplicationCollection.CreateElement();
                fastCgiApplication.FullPath = phpexePath;
                fastCgiApplication.MonitorChangesTo = phpiniPath;
                fastCgiApplication.InstanceMaxRequests = 10000;
                fastCgiApplication.ActivityTimeout = 300;
                fastCgiApplication.RequestTimeout = 300;

                fastCgiApplication.EnvironmentVariables.Add("PHPRC", phpDir);
                fastCgiApplication.EnvironmentVariables.Add("PHP_FCGI_MAX_REQUESTS", "10000");

                _fastCgiApplicationCollection.Add(fastCgiApplication);
                iisUpdateHappened = true;
            }

            // Check if file mapping with this executable already exists
            HandlerElement handlerElement = _handlersCollection.GetHandler("*.php", phpexePath);
            
            if (handlerElement == null)
            {
                // Create a PHP file handler if it does not exist
                handlerElement = _handlersCollection.CreateElement();
                handlerElement.Name = GenerateHandlerName(_handlersCollection, GetPHPExecutableVersion(phpexePath));
                handlerElement.Modules = "FastCgiModule";
                handlerElement.RequireAccess = RequireAccess.Script;
                handlerElement.Verb = "*";
                handlerElement.Path = "*.php";
                handlerElement.ScriptProcessor = phpexePath;
                handlerElement.ResourceType = ResourceType.Either;
                _handlersCollection.AddAt(0, handlerElement);
                iisUpdateHappened = true;
            }
            else if (_handlersCollection.IndexOf(handlerElement) > 0)
            {
                // Move the existing PHP file handler mapping on top
                CopyInheritedHandlers();
                MoveHandlerOnTop(_handlersCollection, handlerElement.Name);
                iisUpdateHappened = true;
            }

            if (iisUpdateHappened)
            {
                _managementUnit.Update();
            }

            // Update the references to current php handler and application
            _currentPHPHandler = handlerElement;
            _currentFastCgiApplication = fastCgiApplication;

            // Make the recommended changes to php.ini file
            ApplyRecommendedPHPIniSettings();
        }
        private void Initialize()
        {
            // Get the handlers collection
            ManagementConfiguration config = _managementUnit.Configuration;
            HandlersSection handlersSection = (HandlersSection)config.GetSection("system.webServer/handlers", typeof(HandlersSection));
            _handlersCollection = handlersSection.Handlers;

            // Get the FastCgi application collection
            Configuration appHostConfig = _managementUnit.ServerManager.GetApplicationHostConfiguration();
            FastCgiSection fastCgiSection = (FastCgiSection)appHostConfig.GetSection("system.webServer/fastCgi", typeof(FastCgiSection));
            _fastCgiApplicationCollection = fastCgiSection.Applications;

            // Find the currently active PHP handler and FastCGI application
            HandlerElement handler = _handlersCollection.GetActiveHandler("*.php");
            if (handler != null)
            {
                string executable = handler.ScriptProcessor;
                
                ApplicationElement fastCgiApplication = _fastCgiApplicationCollection.GetApplication(executable, "");
                if (fastCgiApplication != null)
                {
                    _currentPHPHandler = handler;
                    _currentFastCgiApplication = fastCgiApplication;
                }
            }
        }