Skip to content

Commit

Permalink
MDL-8605 New user directories implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasconnault committed Oct 11, 2007
1 parent 7f4ea06 commit 3d6d2b8
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 20 deletions.
8 changes: 4 additions & 4 deletions auth/mnet/auth.php
Expand Up @@ -115,7 +115,7 @@ function user_authorise($token, $useragent) {
$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));
}
Expand Down Expand Up @@ -319,7 +319,7 @@ function confirm_mnet_session($token, $remotewwwroot) {

// 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 = '';
Expand Down Expand Up @@ -1262,8 +1262,8 @@ function fetch_user_image($username) {
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));
Expand Down
2 changes: 1 addition & 1 deletion enrol/imsenterprise/enrol.php
Expand Up @@ -667,7 +667,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
}
}
Expand Down
1 change: 1 addition & 0 deletions lang/en_utf8/moodle.php
Expand Up @@ -1067,6 +1067,7 @@
$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';
Expand Down
44 changes: 44 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -700,5 +700,49 @@ function xmldb_main_upgrade($oldversion=0) {
return $result;

}

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;

?>
28 changes: 16 additions & 12 deletions lib/gdlib.php
Expand Up @@ -82,9 +82,7 @@ function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $
* @return boolean
* @todo Finish documenting this function
*/
function save_profile_image($id, $uploadmanager, $dir='users') {
//

function save_profile_image($id, $uploadmanager, $dir='user') {
global $CFG;

if (empty($CFG->gdversion)) {
Expand All @@ -103,13 +101,19 @@ function save_profile_image($id, $uploadmanager, $dir='users') {
}
}

if (!file_exists($CFG->dataroot .'/'. $dir .'/'. $id)) {
if (! mkdir($CFG->dataroot .'/'. $dir .'/'. $id, $CFG->directorypermissions)) {
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;
}
}

$destination = $CFG->dataroot .'/'. $dir .'/'. $id;

if (!$uploadmanager->save_files($destination)) {
return false;
}
Expand Down Expand Up @@ -185,12 +189,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 {
Expand Down
29 changes: 29 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -3799,6 +3799,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.
*
Expand Down
19 changes: 19 additions & 0 deletions lib/simpletest/testmoodlelib.php
Expand Up @@ -180,6 +180,25 @@ function test_clean_param()
'#()*#,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));

}
}

?>
4 changes: 2 additions & 2 deletions user/editlib.php
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion user/pix.php
Expand Up @@ -18,7 +18,7 @@
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);
}
Expand Down

0 comments on commit 3d6d2b8

Please sign in to comment.