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