MDL-31484 backup - repeated restores with anonymised users now works correctly
[moodle.git] / backup / util / helper / backup_anonymizer_helper.class.php
CommitLineData
69dd0c8c
EL
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 */
47class backup_anonymizer_helper {
48
1d8066e2
DS
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 $match = $match && preg_match('/^anon\d*@doesntexist\.com$/', $user->email);
60 if ($match) {
61 return true;
62 }
63 }
64 return false;
65 }
66
69dd0c8c
EL
67 public static function process_user_auth($value) {
68 return 'manual'; // Set them to manual always
69 }
70
71 public static function process_user_username($value) {
72 static $counter = 0;
73 $counter++;
74 return 'anon' . $counter; // Just a counter
75 }
76
77 public static function process_user_idnumber($value) {
78 return ''; // Just blank it
79 }
80
81 public static function process_user_firstname($value) {
82 static $counter = 0;
83 $counter++;
84 return 'anonfirstname' . $counter; // Just a counter
85 }
86
87 public static function process_user_lastname($value) {
88 static $counter = 0;
89 $counter++;
90 return 'anonlastname' . $counter; // Just a counter
91 }
92
93 public static function process_user_email($value) {
94 static $counter = 0;
95 $counter++;
96 return 'anon' . $counter . '@doesntexist.com'; // Just a counter
97 }
98
482aac65
EL
99 public static function process_user_icq($value) {
100 return ''; // Clean icq
101 }
102
103 public static function process_user_skype($value) {
104 return ''; // Clean skype
105 }
106
107 public static function process_user_yahoo($value) {
108 return ''; // Clean yahoo
109 }
110
111 public static function process_user_aim($value) {
112 return ''; // Clean aim
113 }
114
115 public static function process_user_msn($value) {
116 return ''; // Clean msn
117 }
118
119 public static function process_user_phone1($value) {
120 return ''; // Clean phone1
121 }
122
123 public static function process_user_phone2($value) {
124 return ''; // Clean phone2
125 }
126
127 public static function process_user_institution($value) {
128 return ''; // Clean institution
129 }
130
131 public static function process_user_department($value) {
132 return ''; // Clean department
133 }
134
135 public static function process_user_address($value) {
136 return ''; // Clean address
137 }
138
139 public static function process_user_city($value) {
140 return 'Perth'; // Set city
141 }
142
143 public static function process_user_country($value) {
144 return 'AU'; // Set country
145 }
146
69dd0c8c
EL
147 public static function process_user_lastip($value) {
148 return '127.0.0.1'; // Set lastip to localhost
149 }
150
151 public static function process_user_picture($value) {
152 return 0; // No picture
153 }
154
155 public static function process_user_url($value) {
156 return ''; // No url
157 }
158
159 public static function process_user_description($value) {
160 return ''; // No user description
161 }
162
c44d4aee
EL
163 public static function process_user_descriptionformat($value) {
164 return 0; // Format moodle
165 }
166
69dd0c8c
EL
167 public static function process_user_imagealt($value) {
168 return ''; // No user imagealt
169 }
170}