3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * WordPress specific stuff goes here
39 class CRM_Utils_System_WordPress
extends CRM_Utils_System_Base
{
42 public function __construct() {
44 * deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific
45 * functions and leave the codebase oblivious to the type of CMS
49 $this->is_drupal
= FALSE;
50 $this->is_wordpress
= TRUE;
56 public function setTitle($title, $pageTitle = NULL) {
61 // FIXME: Why is this global?
62 global $civicrm_wp_title;
63 $civicrm_wp_title = $title;
65 // yes, set page title, depending on context
66 $context = civi_wp()->civicrm_context_get();
70 $template = CRM_Core_Smarty
::singleton();
71 $template->assign('pageTitle', $pageTitle);
78 public function appendBreadCrumb($breadCrumbs) {
79 $breadCrumb = wp_get_breadcrumb();
81 if (is_array($breadCrumbs)) {
82 foreach ($breadCrumbs as $crumbs) {
83 if (stripos($crumbs['url'], 'id%%')) {
84 $args = array('cid', 'mid');
85 foreach ($args as $a) {
86 $val = CRM_Utils_Request
::retrieve($a, 'Positive', CRM_Core_DAO
::$_nullObject,
90 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
94 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
98 $template = CRM_Core_Smarty
::singleton();
99 $template->assign_by_ref('breadcrumb', $breadCrumb);
100 wp_set_breadcrumb($breadCrumb);
106 public function resetBreadCrumb() {
108 wp_set_breadcrumb($bc);
114 public function addHTMLHead($head) {
115 static $registered = FALSE;
118 add_action('wp_head', array(__CLASS__
, '_showHTMLHead'));
120 add_action('admin_head', array(__CLASS__
, '_showHTMLHead'));
122 CRM_Core_Region
::instance('wp_head')->add(array(
128 * WP action callback.
130 public static function _showHTMLHead() {
131 $region = CRM_Core_Region
::instance('wp_head', FALSE);
133 echo $region->render('');
140 public function mapConfigToSSL() {
142 $base_url = str_replace('http://', 'https://', $base_url);
155 $forceBackend = FALSE
157 $config = CRM_Core_Config
::singleton();
159 $separator = $htmlize ?
'&' : '&';
161 $fragment = isset($fragment) ?
('#' . $fragment) : '';
163 $path = CRM_Utils_String
::stripPathChars($path);
165 //this means wp function we are trying to use is not available,
167 if (!function_exists('get_option')) {
168 $this->loadBootStrap(); // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap
170 if ($config->userFrameworkFrontend
) {
171 if (get_option('permalink_structure') != '') {
173 $script = get_permalink($post->ID
);
176 // when shortcode is included in page
177 // also make sure we have valid query object
179 if (method_exists($wp_query, 'get')) {
180 if (get_query_var('page_id')) {
181 $wpPageParam = "page_id=" . get_query_var('page_id');
183 elseif (get_query_var('p')) {
184 // when shortcode is inserted in post
185 $wpPageParam = "p=" . get_query_var('p');
190 $base = $this->getBaseUrl($absolute, $frontend, $forceBackend);
192 if (!isset($path) && !isset($query)) {
193 // FIXME: This short-circuited codepath is the same as the general one below, except
194 // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know
195 // why it's different (and I can only find two obvious use-cases for this codepath,
196 // of which at least one looks gratuitous). A more ambitious person would simply remove
198 return $base . $fragment;
201 if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam ||
$script != '')) {
205 $queryParts = array();
207 $queryParts[] = 'page=CiviCRM';
208 $queryParts[] = "q={$path}";
211 $queryParts[] = $wpPageParam;
214 $queryParts[] = $query;
217 return $base . '?' . implode($separator, $queryParts) . $fragment;
223 * @param $forceBackend
225 * @return mixed|null|string
227 private function getBaseUrl($absolute, $frontend, $forceBackend) {
228 $config = CRM_Core_Config
::singleton();
230 if (!isset($config->useFrameworkRelativeBase
)) {
231 $base = parse_url($config->userFrameworkBaseURL
);
232 $config->useFrameworkRelativeBase
= $base['path'];
235 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
237 if ((is_admin() && !$frontend) ||
$forceBackend) {
238 $base .= 'wp-admin/admin.php';
241 elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
242 $base .= CIVICRM_UF_WP_BASEPAGE
;
245 elseif (isset($config->wpBasePage
)) {
246 $base .= $config->wpBasePage
;
255 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
256 $config = CRM_Core_Config
::singleton();
258 if ($loadCMSBootstrap) {
259 $config->userSystem
->loadBootStrap($name, $password);
262 $user = wp_authenticate($name, $password);
263 if (is_a($user, 'WP_Error')) {
267 // TODO: need to change this to make sure we matched only one row
269 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user->data
, $user->data
->ID
, $user->data
->user_email
, 'WordPress');
270 $contactID = CRM_Core_BAO_UFMatch
::getContactId($user->data
->ID
);
274 return array($contactID, $user->data
->ID
, mt_rand());
278 * FIXME: Do something
280 public function setMessage($message) {
284 * FIXME: Do something
286 public function loadUser($user) {
291 * FIXME: Use CMS-native approach
293 public function permissionDenied() {
294 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
300 public function logout() {
306 wp_redirect(wp_login_url());
312 public function getUFLocale() {
314 if (defined('ICL_LANGUAGE_CODE')) {
315 $language = ICL_LANGUAGE_CODE
;
318 // TODO: set language variable for others WordPress plugin
320 if (isset($language)) {
321 return CRM_Core_I18n_PseudoConstant
::longForShort(substr($language, 0, 2));
329 * Load wordpress bootstrap.
331 * @param string $name
332 * optional username for login.
333 * @param string $pass
334 * optional password for login.
338 public function loadBootStrap($name = NULL, $pass = NULL) {
339 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
341 $cmsRootPath = $this->cmsRootPath();
343 CRM_Core_Error
::fatal("Could not find the install directory for WordPress");
346 require_once $cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-load.php';
347 $wpUserTimezone = get_option('timezone_string');
348 if ($wpUserTimezone) {
349 date_default_timezone_set($wpUserTimezone);
350 CRM_Core_Config
::singleton()->userSystem
->setMySQLTimeZone();
352 require_once $cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-includes/pluggable.php';
353 $uid = CRM_Utils_Array
::value('uid', $name);
355 $name = $name ?
$name : trim(CRM_Utils_Array
::value('name', $_REQUEST));
356 $pass = $pass ?
$pass : trim(CRM_Utils_Array
::value('pass', $_REQUEST));
358 $uid = wp_authenticate($name, $pass);
361 echo '<br />Sorry, unrecognized username or password.';
369 $account = wp_set_current_user($uid);
370 if ($account && $account->data
->ID
) {
384 public function validInstallDir($dir) {
385 $includePath = "$dir/wp-includes";
386 if (file_exists("$includePath/version.php")) {
393 * Determine the location of the CMS root.
395 * @return string|NULL
396 * local file system path to CMS root, or NULL if it cannot be determined
398 public function cmsRootPath() {
399 $cmsRoot = $valid = NULL;
400 if (defined('CIVICRM_CMSDIR')) {
401 if ($this->validInstallDir(CIVICRM_CMSDIR
)) {
402 $cmsRoot = CIVICRM_CMSDIR
;
407 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
409 //might be windows installation.
410 $firstVar = array_shift($pathVars);
412 $cmsRoot = $firstVar;
415 //start w/ csm dir search.
416 foreach ($pathVars as $var) {
418 if ($this->validInstallDir($cmsRoot)) {
419 //stop as we found bootstrap.
426 return ($valid) ?
$cmsRoot : NULL;
432 public function createUser(&$params, $mail) {
435 'user_pass' => $params['cms_pass'],
436 'user_login' => $params['cms_name'],
437 'user_email' => $params[$mail],
438 'nickname' => $params['cms_name'],
439 'role' => get_option('default_role'),
441 if (isset($params['contactID'])) {
442 $contactType = CRM_Contact_BAO_Contact
::getContactType($params['contactID']);
443 if ($contactType == 'Individual') {
444 $user_data['first_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
445 $params['contactID'], 'first_name'
447 $user_data['last_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
448 $params['contactID'], 'last_name'
453 $uid = wp_insert_user($user_data);
456 $creds['user_login'] = $params['cms_name'];
457 $creds['user_password'] = $params['cms_pass'];
458 $creds['remember'] = TRUE;
459 $user = wp_signon($creds, FALSE);
461 wp_new_user_notification($uid, $user_data['user_pass']);
468 public function updateCMSName($ufID, $ufName) {
470 if (function_exists('wp_update_user')) {
471 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
472 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
474 $values = array('ID' => $ufID, 'user_email' => $ufName);
476 wp_update_user($values);
482 * @param array $params
484 * @param string $emailName
486 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
487 $config = CRM_Core_Config
::singleton();
489 $dao = new CRM_Core_DAO();
490 $name = $dao->escape(CRM_Utils_Array
::value('name', $params));
491 $email = $dao->escape(CRM_Utils_Array
::value('mail', $params));
493 if (!empty($params['name'])) {
494 if (!validate_username($params['name'])) {
495 $errors['cms_name'] = ts("Your username contains invalid characters");
497 elseif (username_exists(sanitize_user($params['name']))) {
498 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
502 if (!empty($params['mail'])) {
503 if (!is_email($params['mail'])) {
504 $errors[$emailName] = "Your email is invaid";
506 elseif (email_exists($params['mail'])) {
507 $resetUrl = $config->userFrameworkBaseURL
. 'wp-login.php?action=lostpassword';
508 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
509 array(1 => $params['mail'], 2 => $resetUrl)
518 public function isUserLoggedIn() {
520 if (function_exists('is_user_logged_in')) {
521 $isloggedIn = is_user_logged_in();
530 public function getLoggedInUserObject() {
531 if (function_exists('is_user_logged_in') &&
534 global $current_user;
536 return $current_user;
542 public function getLoggedInUfID() {
544 $current_user = $this->getLoggedInUserObject();
545 return isset($current_user->ID
) ?
$current_user->ID
: NULL;
551 public function getLoggedInUniqueIdentifier() {
552 $user = $this->getLoggedInUserObject();
553 return $this->getUniqueIdentifierFromUserObject($user);
557 * Get User ID from UserFramework system (Joomla)
558 * @param object $user
559 * Object as described by the CMS.
563 public function getUserIDFromUserObject($user) {
564 return !empty($user->ID
) ?
$user->ID
: NULL;
570 public function getUniqueIdentifierFromUserObject($user) {
571 return empty($user->user_email
) ?
NULL : $user->user_email
;
577 public function getLoginURL($destination = '') {
578 $config = CRM_Core_Config
::singleton();
579 $loginURL = $config->userFrameworkBaseURL
;
580 $loginURL .= 'wp-login.php';
585 * FIXME: Do something
587 public function getLoginDestination(&$form) {
594 public function getVersion() {
595 if (function_exists('get_bloginfo')) {
596 return get_bloginfo('version', 'display');
606 public function getTimeZoneString() {
607 return get_option('timezone_string');
613 public function getUserRecordUrl($contactID) {
614 $uid = CRM_Core_BAO_UFMatch
::getUFId($contactID);
615 if (CRM_Core_Session
::singleton()
616 ->get('userID') == $contactID || CRM_Core_Permission
::checkAnyPerm(array('cms:administer users'))
618 return CRM_Core_Config
::singleton()->userFrameworkBaseURL
. "wp-admin/user-edit.php?user_id=" . $uid;