3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * WordPress specific stuff goes here
39 class CRM_Utils_System_WordPress
extends CRM_Utils_System_Base
{
40 function __construct() {
41 $this->is_drupal
= FALSE;
45 * sets the title of the page
47 * @param string $title
48 * @paqram string $pageTitle
53 function setTitle($title, $pageTitle = NULL) {
57 if (civicrm_wp_in_civicrm()) {
58 global $civicrm_wp_title;
59 $civicrm_wp_title = $pageTitle;
60 $template = CRM_Core_Smarty
::singleton();
61 $template->assign('pageTitle', $pageTitle);
66 * Append an additional breadcrumb tag to the existing breadcrumb
68 * @param string $title
75 function appendBreadCrumb($breadCrumbs) {
76 $breadCrumb = wp_get_breadcrumb();
78 if (is_array($breadCrumbs)) {
79 foreach ($breadCrumbs as $crumbs) {
80 if (stripos($crumbs['url'], 'id%%')) {
81 $args = array('cid', 'mid');
82 foreach ($args as $a) {
83 $val = CRM_Utils_Request
::retrieve($a, 'Positive', CRM_Core_DAO
::$_nullObject,
87 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
91 $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
95 $template = CRM_Core_Smarty
::singleton();
96 $template->assign_by_ref('breadcrumb', $breadCrumb);
97 wp_set_breadcrumb($breadCrumb);
101 * Reset an additional breadcrumb tag to the existing breadcrumb
107 function resetBreadCrumb() {
109 wp_set_breadcrumb($bc);
113 * Append a string to the head of the html file
115 * @param string $head the new string to be appended
121 function addHTMLHead($head) {
122 static $registered = FALSE;
125 add_action('wp_head', array(__CLASS__
, '_showHTMLHead'));
127 add_action('admin_head', array(__CLASS__
, '_showHTMLHead'));
129 CRM_Core_Region
::instance('wp_head')->add(array(
134 static function _showHTMLHead() {
135 $region = CRM_Core_Region
::instance('wp_head', FALSE);
137 echo $region->render('');
144 * @param $url: string, absolute path to file
145 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
147 * Note: This function is not to be called directly
148 * @see CRM_Core_Region::render()
150 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
153 public function addScriptUrl($url, $region) {
158 * Add an inline script
160 * @param $code: string, javascript code
161 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
163 * Note: This function is not to be called directly
164 * @see CRM_Core_Region::render()
166 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
169 public function addScript($code, $region) {
176 * @param $url: string, absolute path to file
177 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
179 * Note: This function is not to be called directly
180 * @see CRM_Core_Region::render()
182 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
185 public function addStyleUrl($url, $region) {
190 * Add an inline style
192 * @param $code: string, css code
193 * @param $region string, location within the document: 'html-header', 'page-header', 'page-footer'
195 * Note: This function is not to be called directly
196 * @see CRM_Core_Region::render()
198 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
201 public function addStyle($code, $region) {
206 * rewrite various system urls to https
214 function mapConfigToSSL() {
216 $base_url = str_replace('http://', 'https://', $base_url);
220 * figure out the post url for the form
222 * @param mix $action the default action if one is pre-specified
224 * @return string the url to post the form
228 function postURL($action) {
229 if (!empty($action)) {
233 return $this->url($_GET['q'], NULL, TRUE, NULL, FALSE);
237 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
239 * @param $path string The path being linked to, such as "civicrm/add"
240 * @param $query string A query string to append to the link.
241 * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
242 * Useful for links that will be displayed outside the site, such as in an
244 * @param $fragment string A fragment identifier (named anchor) to append to the link.
245 * @param $htmlize boolean whether to convert to html eqivalant
246 * @param $frontend boolean a gross joomla hack
248 * @return string an HTML string containing a link to the given path.
259 $forceBackend = FALSE
261 $config = CRM_Core_Config
::singleton();
263 $separator = $htmlize ?
'&' : '&';
266 $path = CRM_Utils_String
::stripPathChars($path);
268 //this means wp function we are trying to use is not available,
270 if (!function_exists('get_option')) {
271 $this->loadBootStrap();
273 $permlinkStructure = get_option('permalink_structure');
274 if ($config->userFrameworkFrontend
) {
275 if ($permlinkStructure != '') {
277 $script = get_permalink($post->ID
);
280 // when shortcode is included in page
281 // also make sure we have valid query object
283 if ( method_exists( $wp_query, 'get' ) ) {
284 if (get_query_var('page_id')) {
285 $pageID = "{$separator}page_id=" . get_query_var('page_id');
287 elseif (get_query_var('p')) {
288 // when shortcode is inserted in post
289 $pageID = "{$separator}p=" . get_query_var('p');
294 if (isset($fragment)) {
295 $fragment = '#' . $fragment;
298 if (!isset($config->useFrameworkRelativeBase
)) {
299 $base = parse_url($config->userFrameworkBaseURL
);
300 $config->useFrameworkRelativeBase
= $base['path'];
303 $base = $absolute ?
$config->userFrameworkBaseURL
: $config->useFrameworkRelativeBase
;
305 if ((is_admin() && !$frontend) ||
$forceBackend) {
306 $base .= 'wp-admin/admin.php';
308 elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
309 $base .= CIVICRM_UF_WP_BASEPAGE
;
311 elseif (isset($config->wpBasePage
)) {
312 $base .= $config->wpBasePage
;
317 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
318 return $script . '?page=CiviCRM&q=' . $path . $pageID . $separator . $query . $fragment;
321 return $base . '?page=CiviCRM&q=' . $path . $pageID . $separator . $query . $fragment;
325 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
326 return $script . '?page=CiviCRM&q=' . $path . $pageID . $fragment;
329 return $base .'?page=CiviCRM&q=' . $path . $pageID . $fragment;
335 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
336 return $script . '?' . $query . $pageID . $fragment;
339 return $base . $script . '?' . $query . $pageID . $fragment;
343 return $base . $fragment;
349 * Authenticate the user against the wordpress db
351 * @param string $name the user name
352 * @param string $password the password for the above user name
354 * @return mixed false if no auth
356 contactID, ufID, unique string ) if success
360 function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
361 $config = CRM_Core_Config
::singleton();
363 if ($loadCMSBootstrap) {
364 $config->userSystem
->loadBootStrap($name, $password);
367 $user = wp_authenticate($name, $password);
368 if (is_a($user, 'WP_Error')) {
372 // need to change this to make sure we matched only one row
374 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user->data
, $user->data
->ID
, $user->data
->user_email
, 'WordPress');
375 $contactID = CRM_Core_BAO_UFMatch
::getContactId($user->data
->ID
);
379 return array($contactID, $user->data
->ID
, mt_rand());
383 * Set a message in the UF to display to a user
385 * @param string $message the message to set
390 function setMessage($message) {
393 function loadUser( $user ) {
397 function permissionDenied() {
398 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
407 wp_redirect(wp_login_url());
410 function updateCategories() {}
413 * Get the locale set in the hosting CMS
415 * @return string with the locale or null for none
417 function getUFLocale() {
422 * load wordpress bootstrap
424 * @param $name string optional username for login
425 * @param $pass string optional password for login
427 function loadBootStrap($name = NULL, $pass = NULL) {
428 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
430 $cmsRootPath = $this->cmsRootPath();
432 CRM_Core_Error
::fatal("Could not find the install directory for WordPress");
435 require_once ($cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-load.php');
439 function validInstallDir($dir) {
440 $includePath = "$dir/wp-includes";
442 @opendir
($includePath) &&
443 file_exists("$includePath/version.php")
450 function cmsRootPath() {
451 $cmsRoot = $valid = NULL;
452 if (defined('CIVICRM_CMSDIR')) {
453 if ($this->validInstallDir(CIVICRM_CMSDIR
)) {
454 $cmsRoot = CIVICRM_CMSDIR
;
459 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
461 //might be windows installation.
462 $firstVar = array_shift($pathVars);
464 $cmsRoot = $firstVar;
467 //start w/ csm dir search.
468 foreach ($pathVars as $var) {
470 if ($this->validInstallDir($cmsRoot)) {
471 //stop as we found bootstrap.
478 return ($valid) ?
$cmsRoot : NULL;
481 function createUser(&$params, $mail) {
484 'user_pass' => $params['cms_pass'],
485 'user_login' => $params['cms_name'],
486 'user_email' => $params[$mail],
487 'nickname' => $params['cms_name'],
488 'role' => get_option('default_role'),
490 if (isset($params['contactID'])) {
491 $contactType = CRM_Contact_BAO_Contact
::getContactType($params['contactID']);
492 if ($contactType == 'Individual') {
493 $user_data['first_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
494 $params['contactID'], 'first_name'
496 $user_data['last_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
497 $params['contactID'], 'last_name'
502 $uid = wp_insert_user($user_data);
505 $creds['user_login'] = $params['cms_name'];
506 $creds['user_password'] = $params['cms_pass'];
507 $creds['remember'] = TRUE;
508 $user = wp_signon($creds, FALSE);
510 wp_new_user_notification($uid, $user_data['user_pass']);
515 * Change user name in host CMS
517 * @param integer $ufID User ID in CMS
518 * @param string $ufName User name
520 function updateCMSName($ufID, $ufName) {
522 if (function_exists('wp_update_user')) {
523 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
524 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
526 $values = array ('ID' => $ufID, 'user_email' => $ufName);
528 wp_update_user( $values ) ;
533 function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
534 $config = CRM_Core_Config
::singleton();
536 $dao = new CRM_Core_DAO();
537 $name = $dao->escape(CRM_Utils_Array
::value('name', $params));
538 $email = $dao->escape(CRM_Utils_Array
::value('mail', $params));
540 if (CRM_Utils_Array
::value('name', $params)) {
541 if (!validate_username($params['name'])) {
542 $errors['cms_name'] = ts("Your username contains invalid characters");
544 elseif (username_exists(sanitize_user($params['name']))) {
545 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
549 if (CRM_Utils_Array
::value('mail', $params)) {
550 if (!is_email($params['mail'])) {
551 $errors[$emailName] = "Your email is invaid";
553 elseif (email_exists($params['mail'])) {
554 $resetUrl = $config->userFrameworkBaseURL
. 'wp-login.php?action=lostpassword';
555 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
556 array(1 => $params['mail'], 2 => $resetUrl)
563 * check is user logged in.
565 * @return boolean true/false.
567 public function isUserLoggedIn() {
569 if (function_exists('is_user_logged_in')) {
570 $isloggedIn = is_user_logged_in();
577 * Get currently logged in user uf id.
579 * @return int $userID logged in user uf id.
581 public function getLoggedInUfID() {
583 if (function_exists('is_user_logged_in') &&
586 global $current_user;
587 $ufID = $current_user->ID
;
593 * Get user login URL for hosting CMS (method declared in each CMS system class)
595 * @param string $destination - if present, add destination to querystring (works for Drupal only)
597 * @return string - loginURL for the current CMS
600 public function getLoginURL($destination = '') {
601 $config = CRM_Core_Config
::singleton();
602 $loginURL = $config->userFrameworkBaseURL
;
603 $loginURL .= 'wp-login.php';
607 public function getLoginDestination(&$form) {
612 * Return the current WordPress version if relevant function exists
614 * @return string - version number
617 function getVersion() {
618 if (function_exists('get_bloginfo')) {
619 return get_bloginfo('version', 'display');