From b1eb6dd7f2844d9a3829d41bd43172f995d84391 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 27 Sep 2017 10:10:19 +1300 Subject: [PATCH] MDL-33174 lib/filestorage: Add filename to curl request While the lack of the filename in curl POST does not directly affect Moodle, some external API's expect to get the filename of the original file, which affects plugins using the CURL class. --- lib/filestorage/file_system.php | 2 +- lib/tests/filelib_test.php | 23 +++++++++++++++++++++++ lib/upgrade.txt | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/filestorage/file_system.php b/lib/filestorage/file_system.php index 3d6ff5b6056..21ebe77086e 100644 --- a/lib/filestorage/file_system.php +++ b/lib/filestorage/file_system.php @@ -362,7 +362,7 @@ abstract class file_system { public function add_to_curl_request(stored_file $file, &$curlrequest, $key) { // Note: curl_file_create does not work with remote paths. $path = $this->get_local_path_from_storedfile($file, true); - $curlrequest->_tmp_file_post_params[$key] = curl_file_create($path); + $curlrequest->_tmp_file_post_params[$key] = curl_file_create($path, null, $file->get_filename()); } /** diff --git a/lib/tests/filelib_test.php b/lib/tests/filelib_test.php index cb40c8fe7e8..b6109d98164 100644 --- a/lib/tests/filelib_test.php +++ b/lib/tests/filelib_test.php @@ -545,6 +545,29 @@ class core_filelib_testcase extends advanced_testcase { $this->assertSame('OK', $contents); } + public function test_curl_file_name() { + $this->resetAfterTest(); + $testurl = $this->getExternalTestFileUrl('/test_file_name.php'); + + $fs = get_file_storage(); + $filerecord = array( + 'contextid' => context_system::instance()->id, + 'component' => 'test', + 'filearea' => 'curl_post', + 'itemid' => 0, + 'filepath' => '/', + 'filename' => 'test.txt' + ); + $teststring = 'moodletest'; + $testfile = $fs->create_file_from_string($filerecord, $teststring); + + // Test post with file. + $data = array('testfile' => $testfile); + $curl = new curl(); + $contents = $curl->post($testurl, $data); + $this->assertSame('OK', $contents); + } + public function test_curl_protocols() { // HTTP and HTTPS requests were verified in previous requests. Now check diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 38772ea50e4..1c0781b3291 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -46,6 +46,7 @@ information provided here is intended especially for developers. update to use the ModalEvents.save and ModalEvents.cancel events instead of their yes/no counterparts. * Instead of checking the 'moodle/course:viewparticipants' and 'moodle/site:viewparticipants' capabilities use the new functions course_can_view_participants() and course_require_view_participants(). +* $stored_file->add_to_curl_request() now adds the filename to the curl request. === 3.3.1 ===