Merge pull request #14092 from eileenmcnaughton/search_513
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 if (JRequest::getVar("Itemid") && (strpos($path, 'civicrm/payment/ipn') === FALSE)) {
269 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
270 }
271 }
272
273 if (isset($fragment)) {
274 $fragment = '#' . $fragment;
275 }
276
277 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
278
279 if (!empty($query)) {
280 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
281 }
282 else {
283 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
284 }
285
286 // gross hack for joomla, we are in the backend and want to send a frontend url
287 if ($frontend && $config->userFramework == 'Joomla') {
288 // handle both joomla v1.5 and v1.6, CRM-7939
289 $url = str_replace('/administrator/index2.php', '/index.php', $url);
290 $url = str_replace('/administrator/index.php', '/index.php', $url);
291
292 // CRM-8215
293 $url = str_replace('/administrator/', '/index.php', $url);
294 }
295 elseif ($forceBackend) {
296 if (defined('JVERSION')) {
297 $joomlaVersion = JVERSION;
298 }
299 else {
300 $jversion = new JVersion();
301 $joomlaVersion = $jversion->getShortVersion();
302 }
303
304 if (version_compare($joomlaVersion, '1.6') >= 0) {
305 $url = str_replace('/index.php', '/administrator/index.php', $url);
306 }
307 }
308 return $url;
309 }
310
311 /**
312 * Set the email address of the user.
313 *
314 * @param object $user
315 * Handle to the user object.
316 */
317 public function setEmail(&$user) {
318 global $database;
319 $query = $db->getQuery(TRUE);
320 $query->select($db->quoteName('email'))
321 ->from($db->quoteName('#__users'))
322 ->where($db->quoteName('id') . ' = ' . $user->id);
323 $database->setQuery($query);
324 $user->email = $database->loadResult();
325 }
326
327 /**
328 * @inheritDoc
329 */
330 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
331 require_once 'DB.php';
332
333 $config = CRM_Core_Config::singleton();
334 $user = NULL;
335
336 if ($loadCMSBootstrap) {
337 $bootStrapParams = [];
338 if ($name && $password) {
339 $bootStrapParams = [
340 'name' => $name,
341 'pass' => $password,
342 ];
343 }
344 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
345 }
346
347 jimport('joomla.application.component.helper');
348 jimport('joomla.database.table');
349 jimport('joomla.user.helper');
350
351 $JUserTable = JTable::getInstance('User', 'JTable');
352
353 $db = $JUserTable->getDbo();
354 $query = $db->getQuery(TRUE);
355 $query->select('id, name, username, email, password');
356 $query->from($JUserTable->getTableName());
357 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
358 $db->setQuery($query, 0, 0);
359 $users = $db->loadObjectList();
360
361 $row = [];
362 if (count($users)) {
363 $row = $users[0];
364 }
365
366 $joomlaBase = self::getBasePath();
367 self::getJVersion($joomlaBase);
368
369 if (!empty($row)) {
370 $dbPassword = $row->password;
371 $dbId = $row->id;
372 $dbEmail = $row->email;
373
374 if (version_compare(JVERSION, '2.5.18', 'lt') ||
375 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
376 ) {
377 // now check password
378 list($hash, $salt) = explode(':', $dbPassword);
379 $cryptpass = md5($password . $salt);
380 if ($hash != $cryptpass) {
381 return FALSE;
382 }
383 }
384 else {
385 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
386 return FALSE;
387 }
388
389 if (version_compare(JVERSION, '3.8.0', 'ge')) {
390 jimport('joomla.application.helper');
391 jimport('joomla.application.cms');
392 jimport('joomla.application.administrator');
393 }
394 //include additional files required by Joomla 3.2.1+
395 elseif (version_compare(JVERSION, '3.2.1', 'ge')) {
396 require_once $joomlaBase . '/libraries/cms/application/helper.php';
397 require_once $joomlaBase . '/libraries/cms/application/cms.php';
398 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
399 }
400 }
401
402 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
403 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
404 if (!$contactID) {
405 return FALSE;
406 }
407 return [$contactID, $dbId, mt_rand()];
408 }
409
410 return FALSE;
411 }
412
413 /**
414 * Set a init session with user object.
415 *
416 * @param array $data
417 * Array with user specific data.
418 */
419 public function setUserSession($data) {
420 list($userID, $ufID) = $data;
421 $user = new JUser($ufID);
422 $session = JFactory::getSession();
423 $session->set('user', $user);
424
425 parent::setUserSession($data);
426 }
427
428 /**
429 * FIXME: Do something
430 *
431 * @param string $message
432 */
433 public function setMessage($message) {
434 }
435
436 /**
437 * @param \string $username
438 * @param \string $password
439 *
440 * @return bool
441 */
442 public function loadUser($username, $password = NULL) {
443 $uid = JUserHelper::getUserId($username);
444 if (empty($uid)) {
445 return FALSE;
446 }
447 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
448 if (!empty($password)) {
449 $instance = JFactory::getApplication('site');
450 $params = [
451 'username' => $username,
452 'password' => $password,
453 ];
454 //perform the login action
455 $instance->login($params);
456 }
457
458 $session = CRM_Core_Session::singleton();
459 $session->set('ufID', $uid);
460 $session->set('userID', $contactID);
461 return TRUE;
462 }
463
464 /**
465 * FIXME: Use CMS-native approach
466 */
467 public function permissionDenied() {
468 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
469 }
470
471 /**
472 * @inheritDoc
473 */
474 public function logout() {
475 session_destroy();
476 CRM_Utils_System::setHttpHeader("Location", "index.php");
477 }
478
479 /**
480 * @inheritDoc
481 */
482 public function getUFLocale() {
483 if (defined('_JEXEC')) {
484 $conf = JFactory::getConfig();
485 $locale = $conf->get('language');
486 return str_replace('-', '_', $locale);
487 }
488 return NULL;
489 }
490
491 /**
492 * @inheritDoc
493 */
494 public function setUFLocale($civicrm_language) {
495 // TODO
496 return TRUE;
497 }
498
499 /**
500 * @inheritDoc
501 */
502 public function getVersion() {
503 if (class_exists('JVersion')) {
504 $version = new JVersion();
505 return $version->getShortVersion();
506 }
507 else {
508 return 'Unknown';
509 }
510 }
511
512 public function getJVersion($joomlaBase) {
513 // Files may be in different places depending on Joomla version
514 if (!defined('JVERSION')) {
515 // Joomla 3.8.0+
516 $versionPhp = $joomlaBase . '/libraries/src/Version.php';
517 if (!file_exists($versionPhp)) {
518 // Joomla < 3.8.0
519 $versionPhp = $joomlaBase . '/libraries/cms/version/version.php';
520 }
521 require $versionPhp;
522 $jversion = new JVersion();
523 define('JVERSION', $jversion->getShortVersion());
524 }
525 }
526
527 /**
528 * Setup the base path related constant.
529 * @return mixed
530 */
531 public function getBasePath() {
532 global $civicrm_root;
533 $joomlaPath = explode(DIRECTORY_SEPARATOR . 'administrator', $civicrm_root);
534 $joomlaBase = $joomlaPath[0];
535 return $joomlaBase;
536 }
537
538 /**
539 * Load joomla bootstrap.
540 *
541 * @param array $params
542 * with uid or name and password.
543 * @param bool $loadUser
544 * load cms user?.
545 * @param bool|\throw $throwError throw error on failure?
546 * @param null $realPath
547 * @param bool $loadDefines
548 *
549 * @return bool
550 */
551 public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
552 $joomlaBase = self::getBasePath();
553
554 // load BootStrap here if needed
555 // We are a valid Joomla entry point.
556 if (!defined('_JEXEC') && $loadDefines) {
557 define('_JEXEC', 1);
558 define('DS', DIRECTORY_SEPARATOR);
559 define('JPATH_BASE', $joomlaBase . '/administrator');
560 require $joomlaBase . '/administrator/includes/defines.php';
561 }
562
563 // Get the framework.
564 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
565 require $joomlaBase . '/libraries/import.legacy.php';
566 }
567 require $joomlaBase . '/libraries/cms.php';
568 self::getJVersion($joomlaBase);
569
570 if (version_compare(JVERSION, '3.8', 'lt')) {
571 require $joomlaBase . '/libraries/import.php';
572 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
573 }
574
575 require_once $joomlaBase . '/configuration.php';
576
577 if (version_compare(JVERSION, '3.0', 'lt')) {
578 require $joomlaBase . '/libraries/joomla/environment/uri.php';
579 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
580 }
581 elseif (version_compare(JVERSION, '3.8', 'lt')) {
582 jimport('joomla.environment.uri');
583 }
584
585 if (version_compare(JVERSION, '3.8', 'lt')) {
586 jimport('joomla.application.cli');
587 }
588
589 if (!defined('JDEBUG')) {
590 define('JDEBUG', FALSE);
591 }
592
593 // Set timezone for Joomla on Cron
594 $config = JFactory::getConfig();
595 $timezone = $config->get('offset');
596 if ($timezone) {
597 date_default_timezone_set($timezone);
598 CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
599 }
600
601 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
602 $config = CRM_Core_Config::singleton();
603 CRM_Utils_Hook::config($config);
604
605 return TRUE;
606 }
607
608 /**
609 * @inheritDoc
610 */
611 public function isUserLoggedIn() {
612 $user = JFactory::getUser();
613 return ($user->guest) ? FALSE : TRUE;
614 }
615
616 /**
617 * @inheritDoc
618 */
619 public function isUserRegistrationPermitted() {
620 $userParams = JComponentHelper::getParams('com_users');
621 if (!$userParams->get('allowUserRegistration')) {
622 return FALSE;
623 }
624 return TRUE;
625 }
626
627 /**
628 * @inheritDoc
629 */
630 public function isPasswordUserGenerated() {
631 return TRUE;
632 }
633
634 /**
635 * @inheritDoc
636 */
637 public function getLoggedInUfID() {
638 $user = JFactory::getUser();
639 return ($user->guest) ? NULL : $user->id;
640 }
641
642 /**
643 * @inheritDoc
644 */
645 public function getLoggedInUniqueIdentifier() {
646 $user = JFactory::getUser();
647 return $this->getUniqueIdentifierFromUserObject($user);
648 }
649
650 /**
651 * @inheritDoc
652 */
653 public function getUser($contactID) {
654 $user_details = parent::getUser($contactID);
655 $user = JFactory::getUser($user_details['id']);
656 $user_details['name'] = $user->name;
657 return $user_details;
658 }
659
660 /**
661 * @inheritDoc
662 */
663 public function getUserIDFromUserObject($user) {
664 return !empty($user->id) ? $user->id : NULL;
665 }
666
667 /**
668 * @inheritDoc
669 */
670 public function getUniqueIdentifierFromUserObject($user) {
671 return ($user->guest) ? NULL : $user->email;
672 }
673
674 /**
675 * @inheritDoc
676 */
677 public function getTimeZoneString() {
678 $timezone = JFactory::getConfig()->get('offset');
679 return !$timezone ? date_default_timezone_get() : $timezone;
680 }
681
682 /**
683 * Get a list of all installed modules, including enabled and disabled ones
684 *
685 * @return array
686 * CRM_Core_Module
687 */
688 public function getModules() {
689 $result = [];
690
691 $db = JFactory::getDbo();
692 $query = $db->getQuery(TRUE);
693 $query->select('type, folder, element, enabled')
694 ->from('#__extensions')
695 ->where('type =' . $db->Quote('plugin'));
696 $plugins = $db->setQuery($query)->loadAssocList();
697 foreach ($plugins as $plugin) {
698 // question: is the folder really a critical part of the plugin's name?
699 $name = implode('.', ['joomla', $plugin['type'], $plugin['folder'], $plugin['element']]);
700 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
701 }
702
703 return $result;
704 }
705
706 /**
707 * @inheritDoc
708 */
709 public function getLoginURL($destination = '') {
710 $config = CRM_Core_Config::singleton();
711 $loginURL = $config->userFrameworkBaseURL;
712 $loginURL = str_replace('administrator/', '', $loginURL);
713 $loginURL .= 'index.php?option=com_users&view=login';
714
715 //CRM-14872 append destination
716 if (!empty($destination)) {
717 $loginURL .= '&return=' . urlencode(base64_encode($destination));
718 }
719 return $loginURL;
720 }
721
722 /**
723 * @inheritDoc
724 */
725 public function getLoginDestination(&$form) {
726 $args = NULL;
727
728 $id = $form->get('id');
729 if ($id) {
730 $args .= "&id=$id";
731 }
732 else {
733 $gid = $form->get('gid');
734 if ($gid) {
735 $args .= "&gid=$gid";
736 }
737 else {
738 // Setup Personal Campaign Page link uses pageId
739 $pageId = $form->get('pageId');
740 if ($pageId) {
741 $component = $form->get('component');
742 $args .= "&pageId=$pageId&component=$component&action=add";
743 }
744 }
745 }
746
747 $destination = NULL;
748 if ($args) {
749 // append destination so user is returned to form they came from after login
750 $args = 'reset=1' . $args;
751 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
752 }
753
754 return $destination;
755 }
756
757 /**
758 * Determine the location of the CMS root.
759 *
760 * @return string|NULL
761 * local file system path to CMS root, or NULL if it cannot be determined
762 */
763 public function cmsRootPath() {
764 global $civicrm_paths;
765 if (!empty($civicrm_paths['cms.root']['path'])) {
766 return $civicrm_paths['cms.root']['path'];
767 }
768
769 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
770 if (file_exists("$siteRoot/administrator/index.php")) {
771 return $siteRoot;
772 }
773 return NULL;
774 }
775
776 /**
777 * @inheritDoc
778 */
779 public function getDefaultSiteSettings($dir = NULL) {
780 $config = CRM_Core_Config::singleton();
781 $url = preg_replace(
782 '|/administrator|',
783 '',
784 $config->userFrameworkBaseURL
785 );
786 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
787 // This is the original pattern, but it doesn't work under Windows.
788 // By setting the pattern to the one used before the change first and only
789 // changing it means that the change code only affects Windows users.
790 $pattern = '|/media/civicrm/.*$|';
791 if (DIRECTORY_SEPARATOR == '\\') {
792 // This regular expression will handle Windows as well as Linux
793 // and any combination of forward and back slashes in directory
794 // separators. We only apply it if the directory separator is the one
795 // used by Windows.
796 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
797 }
798 $siteRoot = preg_replace(
799 $pattern,
800 '',
801 $config->imageUploadDir
802 );
803 return [$url, NULL, $siteRoot];
804 }
805
806 /**
807 * @inheritDoc
808 */
809 public function getUserRecordUrl($contactID) {
810 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
811 $userRecordUrl = NULL;
812 // if logged in user has user edit access, then allow link to other users joomla profile
813 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
814 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
815 }
816 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
817 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
818 }
819 }
820
821 /**
822 * @inheritDoc
823 */
824 public function checkPermissionAddUser() {
825 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
826 return TRUE;
827 }
828 }
829
830 /**
831 * Output code from error function.
832 * @param string $content
833 */
834 public function outputError($content) {
835 if (class_exists('JErrorPage')) {
836 $error = new Exception($content);
837 JErrorPage::render($error);
838 }
839 elseif (class_exists('JError')) {
840 JError::raiseError('CiviCRM-001', $content);
841 }
842 else {
843 parent::outputError($content);
844 }
845 }
846
847 /**
848 * @inheritDoc
849 */
850 public function synchronizeUsers() {
851 $config = CRM_Core_Config::singleton();
852 if (PHP_SAPI != 'cli') {
853 set_time_limit(300);
854 }
855 $id = 'id';
856 $mail = 'email';
857 $name = 'name';
858
859 $JUserTable = &JTable::getInstance('User', 'JTable');
860
861 $db = $JUserTable->getDbo();
862 $query = $db->getQuery(TRUE);
863 $query->select($id . ', ' . $mail . ', ' . $name);
864 $query->from($JUserTable->getTableName());
865 $query->where($mail != '');
866
867 $db->setQuery($query);
868 $users = $db->loadObjectList();
869
870 $user = new StdClass();
871 $uf = $config->userFramework;
872 $contactCount = 0;
873 $contactCreated = 0;
874 $contactMatching = 0;
875 for ($i = 0; $i < count($users); $i++) {
876 $user->$id = $users[$i]->$id;
877 $user->$mail = $users[$i]->$mail;
878 $user->$name = $users[$i]->$name;
879 $contactCount++;
880 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
881 $users[$i]->$id,
882 $users[$i]->$mail,
883 $uf,
884 1,
885 'Individual',
886 TRUE
887 )
888 ) {
889 $contactCreated++;
890 }
891 else {
892 $contactMatching++;
893 }
894 if (is_object($match)) {
895 $match->free();
896 }
897 }
898
899 return [
900 'contactCount' => $contactCount,
901 'contactMatching' => $contactMatching,
902 'contactCreated' => $contactCreated,
903 ];
904 }
905
906 }