/** * Function for synchronizing cms users with CiviCRM contacts * * @param NULL * * @return void * * @static * @access public */ static function synchronize() { //start of schronization code $config =& CRM_Core_Config::singleton(); CRM_Core_Error::ignoreException(); $db_uf =& self::dbHandle($config); if ($config->userFramework == 'Drupal') { $id = 'uid'; $mail = 'mail'; $name = 'name'; } else { if ($config->userFramework == 'Joomla') { $id = 'id'; $mail = 'email'; $name = 'name'; } else { CRM_Core_Error::fatal("CMS user creation not supported for this framework"); } } set_time_limit(300); $sql = "SELECT {$id}, {$mail}, {$name} FROM {$config->userFrameworkUsersTableName} where {$mail} != ''"; $query = $db_uf->query($sql); $user = new StdClass(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { $user->{$id} = $row[$id]; $user->{$mail} = $row[$mail]; $user->{$name} = $row[$name]; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } $db_uf->disconnect(); //end of schronization code $status = ts('Synchronize Users to Contacts completed.'); $status .= ' ' . ts('Checked one user record.', array('count' => $contactCount, 'plural' => 'Checked %count user records.')); if ($contactMatching) { $status .= ' ' . ts('Found one matching contact record.', array('count' => $contactMatching, 'plural' => 'Found %count matching contact records.')); } $status .= ' ' . ts('Created one new contact record.', array('count' => $contactCreated, 'plural' => 'Created %count new contact records.')); CRM_Core_Session::setStatus($status, true); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin', 'reset=1')); }
/** * Function for synchronizing drupal users with CiviCRM contacts * * @param NULL * * @return void * * @static * @access public */ function synchronize() { //start of schronization code $config =& CRM_Core_Config::singleton(); /** * Update the next line with the correct Drupal database user, password, db_server and db name * for your Drupal installation. */ $db_drupal = DB::connect($config->userFrameworkDSN); if (DB::isError($db_drupal)) { die("Cannot connect to UF db via {$dsn}, " . $db_drupal->getMessage()); } if ($config->userFramework == 'Drupal') { $id = 'uid'; $mail = 'mail'; } else { if ($config->userFramework == 'Mambo') { $id = 'id'; $mail = 'email'; } else { die("Unknown user framework"); } } $sql = "SELECT {$id}, {$mail} FROM {$config->userFrameworkUsersTableName} where {$mail} != ''"; $query = $db_drupal->query($sql); $user = null; $uf = 'Drupal'; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { $contactCount++; if (CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1)) { $contactCreated++; } else { $contactMatching++; } } $db_drupal->disconnect(); //end of schronization code $status = ts('Synchronize Users to Contacts completed.'); $status .= ' ' . ts('Checked one user record.', array('count' => $contactCount, 'plural' => 'Checked %count user records.')); if ($contactMatching) { $status .= ' ' . ts('Found one matching contact record.', array('count' => $contactMatching, 'plural' => 'Found %count matching contact records.')); } $status .= ' ' . ts('Created one new contact record.', array('count' => $contactCreated, 'plural' => 'Created %count new contact records.')); CRM_Core_Session::setStatus($status); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin', 'reset=1')); }
static function contactID($ufID) { $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); if ($contactID) { return $contactID; } // else synchronize contact for this user $account = user_load($ufID); CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $ufID, $account->mail, 'Drupal'); $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID); if (!$contactID) { CRM_Core_Error::fatal(); } return $contactID; }
/** * Get contact id for the order's customer. * * @param object $order * Wordpress Order Object * * @return false|integer * > 0: existing contact * = 0: create new contact * FALSE: error */ function _woocommerce_civicrm_get_cid($order) { $user_id = get_current_user_id(); if ($user_id > 0) { // Logged in user global $current_user; get_currentuserinfo(); $match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($current_user, $current_user->ID, $current_user->user_email, 'WordPress', FALSE, 'Individual'); if (!is_object($match)) { return FALSE; } return $match->contact_id; } // The customer is anonymous. Look in the CiviCRM contacts table for a // contact that matches the billing email. $params = array('email' => $order->billing_email, 'return.contact_id' => TRUE, 'sequential' => 1); try { $contact = civicrm_api3('contact', 'get', $params); } catch (Exception $e) { return FALSE; } // No matches found, so we will need to create a contact. if (count($contact) == 0) { return 0; } $cid = $contact['values'][0]['id']; return $cid; }
/** * Authenticate the user against the drupal db * * @param string $name the user name * @param string $password the password for the above user name * * @return mixed false if no auth * array( contactID, ufID, unique string ) if success * @access public */ function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { require_once 'DB.php'; $config = CRM_Core_Config::singleton(); $dbDrupal = DB::connect($config->userFrameworkDSN); if (DB::isError($dbDrupal)) { CRM_Core_Error::fatal("Cannot connect to drupal db via {$config->userFrameworkDSN}, " . $dbDrupal->getMessage()); } $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $dbpassword = md5($password); $name = $dbDrupal->escapeSimple($strtolower($name)); $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '{$name}' AND u.pass = '******' AND u.status = 1"; $query = $dbDrupal->query($sql); $user = NULL; // need to change this to make sure we matched only one row while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); if (!$contactID) { return FALSE; } else { //success if ($loadCMSBootstrap) { $bootStrapParams = array(); if ($name && $password) { $bootStrapParams = array('name' => $name, 'pass' => $password); } CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); } return array($contactID, $row['uid'], mt_rand()); } } return FALSE; }
/** * @inheritDoc */ public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { require_once 'DB.php'; $config = CRM_Core_Config::singleton(); $user = NULL; if ($loadCMSBootstrap) { $bootStrapParams = array(); if ($name && $password) { $bootStrapParams = array('name' => $name, 'pass' => $password); } CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE); } jimport('joomla.application.component.helper'); jimport('joomla.database.table'); jimport('joomla.user.helper'); $JUserTable = JTable::getInstance('User', 'JTable'); $db = $JUserTable->getDbo(); $query = $db->getQuery(TRUE); $query->select('id, name, username, email, password'); $query->from($JUserTable->getTableName()); $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)'); $db->setQuery($query, 0, 0); $users = $db->loadObjectList(); $row = array(); if (count($users)) { $row = $users[0]; } $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))))); if (!defined('JVERSION')) { require $joomlaBase . '/libraries/cms/version/version.php'; $jversion = new JVersion(); define('JVERSION', $jversion->getShortVersion()); } if (!empty($row)) { $dbPassword = $row->password; $dbId = $row->id; $dbEmail = $row->email; if (version_compare(JVERSION, '2.5.18', 'lt') || version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt')) { // now check password list($hash, $salt) = explode(':', $dbPassword); $cryptpass = md5($password . $salt); if ($hash != $cryptpass) { return FALSE; } } else { if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) { return FALSE; } //include additional files required by Joomla 3.2.1+ if (version_compare(JVERSION, '3.2.1', 'ge')) { require_once $joomlaBase . '/libraries/cms/application/helper.php'; require_once $joomlaBase . '/libraries/cms/application/cms.php'; require_once $joomlaBase . '/libraries/cms/application/administrator.php'; } } CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla'); $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId); if (!$contactID) { return FALSE; } return array($contactID, $dbId, mt_rand()); } return FALSE; }
/** * @inheritDoc */ public function synchronizeUsers() { $config = CRM_Core_Config::singleton(); if (PHP_SAPI != 'cli') { set_time_limit(300); } $id = 'uid'; $mail = 'mail'; $name = 'name'; $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''"); $user = new StdClass(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; foreach ($result as $row) { $user->{$id} = $row->{$id}; $user->{$mail} = $row->{$mail}; $user->{$name} = $row->{$name}; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row->{$id}, $row->{$mail}, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } return array('contactCount' => $contactCount, 'contactMatching' => $contactMatching, 'contactCreated' => $contactCreated); }
/** * Updates a CiviCRM Contact when a WordPress user is updated * * @param integer $user_id The numeric ID of the WordPress user * @return void */ public function wordpress_contact_updated($user_id) { $this->_debug(array('function' => 'wordpress_contact_updated', 'user_id' => $user_id)); // okay, get user $user = get_userdata($user_id); // did we get one? if ($user) { // init CiviCRM if (!civi_wp()->initialize()) { return; } // get user matching file require_once 'CRM/Core/BAO/UFMatch.php'; // remove CiviCRM and BuddyPress callbacks to prevent recursion $this->_remove_hooks_bp(); $this->_remove_hooks_civi(); // get the Civi contact object $civi_contact = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user->ID, $user->user_email, 'WordPress', null, 'Individual'); // update first name and last name $this->_update_civi_name($user, $civi_contact); // optionally update primary email $this->_update_civi_primary_email($user, $civi_contact); // optionally update website $this->_update_civi_website($user, $civi_contact); // add more built-in WordPress fields here... // add CiviCRM and BuddyPress callbacks once more $this->_add_hooks_bp(); $this->_add_hooks_civi(); } }
/** * Authenticate the user against the drupal db * * @param string $name the user name * @param string $password the password for the above user name * @param boolean $loadCMSBootstrap load cms bootstrap? * @param NULL|string $realPath filename of script * * @return mixed false if no auth * array( * contactID, ufID, unique string ) if success * @access public */ static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { require_once 'DB.php'; $config = CRM_Core_Config::singleton(); $dbDrupal = DB::connect($config->userFrameworkDSN); if (DB::isError($dbDrupal)) { CRM_Core_Error::fatal("Cannot connect to drupal db via {$config->userFrameworkDSN}, " . $dbDrupal->getMessage()); } $account = $userUid = $userMail = NULL; if ($loadCMSBootstrap) { $bootStrapParams = array(); if ($name && $password) { $bootStrapParams = array('name' => $name, 'pass' => $password); } CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); global $user; if ($user) { $userUid = $user->uid; $userMail = $user->mail; } } else { // CRM-8638 // SOAP cannot load drupal bootstrap and hence we do it the old way // Contact CiviSMTP folks if we run into issues with this :) $cmsPath = $config->userSystem->cmsRootPath($realPath); require_once "{$cmsPath}/includes/bootstrap.inc"; require_once "{$cmsPath}/includes/password.inc"; $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $name = $dbDrupal->escapeSimple($strtolower($name)); $sql = "\nSELECT u.*\nFROM {$config->userFrameworkUsersTableName} u\nWHERE LOWER(u.name) = '{$name}'\nAND u.status = 1\n"; $query = $dbDrupal->query($sql); $row = $query->fetchRow(DB_FETCHMODE_ASSOC); if ($row) { $fakeDrupalAccount = drupal_anonymous_user(); $fakeDrupalAccount->name = $name; $fakeDrupalAccount->pass = $row['pass']; $passwordCheck = user_check_password($password, $fakeDrupalAccount); if ($passwordCheck) { $userUid = $row['uid']; $userMail = $row['mail']; } } } if ($userUid && $userMail) { CRM_Core_BAO_UFMatch::synchronizeUFMatch($account, $userUid, $userMail, 'Drupal'); $contactID = CRM_Core_BAO_UFMatch::getContactId($userUid); if (!$contactID) { return FALSE; } return array($contactID, $userUid, mt_rand()); } return FALSE; }
/** * @inheritDoc */ public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { //@todo this 'PEAR-y' stuff is only required when bookstrap is not being loaded which is rare // if ever now. // probably if bootstrap is loaded this call // CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); would be // sufficient to do what this fn does. It does exist as opposed to return which might need some hanky-panky to make // safe in the unknown situation where authenticate might be called & it is important that // false is returned require_once 'DB.php'; $config = CRM_Core_Config::singleton(); $dbDrupal = DB::connect($config->userFrameworkDSN); if (DB::isError($dbDrupal)) { CRM_Core_Error::fatal("Cannot connect to drupal db via {$config->userFrameworkDSN}, " . $dbDrupal->getMessage()); } $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $dbpassword = md5($password); $name = $dbDrupal->escapeSimple($strtolower($name)); $userFrameworkUsersTableName = $this->getUsersTableName(); $sql = 'SELECT u.* FROM ' . $userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '{$name}' AND u.pass = '******' AND u.status = 1"; $query = $dbDrupal->query($sql); $user = NULL; // need to change this to make sure we matched only one row while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); if (!$contactID) { return FALSE; } else { //success if ($loadCMSBootstrap) { $bootStrapParams = array(); if ($name && $password) { $bootStrapParams = array('name' => $name, 'pass' => $password); } CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, $realPath); } return array($contactID, $row['uid'], mt_rand()); } } return FALSE; }
/** * @inheritDoc */ public function synchronizeUsers() { $config = CRM_Core_Config::singleton(); if (PHP_SAPI != 'cli') { set_time_limit(300); } $users = array(); $users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; foreach ($users as $user) { $mail = $user->get('mail')->value; if (empty($mail)) { continue; } $uid = $user->get('uid')->value; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $uid, $mail, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } return array('contactCount' => $contactCount, 'contactMatching' => $contactMatching, 'contactCreated' => $contactCreated); }
/** * @inheritDoc */ public function synchronizeUsers() { $config = CRM_Core_Config::singleton(); if (PHP_SAPI != 'cli') { set_time_limit(300); } $id = 'id'; $mail = 'email'; $name = 'name'; $JUserTable =& JTable::getInstance('User', 'JTable'); $db = $JUserTable->getDbo(); $query = $db->getQuery(TRUE); $query->select($id . ', ' . $mail . ', ' . $name); $query->from($JUserTable->getTableName()); $query->where($mail != ''); $db->setQuery($query); $users = $db->loadObjectList(); $user = new StdClass(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; for ($i = 0; $i < count($users); $i++) { $user->{$id} = $users[$i]->{$id}; $user->{$mail} = $users[$i]->{$mail}; $user->{$name} = $users[$i]->{$name}; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $users[$i]->{$id}, $users[$i]->{$mail}, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } return array('contactCount' => $contactCount, 'contactMatching' => $contactMatching, 'contactCreated' => $contactCreated); }
/** * @return string * @throws CiviCRM_API3_Exception */ static function import_civi_members_to_wordpress() { civicrm_wp_initialize(); $return_message = ''; $account_creation_messages = ''; $member_types_to_sync = array(); $rules = self::get_civi_sync_rules(); $array_counter = 0; foreach ($rules as $key => $value) { $member_types_to_sync[$array_counter] = $value->civi_mem_type; $array_counter += 1; } //have them pull from the CiviSync rules $result_current_members = civicrm_api3('Membership', 'get', array('sequential' => 1, 'return' => array("contact_id"), 'membership_type_id' => array('IN' => $member_types_to_sync))); $current_member_array = array(); foreach ($result_current_members['values'] as $key => $values) { $current_member_array[] = $values['contact_id']; } $result_contacts = civicrm_api3('Contact', 'get', array('sequential' => 1, 'return' => array("first_name", "last_name", "id"), 'id' => array('IN' => $current_member_array))); if (isset($result_contacts)) { foreach ($result_contacts['values'] as $key => $values) { $member_array[$values['id']] = array('email' => '', 'first_name' => $values['first_name'], 'last_name' => $values['last_name']); } } $result_member_emails = civicrm_api3('Email', 'get', array('return' => array("email", "contact_id"), 'contact_id' => array('IN' => $current_member_array), 'is_primary' => 1)); if (isset($result_member_emails)) { foreach ($result_member_emails['values'] as $key => $values) { $member_array[$values['contact_id']]['email'] = $values['email']; } } if (isset($member_array)) { $new_account_count = 0; //TODO: make this more efficient by filtering out the emails that already exist in Wordpress. foreach ($member_array as $key => $values) { //extract the alias part of the email for the new user's username $email = $values['email']; $parts = explode("@", $email); $username = $parts[0]; if (null == username_exists($username) and !empty($email)) { $password = wp_generate_password(12, true); $user_id = wp_create_user($username, $password, $email); if (is_wp_error($user_id)) { echo 'Error creating user ' . $username . ' / ' . $email . ':<br>' . $user_id->get_error_message(); echo '<br>'; } else { $account_creation_messages = $account_creation_messages . 'Created account for user ' . $username . ' ID: ' . $user_id . '. <br>'; wp_update_user(array('ID' => $user_id, 'nickname' => $email, 'first_name' => $values['first_name'], 'last_name' => $values['last_name'])); //Update CiviCRM UFMatch record so that the new Wordpress user is appropriately connected to their Contact record in CiviCrm $user = get_user_by('id', $user_id); /*TODO: this line is not working. Need to research*/ CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user->ID, $user->email, 'WordPress'); // Email the user wp_mail($email, 'Welcome ' . $username . '!', ' Your Password: '******'Welcome ' . $username . '!', ' Your Password: '******' sent to ' . $email . '<br>'; $new_account_count += 1; } } } $return_message = $return_message . 'Total new accounts created: ' . $new_account_count . '<br>' . $account_creation_messages; } return $return_message; }
/** * Authenticate the user against the drupal db * * @param string $name the user name * @param string $password the password for the above user name * * @return mixed false if no auth * array( contactID, ufID, unique string ) if success * @access public * @static */ static function authenticate($name, $password) { require_once 'DB.php'; $config =& CRM_Core_Config::singleton(); $dbDrupal = DB::connect($config->userFrameworkDSN); if (DB::isError($dbDrupal)) { CRM_Core_Error::fatal("Cannot connect to drupal db via {$config->userFrameworkDSN}, " . $dbDrupal->getMessage()); } $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $password = md5($password); $name = $dbDrupal->escapeSimple($strtolower($name)); $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.name) = '{$name}' AND u.pass = '******' AND u.status = 1"; $query = $dbDrupal->query($sql); $user = null; // need to change this to make sure we matched only one row require_once 'CRM/Core/BAO/UFMatch.php'; while ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['uid'], $row['mail'], 'Drupal'); $contactID = CRM_Core_BAO_UFMatch::getContactId($row['uid']); if (!$contactID) { return false; } return array($contactID, $row['uid'], mt_rand()); } return false; }
/** * Authenticate the user against the joomla db * * @param string $name the user name * @param string $password the password for the above user name * @param $loadCMSBootstrap boolean load cms bootstrap? * * @return mixed false if no auth * array( contactID, ufID, unique string ) if success * @access public */ function authenticate($name, $password, $loadCMSBootstrap = FALSE) { require_once 'DB.php'; $config = CRM_Core_Config::singleton(); if ($loadCMSBootstrap) { $bootStrapParams = array(); if ($name && $password) { $bootStrapParams = array('name' => $name, 'pass' => $password); } CRM_Utils_System::loadBootStrap($bootStrapParams); } jimport('joomla.application.component.helper'); jimport('joomla.database.table'); $JUserTable =& JTable::getInstance('User', 'JTable'); $db = $JUserTable->getDbo(); $query = $db->getQuery(TRUE); $query->select('id, username, email, password'); $query->from($JUserTable->getTableName()); $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)'); $db->setQuery($query, 0, 0); $users = $db->loadAssocList(); $row = array(); if (count($users)) { $row = $users[0]; } $user = NULL; if (!empty($row)) { $dbPassword = CRM_Utils_Array::value('password', $row); $dbId = CRM_Utils_Array::value('id', $row); $dbEmail = CRM_Utils_Array::value('email', $row); // now check password if (strpos($dbPassword, ':') === FALSE) { if ($dbPassword != md5($password)) { return FALSE; } } else { list($hash, $salt) = explode(':', $dbPassword); $cryptpass = md5($password . $salt); if ($hash != $cryptpass) { return FALSE; } } CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $dbId, $dbEmail, 'Joomla'); $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId); if (!$contactID) { return FALSE; } return array($contactID, $dbId, mt_rand()); } return FALSE; }
/** * @inheritDoc */ public function synchronizeUsers() { $config = CRM_Core_Config::singleton(); if (PHP_SAPI != 'cli') { set_time_limit(300); } $id = 'ID'; $mail = 'user_email'; $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; global $wpdb; $wpUserIds = $wpdb->get_col("SELECT {$wpdb->users}.ID FROM {$wpdb->users}"); foreach ($wpUserIds as $wpUserId) { $wpUserData = get_userdata($wpUserId); $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, $wpUserData->{$id}, $wpUserData->{$mail}, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } return array('contactCount' => $contactCount, 'contactMatching' => $contactMatching, 'contactCreated' => $contactCreated); }
/** * Authenticate the user against the wordpress db * * @param string $name the user name * @param string $password the password for the above user name * * @return mixed false if no auth * array( contactID, ufID, unique string ) if success * @access public * @static */ function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { $config = CRM_Core_Config::singleton(); if ($loadCMSBootstrap) { $config->userSystem->loadBootStrap($name, $password); } $user = wp_authenticate($name, $password); if (is_a($user, 'WP_Error')) { return FALSE; } // need to change this to make sure we matched only one row CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); if (!$contactID) { return FALSE; } return array($contactID, $user->data->ID, mt_rand()); }
/** * Authenticate the user against the joomla db * * @param string $name the user name * @param string $password the password for the above user name * * @return mixed false if no auth * array( contactID, ufID, unique string ) if success * @access public * @static */ static function authenticate($name, $password) { require_once 'DB.php'; $config =& CRM_Core_Config::singleton(); $dbJoomla = DB::connect($config->userFrameworkDSN); if (DB::isError($dbJoomla)) { CRM_Core_Error::fatal("Cannot connect to joomla db via {$config->userFrameworkDSN}, " . $dbJoomla->getMessage()); } $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; $name = $dbJoomla->escapeSimple($strtolower($name)); $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName . " u WHERE LOWER(u.username) = '{$name}' AND u.block = 0"; $query = $dbJoomla->query($sql); $user = null; require_once 'CRM/Core/BAO/UFMatch.php'; if ($row = $query->fetchRow(DB_FETCHMODE_ASSOC)) { // now check password if (strpos($row['password'], ':') === false) { if ($row['password'] != md5($password)) { return false; } } else { list($hash, $salt) = explode(':', $row['password']); $cryptpass = md5($password . $salt); if ($hash != $cryptpass) { return false; } } CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row['id'], $row['email'], 'Joomla'); $contactID = CRM_Core_BAO_UFMatch::getContactId($row['id']); if (!$contactID) { return false; } return array($contactID, $row['id'], mt_rand()); } return false; }
/** * Synchronizing cms users with CiviCRM contacts. * * @param bool $is_interactive * Whether to show statuses & perform redirects. * This behavior is misplaced in the BAO layer, but we'll preserve it to avoid * contract changes in the middle of the support cycle. In the next major * release, we should remove & document it. * * @return void * */ public static function synchronize($is_interactive = TRUE) { //start of schronization code $config = CRM_Core_Config::singleton(); // Build an array of rows from UF users table. $rows = array(); if ($config->userSystem->is_drupal == '1') { $id = 'uid'; $mail = 'mail'; $name = 'name'; $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''"); if ($config->userFramework == 'Drupal') { while ($row = $result->fetchAssoc()) { $rows[] = $row; } } elseif ($config->userFramework == 'Drupal6') { while ($row = db_fetch_array($result)) { $rows[] = $row; } } } elseif ($config->userFramework == 'Joomla') { $id = 'id'; $mail = 'email'; $name = 'name'; // TODO: Insert code here to populate $rows for Joomla; } elseif ($config->userFramework == 'WordPress') { $id = 'ID'; $mail = 'user_email'; } else { CRM_Core_Error::fatal('CMS user creation not supported for this framework'); } if (PHP_SAPI != 'cli') { set_time_limit(300); } if ($config->userSystem->is_drupal == '1') { $user = new StdClass(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; foreach ($rows as $row) { $user->{$id} = $row[$id]; $user->{$mail} = $row[$mail]; $user->{$name} = $row[$name]; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } } elseif ($config->userFramework == 'Joomla') { $JUserTable =& JTable::getInstance('User', 'JTable'); $db = $JUserTable->getDbo(); $query = $db->getQuery(TRUE); $query->select($id . ', ' . $mail . ', ' . $name); $query->from($JUserTable->getTableName()); $query->where($mail != ''); $db->setQuery($query, 0, $limit); $users = $db->loadObjectList(); $user = new StdClass(); $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; for ($i = 0; $i < count($users); $i++) { $user->{$id} = $users[$i]->{$id}; $user->{$mail} = $users[$i]->{$mail}; $user->{$name} = $users[$i]->{$name}; $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $users[$i]->{$id}, $users[$i]->{$mail}, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } } elseif ($config->userFramework == 'WordPress') { $uf = $config->userFramework; $contactCount = 0; $contactCreated = 0; $contactMatching = 0; global $wpdb; $wpUserIds = $wpdb->get_col("SELECT {$wpdb->users}.ID FROM {$wpdb->users}"); foreach ($wpUserIds as $wpUserId) { $wpUserData = get_userdata($wpUserId); $contactCount++; if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, $wpUserData->{$id}, $wpUserData->{$mail}, $uf, 1, 'Individual', TRUE)) { $contactCreated++; } else { $contactMatching++; } if (is_object($match)) { $match->free(); } } } //end of synchronization code if ($is_interactive) { $status = ts('Synchronize Users to Contacts completed.'); $status .= ' ' . ts('Checked one user record.', array('count' => $contactCount, 'plural' => 'Checked %count user records.')); if ($contactMatching) { $status .= ' ' . ts('Found one matching contact record.', array('count' => $contactMatching, 'plural' => 'Found %count matching contact records.')); } $status .= ' ' . ts('Created one new contact record.', array('count' => $contactCreated, 'plural' => 'Created %count new contact records.')); CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin', 'reset=1')); } }
/** * Given a UF user object, make sure there is a contact * object for this user. If the user has new values, we need * to update the CRM DB with the new values * * @param Object $user the drupal user object * @param boolean $update has the user object been edited * @param $uf * * @return void * @access public * @static */ function synchronize(&$user, $update, $uf) { $session =& CRM_Core_Session::singleton(); if (!is_object($session)) { return; } if ($uf == 'Drupal') { $key = 'uid'; $mail = 'mail'; } else { if ($uf == 'Mambo') { $key = 'id'; $mail = 'email'; } else { CRM_Utils_System::statusBounce(ts('Please set the user framework variable')); } } // have we already processed this user, if so early // return. $userID = $session->get('userID'); $ufID = $session->get('ufID'); if (!$update && $ufID == $user->{$key}) { return; } // reset the session if we are a different user if ($ufID && $ufID != $user->{$key}) { $session->reset(); } // make sure we load the mambo object to get valid information if ($uf == 'Mambo') { $user->load(); } // if the id of the object is zero (true for drupal), return early if ($user->{$key} == 0) { return; } $ufmatch =& CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user->{$key}, $user->{$mail}, $uf); if (!$ufmatch) { return; } $session->set('ufID', $ufmatch->uf_id); $session->set('userID', $ufmatch->contact_id); $session->set('domainID', $ufmatch->domain_id); $session->set('ufEmail', $ufmatch->email); if ($update) { // the only information we care about is email, so lets check that if ($user->{$mail} != $ufmatch->email) { // email has changed, so we need to change all our primary email also $ufmatch->email = $user->{$mail}; $ufmatch->save(); $query = "\nUPDATE civicrm_contact\nLEFT JOIN civicrm_location ON ( civicrm_location.entity_table = 'civicrm_contact' AND\n civicrm_contact.id = civicrm_location.entity_id AND\n civicrm_location.is_primary = 1 )\nLEFT JOIN civicrm_email ON ( civicrm_location.id = civicrm_email.location_id AND\n civicrm_email.is_primary = 1 )\nSET civicrm_email.email = '" . $user->{$mail} . "' WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($ufmatch->contact_id, 'Integer'); CRM_Core_DAO::executeQuery($query); } } }