Merge pull request #15808 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33
34 /**
35 * Joomla specific stuff goes here.
36 */
37 class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
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 */
49 $this->is_drupal = FALSE;
50 }
51
52 /**
53 * @inheritDoc
54 */
55 public function createUser(&$params, $mail) {
56 $baseDir = JPATH_SITE;
57 require_once $baseDir . '/components/com_users/models/registration.php';
58
59 $userParams = JComponentHelper::getParams('com_users');
60 $model = new UsersModelRegistration();
61 $ufID = NULL;
62
63 // get the default usertype
64 $userType = $userParams->get('new_usertype');
65 if (!$userType) {
66 $userType = 2;
67 }
68
69 if (isset($params['name'])) {
70 $fullname = trim($params['name']);
71 }
72 elseif (isset($params['contactID'])) {
73 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
74 }
75 else {
76 $fullname = trim($params['cms_name']);
77 }
78
79 // Prepare the values for a new Joomla user.
80 $values = [];
81 $values['name'] = $fullname;
82 $values['username'] = trim($params['cms_name']);
83 $values['password1'] = $values['password2'] = $params['cms_pass'];
84 $values['email1'] = $values['email2'] = trim($params[$mail]);
85
86 $lang = JFactory::getLanguage();
87 $lang->load('com_users', $baseDir);
88
89 $register = $model->register($values);
90
91 $ufID = JUserHelper::getUserId($values['username']);
92 return $ufID;
93 }
94
95 /**
96 * @inheritDoc
97 */
98 public function updateCMSName($ufID, $ufName) {
99 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
100 $ufName = CRM_Utils_Type::escape($ufName, 'String');
101
102 $values = [];
103 $user = JUser::getInstance($ufID);
104
105 $values['email'] = $ufName;
106 $user->bind($values);
107
108 $user->save();
109 }
110
111 /**
112 * Check if username and email exists in the Joomla db.
113 *
114 * @param array $params
115 * Array of name and mail values.
116 * @param array $errors
117 * Array of errors.
118 * @param string $emailName
119 * Field label for the 'email'.
120 */
121 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
122 $config = CRM_Core_Config::singleton();
123
124 $dao = new CRM_Core_DAO();
125 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
126 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
127 //don't allow the special characters and min. username length is two
128 //regex \\ to match a single backslash would become '/\\\\/'
129 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
130 if ($isNotValid || strlen($name) < 2) {
131 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
132 }
133
134 $JUserTable = &JTable::getInstance('User', 'JTable');
135
136 $db = $JUserTable->getDbo();
137 $query = $db->getQuery(TRUE);
138 $query->select('username, email');
139 $query->from($JUserTable->getTableName());
140
141 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
142 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
143 $db->setQuery($query, 0, 10);
144 $users = $db->loadAssocList();
145
146 $row = [];
147 if (count($users)) {
148 $row = $users[0];
149 }
150
151 if (!empty($row)) {
152 $dbName = CRM_Utils_Array::value('username', $row);
153 $dbEmail = CRM_Utils_Array::value('email', $row);
154 if (strtolower($dbName) == strtolower($name)) {
155 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
156 [1 => $name]
157 );
158 }
159 if (strtolower($dbEmail) == strtolower($email)) {
160 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
161 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
162 [1 => $email, 2 => $resetUrl]
163 );
164 }
165 }
166 }
167
168 /**
169 * @inheritDoc
170 */
171 public function setTitle($title, $pageTitle = NULL) {
172 if (!$pageTitle) {
173 $pageTitle = $title;
174 }
175
176 $template = CRM_Core_Smarty::singleton();
177 $template->assign('pageTitle', $pageTitle);
178
179 $document = JFactory::getDocument();
180 $document->setTitle($title);
181 }
182
183 /**
184 * @inheritDoc
185 */
186 public function appendBreadCrumb($breadCrumbs) {
187 $template = CRM_Core_Smarty::singleton();
188 $bc = $template->get_template_vars('breadcrumb');
189
190 if (is_array($breadCrumbs)) {
191 foreach ($breadCrumbs as $crumbs) {
192 if (stripos($crumbs['url'], 'id%%')) {
193 $args = ['cid', 'mid'];
194 foreach ($args as $a) {
195 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
196 FALSE, NULL, $_GET
197 );
198 if ($val) {
199 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
200 }
201 }
202 }
203 $bc[] = $crumbs;
204 }
205 }
206 $template->assign_by_ref('breadcrumb', $bc);
207 }
208
209 /**
210 * @inheritDoc
211 */
212 public function resetBreadCrumb() {
213 }
214
215 /**
216 * @inheritDoc
217 */
218 public function addHTMLHead($string = NULL) {
219 if ($string) {
220 $document = JFactory::getDocument();
221 $document->addCustomTag($string);
222 }
223 }
224
225 /**
226 * @inheritDoc
227 */
228 public function addStyleUrl($url, $region) {
229 if ($region == 'html-header') {
230 $document = JFactory::getDocument();
231 $document->addStyleSheet($url);
232 return TRUE;
233 }
234 return FALSE;
235 }
236
237 /**
238 * @inheritDoc
239 */
240 public function addStyle($code, $region) {
241 if ($region == 'html-header') {
242 $document = JFactory::getDocument();
243 $document->addStyleDeclaration($code);
244 return TRUE;
245 }
246 return FALSE;
247 }
248
249 /**
250 * @inheritDoc
251 */
252 public function url(
253 $path = NULL,
254 $query = NULL,
255 $absolute = FALSE,
256 $fragment = NULL,
257 $frontend = FALSE,
258 $forceBackend = FALSE
259 ) {
260 $config = CRM_Core_Config::singleton();
261 $separator = '&';
262 $Itemid = '';
263 $script = '';
264 $path = CRM_Utils_String::stripPathChars($path);
265
266 if ($config->userFrameworkFrontend) {
267 $script = 'index.php';
268
269 // Get Itemid using JInput::get()
270 $input = Joomla\CMS\Factory::getApplication()->input;
271 $itemIdNum = $input->get("Itemid");
272 if ($itemIdNum && (strpos($path, 'civicrm/payment/ipn') === FALSE)) {
273 $Itemid = "{$separator}Itemid=" . $itemIdNum;
274 }
275 }
276
277 if (isset($fragment)) {
278 $fragment = '#' . $fragment;
279 }
280
281 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
282
283 if (!empty($query)) {
284 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
285 }
286 else {
287 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
288 }
289
290 // gross hack for joomla, we are in the backend and want to send a frontend url
291 if ($frontend && $config->userFramework == 'Joomla') {
292 // handle both joomla v1.5 and v1.6, CRM-7939
293 $url = str_replace('/administrator/index2.php', '/index.php', $url);
294 $url = str_replace('/administrator/index.php', '/index.php', $url);
295
296 // CRM-8215
297 $url = str_replace('/administrator/', '/index.php', $url);
298 }
299 elseif ($forceBackend) {
300 if (defined('JVERSION')) {
301 $joomlaVersion = JVERSION;
302 }
303 else {
304 $jversion = new JVersion();
305 $joomlaVersion = $jversion->getShortVersion();
306 }
307
308 if (version_compare($joomlaVersion, '1.6') >= 0) {
309 $url = str_replace('/index.php', '/administrator/index.php', $url);
310 }
311 }
312 return $url;
313 }
314
315 /**
316 * Set the email address of the user.
317 *
318 * @param object $user
319 * Handle to the user object.
320 */
321 public function setEmail(&$user) {
322 global $database;
323 $query = $db->getQuery(TRUE);
324 $query->select($db->quoteName('email'))
325 ->from($db->quoteName('#__users'))
326 ->where($db->quoteName('id') . ' = ' . $user->id);
327 $database->setQuery($query);
328 $user->email = $database->loadResult();
329 }
330
331 /**
332 * @inheritDoc
333 */
334 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
335 require_once 'DB.php';
336
337 $config = CRM_Core_Config::singleton();
338 $user = NULL;
339
340 if ($loadCMSBootstrap) {
341 $bootStrapParams = [];
342 if ($name && $password) {
343 $bootStrapParams = [
344 'name' => $name,
345 'pass' => $password,
346 ];
347 }
348 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
349 }
350
351 jimport('joomla.application.component.helper');
352 jimport('joomla.database.table');
353 jimport('joomla.user.helper');
354
355 $JUserTable = JTable::getInstance('User', 'JTable');
356
357 $db = $JUserTable->getDbo();
358 $query = $db->getQuery(TRUE);
359 $query->select('id, name, username, email, password');
360 $query->from($JUserTable->getTableName());
361 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
362 $db->setQuery($query, 0, 0);
363 $users = $db->loadObjectList();
364
365 $row = [];
366 if (count($users)) {
367 $row = $users[0];
368 }
369
370 $joomlaBase = self::getBasePath();
371 self::getJVersion($joomlaBase);
372
373 if (!empty($row)) {
374 $dbPassword = $row->password;
375 $dbId = $row->id;
376 $dbEmail = $row->email;
377
378 if (version_compare(JVERSION, '2.5.18', 'lt') ||
379 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
380 ) {
381 // now check password
382 list($hash, $salt) = explode(':', $dbPassword);
383 $cryptpass = md5($password . $salt);
384 if ($hash != $cryptpass) {
385 return FALSE;
386 }
387 }
388 else {
389 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
390 return FALSE;
391 }
392
393 if (version_compare(JVERSION, '3.8.0', 'ge')) {
394 jimport('joomla.application.helper');
395 jimport('joomla.application.cms');
396 jimport('joomla.application.administrator');
397 }
398 //include additional files required by Joomla 3.2.1+
399 elseif (version_compare(JVERSION, '3.2.1', 'ge')) {
400 require_once $joomlaBase . '/libraries/cms/application/helper.php';
401 require_once $joomlaBase . '/libraries/cms/application/cms.php';
402 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
403 }
404 }
405
406 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
407 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
408 if (!$contactID) {
409 return FALSE;
410 }
411 return [$contactID, $dbId, mt_rand()];
412 }
413
414 return FALSE;
415 }
416
417 /**
418 * Set a init session with user object.
419 *
420 * @param array $data
421 * Array with user specific data.
422 */
423 public function setUserSession($data) {
424 list($userID, $ufID) = $data;
425 $user = new JUser($ufID);
426 $session = JFactory::getSession();
427 $session->set('user', $user);
428
429 parent::setUserSession($data);
430 }
431
432 /**
433 * FIXME: Do something
434 *
435 * @param string $message
436 */
437 public function setMessage($message) {
438 }
439
440 /**
441 * @param \string $username
442 * @param \string $password
443 *
444 * @return bool
445 */
446 public function loadUser($username, $password = NULL) {
447 $uid = JUserHelper::getUserId($username);
448 if (empty($uid)) {
449 return FALSE;
450 }
451 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
452 if (!empty($password)) {
453 $instance = JFactory::getApplication('site');
454 $params = [
455 'username' => $username,
456 'password' => $password,
457 ];
458 //perform the login action
459 $instance->login($params);
460 }
461
462 // Save details in Joomla session
463 $user = JFactory::getUser($uid);
464 $jsession = JFactory::getSession();
465 $jsession->set('user', $user);
466
467 // Save details in Civi session
468 $session = CRM_Core_Session::singleton();
469 $session->set('ufID', $uid);
470 $session->set('userID', $contactID);
471 return TRUE;
472 }
473
474 /**
475 * FIXME: Use CMS-native approach
476 * @throws \CRM_Core_Exception.
477 */
478 public function permissionDenied() {
479 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
480 }
481
482 /**
483 * @inheritDoc
484 */
485 public function logout() {
486 session_destroy();
487 CRM_Utils_System::setHttpHeader("Location", "index.php");
488 }
489
490 /**
491 * @inheritDoc
492 */
493 public function getUFLocale() {
494 if (defined('_JEXEC')) {
495 $conf = JFactory::getConfig();
496 $locale = $conf->get('language');
497 return str_replace('-', '_', $locale);
498 }
499 return NULL;
500 }
501
502 /**
503 * @inheritDoc
504 */
505 public function setUFLocale($civicrm_language) {
506 // TODO
507 return TRUE;
508 }
509
510 /**
511 * @inheritDoc
512 */
513 public function getVersion() {
514 if (class_exists('JVersion')) {
515 $version = new JVersion();
516 return $version->getShortVersion();
517 }
518 else {
519 return 'Unknown';
520 }
521 }
522
523 public function getJVersion($joomlaBase) {
524 // Files may be in different places depending on Joomla version
525 if (!defined('JVERSION')) {
526 // Joomla 3.8.0+
527 $versionPhp = $joomlaBase . '/libraries/src/Version.php';
528 if (!file_exists($versionPhp)) {
529 // Joomla < 3.8.0
530 $versionPhp = $joomlaBase . '/libraries/cms/version/version.php';
531 }
532 require $versionPhp;
533 $jversion = new JVersion();
534 define('JVERSION', $jversion->getShortVersion());
535 }
536 }
537
538 /**
539 * Setup the base path related constant.
540 * @return mixed
541 */
542 public function getBasePath() {
543 global $civicrm_root;
544 $joomlaPath = explode(DIRECTORY_SEPARATOR . 'administrator', $civicrm_root);
545 $joomlaBase = $joomlaPath[0];
546 return $joomlaBase;
547 }
548
549 /**
550 * Load joomla bootstrap.
551 *
552 * @param array $params
553 * with uid or name and password.
554 * @param bool $loadUser
555 * load cms user?.
556 * @param bool|\throw $throwError throw error on failure?
557 * @param null $realPath
558 * @param bool $loadDefines
559 *
560 * @return bool
561 */
562 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
563 $joomlaBase = self::getBasePath();
564
565 // load BootStrap here if needed
566 // We are a valid Joomla entry point.
567 if (!defined('_JEXEC') && $loadDefines) {
568 define('_JEXEC', 1);
569 define('DS', DIRECTORY_SEPARATOR);
570 define('JPATH_BASE', $joomlaBase . '/administrator');
571 require $joomlaBase . '/administrator/includes/defines.php';
572 }
573
574 // Get the framework.
575 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
576 require $joomlaBase . '/libraries/import.legacy.php';
577 }
578 require $joomlaBase . '/libraries/cms.php';
579 self::getJVersion($joomlaBase);
580
581 if (version_compare(JVERSION, '3.8', 'lt')) {
582 require $joomlaBase . '/libraries/import.php';
583 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
584 }
585
586 require_once $joomlaBase . '/configuration.php';
587
588 if (version_compare(JVERSION, '3.0', 'lt')) {
589 require $joomlaBase . '/libraries/joomla/environment/uri.php';
590 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
591 }
592 elseif (version_compare(JVERSION, '3.8', 'lt')) {
593 jimport('joomla.environment.uri');
594 }
595
596 if (version_compare(JVERSION, '3.8', 'lt')) {
597 jimport('joomla.application.cli');
598 }
599
600 if (!defined('JDEBUG')) {
601 define('JDEBUG', FALSE);
602 }
603
604 // Set timezone for Joomla on Cron
605 $config = JFactory::getConfig();
606 $timezone = $config->get('offset');
607 if ($timezone) {
608 date_default_timezone_set($timezone);
609 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
610 }
611
612 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
613 $config = CRM_Core_Config::singleton();
614 CRM_Utils_Hook::config($config);
615
616 return TRUE;
617 }
618
619 /**
620 * @inheritDoc
621 */
622 public function isUserLoggedIn() {
623 $user = JFactory::getUser();
624 return ($user->guest) ? FALSE : TRUE;
625 }
626
627 /**
628 * @inheritDoc
629 */
630 public function isUserRegistrationPermitted() {
631 $userParams = JComponentHelper::getParams('com_users');
632 if (!$userParams->get('allowUserRegistration')) {
633 return FALSE;
634 }
635 return TRUE;
636 }
637
638 /**
639 * @inheritDoc
640 */
641 public function isPasswordUserGenerated() {
642 return TRUE;
643 }
644
645 /**
646 * @inheritDoc
647 */
648 public function getLoggedInUfID() {
649 $user = JFactory::getUser();
650 return ($user->guest) ? NULL : $user->id;
651 }
652
653 /**
654 * @inheritDoc
655 */
656 public function getLoggedInUniqueIdentifier() {
657 $user = JFactory::getUser();
658 return $this->getUniqueIdentifierFromUserObject($user);
659 }
660
661 /**
662 * @inheritDoc
663 */
664 public function getUser($contactID) {
665 $user_details = parent::getUser($contactID);
666 $user = JFactory::getUser($user_details['id']);
667 $user_details['name'] = $user->name;
668 return $user_details;
669 }
670
671 /**
672 * @inheritDoc
673 */
674 public function getUserIDFromUserObject($user) {
675 return !empty($user->id) ? $user->id : NULL;
676 }
677
678 /**
679 * @inheritDoc
680 */
681 public function getUniqueIdentifierFromUserObject($user) {
682 return ($user->guest) ? NULL : $user->email;
683 }
684
685 /**
686 * @inheritDoc
687 */
688 public function getTimeZoneString() {
689 $timezone = JFactory::getConfig()->get('offset');
690 return !$timezone ? date_default_timezone_get() : $timezone;
691 }
692
693 /**
694 * Get a list of all installed modules, including enabled and disabled ones
695 *
696 * @return array
697 * CRM_Core_Module
698 */
699 public function getModules() {
700 $result = [];
701
702 $db = JFactory::getDbo();
703 $query = $db->getQuery(TRUE);
704 $query->select('type, folder, element, enabled')
705 ->from('#__extensions')
706 ->where('type =' . $db->Quote('plugin'));
707 $plugins = $db->setQuery($query)->loadAssocList();
708 foreach ($plugins as $plugin) {
709 // question: is the folder really a critical part of the plugin's name?
710 $name = implode('.', ['joomla', $plugin['type'], $plugin['folder'], $plugin['element']]);
711 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
712 }
713
714 return $result;
715 }
716
717 /**
718 * @inheritDoc
719 */
720 public function getLoginURL($destination = '') {
721 $config = CRM_Core_Config::singleton();
722 $loginURL = $config->userFrameworkBaseURL;
723 $loginURL = str_replace('administrator/', '', $loginURL);
724 $loginURL .= 'index.php?option=com_users&view=login';
725
726 //CRM-14872 append destination
727 if (!empty($destination)) {
728 $loginURL .= '&return=' . urlencode(base64_encode($destination));
729 }
730 return $loginURL;
731 }
732
733 /**
734 * @inheritDoc
735 */
736 public function getLoginDestination(&$form) {
737 $args = NULL;
738
739 $id = $form->get('id');
740 if ($id) {
741 $args .= "&id=$id";
742 }
743 else {
744 $gid = $form->get('gid');
745 if ($gid) {
746 $args .= "&gid=$gid";
747 }
748 else {
749 // Setup Personal Campaign Page link uses pageId
750 $pageId = $form->get('pageId');
751 if ($pageId) {
752 $component = $form->get('component');
753 $args .= "&pageId=$pageId&component=$component&action=add";
754 }
755 }
756 }
757
758 $destination = NULL;
759 if ($args) {
760 // append destination so user is returned to form they came from after login
761 $args = 'reset=1' . $args;
762 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
763 }
764
765 return $destination;
766 }
767
768 /**
769 * Determine the location of the CMS root.
770 *
771 * @return string|NULL
772 * local file system path to CMS root, or NULL if it cannot be determined
773 */
774 public function cmsRootPath() {
775 global $civicrm_paths;
776 if (!empty($civicrm_paths['cms.root']['path'])) {
777 return $civicrm_paths['cms.root']['path'];
778 }
779
780 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
781 if (file_exists("$siteRoot/administrator/index.php")) {
782 return $siteRoot;
783 }
784 return NULL;
785 }
786
787 /**
788 * @inheritDoc
789 */
790 public function getDefaultSiteSettings($dir = NULL) {
791 $config = CRM_Core_Config::singleton();
792 $url = preg_replace(
793 '|/administrator|',
794 '',
795 $config->userFrameworkBaseURL
796 );
797 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
798 // This is the original pattern, but it doesn't work under Windows.
799 // By setting the pattern to the one used before the change first and only
800 // changing it means that the change code only affects Windows users.
801 $pattern = '|/media/civicrm/.*$|';
802 if (DIRECTORY_SEPARATOR == '\\') {
803 // This regular expression will handle Windows as well as Linux
804 // and any combination of forward and back slashes in directory
805 // separators. We only apply it if the directory separator is the one
806 // used by Windows.
807 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
808 }
809 $siteRoot = preg_replace(
810 $pattern,
811 '',
812 $config->imageUploadDir
813 );
814 return [$url, NULL, $siteRoot];
815 }
816
817 /**
818 * @inheritDoc
819 */
820 public function getUserRecordUrl($contactID) {
821 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
822 $userRecordUrl = NULL;
823 // if logged in user has user edit access, then allow link to other users joomla profile
824 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
825 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
826 }
827 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
828 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
829 }
830 }
831
832 /**
833 * @inheritDoc
834 */
835 public function checkPermissionAddUser() {
836 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
837 return TRUE;
838 }
839 }
840
841 /**
842 * @inheritDoc
843 */
844 public function synchronizeUsers() {
845 $config = CRM_Core_Config::singleton();
846 if (PHP_SAPI != 'cli') {
847 set_time_limit(300);
848 }
849 $id = 'id';
850 $mail = 'email';
851 $name = 'name';
852
853 $JUserTable = &JTable::getInstance('User', 'JTable');
854
855 $db = $JUserTable->getDbo();
856 $query = $db->getQuery(TRUE);
857 $query->select($id . ', ' . $mail . ', ' . $name);
858 $query->from($JUserTable->getTableName());
859 $query->where($mail != '');
860
861 $db->setQuery($query);
862 $users = $db->loadObjectList();
863
864 $user = new StdClass();
865 $uf = $config->userFramework;
866 $contactCount = 0;
867 $contactCreated = 0;
868 $contactMatching = 0;
869 for ($i = 0; $i < count($users); $i++) {
870 $user->$id = $users[$i]->$id;
871 $user->$mail = $users[$i]->$mail;
872 $user->$name = $users[$i]->$name;
873 $contactCount++;
874 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
875 $users[$i]->$id,
876 $users[$i]->$mail,
877 $uf,
878 1,
879 'Individual',
880 TRUE
881 )
882 ) {
883 $contactCreated++;
884 }
885 else {
886 $contactMatching++;
887 }
888 }
889
890 return [
891 'contactCount' => $contactCount,
892 'contactMatching' => $contactMatching,
893 'contactCreated' => $contactCreated,
894 ];
895 }
896
897 }