PageRenderTime 46ms CodeModel.GetById 25ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/beta-1.0.2/Server/PHPService.cs

#
C# | 290 lines | 224 code | 58 blank | 8 comment | 23 complexity | edc2a196d947cfd816b722bab0233731 MD5 | raw file
  1//-----------------------------------------------------------------------
  2// <copyright>
  3// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4//
  5// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7// </copyright>
  8//----------------------------------------------------------------------- 
  9
 10using System;
 11using System.Collections;
 12using System.IO;
 13using Microsoft.Web.Administration;
 14using Microsoft.Web.Management.Server;
 15using Web.Management.PHP.Config;
 16
 17namespace Web.Management.PHP
 18{
 19
 20    internal sealed class PHPService : ModuleService
 21    {
 22
 23#if DEBUG
 24        private const bool Passthrough = false;
 25#else
 26        private const bool Passthrough = true;
 27#endif
 28
 29        [ModuleServiceMethod(PassThrough = Passthrough)]
 30        public void AddOrUpdateSettings(object settingsData)
 31        {
 32            EnsureServerConnection();
 33
 34            PHPIniFile file = GetPHPIniFile();
 35
 36            RemoteObjectCollection<PHPIniSetting> settings = new RemoteObjectCollection<PHPIniSetting>();
 37            ((IRemoteObject)settings).SetData(settingsData);
 38
 39            file.AddOrUpdateSettings(settings);
 40            file.Save(file.FileName);
 41        }
 42
 43        [ModuleServiceMethod(PassThrough = Passthrough)]
 44        public string CreatePHPInfo(string siteName)
 45        {
 46            EnsureServerOrSiteConnection();
 47
 48            if (String.IsNullOrEmpty(siteName))
 49            {
 50                throw new InvalidOperationException();
 51            }
 52
 53            Site site = ManagementUnit.ReadOnlyServerManager.Sites[siteName];
 54            if (site == null)
 55            {
 56                throw new InvalidOperationException();
 57            }
 58
 59            Application app = site.Applications["/"];
 60            if (app == null)
 61            {
 62                throw new InvalidOperationException();
 63            }
 64
 65            VirtualDirectory vdir = app.VirtualDirectories["/"];
 66            if (vdir == null)
 67            {
 68                throw new InvalidOperationException();
 69            }
 70
 71            string randomString = Path.GetRandomFileName();
 72            string fileName = randomString.Substring(0, randomString.IndexOf('.')) + ".php";
 73            string filePath = Path.Combine(Environment.ExpandEnvironmentVariables(vdir.PhysicalPath), fileName);
 74
 75            try
 76            {
 77                File.WriteAllText(filePath, @"<?php phpinfo(); ?>");
 78            }
 79            catch (UnauthorizedAccessException)
 80            {
 81                RaiseException("ErrorCannotCreatePHPInfo");
 82            }
 83
 84            return filePath;
 85        }
 86
 87        private void EnsureServerConnection()
 88        {
 89            if (ManagementUnit.Scope != ManagementScope.Server)
 90            {
 91                throw new UnauthorizedAccessException();
 92            }
 93        }
 94
 95        private void EnsureServerOrSiteConnection()
 96        {
 97            if (ManagementUnit.Scope != ManagementScope.Server && ManagementUnit.Scope != ManagementScope.Site)
 98            {
 99                throw new UnauthorizedAccessException();
100            }
101        }
102
103        [ModuleServiceMethod(PassThrough = Passthrough)]
104        public ArrayList GetAllPHPVersions()
105        {
106            EnsureServerOrSiteConnection();
107
108            PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
109            ArrayList versions = phpConfig.GetAllPHPVersions();
110            return versions;
111        }
112
113        [ModuleServiceMethod(PassThrough = Passthrough)]
114        public object GetPHPConfigInfo()
115        {
116            EnsureServerOrSiteConnection();
117
118            PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
119            PHPConfigInfo result = phpConfig.GetPHPConfigInfo();
120
121            return (result != null) ? result.GetData() : null;
122        }
123
124        private PHPIniFile GetPHPIniFile()
125        {
126            PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
127            string phpiniPath = phpConfig.GetPHPIniPath();
128
129            if (String.IsNullOrEmpty(phpiniPath))
130            {
131                RaiseException("ErrorPHPIniNotFound");
132            }
133
134            PHPIniFile file = new PHPIniFile(phpiniPath);
135            file.Parse();
136
137            return file;
138        }
139
140        [ModuleServiceMethod(PassThrough = Passthrough)]
141        public object GetPHPIniPhysicalPath()
142        {
143            if (!ManagementUnit.Context.IsLocalConnection)
144            {
145                return null;
146            }
147
148            PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
149            string phpiniPath = phpConfig.GetPHPIniPath();
150
151            if (String.IsNullOrEmpty(phpiniPath))
152            {
153                RaiseException("ErrorPHPIniNotFound");
154            }
155
156            return phpiniPath;
157        }
158
159        [ModuleServiceMethod(PassThrough = Passthrough)]
160        public object GetPHPIniSettings()
161        {
162            EnsureServerOrSiteConnection();
163
164            PHPIniFile file = GetPHPIniFile();
165
166            return file.GetData();
167        }
168
169        [ModuleServiceMethod(PassThrough = Passthrough)]
170        public ArrayList GetSiteBindings(string siteName)
171        {
172            EnsureServerOrSiteConnection();
173
174            if (String.IsNullOrEmpty(siteName))
175            {
176                throw new InvalidOperationException();
177            }
178
179            Site site = ManagementUnit.ReadOnlyServerManager.Sites[siteName];
180            if (site == null)
181            {
182                throw new InvalidOperationException();
183            }
184
185            ArrayList list = new ArrayList();
186            foreach (Binding binding in site.Bindings)
187            {
188                string protocol = binding.Protocol;
189                if (String.Equals(protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||
190                    String.Equals(protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
191                {
192                    list.Add(new string[] { protocol, binding.BindingInformation });
193                }
194            }
195
196            return list;
197        }
198
199        [ModuleServiceMethod(PassThrough = Passthrough)]
200        public ArrayList GetSites()
201        {
202            EnsureServerConnection();
203
204            SiteCollection siteCollection = ManagementUnit.ReadOnlyServerManager.Sites;
205            ArrayList sites = new ArrayList(siteCollection.Count);
206            foreach (Site site in siteCollection)
207            {
208                sites.Add(site.Name);
209            }
210
211            return sites;
212        }
213
214        [ModuleServiceMethod(PassThrough = Passthrough)]
215        public void RegisterPHPWithIIS(string phpExePath)
216        {
217            EnsureServerConnection();
218
219            try
220            {
221                PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
222                phpConfig.RegisterPHPWithIIS(phpExePath);
223            }
224            catch (ArgumentException)
225            {
226                RaiseException("ErrorInvalidPHPExecutablePath");
227            }
228            catch (FileNotFoundException)
229            {
230                RaiseException("ErrorNoPHPFilesInDirectory");
231            }
232            catch (DirectoryNotFoundException)
233            {
234                RaiseException("ErrorNoExtDirectory");
235            }
236        }
237
238        [ModuleServiceMethod(PassThrough = Passthrough)]
239        public void RemovePHPInfo(string filePath)
240        {
241            EnsureServerOrSiteConnection();
242            
243            if (!File.Exists(filePath)) {
244                return;
245            }
246
247            File.Delete(filePath);
248        }
249
250        [ModuleServiceMethod(PassThrough = Passthrough)]
251        public void RemovePHPIniSetting(object settingData)
252        {
253            EnsureServerConnection();
254
255            PHPIniFile file = GetPHPIniFile();
256
257            PHPIniSetting setting = new PHPIniSetting();
258            setting.SetData(settingData);
259
260            if (file.Remove(setting))
261            {
262                file.Save(file.FileName);
263            }
264        }
265
266        [ModuleServiceMethod(PassThrough = Passthrough)]
267        public void SelectPHPVersion(string name)
268        {
269            EnsureServerOrSiteConnection();
270            
271            PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
272            phpConfig.SelectPHPHandler(name);
273        }
274
275        [ModuleServiceMethod(PassThrough = Passthrough)]
276        public void UpdatePHPExtensions(object extensionsData)
277        {
278            EnsureServerConnection();
279
280            PHPIniFile file = GetPHPIniFile();
281
282            RemoteObjectCollection<PHPIniExtension> extensions = new RemoteObjectCollection<PHPIniExtension>();
283            ((IRemoteObject)extensions).SetData(extensionsData);
284
285            file.UpdateExtensions(extensions);
286            file.Save(file.FileName);
287        }
288
289    }
290}