Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-helper
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Helper class for anonymization of data
  27   *
  28   * This functions includes a collection of methods that are invoked
  29   * from the backup process when anonymization services have been
  30   * requested.
  31   *
  32   * The name of each method must be "process_parentname_name", as defined
  33   * byt the @anonymizer_final_element final element class, where
  34   * parentname is the name ob the parent tag and name the name of the tag
  35   * contents to be anonymized (i.e. process_user_username) with one param
  36   * being the value to anonymize.
  37   *
  38   * Note: current implementation of anonymization is pretty simple, just some
  39   * sequential values are used. If we want more elaborated generation, it
  40   * can be replaced later (using generators or wathever). Don't forget we must
  41   * ensure some fields (username, idnumber, email) are unique always.
  42   *
  43   * TODO: Improve to use more advanced anonymization
  44   *
  45   * TODO: Finish phpdocs
  46   */
  47  class backup_anonymizer_helper {
  48  
  49      /**
  50       * Determine if the given user is an 'anonymous' user, based on their username, firstname, lastname
  51       * and email address.
  52       * @param stdClass $user the user record to test
  53       * @return bool true if this is an 'anonymous' user
  54       */
  55      public static function is_anonymous_user($user) {
  56          if (preg_match('/^anon\d*$/', $user->username)) {
  57              $match = preg_match('/^anonfirstname\d*$/', $user->firstname);
  58              $match = $match && preg_match('/^anonlastname\d*$/', $user->lastname);
  59              // Check .com for backwards compatibility.
  60              $emailmatch = preg_match('/^anon\d*@doesntexist\.com$/', $user->email) ||
  61                  preg_match('/^anon\d*@doesntexist\.invalid$/', $user->email);
  62              if ($match && $emailmatch) {
  63                  return true;
  64              }
  65          }
  66          return false;
  67      }
  68  
  69      public static function process_user_auth($value) {
  70          return 'manual'; // Set them to manual always
  71      }
  72  
  73      public static function process_user_username($value) {
  74          static $counter = 0;
  75          $counter++;
  76          return 'anon' . $counter; // Just a counter
  77      }
  78  
  79      public static function process_user_idnumber($value) {
  80          return ''; // Just blank it
  81      }
  82  
  83      public static function process_user_firstname($value) {
  84          static $counter = 0;
  85          $counter++;
  86          return 'anonfirstname' . $counter; // Just a counter
  87      }
  88  
  89      public static function process_user_lastname($value) {
  90          static $counter = 0;
  91          $counter++;
  92          return 'anonlastname' . $counter; // Just a counter
  93      }
  94  
  95      public static function process_user_email($value) {
  96          static $counter = 0;
  97          $counter++;
  98          return 'anon' . $counter . '@doesntexist.invalid'; // Just a counter.
  99      }
 100  
 101      public static function process_user_phone1($value) {
 102          return ''; // Clean phone1
 103      }
 104  
 105      public static function process_user_phone2($value) {
 106          return ''; // Clean phone2
 107      }
 108  
 109      public static function process_user_institution($value) {
 110          return ''; // Clean institution
 111      }
 112  
 113      public static function process_user_department($value) {
 114          return ''; // Clean department
 115      }
 116  
 117      public static function process_user_address($value) {
 118          return ''; // Clean address
 119      }
 120  
 121      public static function process_user_city($value) {
 122          return 'Perth'; // Set city
 123      }
 124  
 125      public static function process_user_country($value) {
 126          return 'AU'; // Set country
 127      }
 128  
 129      public static function process_user_lastip($value) {
 130          return '127.0.0.1'; // Set lastip to localhost
 131      }
 132  
 133      public static function process_user_picture($value) {
 134          return 0; // No picture
 135      }
 136  
 137      public static function process_user_description($value) {
 138          return ''; // No user description
 139      }
 140  
 141      public static function process_user_descriptionformat($value) {
 142          return 0; // Format moodle
 143      }
 144  
 145      public static function process_user_imagealt($value) {
 146          return ''; // No user imagealt
 147      }
 148  
 149      /**
 150       * Anonymises user's phonetic name field
 151       * @param string $value value of the user field
 152       * @return string anonymised phonetic name
 153       */
 154      public static function process_user_firstnamephonetic($value) {
 155          static $counter = 0;
 156          $counter++;
 157          return 'anonfirstnamephonetic' . $counter; // Just a counter.
 158      }
 159  
 160      /**
 161       * Anonymises user's phonetic last name field
 162       * @param string $value value of the user field
 163       * @return string anonymised last phonetic name
 164       */
 165      public static function process_user_lastnamephonetic($value) {
 166          static $counter = 0;
 167          $counter++;
 168          return 'anonlastnamephonetic' . $counter; // Just a counter.
 169      }
 170  
 171      /**
 172       * Anonymises user's middle name field
 173       * @param string $value value of the user field
 174       * @return string anonymised middle name
 175       */
 176      public static function process_user_middlename($value) {
 177          static $counter = 0;
 178          $counter++;
 179          return 'anonmiddlename' . $counter; // Just a counter.
 180      }
 181  
 182      /**
 183       * Anonymises user's alternate name field
 184       * @param string $value value of the user field
 185       * @return string anonymised alternate name
 186       */
 187      public static function process_user_alternatename($value) {
 188          static $counter = 0;
 189          $counter++;
 190          return 'anonalternatename' . $counter; // Just a counter.
 191      }
 192  }