PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 290 lines | 224 code | 58 blank | 8 comment | 23 complexity | edc2a196d947cfd816b722bab0233731 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using Microsoft.Web.Administration;
  13. using Microsoft.Web.Management.Server;
  14. using Web.Management.PHP.Config;
  15. namespace Web.Management.PHP
  16. {
  17. internal sealed class PHPService : ModuleService
  18. {
  19. #if DEBUG
  20. private const bool Passthrough = false;
  21. #else
  22. private const bool Passthrough = true;
  23. #endif
  24. [ModuleServiceMethod(PassThrough = Passthrough)]
  25. public void AddOrUpdateSettings(object settingsData)
  26. {
  27. EnsureServerConnection();
  28. PHPIniFile file = GetPHPIniFile();
  29. RemoteObjectCollection<PHPIniSetting> settings = new RemoteObjectCollection<PHPIniSetting>();
  30. ((IRemoteObject)settings).SetData(settingsData);
  31. file.AddOrUpdateSettings(settings);
  32. file.Save(file.FileName);
  33. }
  34. [ModuleServiceMethod(PassThrough = Passthrough)]
  35. public string CreatePHPInfo(string siteName)
  36. {
  37. EnsureServerOrSiteConnection();
  38. if (String.IsNullOrEmpty(siteName))
  39. {
  40. throw new InvalidOperationException();
  41. }
  42. Site site = ManagementUnit.ReadOnlyServerManager.Sites[siteName];
  43. if (site == null)
  44. {
  45. throw new InvalidOperationException();
  46. }
  47. Application app = site.Applications["/"];
  48. if (app == null)
  49. {
  50. throw new InvalidOperationException();
  51. }
  52. VirtualDirectory vdir = app.VirtualDirectories["/"];
  53. if (vdir == null)
  54. {
  55. throw new InvalidOperationException();
  56. }
  57. string randomString = Path.GetRandomFileName();
  58. string fileName = randomString.Substring(0, randomString.IndexOf('.')) + ".php";
  59. string filePath = Path.Combine(Environment.ExpandEnvironmentVariables(vdir.PhysicalPath), fileName);
  60. try
  61. {
  62. File.WriteAllText(filePath, @"<?php phpinfo(); ?>");
  63. }
  64. catch (UnauthorizedAccessException)
  65. {
  66. RaiseException("ErrorCannotCreatePHPInfo");
  67. }
  68. return filePath;
  69. }
  70. private void EnsureServerConnection()
  71. {
  72. if (ManagementUnit.Scope != ManagementScope.Server)
  73. {
  74. throw new UnauthorizedAccessException();
  75. }
  76. }
  77. private void EnsureServerOrSiteConnection()
  78. {
  79. if (ManagementUnit.Scope != ManagementScope.Server && ManagementUnit.Scope != ManagementScope.Site)
  80. {
  81. throw new UnauthorizedAccessException();
  82. }
  83. }
  84. [ModuleServiceMethod(PassThrough = Passthrough)]
  85. public ArrayList GetAllPHPVersions()
  86. {
  87. EnsureServerOrSiteConnection();
  88. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  89. ArrayList versions = phpConfig.GetAllPHPVersions();
  90. return versions;
  91. }
  92. [ModuleServiceMethod(PassThrough = Passthrough)]
  93. public object GetPHPConfigInfo()
  94. {
  95. EnsureServerOrSiteConnection();
  96. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  97. PHPConfigInfo result = phpConfig.GetPHPConfigInfo();
  98. return (result != null) ? result.GetData() : null;
  99. }
  100. private PHPIniFile GetPHPIniFile()
  101. {
  102. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  103. string phpiniPath = phpConfig.GetPHPIniPath();
  104. if (String.IsNullOrEmpty(phpiniPath))
  105. {
  106. RaiseException("ErrorPHPIniNotFound");
  107. }
  108. PHPIniFile file = new PHPIniFile(phpiniPath);
  109. file.Parse();
  110. return file;
  111. }
  112. [ModuleServiceMethod(PassThrough = Passthrough)]
  113. public object GetPHPIniPhysicalPath()
  114. {
  115. if (!ManagementUnit.Context.IsLocalConnection)
  116. {
  117. return null;
  118. }
  119. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  120. string phpiniPath = phpConfig.GetPHPIniPath();
  121. if (String.IsNullOrEmpty(phpiniPath))
  122. {
  123. RaiseException("ErrorPHPIniNotFound");
  124. }
  125. return phpiniPath;
  126. }
  127. [ModuleServiceMethod(PassThrough = Passthrough)]
  128. public object GetPHPIniSettings()
  129. {
  130. EnsureServerOrSiteConnection();
  131. PHPIniFile file = GetPHPIniFile();
  132. return file.GetData();
  133. }
  134. [ModuleServiceMethod(PassThrough = Passthrough)]
  135. public ArrayList GetSiteBindings(string siteName)
  136. {
  137. EnsureServerOrSiteConnection();
  138. if (String.IsNullOrEmpty(siteName))
  139. {
  140. throw new InvalidOperationException();
  141. }
  142. Site site = ManagementUnit.ReadOnlyServerManager.Sites[siteName];
  143. if (site == null)
  144. {
  145. throw new InvalidOperationException();
  146. }
  147. ArrayList list = new ArrayList();
  148. foreach (Binding binding in site.Bindings)
  149. {
  150. string protocol = binding.Protocol;
  151. if (String.Equals(protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||
  152. String.Equals(protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
  153. {
  154. list.Add(new string[] { protocol, binding.BindingInformation });
  155. }
  156. }
  157. return list;
  158. }
  159. [ModuleServiceMethod(PassThrough = Passthrough)]
  160. public ArrayList GetSites()
  161. {
  162. EnsureServerConnection();
  163. SiteCollection siteCollection = ManagementUnit.ReadOnlyServerManager.Sites;
  164. ArrayList sites = new ArrayList(siteCollection.Count);
  165. foreach (Site site in siteCollection)
  166. {
  167. sites.Add(site.Name);
  168. }
  169. return sites;
  170. }
  171. [ModuleServiceMethod(PassThrough = Passthrough)]
  172. public void RegisterPHPWithIIS(string phpExePath)
  173. {
  174. EnsureServerConnection();
  175. try
  176. {
  177. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  178. phpConfig.RegisterPHPWithIIS(phpExePath);
  179. }
  180. catch (ArgumentException)
  181. {
  182. RaiseException("ErrorInvalidPHPExecutablePath");
  183. }
  184. catch (FileNotFoundException)
  185. {
  186. RaiseException("ErrorNoPHPFilesInDirectory");
  187. }
  188. catch (DirectoryNotFoundException)
  189. {
  190. RaiseException("ErrorNoExtDirectory");
  191. }
  192. }
  193. [ModuleServiceMethod(PassThrough = Passthrough)]
  194. public void RemovePHPInfo(string filePath)
  195. {
  196. EnsureServerOrSiteConnection();
  197. if (!File.Exists(filePath)) {
  198. return;
  199. }
  200. File.Delete(filePath);
  201. }
  202. [ModuleServiceMethod(PassThrough = Passthrough)]
  203. public void RemovePHPIniSetting(object settingData)
  204. {
  205. EnsureServerConnection();
  206. PHPIniFile file = GetPHPIniFile();
  207. PHPIniSetting setting = new PHPIniSetting();
  208. setting.SetData(settingData);
  209. if (file.Remove(setting))
  210. {
  211. file.Save(file.FileName);
  212. }
  213. }
  214. [ModuleServiceMethod(PassThrough = Passthrough)]
  215. public void SelectPHPVersion(string name)
  216. {
  217. EnsureServerOrSiteConnection();
  218. PHPConfigHelper phpConfig = new PHPConfigHelper(ManagementUnit);
  219. phpConfig.SelectPHPHandler(name);
  220. }
  221. [ModuleServiceMethod(PassThrough = Passthrough)]
  222. public void UpdatePHPExtensions(object extensionsData)
  223. {
  224. EnsureServerConnection();
  225. PHPIniFile file = GetPHPIniFile();
  226. RemoteObjectCollection<PHPIniExtension> extensions = new RemoteObjectCollection<PHPIniExtension>();
  227. ((IRemoteObject)extensions).SetData(extensionsData);
  228. file.UpdateExtensions(extensions);
  229. file.Save(file.FileName);
  230. }
  231. }
  232. }