/** * 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(); }
load_class('files/model/_filelist.class.php', 'FileList'); $selected_Filelist = new Filelist($fm_FileRoot, false); $fm_selected = param("fm_selected", "array"); // print_r($fm_selected); foreach ($fm_selected as $l_source_path) { // echo '<br>'.$l_source_path; $selected_Filelist->add_by_subpath(urldecode($l_source_path), true); } // make sure we have loaded metas for all files in selection! $selected_Filelist->load_meta(); // Ready to create post(s): load_class('items/model/_item.class.php', 'Item'); $fileNum = 0; $cat_Array = param("category", "array"); $title_Array = param("post_title", "array"); while ($l_File =& $selected_Filelist->get_next()) { // Create a post: $edited_Item = new Item(); $edited_Item->set('status', $item_status); // replacing category if selected at preview screen if (isset($cat_Array[$fileNum])) { // checking if selected "same as above" category option if ($cat_Array[$fileNum] != 'same') { // Use a selected category ID $edited_Item->set('main_cat_ID', $cat_Array[$fileNum]); } else { // Get a category ID from previous item $cat_Array[$fileNum] = $cat_Array[$fileNum - 1]; $edited_Item->set('main_cat_ID', $cat_Array[$fileNum - 1]); } } else {
/** * Rename the file in its current directory on disk. * * Also update meta data in DB. * * @access public * @param string new name (without path!) * @return boolean true on success, false on failure */ function rename_to($newname) { $old_file_name = $this->get_name(); // rename() will fail if newname already exists on windows // if it doesn't work that way on linux we need the extra check below // but then we have an integrity issue!! :( if (file_exists($this->_dir . $newname)) { syslog_insert(sprintf('File %s could not be renamed to %s', '<b>' . $old_file_name . '</b>', '<b>' . $newname . '</b>'), 'info', 'file', $this->ID); return false; } global $DB; $DB->begin(); $oldname = $this->get_name(); if ($this->is_dir()) { // modify folder content file paths in db $rel_dir = dirname($this->_rdfp_rel_path) . '/'; if ($rel_dir == './') { $rel_dir = ''; } $rel_dir = $rel_dir . $newname . '/'; $full_dir = $this->_dir . $newname . '/'; $temp_Filelist = new Filelist($this->_FileRoot, $this->_adfp_full_path); $temp_Filelist->load(); while ($temp_File = $temp_Filelist->get_next()) { $temp_File->modify_path($rel_dir, $full_dir); } } if (!@rename($this->_adfp_full_path, $this->_dir . $newname)) { // Rename will fail if $newname already exists (at least on windows) syslog_insert(sprintf('File %s could not be renamed to %s', '<b>' . $old_file_name . '</b>', '<b>' . $newname . '</b>'), 'info', 'file', $this->ID); $DB->rollback(); return false; } // Delete thumb caches for old name: // Note: new name = new usage : there is a fair chance we won't need the same cache sizes in the new loc. $this->rm_cache(); // Get Meta data (before we change name) (we may need to update it later): $this->load_meta(); $this->_name = $newname; $this->Filetype = NULL; // depends on name $rel_dir = dirname($this->_rdfp_rel_path) . '/'; if ($rel_dir == './') { $rel_dir = ''; } $this->_rdfp_rel_path = $rel_dir . $this->_name; $this->_adfp_full_path = $this->_dir . $this->_name; $this->_md5ID = md5($this->_adfp_full_path); if ($this->meta == 'loaded') { // We have meta data, we need to deal with it: // unchanged : $this->set( 'root_type', $this->_FileRoot->type ); // unchanged : $this->set( 'root_ID', $this->_FileRoot->in_type_ID ); $this->set('path', $this->_rdfp_rel_path); // Record to DB: if (!$this->dbupdate()) { // Update failed, try to rollback the rename on disk: if (!@rename($this->_adfp_full_path, $this->_dir . $oldname)) { // rename failed $DB->rollback(); syslog_insert(sprintf('File %s could not be renamed to %s', '<b>' . $old_file_name . '</b>', '<b>' . $newname . '</b>'), 'info', 'file', $this->ID); return false; } // Maybe needs a specific error message here, the db and the disk is out of sync syslog_insert(sprintf('File %s could not be renamed to %s', '<b>' . $old_file_name . '</b>', '<b>' . $newname . '</b>'), 'info', 'file', $this->ID); return false; } } else { // There might be some old meta data to *recycle* in the DB... // This can happen if there has been a file in the same location in the past and if that file // has been manually deleted or moved since then. When the new file arrives here, we'll recover // the zombie meta data and we don't reset it on purpose. Actually, we consider that the meta data // has been *accidentaly* lost and that the user is attempting to recover it by putting back the // file where it was before. Of course the logical way would be to put back the file manually, but // experience proves that users are inconsistent! $this->load_meta(); } $DB->commit(); syslog_insert(sprintf('File %s was renamed to %s', '<b>' . $old_file_name . '</b>', '<b>' . $newname . '</b>'), 'info', 'file', $this->ID); return true; }