From 169f8871738f5ea8920e993a5a44823eeea2b82b Mon Sep 17 00:00:00 2001
From: Ankit Agarwal <ankit@moodle.com>
Date: Wed, 2 Nov 2016 16:06:20 +0530
Subject: [PATCH] MDL-55777 installation: Check libcurl version on install

---
 admin/environment.xml         | 20 ++++++++++++++++++++
 lang/en/admin.php             |  1 +
 lib/tests/upgradelib_test.php | 16 ++++++++++++++++
 lib/upgradelib.php            | 25 +++++++++++++++++++++++++
 version.php                   |  2 +-
 5 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/admin/environment.xml b/admin/environment.xml
index 1f522ab..f2f135d 100644
--- a/admin/environment.xml
+++ b/admin/environment.xml
@@ -1145,6 +1145,11 @@
           <ON_CHECK message="unsupporteddbtablerowformat" />
         </FEEDBACK>
       </CUSTOM_CHECK>
+      <CUSTOM_CHECK file="lib/upgradelib.php" function="check_libcurl_version" level="optional">
+        <FEEDBACK>
+          <ON_CHECK message="libcurlwarning" />
+        </FEEDBACK>
+      </CUSTOM_CHECK>
     </CUSTOM_CHECKS>
   </MOODLE>
   <MOODLE version="2.8" requires="2.2">
@@ -1556,6 +1561,11 @@
           <ON_CHECK message="unsupporteddbtablerowformat" />
         </FEEDBACK>
       </CUSTOM_CHECK>
+      <CUSTOM_CHECK file="lib/upgradelib.php" function="check_libcurl_version" level="optional">
+        <FEEDBACK>
+          <ON_CHECK message="libcurlwarning" />
+        </FEEDBACK>
+      </CUSTOM_CHECK>
     </CUSTOM_CHECKS>
   </MOODLE>
   <MOODLE version="3.1" requires="2.7">
@@ -1696,6 +1706,11 @@
           <ON_CHECK message="unsupporteddbtablerowformat" />
         </FEEDBACK>
       </CUSTOM_CHECK>
+      <CUSTOM_CHECK file="lib/upgradelib.php" function="check_libcurl_version" level="optional">
+        <FEEDBACK>
+          <ON_CHECK message="libcurlwarning" />
+        </FEEDBACK>
+      </CUSTOM_CHECK>
     </CUSTOM_CHECKS>
   </MOODLE>
   <MOODLE version="3.2" requires="2.7">
@@ -1841,6 +1856,11 @@
           <ON_CHECK message="unoconvwarning" />
         </FEEDBACK>
       </CUSTOM_CHECK>
+      <CUSTOM_CHECK file="lib/upgradelib.php" function="check_libcurl_version" level="optional">
+        <FEEDBACK>
+          <ON_CHECK message="libcurlwarning" />
+        </FEEDBACK>
+      </CUSTOM_CHECK>
     </CUSTOM_CHECKS>
   </MOODLE>
 </COMPATIBILITY_MATRIX>
diff --git a/lang/en/admin.php b/lang/en/admin.php
index 17ab63c..002631f 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -613,6 +613,7 @@ $string['legacyfilesaddallowed'] = 'Allow adding to legacy course files';
 $string['legacyfilesaddallowed_help'] = 'If a course has legacy course files, allow new files and folders to be added to it.';
 $string['legacyfilesinnewcourses'] = 'Legacy course files in new courses';
 $string['legacyfilesinnewcourses_help'] = 'By default, legacy course files areas are available in upgraded courses only. Please note that some features such as activity backup and restore are not compatible with this setting.';
+$string['libcurlwarning'] = 'Libcurl with CURLOPT_PROTOCOL support has not been detected. It is recommended to have an up to date libcurl installation for security reasons.';
 $string['licensesettings'] = 'Licence settings';
 $string['linkadmincategories'] = 'Link admin categories';
 $string['linkadmincategories_help'] = 'If enabled admin setting categories will be displayed as links in the navigation and will lead to the admin category pages.';
diff --git a/lib/tests/upgradelib_test.php b/lib/tests/upgradelib_test.php
index ed8c4bf..5fa7fbf 100644
--- a/lib/tests/upgradelib_test.php
+++ b/lib/tests/upgradelib_test.php
@@ -1052,4 +1052,20 @@ class core_upgradelib_testcase extends advanced_testcase {
             $DB->insert_record('grade_letters', $record);
         }
     }
+
+    /**
+     * Test libcurl custom check api.
+     */
+    public function test_check_libcurl_version() {
+        $supportedversion = 0x071304;
+        $curlinfo = curl_version();
+        $currentversion = $curlinfo['version_number'];
+
+        $result = new environment_results("custom_checks");
+        if ($currentversion < $supportedversion) {
+            $this->assertFalse(check_libcurl_version($result)->getStatus());
+        } else {
+            $this->assertTrue(check_libcurl_version($result)->getStatus());
+        }
+    }
 }
diff --git a/lib/upgradelib.php b/lib/upgradelib.php
index c44cee9..07ce5e0 100644
--- a/lib/upgradelib.php
+++ b/lib/upgradelib.php
@@ -2444,3 +2444,28 @@ function upgrade_install_plugins(array $installable, $confirmed, $heading='', $c
         die();
     }
 }
+
+function check_libcurl_version(environment_results $result) {
+
+    // Supported version and version number.
+    $supportedversion = 0x071304;
+    $supportedversionstring = "7.19.4";
+
+    // Installed version.
+    $curlinfo = curl_version();
+    $currentversion = $curlinfo['version_number'];
+
+    // Set info, we want to let user know how to resolve the problem.
+    $result->setInfo('Libcurl version check');
+    $result->setNeededVersion($supportedversionstring);
+    $result->setCurrentVersion($curlinfo['version']);
+
+    if ($currentversion < $supportedversion) {
+        // Test fail.
+        $result->setStatus(false);
+    } else {
+        $result->setStatus(true);
+    }
+
+    return $result;
+}
diff --git a/version.php b/version.php
index 5e8ca9b..6ee0ddb 100644
--- a/version.php
+++ b/version.php
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2015111606.12;              // 20151116      = branching date YYYYMMDD - do not modify!
+$version  = 2015111606.13;              // 20151116      = branching date YYYYMMDD - do not modify!
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 
-- 
2.7.4

