PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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