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