Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 { | |
6714d8d2 | 40 | |
bb3a214a | 41 | /** |
bb3a214a | 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 | /** | |
17f443df | 55 | * @inheritDoc |
6a488035 | 56 | */ |
00be9182 | 57 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
58 | if (!$pageTitle) { |
59 | $pageTitle = $title; | |
60 | } | |
1beae3b1 | 61 | |
17f443df CW |
62 | // FIXME: Why is this global? |
63 | global $civicrm_wp_title; | |
64 | $civicrm_wp_title = $title; | |
1beae3b1 | 65 | |
17f443df CW |
66 | // yes, set page title, depending on context |
67 | $context = civi_wp()->civicrm_context_get(); | |
68 | switch ($context) { | |
69 | case 'admin': | |
70 | case 'shortcode': | |
71 | $template = CRM_Core_Smarty::singleton(); | |
72 | $template->assign('pageTitle', $pageTitle); | |
6a488035 TO |
73 | } |
74 | } | |
75 | ||
7ba2c8ad KC |
76 | /** |
77 | * Moved from CRM_Utils_System_Base | |
78 | */ | |
79 | public function getDefaultFileStorage() { | |
8ce9d9d6 TO |
80 | $config = CRM_Core_Config::singleton(); |
81 | $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
82 | $cmsPath = $this->cmsRootPath(); | |
83 | $filesPath = CRM_Utils_File::baseFilePath(); | |
84 | $filesRelPath = CRM_Utils_File::relativize($filesPath, $cmsPath); | |
85 | $filesURL = rtrim($cmsUrl, '/') . '/' . ltrim($filesRelPath, ' /'); | |
be2fb01f | 86 | return [ |
8ce9d9d6 TO |
87 | 'url' => CRM_Utils_File::addTrailingSlash($filesURL, '/'), |
88 | 'path' => CRM_Utils_File::addTrailingSlash($filesPath), | |
be2fb01f | 89 | ]; |
8ce9d9d6 TO |
90 | } |
91 | ||
92 | /** | |
93 | * Determine the location of the CiviCRM source tree. | |
94 | * | |
95 | * @return array | |
96 | * - url: string. ex: "http://example.com/sites/all/modules/civicrm" | |
97 | * - path: string. ex: "/var/www/sites/all/modules/civicrm" | |
98 | */ | |
99 | public function getCiviSourceStorage() { | |
7ba2c8ad | 100 | global $civicrm_root; |
7ba2c8ad | 101 | |
8ce9d9d6 TO |
102 | // Don't use $config->userFrameworkBaseURL; it has garbage on it. |
103 | // More generally, we shouldn't be using $config here. | |
104 | if (!defined('CIVICRM_UF_BASEURL')) { | |
105 | throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL'); | |
7ba2c8ad | 106 | } |
8ce9d9d6 TO |
107 | |
108 | $cmsPath = $this->cmsRootPath(); | |
109 | ||
110 | // $config = CRM_Core_Config::singleton(); | |
111 | // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE); | |
112 | $cmsUrl = CIVICRM_UF_BASEURL; | |
113 | if (CRM_Utils_System::isSSL()) { | |
114 | $cmsUrl = str_replace('http://', 'https://', $cmsUrl); | |
7ba2c8ad | 115 | } |
d8182404 | 116 | $civiRelPath = CRM_Utils_File::relativize(realpath($civicrm_root), realpath($cmsPath)); |
8ce9d9d6 | 117 | $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /'); |
be2fb01f | 118 | return [ |
8ce9d9d6 TO |
119 | 'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'), |
120 | 'path' => CRM_Utils_File::addTrailingSlash($civicrm_root), | |
be2fb01f | 121 | ]; |
7ba2c8ad KC |
122 | } |
123 | ||
6a488035 | 124 | /** |
17f443df | 125 | * @inheritDoc |
6a488035 | 126 | */ |
00be9182 | 127 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
128 | $breadCrumb = wp_get_breadcrumb(); |
129 | ||
130 | if (is_array($breadCrumbs)) { | |
131 | foreach ($breadCrumbs as $crumbs) { | |
132 | if (stripos($crumbs['url'], 'id%%')) { | |
be2fb01f | 133 | $args = ['cid', 'mid']; |
6a488035 TO |
134 | foreach ($args as $a) { |
135 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
136 | FALSE, NULL, $_GET | |
137 | ); | |
138 | if ($val) { | |
139 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
140 | } | |
141 | } | |
142 | } | |
143 | $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>"; | |
144 | } | |
145 | } | |
146 | ||
147 | $template = CRM_Core_Smarty::singleton(); | |
148 | $template->assign_by_ref('breadcrumb', $breadCrumb); | |
149 | wp_set_breadcrumb($breadCrumb); | |
150 | } | |
151 | ||
152 | /** | |
17f443df | 153 | * @inheritDoc |
6a488035 | 154 | */ |
00be9182 | 155 | public function resetBreadCrumb() { |
be2fb01f | 156 | $bc = []; |
6a488035 TO |
157 | wp_set_breadcrumb($bc); |
158 | } | |
159 | ||
160 | /** | |
17f443df | 161 | * @inheritDoc |
6a488035 | 162 | */ |
00be9182 | 163 | public function addHTMLHead($head) { |
6a488035 TO |
164 | static $registered = FALSE; |
165 | if (!$registered) { | |
166 | // front-end view | |
be2fb01f | 167 | add_action('wp_head', [__CLASS__, '_showHTMLHead']); |
6a488035 | 168 | // back-end views |
be2fb01f | 169 | add_action('admin_head', [__CLASS__, '_showHTMLHead']); |
6a488035 | 170 | } |
be2fb01f | 171 | CRM_Core_Region::instance('wp_head')->add([ |
6a488035 | 172 | 'markup' => $head, |
be2fb01f | 173 | ]); |
6a488035 TO |
174 | } |
175 | ||
17f443df | 176 | /** |
fe482240 | 177 | * WP action callback. |
17f443df | 178 | */ |
00be9182 | 179 | public static function _showHTMLHead() { |
6a488035 TO |
180 | $region = CRM_Core_Region::instance('wp_head', FALSE); |
181 | if ($region) { | |
182 | echo $region->render(''); | |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
17f443df | 187 | * @inheritDoc |
6a488035 | 188 | */ |
00be9182 | 189 | public function mapConfigToSSL() { |
6a488035 TO |
190 | global $base_url; |
191 | $base_url = str_replace('http://', 'https://', $base_url); | |
192 | } | |
193 | ||
194 | /** | |
17f443df | 195 | * @inheritDoc |
6a488035 | 196 | */ |
408b79bf | 197 | public function url( |
6a488035 TO |
198 | $path = NULL, |
199 | $query = NULL, | |
200 | $absolute = FALSE, | |
201 | $fragment = NULL, | |
6a488035 TO |
202 | $frontend = FALSE, |
203 | $forceBackend = FALSE | |
204 | ) { | |
353ffa53 TO |
205 | $config = CRM_Core_Config::singleton(); |
206 | $script = ''; | |
c80e2dbf | 207 | $separator = '&'; |
353ffa53 | 208 | $wpPageParam = ''; |
887f5d81 | 209 | $fragment = isset($fragment) ? ('#' . $fragment) : ''; |
6a488035 TO |
210 | |
211 | $path = CRM_Utils_String::stripPathChars($path); | |
df17aa21 | 212 | $basepage = FALSE; |
6a488035 TO |
213 | |
214 | //this means wp function we are trying to use is not available, | |
215 | //so load bootStrap | |
d8182404 | 216 | // FIXME: Why bootstrap in url()? Generally want to define 1-2 strategic places to put bootstrap |
6a488035 | 217 | if (!function_exists('get_option')) { |
d8182404 | 218 | $this->loadBootStrap(); |
6a488035 | 219 | } |
df17aa21 | 220 | |
6a488035 | 221 | if ($config->userFrameworkFrontend) { |
df17aa21 | 222 | global $post; |
887f5d81 | 223 | if (get_option('permalink_structure') != '') { |
6a488035 TO |
224 | $script = get_permalink($post->ID); |
225 | } | |
df17aa21 CW |
226 | if ($config->wpBasePage == $post->post_name) { |
227 | $basepage = TRUE; | |
228 | } | |
01aca362 | 229 | // when shortcode is included in page |
6a488035 | 230 | // also make sure we have valid query object |
df17aa21 | 231 | // FIXME: $wpPageParam has no effect and is only set on the *basepage* |
6a488035 | 232 | global $wp_query; |
df17aa21 | 233 | if (get_option('permalink_structure') == '' && method_exists($wp_query, 'get')) { |
6a488035 | 234 | if (get_query_var('page_id')) { |
887f5d81 | 235 | $wpPageParam = "page_id=" . get_query_var('page_id'); |
6a488035 TO |
236 | } |
237 | elseif (get_query_var('p')) { | |
238 | // when shortcode is inserted in post | |
887f5d81 | 239 | $wpPageParam = "p=" . get_query_var('p'); |
6a488035 TO |
240 | } |
241 | } | |
242 | } | |
243 | ||
887f5d81 TO |
244 | $base = $this->getBaseUrl($absolute, $frontend, $forceBackend); |
245 | ||
246 | if (!isset($path) && !isset($query)) { | |
247 | // FIXME: This short-circuited codepath is the same as the general one below, except | |
248 | // in that it ignores "permlink_structure" / $wpPageParam / $script . I don't know | |
249 | // why it's different (and I can only find two obvious use-cases for this codepath, | |
250 | // of which at least one looks gratuitous). A more ambitious person would simply remove | |
251 | // this code. | |
252 | return $base . $fragment; | |
253 | } | |
254 | ||
255 | if (!$forceBackend && get_option('permalink_structure') != '' && ($wpPageParam || $script != '')) { | |
256 | $base = $script; | |
6a488035 TO |
257 | } |
258 | ||
be2fb01f | 259 | $queryParts = []; |
df17aa21 | 260 | |
df17aa21 CW |
261 | if ( |
262 | // not using clean URLs | |
263 | !$config->cleanURL | |
264 | // requesting an admin URL | |
265 | || ((is_admin() && !$frontend) || $forceBackend) | |
266 | // is shortcode | |
267 | || (!$basepage && $script != '') | |
268 | ) { | |
269 | ||
270 | // pre-existing logic | |
271 | if (isset($path)) { | |
272 | $queryParts[] = 'page=CiviCRM'; | |
273 | // Encode all but the *path* placeholder | |
274 | if ($path !== '*path*') { | |
275 | $path = rawurlencode($path); | |
276 | } | |
277 | $queryParts[] = "q={$path}"; | |
278 | } | |
279 | if ($wpPageParam) { | |
280 | $queryParts[] = $wpPageParam; | |
281 | } | |
ffa62912 | 282 | if (!empty($query)) { |
df17aa21 CW |
283 | $queryParts[] = $query; |
284 | } | |
285 | ||
286 | $final = $base . '?' . implode($separator, $queryParts) . $fragment; | |
287 | ||
887f5d81 | 288 | } |
df17aa21 CW |
289 | else { |
290 | ||
291 | // clean URLs | |
292 | if (isset($path)) { | |
293 | $base = trailingslashit($base) . str_replace('civicrm/', '', $path) . '/'; | |
294 | } | |
295 | if (isset($query)) { | |
296 | $query = ltrim($query, '=?&'); | |
297 | $queryParts[] = $query; | |
298 | } | |
299 | ||
300 | if (!empty($queryParts)) { | |
301 | $final = $base . '?' . implode($separator, $queryParts) . $fragment; | |
302 | } | |
303 | else { | |
304 | $final = $base . $fragment; | |
305 | } | |
306 | ||
6a488035 TO |
307 | } |
308 | ||
df17aa21 | 309 | return $final; |
887f5d81 TO |
310 | } |
311 | ||
bb3a214a | 312 | /** |
f553d1ea KC |
313 | * 27-09-2016 |
314 | * CRM-16421 CRM-17633 WIP Changes to support WP in it's own directory | |
315 | * https://wiki.civicrm.org/confluence/display/CRM/WordPress+installed+in+its+own+directory+issues | |
316 | * For now leave hard coded wp-admin references. | |
317 | * TODO: remove wp-admin references and replace with admin_url() in the future. Look at best way to get path to admin_url | |
318 | * | |
bb3a214a EM |
319 | * @param $absolute |
320 | * @param $frontend | |
321 | * @param $forceBackend | |
322 | * | |
323 | * @return mixed|null|string | |
324 | */ | |
887f5d81 | 325 | private function getBaseUrl($absolute, $frontend, $forceBackend) { |
353ffa53 | 326 | $config = CRM_Core_Config::singleton(); |
6a488035 | 327 | if ((is_admin() && !$frontend) || $forceBackend) { |
f553d1ea | 328 | return Civi::paths()->getUrl('[wp.backend]/.', $absolute ? 'absolute' : 'relative'); |
6a488035 | 329 | } |
f553d1ea KC |
330 | else { |
331 | return Civi::paths()->getUrl('[wp.frontend]/.', $absolute ? 'absolute' : 'relative'); | |
36b820ae | 332 | } |
01aca362 | 333 | } |
6a488035 TO |
334 | |
335 | /** | |
17f443df | 336 | * @inheritDoc |
6a488035 | 337 | */ |
00be9182 | 338 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
339 | $config = CRM_Core_Config::singleton(); |
340 | ||
341 | if ($loadCMSBootstrap) { | |
9ba02e3e TO |
342 | $config->userSystem->loadBootStrap([ |
343 | 'name' => $name, | |
344 | 'pass' => $password, | |
345 | ]); | |
6a488035 TO |
346 | } |
347 | ||
348 | $user = wp_authenticate($name, $password); | |
349 | if (is_a($user, 'WP_Error')) { | |
350 | return FALSE; | |
351 | } | |
352 | ||
17f443df | 353 | // TODO: need to change this to make sure we matched only one row |
6a488035 TO |
354 | |
355 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($user->data, $user->data->ID, $user->data->user_email, 'WordPress'); | |
356 | $contactID = CRM_Core_BAO_UFMatch::getContactId($user->data->ID); | |
357 | if (!$contactID) { | |
358 | return FALSE; | |
359 | } | |
be2fb01f | 360 | return [$contactID, $user->data->ID, mt_rand()]; |
6a488035 TO |
361 | } |
362 | ||
363 | /** | |
17f443df | 364 | * FIXME: Do something |
ea3ddccf | 365 | * |
366 | * @param string $message | |
6a488035 | 367 | */ |
00be9182 | 368 | public function setMessage($message) { |
6a488035 TO |
369 | } |
370 | ||
bb3a214a | 371 | /** |
b596c3e9 | 372 | * @param \string $user |
ea3ddccf | 373 | * |
374 | * @return bool | |
bb3a214a | 375 | */ |
e7292422 | 376 | public function loadUser($user) { |
b596c3e9 | 377 | $userdata = get_user_by('login', $user); |
378 | if (!$userdata->data->ID) { | |
7ca9cd52 | 379 | return FALSE; |
b596c3e9 | 380 | } |
381 | ||
382 | $uid = $userdata->data->ID; | |
383 | wp_set_current_user($uid); | |
384 | $contactID = CRM_Core_BAO_UFMatch::getContactId($uid); | |
385 | ||
386 | // lets store contact id and user id in session | |
387 | $session = CRM_Core_Session::singleton(); | |
388 | $session->set('ufID', $uid); | |
389 | $session->set('userID', $contactID); | |
e7292422 | 390 | return TRUE; |
6a488035 TO |
391 | } |
392 | ||
17f443df CW |
393 | /** |
394 | * FIXME: Use CMS-native approach | |
395 | */ | |
00be9182 | 396 | public function permissionDenied() { |
0499b0ad | 397 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
398 | } |
399 | ||
8ee9bea9 SL |
400 | /** |
401 | * Determine the native ID of the CMS user. | |
402 | * | |
403 | * @param string $username | |
404 | * @return int|NULL | |
405 | */ | |
406 | public function getUfId($username) { | |
407 | $userdata = get_user_by('login', $username); | |
408 | if (!$userdata->data->ID) { | |
409 | return NULL; | |
410 | } | |
411 | return $userdata->data->ID; | |
412 | } | |
413 | ||
17f443df CW |
414 | /** |
415 | * @inheritDoc | |
416 | */ | |
00be9182 | 417 | public function logout() { |
6a488035 TO |
418 | // destroy session |
419 | if (session_id()) { | |
420 | session_destroy(); | |
421 | } | |
422 | wp_logout(); | |
423 | wp_redirect(wp_login_url()); | |
424 | } | |
425 | ||
6a488035 | 426 | /** |
17f443df | 427 | * @inheritDoc |
6a488035 | 428 | */ |
00be9182 | 429 | public function getUFLocale() { |
cba2601a SV |
430 | // Polylang plugin |
431 | if (function_exists('pll_current_language')) { | |
432 | $language = pll_current_language(); | |
433 | } | |
19780d2b | 434 | // WPML plugin |
cba2601a | 435 | elseif (defined('ICL_LANGUAGE_CODE')) { |
19780d2b DL |
436 | $language = ICL_LANGUAGE_CODE; |
437 | } | |
438 | ||
439 | // TODO: set language variable for others WordPress plugin | |
440 | ||
5a44748c | 441 | if (!empty($language)) { |
19780d2b | 442 | return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2)); |
0db6c3e1 TO |
443 | } |
444 | else { | |
19780d2b DL |
445 | return NULL; |
446 | } | |
6a488035 TO |
447 | } |
448 | ||
fd1f3a26 SV |
449 | /** |
450 | * @inheritDoc | |
451 | */ | |
452 | public function setUFLocale($civicrm_language) { | |
453 | // TODO (probably not possible with WPML?) | |
454 | return TRUE; | |
455 | } | |
456 | ||
6a488035 | 457 | /** |
fe482240 | 458 | * Load wordpress bootstrap. |
6a488035 | 459 | * |
9ba02e3e TO |
460 | * @param array $params |
461 | * Optional credentials | |
462 | * - name: string, cms username | |
463 | * - pass: string, cms password | |
6714d8d2 SL |
464 | * @param bool $loadUser |
465 | * @param bool $throwError | |
466 | * @param mixed $realPath | |
f4aaa82a EM |
467 | * |
468 | * @return bool | |
6a488035 | 469 | */ |
be2fb01f | 470 | public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
05fcde76 | 471 | global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user; |
6a488035 | 472 | |
9ba02e3e TO |
473 | $name = CRM_Utils_Array::value('name', $params); |
474 | $pass = CRM_Utils_Array::value('pass', $params); | |
475 | if (isset($params['uid'])) { | |
476 | throw new \RuntimeException("Not implemented WordPress::loadBootStrap([uid=>\$num]))"); | |
477 | } | |
478 | ||
7a44e49f | 479 | if (!defined('WP_USE_THEMES')) { |
c5f77355 | 480 | define('WP_USE_THEMES', FALSE); |
7a44e49f | 481 | } |
6a488035 TO |
482 | |
483 | $cmsRootPath = $this->cmsRootPath(); | |
484 | if (!$cmsRootPath) { | |
485 | CRM_Core_Error::fatal("Could not find the install directory for WordPress"); | |
486 | } | |
aaffa79f | 487 | $path = Civi::settings()->get('wpLoadPhp'); |
b299b1cc | 488 | if (!empty($path)) { |
35da5d8d | 489 | require_once $path; |
b299b1cc KC |
490 | } |
491 | elseif (file_exists($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php')) { | |
35da5d8d | 492 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php'; |
b299b1cc KC |
493 | } |
494 | else { | |
495 | CRM_Core_Error::fatal("Could not find the bootstrap file for WordPress"); | |
35da5d8d | 496 | } |
6491539b DL |
497 | $wpUserTimezone = get_option('timezone_string'); |
498 | if ($wpUserTimezone) { | |
499 | date_default_timezone_set($wpUserTimezone); | |
500 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
501 | } | |
e7292422 | 502 | require_once $cmsRootPath . DIRECTORY_SEPARATOR . 'wp-includes/pluggable.php'; |
fe1e7958 | 503 | $uid = CRM_Utils_Array::value('uid', $name); |
17763922 WA |
504 | if (!$uid) { |
505 | $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST)); | |
506 | $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST)); | |
507 | if ($name) { | |
d8182404 | 508 | $uid = wp_authenticate($name, $pass); |
17763922 WA |
509 | if (!$uid) { |
510 | if ($throwError) { | |
511 | echo '<br />Sorry, unrecognized username or password.'; | |
512 | exit(); | |
513 | } | |
514 | return FALSE; | |
515 | } | |
516 | } | |
517 | } | |
fe1e7958 | 518 | if ($uid) { |
a4111333 CW |
519 | if ($uid instanceof WP_User) { |
520 | $account = wp_set_current_user($uid->ID); | |
c5f77355 CW |
521 | } |
522 | else { | |
a4111333 CW |
523 | $account = wp_set_current_user($uid); |
524 | } | |
fe1e7958 | 525 | if ($account && $account->data->ID) { |
526 | global $user; | |
527 | $user = $account; | |
528 | return TRUE; | |
529 | } | |
530 | } | |
e7292422 | 531 | return TRUE; |
6a488035 TO |
532 | } |
533 | ||
bb3a214a EM |
534 | /** |
535 | * @param $dir | |
536 | * | |
537 | * @return bool | |
538 | */ | |
00be9182 | 539 | public function validInstallDir($dir) { |
dfbcf0b7 | 540 | $includePath = "$dir/wp-includes"; |
468176f6 | 541 | if (@file_exists("$includePath/version.php")) { |
dfbcf0b7 DL |
542 | return TRUE; |
543 | } | |
544 | return FALSE; | |
545 | } | |
546 | ||
bb3a214a EM |
547 | /** |
548 | * Determine the location of the CMS root. | |
549 | * | |
72b3a70c CW |
550 | * @return string|NULL |
551 | * local file system path to CMS root, or NULL if it cannot be determined | |
bb3a214a | 552 | */ |
00be9182 | 553 | public function cmsRootPath() { |
a93a0366 TO |
554 | global $civicrm_paths; |
555 | if (!empty($civicrm_paths['cms.root']['path'])) { | |
556 | return $civicrm_paths['cms.root']['path']; | |
557 | } | |
558 | ||
6a488035 | 559 | $cmsRoot = $valid = NULL; |
dfbcf0b7 DL |
560 | if (defined('CIVICRM_CMSDIR')) { |
561 | if ($this->validInstallDir(CIVICRM_CMSDIR)) { | |
562 | $cmsRoot = CIVICRM_CMSDIR; | |
563 | $valid = TRUE; | |
564 | } | |
6a488035 | 565 | } |
dfbcf0b7 DL |
566 | else { |
567 | $pathVars = explode('/', str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME'])); | |
6a488035 | 568 | |
dfbcf0b7 DL |
569 | //might be windows installation. |
570 | $firstVar = array_shift($pathVars); | |
571 | if ($firstVar) { | |
572 | $cmsRoot = $firstVar; | |
573 | } | |
574 | ||
575 | //start w/ csm dir search. | |
576 | foreach ($pathVars as $var) { | |
577 | $cmsRoot .= "/$var"; | |
578 | if ($this->validInstallDir($cmsRoot)) { | |
579 | //stop as we found bootstrap. | |
580 | $valid = TRUE; | |
581 | break; | |
582 | } | |
6a488035 TO |
583 | } |
584 | } | |
585 | ||
586 | return ($valid) ? $cmsRoot : NULL; | |
587 | } | |
588 | ||
bb3a214a | 589 | /** |
17f443df | 590 | * @inheritDoc |
bb3a214a | 591 | */ |
00be9182 | 592 | public function createUser(&$params, $mail) { |
be2fb01f | 593 | $user_data = [ |
6a488035 TO |
594 | 'ID' => '', |
595 | 'user_pass' => $params['cms_pass'], | |
596 | 'user_login' => $params['cms_name'], | |
597 | 'user_email' => $params[$mail], | |
598 | 'nickname' => $params['cms_name'], | |
599 | 'role' => get_option('default_role'), | |
be2fb01f | 600 | ]; |
6a488035 TO |
601 | if (isset($params['contactID'])) { |
602 | $contactType = CRM_Contact_BAO_Contact::getContactType($params['contactID']); | |
603 | if ($contactType == 'Individual') { | |
604 | $user_data['first_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
605 | $params['contactID'], 'first_name' | |
606 | ); | |
607 | $user_data['last_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
608 | $params['contactID'], 'last_name' | |
609 | ); | |
610 | } | |
611 | } | |
612 | ||
613 | $uid = wp_insert_user($user_data); | |
614 | ||
be2fb01f | 615 | $creds = []; |
6a488035 TO |
616 | $creds['user_login'] = $params['cms_name']; |
617 | $creds['user_password'] = $params['cms_pass']; | |
618 | $creds['remember'] = TRUE; | |
619 | $user = wp_signon($creds, FALSE); | |
620 | ||
621 | wp_new_user_notification($uid, $user_data['user_pass']); | |
622 | return $uid; | |
623 | } | |
624 | ||
f4aaa82a | 625 | /** |
17f443df | 626 | * @inheritDoc |
6a488035 | 627 | */ |
00be9182 | 628 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
629 | // CRM-10620 |
630 | if (function_exists('wp_update_user')) { | |
353ffa53 | 631 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); |
6a488035 TO |
632 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); |
633 | ||
be2fb01f | 634 | $values = ['ID' => $ufID, 'user_email' => $ufName]; |
481a74f4 TO |
635 | if ($ufID) { |
636 | wp_update_user($values); | |
6a488035 TO |
637 | } |
638 | } | |
639 | } | |
640 | ||
bb3a214a | 641 | /** |
c490a46a | 642 | * @param array $params |
bb3a214a EM |
643 | * @param $errors |
644 | * @param string $emailName | |
645 | */ | |
00be9182 | 646 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
647 | $config = CRM_Core_Config::singleton(); |
648 | ||
353ffa53 TO |
649 | $dao = new CRM_Core_DAO(); |
650 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
651 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
652 | ||
a7488080 | 653 | if (!empty($params['name'])) { |
6a488035 TO |
654 | if (!validate_username($params['name'])) { |
655 | $errors['cms_name'] = ts("Your username contains invalid characters"); | |
656 | } | |
657 | elseif (username_exists(sanitize_user($params['name']))) { | |
be2fb01f | 658 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', [1 => $params['name']]); |
6a488035 TO |
659 | } |
660 | } | |
661 | ||
a7488080 | 662 | if (!empty($params['mail'])) { |
6a488035 TO |
663 | if (!is_email($params['mail'])) { |
664 | $errors[$emailName] = "Your email is invaid"; | |
665 | } | |
666 | elseif (email_exists($params['mail'])) { | |
db18d815 | 667 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
be2fb01f | 668 | [1 => $params['mail'], 2 => wp_lostpassword_url()] |
6a488035 TO |
669 | ); |
670 | } | |
671 | } | |
672 | } | |
673 | ||
674 | /** | |
17f443df | 675 | * @inheritDoc |
6a488035 TO |
676 | */ |
677 | public function isUserLoggedIn() { | |
678 | $isloggedIn = FALSE; | |
679 | if (function_exists('is_user_logged_in')) { | |
680 | $isloggedIn = is_user_logged_in(); | |
681 | } | |
682 | ||
683 | return $isloggedIn; | |
684 | } | |
685 | ||
8caad0ce | 686 | /** |
687 | * @inheritDoc | |
688 | */ | |
689 | public function isUserRegistrationPermitted() { | |
690 | if (!get_option('users_can_register')) { | |
691 | return FALSE; | |
692 | } | |
693 | return TRUE; | |
694 | } | |
695 | ||
63df6889 HD |
696 | /** |
697 | * @inheritDoc | |
698 | */ | |
1a6630be | 699 | public function isPasswordUserGenerated() { |
63df6889 HD |
700 | return TRUE; |
701 | } | |
702 | ||
bb3a214a EM |
703 | /** |
704 | * @return mixed | |
705 | */ | |
00be9182 | 706 | public function getLoggedInUserObject() { |
2b617cb0 | 707 | if (function_exists('is_user_logged_in') && |
353ffa53 TO |
708 | is_user_logged_in() |
709 | ) { | |
2b617cb0 EM |
710 | global $current_user; |
711 | } | |
712 | return $current_user; | |
713 | } | |
353ffa53 | 714 | |
6a488035 | 715 | /** |
17f443df | 716 | * @inheritDoc |
6a488035 TO |
717 | */ |
718 | public function getLoggedInUfID() { | |
719 | $ufID = NULL; | |
2b617cb0 EM |
720 | $current_user = $this->getLoggedInUserObject(); |
721 | return isset($current_user->ID) ? $current_user->ID : NULL; | |
722 | } | |
723 | ||
724 | /** | |
17f443df | 725 | * @inheritDoc |
2b617cb0 | 726 | */ |
00be9182 | 727 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
728 | $user = $this->getLoggedInUserObject(); |
729 | return $this->getUniqueIdentifierFromUserObject($user); | |
6a488035 TO |
730 | } |
731 | ||
32998c82 EM |
732 | /** |
733 | * Get User ID from UserFramework system (Joomla) | |
77855840 TO |
734 | * @param object $user |
735 | * Object as described by the CMS. | |
72b3a70c CW |
736 | * |
737 | * @return int|null | |
32998c82 | 738 | */ |
00be9182 | 739 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
740 | return !empty($user->ID) ? $user->ID : NULL; |
741 | } | |
742 | ||
2b617cb0 | 743 | /** |
17f443df | 744 | * @inheritDoc |
2b617cb0 | 745 | */ |
00be9182 | 746 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
747 | return empty($user->user_email) ? NULL : $user->user_email; |
748 | } | |
749 | ||
6a488035 | 750 | /** |
17f443df | 751 | * @inheritDoc |
6a488035 TO |
752 | */ |
753 | public function getLoginURL($destination = '') { | |
754 | $config = CRM_Core_Config::singleton(); | |
153155d3 | 755 | $loginURL = wp_login_url(); |
6a488035 TO |
756 | return $loginURL; |
757 | } | |
758 | ||
bb3a214a | 759 | /** |
ad37ac8e | 760 | * FIXME: Do something. |
761 | * | |
762 | * @param \CRM_Core_Form $form | |
763 | * | |
764 | * @return NULL|string | |
bb3a214a | 765 | */ |
6a488035 | 766 | public function getLoginDestination(&$form) { |
408b79bf | 767 | return NULL; |
6a488035 TO |
768 | } |
769 | ||
770 | /** | |
17f443df | 771 | * @inheritDoc |
6a488035 | 772 | */ |
00be9182 | 773 | public function getVersion() { |
6a488035 TO |
774 | if (function_exists('get_bloginfo')) { |
775 | return get_bloginfo('version', 'display'); | |
776 | } | |
777 | else { | |
778 | return 'Unknown'; | |
779 | } | |
780 | } | |
6491539b DL |
781 | |
782 | /** | |
17f443df | 783 | * @inheritDoc |
6491539b | 784 | */ |
00be9182 | 785 | public function getTimeZoneString() { |
6491539b DL |
786 | return get_option('timezone_string'); |
787 | } | |
59f97da6 EM |
788 | |
789 | /** | |
17f443df | 790 | * @inheritDoc |
59f97da6 | 791 | */ |
00be9182 | 792 | public function getUserRecordUrl($contactID) { |
59f97da6 | 793 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
353ffa53 | 794 | if (CRM_Core_Session::singleton() |
6714d8d2 | 795 | ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(['cms:administer users']) |
353ffa53 | 796 | ) { |
59f97da6 EM |
797 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "wp-admin/user-edit.php?user_id=" . $uid; |
798 | } | |
799 | } | |
96025800 | 800 | |
469d8dab CW |
801 | /** |
802 | * Append WP js to coreResourcesList. | |
ad37ac8e | 803 | * |
804 | * @param array $list | |
469d8dab CW |
805 | */ |
806 | public function appendCoreResources(&$list) { | |
807 | $list[] = 'js/crm.wordpress.js'; | |
808 | } | |
809 | ||
03d5592a CW |
810 | /** |
811 | * @inheritDoc | |
812 | */ | |
813 | public function synchronizeUsers() { | |
814 | $config = CRM_Core_Config::singleton(); | |
815 | if (PHP_SAPI != 'cli') { | |
816 | set_time_limit(300); | |
817 | } | |
818 | $id = 'ID'; | |
819 | $mail = 'user_email'; | |
820 | ||
821 | $uf = $config->userFramework; | |
822 | $contactCount = 0; | |
823 | $contactCreated = 0; | |
824 | $contactMatching = 0; | |
825 | ||
826 | global $wpdb; | |
827 | $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users"); | |
828 | ||
829 | foreach ($wpUserIds as $wpUserId) { | |
830 | $wpUserData = get_userdata($wpUserId); | |
831 | $contactCount++; | |
832 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData, | |
833 | $wpUserData->$id, | |
834 | $wpUserData->$mail, | |
835 | $uf, | |
836 | 1, | |
837 | 'Individual', | |
838 | TRUE | |
839 | ) | |
840 | ) { | |
841 | $contactCreated++; | |
842 | } | |
843 | else { | |
844 | $contactMatching++; | |
845 | } | |
846 | if (is_object($match)) { | |
847 | $match->free(); | |
848 | } | |
849 | } | |
850 | ||
be2fb01f | 851 | return [ |
03d5592a CW |
852 | 'contactCount' => $contactCount, |
853 | 'contactMatching' => $contactMatching, | |
854 | 'contactCreated' => $contactCreated, | |
be2fb01f | 855 | ]; |
03d5592a CW |
856 | } |
857 | ||
6a488035 | 858 | } |