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