Merge pull request #7970 from otetard/CRM-18235
[civicrm-core.git] / CRM / Utils / System / Joomla.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
b8c71ffa 35 * Joomla specific stuff goes here.
6a488035
TO
36 */
37class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
bb3a214a 38 /**
b8c71ffa 39 * Class constructor.
bb3a214a 40 */
00be9182 41 public function __construct() {
4caaa696
EM
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 */
6a488035
TO
48 $this->is_drupal = FALSE;
49 }
50
51 /**
17f443df 52 * @inheritDoc
6a488035 53 */
00be9182 54 public function createUser(&$params, $mail) {
6a488035
TO
55 $baseDir = JPATH_SITE;
56 require_once $baseDir . '/components/com_users/models/registration.php';
57
58 $userParams = JComponentHelper::getParams('com_users');
353ffa53
TO
59 $model = new UsersModelRegistration();
60 $ufID = NULL;
6a488035
TO
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.
353ffa53
TO
79 $values = array();
80 $values['name'] = $fullname;
81 $values['username'] = trim($params['cms_name']);
6a488035 82 $values['password1'] = $values['password2'] = $params['cms_pass'];
353ffa53 83 $values['email1'] = $values['email2'] = trim($params[$mail]);
6a488035
TO
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
f4aaa82a 94 /**
17f443df 95 * @inheritDoc
6a488035 96 */
00be9182 97 public function updateCMSName($ufID, $ufName) {
6a488035
TO
98 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
99 $ufName = CRM_Utils_Type::escape($ufName, 'String');
100
101 $values = array();
e851ce06 102 $user = JUser::getInstance($ufID);
6a488035
TO
103
104 $values['email'] = $ufName;
105 $user->bind($values);
106
107 $user->save();
108 }
109
110 /**
94f9f81a 111 * Check if username and email exists in the Joomla db.
6a488035 112 *
77855840
TO
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'.
6a488035 119 */
00be9182 120 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
121 $config = CRM_Core_Config::singleton();
122
353ffa53
TO
123 $dao = new CRM_Core_DAO();
124 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
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
6a488035
TO
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 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
140 $db->setQuery($query, 0, 10);
141 $users = $db->loadAssocList();
142
94f9f81a 143 $row = array();
6a488035
TO
144 if (count($users)) {
145 $row = $users[0];
146 }
147
148 if (!empty($row)) {
149 $dbName = CRM_Utils_Array::value('username', $row);
150 $dbEmail = CRM_Utils_Array::value('email', $row);
151 if (strtolower($dbName) == strtolower($name)) {
152 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
153 array(1 => $name)
154 );
155 }
156 if (strtolower($dbEmail) == strtolower($email)) {
157 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
89374eb2 158 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
6a488035
TO
159 array(1 => $email, 2 => $resetUrl)
160 );
161 }
162 }
163 }
164
165 /**
17f443df 166 * @inheritDoc
6a488035 167 */
00be9182 168 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
169 if (!$pageTitle) {
170 $pageTitle = $title;
171 }
172
173 $template = CRM_Core_Smarty::singleton();
174 $template->assign('pageTitle', $pageTitle);
175
176 $document = JFactory::getDocument();
177 $document->setTitle($title);
6a488035
TO
178 }
179
180 /**
17f443df 181 * @inheritDoc
6a488035 182 */
00be9182 183 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
184 $template = CRM_Core_Smarty::singleton();
185 $bc = $template->get_template_vars('breadcrumb');
186
187 if (is_array($breadCrumbs)) {
188 foreach ($breadCrumbs as $crumbs) {
189 if (stripos($crumbs['url'], 'id%%')) {
190 $args = array('cid', 'mid');
191 foreach ($args as $a) {
192 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
193 FALSE, NULL, $_GET
194 );
195 if ($val) {
196 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
197 }
198 }
199 }
200 $bc[] = $crumbs;
201 }
202 }
203 $template->assign_by_ref('breadcrumb', $bc);
6a488035
TO
204 }
205
206 /**
17f443df 207 * @inheritDoc
6a488035 208 */
00be9182 209 public function resetBreadCrumb() {
6a488035
TO
210 }
211
212 /**
17f443df 213 * @inheritDoc
6a488035 214 */
17f443df 215 public function addHTMLHead($string = NULL) {
6a488035
TO
216 if ($string) {
217 $document = JFactory::getDocument();
218 $document->addCustomTag($string);
219 }
220 }
221
222 /**
17f443df 223 * @inheritDoc
6a488035
TO
224 */
225 public function addStyleUrl($url, $region) {
226 if ($region == 'html-header') {
227 $document = JFactory::getDocument();
228 $document->addStyleSheet($url);
229 return TRUE;
230 }
231 return FALSE;
232 }
233
234 /**
17f443df 235 * @inheritDoc
6a488035
TO
236 */
237 public function addStyle($code, $region) {
238 if ($region == 'html-header') {
239 $document = JFactory::getDocument();
240 $document->addStyleDeclaration($code);
241 return TRUE;
242 }
243 return FALSE;
244 }
245
246 /**
17f443df 247 * @inheritDoc
6a488035 248 */
e7483cbe 249 public function url(
17f443df
CW
250 $path = NULL,
251 $query = NULL,
252 $absolute = FALSE,
253 $fragment = NULL,
17f443df
CW
254 $frontend = FALSE,
255 $forceBackend = FALSE
6a488035 256 ) {
353ffa53 257 $config = CRM_Core_Config::singleton();
c80e2dbf 258 $separator = '&';
353ffa53
TO
259 $Itemid = '';
260 $script = '';
261 $path = CRM_Utils_String::stripPathChars($path);
6a488035
TO
262
263 if ($config->userFrameworkFrontend) {
264 $script = 'index.php';
265 if (JRequest::getVar("Itemid")) {
266 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
267 }
268 }
269
270 if (isset($fragment)) {
271 $fragment = '#' . $fragment;
272 }
273
6a488035
TO
274 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
275
276 if (!empty($query)) {
277 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
278 }
279 else {
280 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
281 }
282
283 // gross hack for joomla, we are in the backend and want to send a frontend url
284 if ($frontend && $config->userFramework == 'Joomla') {
285 // handle both joomla v1.5 and v1.6, CRM-7939
286 $url = str_replace('/administrator/index2.php', '/index.php', $url);
287 $url = str_replace('/administrator/index.php', '/index.php', $url);
288
289 // CRM-8215
290 $url = str_replace('/administrator/', '/index.php', $url);
291 }
292 elseif ($forceBackend) {
293 if (defined('JVERSION')) {
294 $joomlaVersion = JVERSION;
0db6c3e1
TO
295 }
296 else {
e7483cbe 297 $jversion = new JVersion();
6a488035
TO
298 $joomlaVersion = $jversion->getShortVersion();
299 }
300
301 if (version_compare($joomlaVersion, '1.6') >= 0) {
302 $url = str_replace('/index.php', '/administrator/index.php', $url);
303 }
304 }
305 return $url;
306 }
307
6a488035 308 /**
fe482240 309 * Set the email address of the user.
6a488035 310 *
77855840
TO
311 * @param object $user
312 * Handle to the user object.
6a488035 313 */
00be9182 314 public function setEmail(&$user) {
6a488035 315 global $database;
94f9f81a
EW
316 $query = $db->getQuery(TRUE);
317 $query->select($db->quoteName('email'))
318 ->from($db->quoteName('#__users'))
319 ->where($db->quoteName('id') . ' = ' . $user->id);
6a488035
TO
320 $database->setQuery($query);
321 $user->email = $database->loadResult();
322 }
323
324 /**
17f443df 325 * @inheritDoc
6a488035 326 */
17f443df 327 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
328 require_once 'DB.php';
329
330 $config = CRM_Core_Config::singleton();
ebc28bab 331 $user = NULL;
6a488035
TO
332
333 if ($loadCMSBootstrap) {
334 $bootStrapParams = array();
335 if ($name && $password) {
336 $bootStrapParams = array(
337 'name' => $name,
338 'pass' => $password,
339 );
340 }
bec3fc7c 341 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
6a488035
TO
342 }
343
344 jimport('joomla.application.component.helper');
345 jimport('joomla.database.table');
c1f3c6da
BS
346 jimport('joomla.user.helper');
347
348 $JUserTable = JTable::getInstance('User', 'JTable');
349
350 $db = $JUserTable->getDbo();
351 $query = $db->getQuery(TRUE);
352 $query->select('id, name, username, email, password');
353 $query->from($JUserTable->getTableName());
354 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
355 $db->setQuery($query, 0, 0);
356 $users = $db->loadObjectList();
357
358 $row = array();
359 if (count($users)) {
360 $row = $users[0];
361 }
6a488035 362
e46506b2 363 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
481a74f4 364 if (!defined('JVERSION')) {
ebc28bab 365 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 366 $jversion = new JVersion();
ebc28bab
BS
367 define('JVERSION', $jversion->getShortVersion());
368 }
6a488035 369
c1f3c6da
BS
370 if (!empty($row)) {
371 $dbPassword = $row->password;
372 $dbId = $row->id;
373 $dbEmail = $row->email;
6a488035 374
481a74f4
TO
375 if (version_compare(JVERSION, '2.5.18', 'lt') ||
376 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
c1f3c6da 377 ) {
ebc28bab 378 // now check password
009eff21
EW
379 list($hash, $salt) = explode(':', $dbPassword);
380 $cryptpass = md5($password . $salt);
381 if ($hash != $cryptpass) {
01c77fa9
EW
382 return FALSE;
383 }
6a488035 384 }
c1f3c6da 385 else {
4f99ca55
TO
386 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
387 return FALSE;
e7292422 388 }
9d735153
BS
389
390 //include additional files required by Joomla 3.2.1+
481a74f4 391 if (version_compare(JVERSION, '3.2.1', 'ge')) {
90eac10a
BS
392 require_once $joomlaBase . '/libraries/cms/application/helper.php';
393 require_once $joomlaBase . '/libraries/cms/application/cms.php';
394 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
9d735153 395 }
6a488035
TO
396 }
397
c1f3c6da 398 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
6a488035
TO
399 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
400 if (!$contactID) {
401 return FALSE;
402 }
403 return array($contactID, $dbId, mt_rand());
404 }
c1f3c6da 405
6a488035
TO
406 return FALSE;
407 }
408
bec3fc7c 409 /**
fe482240 410 * Set a init session with user object.
bec3fc7c 411 *
77855840
TO
412 * @param array $data
413 * Array with user specific data.
bec3fc7c 414 */
00be9182 415 public function setUserSession($data) {
bec3fc7c 416 list($userID, $ufID) = $data;
481a74f4 417 $user = new JUser($ufID);
2d8f9c75 418 $session = JFactory::getSession();
bec3fc7c
BS
419 $session->set('user', $user);
420
cb0e36de 421 parent::setUserSession($data);
bec3fc7c
BS
422 }
423
6a488035 424 /**
17f443df 425 * FIXME: Do something
ea3ddccf 426 *
427 * @param string $message
6a488035 428 */
00be9182 429 public function setMessage($message) {
6a488035
TO
430 }
431
bb3a214a 432 /**
b596c3e9 433 * @param \string $username
434 * @param \string $password
ea3ddccf 435 *
436 * @return bool
bb3a214a 437 */
b596c3e9 438 public function loadUser($username, $password = NULL) {
439 $uid = JUserHelper::getUserId($username);
440 if (empty($uid)) {
441 return FALSE;
442 }
443 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
444 if (!empty($password)) {
445 $instance = JFactory::getApplication('site');
446 $params = array(
447 'username' => $username,
448 'password' => $password,
449 );
450 //perform the login action
451 $instance->login($params);
452 }
453
454 $session = CRM_Core_Session::singleton();
455 $session->set('ufID', $uid);
456 $session->set('userID', $contactID);
6a488035
TO
457 return TRUE;
458 }
459
17f443df
CW
460 /**
461 * FIXME: Use CMS-native approach
462 */
00be9182 463 public function permissionDenied() {
0499b0ad 464 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
465 }
466
17f443df
CW
467 /**
468 * @inheritDoc
469 */
00be9182 470 public function logout() {
6a488035 471 session_destroy();
d42a224c 472 CRM_Utils_System::setHttpHeader("Location", "index.php");
6a488035
TO
473 }
474
475 /**
17f443df 476 * @inheritDoc
6a488035 477 */
00be9182 478 public function getUFLocale() {
6a488035
TO
479 if (defined('_JEXEC')) {
480 $conf = JFactory::getConfig();
4965d8e9 481 $locale = $conf->get('language');
6a488035
TO
482 return str_replace('-', '_', $locale);
483 }
484 return NULL;
485 }
486
fd1f3a26
SV
487 /**
488 * @inheritDoc
489 */
490 public function setUFLocale($civicrm_language) {
491 // TODO
492 return TRUE;
493 }
494
bb3a214a 495 /**
17f443df 496 * @inheritDoc
bb3a214a 497 */
00be9182 498 public function getVersion() {
6a488035 499 if (class_exists('JVersion')) {
e7483cbe 500 $version = new JVersion();
6a488035
TO
501 return $version->getShortVersion();
502 }
503 else {
504 return 'Unknown';
505 }
506 }
507
f4aaa82a 508 /**
fe482240 509 * Load joomla bootstrap.
6a488035 510 *
5a4f6742
CW
511 * @param array $params
512 * with uid or name and password.
513 * @param bool $loadUser
514 * load cms user?.
f4aaa82a
EM
515 * @param bool|\throw $throwError throw error on failure?
516 * @param null $realPath
517 * @param bool $loadDefines
518 *
519 * @return bool
6a488035 520 */
00be9182 521 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
6a488035
TO
522 // Setup the base path related constant.
523 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
524
525 // load BootStrap here if needed
526 // We are a valid Joomla entry point.
353ffa53 527 if (!defined('_JEXEC') && $loadDefines) {
6a488035
TO
528 define('_JEXEC', 1);
529 define('DS', DIRECTORY_SEPARATOR);
530 define('JPATH_BASE', $joomlaBase . '/administrator');
531 require $joomlaBase . '/administrator/includes/defines.php';
532 }
533
534 // Get the framework.
2cb7adde 535 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
2efcf0c2 536 require $joomlaBase . '/libraries/import.legacy.php';
2cb7adde 537 }
6a488035
TO
538 require $joomlaBase . '/libraries/import.php';
539 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
6a488035
TO
540 require $joomlaBase . '/configuration.php';
541
6fde79f5 542 // Files may be in different places depending on Joomla version
481a74f4 543 if (!defined('JVERSION')) {
6fde79f5 544 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 545 $jversion = new JVersion();
6fde79f5
BS
546 define('JVERSION', $jversion->getShortVersion());
547 }
548
481a74f4 549 if (version_compare(JVERSION, '3.0', 'lt')) {
6fde79f5
BS
550 require $joomlaBase . '/libraries/joomla/environment/uri.php';
551 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
552 }
553 else {
87cdafb0 554 require $joomlaBase . '/libraries/cms.php';
6fde79f5 555 require $joomlaBase . '/libraries/joomla/uri/uri.php';
6fde79f5
BS
556 }
557
6a488035 558 jimport('joomla.application.cli');
f4aaa82a 559
182f835d 560 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
561 $config = CRM_Core_Config::singleton();
562 CRM_Utils_Hook::config($config);
6a488035
TO
563
564 return TRUE;
565 }
566
567 /**
17f443df 568 * @inheritDoc
6a488035
TO
569 */
570 public function isUserLoggedIn() {
571 $user = JFactory::getUser();
572 return ($user->guest) ? FALSE : TRUE;
573 }
574
575 /**
17f443df 576 * @inheritDoc
6a488035
TO
577 */
578 public function getLoggedInUfID() {
579 $user = JFactory::getUser();
580 return ($user->guest) ? NULL : $user->id;
581 }
582
2b617cb0 583 /**
17f443df 584 * @inheritDoc
2b617cb0 585 */
00be9182 586 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
587 $user = JFactory::getUser();
588 return $this->getUniqueIdentifierFromUserObject($user);
589 }
353ffa53 590
32998c82 591 /**
17f443df 592 * @inheritDoc
32998c82 593 */
00be9182 594 public function getUserIDFromUserObject($user) {
32998c82
EM
595 return !empty($user->id) ? $user->id : NULL;
596 }
597
2b617cb0 598 /**
17f443df 599 * @inheritDoc
2b617cb0 600 */
00be9182 601 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
602 return ($user->guest) ? NULL : $user->email;
603 }
604
6a488035
TO
605 /**
606 * Get a list of all installed modules, including enabled and disabled ones
607 *
a6c01b45
CW
608 * @return array
609 * CRM_Core_Module
6a488035 610 */
00be9182 611 public function getModules() {
6a488035
TO
612 $result = array();
613
614 $db = JFactory::getDbo();
e7292422 615 $query = $db->getQuery(TRUE);
6a488035
TO
616 $query->select('type, folder, element, enabled')
617 ->from('#__extensions')
618 ->where('type =' . $db->Quote('plugin'));
619 $plugins = $db->setQuery($query)->loadAssocList();
620 foreach ($plugins as $plugin) {
621 // question: is the folder really a critical part of the plugin's name?
622 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
623 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
624 }
625
626 return $result;
627 }
628
629 /**
17f443df 630 * @inheritDoc
6a488035
TO
631 */
632 public function getLoginURL($destination = '') {
633 $config = CRM_Core_Config::singleton();
634 $loginURL = $config->userFrameworkBaseURL;
635 $loginURL = str_replace('administrator/', '', $loginURL);
636 $loginURL .= 'index.php?option=com_users&view=login';
091412ab
BS
637
638 //CRM-14872 append destination
481a74f4 639 if (!empty($destination)) {
92fcb95f 640 $loginURL .= '&return=' . urlencode(base64_encode($destination));
091412ab 641 }
6a488035
TO
642 return $loginURL;
643 }
f813f78e 644
bb3a214a 645 /**
17f443df 646 * @inheritDoc
bb3a214a 647 */
6a488035 648 public function getLoginDestination(&$form) {
091412ab
BS
649 $args = NULL;
650
651 $id = $form->get('id');
652 if ($id) {
653 $args .= "&id=$id";
654 }
655 else {
656 $gid = $form->get('gid');
657 if ($gid) {
658 $args .= "&gid=$gid";
659 }
660 else {
661 // Setup Personal Campaign Page link uses pageId
662 $pageId = $form->get('pageId');
663 if ($pageId) {
664 $component = $form->get('component');
665 $args .= "&pageId=$pageId&component=$component&action=add";
666 }
667 }
668 }
669
670 $destination = NULL;
671 if ($args) {
672 // append destination so user is returned to form they came from after login
92fcb95f 673 $args = 'reset=1' . $args;
48341e06 674 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
091412ab
BS
675 }
676
677 return $destination;
6a488035 678 }
9977c6f5 679
680 /**
17f443df 681 * @inheritDoc
9977c6f5 682 */
9b873358 683 public function getDefaultSiteSettings($dir) {
9977c6f5 684 $config = CRM_Core_Config::singleton();
685 $url = preg_replace(
686 '|/administrator|',
687 '',
688 $config->userFrameworkBaseURL
689 );
690 $siteRoot = preg_replace(
691 '|/media/civicrm/.*$|',
692 '',
693 $config->imageUploadDir
694 );
695 return array($url, NULL, $siteRoot);
696 }
59f97da6
EM
697
698 /**
17f443df 699 * @inheritDoc
59f97da6 700 */
00be9182 701 public function getUserRecordUrl($contactID) {
59f97da6
EM
702 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
703 $userRecordUrl = NULL;
55904b50 704 // if logged in user has user edit access, then allow link to other users joomla profile
a8e5af2a 705 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
59f97da6
EM
706 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
707 }
708 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
709 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
710 }
711 }
712
713 /**
17f443df 714 * @inheritDoc
59f97da6 715 */
00be9182 716 public function checkPermissionAddUser() {
59f97da6
EM
717 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
718 return TRUE;
719 }
720 }
f85b1d20
EM
721
722 /**
fe482240 723 * Output code from error function.
f85b1d20
EM
724 * @param string $content
725 */
00be9182 726 public function outputError($content) {
f85b1d20
EM
727 if (class_exists('JErrorPage')) {
728 $error = new Exception($content);
729 JErrorPage::render($error);
730 }
4c9b6178 731 elseif (class_exists('JError')) {
f85b1d20
EM
732 JError::raiseError('CiviCRM-001', $content);
733 }
734 else {
735 parent::outputError($content);
736 }
737 }
e7292422 738
f58e4c2e 739 /**
fe482240 740 * Append Joomla js to coreResourcesList.
ad37ac8e 741 *
742 * @param array $list
f58e4c2e 743 */
00be9182 744 public function appendCoreResources(&$list) {
f58e4c2e
DC
745 $list[] = 'js/crm.joomla.js';
746 }
96025800 747
03d5592a
CW
748 /**
749 * @inheritDoc
750 */
751 public function synchronizeUsers() {
752 $config = CRM_Core_Config::singleton();
753 if (PHP_SAPI != 'cli') {
754 set_time_limit(300);
755 }
756 $id = 'id';
757 $mail = 'email';
758 $name = 'name';
759
760 $JUserTable = &JTable::getInstance('User', 'JTable');
761
762 $db = $JUserTable->getDbo();
763 $query = $db->getQuery(TRUE);
764 $query->select($id . ', ' . $mail . ', ' . $name);
765 $query->from($JUserTable->getTableName());
766 $query->where($mail != '');
767
768 $db->setQuery($query);
769 $users = $db->loadObjectList();
770
771 $user = new StdClass();
772 $uf = $config->userFramework;
773 $contactCount = 0;
774 $contactCreated = 0;
775 $contactMatching = 0;
776 for ($i = 0; $i < count($users); $i++) {
777 $user->$id = $users[$i]->$id;
778 $user->$mail = $users[$i]->$mail;
779 $user->$name = $users[$i]->$name;
780 $contactCount++;
781 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
782 $users[$i]->$id,
783 $users[$i]->$mail,
784 $uf,
785 1,
786 'Individual',
787 TRUE
788 )
789 ) {
790 $contactCreated++;
791 }
792 else {
793 $contactMatching++;
794 }
795 if (is_object($match)) {
796 $match->free();
797 }
798 }
799
800 return array(
801 'contactCount' => $contactCount,
802 'contactMatching' => $contactMatching,
803 'contactCreated' => $contactCreated,
804 );
805 }
806
6a488035 807}