Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | /** | |
17f443df | 54 | * @inheritDoc |
6a488035 | 55 | */ |
00be9182 | 56 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
57 | if (!$pageTitle) { |
58 | $pageTitle = $title; | |
59 | } | |
1beae3b1 | 60 | |
17f443df CW |
61 | // FIXME: Why is this global? |
62 | global $civicrm_wp_title; | |
63 | $civicrm_wp_title = $title; | |
1beae3b1 | 64 | |
17f443df CW |
65 | // yes, set page title, depending on context |
66 | $context = civi_wp()->civicrm_context_get(); | |
67 | switch ($context) { | |
68 | case 'admin': | |
69 | case 'shortcode': | |
70 | $template = CRM_Core_Smarty::singleton(); | |
71 | $template->assign('pageTitle', $pageTitle); | |
6a488035 TO |
72 | } |
73 | } | |
74 | ||
7ba2c8ad KC |
75 | /** |
76 | * Moved from CRM_Utils_System_Base | |
77 | */ | |
78 | public function getDefaultFileStorage() { | |
79 | global $civicrm_root; | |
80 | $config = CRM_Core_Config::singleton(); | |
81 | $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
82 | ||
83 | $filesURL = NULL; | |
84 | $filesPath = NULL; | |
85 | $upload_dir = wp_upload_dir(); | |
86 | $settingsDir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR; | |
87 | $settingsURL = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR; | |
750752f2 | 88 | if (is_dir(WP_PLUGIN_DIR . '/files/civicrm/')) { |
7ba2c8ad | 89 | //for legacy path |
750752f2 | 90 | $filesURL = WP_PLUGIN_URL . "/files/civicrm/"; |
7ba2c8ad KC |
91 | } |
92 | elseif (is_dir($settingsDir)) { | |
93 | $filesURL = $settingsURL; | |
94 | } | |
95 | else { | |
96 | throw new CRM_Core_Exception("Failed to locate default file storage ($config->userFramework)"); | |
97 | } | |
98 | ||
99 | return array( | |
100 | 'url' => $filesURL, | |
101 | 'path' => CRM_Utils_File::baseFilePath(), | |
102 | ); | |
103 | } | |
104 | ||
6a488035 | 105 | /** |
17f443df | 106 | * @inheritDoc |
6a488035 | 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 | /** | |
17f443df | 134 | * @inheritDoc |
6a488035 | 135 | */ |
00be9182 | 136 | public function resetBreadCrumb() { |
6a488035 TO |
137 | $bc = array(); |
138 | wp_set_breadcrumb($bc); | |
139 | } | |
140 | ||
141 | /** | |
17f443df | 142 | * @inheritDoc |
6a488035 | 143 | */ |
00be9182 | 144 | public function addHTMLHead($head) { |
6a488035 TO |
145 | static $registered = FALSE; |
146 | if (!$registered) { | |
147 | // front-end view | |
148 | add_action('wp_head', array(__CLASS__, '_showHTMLHead')); | |
149 | // back-end views | |
150 | add_action('admin_head', array(__CLASS__, '_showHTMLHead')); | |
151 | } | |
152 | CRM_Core_Region::instance('wp_head')->add(array( | |
153 | 'markup' => $head, | |
154 | )); | |
155 | } | |
156 | ||
17f443df | 157 | /** |
fe482240 | 158 | * WP action callback. |
17f443df | 159 | */ |
00be9182 | 160 | public static function _showHTMLHead() { |
6a488035 TO |
161 | $region = CRM_Core_Region::instance('wp_head', FALSE); |
162 | if ($region) { | |
163 | echo $region->render(''); | |
164 | } | |
165 | } | |
166 | ||
167 | /** | |
17f443df | 168 | * @inheritDoc |
6a488035 | 169 | */ |
00be9182 | 170 | public function mapConfigToSSL() { |
6a488035 TO |
171 | global $base_url; |
172 | $base_url = str_replace('http://', 'https://', $base_url); | |
173 | } | |
174 | ||
175 | /** | |
17f443df | 176 | * @inheritDoc |
6a488035 | 177 | */ |
408b79bf | 178 | public function url( |
6a488035 TO |
179 | $path = NULL, |
180 | $query = NULL, | |
181 | $absolute = FALSE, | |
182 | $fragment = NULL, | |
6a488035 TO |
183 | $frontend = FALSE, |
184 | $forceBackend = FALSE | |
185 | ) { | |
353ffa53 TO |
186 | $config = CRM_Core_Config::singleton(); |
187 | $script = ''; | |
c80e2dbf | 188 | $separator = '&'; |
353ffa53 | 189 | $wpPageParam = ''; |
887f5d81 | 190 | $fragment = isset($fragment) ? ('#' . $fragment) : ''; |
6a488035 TO |
191 | |
192 | $path = CRM_Utils_String::stripPathChars($path); | |
193 | ||
194 | //this means wp function we are trying to use is not available, | |
195 | //so load bootStrap | |
196 | if (!function_exists('get_option')) { | |
887f5d81 | 197 | $this->loadBootStrap(); // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap |
6a488035 | 198 | } |
6a488035 | 199 | if ($config->userFrameworkFrontend) { |
887f5d81 | 200 | if (get_option('permalink_structure') != '') { |
6a488035 TO |
201 | global $post; |
202 | $script = get_permalink($post->ID); | |
203 | } | |
204 | ||
01aca362 | 205 | // when shortcode is included in page |
6a488035 TO |
206 | // also make sure we have valid query object |
207 | global $wp_query; | |
481a74f4 | 208 | if (method_exists($wp_query, 'get')) { |
6a488035 | 209 | if (get_query_var('page_id')) { |
887f5d81 | 210 | $wpPageParam = "page_id=" . get_query_var('page_id'); |
6a488035 TO |
211 | } |
212 | elseif (get_query_var('p')) { | |
213 | // when shortcode is inserted in post | |
887f5d81 | 214 | $wpPageParam = "p=" . get_query_var('p'); |
6a488035 TO |
215 | } |
216 | } | |
217 | } | |
218 | ||
887f5d81 TO |
219 | $base = $this->getBaseUrl($absolute, $frontend, $forceBackend); |
220 | ||
221 | if (!isset($path) && !isset($query)) { | |
222 | // FIXME: This short-circuited codepath is the same as the general one below, except | |
223 | // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know | |
224 | // why it's different (and I can only find two obvious use-cases for this codepath, | |
225 | // of which at least one looks gratuitous). A more ambitious person would simply remove | |
226 | // this code. | |
227 | return $base . $fragment; | |
228 | } | |
229 | ||
230 | if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) { | |
231 | $base = $script; | |
6a488035 TO |
232 | } |
233 | ||
887f5d81 TO |
234 | $queryParts = array(); |
235 | if (isset($path)) { | |
236 | $queryParts[] = 'page=CiviCRM'; | |
237 | $queryParts[] = "q={$path}"; | |
6a488035 | 238 | } |
887f5d81 TO |
239 | if ($wpPageParam) { |
240 | $queryParts[] = $wpPageParam; | |
241 | } | |
242 | if (isset($query)) { | |
243 | $queryParts[] = $query; | |
6a488035 TO |
244 | } |
245 | ||
887f5d81 TO |
246 | return $base . '?' . implode($separator, $queryParts) . $fragment; |
247 | } | |
248 | ||
bb3a214a EM |
249 | /** |
250 | * @param $absolute | |
251 | * @param $frontend | |
252 | * @param $forceBackend | |
253 | * | |
254 | * @return mixed|null|string | |
255 | */ | |
887f5d81 | 256 | private function getBaseUrl($absolute, $frontend, $forceBackend) { |
353ffa53 | 257 | $config = CRM_Core_Config::singleton(); |
6a488035 | 258 | |
6a488035 TO |
259 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; |
260 | ||
261 | if ((is_admin() && !$frontend) || $forceBackend) { | |
262 | $base .= 'wp-admin/admin.php'; | |
887f5d81 | 263 | return $base; |
6a488035 TO |
264 | } |
265 | elseif (defined('CIVICRM_UF_WP_BASEPAGE')) { | |
266 | $base .= CIVICRM_UF_WP_BASEPAGE; | |
887f5d81 | 267 | return $base; |
6a488035 | 268 | } |
36b820ae DL |
269 | elseif (isset($config->wpBasePage)) { |
270 | $base .= $config->wpBasePage; | |
887f5d81 | 271 | return $base; |
36b820ae | 272 | } |
887f5d81 | 273 | return $base; |
01aca362 | 274 | } |
6a488035 TO |
275 | |
276 | /** | |
17f443df | 277 | * @inheritDoc |
6a488035 | 278 | */ |
00be9182 | 279 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
280 | $config = CRM_Core_Config::singleton(); |
281 | ||
282 | if ($loadCMSBootstrap) { | |
b6c54d16 | 283 | $config->userSystem->loadBootStrap($name, $password); |
6a488035 TO |
284 | } |
285 | ||
286 | $user = wp_authenticate($name, $password); | |
287 | if (is_a($user, 'WP_Error')) { | |
288 | return FALSE; | |
289 | } | |
290 | ||
17f443df | 291 | // TODO: need to change this to make sure we matched only one row |
6a488035 TO |
292 | |
293 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); | |
294 | $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); | |
295 | if (!$contactID) { | |
296 | return FALSE; | |
297 | } | |
298 | return array($contactID, $user->data->ID, mt_rand()); | |
299 | } | |
300 | ||
301 | /** | |
17f443df | 302 | * FIXME: Do something |
ea3ddccf | 303 | * |
304 | * @param string $message | |
6a488035 | 305 | */ |
00be9182 | 306 | public function setMessage($message) { |
6a488035 TO |
307 | } |
308 | ||
bb3a214a | 309 | /** |
17f443df | 310 | * FIXME: Do something |
ea3ddccf | 311 | * |
312 | * @param \obj $user | |
313 | * | |
314 | * @return bool | |
bb3a214a | 315 | */ |
e7292422 TO |
316 | public function loadUser($user) { |
317 | return TRUE; | |
6a488035 TO |
318 | } |
319 | ||
17f443df CW |
320 | /** |
321 | * FIXME: Use CMS-native approach | |
322 | */ | |
00be9182 | 323 | public function permissionDenied() { |
0499b0ad | 324 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
325 | } |
326 | ||
17f443df CW |
327 | /** |
328 | * @inheritDoc | |
329 | */ | |
00be9182 | 330 | public function logout() { |
6a488035 TO |
331 | // destroy session |
332 | if (session_id()) { | |
333 | session_destroy(); | |
334 | } | |
335 | wp_logout(); | |
336 | wp_redirect(wp_login_url()); | |
337 | } | |
338 | ||
6a488035 | 339 | /** |
17f443df | 340 | * @inheritDoc |
6a488035 | 341 | */ |
00be9182 | 342 | public function getUFLocale() { |
19780d2b DL |
343 | // WPML plugin |
344 | if (defined('ICL_LANGUAGE_CODE')) { | |
345 | $language = ICL_LANGUAGE_CODE; | |
346 | } | |
347 | ||
348 | // TODO: set language variable for others WordPress plugin | |
349 | ||
350 | if (isset($language)) { | |
351 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2)); | |
0db6c3e1 TO |
352 | } |
353 | else { | |
19780d2b DL |
354 | return NULL; |
355 | } | |
6a488035 TO |
356 | } |
357 | ||
fd1f3a26 SV |
358 | /** |
359 | * @inheritDoc | |
360 | */ | |
361 | public function setUFLocale($civicrm_language) { | |
362 | // TODO (probably not possible with WPML?) | |
363 | return TRUE; | |
364 | } | |
365 | ||
6a488035 | 366 | /** |
fe482240 | 367 | * Load wordpress bootstrap. |
6a488035 | 368 | * |
5a4f6742 CW |
369 | * @param string $name |
370 | * optional username for login. | |
371 | * @param string $pass | |
372 | * optional password for login. | |
f4aaa82a EM |
373 | * |
374 | * @return bool | |
6a488035 | 375 | */ |
00be9182 | 376 | public function loadBootStrap($name = NULL, $pass = NULL) { |
05fcde76 | 377 | global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user; |
6a488035 | 378 | |
7a44e49f | 379 | if (!defined('WP_USE_THEMES')) { |
c5f77355 | 380 | define('WP_USE_THEMES', FALSE); |
7a44e49f | 381 | } |
6a488035 TO |
382 | |
383 | $cmsRootPath = $this->cmsRootPath(); | |
384 | if (!$cmsRootPath) { | |
385 | CRM_Core_Error::fatal("Could not find the install directory for WordPress"); | |
386 | } | |
aaffa79f | 387 | $path = Civi::settings()->get('wpLoadPhp'); |
b299b1cc | 388 | if (!empty($path)) { |
35da5d8d | 389 | require_once $path; |
b299b1cc KC |
390 | } |
391 | elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) { | |
35da5d8d | 392 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php'; |
b299b1cc KC |
393 | } |
394 | else { | |
395 | CRM_Core_Error::fatal("Could not find the bootstrap file for WordPress"); | |
35da5d8d | 396 | } |
6491539b DL |
397 | $wpUserTimezone = get_option('timezone_string'); |
398 | if ($wpUserTimezone) { | |
399 | date_default_timezone_set($wpUserTimezone); | |
400 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
401 | } | |
e7292422 | 402 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php'; |
fe1e7958 | 403 | $uid = CRM_Utils_Array::value('uid', $name); |
17763922 WA |
404 | if (!$uid) { |
405 | $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
406 | $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
407 | if ($name) { | |
a4111333 | 408 | $uid = wp_authenticate($name, $pass); // this returns a WP_User object if successful |
17763922 WA |
409 | if (!$uid) { |
410 | if ($throwError) { | |
411 | echo '<br />Sorry, unrecognized username or password.'; | |
412 | exit(); | |
413 | } | |
414 | return FALSE; | |
415 | } | |
416 | } | |
417 | } | |
fe1e7958 | 418 | if ($uid) { |
a4111333 CW |
419 | if ($uid instanceof WP_User) { |
420 | $account = wp_set_current_user($uid->ID); | |
c5f77355 CW |
421 | } |
422 | else { | |
a4111333 CW |
423 | $account = wp_set_current_user($uid); |
424 | } | |
fe1e7958 | 425 | if ($account && $account->data->ID) { |
426 | global $user; | |
427 | $user = $account; | |
428 | return TRUE; | |
429 | } | |
430 | } | |
e7292422 | 431 | return TRUE; |
6a488035 TO |
432 | } |
433 | ||
bb3a214a EM |
434 | /** |
435 | * @param $dir | |
436 | * | |
437 | * @return bool | |
438 | */ | |
00be9182 | 439 | public function validInstallDir($dir) { |
dfbcf0b7 | 440 | $includePath = "$dir/wp-includes"; |
f81c7606 | 441 | if (file_exists("$includePath/version.php")) { |
dfbcf0b7 DL |
442 | return TRUE; |
443 | } | |
444 | return FALSE; | |
445 | } | |
446 | ||
bb3a214a EM |
447 | /** |
448 | * Determine the location of the CMS root. | |
449 | * | |
72b3a70c CW |
450 | * @return string|NULL |
451 | * local file system path to CMS root, or NULL if it cannot be determined | |
bb3a214a | 452 | */ |
00be9182 | 453 | public function cmsRootPath() { |
6a488035 | 454 | $cmsRoot = $valid = NULL; |
dfbcf0b7 DL |
455 | if (defined('CIVICRM_CMSDIR')) { |
456 | if ($this->validInstallDir(CIVICRM_CMSDIR)) { | |
457 | $cmsRoot = CIVICRM_CMSDIR; | |
458 | $valid = TRUE; | |
459 | } | |
6a488035 | 460 | } |
dfbcf0b7 DL |
461 | else { |
462 | $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
6a488035 | 463 | |
dfbcf0b7 DL |
464 | //might be windows installation. |
465 | $firstVar = array_shift($pathVars); | |
466 | if ($firstVar) { | |
467 | $cmsRoot = $firstVar; | |
468 | } | |
469 | ||
470 | //start w/ csm dir search. | |
471 | foreach ($pathVars as $var) { | |
472 | $cmsRoot .= "/$var"; | |
473 | if ($this->validInstallDir($cmsRoot)) { | |
474 | //stop as we found bootstrap. | |
475 | $valid = TRUE; | |
476 | break; | |
477 | } | |
6a488035 TO |
478 | } |
479 | } | |
480 | ||
481 | return ($valid) ? $cmsRoot : NULL; | |
482 | } | |
483 | ||
bb3a214a | 484 | /** |
17f443df | 485 | * @inheritDoc |
bb3a214a | 486 | */ |
00be9182 | 487 | public function createUser(&$params, $mail) { |
6a488035 TO |
488 | $user_data = array( |
489 | 'ID' => '', | |
490 | 'user_pass' => $params['cms_pass'], | |
491 | 'user_login' => $params['cms_name'], | |
492 | 'user_email' => $params[$mail], | |
493 | 'nickname' => $params['cms_name'], | |
494 | 'role' => get_option('default_role'), | |
495 | ); | |
496 | if (isset($params['contactID'])) { | |
497 | $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']); | |
498 | if ($contactType == 'Individual') { | |
499 | $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
500 | $params['contactID'], 'first_name' | |
501 | ); | |
502 | $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
503 | $params['contactID'], 'last_name' | |
504 | ); | |
505 | } | |
506 | } | |
507 | ||
508 | $uid = wp_insert_user($user_data); | |
509 | ||
510 | $creds = array(); | |
511 | $creds['user_login'] = $params['cms_name']; | |
512 | $creds['user_password'] = $params['cms_pass']; | |
513 | $creds['remember'] = TRUE; | |
514 | $user = wp_signon($creds, FALSE); | |
515 | ||
516 | wp_new_user_notification($uid, $user_data['user_pass']); | |
517 | return $uid; | |
518 | } | |
519 | ||
f4aaa82a | 520 | /** |
17f443df | 521 | * @inheritDoc |
6a488035 | 522 | */ |
00be9182 | 523 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
524 | // CRM-10620 |
525 | if (function_exists('wp_update_user')) { | |
353ffa53 | 526 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); |
6a488035 TO |
527 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); |
528 | ||
e7292422 | 529 | $values = array('ID' => $ufID, 'user_email' => $ufName); |
481a74f4 TO |
530 | if ($ufID) { |
531 | wp_update_user($values); | |
6a488035 TO |
532 | } |
533 | } | |
534 | } | |
535 | ||
bb3a214a | 536 | /** |
c490a46a | 537 | * @param array $params |
bb3a214a EM |
538 | * @param $errors |
539 | * @param string $emailName | |
540 | */ | |
00be9182 | 541 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
542 | $config = CRM_Core_Config::singleton(); |
543 | ||
353ffa53 TO |
544 | $dao = new CRM_Core_DAO(); |
545 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
546 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
547 | ||
a7488080 | 548 | if (!empty($params['name'])) { |
6a488035 TO |
549 | if (!validate_username($params['name'])) { |
550 | $errors['cms_name'] = ts("Your username contains invalid characters"); | |
551 | } | |
552 | elseif (username_exists(sanitize_user($params['name']))) { | |
553 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', array(1 => $params['name'])); | |
554 | } | |
555 | } | |
556 | ||
a7488080 | 557 | if (!empty($params['mail'])) { |
6a488035 TO |
558 | if (!is_email($params['mail'])) { |
559 | $errors[$emailName] = "Your email is invaid"; | |
560 | } | |
561 | elseif (email_exists($params['mail'])) { | |
562 | $resetUrl = $config->userFrameworkBaseURL . 'wp-login.php?action=lostpassword'; | |
db18d815 | 563 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
6a488035 TO |
564 | array(1 => $params['mail'], 2 => $resetUrl) |
565 | ); | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | /** | |
17f443df | 571 | * @inheritDoc |
6a488035 TO |
572 | */ |
573 | public function isUserLoggedIn() { | |
574 | $isloggedIn = FALSE; | |
575 | if (function_exists('is_user_logged_in')) { | |
576 | $isloggedIn = is_user_logged_in(); | |
577 | } | |
578 | ||
579 | return $isloggedIn; | |
580 | } | |
581 | ||
bb3a214a EM |
582 | /** |
583 | * @return mixed | |
584 | */ | |
00be9182 | 585 | public function getLoggedInUserObject() { |
2b617cb0 | 586 | if (function_exists('is_user_logged_in') && |
353ffa53 TO |
587 | is_user_logged_in() |
588 | ) { | |
2b617cb0 EM |
589 | global $current_user; |
590 | } | |
591 | return $current_user; | |
592 | } | |
353ffa53 | 593 | |
6a488035 | 594 | /** |
17f443df | 595 | * @inheritDoc |
6a488035 TO |
596 | */ |
597 | public function getLoggedInUfID() { | |
598 | $ufID = NULL; | |
2b617cb0 EM |
599 | $current_user = $this->getLoggedInUserObject(); |
600 | return isset($current_user->ID) ? $current_user->ID : NULL; | |
601 | } | |
602 | ||
603 | /** | |
17f443df | 604 | * @inheritDoc |
2b617cb0 | 605 | */ |
00be9182 | 606 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
607 | $user = $this->getLoggedInUserObject(); |
608 | return $this->getUniqueIdentifierFromUserObject($user); | |
6a488035 TO |
609 | } |
610 | ||
32998c82 EM |
611 | /** |
612 | * Get User ID from UserFramework system (Joomla) | |
77855840 TO |
613 | * @param object $user |
614 | * Object as described by the CMS. | |
72b3a70c CW |
615 | * |
616 | * @return int|null | |
32998c82 | 617 | */ |
00be9182 | 618 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
619 | return !empty($user->ID) ? $user->ID : NULL; |
620 | } | |
621 | ||
2b617cb0 | 622 | /** |
17f443df | 623 | * @inheritDoc |
2b617cb0 | 624 | */ |
00be9182 | 625 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
626 | return empty($user->user_email) ? NULL : $user->user_email; |
627 | } | |
628 | ||
6a488035 | 629 | /** |
17f443df | 630 | * @inheritDoc |
6a488035 TO |
631 | */ |
632 | public function getLoginURL($destination = '') { | |
633 | $config = CRM_Core_Config::singleton(); | |
634 | $loginURL = $config->userFrameworkBaseURL; | |
635 | $loginURL .= 'wp-login.php'; | |
636 | return $loginURL; | |
637 | } | |
638 | ||
bb3a214a | 639 | /** |
ad37ac8e | 640 | * FIXME: Do something. |
641 | * | |
642 | * @param \CRM_Core_Form $form | |
643 | * | |
644 | * @return NULL|string | |
bb3a214a | 645 | */ |
6a488035 | 646 | public function getLoginDestination(&$form) { |
408b79bf | 647 | return NULL; |
6a488035 TO |
648 | } |
649 | ||
650 | /** | |
17f443df | 651 | * @inheritDoc |
6a488035 | 652 | */ |
00be9182 | 653 | public function getVersion() { |
6a488035 TO |
654 | if (function_exists('get_bloginfo')) { |
655 | return get_bloginfo('version', 'display'); | |
656 | } | |
657 | else { | |
658 | return 'Unknown'; | |
659 | } | |
660 | } | |
6491539b DL |
661 | |
662 | /** | |
17f443df | 663 | * @inheritDoc |
6491539b | 664 | */ |
00be9182 | 665 | public function getTimeZoneString() { |
6491539b DL |
666 | return get_option('timezone_string'); |
667 | } | |
59f97da6 EM |
668 | |
669 | /** | |
17f443df | 670 | * @inheritDoc |
59f97da6 | 671 | */ |
00be9182 | 672 | public function getUserRecordUrl($contactID) { |
59f97da6 | 673 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
353ffa53 TO |
674 | if (CRM_Core_Session::singleton() |
675 | ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users')) | |
676 | ) { | |
59f97da6 EM |
677 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid; |
678 | } | |
679 | } | |
96025800 | 680 | |
469d8dab CW |
681 | /** |
682 | * Append WP js to coreResourcesList. | |
ad37ac8e | 683 | * |
684 | * @param array $list | |
469d8dab CW |
685 | */ |
686 | public function appendCoreResources(&$list) { | |
687 | $list[] = 'js/crm.wordpress.js'; | |
688 | } | |
689 | ||
03d5592a CW |
690 | /** |
691 | * @inheritDoc | |
692 | */ | |
693 | public function synchronizeUsers() { | |
694 | $config = CRM_Core_Config::singleton(); | |
695 | if (PHP_SAPI != 'cli') { | |
696 | set_time_limit(300); | |
697 | } | |
698 | $id = 'ID'; | |
699 | $mail = 'user_email'; | |
700 | ||
701 | $uf = $config->userFramework; | |
702 | $contactCount = 0; | |
703 | $contactCreated = 0; | |
704 | $contactMatching = 0; | |
705 | ||
706 | global $wpdb; | |
707 | $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users"); | |
708 | ||
709 | foreach ($wpUserIds as $wpUserId) { | |
710 | $wpUserData = get_userdata($wpUserId); | |
711 | $contactCount++; | |
712 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, | |
713 | $wpUserData->$id, | |
714 | $wpUserData->$mail, | |
715 | $uf, | |
716 | 1, | |
717 | 'Individual', | |
718 | TRUE | |
719 | ) | |
720 | ) { | |
721 | $contactCreated++; | |
722 | } | |
723 | else { | |
724 | $contactMatching++; | |
725 | } | |
726 | if (is_object($match)) { | |
727 | $match->free(); | |
728 | } | |
729 | } | |
730 | ||
731 | return array( | |
732 | 'contactCount' => $contactCount, | |
733 | 'contactMatching' => $contactMatching, | |
734 | 'contactCreated' => $contactCreated, | |
735 | ); | |
736 | } | |
737 | ||
6a488035 | 738 | } |