public void BuildExistingSiteList()
        {
            listViewSites.Items.Clear();
            _XAMPPInstallDir = ConfigManager.GetXAMPPInstallDir();
            _XAMPPWebRootPath = Path.Combine(_XAMPPInstallDir, "htdocs");
            _XAMPPApacheVhostsConfigFile = Path.Combine(_XAMPPInstallDir, "apache\\conf\\extra\\httpd-vhosts.conf");

            if (!CheckSettings())
            {
                return;
            }

            _vhostsManager = new ApacheVhostsConfigManager(_XAMPPApacheVhostsConfigFile, _XAMPPWebRootPath);

            listViewSites.Items.Clear();
            List<VhostsEntry> vhostEntries = _vhostsManager.GetVHostEntries();
            List<HostsEntry> hostsEntries = _hostsManager.GetHostsEntries();
            List<DirectoryInfo> sites = new DirectoryInfo(_XAMPPWebRootPath).GetDirectories("*", SearchOption.TopDirectoryOnly).ToList();

            foreach (VhostsEntry vHostEntry in vhostEntries)
            {
                if (hostsEntries.Count(p => p.DomainName == vHostEntry.ServerName) > 0 ||
                    sites.Count(p => p.Name == vHostEntry.ServerName) > 0)
                {
                    ListViewItem lvi = new ListViewItem(vHostEntry.ServerName);
                    lvi.SubItems.Add("OK");
                    listViewSites.Items.Add(lvi);
                }
            }
        }
        public void BuildExistingSiteList()
        {
            listViewSites.Items.Clear();
            _XAMPPInstallDir = ConfigManager.GetXAMPPInstallDir();
            _XAMPPWebRootPath = Path.Combine(_XAMPPInstallDir, "htdocs");
            _XAMPPApacheVhostsConfigFile = Path.Combine(_XAMPPInstallDir, "apache\\conf\\extra\\httpd-vhosts.conf");

            if (!CheckSettings())
            {
                return;
            }

            _vhostsManager = new ApacheVhostsConfigManager(_XAMPPApacheVhostsConfigFile, _XAMPPWebRootPath);

            listViewSites.Items.Clear();
            List<VhostsEntry> vhostEntries = _vhostsManager.GetVHostEntries();
            List<HostsEntry> hostsEntries = _hostsManager.GetHostsEntries();
            List<DirectoryInfo> sites = new DirectoryInfo(_XAMPPWebRootPath).GetDirectories("*", SearchOption.TopDirectoryOnly).ToList();

            foreach (VhostsEntry vHostEntry in vhostEntries)
            {
                if (hostsEntries.Count(p => p.DomainName == vHostEntry.ServerName) > 0 ||
                    sites.Count(p => p.Name == vHostEntry.ServerName) > 0)
                {
                    ListViewItem lvi = new ListViewItem(vHostEntry.ServerName);
                    lvi.SubItems.Add("OK");
                    listViewSites.Items.Add(lvi);
                }
            }
        }
Beispiel #3
0
        private void CreateSite()
        {
            string siteName = textBoxNewSiteDomainName.Text;

            if (siteName == "")
            {
                MessageBox.Show(
                    this,
                    "Site domain name can't be empty",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Stop);
                return;
            }

            if (_vhostsManager.GetVHostEntries().Count(p => p.ServerName == siteName) > 0)
            {
                MessageBox.Show(
                    this,
                    "Site with that domain name already exists",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Stop);
                return;
            }

            try
            {
                string sitePath = Path.Combine(_XAMPPSiteRoot, siteName);

                if (Directory.Exists(sitePath))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(sitePath);

                    if (dirInfo.GetFiles().Length > 0 || dirInfo.GetDirectories().Length > 0)
                    {
                        DialogResult res = MessageBox.Show(
                            this,
                            string.Format("Directory for {0} site is not empty. Do you want to truncate all its content?", siteName),
                            "Confirmation needed",
                            MessageBoxButtons.YesNoCancel,
                            MessageBoxIcon.Question);

                        if (res == DialogResult.Yes)
                        {
                            Directory.Delete(sitePath, true);
                        }
                        else if (res == DialogResult.Cancel)
                        {
                            return;
                        }
                    }
                }


                _vhostsManager.CreateEntry(siteName);
                _hostsManager.CreateEntry(siteName);
                Directory.CreateDirectory(Path.Combine(_XAMPPSiteRoot, siteName));
                DialogResult dRes = MessageBox.Show(
                    this,
                    string.Format("Site successfuly created.\r\nApache need to be restarted so new settings to take effect.\r\nPlace your web application files to {0} folder and site will be accessible throu {1} url from the web browser.\r\nDo you want to restart Apache now?",
                                  Path.Combine(_XAMPPSiteRoot, siteName), siteName),
                    "Success", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information);

                if (dRes == DialogResult.Yes)
                {
                    try
                    {
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        startInfo.FileName = Path.Combine(_XAMPPSiteInstallDir, ConfigManager.GetXAMPPApacheStopFilePath());
                        startInfo.RedirectStandardOutput = true;
                        startInfo.RedirectStandardError  = true;
                        startInfo.UseShellExecute        = false;
                        startInfo.CreateNoWindow         = true;

                        Process processApache = new Process();
                        processApache.StartInfo           = startInfo;
                        processApache.EnableRaisingEvents = true;
                        processApache.Start();
                        processApache.WaitForExit();

                        startInfo.FileName = Path.Combine(_XAMPPSiteInstallDir, ConfigManager.GetXAMPPApacheStartFilePath());
                        processApache.Start();
                        MessageBox.Show(this,
                                        "Apache successfuly restarted",
                                        "Information",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this,
                                        "Failed to restart Apache\r\nDetails:\r\n" + ex.Message,
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,
                                "Failed to register new site\r\nDetails:\r\n" + ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }

            this.Close();
        }