PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/stable-1.1.0/Client/PHPPage.cs

#
C# | 588 lines | 480 code | 79 blank | 29 comment | 71 complexity | 1ff4a3ca7e03c9d1ecbcdca520f6f2f0 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.ComponentModel;
  11. using System.Drawing;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Windows.Forms;
  15. using Microsoft.Web.Management.Client;
  16. using Microsoft.Web.Management.Client.Win32;
  17. using Web.Management.PHP.Config;
  18. namespace Web.Management.PHP
  19. {
  20. [ModulePageIdentifier(Globals.PHPPageIdentifier)]
  21. internal sealed class PHPPage : ModulePage
  22. {
  23. private const int IndexRegisterPHPTask = 0;
  24. private const int IndexChangeVersionTask = 1;
  25. private const int IndexCheckPHPInfoTask = 2;
  26. private const int IndexErrorReportingTask = 0;
  27. private const int IndexLimitsTask = 1;
  28. private const int IndexAllSettingsTask = 2;
  29. private const int IndexAllExtensionsTask = 0;
  30. private const int IndexAddExtensionTask = 1;
  31. // Summary labels
  32. private Label _enabledExtLabel;
  33. private Label _installedExtLabel;
  34. private Label _errorLogNameLabel;
  35. private LinkLabel _errorLogValueLabel;
  36. private LinkLabel _configPathValueLabel;
  37. private Label _configPathNameLabel;
  38. private Label _executableValueLabel;
  39. private Label _executableNameLabel;
  40. private Label _versionValueLabel;
  41. private Label _versionNameLabel;
  42. private PHPPageItemControl _phpExtensionItem;
  43. private PHPPageItemControl _phpSettingsItem;
  44. private PHPPageItemControl _phpSetupItem;
  45. private new PHPModule Module
  46. {
  47. get
  48. {
  49. return (PHPModule)base.Module;
  50. }
  51. }
  52. protected override bool ShowTaskList
  53. {
  54. get
  55. {
  56. return false;
  57. }
  58. }
  59. private void GetSettings()
  60. {
  61. StartAsyncTask(Resources.AllSettingsPageGettingSettings, OnGetSettings, OnGetSettingsCompleted);
  62. }
  63. private string GetSiteUrlAndName(out string siteName)
  64. {
  65. using (Setup.SelectSiteAndUrlDialog dlg = new Setup.SelectSiteAndUrlDialog(this.Module, this.Connection))
  66. {
  67. if (ShowDialog(dlg) == DialogResult.OK)
  68. {
  69. siteName = dlg.SiteName;
  70. return dlg.SelectedUrl;
  71. }
  72. }
  73. siteName = string.Empty;
  74. return null;
  75. }
  76. private void InitializeUI()
  77. {
  78. SuspendLayout();
  79. IManagementUIService uiService = (IManagementUIService)GetService(typeof(IManagementUIService));
  80. Font titleFont = (Font)uiService.Styles["PageHeaderTitleFont"];
  81. Padding = new Padding(0, 12, 0, 0);
  82. //
  83. // All page item labels
  84. //
  85. _versionNameLabel = new Label();
  86. _versionNameLabel.Text = Resources.PHPPageVersion;
  87. _versionValueLabel = new Label();
  88. _executableNameLabel = new Label();
  89. _executableNameLabel.Text = Resources.PHPPageExecutable;
  90. _executableValueLabel = new Label();
  91. _configPathNameLabel = new Label();
  92. _configPathNameLabel.Text = Resources.PHPPageConfigurationFile;
  93. _configPathValueLabel = new LinkLabel();
  94. _configPathValueLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(OnPathLinkLabelLinkClicked);
  95. _errorLogNameLabel = new Label();
  96. _errorLogNameLabel.Text = Resources.PHPPageErrorLog;
  97. _errorLogValueLabel = new LinkLabel();
  98. _errorLogValueLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(OnPathLinkLabelLinkClicked);
  99. _enabledExtLabel = new Label();
  100. _installedExtLabel = new Label();
  101. //
  102. // PHPSetup
  103. //
  104. _phpSetupItem = new PHPPageItemControl();
  105. _phpSetupItem.RightToLeftLayout = this.RightToLeftLayout;
  106. _phpSetupItem.RightToLeft = this.RightToLeft;
  107. _phpSetupItem.TitleClick += new LinkLabelLinkClickedEventHandler(OnPHPSetupItemTitleClick);
  108. _phpSetupItem.Title = Resources.PHPSetupItemTitle;
  109. _phpSetupItem.TitleFont = titleFont;
  110. _phpSetupItem.Image = Resources.PHPSetup32;
  111. _phpSetupItem.AddInfoRow(_versionNameLabel, _versionValueLabel);
  112. _phpSetupItem.AddInfoRow(_executableNameLabel, _executableValueLabel);
  113. _phpSetupItem.AddTask(OnPHPSetupItemClick,
  114. Resources.PHPSetupItemRegisterPHPTask,
  115. Resources.PHPSetupItemChangeVersionTask,
  116. Resources.PHPSetupItemCheckPHPInfoTask);
  117. Controls.Add(_phpSetupItem);
  118. //
  119. // PHP Settings
  120. //
  121. _phpSettingsItem = new PHPPageItemControl();
  122. _phpSettingsItem.RightToLeftLayout = this.RightToLeftLayout;
  123. _phpSettingsItem.RightToLeft = this.RightToLeft;
  124. _phpSettingsItem.TitleClick += new LinkLabelLinkClickedEventHandler(OnPHPSettingsItemTitleClick);
  125. _phpSettingsItem.Title = Resources.PHPSettingsItemTitle;
  126. _phpSettingsItem.TitleFont = titleFont;
  127. _phpSettingsItem.Image = Resources.PHPSettings32;
  128. _phpSettingsItem.AddInfoRow(_configPathNameLabel, _configPathValueLabel);
  129. _phpSettingsItem.AddInfoRow(_errorLogNameLabel, _errorLogValueLabel);
  130. if (Connection.IsUserServerAdministrator)
  131. {
  132. _phpSettingsItem.AddTask(OnPHPSettingsItemClick,
  133. Resources.PHPSettingsItemErrorReportingTask,
  134. Resources.PHPSettingsItemLimitsTask,
  135. Resources.PHPSettingsItemAllSettingsTask);
  136. }
  137. else
  138. {
  139. _phpSettingsItem.AddTask(OnPHPSettingsItemClick,
  140. Resources.PHPSettingsItemReadOnlyErrorReportingTask,
  141. Resources.PHPSettingsItemReadOnlyLimitsTask,
  142. Resources.PHPSettingsItemReadOnlyAllSettingsTask);
  143. }
  144. Controls.Add(_phpSettingsItem);
  145. //
  146. // PHP Extensions
  147. //
  148. _phpExtensionItem = new PHPPageItemControl();
  149. _phpExtensionItem.RightToLeftLayout = this.RightToLeftLayout;
  150. _phpExtensionItem.RightToLeft = this.RightToLeft;
  151. _phpExtensionItem.TitleClick += new LinkLabelLinkClickedEventHandler(OnPHPExtensionItemTitleClick);
  152. _phpExtensionItem.Title = Resources.PHPExtensionsItemTitle;
  153. _phpExtensionItem.TitleFont = titleFont;
  154. _phpExtensionItem.Image = Resources.PHPExtensions32;
  155. _phpExtensionItem.AddSpanRow(_enabledExtLabel);
  156. _phpExtensionItem.AddSpanRow(_installedExtLabel);
  157. if (Connection.IsUserServerAdministrator)
  158. {
  159. _phpExtensionItem.AddTask(OnPHPExtensionItemClick,
  160. Resources.PHPExtensionItemEnableTask, Resources.PHPExtensionItemAddTask);
  161. }
  162. else
  163. {
  164. _phpExtensionItem.AddTask(OnPHPExtensionItemClick,
  165. Resources.PHPExtensionItemReadOnlyEnableTask);
  166. }
  167. Controls.Add(_phpExtensionItem);
  168. // Update the information summaries for each PHPPageItemControl
  169. Refresh();
  170. ResumeLayout(true);
  171. }
  172. private void NavigateToPHPInfo()
  173. {
  174. string siteName = null;
  175. string siteUrl = GetSiteUrlAndName(out siteName);
  176. if (!String.IsNullOrEmpty(siteUrl))
  177. {
  178. Navigate(typeof(Setup.PHPInfoPage), new string[] { siteUrl, siteName });
  179. }
  180. }
  181. protected override void OnActivated(bool initialActivation)
  182. {
  183. base.OnActivated(initialActivation);
  184. if (initialActivation)
  185. {
  186. InitializeUI();
  187. }
  188. }
  189. private void OnGetSettings(object sender, DoWorkEventArgs e)
  190. {
  191. e.Result = Module.Proxy.GetPHPConfigInfo();
  192. }
  193. private void OnGetSettingsCompleted(object sender, RunWorkerCompletedEventArgs e)
  194. {
  195. try
  196. {
  197. PHPConfigInfo configInfo = (PHPConfigInfo)e.Result;
  198. UpdatePageItemsState(configInfo);
  199. }
  200. catch (Exception ex)
  201. {
  202. DisplayErrorMessage(ex, Resources.ResourceManager);
  203. UpdatePageItemsState(null);
  204. }
  205. }
  206. protected override void OnLayout(LayoutEventArgs e)
  207. {
  208. if (!this.Visible || this.Height == 0)
  209. {
  210. return;
  211. }
  212. Control.ControlCollection controls = this.Controls;
  213. Size clientSize = ClientSize;
  214. int width = clientSize.Width - Padding.Horizontal - 12;
  215. Size proposedSize = new Size(width, Int32.MaxValue);
  216. int top = Padding.Top + AutoScrollPosition.Y;
  217. for (int i = 0; i < controls.Count; i++)
  218. {
  219. Control ctl = controls[i];
  220. Size size = ctl.GetPreferredSize(proposedSize);
  221. ctl.SetBounds(Padding.Left,
  222. top,
  223. size.Width,
  224. size.Height);
  225. top += ctl.Height;
  226. }
  227. if (top >= this.ClientSize.Height)
  228. {
  229. AdjustFormScrollbars(true);
  230. AutoScrollMinSize = new Size(ClientSize.Width, top);
  231. }
  232. else
  233. {
  234. AutoScrollMinSize = Size.Empty;
  235. AdjustFormScrollbars(false);
  236. }
  237. }
  238. private void OnPathLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  239. {
  240. OpenPhysicalFile((string)e.Link.LinkData);
  241. }
  242. private void OnPHPExtensionItemClick(int index)
  243. {
  244. if (index == IndexAllExtensionsTask)
  245. {
  246. Navigate(typeof(Extensions.AllExtensionsPage));
  247. }
  248. if (index == IndexAddExtensionTask)
  249. {
  250. AddExtension();
  251. }
  252. }
  253. private void AddExtension()
  254. {
  255. using (Extensions.AddExtensionDialog dlg = new Extensions.AddExtensionDialog(this.Module, Connection.IsLocalConnection))
  256. {
  257. if (ShowDialog(dlg) == DialogResult.OK)
  258. {
  259. Navigate(typeof(Extensions.AllExtensionsPage), dlg.AddedExtensionName);
  260. }
  261. }
  262. }
  263. private void OnPHPExtensionItemTitleClick(object sender, LinkLabelLinkClickedEventArgs e)
  264. {
  265. Navigate(typeof(Extensions.AllExtensionsPage));
  266. }
  267. private void OnPHPSettingsItemClick(int index)
  268. {
  269. if (index == IndexErrorReportingTask)
  270. {
  271. Navigate(typeof(Settings.ErrorReportingPage));
  272. }
  273. if (index == IndexLimitsTask)
  274. {
  275. Navigate(typeof(Settings.RuntimeLimitsPage));
  276. }
  277. if (index == IndexAllSettingsTask)
  278. {
  279. Navigate(typeof(Settings.AllSettingsPage));
  280. }
  281. }
  282. private void OnPHPSettingsItemTitleClick(object sender, LinkLabelLinkClickedEventArgs e)
  283. {
  284. Navigate(typeof(Settings.AllSettingsPage));
  285. }
  286. private void OnPHPSetupItemClick(int index)
  287. {
  288. if (index == IndexRegisterPHPTask)
  289. {
  290. RegisterPHPWithIIS();
  291. }
  292. else if (index == IndexChangeVersionTask)
  293. {
  294. SelectPHPVersion();
  295. }
  296. else if (index == IndexCheckPHPInfoTask)
  297. {
  298. NavigateToPHPInfo();
  299. }
  300. }
  301. private void OnPHPSetupItemTitleClick(object sender, LinkLabelLinkClickedEventArgs e)
  302. {
  303. NavigateToPHPInfo();
  304. }
  305. private void OnWarningLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  306. {
  307. using (Setup.RecommendedConfigDialog dlg = new Setup.RecommendedConfigDialog(Module))
  308. {
  309. if (ShowDialog(dlg) == DialogResult.OK)
  310. {
  311. Refresh();
  312. }
  313. }
  314. }
  315. internal void OpenPhysicalFile(string physicalPath)
  316. {
  317. try
  318. {
  319. if (!String.IsNullOrEmpty(physicalPath) &&
  320. File.Exists(physicalPath))
  321. {
  322. System.Diagnostics.Process.Start(physicalPath);
  323. }
  324. else
  325. {
  326. ShowMessage(String.Format(CultureInfo.CurrentCulture, Resources.ErrorFileDoesNotExist, physicalPath), MessageBoxButtons.OK, MessageBoxIcon.Information);
  327. }
  328. }
  329. catch (Exception ex)
  330. {
  331. DisplayErrorMessage(ex, Resources.ResourceManager);
  332. }
  333. }
  334. private static void PrepareOpenFileLink(LinkLabel linkLabel, string path, bool showLink)
  335. {
  336. linkLabel.Text = path;
  337. if (showLink && !String.IsNullOrEmpty(path))
  338. {
  339. if (linkLabel.Links.Count == 0)
  340. {
  341. LinkLabel.Link link = new LinkLabel.Link(0, path.Length, path);
  342. linkLabel.Links.Add(link);
  343. }
  344. else
  345. {
  346. LinkLabel.Link link = linkLabel.Links[0];
  347. link.Length = path.Length;
  348. link.LinkData = path;
  349. }
  350. }
  351. else
  352. {
  353. if (linkLabel.Links.Count > 0)
  354. {
  355. linkLabel.Links.Clear();
  356. }
  357. }
  358. }
  359. private LinkLabel PreparePHPConfigWarning()
  360. {
  361. LinkLabel result = new LinkLabel();
  362. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  363. sb.Append(Resources.WarningPHPConfigNotOptimal);
  364. int viewRecommendationsLinkStart = Resources.WarningPHPConfigNotOptimal.Length;
  365. sb.Append(Resources.WarningViewRecommendations);
  366. result.Text = sb.ToString();
  367. LinkLabel.Link fixItLink = new LinkLabel.Link(viewRecommendationsLinkStart, Resources.WarningViewRecommendations.Length, 0);
  368. result.Links.Add(fixItLink);
  369. result.LinkClicked += new LinkLabelLinkClickedEventHandler(OnWarningLinkClicked);
  370. return result;
  371. }
  372. private static Label PreparePHPRegistrationWarning(PHPRegistrationType registrationType)
  373. {
  374. Label result = new Label();
  375. if (registrationType == PHPRegistrationType.Cgi)
  376. {
  377. result.Text = Resources.WarningPHPConfigCgi;
  378. }
  379. else if (registrationType == PHPRegistrationType.Isapi)
  380. {
  381. result.Text = Resources.WarningPHPConfigIsapi;
  382. }
  383. else if (registrationType == PHPRegistrationType.None)
  384. {
  385. result.Text = Resources.WarningPHPConfigNotRegistered;
  386. }
  387. return result;
  388. }
  389. protected override void Refresh()
  390. {
  391. GetSettings();
  392. }
  393. private void RegisterPHPWithIIS()
  394. {
  395. using (Setup.RegisterPHPDialog dlg = new Setup.RegisterPHPDialog(this.Module, Connection.IsLocalConnection))
  396. {
  397. if (ShowDialog(dlg) == DialogResult.OK)
  398. {
  399. Refresh();
  400. }
  401. }
  402. }
  403. private void SelectPHPVersion()
  404. {
  405. using (Setup.ChangeVersionDialog dlg = new Setup.ChangeVersionDialog(this.Module))
  406. {
  407. if (ShowDialog(dlg) == DialogResult.OK)
  408. {
  409. Refresh();
  410. }
  411. }
  412. }
  413. protected override bool ShowHelp()
  414. {
  415. return ShowOnlineHelp();
  416. }
  417. protected override bool ShowOnlineHelp()
  418. {
  419. return Helper.Browse(Globals.PHPPageOnlineHelp);
  420. }
  421. private void UpdatePageItemsState(PHPConfigInfo configInfo)
  422. {
  423. UpdatePHPSetupItem(configInfo);
  424. UpdatePHPSettingsItem(configInfo);
  425. UpdatePHPExtensionsItem(configInfo);
  426. PerformLayout();
  427. }
  428. private void UpdatePHPExtensionsItem(PHPConfigInfo configInfo)
  429. {
  430. bool isPHPSetup = (configInfo != null && configInfo.RegistrationType == PHPRegistrationType.FastCgi);
  431. _phpExtensionItem.SetTitleState(isPHPSetup);
  432. if (isPHPSetup)
  433. {
  434. _enabledExtLabel.Text = String.Format(CultureInfo.CurrentCulture, Resources.PHPPageEnabledExtensions, configInfo.EnabledExtCount);
  435. _installedExtLabel.Text = String.Format(CultureInfo.CurrentCulture, Resources.PHPPageInstalledExtensions, configInfo.InstalledExtCount);
  436. }
  437. else
  438. {
  439. _enabledExtLabel.Text = Resources.PHPPageExtensionsNotAvailable;
  440. _installedExtLabel.Text = Resources.PHPPageExtensionsNotAvailable;
  441. }
  442. _phpExtensionItem.SetTaskState(IndexAllExtensionsTask, isPHPSetup);
  443. if (Connection.IsUserServerAdministrator)
  444. {
  445. _phpExtensionItem.SetTaskState(IndexAddExtensionTask, isPHPSetup);
  446. }
  447. }
  448. private void UpdatePHPSettingsItem(PHPConfigInfo configInfo)
  449. {
  450. bool isPHPSetup = (configInfo != null && configInfo.RegistrationType == PHPRegistrationType.FastCgi);
  451. _phpSettingsItem.SetTitleState(isPHPSetup);
  452. if (isPHPSetup)
  453. {
  454. PrepareOpenFileLink(_configPathValueLabel, configInfo.PHPIniFilePath, Connection.IsLocalConnection);
  455. PrepareOpenFileLink(_errorLogValueLabel, configInfo.ErrorLog, Connection.IsLocalConnection);
  456. }
  457. else
  458. {
  459. PrepareOpenFileLink(_configPathValueLabel, Resources.PHPPagePHPNotAvailable, false);
  460. PrepareOpenFileLink(_errorLogValueLabel, Resources.PHPPagePHPNotAvailable, false);
  461. }
  462. _phpSettingsItem.SetTaskState(IndexErrorReportingTask, isPHPSetup);
  463. _phpSettingsItem.SetTaskState(IndexLimitsTask, isPHPSetup);
  464. _phpSettingsItem.SetTaskState(IndexAllSettingsTask, isPHPSetup);
  465. }
  466. private void UpdatePHPSetupItem(PHPConfigInfo configInfo)
  467. {
  468. bool isPHPSetup = (configInfo != null && configInfo.RegistrationType == PHPRegistrationType.FastCgi);
  469. _phpSetupItem.SetTitleState(isPHPSetup);
  470. _phpSetupItem.ClearWarning();
  471. if (isPHPSetup)
  472. {
  473. // Show warning about non optimal configuration if
  474. // PHP configuration is not optimal and
  475. // user is a server administrator.
  476. if (!configInfo.IsConfigOptimal && Connection.IsUserServerAdministrator)
  477. {
  478. _phpSetupItem.SetWarning(PreparePHPConfigWarning());
  479. }
  480. }
  481. else if (configInfo != null)
  482. {
  483. // Show warning about PHP not being setup or setup incorrectly
  484. _phpSetupItem.SetWarning(PreparePHPRegistrationWarning(configInfo.RegistrationType));
  485. }
  486. else
  487. {
  488. // Show warning about failed IIS configuration
  489. Label errorLabel = new Label();
  490. errorLabel.Text = Resources.ErrorFailedToGetConfiguration;
  491. _phpSetupItem.SetWarning(errorLabel);
  492. }
  493. _versionValueLabel.Text = isPHPSetup ? configInfo.Version : Resources.PHPPagePHPNotAvailable;
  494. _executableValueLabel.Text = isPHPSetup ? configInfo.Executable : Resources.PHPPagePHPNotAvailable;
  495. // Allow PHP registration only for server administrators
  496. if (configInfo != null)
  497. {
  498. _phpSetupItem.SetTaskState(IndexRegisterPHPTask, Connection.IsUserServerAdministrator);
  499. }
  500. else
  501. {
  502. // If there is an error in IIS configuration then do not allow new registrations
  503. _phpSetupItem.SetTaskState(IndexRegisterPHPTask, false);
  504. }
  505. _phpSetupItem.SetTaskState(IndexChangeVersionTask, isPHPSetup);
  506. _phpSetupItem.SetTaskState(IndexCheckPHPInfoTask, isPHPSetup);
  507. }
  508. }
  509. }