Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * WordPress specific stuff goes here | |
38 | */ | |
39 | class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { | |
bb3a214a EM |
40 | /** |
41 | * | |
42 | */ | |
00be9182 | 43 | public function __construct() { |
4caaa696 EM |
44 | /** |
45 | * 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 | |
46 | * functions and leave the codebase oblivious to the type of CMS | |
47 | * @deprecated | |
48 | * @var bool | |
49 | */ | |
6a488035 | 50 | $this->is_drupal = FALSE; |
fe17e8d1 | 51 | $this->is_wordpress = TRUE; |
6a488035 TO |
52 | } |
53 | ||
54 | /** | |
df7cbeeb | 55 | * Sets the title of the page |
6a488035 TO |
56 | * |
57 | * @param string $title | |
f4aaa82a EM |
58 | * @param null $pageTitle |
59 | * | |
6a488035 | 60 | * @return void |
6a488035 | 61 | */ |
00be9182 | 62 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
63 | if (!$pageTitle) { |
64 | $pageTitle = $title; | |
65 | } | |
1beae3b1 | 66 | |
0734e0e8 CW |
67 | // get civi-wordpress instance |
68 | $civi = civi_wp(); | |
1beae3b1 | 69 | |
0734e0e8 CW |
70 | // do we have functionality provided by plugin version 4.6+ present? |
71 | if (method_exists($civi, 'civicrm_context_get')) { | |
1beae3b1 | 72 | |
0734e0e8 CW |
73 | global $civicrm_wp_title; |
74 | $civicrm_wp_title = $pageTitle; | |
1beae3b1 | 75 | |
0734e0e8 CW |
76 | // yes, set page title, depending on context |
77 | $context = civi_wp()->civicrm_context_get(); | |
481a74f4 | 78 | switch ($context) { |
0734e0e8 CW |
79 | case 'admin': |
80 | case 'shortcode': | |
81 | $template = CRM_Core_Smarty::singleton(); | |
82 | $template->assign('pageTitle', $pageTitle); | |
83 | } | |
1beae3b1 | 84 | |
0db6c3e1 TO |
85 | } |
86 | elseif (civicrm_wp_in_civicrm()) { | |
1beae3b1 | 87 | |
0734e0e8 | 88 | // legacy pre-4.6 behaviour |
6a488035 TO |
89 | global $civicrm_wp_title; |
90 | $civicrm_wp_title = $pageTitle; | |
91 | $template = CRM_Core_Smarty::singleton(); | |
92 | $template->assign('pageTitle', $pageTitle); | |
1beae3b1 | 93 | |
6a488035 TO |
94 | } |
95 | } | |
96 | ||
97 | /** | |
98 | * Append an additional breadcrumb tag to the existing breadcrumb | |
99 | * | |
f4aaa82a EM |
100 | * @param $breadCrumbs |
101 | * | |
102 | * @internal param string $title | |
103 | * @internal param string $url | |
6a488035 TO |
104 | * |
105 | * @return void | |
6a488035 TO |
106 | * @static |
107 | */ | |
00be9182 | 108 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
109 | $breadCrumb = wp_get_breadcrumb(); |
110 | ||
111 | if (is_array($breadCrumbs)) { | |
112 | foreach ($breadCrumbs as $crumbs) { | |
113 | if (stripos($crumbs['url'], 'id%%')) { | |
114 | $args = array('cid', 'mid'); | |
115 | foreach ($args as $a) { | |
116 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
117 | FALSE, NULL, $_GET | |
118 | ); | |
119 | if ($val) { | |
120 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
121 | } | |
122 | } | |
123 | } | |
124 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
125 | } | |
126 | } | |
127 | ||
128 | $template = CRM_Core_Smarty::singleton(); | |
129 | $template->assign_by_ref('breadcrumb', $breadCrumb); | |
130 | wp_set_breadcrumb($breadCrumb); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
135 | * | |
136 | * @return void | |
6a488035 TO |
137 | * @static |
138 | */ | |
00be9182 | 139 | public function resetBreadCrumb() { |
6a488035 TO |
140 | $bc = array(); |
141 | wp_set_breadcrumb($bc); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Append a string to the head of the html file | |
146 | * | |
77855840 TO |
147 | * @param string $head |
148 | * The new string to be appended. | |
6a488035 TO |
149 | * |
150 | * @return void | |
6a488035 TO |
151 | * @static |
152 | */ | |
00be9182 | 153 | public function addHTMLHead($head) { |
6a488035 TO |
154 | static $registered = FALSE; |
155 | if (!$registered) { | |
156 | // front-end view | |
157 | add_action('wp_head', array(__CLASS__, '_showHTMLHead')); | |
158 | // back-end views | |
159 | add_action('admin_head', array(__CLASS__, '_showHTMLHead')); | |
160 | } | |
161 | CRM_Core_Region::instance('wp_head')->add(array( | |
162 | 'markup' => $head, | |
163 | )); | |
164 | } | |
165 | ||
00be9182 | 166 | public static function _showHTMLHead() { |
6a488035 TO |
167 | $region = CRM_Core_Region::instance('wp_head', FALSE); |
168 | if ($region) { | |
169 | echo $region->render(''); | |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
174 | * Add a script file | |
175 | * | |
176 | * @param $url: string, absolute path to file | |
77855840 TO |
177 | * @param $region |
178 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
179 | * |
180 | * Note: This function is not to be called directly | |
181 | * @see CRM_Core_Region::render() | |
182 | * | |
183 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
184 | */ |
185 | public function addScriptUrl($url, $region) { | |
186 | return FALSE; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Add an inline script | |
191 | * | |
192 | * @param $code: string, javascript code | |
77855840 TO |
193 | * @param $region |
194 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
195 | * |
196 | * Note: This function is not to be called directly | |
197 | * @see CRM_Core_Region::render() | |
198 | * | |
199 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
200 | */ |
201 | public function addScript($code, $region) { | |
202 | return FALSE; | |
203 | } | |
204 | ||
205 | /** | |
206 | * Add a css file | |
207 | * | |
208 | * @param $url: string, absolute path to file | |
77855840 TO |
209 | * @param $region |
210 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
211 | * |
212 | * Note: This function is not to be called directly | |
213 | * @see CRM_Core_Region::render() | |
214 | * | |
215 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
216 | */ |
217 | public function addStyleUrl($url, $region) { | |
218 | return FALSE; | |
219 | } | |
220 | ||
221 | /** | |
222 | * Add an inline style | |
223 | * | |
224 | * @param $code: string, css code | |
77855840 TO |
225 | * @param $region |
226 | * String, location within the document: 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
227 | * |
228 | * Note: This function is not to be called directly | |
229 | * @see CRM_Core_Region::render() | |
230 | * | |
231 | * @return bool TRUE if we support this operation in this CMS, FALSE otherwise | |
6a488035 TO |
232 | */ |
233 | public function addStyle($code, $region) { | |
234 | return FALSE; | |
235 | } | |
236 | ||
237 | /** | |
100fef9d | 238 | * Rewrite various system urls to https |
6a488035 TO |
239 | * |
240 | * @param null | |
241 | * | |
242 | * @return void | |
6a488035 TO |
243 | * @static |
244 | */ | |
00be9182 | 245 | public function mapConfigToSSL() { |
6a488035 TO |
246 | global $base_url; |
247 | $base_url = str_replace('http://', 'https://', $base_url); | |
248 | } | |
249 | ||
250 | /** | |
100fef9d | 251 | * Figure out the post url for the form |
6a488035 | 252 | * |
77855840 TO |
253 | * @param mix $action |
254 | * The default action if one is pre-specified. | |
6a488035 TO |
255 | * |
256 | * @return string the url to post the form | |
6a488035 TO |
257 | * @static |
258 | */ | |
00be9182 | 259 | public function postURL($action) { |
6a488035 TO |
260 | if (!empty($action)) { |
261 | return $action; | |
262 | } | |
263 | ||
264 | return $this->url($_GET['q'], NULL, TRUE, NULL, FALSE); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) | |
269 | * | |
77855840 TO |
270 | * @param $path |
271 | * String The path being linked to, such as "civicrm/add". | |
272 | * @param $query | |
273 | * String A query string to append to the link. | |
274 | * @param $absolute | |
275 | * Boolean Whether to force the output to be an absolute link (beginning with http:). | |
6a488035 TO |
276 | * Useful for links that will be displayed outside the site, such as in an |
277 | * RSS feed. | |
77855840 TO |
278 | * @param $fragment |
279 | * String A fragment identifier (named anchor) to append to the link. | |
280 | * @param $htmlize | |
281 | * Boolean whether to convert to html eqivalant. | |
282 | * @param $frontend | |
283 | * Boolean a gross joomla hack. | |
6a488035 | 284 | * |
f4aaa82a EM |
285 | * @param bool $forceBackend |
286 | * | |
6a488035 | 287 | * @return string an HTML string containing a link to the given path. |
6a488035 TO |
288 | */ |
289 | function url( | |
290 | $path = NULL, | |
291 | $query = NULL, | |
292 | $absolute = FALSE, | |
293 | $fragment = NULL, | |
294 | $htmlize = TRUE, | |
295 | $frontend = FALSE, | |
296 | $forceBackend = FALSE | |
297 | ) { | |
298 | $config = CRM_Core_Config::singleton(); | |
299 | $script = ''; | |
300 | $separator = $htmlize ? '&' : '&'; | |
887f5d81 TO |
301 | $wpPageParam = ''; |
302 | $fragment = isset($fragment) ? ('#' . $fragment) : ''; | |
6a488035 TO |
303 | |
304 | $path = CRM_Utils_String::stripPathChars($path); | |
305 | ||
306 | //this means wp function we are trying to use is not available, | |
307 | //so load bootStrap | |
308 | if (!function_exists('get_option')) { | |
887f5d81 | 309 | $this->loadBootStrap(); // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap |
6a488035 | 310 | } |
6a488035 | 311 | if ($config->userFrameworkFrontend) { |
887f5d81 | 312 | if (get_option('permalink_structure') != '') { |
6a488035 TO |
313 | global $post; |
314 | $script = get_permalink($post->ID); | |
315 | } | |
316 | ||
01aca362 | 317 | // when shortcode is included in page |
6a488035 TO |
318 | // also make sure we have valid query object |
319 | global $wp_query; | |
481a74f4 | 320 | if (method_exists($wp_query, 'get')) { |
6a488035 | 321 | if (get_query_var('page_id')) { |
887f5d81 | 322 | $wpPageParam = "page_id=" . get_query_var('page_id'); |
6a488035 TO |
323 | } |
324 | elseif (get_query_var('p')) { | |
325 | // when shortcode is inserted in post | |
887f5d81 | 326 | $wpPageParam = "p=" . get_query_var('p'); |
6a488035 TO |
327 | } |
328 | } | |
329 | } | |
330 | ||
887f5d81 TO |
331 | $base = $this->getBaseUrl($absolute, $frontend, $forceBackend); |
332 | ||
333 | if (!isset($path) && !isset($query)) { | |
334 | // FIXME: This short-circuited codepath is the same as the general one below, except | |
335 | // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know | |
336 | // why it's different (and I can only find two obvious use-cases for this codepath, | |
337 | // of which at least one looks gratuitous). A more ambitious person would simply remove | |
338 | // this code. | |
339 | return $base . $fragment; | |
340 | } | |
341 | ||
342 | if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) { | |
343 | $base = $script; | |
6a488035 TO |
344 | } |
345 | ||
887f5d81 TO |
346 | $queryParts = array(); |
347 | if (isset($path)) { | |
348 | $queryParts[] = 'page=CiviCRM'; | |
349 | $queryParts[] = "q={$path}"; | |
6a488035 | 350 | } |
887f5d81 TO |
351 | if ($wpPageParam) { |
352 | $queryParts[] = $wpPageParam; | |
353 | } | |
354 | if (isset($query)) { | |
355 | $queryParts[] = $query; | |
6a488035 TO |
356 | } |
357 | ||
887f5d81 TO |
358 | return $base . '?' . implode($separator, $queryParts) . $fragment; |
359 | } | |
360 | ||
bb3a214a EM |
361 | /** |
362 | * @param $absolute | |
363 | * @param $frontend | |
364 | * @param $forceBackend | |
365 | * | |
366 | * @return mixed|null|string | |
367 | */ | |
887f5d81 TO |
368 | private function getBaseUrl($absolute, $frontend, $forceBackend) { |
369 | $config = CRM_Core_Config::singleton(); | |
6a488035 TO |
370 | |
371 | if (!isset($config->useFrameworkRelativeBase)) { | |
372 | $base = parse_url($config->userFrameworkBaseURL); | |
373 | $config->useFrameworkRelativeBase = $base['path']; | |
374 | } | |
375 | ||
376 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; | |
377 | ||
378 | if ((is_admin() && !$frontend) || $forceBackend) { | |
379 | $base .= 'wp-admin/admin.php'; | |
887f5d81 | 380 | return $base; |
6a488035 TO |
381 | } |
382 | elseif (defined('CIVICRM_UF_WP_BASEPAGE')) { | |
383 | $base .= CIVICRM_UF_WP_BASEPAGE; | |
887f5d81 | 384 | return $base; |
6a488035 | 385 | } |
36b820ae DL |
386 | elseif (isset($config->wpBasePage)) { |
387 | $base .= $config->wpBasePage; | |
887f5d81 | 388 | return $base; |
36b820ae | 389 | } |
887f5d81 | 390 | return $base; |
01aca362 | 391 | } |
6a488035 TO |
392 | |
393 | /** | |
394 | * Authenticate the user against the wordpress db | |
395 | * | |
77855840 TO |
396 | * @param string $name |
397 | * The user name. | |
398 | * @param string $password | |
399 | * The password for the above user name. | |
6a488035 | 400 | * |
f4aaa82a EM |
401 | * @param bool $loadCMSBootstrap |
402 | * @param null $realPath | |
403 | * | |
6a488035 TO |
404 | * @return mixed false if no auth |
405 | * array( | |
f4aaa82a | 406 | * contactID, ufID, unique string ) if success |
6a488035 TO |
407 | * @static |
408 | */ | |
00be9182 | 409 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
410 | $config = CRM_Core_Config::singleton(); |
411 | ||
412 | if ($loadCMSBootstrap) { | |
b6c54d16 | 413 | $config->userSystem->loadBootStrap($name, $password); |
6a488035 TO |
414 | } |
415 | ||
416 | $user = wp_authenticate($name, $password); | |
417 | if (is_a($user, 'WP_Error')) { | |
418 | return FALSE; | |
419 | } | |
420 | ||
421 | // need to change this to make sure we matched only one row | |
422 | ||
423 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); | |
424 | $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); | |
425 | if (!$contactID) { | |
426 | return FALSE; | |
427 | } | |
428 | return array($contactID, $user->data->ID, mt_rand()); | |
429 | } | |
430 | ||
431 | /** | |
432 | * Set a message in the UF to display to a user | |
433 | * | |
77855840 TO |
434 | * @param string $message |
435 | * The message to set. | |
6a488035 | 436 | * |
6a488035 TO |
437 | * @static |
438 | */ | |
00be9182 | 439 | public function setMessage($message) { |
6a488035 TO |
440 | } |
441 | ||
bb3a214a EM |
442 | /** |
443 | * @param $user | |
444 | * | |
445 | * @return bool | |
446 | */ | |
e7292422 TO |
447 | public function loadUser($user) { |
448 | return TRUE; | |
6a488035 TO |
449 | } |
450 | ||
00be9182 | 451 | public function permissionDenied() { |
0499b0ad | 452 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
453 | } |
454 | ||
00be9182 | 455 | public function logout() { |
6a488035 TO |
456 | // destroy session |
457 | if (session_id()) { | |
458 | session_destroy(); | |
459 | } | |
460 | wp_logout(); | |
461 | wp_redirect(wp_login_url()); | |
462 | } | |
463 | ||
e7292422 TO |
464 | public function updateCategories() { |
465 | } | |
6a488035 TO |
466 | |
467 | /** | |
468 | * Get the locale set in the hosting CMS | |
469 | * | |
470 | * @return string with the locale or null for none | |
471 | */ | |
00be9182 | 472 | public function getUFLocale() { |
19780d2b DL |
473 | // WPML plugin |
474 | if (defined('ICL_LANGUAGE_CODE')) { | |
475 | $language = ICL_LANGUAGE_CODE; | |
476 | } | |
477 | ||
478 | // TODO: set language variable for others WordPress plugin | |
479 | ||
480 | if (isset($language)) { | |
481 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2)); | |
0db6c3e1 TO |
482 | } |
483 | else { | |
19780d2b DL |
484 | return NULL; |
485 | } | |
6a488035 TO |
486 | } |
487 | ||
488 | /** | |
100fef9d | 489 | * Load wordpress bootstrap |
6a488035 | 490 | * |
77855840 TO |
491 | * @param $name |
492 | * String optional username for login. | |
493 | * @param $pass | |
494 | * String optional password for login. | |
f4aaa82a EM |
495 | * |
496 | * @return bool | |
6a488035 | 497 | */ |
00be9182 | 498 | public function loadBootStrap($name = NULL, $pass = NULL) { |
6a488035 TO |
499 | global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb; |
500 | ||
501 | $cmsRootPath = $this->cmsRootPath(); | |
502 | if (!$cmsRootPath) { | |
503 | CRM_Core_Error::fatal("Could not find the install directory for WordPress"); | |
504 | } | |
505 | ||
e7292422 | 506 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php'; |
6491539b DL |
507 | $wpUserTimezone = get_option('timezone_string'); |
508 | if ($wpUserTimezone) { | |
509 | date_default_timezone_set($wpUserTimezone); | |
510 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
511 | } | |
e7292422 | 512 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php'; |
fe1e7958 | 513 | $uid = CRM_Utils_Array::value('uid', $name); |
514 | if ($uid) { | |
515 | $account = wp_set_current_user($uid); | |
516 | if ($account && $account->data->ID) { | |
517 | global $user; | |
518 | $user = $account; | |
519 | return TRUE; | |
520 | } | |
521 | } | |
e7292422 | 522 | return TRUE; |
6a488035 TO |
523 | } |
524 | ||
bb3a214a EM |
525 | /** |
526 | * @param $dir | |
527 | * | |
528 | * @return bool | |
529 | */ | |
00be9182 | 530 | public function validInstallDir($dir) { |
dfbcf0b7 | 531 | $includePath = "$dir/wp-includes"; |
f81c7606 | 532 | if (file_exists("$includePath/version.php")) { |
dfbcf0b7 DL |
533 | return TRUE; |
534 | } | |
535 | return FALSE; | |
536 | } | |
537 | ||
bb3a214a EM |
538 | /** |
539 | * Determine the location of the CMS root. | |
540 | * | |
541 | * @return string|NULL local file system path to CMS root, or NULL if it cannot be determined | |
542 | */ | |
543 | /** | |
544 | * @return NULL|string | |
545 | */ | |
00be9182 | 546 | public function cmsRootPath() { |
6a488035 | 547 | $cmsRoot = $valid = NULL; |
dfbcf0b7 DL |
548 | if (defined('CIVICRM_CMSDIR')) { |
549 | if ($this->validInstallDir(CIVICRM_CMSDIR)) { | |
550 | $cmsRoot = CIVICRM_CMSDIR; | |
551 | $valid = TRUE; | |
552 | } | |
6a488035 | 553 | } |
dfbcf0b7 DL |
554 | else { |
555 | $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
6a488035 | 556 | |
dfbcf0b7 DL |
557 | //might be windows installation. |
558 | $firstVar = array_shift($pathVars); | |
559 | if ($firstVar) { | |
560 | $cmsRoot = $firstVar; | |
561 | } | |
562 | ||
563 | //start w/ csm dir search. | |
564 | foreach ($pathVars as $var) { | |
565 | $cmsRoot .= "/$var"; | |
566 | if ($this->validInstallDir($cmsRoot)) { | |
567 | //stop as we found bootstrap. | |
568 | $valid = TRUE; | |
569 | break; | |
570 | } | |
6a488035 TO |
571 | } |
572 | } | |
573 | ||
574 | return ($valid) ? $cmsRoot : NULL; | |
575 | } | |
576 | ||
bb3a214a | 577 | /** |
c490a46a | 578 | * @param array $params |
bb3a214a EM |
579 | * @param $mail |
580 | * | |
581 | * @return mixed | |
582 | */ | |
00be9182 | 583 | public function createUser(&$params, $mail) { |
6a488035 TO |
584 | $user_data = array( |
585 | 'ID' => '', | |
586 | 'user_pass' => $params['cms_pass'], | |
587 | 'user_login' => $params['cms_name'], | |
588 | 'user_email' => $params[$mail], | |
589 | 'nickname' => $params['cms_name'], | |
590 | 'role' => get_option('default_role'), | |
591 | ); | |
592 | if (isset($params['contactID'])) { | |
593 | $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']); | |
594 | if ($contactType == 'Individual') { | |
595 | $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
596 | $params['contactID'], 'first_name' | |
597 | ); | |
598 | $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
599 | $params['contactID'], 'last_name' | |
600 | ); | |
601 | } | |
602 | } | |
603 | ||
604 | $uid = wp_insert_user($user_data); | |
605 | ||
606 | $creds = array(); | |
607 | $creds['user_login'] = $params['cms_name']; | |
608 | $creds['user_password'] = $params['cms_pass']; | |
609 | $creds['remember'] = TRUE; | |
610 | $user = wp_signon($creds, FALSE); | |
611 | ||
612 | wp_new_user_notification($uid, $user_data['user_pass']); | |
613 | return $uid; | |
614 | } | |
615 | ||
f4aaa82a | 616 | /** |
6a488035 TO |
617 | * Change user name in host CMS |
618 | * | |
77855840 TO |
619 | * @param int $ufID |
620 | * User ID in CMS. | |
621 | * @param string $ufName | |
622 | * User name. | |
6a488035 | 623 | */ |
00be9182 | 624 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
625 | // CRM-10620 |
626 | if (function_exists('wp_update_user')) { | |
627 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); | |
628 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); | |
629 | ||
e7292422 | 630 | $values = array('ID' => $ufID, 'user_email' => $ufName); |
481a74f4 TO |
631 | if ($ufID) { |
632 | wp_update_user($values); | |
6a488035 TO |
633 | } |
634 | } | |
635 | } | |
636 | ||
bb3a214a | 637 | /** |
c490a46a | 638 | * @param array $params |
bb3a214a EM |
639 | * @param $errors |
640 | * @param string $emailName | |
641 | */ | |
00be9182 | 642 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
643 | $config = CRM_Core_Config::singleton(); |
644 | ||
645 | $dao = new CRM_Core_DAO(); | |
646 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
647 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); | |
648 | ||
a7488080 | 649 | if (!empty($params['name'])) { |
6a488035 TO |
650 | if (!validate_username($params['name'])) { |
651 | $errors['cms_name'] = ts("Your username contains invalid characters"); | |
652 | } | |
653 | elseif (username_exists(sanitize_user($params['name']))) { | |
654 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name'])); | |
655 | } | |
656 | } | |
657 | ||
a7488080 | 658 | if (!empty($params['mail'])) { |
6a488035 TO |
659 | if (!is_email($params['mail'])) { |
660 | $errors[$emailName] = "Your email is invaid"; | |
661 | } | |
662 | elseif (email_exists($params['mail'])) { | |
663 | $resetUrl = $config->userFrameworkBaseURL . 'wp-login.php?action=lostpassword'; | |
664 | $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>', | |
665 | array(1 => $params['mail'], 2 => $resetUrl) | |
666 | ); | |
667 | } | |
668 | } | |
669 | } | |
670 | ||
671 | /** | |
100fef9d | 672 | * Check is user logged in. |
6a488035 TO |
673 | * |
674 | * @return boolean true/false. | |
675 | */ | |
676 | public function isUserLoggedIn() { | |
677 | $isloggedIn = FALSE; | |
678 | if (function_exists('is_user_logged_in')) { | |
679 | $isloggedIn = is_user_logged_in(); | |
680 | } | |
681 | ||
682 | return $isloggedIn; | |
683 | } | |
684 | ||
bb3a214a EM |
685 | /** |
686 | * @return mixed | |
687 | */ | |
00be9182 | 688 | public function getLoggedInUserObject() { |
2b617cb0 EM |
689 | if (function_exists('is_user_logged_in') && |
690 | is_user_logged_in()) { | |
691 | global $current_user; | |
692 | } | |
693 | return $current_user; | |
694 | } | |
6a488035 TO |
695 | /** |
696 | * Get currently logged in user uf id. | |
697 | * | |
698 | * @return int $userID logged in user uf id. | |
699 | */ | |
700 | public function getLoggedInUfID() { | |
701 | $ufID = NULL; | |
2b617cb0 EM |
702 | $current_user = $this->getLoggedInUserObject(); |
703 | return isset($current_user->ID) ? $current_user->ID : NULL; | |
704 | } | |
705 | ||
706 | /** | |
707 | * Get currently logged in user unique identifier - this tends to be the email address or user name. | |
708 | * | |
709 | * @return string $userID logged in user unique identifier | |
710 | */ | |
00be9182 | 711 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
712 | $user = $this->getLoggedInUserObject(); |
713 | return $this->getUniqueIdentifierFromUserObject($user); | |
6a488035 TO |
714 | } |
715 | ||
32998c82 EM |
716 | /** |
717 | * Get User ID from UserFramework system (Joomla) | |
77855840 TO |
718 | * @param object $user |
719 | * Object as described by the CMS. | |
32998c82 EM |
720 | * @return mixed <NULL, number> |
721 | */ | |
00be9182 | 722 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
723 | return !empty($user->ID) ? $user->ID : NULL; |
724 | } | |
725 | ||
2b617cb0 EM |
726 | /** |
727 | * Get Unique Identifier from UserFramework system (CMS) | |
77855840 TO |
728 | * @param object $user |
729 | * Object as described by the User Framework. | |
2b617cb0 EM |
730 | * @return mixed $uniqueIdentifer Unique identifier from the user Framework system |
731 | * | |
732 | */ | |
00be9182 | 733 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
734 | return empty($user->user_email) ? NULL : $user->user_email; |
735 | } | |
736 | ||
6a488035 TO |
737 | /** |
738 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
739 | * | |
77855840 TO |
740 | * @param string $destination |
741 | * If present, add destination to querystring (works for Drupal only). | |
6a488035 TO |
742 | * |
743 | * @return string - loginURL for the current CMS | |
744 | * | |
745 | */ | |
746 | public function getLoginURL($destination = '') { | |
747 | $config = CRM_Core_Config::singleton(); | |
748 | $loginURL = $config->userFrameworkBaseURL; | |
749 | $loginURL .= 'wp-login.php'; | |
750 | return $loginURL; | |
751 | } | |
752 | ||
bb3a214a | 753 | /** |
c490a46a | 754 | * @param CRM_Core_Form $form |
bb3a214a | 755 | */ |
6a488035 TO |
756 | public function getLoginDestination(&$form) { |
757 | return; | |
758 | } | |
759 | ||
760 | /** | |
761 | * Return the current WordPress version if relevant function exists | |
762 | * | |
763 | * @return string - version number | |
764 | * | |
765 | */ | |
00be9182 | 766 | public function getVersion() { |
6a488035 TO |
767 | if (function_exists('get_bloginfo')) { |
768 | return get_bloginfo('version', 'display'); | |
769 | } | |
770 | else { | |
771 | return 'Unknown'; | |
772 | } | |
773 | } | |
6491539b DL |
774 | |
775 | /** | |
100fef9d | 776 | * Get timezone as a string |
6491539b DL |
777 | * @return string Timezone e.g. 'America/Los_Angeles' |
778 | */ | |
00be9182 | 779 | public function getTimeZoneString() { |
6491539b DL |
780 | return get_option('timezone_string'); |
781 | } | |
59f97da6 EM |
782 | |
783 | /** | |
784 | * Get Url to view user record | |
77855840 TO |
785 | * @param int $contactID |
786 | * Contact ID. | |
59f97da6 EM |
787 | * |
788 | * @return string | |
789 | */ | |
00be9182 | 790 | public function getUserRecordUrl($contactID) { |
59f97da6 EM |
791 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
792 | if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users'))) { | |
793 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid; | |
794 | } | |
795 | } | |
6a488035 | 796 | } |