From 1f814b847846265b009abc7615fa5133db3ce07a Mon Sep 17 00:00:00 2001
From: Dan Marsden <dan@danmarsden.com>
Date: Tue, 14 May 2019 21:55:34 +1200
Subject: [PATCH] MDL-60347 core: debugsmtp should be a developer only setting.

---
 admin/settings/development.php | 1 -
 config-dist.php                | 4 ++++
 lang/en/admin.php              | 2 --
 lib/db/upgrade.php             | 7 +++++++
 lib/moodlelib.php              | 2 +-
 version.php                    | 2 +-
 6 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/admin/settings/development.php b/admin/settings/development.php
index 58493dda8d9..09a8a82636e 100644
--- a/admin/settings/development.php
+++ b/admin/settings/development.php
@@ -35,7 +35,6 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
     $temp = new admin_settingpage('debugging', new lang_string('debugging', 'admin'));
     $temp->add(new admin_setting_special_debug());
     $temp->add(new admin_setting_configcheckbox('debugdisplay', new lang_string('debugdisplay', 'admin'), new lang_string('configdebugdisplay', 'admin'), ini_get_bool('display_errors')));
-    $temp->add(new admin_setting_configcheckbox('debugsmtp', new lang_string('debugsmtp', 'admin'), new lang_string('configdebugsmtp', 'admin'), 0));
     $temp->add(new admin_setting_configcheckbox('perfdebug', new lang_string('perfdebug', 'admin'), new lang_string('configperfdebug', 'admin'), '7', '15', '7'));
     $temp->add(new admin_setting_configcheckbox('debugstringids', new lang_string('debugstringids', 'admin'), new lang_string('debugstringids_desc', 'admin'), 0));
     $temp->add(new admin_setting_configcheckbox('debugvalidators', new lang_string('debugvalidators', 'admin'), new lang_string('configdebugvalidators', 'admin'), 0));
diff --git a/config-dist.php b/config-dist.php
index 5f4258a3b16..ac730124279 100644
--- a/config-dist.php
+++ b/config-dist.php
@@ -641,6 +641,10 @@ $CFG->admin = 'admin';
 // Enable verbose debug information during fetching of email messages from IMAP server.
 // $CFG->debugimap = true;
 //
+// Enable verbose debug information during sending of email messages to SMTP server.
+// Note: also requires $CFG->debug set to DEBUG_DEVELOPER.
+// $CFG->debugsmtp = true;
+//
 // Prevent JS caching
 // $CFG->cachejs = false; // NOT FOR PRODUCTION SERVERS!
 //
diff --git a/lang/en/admin.php b/lang/en/admin.php
index 26c89616de3..5aace8cd4c2 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -200,7 +200,6 @@ $string['configdbsessions'] = 'If enabled, this setting will use the database to
 $string['configdebug'] = 'If you turn this on, then PHP\'s error_reporting will be increased so that more warnings are printed.  This is only useful for developers.';
 $string['configdebugdisplay'] = 'Set to on, the error reporting will go to the HTML page. This is practical, but breaks XHTML, JS, cookies and HTTP headers in general. Set to off, it will send the output to your server logs, allowing better debugging. The PHP setting error_log controls which log this goes to.';
 $string['configdebugpageinfo'] = 'Enable if you want page information printed in page footer.';
-$string['configdebugsmtp'] = 'Enable verbose debug information during sending of email messages to SMTP server.';
 $string['configdebugvalidators'] = 'Enable if you want to have links to external validator servers in page footer. You may need to create new user with username <em>w3cvalidator</em>, and enable guest access. These changes may allow unauthorized access to server, do not enable on production sites!';
 $string['configdefaulthomepage'] = 'This determines the home page for logged in users';
 $string['configdefaultrequestcategory'] = 'Courses requested by users will be automatically placed in this category.';
@@ -449,7 +448,6 @@ $string['debugminimal'] = 'MINIMAL: Show only fatal errors';
 $string['debugnone'] = 'NONE: Do not show any errors or warnings';
 $string['debugnormal'] = 'NORMAL: Show errors, warnings and notices';
 $string['debugpageinfo'] = 'Show page information';
-$string['debugsmtp'] = 'Debug email sending';
 $string['debugstringids'] = 'Show origin of languages strings';
 $string['debugstringids_desc'] = 'If enabled, language string components and identifiers are displayed when ?strings=1 or &strings=1 is appended to the page URL.';
 $string['debugvalidators'] = 'Show validator links';
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index f304bab7611..74a39c6f2df 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -3378,5 +3378,12 @@ function xmldb_main_upgrade($oldversion) {
     // Automatically generated Moodle v3.7.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2019060600.02) {
+        // Debugsmtp is now only available via config.php.
+        $DB->delete_records('config', array('name' => 'debugsmtp'));
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2019060600.02);
+    }
     return true;
 }
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index 820dc475545..e5ffe1dfd15 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -5762,7 +5762,7 @@ function get_mailer($action='get') {
         } else {
             // Use SMTP directly.
             $mailer->isSMTP();
-            if (!empty($CFG->debugsmtp)) {
+            if (!empty($CFG->debugsmtp) && (!empty($CFG->debugdeveloper))) {
                 $mailer->SMTPDebug = 3;
             }
             // Specify main and backup servers.
diff --git a/version.php b/version.php
index cd947aead4e..14eeb72a9b7 100644
--- a/version.php
+++ b/version.php
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2019060600.01;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2019060600.02;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 
-- 
2.17.1

