Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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_icq($value) {
 102          return ''; // Clean icq
 103      }
 104  
 105      public static function process_user_skype($value) {
 106          return ''; // Clean skype
 107      }
 108  
 109      public static function process_user_yahoo($value) {
 110          return ''; // Clean yahoo
 111      }
 112  
 113      public static function process_user_aim($value) {
 114          return ''; // Clean aim
 115      }
 116  
 117      public static function process_user_msn($value) {
 118          return ''; // Clean msn
 119      }
 120  
 121      public static function process_user_phone1($value) {
 122          return ''; // Clean phone1
 123      }
 124  
 125      public static function process_user_phone2($value) {
 126          return ''; // Clean phone2
 127      }
 128  
 129      public static function process_user_institution($value) {
 130          return ''; // Clean institution
 131      }
 132  
 133      public static function process_user_department($value) {
 134          return ''; // Clean department
 135      }
 136  
 137      public static function process_user_address($value) {
 138          return ''; // Clean address
 139      }
 140  
 141      public static function process_user_city($value) {
 142          return 'Perth'; // Set city
 143      }
 144  
 145      public static function process_user_country($value) {
 146          return 'AU'; // Set country
 147      }
 148  
 149      public static function process_user_lastip($value) {
 150          return '127.0.0.1'; // Set lastip to localhost
 151      }
 152  
 153      public static function process_user_picture($value) {
 154          return 0; // No picture
 155      }
 156  
 157      public static function process_user_url($value) {
 158          return ''; // No url
 159      }
 160  
 161      public static function process_user_description($value) {
 162          return ''; // No user description
 163      }
 164  
 165      public static function process_user_descriptionformat($value) {
 166          return 0; // Format moodle
 167      }
 168  
 169      public static function process_user_imagealt($value) {
 170          return ''; // No user imagealt
 171      }
 172  
 173      /**
 174       * Anonymises user's phonetic name field
 175       * @param string $value value of the user field
 176       * @return string anonymised phonetic name
 177       */
 178      public static function process_user_firstnamephonetic($value) {
 179          static $counter = 0;
 180          $counter++;
 181          return 'anonfirstnamephonetic' . $counter; // Just a counter.
 182      }
 183  
 184      /**
 185       * Anonymises user's phonetic last name field
 186       * @param string $value value of the user field
 187       * @return string anonymised last phonetic name
 188       */
 189      public static function process_user_lastnamephonetic($value) {
 190          static $counter = 0;
 191          $counter++;
 192          return 'anonlastnamephonetic' . $counter; // Just a counter.
 193      }
 194  
 195      /**
 196       * Anonymises user's middle name field
 197       * @param string $value value of the user field
 198       * @return string anonymised middle name
 199       */
 200      public static function process_user_middlename($value) {
 201          static $counter = 0;
 202          $counter++;
 203          return 'anonmiddlename' . $counter; // Just a counter.
 204      }
 205  
 206      /**
 207       * Anonymises user's alternate name field
 208       * @param string $value value of the user field
 209       * @return string anonymised alternate name
 210       */
 211      public static function process_user_alternatename($value) {
 212          static $counter = 0;
 213          $counter++;
 214          return 'anonalternatename' . $counter; // Just a counter.
 215      }
 216  }