0fb75eaab3dcec5a0b72f5a3cd124b88cdc16869
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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
57 * Associated array.
58 * @param string $mail
59 * Email id for cms user.
60 *
61 * @return uid if user exists, false otherwise
62 *
63 */
64 public function createUser(&$params, $mail) {
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
104 /**
105 * Change user name in host CMS
106 *
107 * @param int $ufID
108 * @param string $ufName User name
109 */
110 public function updateCMSName($ufID, $ufName) {
111 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
112 $ufName = CRM_Utils_Type::escape($ufName, 'String');
113
114 $values = array();
115 $user = JUser::getInstance($ufID);
116
117 $values['email'] = $ufName;
118 $user->bind($values);
119
120 $user->save();
121 }
122
123 /**
124 * Check if username and email exists in the drupal db
125 *
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'.
132 *
133 * @return void
134 */
135 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
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
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 /**
181 * Sets the title of the page
182 *
183 * @param string $title
184 * Title to set.
185 * @param string $pageTitle
186 *
187 * @return void
188 */
189 public function setTitle($title, $pageTitle = NULL) {
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 *
206 * @param $breadCrumbs
207 *
208 * @internal param string $title
209 * @internal param string $url
210 *
211 * @return void
212 */
213 public function appendBreadCrumb($breadCrumbs) {
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 *
240 * @internal param string $bc the new breadcrumb to be appended
241 *
242 * @return void
243 */
244 public function resetBreadCrumb() {
245 return;
246 }
247
248 /**
249 * Append a string to the head of the html file
250 *
251 * @param null $string
252 *
253 * @internal param string $head the new string to be appended
254 *
255 * @return void
256 */
257 public 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
269 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
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
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
285 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
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
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
301 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
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
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
322 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
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
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
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:).
347 * Useful for links that will be displayed outside the site, such as in an
348 * RSS feed.
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.
355 *
356 * @param bool $forceBackend
357 *
358 * @return string an HTML string containing a link to the given path.
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 /**
419 * Rewrite various system urls to https
420 *
421 * @return void
422 * access public
423 */
424 public function mapConfigToSSL() {
425 // dont need to do anything, let CMS handle their own switch to SSL
426 return;
427 }
428
429 /**
430 * Figure out the post url for the form
431 *
432 * @param $action
433 * The default action if one is pre-specified.
434 *
435 * @return string the url to post the form
436 */
437 public function postURL($action) {
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 /**
448 * Set the email address of the user
449 *
450 * @param object $user
451 * Handle to the user object.
452 *
453 * @return void
454 */
455 public function setEmail(&$user) {
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 *
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?.
471 *
472 * @return mixed false if no auth
473 * array(
474 contactID, ufID, unique string ) if success
475 */
476 public function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
477 require_once 'DB.php';
478
479 $config = CRM_Core_Config::singleton();
480 $user = NULL;
481
482 if ($loadCMSBootstrap) {
483 $bootStrapParams = array();
484 if ($name && $password) {
485 $bootStrapParams = array(
486 'name' => $name,
487 'pass' => $password,
488 );
489 }
490 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
491 }
492
493 jimport('joomla.application.component.helper');
494 jimport('joomla.database.table');
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 }
511
512 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
513 if ( !defined('JVERSION') ) {
514 require $joomlaBase . '/libraries/cms/version/version.php';
515 $jversion = new JVersion;
516 define('JVERSION', $jversion->getShortVersion());
517 }
518
519 if (!empty($row)) {
520 $dbPassword = $row->password;
521 $dbId = $row->id;
522 $dbEmail = $row->email;
523
524 if ( version_compare(JVERSION, '2.5.18', 'lt') ||
525 ( version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt') )
526 ) {
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 }
539 }
540 }
541 else {
542 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) { return FALSE;
543 }
544
545 //include additional files required by Joomla 3.2.1+
546 if ( version_compare(JVERSION, '3.2.1', 'ge') ) {
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';
550 }
551 }
552
553 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
554 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
555 if (!$contactID) {
556 return FALSE;
557 }
558 return array($contactID, $dbId, mt_rand());
559 }
560
561 return FALSE;
562 }
563
564 /**
565 * Set a init session with user object
566 *
567 * @param array $data
568 * Array with user specific data.
569 *
570 */
571 public function setUserSession($data) {
572 list($userID, $ufID) = $data;
573 $user = new JUser( $ufID );
574 $session = JFactory::getSession();
575 $session->set('user', $user);
576
577 parent::setUserSession($data);
578 }
579
580 /**
581 * Set a message in the UF to display to a user
582 *
583 * @param string $message
584 * The message to set.
585 *
586 */
587 public function setMessage($message) {
588 return;
589 }
590
591 /**
592 * @param $user
593 *
594 * @return bool
595 */
596 public function loadUser($user) {
597 return TRUE;
598 }
599
600 public function permissionDenied() {
601 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
602 }
603
604 public function logout() {
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 */
614 public function getUFLocale() {
615 if (defined('_JEXEC')) {
616 $conf = JFactory::getConfig();
617 $locale = $conf->get('language');
618 return str_replace('-', '_', $locale);
619 }
620 return NULL;
621 }
622
623 /**
624 * @return string
625 */
626 public function getVersion() {
627 if (class_exists('JVersion')) {
628 $version = new JVersion;
629 return $version->getShortVersion();
630 }
631 else {
632 return 'Unknown';
633 }
634 }
635
636 /**
637 * Load joomla bootstrap
638 *
639 * @param $params
640 * Array with uid or name and password.
641 * @param $loadUser
642 * Boolean load cms user?.
643 * @param bool|\throw $throwError throw error on failure?
644 * @param null $realPath
645 * @param bool $loadDefines
646 *
647 * @return bool
648 */
649 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
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.
655 if ( ! defined( '_JEXEC' ) && $loadDefines ) {
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.
663 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
664 require $joomlaBase . '/libraries/import.legacy.php';
665 }
666 require $joomlaBase . '/libraries/import.php';
667 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
668 require $joomlaBase . '/configuration.php';
669
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 {
682 require $joomlaBase . '/libraries/cms.php';
683 require $joomlaBase . '/libraries/joomla/uri/uri.php';
684 }
685
686 jimport('joomla.application.cli');
687
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);
691
692 return TRUE;
693 }
694
695 /**
696 * Check is user logged in.
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
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 */
720 public function getLoggedInUniqueIdentifier() {
721 $user = JFactory::getUser();
722 return $this->getUniqueIdentifierFromUserObject($user);
723 }
724 /**
725 * Get User ID from UserFramework system (Joomla)
726 * @param object $user
727 * Object as described by the CMS.
728 * @return mixed <NULL, number>
729 */
730 public function getUserIDFromUserObject($user) {
731 return !empty($user->id) ? $user->id : NULL;
732 }
733
734 /**
735 * Get Unique Identifier from UserFramework system (CMS)
736 * @param object $user
737 * Object as described by the User Framework.
738 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
739 *
740 */
741 public function getUniqueIdentifierFromUserObject($user) {
742 return ($user->guest) ? NULL : $user->email;
743 }
744
745 /**
746 * Get a list of all installed modules, including enabled and disabled ones
747 *
748 * @return array CRM_Core_Module
749 */
750 public function getModules() {
751 $result = array();
752
753 $db = JFactory::getDbo();
754 $query = $db->getQuery(TRUE);
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 *
771 * @param string $destination
772 * If present, add destination to querystring (works for Drupal only).
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';
782
783 //CRM-14872 append destination
784 if ( !empty($destination) ) {
785 $loginURL .= '&return='.urlencode(base64_encode($destination));
786 }
787 return $loginURL;
788 }
789
790 /**
791 * @param CRM_Core_Form $form
792 */
793 public function getLoginDestination(&$form) {
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;
823 }
824
825 /**
826 * Return default Site Settings
827 *
828 * @param $dir
829 *
830 * @return array array
831 * - $url, (Joomla - non admin url)
832 * - $siteName,
833 * - $siteRoot
834 */
835 public function getDefaultSiteSettings($dir){
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 }
849
850 /**
851 * Get Url to view user record
852 * @param int $contactID
853 * Contact ID.
854 *
855 * @return string
856 */
857 public function getUserRecordUrl($contactID) {
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 */
873 public function checkPermissionAddUser() {
874 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
875 return TRUE;
876 }
877 }
878
879 /**
880 * Output code from error function
881 * @param string $content
882 */
883 public function outputError($content) {
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 }
895
896 /**
897 * Append to coreResourcesList
898 */
899 public function appendCoreResources(&$list) {
900 $list[] = 'js/crm.joomla.js';
901 }
902 }