Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * this file contains functions for synchronizing cms users with CiviCRM contacts | |
38 | */ | |
39 | ||
40 | require_once 'DB.php'; | |
41 | class CRM_Core_BAO_CMSUser { | |
42 | ||
43 | /** | |
44 | * Function for synchronizing cms users with CiviCRM contacts | |
45 | * | |
46 | * @param NULL | |
47 | * | |
48 | * @return void | |
49 | * | |
50 | * @static | |
51 | * @access public | |
52 | */ | |
53 | static function synchronize() { | |
54 | //start of schronization code | |
55 | $config = CRM_Core_Config::singleton(); | |
56 | ||
57 | // Build an array of rows from UF users table. | |
58 | $rows = array(); | |
59 | if ($config->userSystem->is_drupal == '1') { | |
60 | $id = 'uid'; | |
61 | $mail = 'mail'; | |
62 | $name = 'name'; | |
63 | ||
64 | $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''"); | |
65 | ||
66 | if ($config->userFramework == 'Drupal') { | |
67 | while ($row = $result->fetchAssoc()) { | |
68 | $rows[] = $row; | |
69 | } | |
70 | } | |
71 | elseif ($config->userFramework == 'Drupal6') { | |
72 | while ($row = db_fetch_array($result)) { | |
73 | $rows[] = $row; | |
74 | } | |
75 | } | |
76 | } | |
77 | elseif ($config->userFramework == 'Joomla') { | |
78 | $id = 'id'; | |
79 | $mail = 'email'; | |
80 | $name = 'name'; | |
81 | // TODO: Insert code here to populate $rows for Joomla; | |
82 | } | |
83 | elseif ($config->userFramework == 'WordPress') { | |
84 | $id = 'ID'; | |
85 | $mail = 'user_email'; | |
86 | } | |
87 | else { | |
88 | CRM_Core_Error::fatal('CMS user creation not supported for this framework'); | |
89 | } | |
90 | ||
91 | set_time_limit(300); | |
92 | ||
93 | if ($config->userSystem->is_drupal == '1') { | |
94 | $user = new StdClass(); | |
95 | $uf = $config->userFramework; | |
96 | $contactCount = 0; | |
97 | $contactCreated = 0; | |
98 | $contactMatching = 0; | |
99 | foreach ($rows as $row) { | |
100 | $user->$id = $row[$id]; | |
101 | $user->$mail = $row[$mail]; | |
102 | $user->$name = $row[$name]; | |
103 | $contactCount++; | |
104 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) { | |
105 | $contactCreated++; | |
106 | } | |
107 | else { | |
108 | $contactMatching++; | |
109 | } | |
110 | if (is_object($match)) { | |
111 | $match->free(); | |
112 | } | |
113 | } | |
114 | } | |
115 | elseif ($config->userFramework == 'Joomla') { | |
116 | ||
117 | $JUserTable = &JTable::getInstance('User', 'JTable'); | |
118 | ||
119 | $db = $JUserTable->getDbo(); | |
120 | $query = $db->getQuery(TRUE); | |
121 | $query->select($id . ', ' . $mail . ', ' . $name); | |
122 | $query->from($JUserTable->getTableName()); | |
123 | $query->where($mail != ''); | |
124 | ||
125 | $db->setQuery($query, 0, $limit); | |
126 | $users = $db->loadObjectList(); | |
127 | ||
128 | $user = new StdClass(); | |
129 | $uf = $config->userFramework; | |
130 | $contactCount = 0; | |
131 | $contactCreated = 0; | |
132 | $contactMatching = 0; | |
133 | for ($i = 0; $i < count($users); $i++) { | |
134 | $user->$id = $users[$i]->$id; | |
135 | $user->$mail = $users[$i]->$mail; | |
136 | $user->$name = $users[$i]->$name; | |
137 | $contactCount++; | |
138 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, | |
139 | $users[$i]->$id, | |
140 | $users[$i]->$mail, | |
141 | $uf, | |
142 | 1, | |
143 | 'Individual', | |
144 | TRUE | |
145 | )) { | |
146 | $contactCreated++; | |
147 | } | |
148 | else { | |
149 | $contactMatching++; | |
150 | } | |
151 | if (is_object($match)) { | |
152 | $match->free(); | |
153 | } | |
154 | } | |
155 | } | |
156 | elseif ($config->userFramework == 'WordPress') { | |
157 | $uf = $config->userFramework; | |
158 | $contactCount = 0; | |
159 | $contactCreated = 0; | |
160 | $contactMatching = 0; | |
161 | ||
162 | global $wpdb; | |
82d5bb05 | 163 | $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users"); |
6a488035 TO |
164 | |
165 | foreach ($wpUserIds as $wpUserId) { | |
166 | $wpUserData = get_userdata($wpUserId); | |
167 | $contactCount++; | |
168 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, | |
169 | $wpUserData->$id, | |
170 | $wpUserData->$mail, | |
171 | $uf, | |
172 | 1, | |
173 | 'Individual', | |
174 | TRUE | |
175 | )) { | |
176 | $contactCreated++; | |
177 | } | |
178 | else { | |
179 | $contactMatching++; | |
180 | } | |
181 | if (is_object($match)) { | |
182 | $match->free(); | |
183 | } | |
184 | } | |
185 | } | |
186 | //end of schronization code | |
187 | $status = ts('Synchronize Users to Contacts completed.'); | |
188 | $status .= ' ' . ts('Checked one user record.', | |
189 | array( | |
190 | 'count' => $contactCount, | |
191 | 'plural' => 'Checked %count user records.' | |
192 | ) | |
193 | ); | |
194 | if ($contactMatching) { | |
195 | $status .= ' ' . ts('Found one matching contact record.', | |
196 | array( | |
197 | 'count' => $contactMatching, | |
198 | 'plural' => 'Found %count matching contact records.' | |
199 | ) | |
200 | ); | |
201 | } | |
202 | ||
203 | $status .= ' ' . ts('Created one new contact record.', | |
204 | array( | |
205 | 'count' => $contactCreated, | |
206 | 'plural' => 'Created %count new contact records.' | |
207 | ) | |
208 | ); | |
209 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
210 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Function to create CMS user using Profile | |
215 | * | |
216 | * @param array $params associated array | |
217 | * @param string $mail email id for cms user | |
218 | * | |
219 | * @return int contact id that has been created | |
220 | * @access public | |
221 | * @static | |
222 | */ | |
223 | static function create(&$params, $mail) { | |
224 | $config = CRM_Core_Config::singleton(); | |
225 | ||
226 | $ufID = $config->userSystem->createUser($params, $mail); | |
227 | ||
228 | //if contact doesn't already exist create UF Match | |
229 | if ($ufID !== FALSE && | |
230 | isset($params['contactID']) | |
231 | ) { | |
232 | // create the UF Match record | |
233 | $ufmatch = new CRM_Core_DAO_UFMatch(); | |
234 | $ufmatch->domain_id = CRM_Core_Config::domainID(); | |
235 | $ufmatch->uf_id = $ufID; | |
236 | $ufmatch->contact_id = $params['contactID']; | |
237 | $ufmatch->uf_name = $params[$mail]; | |
238 | ||
239 | if (!$ufmatch->find(TRUE)) { | |
240 | $ufmatch->save(); | |
241 | } | |
242 | } | |
243 | ||
244 | return $ufID; | |
245 | } | |
246 | ||
247 | /** | |
248 | * Function to create Form for CMS user using Profile | |
249 | * | |
250 | * @param object $form | |
251 | * @param integer $gid id of group of profile | |
252 | * @param bool $emailPresent true if the profile field has email(primary) | |
253 | * @return FALSE|void WTF | |
254 | * | |
255 | * @access public | |
256 | * @static | |
257 | */ | |
258 | static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action::NONE) { | |
259 | $config = CRM_Core_Config::singleton(); | |
260 | $showCMS = FALSE; | |
261 | ||
262 | $isDrupal = $config->userSystem->is_drupal; | |
263 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
264 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; | |
265 | ||
266 | //if CMS is configured for not to allow creating new CMS user, | |
267 | //don't build the form,Fixed for CRM-4036 | |
268 | if ($isJoomla) { | |
269 | $userParams = JComponentHelper::getParams('com_users'); | |
270 | if (!$userParams->get('allowUserRegistration')) { | |
271 | return FALSE; | |
272 | } | |
273 | } | |
274 | elseif ($isDrupal && !variable_get('user_register', TRUE)) { | |
275 | return FALSE; | |
276 | } | |
277 | elseif ($isWordPress && !get_option('users_can_register')) { | |
278 | return FALSE; | |
279 | } | |
280 | ||
281 | if ($gid) { | |
282 | $isCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user'); | |
283 | } | |
284 | ||
285 | // $cms is true when there is email(primary location) is set in the profile field. | |
286 | $session = CRM_Core_Session::singleton(); | |
287 | $userID = $session->get('userID'); | |
288 | $showUserRegistration = FALSE; | |
289 | if ($action) { | |
290 | $showUserRegistration = TRUE; | |
291 | } | |
292 | elseif (!$action && !$userID) { | |
293 | $showUserRegistration = TRUE; | |
294 | } | |
295 | ||
296 | if ($isCMSUser && $emailPresent) { | |
297 | if ($showUserRegistration) { | |
298 | if ($isCMSUser != 2) { | |
299 | $extra = array( | |
300 | 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );", | |
301 | ); | |
302 | $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra); | |
303 | $required = FALSE; | |
304 | } | |
305 | else { | |
306 | $form->add('hidden', 'cms_create_account', 1); | |
307 | $required = TRUE; | |
308 | } | |
309 | ||
310 | $form->assign('isCMS', $required); | |
311 | if (!$userID || $action & CRM_Core_Action::PREVIEW || $action & CRM_Core_Action::PROFILE) { | |
312 | $form->add('text', 'cms_name', ts('Username'), NULL, $required); | |
313 | if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) { | |
314 | $form->add('password', 'cms_pass', ts('Password')); | |
315 | $form->add('password', 'cms_confirm_pass', ts('Confirm Password')); | |
316 | } | |
317 | ||
318 | $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form); | |
319 | } | |
320 | $showCMS = TRUE; | |
321 | } | |
322 | } | |
323 | ||
324 | $destination = $config->userSystem->getLoginDestination($form); | |
325 | $loginURL = $config->userSystem->getLoginURL($destination); | |
326 | $form->assign('loginURL', $loginURL); | |
327 | $form->assign('showCMS', $showCMS); | |
328 | } | |
329 | ||
330 | /* | |
331 | * Checks that there is a valid username & email | |
332 | * optionally checks password is present & matches DB & gets the CMS to validate | |
333 | * | |
334 | * @params array $fields Posted values of form | |
335 | * @param array $files uploaded files if any | |
336 | * @param array $self reference to form object | |
337 | * | |
338 | */ | |
339 | static function formRule($fields, $files, $self) { | |
340 | if (!CRM_Utils_Array::value('cms_create_account', $fields)) { | |
341 | return TRUE; | |
342 | } | |
343 | ||
344 | $config = CRM_Core_Config::singleton(); | |
345 | ||
346 | $isDrupal = $config->userSystem->is_drupal; | |
347 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
348 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; | |
349 | ||
350 | $errors = array(); | |
351 | if ($isDrupal || $isJoomla || $isWordPress) { | |
352 | $emailName = NULL; | |
353 | if (!empty($self->_bltID) && array_key_exists("email-{$self->_bltID}", $fields)) { | |
354 | // this is a transaction related page | |
355 | $emailName = 'email-' . $self->_bltID; | |
356 | } else { | |
357 | // find the email field in a profile page | |
358 | foreach ($fields as $name => $dontCare) { | |
359 | if (substr($name, 0, 5) == 'email') { | |
360 | $emailName = $name; | |
361 | break; | |
362 | } | |
363 | } | |
364 | } | |
365 | ||
366 | if ($emailName == NULL) { | |
367 | $errors['_qf_default'] == ts('Could not find an email address.'); | |
368 | return $errors; | |
369 | } | |
370 | ||
371 | if (empty($fields['cms_name'])) { | |
372 | $errors['cms_name'] = ts('Please specify a username.'); | |
373 | } | |
374 | ||
375 | if (empty($fields[$emailName])) { | |
376 | $errors[$emailName] = ts('Please specify a valid email address.'); | |
377 | } | |
378 | ||
379 | if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) { | |
380 | if (empty($fields['cms_pass']) || | |
381 | empty($fields['cms_confirm_pass']) | |
382 | ) { | |
383 | $errors['cms_pass'] = ts('Please enter a password.'); | |
384 | } | |
385 | if ($fields['cms_pass'] != $fields['cms_confirm_pass']) { | |
386 | $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.'); | |
387 | } | |
388 | } | |
389 | ||
390 | if (!empty($errors)) { | |
391 | return $errors; | |
392 | } | |
393 | ||
394 | // now check that the cms db does not have the user name and/or email | |
395 | if ($isDrupal OR $isJoomla OR $isWordPress) { | |
396 | $params = array( | |
397 | 'name' => $fields['cms_name'], | |
398 | 'mail' => $fields[$emailName], | |
399 | ); | |
400 | } | |
401 | ||
402 | $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName); | |
403 | } | |
404 | return (!empty($errors)) ? $errors : TRUE; | |
405 | } | |
406 | ||
407 | /** | |
408 | * Function to check if a cms user already exists. | |
409 | * | |
410 | * @param Array $contact array of contact-details | |
411 | * | |
412 | * @return uid if user exists, false otherwise | |
413 | * | |
414 | * @access public | |
415 | * @static | |
416 | */ | |
417 | static function userExists(&$contact) { | |
418 | $config = CRM_Core_Config::singleton(); | |
419 | ||
420 | $isDrupal = $config->userSystem->is_drupal; | |
421 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
422 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; | |
423 | ||
424 | if (!$isDrupal && !$isJoomla && !$isWordPress) { | |
425 | die('Unknown user framework'); | |
426 | } | |
427 | ||
428 | // Use UF native framework to fetch data from UF user table | |
429 | if ($isDrupal) { | |
430 | $uid = db_query( | |
431 | "SELECT uid FROM {users} where mail = :email", | |
432 | array(':email' => $contact['email']) | |
433 | )->fetchField(); | |
434 | ||
435 | if ($uid) { | |
436 | $contact['user_exists'] = TRUE; | |
437 | $result = $uid; | |
438 | } | |
439 | } | |
440 | elseif ($isJoomla) { | |
441 | $mail = $contact['email']; | |
442 | ||
443 | $JUserTable = &JTable::getInstance('User', 'JTable'); | |
444 | ||
445 | $db = $JUserTable->getDbo(); | |
446 | $query = $db->getQuery(TRUE); | |
447 | $query->select('username, email'); | |
448 | $query->from($JUserTable->getTableName()); | |
449 | $query->where('(LOWER(email) = LOWER(\'' . $email . '\'))'); | |
450 | $db->setQuery($query, 0, $limit); | |
451 | $users = $db->loadAssocList(); | |
452 | ||
453 | $row = array();; | |
454 | if (count($users)) { | |
455 | $row = $users[0]; | |
456 | } | |
457 | ||
458 | if (!empty($row)) { | |
459 | $uid = CRM_Utils_Array::value('id', $row); | |
460 | $contact['user_exists'] = TRUE; | |
461 | $result = $uid; | |
462 | } | |
463 | } | |
464 | elseif ($isWordPress) { | |
465 | if (email_exists($params['mail'])) { | |
466 | $contact['user_exists'] = TRUE; | |
467 | $userObj = get_user_by('email', $params['mail']); | |
468 | return $userObj->ID; | |
469 | } | |
470 | } | |
471 | ||
472 | return $result; | |
473 | } | |
474 | ||
475 | static function &dbHandle(&$config) { | |
476 | CRM_Core_Error::ignoreException(); | |
477 | $db_uf = DB::connect($config->userFrameworkDSN); | |
478 | CRM_Core_Error::setCallback(); | |
479 | if (!$db_uf || | |
480 | DB::isError($db_uf) | |
481 | ) { | |
482 | $session = CRM_Core_Session::singleton(); | |
483 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
484 | CRM_Core_Error::statusBounce(ts("Cannot connect to UF db via %1. Please check the CIVICRM_UF_DSN value in your civicrm.settings.php file", | |
485 | array(1 => $db_uf->getMessage()) | |
486 | )); | |
487 | } | |
488 | $db_uf->query('/*!40101 SET NAMES utf8 */'); | |
489 | return $db_uf; | |
490 | } | |
491 | } | |
492 |