BxDataMigrationProfiles.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. require_once 'BxDataMigrationData.php';
  3. class BxDataMigrationProfiles extends BxDataMigrationData
  4. {
  5. var $bAvatarInstalled;
  6. var $iTransffered = 0;
  7. /**
  8. * Class constructor;
  9. *
  10. * @param : $oMigrationModule (object) - object instance of migration class;
  11. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  12. * @param : $oDolModule (object);
  13. * @return : void;
  14. */
  15. function BxDataMigrationProfiles(&$oMigrationModule, &$rOldDb, $oDolModule = '')
  16. {
  17. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  18. $this -> bAvatarInstalled = $this -> oMigrationModule -> _oDb -> isModule('avatar') ? true : false;
  19. }
  20. /**
  21. * Function migrate profiles data;
  22. *
  23. * @return : (string) operation result;
  24. */
  25. function getMigration()
  26. {
  27. // set new status;
  28. $this -> setResultStatus('Profiles transfer now');
  29. mysql_query('SET NAMES utf8', $this->rOldDb);
  30. $sError = $this -> transformProfiles();
  31. if($sError) {
  32. $this -> setResultStatus($sError);
  33. return MIGRATION_FAILED;
  34. }
  35. // set as finished;
  36. $this -> setResultStatus('All profiles were transferred (' . $this -> iTransffered . ' items)');
  37. return MIGRATION_SUCCESSFUL;
  38. }
  39. /**
  40. * Function Transform all old profiles;
  41. *
  42. * @return : (string) - error message if nedded;
  43. */
  44. function transformProfiles()
  45. {
  46. // upate new profiles table;
  47. $sError = $this -> _updateNewProfiles();
  48. if($sError) {
  49. return $sError;
  50. }
  51. $sQuery = "SELECT * FROM `Profiles`";
  52. $rResult = mysql_query($sQuery, $this -> rOldDb);
  53. while( $aRow = mysql_fetch_assoc($rResult) )
  54. {
  55. if( !$this -> isProfileExisting($aRow['NickName']) ) {
  56. // escape all data;
  57. $aRow = $this -> escapeData($aRow);
  58. $sQuery =
  59. "
  60. INSERT INTO
  61. `Profiles`
  62. SET
  63. `ID` = '{$aRow['ID']}',
  64. `NickName` = '{$aRow['NickName']}',
  65. `Email` = '{$aRow['Email']}',
  66. `Password` = '{$aRow['Password']}',
  67. `Status` = '{$aRow['Status']}',
  68. `Couple` = '{$aRow['Couple']}',
  69. `Sex` = '{$aRow['Sex']}',
  70. `LookingFor` = '{$aRow['LookingFor']}',
  71. `Headline` = '{$aRow['Headline']}',
  72. `DescriptionMe` = '{$aRow['DescriptionMe']}',
  73. `Country` = '{$aRow['Country']}',
  74. `City` = '{$aRow['City']}',
  75. `DateOfBirth` = '{$aRow['DateOfBirth']}',
  76. `Featured` = '{$aRow['Featured']}',
  77. `DateReg` = '{$aRow['DateReg']}',
  78. `DateLastEdit` = '{$aRow['DateLastEdit']}',
  79. `DateLastLogin` = '{$aRow['DateLastLogin']}',
  80. `DateLastNav` = '{$aRow['DateLastNav']}',
  81. `aff_num` = '{$aRow['aff_num']}',
  82. `Tags` = '{$aRow['Tags']}',
  83. `zip` = '{$aRow['zip']}',
  84. `EmailNotify` = '{$aRow['EmailNotify']}'
  85. ";
  86. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  87. if($iResult <= 0) {
  88. return 'Database error. Cannot insert new profile in the database.';
  89. }
  90. // update passwords
  91. $this -> oMigrationModule -> _oDb -> query("UPDATE `Profiles` SET `Salt` = CONV(FLOOR(RAND()*99999999999999), 10, 36) WHERE `ID` = '{$aRow['ID']}'");
  92. $this -> oMigrationModule -> _oDb -> query("UPDATE `Profiles` SET `Password` = SHA1(CONCAT(`Password`, `Salt`)) WHERE `ID` = '{$aRow['ID']}'");
  93. createUserDataFile($aRow['ID']);
  94. // migrate profile's picture;
  95. if($aRow['Picture'] && $this -> bAvatarInstalled) {
  96. $this -> _exportAvatar($aRow['ID'], $aRow['PrimPhoto']);
  97. }
  98. // migrate friends;
  99. $sResult = $this -> _exportFriends($aRow['ID']);
  100. if($sResult) {
  101. return $sResult;
  102. }
  103. $this -> iTransffered++;
  104. }
  105. }
  106. }
  107. /**
  108. * Function get profile image;
  109. *
  110. * @param : $iImgId (integer) - profile's image id;
  111. * @return : (string) - image's name;
  112. */
  113. function getProfileImage($iImgId)
  114. {
  115. $iImgId = (int) $iImgId;
  116. $sQuery = "SELECT `med_file` FROM `media` WHERE `med_id` = {$iImgId} AND `med_type` = 'photo' LIMIT 1";
  117. $rResult = mysql_query($sQuery, $this -> rOldDb);
  118. $aRow = mysql_fetch_assoc($rResult);
  119. return isset($aRow['med_file']) ? $aRow['med_file'] : '';
  120. }
  121. /**
  122. * Function update new dolphin's profiles table;
  123. *
  124. * @return : (string) - error message or empty;
  125. */
  126. function _updateNewProfiles()
  127. {
  128. // define the last profile Id from old Db;
  129. $sQuery = "SELECT `ID` FROM `Profiles` ORDER BY `ID` DESC LIMIT 1";
  130. $rResult = mysql_query($sQuery, $this -> rOldDb);
  131. $aRow = mysql_fetch_assoc($rResult);
  132. $iLastProfileId = (int) $aRow['ID'];
  133. $iLastProfileId++;
  134. // update current profiles table;
  135. $sQuery = "SELECT `ID` FROM `Profiles`";
  136. $aProfiles = $this -> oMigrationModule -> _oDb -> getAll($sQuery);
  137. if($aProfiles) {
  138. foreach($aProfiles as $iKey => $aItems)
  139. {
  140. $sQuery = "UPDATE `Profiles` SET `ID` = '{$iLastProfileId}' WHERE `ID` = '{$aItems['ID']}'";
  141. $this -> oMigrationModule -> _oDb -> query($sQuery);
  142. createUserDataFile($iLastProfileId);
  143. $iLastProfileId++;
  144. }
  145. }
  146. }
  147. /**
  148. * Export all profile's friends;
  149. *
  150. * @param $iProfileId integer;
  151. * @return string - error message if needed;
  152. */
  153. function _exportFriends($iProfileId)
  154. {
  155. $iProfileId = (int) $iProfileId;
  156. $sQuery = "SELECT * FROM `FriendList` WHERE `ID` = {$iProfileId} OR `Profile` = {$iProfileId}";
  157. $rResult = mysql_query($sQuery, $this -> rOldDb);
  158. while( $aRow = mysql_fetch_assoc($rResult) )
  159. {
  160. if( !$this -> isFriendExisting($aRow['ID'], $aRow['Profile'])) {
  161. // build query;
  162. $sQuery =
  163. "
  164. INSERT IGNORE INTO
  165. `sys_friend_list`
  166. SET
  167. `ID` = '{$aRow['ID']}',
  168. `Profile` = '{$aRow['Profile']}',
  169. `Check` = '{$aRow['Check']}',
  170. `When` = NOW()
  171. ";
  172. $iResult = (int) $this -> oMigrationModule -> _oDb -> query($sQuery);
  173. if($iResult <= 0) {
  174. return 'Database error. Cannot insert friend list in the database.';
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * Function export profile's avatar;
  181. *
  182. * @param : $iProfileId (integer);
  183. * @param : $sAvatarName (integer) - avatar's id;
  184. * @return : void;
  185. */
  186. function _exportAvatar($iProfileId, $iAvatarId)
  187. {
  188. $iProfileId = (int) $iProfileId;
  189. $iAvatarId = (int) $iAvatarId;
  190. //define the old avatar's name;
  191. $sOldAvatarName = $this -> getProfileImage($iAvatarId);
  192. if($sOldAvatarName) {
  193. //define old avatar's path;
  194. $sOldAvatarPath = $this -> oMigrationModule -> _oDb -> getExtraParam('config_profileImage')
  195. . $iProfileId . '/photo_' . $sOldAvatarName;
  196. require_once(BX_DIRECTORY_PATH_MODULES . 'boonex/avatar/classes/BxAvaModule.php');
  197. $oAvatar = BxDolModule::getInstance('bx_avatar');
  198. $oAvatar -> _iProfileId = $iProfileId;
  199. // resize image;
  200. $sNewImagePath = BX_AVA_DIR_TMP . '_' . $iProfileId . BX_AVA_EXT;
  201. $o =& BxDolImageResize::instance(BX_AVA_W, BX_AVA_W);
  202. $o->removeCropOptions ();
  203. $o->setJpegOutput (true);
  204. $o->setSize (BX_AVA_W, BX_AVA_W);
  205. $o->setSquareResize (true);
  206. if (IMAGE_ERROR_SUCCESS != $o->resize($sOldAvatarPath, $sNewImagePath))
  207. return false;
  208. return $oAvatar ->_addAvatar($sNewImagePath, false);
  209. }
  210. }
  211. /**
  212. * Function will check existing poll in module;
  213. *
  214. * @param : $sPollQuestion (string) - poll's question;
  215. * @return : true if exist;
  216. */
  217. function isProfileExisting($sNickName)
  218. {
  219. $sNickName = $this -> oMigrationModule -> _oDb -> escape($sNickName);
  220. $sQuery = "SELECT COUNT(*) FROM `Profiles` WHERE `NickName` = '{$sNickName}'";
  221. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  222. }
  223. /**
  224. * Check friends pair;
  225. *
  226. * @param $iProfileId integer
  227. * @param $iFriendId integer
  228. * @return boolean - true if already existing
  229. */
  230. function isFriendExisting($iProfileId, $iFriendId)
  231. {
  232. $iProfileId = (int) $iProfileId;
  233. $iFriendId = (int) $iFriendId;
  234. $sQuery =
  235. "
  236. SELECT
  237. COUNT(*)
  238. FROM
  239. `sys_friend_list`
  240. WHERE
  241. (
  242. (`ID` = {$iProfileId} AND `Profile` = {$iFriendId})
  243. OR
  244. (`ID` = {$iFriendId} AND `Profile` = {$iProfileId})
  245. )
  246. ";
  247. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  248. }
  249. }