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