MDL-65187 core_hub: truncate registration request, use post
authorMarina Glancy <marina@moodle.com>
Tue, 28 May 2019 11:56:53 +0000 (13:56 +0200)
committerMarina Glancy <marina@moodle.com>
Mon, 30 Sep 2019 11:05:19 +0000 (13:05 +0200)
lib/classes/hub/api.php
lib/classes/hub/registration.php
lib/classes/hub/site_registration_form.php

index 2a1434d..7c0ed37 100644 (file)
@@ -88,7 +88,8 @@ class api {
 
         $curl = new curl();
         $serverurl = HUB_MOODLEORGHUBURL . "/local/hub/webservice/webservices.php";
-        $curloutput = @json_decode($curl->get($serverurl, $params), true);
+        $query = http_build_query($params, '', '&');
+        $curloutput = @json_decode($curl->post($serverurl, $query), true);
         $info = $curl->get_info();
         if ($curl->get_errno()) {
             // Connection error.
index 3807d9c..cead3bb 100644 (file)
@@ -350,6 +350,13 @@ class registration {
         $record['timemodified'] = time();
         $DB->update_record('registration_hubs', $record);
         self::$registration = null;
+
+        $siteinfo = self::get_site_info();
+        if (strlen(http_build_query($siteinfo)) > 1800) {
+            // Update registration again because the initial request was too long and could have been truncated.
+            api::update_registration($siteinfo);
+            self::$registration = null;
+        }
     }
 
     /**
@@ -397,10 +404,22 @@ class registration {
             self::$registration = null;
         }
 
-        $params = ['token' => $hub->token] + self::get_site_info();
+        $params = self::get_site_info();
+
+        // The most conservative limit for the redirect URL length is 2000 characters. Only pass parameters before
+        // we reach this limit. The next registration update will update all fields.
+        // We will also update registration after we receive confirmation from moodle.net.
+        $url = new moodle_url(HUB_MOODLEORGHUBURL . '/local/hub/siteregistration.php',
+            ['token' => $hub->token, 'url' => $params['url']]);
+        foreach ($params as $key => $value) {
+            if (strlen($url->out(false, [$key => $value])) > 2000) {
+                break;
+            }
+            $url->param($key, $value);
+        }
 
         $SESSION->registrationredirect = $returnurl;
-        redirect(new moodle_url(HUB_MOODLEORGHUBURL . '/local/hub/siteregistration.php', $params));
+        redirect($url);
     }
 
     /**
index 510a2da..0369aa3 100644 (file)
@@ -75,7 +75,7 @@ class site_registration_form extends \moodleform {
         $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
 
         $mform->addElement('text', 'name', get_string('sitename', 'hub'),
-            array('class' => 'registration_textfield'));
+            array('class' => 'registration_textfield', 'maxlength' => 255));
         $mform->setType('name', PARAM_TEXT);
         $mform->addHelpButton('name', 'sitename', 'hub');