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