Merge branch 'wip-mdl-29001-m19' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / admin / fixuserpix.php
blobe556023190f98fd0cccddee67b106ad44f409280
1 <?php
3 require_once('../config.php');
4 /**
5 * SCRIPT CONFIGURATION
6 */
7 $starttimer = time()+microtime();
9 $settings = array();
10 $settings['verbose'] = false;
11 $settings['username'] = null;
12 $settings['password'] = null;
13 $settings['eolchar'] = '<br />'; // Character used to break lines
15 // Argument arrays: 0=>short name, 1=>long name
16 $arguments = array(
17 array('short'=>'u', 'long'=>'username', 'help' => 'Your moodle username', 'type'=>'STRING', 'default' => ''),
18 array('short'=>'pw', 'long'=>'password', 'help' => 'Your moodle password', 'type'=>'STRING', 'default' => ''),
19 array('short'=>'v', 'long' => 'verbose', 'help' => 'Display extra information about the process')
22 // Building the USAGE output of the command line version
23 if (isset($argv) && isset($argc)) {
24 $help = "Moodle User Pix Fix. Restores user profile images that were not properly moved during 1.8.2 upgrade to newer versions.\n\n"
25 . "Usage: {$argv[0]}; [OPTION] ...\n"
26 . "Options:\n"
27 . " -h, -?, -help, --help This output\n";
29 foreach ($arguments as $arg_array) {
30 $equal = '';
31 if (!empty($arg_array['type'])) {
32 $equal = "={$arg_array['type']}";
35 $padding1 = 5 - strlen($arg_array['short']);
36 $padding2 = 30 - (strlen($arg_array['long']) + strlen($equal));
37 $paddingstr1 = '';
38 for ($i = 0; $i < $padding1; $i++) {
39 $paddingstr1 .= ' ';
41 $paddingstr2 = '';
42 for ($i = 0; $i < $padding2; $i++) {
43 $paddingstr2 .= ' ';
46 $help .= " -{$arg_array['short']},$paddingstr1--{$arg_array['long']}$equal$paddingstr2{$arg_array['help']}\n";
49 $help .= "\nEmail nicolasconnault@gmail.com for any suggestions or bug reports.\n";
51 if ($argc == 1 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
52 echo $help;
53 die();
55 } else {
57 $settings['eolchar'] = "\n";
58 $argv = arguments($argv);
59 $argscount = 0;
61 foreach ($arguments as $arg_array) {
62 $value = null;
63 if (in_array($arg_array['short'], array_keys($argv))) {
64 $value = $argv[$arg_array['short']];
65 unset($argv[$arg_array['short']]);
66 } elseif (in_array($arg_array['long'], array_keys($argv))) {
67 $value = $argv[$arg_array['long']];
68 unset($argv[$arg_array['long']]);
70 if (!is_null($value)) {
71 $settings[$arg_array['long']] = $value;
72 $argscount++;
76 // If some params are left in argv, it means they are not supported
77 if ($argscount == 0 || count($argv) > 0) {
78 echo $help;
79 die();
84 /**
85 * SCRIPT SETUP
87 require_once($CFG->libdir . '/formslib.php');
88 require_once($CFG->dirroot .'/course/lib.php');
89 verbose("Loading libraries...");
90 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
92 /**
93 * WEB INTERFACE FORM
96 class pixfix_form extends moodleform {
97 function definition() {
98 global $arguments;
99 $mform =& $this->_form;
101 foreach ($arguments as $arg_array) {
102 $type = 'advcheckbox';
104 $label = ucfirst(str_replace('-', ' ', $arg_array['long']));
105 if (!empty($arg_array['type'])) {
106 $type = 'text';
109 if ($arg_array['long'] == 'password' || $arg_array['long'] == 'username') {
110 continue;
113 $mform->addElement($type, $arg_array['long'], $label);
115 if (isset($arg_array['default'])) {
116 $mform->setDefault($arg_array['long'], $arg_array['default']);
119 $this->add_action_buttons(false, 'Restore Images');
122 function definition_after_data() {
127 $run_script = true;
128 $web_interface = false;
130 // If eolchar is still <br />, load the web interface
131 if ($settings['eolchar'] == '<br />') {
132 print_header("User Pix-Fix");
133 print_heading("User Pix-Fix");
134 $mform = new pixfix_form();
136 if ($data = $mform->get_data(false)) {
137 foreach ($arguments as $arg_array) {
138 if (!empty($data->{$arg_array['long']})) {
139 $settings[$arg_array['long']] = $data->{$arg_array['long']};
142 } else {
143 $run_script = false;
146 if (!has_capability('moodle/site:doanything', $systemcontext)) {
147 // If not logged in, give link to login page for current site
148 notify("You must be logged in as administrator before using this script.");
149 require_login();
150 } else {
151 $mform->display();
154 $web_interface = true;
157 if ($run_script) {
159 // User authentication
160 if (!$web_interface) {
161 if (empty($settings['username'])) {
162 echo "You must enter a valid username for a moodle administrator account on this site.{$settings['eolchar']}";
163 die();
164 } elseif (empty($settings['password'])) {
165 echo "You must enter a valid password for a moodle administrator account on this site.{$settings['eolchar']}";
166 die();
167 } else {
168 if (!$user = authenticate_user_login($settings['username'], $settings['password'])) {
169 echo "Invalid username or password!{$settings['eolchar']}";
170 die();
172 $USER = complete_user_login($user);
173 if (!has_capability('moodle/site:doanything', $systemcontext)) {
174 echo "You do not have administration privileges on this Moodle site. These are required for running the restore script.{$settings['eolchar']}";
175 die();
180 // Script code here
182 // Look for old moodledata/users directory
183 $oldusersdir = $CFG->dataroot . '/users';
185 if (!file_exists($oldusersdir)) {
186 notify('The old directory for user profile images ('.$oldusersdir.') does not exist. Pictures cannot be restored!');
187 } else {
188 // Find user profile images that are not yet in the new directory
189 $folders = get_directory_list($oldusersdir, '', false, true, false);
191 $restored_count = 0;
193 foreach ($folders as $userid) {
194 $olddir = $oldusersdir . '/' . $userid;
195 $files = get_directory_list($olddir);
197 if (empty($files)) {
198 continue;
201 // Create new user directory
202 if (!$newdir = make_user_directory($userid)) {
203 // some weird directory - do not stop the upgrade, just ignore it
204 continue;
207 // Move contents of old directory to new one
208 if (file_exists($olddir) && file_exists($newdir)) {
209 $restored = false;
211 foreach ($files as $file) {
212 if (!file_exists($newdir . '/' . $file)) {
213 copy($olddir . '/' . $file, $newdir . '/' . $file);
214 verbose("Moved $olddir/$file into $newdir/$file");
215 $restored = true;
219 if ($restored) {
220 $restored_count++;
222 } else {
223 notify("Could not move the contents of $olddir into $newdir!");
224 $result = false;
225 break;
229 if ($settings['eolchar'] == '<br />') {
230 print_box_start('generalbox centerpara');
232 if ($restored_count > 0) {
233 echo "Successfully restored profile images for $restored_count users!" . $settings['eolchar'];
234 } else {
235 echo "Did not find any user profile images in need of restoring." . $settings['eolchar'];
237 if ($settings['eolchar'] == '<br />') {
238 print_box_end();
243 if ($settings['eolchar'] == '<br />') {
244 print_footer();
248 * Converts the standard $argv into an associative array taking var=val arguments into account
249 * @param array $argv
250 * @return array $_ARG
252 function arguments($argv) {
253 $_ARG = array();
254 foreach ($argv as $arg) {
255 if (ereg('--?([^=]+)=(.*)',$arg,$reg)) {
256 $_ARG[$reg[1]] = $reg[2];
257 } elseif(ereg('-([a-zA-Z0-9]+)',$arg,$reg)) {
258 $_ARG[$reg[1]] = 'true';
261 return $_ARG;
265 * If verbose is switched on, prints a string terminated by the global eolchar string.
266 * @param string $string The string to STDOUT
268 function verbose($string) {
269 global $settings;
270 if ($settings['verbose'] && !$settings['quiet']) {
271 echo $string . $settings['eolchar'];