source: trunk/inc/classes/BxDolProfilesController.php

Last change on this file was 2, checked in by george, 14 years ago
  • Přidáno: Trunk revize 13719.
File size: 9.7 KB
Line 
1<?php
2
3require_once( BX_DIRECTORY_PATH_CLASSES . 'BxDolProfileFields.php' );
4require_once( BX_DIRECTORY_PATH_CLASSES . 'BxDolEmailTemplates.php' );
5
6class BxDolProfilesController {
7
8 var $oPF;
9 var $aItems;
10 var $oEmailTemplate;
11
12 function BxDolProfilesController() {
13 $this -> oEmailTemplate = new BxDolEmailTemplates();
14 }
15
16 function createProfile( $aData, $bSendMails = true, $iMainMemberID = 0 ) {
17
18 if( !$aData or !is_array($aData) or empty($aData) )
19 return false;
20
21 unset( $aData['Couple'] );
22 unset( $aData['Captcha'] );
23 unset( $aData['TermsOfUse'] );
24 unset( $aData['ProfilePhoto'] );
25
26 /* @var $this->oPF BxDolProfileFields */
27 $this -> oPF = new BxDolProfileFields(100);
28
29 if( !$this -> oPF -> aArea ) {
30 echo 'Profile Fields cache not loaded. Cannot continue.';
31 return false;
32 }
33
34 $this -> aItems = $this -> oPF -> aArea[0]['Items'];
35
36 if( $iMainMemberID )
37 $aMainMember = $this -> getProfileInfo( $iMainMemberID );
38 else
39 $aMainMember = false;
40
41 // begin profile info collecting
42 $aNewProfile = array();
43
44 foreach( $this -> aItems as $aItem ) {
45 $sItemName = $aItem['Name'];
46
47 if( array_key_exists( $sItemName, $aData ) ) {
48 $aNewProfile[$sItemName] = $aData[$sItemName];
49 } elseif( $aMainMember and array_key_exists( $sItemName, $aMainMember ) and $aItem['Type'] != 'system' ) {
50 if( $aItem['Unique'] )
51 $aNewProfile[$sItemName] = $this -> genUniqueValue( $sItemName, $aMainMember[$sItemName] );
52 else
53 $aNewProfile[$sItemName] = $aMainMember[$sItemName];
54 } else {
55 switch( $aItem['Type'] ) {
56 case 'pass':
57 $aNewProfile[$sItemName] = $this -> genRandomPassword();
58 break;
59
60 case 'num':
61 $aNewProfile[$sItemName] = (int)$aItem['Default'];
62 break;
63
64 case 'bool':
65 $aNewProfile[$sItemName] = (bool)$aItem['Default'];
66 break;
67
68 case 'system':
69 switch( $sItemName ) {
70 case 'ID': //set automatically
71 case 'Captcha': //not been inserted
72 case 'Location': //not been inserted
73 case 'Keyword': //not been inserted
74 case 'TermsOfUse': //not been inserted
75 //pass
76 break;
77
78 case 'DateReg':
79 $aNewProfile[$sItemName] = date( 'Y-m-d H:i:s' ); // set current date
80 break;
81
82 case 'DateLastEdit':
83 case 'DateLastLogin':
84 $aNewProfile[$sItemName] = '0000-00-00';
85 break;
86
87 case 'Couple':
88 $aNewProfile[$sItemName] = $aMainMember ? $iMainMemberID : 0; //if main member exists, set him as a couple link
89 break;
90
91 case 'Featured':
92 $aNewProfile[$sItemName] = false;
93 break;
94
95 case 'Status':
96 if ( getParam('autoApproval_ifNoConfEmail') == 'on' ) {
97 if ( getParam('autoApproval_ifJoin') == 'on' )
98 $aNewProfile[$sItemName] = 'Active';
99 else
100 $aNewProfile[$sItemName] = 'Approval';
101 } else
102 $aNewProfile[$sItemName] = 'Unconfirmed';
103 break;
104 }
105 break;
106
107 default:
108 $aNewProfile[$sItemName] = $aItem['Default'];
109 }
110 }
111 } //we completed collecting
112
113 $sSet = $this -> collectSetString( $aNewProfile );
114 $sQuery = "INSERT INTO `Profiles` SET \n$sSet";
115
116 $rRes = db_res( $sQuery );
117
118 if( $rRes ) {
119 $iNewID = db_last_id();
120
121 $this -> createProfileCache( $iNewID );
122
123 if( $aMainMember )
124 $this -> updateProfile( $iMainMemberID, array('Couple' => $iNewID ) ); //set main member's couple. they will be linked each other
125
126 //collect status text
127 if( $bSendMails and !$aMainMember ) { //send mail only to main member, not to couple
128 if( getParam('autoApproval_ifNoConfEmail') == 'on' ) {
129 if ( getParam('autoApproval_ifJoin') == 'on' ) {
130 $sStatusText = 'Active';
131 $this -> sendActivationMail( $iNewID );
132 } else {
133 $sStatusText = 'Approval';
134 $this -> sendApprovalMail( $iNewID );
135 }
136 } else {
137 if( $this -> sendConfMail( $iNewID ) )
138 $sStatusText = 'Unconfirmed';
139 else
140 $sStatusText = 'NotSent';
141 }
142 } else
143 $sStatusText = 'OK';
144
145 //set crypted password
146 $sSalt = genRndSalt();
147 $this -> updateProfile($iNewID, array(
148 'Password' => encryptUserPwd($aNewProfile['Password'], $sSalt),
149 'Salt' => $sSalt
150 ));
151
152 return array( $iNewID, $sStatusText );
153 } else
154 return array( false, 'Failed' );
155 }
156
157 function createProfileCache( $iMemID ) {
158 createUserDataFile( $iMemID );
159 }
160
161 function sendConfMail( $iMemID ) {
162 global $site;
163
164 $iMemID = (int)$iMemID;
165 $aMember = $this -> getProfileInfo( $iMemID );
166 if( !$aMember )
167 return false;
168
169 $sEmail = $aMember['Email'];
170
171 $sConfCode = base64_encode( base64_encode( crypt( $sEmail, CRYPT_EXT_DES ? 'secret_ph' : 'se' ) ) );
172 $sConfLink = "{$site['url']}profile_activate.php?ConfID={$iMemID}&ConfCode=" . urlencode( $sConfCode );
173
174 $aPlus = array( 'ConfCode' => $sConfCode, 'ConfirmationLink' => $sConfLink );
175
176 $aTemplate = $this -> oEmailTemplate -> getTemplate( 't_Confirmation' ) ;
177 return sendMail( $sEmail, $aTemplate['Subject'], $aTemplate['Body'], $iMemID, $aPlus );
178 }
179
180 // sent when user status changed to active
181 function sendActivationMail( $iMemID ) {
182
183 $iMemID = (int)$iMemID;
184 $aMember = $this -> getProfileInfo( $iMemID );
185 if( !$aMember )
186 return false;
187
188 $sEmail = $aMember['Email'];
189 $aTemplate = $this -> oEmailTemplate -> getTemplate( 't_Activation' ) ;
190
191 return sendMail( $sEmail, $aTemplate['Subject'], $aTemplate['Body'], $iMemID );
192 }
193
194 //sent if member in approval status
195 function sendApprovalMail( $iMemId ) {
196
197 }
198
199 // sent to admin
200 function sendNewUserNotify( $iMemID ) {
201 global $site;
202
203 $iMemID = (int)$iMemID;
204 $aMember = $this -> getProfileInfo( $iMemID );
205 if( !$aMember )
206 return false;
207
208 $sSubject = "New user confirmed";
209 $sMessage =
210"New user {$aMember['NickName']} with email {$aMember['Email']} has been confirmed,
211his/her ID is {$iMemID}.
212--
213{$site['title']} mail delivery system
214Auto-generated e-mail, please, do not reply
215";
216 return sendMail( $site['email_notify'], $sSubject, $sMessage );
217 }
218
219 function updateProfile( $iMemberID, $aData ) {
220 if( !$aData or !is_array($aData) or empty($aData) )
221 return false;
222
223 $this -> _checkUpdateMatchFields($aData);
224
225 $sSet = $this -> collectSetString( $aData );
226 $sQuery = "UPDATE `Profiles` SET {$sSet} WHERE `ID` = " . (int)$iMemberID;
227 //echo $sQuery ;
228 db_res($sQuery);
229 $this -> createProfileCache( $iMemberID );
230 return (bool)db_affected_rows();
231 }
232
233 /**
234 * Check if we need to update profile matching
235 */
236 function _checkUpdateMatchFields(&$aData) {
237 // list of all matchable fields
238 $aAllMatchFields = array();
239
240 // temporary flag of member
241 $aData['UpdateMatch'] = false;
242
243 // get array of matching fields
244 $oMatchFields = new BxDolProfileFields(101);
245 $aMatchFields = $oMatchFields -> aArea[0]['Items'];
246
247 // get array of all fields
248 $oAllFields = new BxDolProfileFields(100);
249 $aAllFields = $oAllFields -> aArea[0]['Items'];
250
251 // find all matchable fields
252 foreach ($aMatchFields as $iFieldID => $aField) {
253 // put it to the list
254 $aAllMatchFields[$iFieldID] = $aField['Name'];
255
256 // get matched field too
257 $iNewFieldID = $aField['MatchField'];
258 $aNewField = $aAllFields[$iNewFieldID];
259
260 // and put it to the list too
261 $aAllMatchFields[$iNewFieldID] = $aNewField['Name'];
262 }
263
264 // also need to re-match if Status is changed
265 $aAllMatchFields[7] = 'Status';
266
267 //echoDbg($aAllMatchFields);
268
269 // check if one of updated fields is matchable
270 foreach ($aData as $sName => $sValue) {
271 //echo $sName . "\n";
272 if (in_array($sName, $aAllMatchFields)) {
273 $aData['UpdateMatch'] = true;
274 break; // if at least one of the fields is matchable then true
275 }
276 }
277
278 //echoDbg($aData);
279 }
280
281 function collectSetString( $aData ) {
282 $sRequestSet = '';
283
284 foreach( $aData as $sField => $mValue ) {
285 if( is_string($mValue) )
286 $sValue = "'" . addslashes( $mValue ) . "'";
287 elseif( is_bool($mValue) )
288 $sValue = (int)$mValue;
289 elseif( is_array($mValue) ) {
290 $sValue = '';
291 foreach( $mValue as $sStr )
292 $sValue .= addslashes( str_replace( ',', '', $sStr ) ) . ',';
293
294 $sValue = "'" . substr($sValue,0,-1) . "'";
295 } elseif( is_int($mValue) ) {
296 $sValue = $mValue;
297 } else
298 continue;
299
300 $sRequestSet .= "`$sField` = $sValue,\n";
301 }
302
303 $sRequestSet = substr( $sRequestSet,0, -2 );// remove ,\n
304
305 return $sRequestSet;
306 }
307
308 function deleteProfile( $iMemberID ) {
309
310 }
311
312 function genRandomPassword() {
313 return 'aaaaaa';
314 }
315
316 function getProfileInfo( $iMemberID ) {
317 return db_assoc_arr( "SELECT * FROM `Profiles` WHERE `ID` = " . (int)$iMemberID );
318 }
319
320 function genUniqueValue( $sFieldName, $sValue, $bRandMore = false ) {
321 if( $bRandMore )
322 $sRand = '(' . rand(1000, 9999) . ')';
323 else
324 $sRand = '(2)';
325
326 $sNewValue = $sValue . $sRand;
327
328 $iCount = (int)db_value( "SELECT COUNT(*) FROM `Profiles` WHERE `$sFieldName` = '" . addslashes($sNewValue) . "'" );
329 if( $iCount )
330 return genUniqueValue( $sFieldName, $sValue, true );
331 else
332 return $sNewValue;
333 }
334}
Note: See TracBrowser for help on using the repository browser.