Merge pull request #12258 from eileenmcnaughton/alpha
[civicrm-core.git] / CRM / Utils / System / Joomla.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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());
b27d1855 139
140 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
6a488035
TO
141 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
142 $db->setQuery($query, 0, 10);
143 $users = $db->loadAssocList();
144
94f9f81a 145 $row = array();
6a488035
TO
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';
89374eb2 160 $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
161 array(1 => $email, 2 => $resetUrl)
162 );
163 }
164 }
165 }
166
167 /**
17f443df 168 * @inheritDoc
6a488035 169 */
00be9182 170 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
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);
6a488035
TO
180 }
181
182 /**
17f443df 183 * @inheritDoc
6a488035 184 */
00be9182 185 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
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);
6a488035
TO
206 }
207
208 /**
17f443df 209 * @inheritDoc
6a488035 210 */
00be9182 211 public function resetBreadCrumb() {
6a488035
TO
212 }
213
214 /**
17f443df 215 * @inheritDoc
6a488035 216 */
17f443df 217 public function addHTMLHead($string = NULL) {
6a488035
TO
218 if ($string) {
219 $document = JFactory::getDocument();
220 $document->addCustomTag($string);
221 }
222 }
223
224 /**
17f443df 225 * @inheritDoc
6a488035
TO
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 /**
17f443df 237 * @inheritDoc
6a488035
TO
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 /**
17f443df 249 * @inheritDoc
6a488035 250 */
e7483cbe 251 public function url(
17f443df
CW
252 $path = NULL,
253 $query = NULL,
254 $absolute = FALSE,
255 $fragment = NULL,
17f443df
CW
256 $frontend = FALSE,
257 $forceBackend = FALSE
6a488035 258 ) {
353ffa53 259 $config = CRM_Core_Config::singleton();
c80e2dbf 260 $separator = '&';
353ffa53
TO
261 $Itemid = '';
262 $script = '';
263 $path = CRM_Utils_String::stripPathChars($path);
6a488035
TO
264
265 if ($config->userFrameworkFrontend) {
266 $script = 'index.php';
378e2b4c 267 if (JRequest::getVar("Itemid") && (strpos($path, 'civicrm/payment/ipn') === FALSE)) {
6a488035
TO
268 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
269 }
270 }
271
272 if (isset($fragment)) {
273 $fragment = '#' . $fragment;
274 }
275
6a488035
TO
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;
0db6c3e1
TO
297 }
298 else {
e7483cbe 299 $jversion = new JVersion();
6a488035
TO
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
6a488035 310 /**
fe482240 311 * Set the email address of the user.
6a488035 312 *
77855840
TO
313 * @param object $user
314 * Handle to the user object.
6a488035 315 */
00be9182 316 public function setEmail(&$user) {
6a488035 317 global $database;
94f9f81a
EW
318 $query = $db->getQuery(TRUE);
319 $query->select($db->quoteName('email'))
320 ->from($db->quoteName('#__users'))
321 ->where($db->quoteName('id') . ' = ' . $user->id);
6a488035
TO
322 $database->setQuery($query);
323 $user->email = $database->loadResult();
324 }
325
326 /**
17f443df 327 * @inheritDoc
6a488035 328 */
17f443df 329 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
330 require_once 'DB.php';
331
332 $config = CRM_Core_Config::singleton();
ebc28bab 333 $user = NULL;
6a488035
TO
334
335 if ($loadCMSBootstrap) {
336 $bootStrapParams = array();
337 if ($name && $password) {
338 $bootStrapParams = array(
339 'name' => $name,
340 'pass' => $password,
341 );
342 }
bec3fc7c 343 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
6a488035
TO
344 }
345
346 jimport('joomla.application.component.helper');
347 jimport('joomla.database.table');
c1f3c6da
BS
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 }
6a488035 364
e1d37cef
MW
365 $joomlaBase = self::getBasePath();
366 self::getJVersion($joomlaBase);
6a488035 367
c1f3c6da
BS
368 if (!empty($row)) {
369 $dbPassword = $row->password;
370 $dbId = $row->id;
371 $dbEmail = $row->email;
6a488035 372
481a74f4
TO
373 if (version_compare(JVERSION, '2.5.18', 'lt') ||
374 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
c1f3c6da 375 ) {
ebc28bab 376 // now check password
009eff21
EW
377 list($hash, $salt) = explode(':', $dbPassword);
378 $cryptpass = md5($password . $salt);
379 if ($hash != $cryptpass) {
01c77fa9
EW
380 return FALSE;
381 }
6a488035 382 }
c1f3c6da 383 else {
4f99ca55
TO
384 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
385 return FALSE;
e7292422 386 }
9d735153 387
3a8c9bb6
MW
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 }
9d735153 393 //include additional files required by Joomla 3.2.1+
3a8c9bb6 394 elseif (version_compare(JVERSION, '3.2.1', 'ge')) {
90eac10a
BS
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';
9d735153 398 }
6a488035
TO
399 }
400
c1f3c6da 401 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
6a488035
TO
402 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
403 if (!$contactID) {
404 return FALSE;
405 }
406 return array($contactID, $dbId, mt_rand());
407 }
c1f3c6da 408
6a488035
TO
409 return FALSE;
410 }
411
bec3fc7c 412 /**
fe482240 413 * Set a init session with user object.
bec3fc7c 414 *
77855840
TO
415 * @param array $data
416 * Array with user specific data.
bec3fc7c 417 */
00be9182 418 public function setUserSession($data) {
bec3fc7c 419 list($userID, $ufID) = $data;
481a74f4 420 $user = new JUser($ufID);
2d8f9c75 421 $session = JFactory::getSession();
bec3fc7c
BS
422 $session->set('user', $user);
423
cb0e36de 424 parent::setUserSession($data);
bec3fc7c
BS
425 }
426
6a488035 427 /**
17f443df 428 * FIXME: Do something
ea3ddccf 429 *
430 * @param string $message
6a488035 431 */
00be9182 432 public function setMessage($message) {
6a488035
TO
433 }
434
bb3a214a 435 /**
b596c3e9 436 * @param \string $username
437 * @param \string $password
ea3ddccf 438 *
439 * @return bool
bb3a214a 440 */
b596c3e9 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 $session = CRM_Core_Session::singleton();
458 $session->set('ufID', $uid);
459 $session->set('userID', $contactID);
6a488035
TO
460 return TRUE;
461 }
462
17f443df
CW
463 /**
464 * FIXME: Use CMS-native approach
465 */
00be9182 466 public function permissionDenied() {
0499b0ad 467 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
468 }
469
17f443df
CW
470 /**
471 * @inheritDoc
472 */
00be9182 473 public function logout() {
6a488035 474 session_destroy();
d42a224c 475 CRM_Utils_System::setHttpHeader("Location", "index.php");
6a488035
TO
476 }
477
478 /**
17f443df 479 * @inheritDoc
6a488035 480 */
00be9182 481 public function getUFLocale() {
6a488035
TO
482 if (defined('_JEXEC')) {
483 $conf = JFactory::getConfig();
4965d8e9 484 $locale = $conf->get('language');
6a488035
TO
485 return str_replace('-', '_', $locale);
486 }
487 return NULL;
488 }
489
fd1f3a26
SV
490 /**
491 * @inheritDoc
492 */
493 public function setUFLocale($civicrm_language) {
494 // TODO
495 return TRUE;
496 }
497
bb3a214a 498 /**
17f443df 499 * @inheritDoc
bb3a214a 500 */
00be9182 501 public function getVersion() {
6a488035 502 if (class_exists('JVersion')) {
e7483cbe 503 $version = new JVersion();
6a488035
TO
504 return $version->getShortVersion();
505 }
506 else {
507 return 'Unknown';
508 }
509 }
510
e1d37cef
MW
511 public function getJVersion($joomlaBase) {
512 // Files may be in different places depending on Joomla version
513 if (!defined('JVERSION')) {
514 // Joomla 3.8.0+
515 $versionPhp = $joomlaBase . '/libraries/src/Version.php';
516 if (!file_exists($versionPhp)) {
517 // Joomla < 3.8.0
518 $versionPhp = $joomlaBase . '/libraries/cms/version/version.php';
519 }
520 require $versionPhp;
521 $jversion = new JVersion();
522 define('JVERSION', $jversion->getShortVersion());
523 }
524 }
525
f3884007
MW
526 /**
527 * Setup the base path related constant.
528 * @return mixed
529 */
e1d37cef 530 public function getBasePath() {
f3884007
MW
531 global $civicrm_root;
532 $joomlaPath = explode('/administrator', $civicrm_root);
533 $joomlaBase = $joomlaPath[0];
534 return $joomlaBase;
e1d37cef
MW
535 }
536
f4aaa82a 537 /**
fe482240 538 * Load joomla bootstrap.
6a488035 539 *
5a4f6742
CW
540 * @param array $params
541 * with uid or name and password.
542 * @param bool $loadUser
543 * load cms user?.
f4aaa82a
EM
544 * @param bool|\throw $throwError throw error on failure?
545 * @param null $realPath
546 * @param bool $loadDefines
547 *
548 * @return bool
6a488035 549 */
00be9182 550 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
e1d37cef 551 $joomlaBase = self::getBasePath();
6a488035
TO
552
553 // load BootStrap here if needed
554 // We are a valid Joomla entry point.
353ffa53 555 if (!defined('_JEXEC') && $loadDefines) {
6a488035
TO
556 define('_JEXEC', 1);
557 define('DS', DIRECTORY_SEPARATOR);
558 define('JPATH_BASE', $joomlaBase . '/administrator');
559 require $joomlaBase . '/administrator/includes/defines.php';
560 }
561
562 // Get the framework.
2cb7adde 563 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
2efcf0c2 564 require $joomlaBase . '/libraries/import.legacy.php';
2cb7adde 565 }
3a8c9bb6 566 require $joomlaBase . '/libraries/cms.php';
e1d37cef 567 self::getJVersion($joomlaBase);
6fde79f5 568
8421b502 569 if (version_compare(JVERSION, '3.8', 'lt')) {
570 require $joomlaBase . '/libraries/import.php';
571 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
572 }
573
574 require_once $joomlaBase . '/configuration.php';
575
481a74f4 576 if (version_compare(JVERSION, '3.0', 'lt')) {
6fde79f5
BS
577 require $joomlaBase . '/libraries/joomla/environment/uri.php';
578 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
579 }
8421b502 580 elseif (version_compare(JVERSION, '3.8', 'lt')) {
3a8c9bb6 581 jimport('joomla.environment.uri');
6fde79f5
BS
582 }
583
8421b502 584 if (version_compare(JVERSION, '3.8', 'lt')) {
585 jimport('joomla.application.cli');
586 }
f4aaa82a 587
a5291777
TO
588 if (!defined('JDEBUG')) {
589 define('JDEBUG', FALSE);
590 }
591
182f835d 592 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
593 $config = CRM_Core_Config::singleton();
594 CRM_Utils_Hook::config($config);
6a488035
TO
595
596 return TRUE;
597 }
598
599 /**
17f443df 600 * @inheritDoc
6a488035
TO
601 */
602 public function isUserLoggedIn() {
603 $user = JFactory::getUser();
604 return ($user->guest) ? FALSE : TRUE;
605 }
606
8caad0ce 607 /**
608 * @inheritDoc
609 */
610 public function isUserRegistrationPermitted() {
611 $userParams = JComponentHelper::getParams('com_users');
612 if (!$userParams->get('allowUserRegistration')) {
613 return FALSE;
614 }
615 return TRUE;
616 }
617
63df6889
HD
618 /**
619 * @inheritDoc
620 */
1a6630be 621 public function isPasswordUserGenerated() {
63df6889
HD
622 return TRUE;
623 }
624
6a488035 625 /**
17f443df 626 * @inheritDoc
6a488035
TO
627 */
628 public function getLoggedInUfID() {
629 $user = JFactory::getUser();
630 return ($user->guest) ? NULL : $user->id;
631 }
632
2b617cb0 633 /**
17f443df 634 * @inheritDoc
2b617cb0 635 */
00be9182 636 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
637 $user = JFactory::getUser();
638 return $this->getUniqueIdentifierFromUserObject($user);
639 }
353ffa53 640
cff0c9aa
DS
641 /**
642 * @inheritDoc
643 */
644 public function getUser($contactID) {
645 $user_details = parent::getUser($contactID);
646 $user = JFactory::getUser($user_details['id']);
647 $user_details['name'] = $user->name;
648 return $user_details;
649 }
650
32998c82 651 /**
17f443df 652 * @inheritDoc
32998c82 653 */
00be9182 654 public function getUserIDFromUserObject($user) {
32998c82
EM
655 return !empty($user->id) ? $user->id : NULL;
656 }
657
2b617cb0 658 /**
17f443df 659 * @inheritDoc
2b617cb0 660 */
00be9182 661 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
662 return ($user->guest) ? NULL : $user->email;
663 }
664
29864da4 665 /**
666 * @inheritDoc
667 */
668 public function getTimeZoneString() {
669 $timezone = JFactory::getConfig()->get('offset');
670 return !$timezone ? date_default_timezone_get() : $timezone;
671 }
672
6a488035
TO
673 /**
674 * Get a list of all installed modules, including enabled and disabled ones
675 *
a6c01b45
CW
676 * @return array
677 * CRM_Core_Module
6a488035 678 */
00be9182 679 public function getModules() {
6a488035
TO
680 $result = array();
681
682 $db = JFactory::getDbo();
e7292422 683 $query = $db->getQuery(TRUE);
6a488035
TO
684 $query->select('type, folder, element, enabled')
685 ->from('#__extensions')
686 ->where('type =' . $db->Quote('plugin'));
687 $plugins = $db->setQuery($query)->loadAssocList();
688 foreach ($plugins as $plugin) {
689 // question: is the folder really a critical part of the plugin's name?
690 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
691 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
692 }
693
694 return $result;
695 }
696
697 /**
17f443df 698 * @inheritDoc
6a488035
TO
699 */
700 public function getLoginURL($destination = '') {
701 $config = CRM_Core_Config::singleton();
702 $loginURL = $config->userFrameworkBaseURL;
703 $loginURL = str_replace('administrator/', '', $loginURL);
704 $loginURL .= 'index.php?option=com_users&view=login';
091412ab
BS
705
706 //CRM-14872 append destination
481a74f4 707 if (!empty($destination)) {
92fcb95f 708 $loginURL .= '&return=' . urlencode(base64_encode($destination));
091412ab 709 }
6a488035
TO
710 return $loginURL;
711 }
f813f78e 712
bb3a214a 713 /**
17f443df 714 * @inheritDoc
bb3a214a 715 */
6a488035 716 public function getLoginDestination(&$form) {
091412ab
BS
717 $args = NULL;
718
719 $id = $form->get('id');
720 if ($id) {
721 $args .= "&id=$id";
722 }
723 else {
724 $gid = $form->get('gid');
725 if ($gid) {
726 $args .= "&gid=$gid";
727 }
728 else {
729 // Setup Personal Campaign Page link uses pageId
730 $pageId = $form->get('pageId');
731 if ($pageId) {
732 $component = $form->get('component');
733 $args .= "&pageId=$pageId&component=$component&action=add";
734 }
735 }
736 }
737
738 $destination = NULL;
739 if ($args) {
740 // append destination so user is returned to form they came from after login
92fcb95f 741 $args = 'reset=1' . $args;
48341e06 742 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
091412ab
BS
743 }
744
745 return $destination;
6a488035 746 }
9977c6f5 747
214fbc2c 748 /**
749 * Determine the location of the CMS root.
750 *
751 * @return string|NULL
752 * local file system path to CMS root, or NULL if it cannot be determined
753 */
754 public function cmsRootPath() {
a93a0366
TO
755 global $civicrm_paths;
756 if (!empty($civicrm_paths['cms.root']['path'])) {
757 return $civicrm_paths['cms.root']['path'];
758 }
759
214fbc2c 760 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
e1d37cef 761 if (file_exists("$siteRoot/administrator/index.php")) {
214fbc2c 762 return $siteRoot;
763 }
764 return NULL;
765 }
766
9977c6f5 767 /**
17f443df 768 * @inheritDoc
9977c6f5 769 */
70e8beda 770 public function getDefaultSiteSettings($dir = NULL) {
9977c6f5 771 $config = CRM_Core_Config::singleton();
772 $url = preg_replace(
773 '|/administrator|',
774 '',
775 $config->userFrameworkBaseURL
776 );
ad2d96ba 777 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
778 // This is the original pattern, but it doesn't work under Windows.
779 // By setting the pattern to the one used before the change first and only
780 // changing it means that the change code only affects Windows users.
781 $pattern = '|/media/civicrm/.*$|';
782 if (DIRECTORY_SEPARATOR == '\\') {
783 // This regular expression will handle Windows as well as Linux
784 // and any combination of forward and back slashes in directory
785 // separators. We only apply it if the directory separator is the one
786 // used by Windows.
787 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
788 }
9977c6f5 789 $siteRoot = preg_replace(
ad2d96ba 790 $pattern,
9977c6f5 791 '',
792 $config->imageUploadDir
793 );
794 return array($url, NULL, $siteRoot);
795 }
59f97da6
EM
796
797 /**
17f443df 798 * @inheritDoc
59f97da6 799 */
00be9182 800 public function getUserRecordUrl($contactID) {
59f97da6
EM
801 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
802 $userRecordUrl = NULL;
55904b50 803 // if logged in user has user edit access, then allow link to other users joomla profile
a8e5af2a 804 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
59f97da6
EM
805 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
806 }
807 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
808 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
809 }
810 }
811
812 /**
17f443df 813 * @inheritDoc
59f97da6 814 */
00be9182 815 public function checkPermissionAddUser() {
59f97da6
EM
816 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
817 return TRUE;
818 }
819 }
f85b1d20
EM
820
821 /**
fe482240 822 * Output code from error function.
f85b1d20
EM
823 * @param string $content
824 */
00be9182 825 public function outputError($content) {
f85b1d20
EM
826 if (class_exists('JErrorPage')) {
827 $error = new Exception($content);
828 JErrorPage::render($error);
829 }
4c9b6178 830 elseif (class_exists('JError')) {
f85b1d20
EM
831 JError::raiseError('CiviCRM-001', $content);
832 }
833 else {
834 parent::outputError($content);
835 }
836 }
e7292422 837
f58e4c2e 838 /**
fe482240 839 * Append Joomla js to coreResourcesList.
ad37ac8e 840 *
841 * @param array $list
f58e4c2e 842 */
00be9182 843 public function appendCoreResources(&$list) {
f58e4c2e
DC
844 $list[] = 'js/crm.joomla.js';
845 }
96025800 846
03d5592a
CW
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 array(
900 'contactCount' => $contactCount,
901 'contactMatching' => $contactMatching,
902 'contactCreated' => $contactCreated,
903 );
904 }
905
6a488035 906}