Merge pull request #5041 from colemanw/CRM-15890
[civicrm-core.git] / CRM / Utils / System / Joomla.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Joomla specific stuff goes here
38 */
39class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
bb3a214a 40 /**
bb3a214a 41 */
00be9182 42 public function __construct() {
4caaa696
EM
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 */
6a488035
TO
49 $this->is_drupal = FALSE;
50 }
51
52 /**
100fef9d 53 * Create a user of Joomla.
6a488035 54 *
77855840 55 * @param array $params
77855840
TO
56 * @param string $mail
57 * Email id for cms user.
6a488035 58 *
72b3a70c
CW
59 * @return int|bool
60 * uid if user exists, false otherwise
6a488035 61 */
00be9182 62 public function createUser(&$params, $mail) {
6a488035
TO
63 $baseDir = JPATH_SITE;
64 require_once $baseDir . '/components/com_users/models/registration.php';
65
66 $userParams = JComponentHelper::getParams('com_users');
353ffa53
TO
67 $model = new UsersModelRegistration();
68 $ufID = NULL;
6a488035
TO
69
70 // get the default usertype
71 $userType = $userParams->get('new_usertype');
72 if (!$userType) {
73 $userType = 2;
74 }
75
76 if (isset($params['name'])) {
77 $fullname = trim($params['name']);
78 }
79 elseif (isset($params['contactID'])) {
80 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
81 }
82 else {
83 $fullname = trim($params['cms_name']);
84 }
85
86 // Prepare the values for a new Joomla user.
353ffa53
TO
87 $values = array();
88 $values['name'] = $fullname;
89 $values['username'] = trim($params['cms_name']);
6a488035 90 $values['password1'] = $values['password2'] = $params['cms_pass'];
353ffa53 91 $values['email1'] = $values['email2'] = trim($params[$mail]);
6a488035
TO
92
93 $lang = JFactory::getLanguage();
94 $lang->load('com_users', $baseDir);
95
96 $register = $model->register($values);
97
98 $ufID = JUserHelper::getUserId($values['username']);
99 return $ufID;
100 }
101
f4aaa82a 102 /**
6a488035
TO
103 * Change user name in host CMS
104 *
e7292422
TO
105 * @param int $ufID
106 * @param string $ufName User name
6a488035 107 */
00be9182 108 public function updateCMSName($ufID, $ufName) {
6a488035
TO
109 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
110 $ufName = CRM_Utils_Type::escape($ufName, 'String');
111
112 $values = array();
e851ce06 113 $user = JUser::getInstance($ufID);
6a488035
TO
114
115 $values['email'] = $ufName;
116 $user->bind($values);
117
118 $user->save();
119 }
120
121 /**
c490a46a 122 * Check if username and email exists in the drupal db
6a488035 123 *
77855840
TO
124 * @param array $params
125 * Array of name and mail values.
126 * @param array $errors
127 * Array of errors.
128 * @param string $emailName
129 * Field label for the 'email'.
f4aaa82a 130 *
6a488035
TO
131 * @return void
132 */
00be9182 133 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
134 $config = CRM_Core_Config::singleton();
135
353ffa53
TO
136 $dao = new CRM_Core_DAO();
137 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
138 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
139 //don't allow the special characters and min. username length is two
140 //regex \\ to match a single backslash would become '/\\\\/'
141 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
142 if ($isNotValid || strlen($name) < 2) {
143 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
144 }
145
6a488035
TO
146 $JUserTable = &JTable::getInstance('User', 'JTable');
147
148 $db = $JUserTable->getDbo();
149 $query = $db->getQuery(TRUE);
150 $query->select('username, email');
151 $query->from($JUserTable->getTableName());
152 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
153 $db->setQuery($query, 0, 10);
154 $users = $db->loadAssocList();
155
156 $row = array();;
157 if (count($users)) {
158 $row = $users[0];
159 }
160
161 if (!empty($row)) {
162 $dbName = CRM_Utils_Array::value('username', $row);
163 $dbEmail = CRM_Utils_Array::value('email', $row);
164 if (strtolower($dbName) == strtolower($name)) {
165 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
166 array(1 => $name)
167 );
168 }
169 if (strtolower($dbEmail) == strtolower($email)) {
170 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
171 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
172 array(1 => $email, 2 => $resetUrl)
173 );
174 }
175 }
176 }
177
178 /**
100fef9d 179 * Sets the title of the page
6a488035 180 *
77855840
TO
181 * @param string $title
182 * Title to set.
6a488035
TO
183 * @param string $pageTitle
184 *
185 * @return void
6a488035 186 */
00be9182 187 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
188 if (!$pageTitle) {
189 $pageTitle = $title;
190 }
191
192 $template = CRM_Core_Smarty::singleton();
193 $template->assign('pageTitle', $pageTitle);
194
195 $document = JFactory::getDocument();
196 $document->setTitle($title);
6a488035
TO
197 }
198
199 /**
200 * Append an additional breadcrumb tag to the existing breadcrumb
201 *
f4aaa82a
EM
202 * @param $breadCrumbs
203 *
204 * @internal param string $title
205 * @internal param string $url
6a488035
TO
206 *
207 * @return void
6a488035 208 */
00be9182 209 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
210 $template = CRM_Core_Smarty::singleton();
211 $bc = $template->get_template_vars('breadcrumb');
212
213 if (is_array($breadCrumbs)) {
214 foreach ($breadCrumbs as $crumbs) {
215 if (stripos($crumbs['url'], 'id%%')) {
216 $args = array('cid', 'mid');
217 foreach ($args as $a) {
218 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
219 FALSE, NULL, $_GET
220 );
221 if ($val) {
222 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
223 }
224 }
225 }
226 $bc[] = $crumbs;
227 }
228 }
229 $template->assign_by_ref('breadcrumb', $bc);
6a488035
TO
230 }
231
232 /**
233 * Reset an additional breadcrumb tag to the existing breadcrumb
234 *
f4aaa82a 235 * @internal param string $bc the new breadcrumb to be appended
6a488035
TO
236 *
237 * @return void
6a488035 238 */
00be9182 239 public function resetBreadCrumb() {
6a488035
TO
240 }
241
242 /**
243 * Append a string to the head of the html file
244 *
f4aaa82a
EM
245 * @param null $string
246 *
247 * @internal param string $head the new string to be appended
6a488035
TO
248 *
249 * @return void
6a488035 250 */
00be9182 251 public static function addHTMLHead($string = NULL) {
6a488035
TO
252 if ($string) {
253 $document = JFactory::getDocument();
254 $document->addCustomTag($string);
255 }
256 }
257
258 /**
259 * Add a script file
260 *
353ffa53 261 * @param $url : string, absolute path to file
5a4f6742
CW
262 * @param string $region
263 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
264 *
265 * Note: This function is not to be called directly
266 * @see CRM_Core_Region::render()
267 *
a6c01b45
CW
268 * @return bool
269 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
270 */
271 public function addScriptUrl($url, $region) {
6a488035
TO
272 return FALSE;
273 }
274
275 /**
276 * Add an inline script
277 *
353ffa53 278 * @param $code : string, javascript code
5a4f6742
CW
279 * @param string $region
280 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
281 *
282 * Note: This function is not to be called directly
283 * @see CRM_Core_Region::render()
284 *
a6c01b45
CW
285 * @return bool
286 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
287 */
288 public function addScript($code, $region) {
6a488035
TO
289 return FALSE;
290 }
291
292 /**
293 * Add a css file
294 *
353ffa53 295 * @param $url : string, absolute path to file
5a4f6742
CW
296 * @param string $region
297 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
298 *
299 * Note: This function is not to be called directly
300 * @see CRM_Core_Region::render()
301 *
a6c01b45
CW
302 * @return bool
303 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
304 */
305 public function addStyleUrl($url, $region) {
306 if ($region == 'html-header') {
307 $document = JFactory::getDocument();
308 $document->addStyleSheet($url);
309 return TRUE;
310 }
311 return FALSE;
312 }
313
314 /**
315 * Add an inline style
316 *
353ffa53 317 * @param $code : string, css code
5a4f6742
CW
318 * @param string $region
319 * location within the document: 'html-header', 'page-header', 'page-footer'.
6a488035
TO
320 *
321 * Note: This function is not to be called directly
322 * @see CRM_Core_Region::render()
323 *
a6c01b45
CW
324 * @return bool
325 * TRUE if we support this operation in this CMS, FALSE otherwise
6a488035
TO
326 */
327 public function addStyle($code, $region) {
328 if ($region == 'html-header') {
329 $document = JFactory::getDocument();
330 $document->addStyleDeclaration($code);
331 return TRUE;
332 }
333 return FALSE;
334 }
335
336 /**
337 * Generate an internal CiviCRM URL
338 *
5a4f6742
CW
339 * @param string $path
340 * The path being linked to, such as "civicrm/add".
341 * @param string $query
342 * A query string to append to the link.
343 * @param bool $absolute
344 * Whether to force the output to be an absolute link (beginning with http:).
6a488035
TO
345 * Useful for links that will be displayed outside the site, such as in an
346 * RSS feed.
5a4f6742
CW
347 * @param string $fragment
348 * A fragment identifier (named anchor) to append to the link.
349 * @param bool $htmlize
350 * whether to convert to html eqivalant.
351 * @param bool $frontend
352 * a gross joomla hack.
6a488035 353 *
f4aaa82a
EM
354 * @param bool $forceBackend
355 *
a6c01b45
CW
356 * @return string
357 * an HTML string containing a link to the given path.
6a488035 358 */
e7483cbe 359 public function url(
a3e55d9c 360 $path = NULL, $query = NULL, $absolute = TRUE,
6a488035
TO
361 $fragment = NULL, $htmlize = TRUE,
362 $frontend = FALSE, $forceBackend = FALSE
363 ) {
353ffa53 364 $config = CRM_Core_Config::singleton();
6a488035 365 $separator = $htmlize ? '&amp;' : '&';
353ffa53
TO
366 $Itemid = '';
367 $script = '';
368 $path = CRM_Utils_String::stripPathChars($path);
6a488035
TO
369
370 if ($config->userFrameworkFrontend) {
371 $script = 'index.php';
372 if (JRequest::getVar("Itemid")) {
373 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
374 }
375 }
376
377 if (isset($fragment)) {
378 $fragment = '#' . $fragment;
379 }
380
381 if (!isset($config->useFrameworkRelativeBase)) {
382 $base = parse_url($config->userFrameworkBaseURL);
383 $config->useFrameworkRelativeBase = $base['path'];
384 }
385 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
386
387 if (!empty($query)) {
388 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
389 }
390 else {
391 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
392 }
393
394 // gross hack for joomla, we are in the backend and want to send a frontend url
395 if ($frontend && $config->userFramework == 'Joomla') {
396 // handle both joomla v1.5 and v1.6, CRM-7939
397 $url = str_replace('/administrator/index2.php', '/index.php', $url);
398 $url = str_replace('/administrator/index.php', '/index.php', $url);
399
400 // CRM-8215
401 $url = str_replace('/administrator/', '/index.php', $url);
402 }
403 elseif ($forceBackend) {
404 if (defined('JVERSION')) {
405 $joomlaVersion = JVERSION;
0db6c3e1
TO
406 }
407 else {
e7483cbe 408 $jversion = new JVersion();
6a488035
TO
409 $joomlaVersion = $jversion->getShortVersion();
410 }
411
412 if (version_compare($joomlaVersion, '1.6') >= 0) {
413 $url = str_replace('/index.php', '/administrator/index.php', $url);
414 }
415 }
416 return $url;
417 }
418
419 /**
100fef9d 420 * Rewrite various system urls to https
6a488035
TO
421 *
422 * @return void
e7483cbe 423 * access public
6a488035 424 */
00be9182 425 public function mapConfigToSSL() {
6a488035 426 // dont need to do anything, let CMS handle their own switch to SSL
6a488035
TO
427 }
428
429 /**
100fef9d 430 * Figure out the post url for the form
6a488035 431 *
77855840
TO
432 * @param $action
433 * The default action if one is pre-specified.
6a488035 434 *
a6c01b45
CW
435 * @return string
436 * the url to post the form
6a488035 437 */
00be9182 438 public function postURL($action) {
6a488035
TO
439 if (!empty($action)) {
440 return $action;
441 }
442
443 return $this->url(CRM_Utils_Array::value('task', $_GET),
444 NULL, TRUE, NULL, FALSE
445 );
446 }
447
448 /**
100fef9d 449 * Set the email address of the user
6a488035 450 *
77855840
TO
451 * @param object $user
452 * Handle to the user object.
6a488035
TO
453 *
454 * @return void
6a488035 455 */
00be9182 456 public function setEmail(&$user) {
6a488035
TO
457 global $database;
458 $query = "SELECT email FROM #__users WHERE id='$user->id'";
459 $database->setQuery($query);
460 $user->email = $database->loadResult();
461 }
462
463 /**
464 * Authenticate the user against the joomla db
465 *
77855840
TO
466 * @param string $name
467 * The user name.
468 * @param string $password
469 * The password for the above user name.
5a4f6742
CW
470 * @param bool $loadCMSBootstrap
471 * load cms bootstrap?.
6a488035 472 *
72b3a70c
CW
473 * @return array|bool
474 * [contactID, ufID, uniqueString] if success else false if no auth
6a488035 475 */
00be9182 476 public function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
6a488035
TO
477 require_once 'DB.php';
478
479 $config = CRM_Core_Config::singleton();
ebc28bab 480 $user = NULL;
6a488035
TO
481
482 if ($loadCMSBootstrap) {
483 $bootStrapParams = array();
484 if ($name && $password) {
485 $bootStrapParams = array(
486 'name' => $name,
487 'pass' => $password,
488 );
489 }
bec3fc7c 490 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
6a488035
TO
491 }
492
493 jimport('joomla.application.component.helper');
494 jimport('joomla.database.table');
c1f3c6da
BS
495 jimport('joomla.user.helper');
496
497 $JUserTable = JTable::getInstance('User', 'JTable');
498
499 $db = $JUserTable->getDbo();
500 $query = $db->getQuery(TRUE);
501 $query->select('id, name, username, email, password');
502 $query->from($JUserTable->getTableName());
503 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
504 $db->setQuery($query, 0, 0);
505 $users = $db->loadObjectList();
506
507 $row = array();
508 if (count($users)) {
509 $row = $users[0];
510 }
6a488035 511
e46506b2 512 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
481a74f4 513 if (!defined('JVERSION')) {
ebc28bab 514 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 515 $jversion = new JVersion();
ebc28bab
BS
516 define('JVERSION', $jversion->getShortVersion());
517 }
6a488035 518
c1f3c6da
BS
519 if (!empty($row)) {
520 $dbPassword = $row->password;
521 $dbId = $row->id;
522 $dbEmail = $row->email;
6a488035 523
481a74f4
TO
524 if (version_compare(JVERSION, '2.5.18', 'lt') ||
525 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
c1f3c6da 526 ) {
ebc28bab
BS
527 // now check password
528 if (strpos($dbPassword, ':') === FALSE) {
529 if ($dbPassword != md5($password)) {
530 return FALSE;
531 }
532 }
533 else {
534 list($hash, $salt) = explode(':', $dbPassword);
535 $cryptpass = md5($password . $salt);
536 if ($hash != $cryptpass) {
537 return FALSE;
538 }
6a488035
TO
539 }
540 }
c1f3c6da 541 else {
4f99ca55
TO
542 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
543 return FALSE;
e7292422 544 }
9d735153
BS
545
546 //include additional files required by Joomla 3.2.1+
481a74f4 547 if (version_compare(JVERSION, '3.2.1', 'ge')) {
90eac10a
BS
548 require_once $joomlaBase . '/libraries/cms/application/helper.php';
549 require_once $joomlaBase . '/libraries/cms/application/cms.php';
550 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
9d735153 551 }
6a488035
TO
552 }
553
c1f3c6da 554 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
6a488035
TO
555 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
556 if (!$contactID) {
557 return FALSE;
558 }
559 return array($contactID, $dbId, mt_rand());
560 }
c1f3c6da 561
6a488035
TO
562 return FALSE;
563 }
564
bec3fc7c
BS
565 /**
566 * Set a init session with user object
567 *
77855840
TO
568 * @param array $data
569 * Array with user specific data.
bec3fc7c 570 */
00be9182 571 public function setUserSession($data) {
bec3fc7c 572 list($userID, $ufID) = $data;
481a74f4 573 $user = new JUser($ufID);
2d8f9c75 574 $session = JFactory::getSession();
bec3fc7c
BS
575 $session->set('user', $user);
576
cb0e36de 577 parent::setUserSession($data);
bec3fc7c
BS
578 }
579
6a488035
TO
580 /**
581 * Set a message in the UF to display to a user
582 *
77855840
TO
583 * @param string $message
584 * The message to set.
6a488035 585 */
00be9182 586 public function setMessage($message) {
6a488035
TO
587 }
588
bb3a214a
EM
589 /**
590 * @param $user
591 *
592 * @return bool
593 */
00be9182 594 public function loadUser($user) {
6a488035
TO
595 return TRUE;
596 }
597
00be9182 598 public function permissionDenied() {
0499b0ad 599 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
600 }
601
00be9182 602 public function logout() {
6a488035
TO
603 session_destroy();
604 header("Location:index.php");
605 }
606
607 /**
608 * Get the locale set in the hosting CMS
609 *
a6c01b45
CW
610 * @return string
611 * the used locale or null for none
6a488035 612 */
00be9182 613 public function getUFLocale() {
6a488035
TO
614 if (defined('_JEXEC')) {
615 $conf = JFactory::getConfig();
4965d8e9 616 $locale = $conf->get('language');
6a488035
TO
617 return str_replace('-', '_', $locale);
618 }
619 return NULL;
620 }
621
bb3a214a
EM
622 /**
623 * @return string
624 */
00be9182 625 public function getVersion() {
6a488035 626 if (class_exists('JVersion')) {
e7483cbe 627 $version = new JVersion();
6a488035
TO
628 return $version->getShortVersion();
629 }
630 else {
631 return 'Unknown';
632 }
633 }
634
f4aaa82a 635 /**
100fef9d 636 * Load joomla bootstrap
6a488035 637 *
5a4f6742
CW
638 * @param array $params
639 * with uid or name and password.
640 * @param bool $loadUser
641 * load cms user?.
f4aaa82a
EM
642 * @param bool|\throw $throwError throw error on failure?
643 * @param null $realPath
644 * @param bool $loadDefines
645 *
646 * @return bool
6a488035 647 */
00be9182 648 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
6a488035
TO
649 // Setup the base path related constant.
650 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
651
652 // load BootStrap here if needed
653 // We are a valid Joomla entry point.
353ffa53 654 if (!defined('_JEXEC') && $loadDefines) {
6a488035
TO
655 define('_JEXEC', 1);
656 define('DS', DIRECTORY_SEPARATOR);
657 define('JPATH_BASE', $joomlaBase . '/administrator');
658 require $joomlaBase . '/administrator/includes/defines.php';
659 }
660
661 // Get the framework.
2cb7adde 662 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
2efcf0c2 663 require $joomlaBase . '/libraries/import.legacy.php';
2cb7adde 664 }
6a488035
TO
665 require $joomlaBase . '/libraries/import.php';
666 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
6a488035
TO
667 require $joomlaBase . '/configuration.php';
668
6fde79f5 669 // Files may be in different places depending on Joomla version
481a74f4 670 if (!defined('JVERSION')) {
6fde79f5 671 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 672 $jversion = new JVersion();
6fde79f5
BS
673 define('JVERSION', $jversion->getShortVersion());
674 }
675
481a74f4 676 if (version_compare(JVERSION, '3.0', 'lt')) {
6fde79f5
BS
677 require $joomlaBase . '/libraries/joomla/environment/uri.php';
678 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
679 }
680 else {
87cdafb0 681 require $joomlaBase . '/libraries/cms.php';
6fde79f5 682 require $joomlaBase . '/libraries/joomla/uri/uri.php';
6fde79f5
BS
683 }
684
6a488035 685 jimport('joomla.application.cli');
f4aaa82a 686
182f835d 687 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
688 $config = CRM_Core_Config::singleton();
689 CRM_Utils_Hook::config($config);
6a488035
TO
690
691 return TRUE;
692 }
693
694 /**
100fef9d 695 * Check is user logged in.
6a488035 696 *
e7483cbe 697 * @return bool
6a488035
TO
698 */
699 public function isUserLoggedIn() {
700 $user = JFactory::getUser();
701 return ($user->guest) ? FALSE : TRUE;
702 }
703
704 /**
705 * Get currently logged in user uf id.
706 *
a6c01b45
CW
707 * @return int
708 * logged in user uf id.
6a488035
TO
709 */
710 public function getLoggedInUfID() {
711 $user = JFactory::getUser();
712 return ($user->guest) ? NULL : $user->id;
713 }
714
2b617cb0
EM
715 /**
716 * Get currently logged in user unique identifier - this tends to be the email address or user name.
717 *
a6c01b45
CW
718 * @return string
719 * logged in user unique identifier
2b617cb0 720 */
00be9182 721 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
722 $user = JFactory::getUser();
723 return $this->getUniqueIdentifierFromUserObject($user);
724 }
353ffa53 725
32998c82
EM
726 /**
727 * Get User ID from UserFramework system (Joomla)
77855840
TO
728 * @param object $user
729 * Object as described by the CMS.
72b3a70c
CW
730 * @return mixed
731 * <NULL, number>
32998c82 732 */
00be9182 733 public function getUserIDFromUserObject($user) {
32998c82
EM
734 return !empty($user->id) ? $user->id : NULL;
735 }
736
2b617cb0
EM
737 /**
738 * Get Unique Identifier from UserFramework system (CMS)
77855840
TO
739 * @param object $user
740 * Object as described by the User Framework.
72b3a70c
CW
741 * @return mixed
742 * $uniqueIdentifer Unique identifier from the user Framework system
2b617cb0 743 */
00be9182 744 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
745 return ($user->guest) ? NULL : $user->email;
746 }
747
6a488035
TO
748 /**
749 * Get a list of all installed modules, including enabled and disabled ones
750 *
a6c01b45
CW
751 * @return array
752 * CRM_Core_Module
6a488035 753 */
00be9182 754 public function getModules() {
6a488035
TO
755 $result = array();
756
757 $db = JFactory::getDbo();
e7292422 758 $query = $db->getQuery(TRUE);
6a488035
TO
759 $query->select('type, folder, element, enabled')
760 ->from('#__extensions')
761 ->where('type =' . $db->Quote('plugin'));
762 $plugins = $db->setQuery($query)->loadAssocList();
763 foreach ($plugins as $plugin) {
764 // question: is the folder really a critical part of the plugin's name?
765 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
766 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
767 }
768
769 return $result;
770 }
771
772 /**
773 * Get user login URL for hosting CMS (method declared in each CMS system class)
774 *
77855840
TO
775 * @param string $destination
776 * If present, add destination to querystring (works for Drupal only).
6a488035 777 *
a6c01b45
CW
778 * @return string
779 * loginURL for the current CMS
6a488035
TO
780 */
781 public function getLoginURL($destination = '') {
782 $config = CRM_Core_Config::singleton();
783 $loginURL = $config->userFrameworkBaseURL;
784 $loginURL = str_replace('administrator/', '', $loginURL);
785 $loginURL .= 'index.php?option=com_users&view=login';
091412ab
BS
786
787 //CRM-14872 append destination
481a74f4 788 if (!empty($destination)) {
92fcb95f 789 $loginURL .= '&return=' . urlencode(base64_encode($destination));
091412ab 790 }
6a488035
TO
791 return $loginURL;
792 }
f813f78e 793
bb3a214a 794 /**
c490a46a 795 * @param CRM_Core_Form $form
bb3a214a 796 */
6a488035 797 public function getLoginDestination(&$form) {
091412ab
BS
798 $args = NULL;
799
800 $id = $form->get('id');
801 if ($id) {
802 $args .= "&id=$id";
803 }
804 else {
805 $gid = $form->get('gid');
806 if ($gid) {
807 $args .= "&gid=$gid";
808 }
809 else {
810 // Setup Personal Campaign Page link uses pageId
811 $pageId = $form->get('pageId');
812 if ($pageId) {
813 $component = $form->get('component');
814 $args .= "&pageId=$pageId&component=$component&action=add";
815 }
816 }
817 }
818
819 $destination = NULL;
820 if ($args) {
821 // append destination so user is returned to form they came from after login
92fcb95f 822 $args = 'reset=1' . $args;
091412ab
BS
823 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, TRUE, TRUE);
824 }
825
826 return $destination;
6a488035 827 }
9977c6f5 828
829 /**
830 * Return default Site Settings
f4aaa82a
EM
831 *
832 * @param $dir
833 *
a6c01b45
CW
834 * @return array
835 * array
e7483cbe
J
836 * - $url, (Joomla - non admin url)
837 * - $siteName,
838 * - $siteRoot
9977c6f5 839 */
9b873358 840 public function getDefaultSiteSettings($dir) {
9977c6f5 841 $config = CRM_Core_Config::singleton();
842 $url = preg_replace(
843 '|/administrator|',
844 '',
845 $config->userFrameworkBaseURL
846 );
847 $siteRoot = preg_replace(
848 '|/media/civicrm/.*$|',
849 '',
850 $config->imageUploadDir
851 );
852 return array($url, NULL, $siteRoot);
853 }
59f97da6
EM
854
855 /**
856 * Get Url to view user record
77855840
TO
857 * @param int $contactID
858 * Contact ID.
59f97da6
EM
859 *
860 * @return string
861 */
00be9182 862 public function getUserRecordUrl($contactID) {
59f97da6
EM
863 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
864 $userRecordUrl = NULL;
865 // if logged in user is super user, then he can view other users joomla profile
866 if (JFactory::getUser()->authorise('core.admin')) {
867 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
868 }
869 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
870 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
871 }
872 }
873
874 /**
875 * Is the current user permitted to add a user
876 * @return bool
877 */
00be9182 878 public function checkPermissionAddUser() {
59f97da6
EM
879 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
880 return TRUE;
881 }
882 }
f85b1d20
EM
883
884 /**
100fef9d 885 * Output code from error function
f85b1d20
EM
886 * @param string $content
887 */
00be9182 888 public function outputError($content) {
f85b1d20
EM
889 if (class_exists('JErrorPage')) {
890 $error = new Exception($content);
891 JErrorPage::render($error);
892 }
4c9b6178 893 elseif (class_exists('JError')) {
f85b1d20
EM
894 JError::raiseError('CiviCRM-001', $content);
895 }
896 else {
897 parent::outputError($content);
898 }
899 }
e7292422 900
f58e4c2e
DC
901 /**
902 * Append to coreResourcesList
903 */
00be9182 904 public function appendCoreResources(&$list) {
f58e4c2e
DC
905 $list[] = 'js/crm.joomla.js';
906 }
96025800 907
6a488035 908}