Oxymoron290 Posted January 16, 2013 Report Share Posted January 16, 2013 (edited) Below is a function which will take any givin pilot and find the appropriate avatar for him and re-associate the avatar with the pilot after said pilot has been moved to another airline, or had their ID changed. ​it also deletes any unnecessary avatars. public static function fixAvatar($pilotid){ // Get the pilots Information. $pilotid = (int)$pilotid; $sql = "SELECT * FROM `".TABLE_PREFIX."pilots` WHERE `pilotid`=$pilotid"; $pilot = DB::get_row($sql); unset($pilotid); $err = DB::errno(); if($err != 0){ return false; } // Setting up the check (with regex) for an avatar with the pilots current ID. $pilotcode = PilotData::getPilotCode($pilot->code, $pilot->pilotid); $pilotid = preg_replace("/[^0-9]/", "", $pilotcode ); if(substr(SITE_ROOT, -1) == DS){ $rootDir = substr_replace(SITE_ROOT, "", -1); }else{ $rootDir = SITE_ROOT; } $directory = $rootDir.AVATAR_PATH; if (!is_dir($directory)) { return false; } $files = array(); if($handel = opendir($directory)){ // Go through each avatar and find any that match the pilots. while (false !== ($file = readdir($handel))){ if ('.' === $file) continue; if ('..' === $file) continue; if(preg_replace("/[^0-9]/", "", $file ) == $pilotid){ $files[] = $file; } } closedir($handel); if(count($files) == 0){ return true; // No files to update, exit successful. }else{ // if there is more than one avatars for the pilot find the newest one. if(count($files) > 1){ $newer = 0; foreach($files as $file){ $tmp = @filemtime($directory.DS.$file); if($tmp == NULL) $tmp = @filemtime(utf8_decode($directory.DS.$file)); if($tmp == null) // if your function is exiting here, try CHMODing your avatars directory. return false; if($tmp > $newer){ $newer = $tmp; $newAvatar = $file; } } // we will need to loop through these again and delete all matching $files that don't match $newAvatar. foreach($files as $file){ if($file != $newAvatar){ @unlink($directory.DS.$file); // delete the file. if(file_exists($directory.DS.$file)){ LogData::addLog(Auth::$userinfo->pilotid, "There was an error while deleting ".SITE_URL.AVATAR_PATH.$file.". Please delete it manually."); return false; }else{ LogData::addLog(Auth::$userinfo->pilotid, "The file ".SITE_URL.AVATAR_PATH.$file." was deleted successfully."); } } } }else{ $newAvatar = $files[0]; } $oldFile = pathinfo($directory.DS.$newAvatar); if($oldFile['filename'] != $pilotcode){ LogData::addLog(Auth::$userinfo->pilotid, "The file ".$newAvatar." was renamed to ".$pilotcode.'.'.$oldFile['extension']); return @rename($directory.DS.$newAvatar, $directory.DS.$pilotcode.'.'.$oldFile['extension']); }else{ return true; } } } return false; } This is the bare function and depending on where you put it, will be called different from implementation to implementation. For more information, or a pre-build implementation, I shall require 78 Smirnoffs, 12 rubber bands, 117 pop-sickle sticks, and a bottle of pepto bismol. ~wings on air~ Edited January 16, 2013 by Oxymoron290 Quote Link to comment Share on other sites More sharing options...
Oxymoron290 Posted January 16, 2013 Author Report Share Posted January 16, 2013 and a bottle of Elmer's glue. forgot to add that in there... Quote Link to comment Share on other sites More sharing options...
warpennys Posted January 16, 2013 Report Share Posted January 16, 2013 Great Job, Son. You don't need any Smirnoff's! Quote Link to comment Share on other sites More sharing options...
Oxymoron290 Posted January 16, 2013 Author Report Share Posted January 16, 2013 (edited) you alternatively want to do this: if($oldFile['filename'] != $pilotcode){ LogData::addLog(Auth::$userinfo->pilotid, "[Avatar HotFix]The file ".$newAvatar." was renamed to ".$pilotcode.'.'.$oldFile['extension']); $reply = @rename($directory.DS.$newAvatar, $directory.DS.$pilotcode.'.'.$oldFile['extension']); if (Config::Get('SIGNATURE_SHOW_AVATAR_IMAGE') == true) { LogData::addLog(Auth::$userinfo->pilotid, "[Avatar HotFix]Regernated signature for ".$pilotcode); PilotData::GenerateSignature($pilot->pilotid); } return $reply; }else{ return true; } Edited January 16, 2013 by Oxymoron290 Quote Link to comment Share on other sites More sharing options...
Ademar Andrade Posted April 2, 2013 Report Share Posted April 2, 2013 Hi Oxymoron290, Well done your script. I have this problem ins my V.A. Where will i put this code? Thanks, 1 Quote Link to comment Share on other sites More sharing options...
Oxymoron290 Posted April 9, 2013 Author Report Share Posted April 9, 2013 Hi Oxymoron290, Well done your script. I have this problem ins my V.A. Where will i put this code? Thanks, I shall require 78 Smirnoffs, 12 rubber bands, 117 pop-sickle sticks, a bottle of pepto bismol and a bottle of elmers glue. I will PM you with the address where you can ship the supplies. Haha, just kidding. here is how I decided to implement that function: I placed the above function in ./core/common/MaintenanceData.class.php ​Now for the following bit you will have to forgive me as I have modded our installation of phpVMS tremendously and can't remember exactally what is in the default package. In ./core/common/PilotData.class.php around line 291 I put the following code (Keep in mind I have edited this file alot in my installation and might be on a different line for you. The function is changePilotID($old_pilotid, $new_pilotid) and it should go 3 lines after the $pilot_exists line) [/size] [size=4] $avatarFix = MaintenanceData::fixAvatar($old_pilotid, $new_pilotid); if($avatarFix !== true){ return $avatarFix; } Now for your convenience I will provide you with the full function that we are currently using on WOAVA, however, I would recommend AGAINST copying and using the code as I have made some security patches to the phpVMS Pilot Groups and this exact code wont work with the current release. (However, it might with the dev release! =] available here: https://github.com/n...archive/dev.zip ) [/size] [size=4]/** * PilotData::changePilotID() * * @param mixed $old_pilotid * @param mixed $new_pilotid * @return */ public static function changePilotID($old_pilotid, $new_pilotid) { if(!PilotGroups::group_has_perm(Auth::$usergroups, EDIT_PILOTS)) { Debug::showCritical('Unauthorized access - You do not have permissions to edit pilots'); die(); } $pilot_exists = self::getPilotData($new_pilotid); if (is_object($pilot_exists)) { return false; }[/size] [size=4] $avatarFix = MaintenanceData::fixAvatar($old_pilotid, $new_pilotid); if($avatarFix !== true){ return $avatarFix; } DB::query('SET foreign_key_checks = 0;'); // List of all the tables which need to update $table_list = array( 'groupmembers', 'pilots', 'adminlog', 'awardsgranted', 'acarsdata', 'sessions', 'pireps', 'pirepcomments', 'fieldvalues', 'bids');[/size] [size=4] foreach ($table_list as $table) { $sql = 'UPDATE `' . TABLE_PREFIX . $table . '` SET `pilotid`=' . $new_pilotid . ' WHERE `pilotid`=' . $old_pilotid; DB::query($sql); } return true; }[/size] [size=4] Quote Link to comment Share on other sites More sharing options...
Ademar Andrade Posted April 17, 2013 Report Share Posted April 17, 2013 I shall require 78 Smirnoffs, 12 rubber bands, 117 pop-sickle sticks, a bottle of pepto bismol and a bottle of elmers glue. I will PM you with the address where you can ship the supplies. Haha, just kidding. here is how I decided to implement that function: I placed the above function in ./core/common/MaintenanceData.class.php​Now for the following bit you will have to forgive me as I have modded our installation of phpVMS tremendously and can't remember exactally what is in the default package. In ./core/common/PilotData.class.php around line 291 I put the following code (Keep in mind I have edited this file alot in my installation and might be on a different line for you. The function is changePilotID($old_pilotid, $new_pilotid) and it should go 3 lines after the $pilot_exists line) [/size] [size=4] $avatarFix = MaintenanceData::fixAvatar($old_pilotid, $new_pilotid); if($avatarFix !== true){ return $avatarFix; } Now for your convenience I will provide you with the full function that we are currently using on WOAVA, however, I would recommend AGAINST copying and using the code as I have made some security patches to the phpVMS Pilot Groups and this exact code wont work with the current release. (However, it might with the dev release! =] available here: https://github.com/n...archive/dev.zip ) [/size] [size=4]/** * PilotData::changePilotID() * * @param mixed $old_pilotid * @param mixed $new_pilotid * @return */ public static function changePilotID($old_pilotid, $new_pilotid) { if(!PilotGroups::group_has_perm(Auth::$usergroups, EDIT_PILOTS)) { Debug::showCritical('Unauthorized access - You do not have permissions to edit pilots'); die(); } $pilot_exists = self::getPilotData($new_pilotid); if (is_object($pilot_exists)) { return false; }[/size] [size=4] $avatarFix = MaintenanceData::fixAvatar($old_pilotid, $new_pilotid); if($avatarFix !== true){ return $avatarFix; } DB::query('SET foreign_key_checks = 0;'); // List of all the tables which need to update $table_list = array( 'groupmembers', 'pilots', 'adminlog', 'awardsgranted', 'acarsdata', 'sessions', 'pireps', 'pirepcomments', 'fieldvalues', 'bids');[/size] [size=4] foreach ($table_list as $table) { $sql = 'UPDATE `' . TABLE_PREFIX . $table . '` SET `pilotid`=' . $new_pilotid . ' WHERE `pilotid`=' . $old_pilotid; DB::query($sql); } return true; }[/size] [size=4] Thanks dude! Nice job! See you... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.