3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
;
314 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
315 return $script . '?page=CiviCRM&q=' . $path . $pageID . $separator . $query . $fragment;
318 return $base . '?page=CiviCRM&q=' . $path . $pageID . $separator . $query . $fragment;
322 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
323 return $script . '?page=CiviCRM&q=' . $path . $pageID . $fragment;
326 return $base .'?page=CiviCRM&q=' . $path . $pageID . $fragment;
332 if ($permlinkStructure != '' && ($pageID ||
$script != '')) {
333 return $script . '?' . $query . $pageID . $fragment;
336 return $base . $script . '?' . $query . $pageID . $fragment;
340 return $base . $fragment;
346 * Authenticate the user against the wordpress db
348 * @param string $name the user name
349 * @param string $password the password for the above user name
351 * @return mixed false if no auth
353 contactID, ufID, unique string ) if success
357 function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
358 $config = CRM_Core_Config
::singleton();
360 if ($loadCMSBootstrap) {
361 self
::loadBootstrap($name, $password);
364 $user = wp_authenticate($name, $password);
365 if (is_a($user, 'WP_Error')) {
369 // need to change this to make sure we matched only one row
371 CRM_Core_BAO_UFMatch
::synchronizeUFMatch($user->data
, $user->data
->ID
, $user->data
->user_email
, 'WordPress');
372 $contactID = CRM_Core_BAO_UFMatch
::getContactId($user->data
->ID
);
376 return array($contactID, $user->data
->ID
, mt_rand());
380 * Set a message in the UF to display to a user
382 * @param string $message the message to set
387 function setMessage($message) {
390 function loadUser( $user ) {
394 function permissionDenied() {
395 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
404 wp_redirect(wp_login_url());
407 function updateCategories() {}
410 * Get the locale set in the hosting CMS
412 * @return string with the locale or null for none
414 function getUFLocale() {
419 * load wordpress bootstrap
421 * @param $name string optional username for login
422 * @param $pass string optional password for login
424 function loadBootStrap($name = NULL, $pass = NULL) {
425 global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
427 $cmsRootPath = $this->cmsRootPath();
429 CRM_Core_Error
::fatal("Could not find the install directory for WordPress");
432 require_once ($cmsRootPath . DIRECTORY_SEPARATOR
. 'wp-load.php');
436 function validInstallDir($dir) {
437 $includePath = "$dir/wp-includes";
439 @opendir
($includePath) &&
440 file_exists("$includePath/version.php")
447 function cmsRootPath() {
448 $cmsRoot = $valid = NULL;
449 if (defined('CIVICRM_CMSDIR')) {
450 if ($this->validInstallDir(CIVICRM_CMSDIR
)) {
451 $cmsRoot = CIVICRM_CMSDIR
;
456 $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']));
458 //might be windows installation.
459 $firstVar = array_shift($pathVars);
461 $cmsRoot = $firstVar;
464 //start w/ csm dir search.
465 foreach ($pathVars as $var) {
467 if ($this->validInstallDir($cmsRoot)) {
468 //stop as we found bootstrap.
475 return ($valid) ?
$cmsRoot : NULL;
478 function createUser(&$params, $mail) {
481 'user_pass' => $params['cms_pass'],
482 'user_login' => $params['cms_name'],
483 'user_email' => $params[$mail],
484 'nickname' => $params['cms_name'],
485 'role' => get_option('default_role'),
487 if (isset($params['contactID'])) {
488 $contactType = CRM_Contact_BAO_Contact
::getContactType($params['contactID']);
489 if ($contactType == 'Individual') {
490 $user_data['first_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
491 $params['contactID'], 'first_name'
493 $user_data['last_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
494 $params['contactID'], 'last_name'
499 $uid = wp_insert_user($user_data);
502 $creds['user_login'] = $params['cms_name'];
503 $creds['user_password'] = $params['cms_pass'];
504 $creds['remember'] = TRUE;
505 $user = wp_signon($creds, FALSE);
507 wp_new_user_notification($uid, $user_data['user_pass']);
512 * Change user name in host CMS
514 * @param integer $ufID User ID in CMS
515 * @param string $ufName User name
517 function updateCMSName($ufID, $ufName) {
519 if (function_exists('wp_update_user')) {
520 $ufID = CRM_Utils_Type
::escape($ufID, 'Integer');
521 $ufName = CRM_Utils_Type
::escape($ufName, 'String');
523 $values = array ('ID' => $ufID, 'user_email' => $ufName);
525 wp_update_user( $values ) ;
530 function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
531 $config = CRM_Core_Config
::singleton();
533 $dao = new CRM_Core_DAO();
534 $name = $dao->escape(CRM_Utils_Array
::value('name', $params));
535 $email = $dao->escape(CRM_Utils_Array
::value('mail', $params));
537 if (CRM_Utils_Array
::value('name', $params)) {
538 if (!validate_username($params['name'])) {
539 $errors['cms_name'] = ts("Your username contains invalid characters");
541 elseif (username_exists(sanitize_user($params['name']))) {
542 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name']));
546 if (CRM_Utils_Array
::value('mail', $params)) {
547 if (!is_email($params['mail'])) {
548 $errors[$emailName] = "Your email is invaid";
550 elseif (email_exists($params['mail'])) {
551 $resetUrl = $config->userFrameworkBaseURL
. 'wp-login.php?action=lostpassword';
552 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
553 array(1 => $params['mail'], 2 => $resetUrl)
560 * check is user logged in.
562 * @return boolean true/false.
564 public function isUserLoggedIn() {
566 if (function_exists('is_user_logged_in')) {
567 $isloggedIn = is_user_logged_in();
574 * Get currently logged in user uf id.
576 * @return int $userID logged in user uf id.
578 public function getLoggedInUfID() {
580 if (function_exists('is_user_logged_in') &&
583 global $current_user;
584 $ufID = $current_user->ID
;
590 * Get user login URL for hosting CMS (method declared in each CMS system class)
592 * @param string $destination - if present, add destination to querystring (works for Drupal only)
594 * @return string - loginURL for the current CMS
597 public function getLoginURL($destination = '') {
598 $config = CRM_Core_Config
::singleton();
599 $loginURL = $config->userFrameworkBaseURL
;
600 $loginURL .= 'wp-login.php';
604 public function getLoginDestination(&$form) {
609 * Return the current WordPress version if relevant function exists
611 * @return string - version number
614 function getVersion() {
615 if (function_exists('get_bloginfo')) {
616 return get_bloginfo('version', 'display');