mirror of
https://github.com/moodle/moodle.git
synced 2025-04-07 09:23:31 +02:00
MDL-46269 tool_httpsreplace: Make tool ready for core
Remove known domains add config for renames Fix coding style Add capability checks Add page setters Match moodle string style Disable form change checker for form Add todo issue Fix docs Bump version
This commit is contained in:
parent
836226c4ad
commit
7821d93aa6
admin/tool/httpsreplace
@ -1,8 +0,0 @@
|
||||
This plugin was contributed by the Moodlerooms Product Development team. Moodlerooms is an education technology company
|
||||
dedicated to bringing excellent online teaching to institutions across the globe. We serve colleges and universities,
|
||||
schools and organizations by supporting the software that educators use to manage and deliver instructional content to
|
||||
learners in virtual classrooms. Moodlerooms is headquartered in Baltimore, MD. We are proud to be a Moodle Partner company.
|
||||
|
||||
For more information about installation, configuration and usage, please see [the wiki page]
|
||||
|
||||
[the wiki page]: <https://docs.moodle.org/30/en/admin/tool/httpsreplace/index>
|
@ -30,9 +30,14 @@ require_once("$CFG->libdir/formslib.php");
|
||||
|
||||
/**
|
||||
* Site wide http -> https search-replace form.
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class form extends \moodleform {
|
||||
|
||||
/**
|
||||
* Define the form.
|
||||
*/
|
||||
public function definition() {
|
||||
$mform = $this->_form;
|
||||
|
||||
@ -40,6 +45,7 @@ class form extends \moodleform {
|
||||
$mform->setExpanded('confirmhdr', true);
|
||||
$mform->addElement('checkbox', 'sure', get_string('disclaimer', 'tool_httpsreplace'));
|
||||
$mform->addRule('sure', get_string('required'), 'required', null, 'client');
|
||||
$mform->disable_form_change_checker();
|
||||
|
||||
$this->add_action_buttons(false, get_string('doit', 'tool_httpsreplace'));
|
||||
}
|
||||
|
@ -14,33 +14,42 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* url_finder class definition.
|
||||
*
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_httpsreplace;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Examines DB for non-https src or data links that will cause trouble
|
||||
* when embedded in HTTPS sites.
|
||||
* Examines DB for non-https src or data links
|
||||
*
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class url_finder {
|
||||
|
||||
/**
|
||||
* Domains that need replaced when using https links.
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
private $exceptions = [
|
||||
'cdnapi.kaltura.com' => 'cdnapisec.kaltura.com',
|
||||
];
|
||||
|
||||
/**
|
||||
* Returns a hash of what hosts are referred to over http and would need to be changed.
|
||||
*
|
||||
* @return array Hash of domains with number of references as the value.
|
||||
*/
|
||||
public function http_link_stats() {
|
||||
return $this->process(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all resources referred to over http to https.
|
||||
*
|
||||
* @return bool True upon success
|
||||
*/
|
||||
public function upgrade_http_links() {
|
||||
return $this->process(true);
|
||||
}
|
||||
@ -52,23 +61,17 @@ class url_finder {
|
||||
* @param string $table
|
||||
* @param string $column
|
||||
* @param string $domain
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function domain_swap($table, $column, $domain) {
|
||||
global $DB;
|
||||
|
||||
$renames = (array)json_decode(get_config('tool_httpsreplace', 'renames'));
|
||||
|
||||
$search = "http://$domain";
|
||||
$replace = "https://$domain";
|
||||
if (isset($this->exceptions[$domain])) {
|
||||
$replace = 'https://' . $this->exceptions[$domain];
|
||||
}
|
||||
if (preg_match('/rackcdn.com$/', $domain)) {
|
||||
// Regexes adapted from
|
||||
// https://www.eff.org/https-everywhere/atlas/domains/rackcdn.com.html ruleset.
|
||||
$pattern = '/^([\w-]+)\.(?:r\d+|ssl)\.cf(\d)\.rackcdn\.com$/';
|
||||
$replacement = 'https://$1.ssl.cf$2.rackcdn.com';
|
||||
$replace = preg_replace($pattern, $replacement, $domain);
|
||||
if (isset($renames[$domain])) {
|
||||
$replace = 'https://' . $renames[$domain];
|
||||
}
|
||||
$DB->set_debug(true);
|
||||
// Note, this search is case sensitive.
|
||||
@ -78,6 +81,8 @@ class url_finder {
|
||||
|
||||
/**
|
||||
* Originally forked from core function db_search().
|
||||
* @param bool $replacing Whether or not to replace the found urls.
|
||||
* @return bool|array If $replacing, return true on success. If not, return hash of http urls to number of times used.
|
||||
*/
|
||||
private function process($replacing = false) {
|
||||
global $DB, $CFG;
|
||||
@ -87,7 +92,7 @@ class url_finder {
|
||||
$httpurls = "(src|data)\ *=\ *[\\\"\']http://";
|
||||
|
||||
// TODO: block_instances have HTML content as base64, need to decode then
|
||||
// search, currently just skipped.
|
||||
// search, currently just skipped. See MDL-60024.
|
||||
$skiptables = array(
|
||||
'block_instances',
|
||||
'config',
|
||||
@ -186,19 +191,8 @@ class url_finder {
|
||||
$uniquedomains = array_unique($domains);
|
||||
|
||||
$sslfailures = array();
|
||||
$knownsupported = array(
|
||||
'amazon.com',
|
||||
'www.amazon.com',
|
||||
'dropbox.com',
|
||||
'www.dropbox.com',
|
||||
'cdnapi.kaltura.com',
|
||||
'fe8be92ac963979368eca.r38.cf1.rackcdn.com', // Not actually a real domain, but used for testing.
|
||||
);
|
||||
|
||||
foreach ($uniquedomains as $domain) {
|
||||
if (in_array($domain, $knownsupported)) {
|
||||
continue;
|
||||
}
|
||||
$url = "https://$domain/";
|
||||
$curl = new \curl();
|
||||
$curl->head($url);
|
||||
|
@ -14,6 +14,14 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* url_finder cli script. Examines DB for non-https src or data links, and lists them.
|
||||
*
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
define('CLI_SCRIPT', true);
|
||||
require(__DIR__ . '/../../../../config.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
|
@ -14,6 +14,14 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* url_finder cli script. Examines DB for non-https src or data links, and replaces them.
|
||||
*
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
define('CLI_SCRIPT', true);
|
||||
require(__DIR__ . '/../../../../config.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
|
@ -24,12 +24,22 @@
|
||||
|
||||
define('NO_OUTPUT_BUFFERING', true);
|
||||
|
||||
require_once('../../../config.php');
|
||||
require_once($CFG->dirroot.'/course/lib.php');
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
require_once(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
admin_externalpage_setup('toolhttpsreplace');
|
||||
|
||||
$context = context_system::instance();
|
||||
|
||||
require_login();
|
||||
require_capability('moodle/site:config', $context);
|
||||
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_url(new moodle_url('/admin/tool/httpsreplace/index.php'));
|
||||
$PAGE->set_title(get_string('pageheader', 'tool_httpsreplace'));
|
||||
$PAGE->set_pagelayout('admin');
|
||||
|
||||
echo $OUTPUT->header();
|
||||
|
||||
echo $OUTPUT->heading(get_string('pageheader', 'tool_httpsreplace'));
|
||||
|
@ -17,25 +17,21 @@
|
||||
/**
|
||||
* Strings for component 'tool_httpsreplace'
|
||||
*
|
||||
* @package tool
|
||||
* @subpackage httpsreplace
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string['count'] = 'Number of links';
|
||||
$string['count'] = 'Number of embeded content items';
|
||||
$string['disclaimer'] = 'I understand the risks of this operation';
|
||||
$string['doclink'] = 'Read more documentation on the wiki';
|
||||
$string['doit'] = 'Yes, do it!';
|
||||
$string['doit'] = 'Perform replacement';
|
||||
$string['domain'] = 'Problematic domain';
|
||||
$string['domainexplain'] = 'This tool locates embedded content that may not work when upgrading a site to use https. It also allows you to fix the problems automatically.';
|
||||
$string['domainexplainhelp'] = 'These domains are found in your content, but do not appear to support https links. After switching to https, the content included from these sites will no longer display within Moodle for users with secure modern browsers. It is possible that these sites are temporarily or permanently unavailable and will not work with either security setting. Proceed only after reviewing these results and determining if this externally hosted content is non-essential.';
|
||||
$string['invalidcharacter'] = 'Invalid characters were found in the search or replace text.';
|
||||
$string['notifyfinished'] = '...finished';
|
||||
$string['notifyrebuilding'] = 'Rebuilding course cache...';
|
||||
$string['domainexplain'] = 'When an instance is moved from http to https, all embeded http content will stop working. This tool always you to automatically convert the http content to https. Below is a report of content that may not work once you run this script. You may want to check each one has https available or find alternative resources.';
|
||||
$string['domainexplainhelp'] = 'These domains are found in your content, but do not appear to support https content. After switching to https, the content included from these sites will no longer display within Moodle for users with secure modern browsers. It is possible that these sites are temporarily or permanently unavailable and will not work with either security setting. Proceed only after reviewing these results and determining if this externally hosted content is non-essential.';
|
||||
$string['notimplemented'] = 'Sorry, this feature is not implemented in your database driver.';
|
||||
$string['oktoprocede'] = 'The scan finds no issues with your content. You can proceed to upgrade any http links to use https.';
|
||||
$string['oktoprocede'] = 'The scan finds no issues with your content. You can proceed to upgrade any http content to use https.';
|
||||
$string['pageheader'] = 'Upgrade externally hosted content urls to https';
|
||||
$string['pluginname'] = 'HTTPS Replace';
|
||||
$string['replacing'] = 'Replacing http links with https...';
|
||||
$string['takeabackupwarning'] = 'Changes made can\'t be reverted. A complete backup should be made before running this script!';
|
||||
$string['pluginname'] = 'HTTPS conversion tool';
|
||||
$string['replacing'] = 'Replacing http content with https...';
|
||||
$string['takeabackupwarning'] = 'Once this tool run, changes made can\'t be reverted. A complete backup should be made before running this script! There is a low risk that the wrong content will be replaced, introducing problems.';
|
||||
|
@ -27,6 +27,13 @@ namespace tool_httpsreplace\tests;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Tests the httpsreplace tool.
|
||||
*
|
||||
* @package tool_httpsreplace
|
||||
* @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class httpsreplace_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
@ -67,16 +74,6 @@ class httpsreplace_test extends \advanced_testcase {
|
||||
"outputregex" => '/^$/',
|
||||
"expectedcontent" => '<img src="https://anothersite.com?param=http://asdf.com">',
|
||||
],
|
||||
"Known supported domain should be replaced" => [
|
||||
"content" => '<iframe src="http://fe8be92ac963979368eca.r38.cf1.rackcdn.com/Helpful_ET_Websites_Apps_Resources.pdf">',
|
||||
"domain" => 'fe8be92ac963979368eca.ssl.cf1.rackcdn.com',
|
||||
"outputregex" => '/UPDATE/',
|
||||
],
|
||||
"Exception is replaced with new domain" => [
|
||||
"content" => '<script src="http://cdnapi.kaltura.com/p/730212/sp/73021200/embedIframeJs">',
|
||||
"domain" => 'cdnapisec.kaltura.com',
|
||||
"outputregex" => '/UPDATE/',
|
||||
],
|
||||
"More params should not interfere" => [
|
||||
"content" => '<img alt="A picture" src="' . $this->getExternalTestFileUrl('/test.png', false) . '" width="1”><p style="font-size: \'20px\'"></p>',
|
||||
"outputregex" => '/UPDATE/',
|
||||
@ -156,11 +153,6 @@ class httpsreplace_test extends \advanced_testcase {
|
||||
"domain" => 'intentionally.unavailable',
|
||||
"expectedcount" => 1,
|
||||
],
|
||||
"Known supported domain should not be reported" => [
|
||||
"content" => '<iframe src="http://fe8be92ac963979368eca.r38.cf1.rackcdn.com/Helpful_ET_Websites_Apps_Resources.pdf">',
|
||||
"domain" => 'fe8be92ac963979368eca.r38.cf1.rackcdn.com',
|
||||
"expectedcount" => 0,
|
||||
],
|
||||
"Link should not be reported" => [
|
||||
"content" => '<a href="http://intentionally.unavailable/page.php">Link</a>',
|
||||
"domain" => 'intentionally.unavailable',
|
||||
@ -272,4 +264,35 @@ class httpsreplace_test extends \advanced_testcase {
|
||||
$this->assertNotContains('https://somesite', $testconf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test renamed domains
|
||||
*/
|
||||
public function test_renames() {
|
||||
global $DB, $CFG;
|
||||
$this->resetAfterTest();
|
||||
$this->expectOutputRegex('/UPDATE/');
|
||||
|
||||
$renames = [
|
||||
'example.com' => 'secure.example.com',
|
||||
];
|
||||
|
||||
set_config('renames', json_encode($renames), 'tool_httpsreplace');
|
||||
|
||||
$finder = new \tool_httpsreplace\url_finder();
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course((object) [
|
||||
'summary' => '<script src="http://example.com/test.js">',
|
||||
]);
|
||||
|
||||
$results = $finder->http_link_stats();
|
||||
$this->assertCount(0, $results);
|
||||
|
||||
$finder->upgrade_http_links();
|
||||
|
||||
$summary = $DB->get_field('course', 'summary', ['id' => $course->id]);
|
||||
$this->assertContains('https://secure.example.com', $summary);
|
||||
$this->assertNotContains('http://example.com', $summary);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,9 +24,6 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2017063000; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2016120500; // Requires this Moodle version.
|
||||
$plugin->release = '3.2.3';
|
||||
$plugin->version = 2017082500; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2017082400; // Requires this Moodle version.
|
||||
$plugin->component = 'tool_httpsreplace'; // Full name of the plugin (used for diagnostics).
|
||||
|
||||
$plugin->maturity = MATURITY_STABLE; // This version's maturity level.
|
||||
|
Loading…
x
Reference in New Issue
Block a user