diff --git a/auth/mnet/auth.php b/auth/mnet/auth.php index 3f611062f1e..c8f5800cdcc 100644 --- a/auth/mnet/auth.php +++ b/auth/mnet/auth.php @@ -115,7 +115,7 @@ class auth_plugin_mnet extends auth_plugin_base { $userdata['session.gc_maxlifetime'] = ini_get('session.gc_maxlifetime'); $userdata['picture'] = $user->picture; if (!empty($user->picture)) { - $imagefile = "{$CFG->dataroot}/users/{$user->id}/f1.jpg"; + $imagefile = make_user_directory($user->id, true) . "/f1.jpg"; if (file_exists($imagefile)) { $userdata['imagehash'] = sha1(file_get_contents($imagefile)); } @@ -319,7 +319,7 @@ class auth_plugin_mnet extends auth_plugin_base { // TODO: fetch image if it has changed if ($key == 'imagehash') { - $dirname = "{$CFG->dataroot}/users/{$localuser->id}"; + $dirname = make_user_directory($localuser->id, true); $filename = "$dirname/f1.jpg"; $localhash = ''; @@ -1263,8 +1263,8 @@ class auth_plugin_mnet extends auth_plugin_base { global $CFG; if ($user = get_record('user', 'username', addslashes($username), 'mnethostid', $CFG->mnet_localhost_id)) { - $filename1 = "{$CFG->dataroot}/users/{$user->id}/f1.jpg"; - $filename2 = "{$CFG->dataroot}/users/{$user->id}/f2.jpg"; + $filename1 = make_user_directory($user->id, true) . "/f1.jpg"; + $filename2 = make_user_directory($user->id, true) . "/f2.jpg"; $return = array(); if (file_exists($filename1)) { $return['f1'] = base64_encode(file_get_contents($filename1)); diff --git a/enrol/imsenterprise/enrol.php b/enrol/imsenterprise/enrol.php index 56fbc89c627..416073d5599 100644 --- a/enrol/imsenterprise/enrol.php +++ b/enrol/imsenterprise/enrol.php @@ -669,7 +669,7 @@ function process_person_tag($tagcontents){ $person->picture = 1; //Llibreria creada per nosaltres mateixos. require_once($CFG->dirroot.'/lib/gdlib.php'); - if ($usernew->picture = save_profile_image($id, $person->urlphoto,'users', true)) { + if ($usernew->picture = save_profile_image($id, $person->urlphoto,'user')) { set_field('user', 'picture', $usernew->picture, 'id', $id); /// Note picture in DB } } diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index 32315e76fed..9c931b7e384 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -1096,6 +1096,7 @@ $string['numwords'] = '$a words'; $string['numyears'] = '$a years'; $string['ok'] = 'OK'; $string['oldpassword'] = 'Current Password'; +$string['olduserdirectory'] = 'This is the OLD users directory, and is no longer needed. \r\nYou may safely delete it. \r\nThe files it contains have been copied to the NEW user directory.'; $string['opentoguests'] = 'Guest access'; $string['optional'] = 'optional'; $string['order'] = 'Order'; diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index dcd11c46809..7d97d4a23d8 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2547,8 +2547,47 @@ function xmldb_main_upgrade($oldversion=0) { upgrade_main_savepoint($result, 2007100903); } + + if ($result && $oldversion < 2007101101) { + // Get list of users by browsing moodledata/user + $oldusersdir = $CFG->dataroot . '/users'; + $folders = get_directory_list($oldusersdir, '', false, true, false); + + foreach ($folders as $userid) { + $olddir = $oldusersdir . '/' . $userid; + // Create new user directory + if (!$newdir = make_user_directory($userid)) { + $result = false; + break; + } + // Move contents of old directory to new one + if (file_exists($olddir) && file_exists($newdir)) { + $files = get_directory_list($olddir); + foreach ($files as $file) { + copy($olddir . '/' . $file, $newdir . '/' . $file); + } + } else { + notify("Could not move the contents of $olddir into $newdir!"); + $result = false; + break; + } + } + + // Leave a README in old users directory + $readmefilename = $oldusersdir . '/README.txt'; + if ($handle = fopen($readmefilename, 'w+b')) { + if (!fwrite($handle, get_string('olduserdirectory'))) { + // Could not write to the readme file. No cause for huge concern + notify("Could not write to the README.txt file in $readmefilename."); + } + fclose($handle); + } else { + // Could not create the readme file. No cause for huge concern + notify("Could not create the README.txt file in $readmefilename."); + } + } return $result; } diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 9d5eb092388..bd65e33b2f3 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -2,7 +2,7 @@ /* * This file is used for special upgrade functions - for example groups and gradebook. - * These functions must use SQL and dabase related functions only- no other Moodle API, + * These functions must use SQL and database related functions only- no other Moodle API, * because it might depend on db structures that are not yet present during upgrade. * (Do not use functions from accesslib.php, grades classes or group functions at all!) */ diff --git a/lib/gdlib.php b/lib/gdlib.php index 37e53591e5f..9e39ad5835d 100644 --- a/lib/gdlib.php +++ b/lib/gdlib.php @@ -96,12 +96,10 @@ function delete_profile_image($id, $dir='users') { * * @param int $id user or group id * @param object $uploadmanager object referencing the image - * @param string $dir type of entity - groups, users, ... + * @param string $dir type of entity - groups, user, ... * @return boolean success */ -function save_profile_image($id, $uploadmanager, $dir='users') { -// - +function save_profile_image($id, $uploadmanager, $dir='user') { global $CFG; if (empty($CFG->gdversion)) { @@ -115,18 +113,23 @@ function save_profile_image($id, $uploadmanager, $dir='users') { umask(0000); if (!file_exists($CFG->dataroot .'/'. $dir)) { - if (! mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) { + if (!mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) { + return false; + } + } + + if ($dir == 'user') { + $destination = make_user_directory($id, true); + } else { + $destination = "$CFG->dataroot/$dir/$id"; + } + + if (!file_exists($destination)) { + if (!mkdir($destination, $CFG->directorypermissions)) { return false; } } - if (!file_exists($CFG->dataroot .'/'. $dir .'/'. $id)) { - if (! mkdir($CFG->dataroot .'/'. $dir .'/'. $id, $CFG->directorypermissions)) { - return false; - } - } - - $destination = $CFG->dataroot .'/'. $dir .'/'. $id; if (!$uploadmanager->save_files($destination)) { return false; } @@ -202,12 +205,12 @@ function save_profile_image($id, $uploadmanager, $dir='users') { ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2); if (function_exists('ImageJpeg')) { - @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg'); // Helps in Safe mode - @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg'); // Helps in Safe mode - if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and - ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) { - @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666); - @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666); + @touch($destination .'/f1.jpg'); // Helps in Safe mode + @touch($destination .'/f2.jpg'); // Helps in Safe mode + if (ImageJpeg($im1, $destination .'/f1.jpg', 90) and + ImageJpeg($im2, $destination .'/f2.jpg', 95) ) { + @chmod($destination .'/f1.jpg', 0666); + @chmod($destination .'/f2.jpg', 0666); return 1; } } else { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 33634e6b2ec..59fecc8c9b1 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.org // // // -// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // +// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -4124,6 +4124,35 @@ function make_mod_upload_directory($courseid) { return $moddata; } +/** + * Makes a directory for a particular user. + * + * @uses $CFG + * @param int $userid The id of the user in question - maps to id field of 'user' table. + * @param bool $test Whether we are only testing the return value (do not create the directory) + * @return string|false Returns full path to directory if successful, false if not + */ +function make_user_directory($userid, $test=false) { + global $CFG; + + if (is_bool($userid) || $userid < 0 || !ereg('^[0-9]{1,10}$', $userid) || $userid > 2147483647) { + if (!$test) { + notify("Given userid was not a valid integer! (" . gettype($userid) . " $userid)"); + } + return false; + } + + // Generate a two-level path for the userid. First level groups them by slices of 1000 users, second level is userid + $level1 = floor($userid / 1000) * 1000; + + $userdir = "user/$level1/$userid"; + if ($test) { + return $CFG->dataroot . '/' . $userdir; + } else { + return make_upload_directory($userdir); + } +} + /** * Returns current name of file on disk if it exists. * diff --git a/lib/simpletest/testmoodlelib.php b/lib/simpletest/testmoodlelib.php index f2b8b74cc47..7a33665a10e 100644 --- a/lib/simpletest/testmoodlelib.php +++ b/lib/simpletest/testmoodlelib.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.org // // // -// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // +// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -179,6 +179,25 @@ class moodlelib_test extends UnitTestCase { '#()*#,9789\\\'\".,'); } + + function test_make_user_directory() { + global $CFG; + + // Test success conditions + $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true)); + $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true)); + $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true)); + $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true)); + $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible + + // Test fail conditions + $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary + $this->assertFalse(make_user_directory(-1, true)); + $this->assertFalse(make_user_directory('string', true)); + $this->assertFalse(make_user_directory(false, true)); + $this->assertFalse(make_user_directory(true, true)); + + } } ?> diff --git a/user/editlib.php b/user/editlib.php index df0019b8afe..a48ea664080 100644 --- a/user/editlib.php +++ b/user/editlib.php @@ -23,10 +23,10 @@ function useredit_update_picture(&$usernew, &$userform) { global $CFG; if (isset($usernew->deletepicture) and $usernew->deletepicture) { - $location = $CFG->dataroot.'/users/'.$usernew->id; + $location = make_user_directory($usernew->id, true); @remove_dir($location); set_field('user', 'picture', 0, 'id', $usernew->id); - } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'users')) { + } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'user')) { set_field('user', 'picture', 1, 'id', $usernew->id); } } diff --git a/user/pix.php b/user/pix.php index b3435be9c8a..d909551eaff 100644 --- a/user/pix.php +++ b/user/pix.php @@ -18,10 +18,10 @@ if (count($args) == 2) { $userid = (integer)$args[0]; $image = $args[1]; - $pathname = $CFG->dataroot.'/users/'.$userid.'/'.$image; + $pathname = make_user_directory($userid, true) . "/$image"; if (file_exists($pathname) and !is_dir($pathname)) { send_file($pathname, $image); - } + } } // picture was deleted - use default instead diff --git a/version.php b/version.php index 9ca8761aa8b..b927d5a6120 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2007101000; // YYYYMMDD = date + $version = 2007101100; // YYYYMMDD = date // XY = increments within a single day $release = '2.0 dev'; // Human-friendly version name