Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * This script allows you to view and change the emailstop flag of any user.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2023 Stephan Robotta (stephan.robotta@bfh.ch)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__.'/../../config.php');
  29  require_once($CFG->libdir.'/clilib.php');
  30  
  31  // Define the input options.
  32  $longparams = [
  33      'email' => '',
  34      'help' => false,
  35      'id' => '',
  36      'quiet' => false,
  37      'stop' => '',
  38      'username' => '',
  39  ];
  40  
  41  $shortparams = [
  42      'e' => 'email',
  43      'h' => 'help',
  44      'i' => 'id',
  45      'q' => 'quiet',
  46      's' => 'stop',
  47      'u' => 'username',
  48  ];
  49  
  50  // Define exit codes.
  51  $exitsuccess = 0;
  52  $exitunknownoption = 1;
  53  $exitmissinguserarg = 2;
  54  $exittoomanyuserarg = 3;
  55  $exitnosearchargs = 4;
  56  $exitnousersfound = 5;
  57  $exitinvalidstopflag = 6;
  58  $exiterrordb = 7;
  59  
  60  // Now get cli options that are set by the caller.
  61  list($options, $unrecognized) = cli_get_params($longparams, $shortparams);
  62  
  63  $verbose = empty($options['quiet']);
  64  
  65  if ($unrecognized) {
  66      $unrecognized = implode("\n  ", $unrecognized);
  67      if ($verbose) {
  68          cli_error(get_string('cliunknowoption', 'admin', $unrecognized), $exitunknownoption);
  69      }
  70      exit($exitunknownoption);
  71  }
  72  
  73  if ($options['help']) {
  74      $help =
  75          "Set/unset or show status of emailstop flag for a user, identified by username or email.
  76  
  77  There are no security checks here because anybody who is able to
  78  execute this file may execute any PHP too.
  79  
  80  Options:
  81  -h, --help                    Print out this help
  82  -e, --email=email             Specify user by email, separate many users by comma
  83  -i, --id=id                   Specify user by id, separate many users by comma
  84  -q, --quiet                   No output to stdout
  85  -s, --stop=0|1|off|on         Set new value for emailstop flag
  86  -u, --username=username       Specify user by username, separate many users by comma
  87  
  88  Example:
  89  \$sudo -u www-data /usr/bin/php admin/cli/emailstop.php --email=student1@example.com --stop=1
  90  \$sudo -u www-data /usr/bin/php admin/cli/emailstop.php --email=student1@example.com,student2@example.com
  91  \$sudo -u www-data /usr/bin/php admin/cli/emailstop.php --u=student1,student2 -s=on
  92  ";
  93  
  94      echo $help;
  95      exit($exitsuccess);
  96  }
  97  
  98  $cntempty = 0;
  99  $cntfilled = 0;
 100  $searchargs = [];
 101  
 102  // Try to find out which option is used to fetch the users from. Also do sanitize etc.
 103  foreach (['email', 'username', 'id'] as $option) {
 104      if (empty($options[$option])) {
 105          $cntempty++;
 106      } else {
 107          $cntfilled++;
 108          $argname = $option;
 109          // The search args must be: split by the comma, trimmed, and empty elements filtered out.
 110          $searchargs = array_flip(array_filter(
 111              array_map(
 112                  function ($item) {
 113                      return trim($item);
 114                  },
 115                  explode(',', $options[$option])
 116              ),
 117              function ($item) {
 118                  return $item !== '';
 119              }
 120          ));
 121      }
 122  }
 123  if ($cntempty === 3) {
 124      if ($verbose) {
 125          cli_error('One of username, email, or id must be set.', $exitmissinguserarg);
 126      }
 127      exit($exitmissinguserarg);
 128  }
 129  if ($cntfilled > 1) {
 130      if ($verbose) {
 131          cli_error('Only one of email, username, or id can be set to identify a user.', $exittoomanyuserarg);
 132      }
 133      exit($exittoomanyuserarg);
 134  }
 135  if (empty($searchargs)) {
 136      if ($verbose) {
 137          cli_error('No values are provided for users.', $exitnosearchargs);
 138      }
 139      exit($exitnosearchargs);
 140  }
 141  try {
 142      $users = $DB->get_records_list('user', $argname, array_keys($searchargs));
 143  } catch (Exception $e) {
 144      if ($verbose) {
 145          cli_error("Could not fetch data from db by {$argname}: '{$options[$argname]}'.", $exiterrordb);
 146      }
 147      exit($exiterrordb);
 148  }
 149  if (empty($users)) {
 150      if ($verbose) {
 151          cli_error("Can not find any user by {$argname}: '{$options[$argname]}'.", $exitnousersfound);
 152      }
 153      exit($exitnousersfound);
 154  }
 155  
 156  // No stop flag set, then just print the user and the current emailstop flag state.
 157  if ($options['stop'] === '') {
 158      foreach ($users as $user) {
 159          if ($verbose) {
 160              echo 'user=' . $user->{$argname} . ' - emailstop=' . (int)$user->emailstop . PHP_EOL;
 161              unset($searchargs[$user->{$argname}]);
 162          }
 163      }
 164      if ($verbose) {
 165          foreach (array_keys($searchargs) as $arg) {
 166              echo 'user=' . $arg . ' - not found' . PHP_EOL;
 167          }
 168      }
 169      exit($exitsuccess);
 170  }
 171  
 172  // Allowed values for the stop flag enabled are 1 and on, for disabled are 0 and off.
 173  $validvalues = ['0', '1', 'off', 'on'];
 174  $stopflag = strtolower($options['stop']);
 175  if (!in_array($stopflag, $validvalues)) {
 176      if ($verbose) {
 177          cli_error('Value for the emailstop flag must be one of: ' . implode(', ', $validvalues) . '.', $exitinvalidstopflag);
 178      }
 179      exit($exitinvalidstopflag);
 180  }
 181  
 182  foreach ($validvalues as $value) {
 183      if ($value === $stopflag) {
 184          $stopflag = ($value === '1' || $value === 'on') ? 1 : 0;
 185          break;
 186      }
 187  }
 188  // Update each user with the stop flag to be set if it is necessary.
 189  foreach ($users as $user) {
 190      $line = 'Update user ' . $user->{$argname} . ($argname !== 'id' ? ' (' . $user->id . ')' : '') . ' - ';
 191      if ((int)$user->emailstop !== $stopflag) {
 192          $DB->set_field('user', 'emailstop', $stopflag, ['id' => $user->id]);
 193          $line .= 'ok';
 194      } else {
 195          $line .= 'already done';
 196      }
 197      if ($verbose) {
 198          echo $line . PHP_EOL;
 199          unset($searchargs[$user->{$argname}]);
 200      }
 201  
 202  }
 203  if ($verbose) {
 204      foreach (array_keys($searchargs) as $arg) {
 205          echo 'user=' . $arg . ' - not found' . PHP_EOL;
 206      }
 207  }
 208  exit($exitsuccess);
 209