Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * url_replace cli script. Examines DB for non-https src or data links, and lists broken ones or replaces all links.
  19   *
  20   * @package    tool_httpsreplace
  21   * @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  require(__DIR__ . '/../../../../config.php');
  27  require_once($CFG->libdir.'/clilib.php');
  28  
  29  list($options, $unrecognized) = cli_get_params(
  30      array(
  31          'help' => false,
  32          'list' => false,
  33          'replace' => false,
  34          'confirm' => false,
  35      ),
  36      array(
  37          'h' => 'help',
  38          'l' => 'list',
  39          'r' => 'replace',
  40      )
  41  );
  42  if ($unrecognized) {
  43      $unrecognized = implode("\n  ", $unrecognized);
  44      cli_error(get_string('cliunknowoption', 'admin', $unrecognized), 2);
  45  }
  46  if ($options['help'] || (!$options['list'] && !$options['replace'])) {
  47      $help = "Examines DB for non-https src or data links, and lists broken links or replaces all links.
  48  Options:
  49  -h, --help            Print out this help
  50  -l, --list            List of http (not https) urls on a site in the DB that would become broken.
  51  -r, --replace         List of http (not https) urls on a site in the DB that would become broken.
  52  --confirm             Replaces http urls with https across a site's content.
  53  Example:
  54  \$ sudo -u www-data /usr/bin/php admin/tool/httpsreplace/cli/url_replace.php --list \n";
  55      echo $help;
  56      exit(0);
  57  }
  58  
  59  if (!$DB->replace_all_text_supported()) {
  60      echo $OUTPUT->notification(get_string('notimplemented', 'tool_httpsreplace'));
  61      exit(1);
  62  }
  63  
  64  if (!is_https()) {
  65      echo $OUTPUT->notification(get_string('httpwarning', 'tool_httpsreplace'), 'warning');
  66      echo "\n";
  67  }
  68  
  69  if ($options['replace']) {
  70  
  71      if ($options['confirm']) {
  72  
  73          $urlfinder = new \tool_httpsreplace\url_finder();
  74          $urlfinder->upgrade_http_links();
  75      } else {
  76          echo "Once this is tool run, changes made can't be reverted. \n" .
  77               "A complete backup should be made before running this script. \n\n" .
  78               "There is a low risk that the wrong content will be replaced, introducing problems. \n" .
  79               "If you are sure you want to continue, add --confirm\n\n";
  80      }
  81  
  82  } else {
  83  
  84      $urlfinder = new \tool_httpsreplace\url_finder();
  85      $results = $urlfinder->http_link_stats();
  86      asort($results);
  87      $fp = fopen('php://stdout', 'w');
  88      fputcsv($fp, ['clientsite', 'httpdomain', 'urlcount']);
  89      foreach ($results as $domain => $count) {
  90          fputcsv($fp, [$SITE->shortname, $domain, $count]);
  91      }
  92      fclose($fp);
  93  }