Esempio n. 1
0
/**
 * Create links between users and image files from the users profile_pictures folder
 */
function create_profile_picture_links()
{
    global $DB;
    load_class('files/model/_filelist.class.php', 'Filelist');
    load_class('files/model/_fileroot.class.php', 'FileRoot');
    $path = 'profile_pictures';
    $FileRootCache =& get_FileRootCache();
    $UserCache =& get_UserCache();
    // SQL query to get all users and limit by page below
    $users_SQL = new SQL();
    $users_SQL->SELECT('*');
    $users_SQL->FROM('T_users');
    $users_SQL->ORDER_BY('user_ID');
    $page = 0;
    $page_size = 100;
    while (count($UserCache->cache) > 0 || $page == 0) {
        // Load users by 100 at one time to avoid errors about memory exhausting
        $users_SQL->LIMIT($page * $page_size . ', ' . $page_size);
        $UserCache->clear();
        $UserCache->load_by_sql($users_SQL);
        while (($iterator_User =& $UserCache->get_next()) != NULL) {
            // Iterate through UserCache)
            $FileRootCache->clear();
            $user_FileRoot =& $FileRootCache->get_by_type_and_ID('user', $iterator_User->ID);
            if (!$user_FileRoot) {
                // User FileRoot doesn't exist
                continue;
            }
            $ads_list_path = get_canonical_path($user_FileRoot->ads_path . $path);
            // Previously uploaded avatars
            if (!is_dir($ads_list_path)) {
                // profile_picture folder doesn't exists in the user root dir
                continue;
            }
            $user_avatar_Filelist = new Filelist($user_FileRoot, $ads_list_path);
            $user_avatar_Filelist->load();
            if ($user_avatar_Filelist->count() > 0) {
                // profile_pictures folder is not empty
                $info_content = '';
                $LinkOwner = new LinkUser($iterator_User);
                while ($lFile =& $user_avatar_Filelist->get_next()) {
                    // Loop through all Files:
                    $fileName = $lFile->get_name();
                    if (process_filename($fileName)) {
                        // The file has invalid file name, don't create in the database
                        // TODO: asimo> we should collect each invalid file name here, and send an email to the admin
                        continue;
                    }
                    $lFile->load_meta(true);
                    if ($lFile->is_image()) {
                        $lFile->link_to_Object($LinkOwner);
                    }
                }
            }
        }
        // Increase page number to get next portion of users
        $page++;
    }
    // Clear cache data
    $UserCache->clear();
    $FileRootCache->clear();
}
Esempio n. 2
0
     // We have EXITed already at this point!!
     break;
 case 'link_user':
     // TODO: We don't need the Filelist, move UP!
     // Check that this action request is not a CSRF hacked request:
     $Session->assert_received_crumb('file');
     // Link File to User:
     if (!isset($edited_User)) {
         // No User to link to
         $fm_mode = NULL;
         // not really needed but just  n case...
         break;
     }
     // Permission HAS been checked on top of controller!
     // Get the file we want to link:
     if (!$selected_Filelist->count()) {
         $Messages->add(T_('Nothing selected.'), 'error');
         break;
     }
     $edited_File =& $selected_Filelist->get_by_idx(0);
     // Load meta data AND MAKE SURE IT IS CREATED IN DB:
     $edited_File->load_meta(true);
     // Check a file for min size
     $min_size = $Settings->get('min_picture_size');
     $image_sizes = $edited_File->get_image_size('widthheight');
     if ($image_sizes[0] < $min_size || $image_sizes[1] < $min_size) {
         // Don't use this file as profile picture because it has small sizes
         $Messages->add(sprintf(T_('Your profile picture must have a minimum size of %dx%d pixels.'), $min_size, $min_size), 'error');
         break;
     }
     // Link file to user