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