de314ec906fc1d13e0bb6aaa2bd6b46d2d52b0bc
[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(
361 $path = NULL, $query = NULL, $absolute = TRUE,
362 $fragment = NULL, $htmlize = TRUE,
363 $frontend = FALSE, $forceBackend = FALSE
364 ) {
365 $config = CRM_Core_Config::singleton();
366 $separator = $htmlize ? '&amp;' : '&';
367 $Itemid = '';
368 $script = '';
369 $path = CRM_Utils_String::stripPathChars($path);
370
371 if ($config->userFrameworkFrontend) {
372 $script = 'index.php';
373 if (JRequest::getVar("Itemid")) {
374 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
375 }
376 }
377
378 if (isset($fragment)) {
379 $fragment = '#' . $fragment;
380 }
381
382 if (!isset($config->useFrameworkRelativeBase)) {
383 $base = parse_url($config->userFrameworkBaseURL);
384 $config->useFrameworkRelativeBase = $base['path'];
385 }
386 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
387
388 if (!empty($query)) {
389 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
390 }
391 else {
392 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
393 }
394
395 // gross hack for joomla, we are in the backend and want to send a frontend url
396 if ($frontend && $config->userFramework == 'Joomla') {
397 // handle both joomla v1.5 and v1.6, CRM-7939
398 $url = str_replace('/administrator/index2.php', '/index.php', $url);
399 $url = str_replace('/administrator/index.php', '/index.php', $url);
400
401 // CRM-8215
402 $url = str_replace('/administrator/', '/index.php', $url);
403 }
404 elseif ($forceBackend) {
405 if (defined('JVERSION')) {
406 $joomlaVersion = JVERSION;
407 } else {
408 $jversion = new JVersion;
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 /**
420 * Rewrite various system urls to https
421 *
422 * @return void
423 * access public
424 */
425 public function mapConfigToSSL() {
426 // dont need to do anything, let CMS handle their own switch to SSL
427 return;
428 }
429
430 /**
431 * Figure out the post url for the form
432 *
433 * @param $action
434 * The default action if one is pre-specified.
435 *
436 * @return string the url to post the form
437 */
438 public function postURL($action) {
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 /**
449 * Set the email address of the user
450 *
451 * @param object $user
452 * Handle to the user object.
453 *
454 * @return void
455 */
456 public function setEmail(&$user) {
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 *
466 * @param string $name
467 * The user name.
468 * @param string $password
469 * The password for the above user name.
470 * @param $loadCMSBootstrap
471 * Boolean load cms bootstrap?.
472 *
473 * @return mixed false if no auth
474 * array(
475 contactID, ufID, unique string ) if success
476 */
477 public function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
478 require_once 'DB.php';
479
480 $config = CRM_Core_Config::singleton();
481 $user = NULL;
482
483 if ($loadCMSBootstrap) {
484 $bootStrapParams = array();
485 if ($name && $password) {
486 $bootStrapParams = array(
487 'name' => $name,
488 'pass' => $password,
489 );
490 }
491 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
492 }
493
494 jimport('joomla.application.component.helper');
495 jimport('joomla.database.table');
496 jimport('joomla.user.helper');
497
498 $JUserTable = JTable::getInstance('User', 'JTable');
499
500 $db = $JUserTable->getDbo();
501 $query = $db->getQuery(TRUE);
502 $query->select('id, name, username, email, password');
503 $query->from($JUserTable->getTableName());
504 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
505 $db->setQuery($query, 0, 0);
506 $users = $db->loadObjectList();
507
508 $row = array();
509 if (count($users)) {
510 $row = $users[0];
511 }
512
513 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
514 if ( !defined('JVERSION') ) {
515 require $joomlaBase . '/libraries/cms/version/version.php';
516 $jversion = new JVersion;
517 define('JVERSION', $jversion->getShortVersion());
518 }
519
520 if (!empty($row)) {
521 $dbPassword = $row->password;
522 $dbId = $row->id;
523 $dbEmail = $row->email;
524
525 if ( version_compare(JVERSION, '2.5.18', 'lt') ||
526 ( version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt') )
527 ) {
528 // now check password
529 if (strpos($dbPassword, ':') === FALSE) {
530 if ($dbPassword != md5($password)) {
531 return FALSE;
532 }
533 }
534 else {
535 list($hash, $salt) = explode(':', $dbPassword);
536 $cryptpass = md5($password . $salt);
537 if ($hash != $cryptpass) {
538 return FALSE;
539 }
540 }
541 }
542 else {
543 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) { return FALSE;
544 }
545
546 //include additional files required by Joomla 3.2.1+
547 if ( version_compare(JVERSION, '3.2.1', 'ge') ) {
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';
551 }
552 }
553
554 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
555 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
556 if (!$contactID) {
557 return FALSE;
558 }
559 return array($contactID, $dbId, mt_rand());
560 }
561
562 return FALSE;
563 }
564
565 /**
566 * Set a init session with user object
567 *
568 * @param array $data
569 * Array with user specific data.
570 *
571 */
572 public function setUserSession($data) {
573 list($userID, $ufID) = $data;
574 $user = new JUser( $ufID );
575 $session = JFactory::getSession();
576 $session->set('user', $user);
577
578 parent::setUserSession($data);
579 }
580
581 /**
582 * Set a message in the UF to display to a user
583 *
584 * @param string $message
585 * The message to set.
586 *
587 */
588 public function setMessage($message) {
589 return;
590 }
591
592 /**
593 * @param $user
594 *
595 * @return bool
596 */
597 public function loadUser($user) {
598 return TRUE;
599 }
600
601 public function permissionDenied() {
602 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
603 }
604
605 public function logout() {
606 session_destroy();
607 header("Location:index.php");
608 }
609
610 /**
611 * Get the locale set in the hosting CMS
612 *
613 * @return string the used locale or null for none
614 */
615 public function getUFLocale() {
616 if (defined('_JEXEC')) {
617 $conf = JFactory::getConfig();
618 $locale = $conf->get('language');
619 return str_replace('-', '_', $locale);
620 }
621 return NULL;
622 }
623
624 /**
625 * @return string
626 */
627 public function getVersion() {
628 if (class_exists('JVersion')) {
629 $version = new JVersion;
630 return $version->getShortVersion();
631 }
632 else {
633 return 'Unknown';
634 }
635 }
636
637 /**
638 * Load joomla bootstrap
639 *
640 * @param $params
641 * Array with uid or name and password.
642 * @param $loadUser
643 * Boolean load cms user?.
644 * @param bool|\throw $throwError throw error on failure?
645 * @param null $realPath
646 * @param bool $loadDefines
647 *
648 * @return bool
649 */
650 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
651 // Setup the base path related constant.
652 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
653
654 // load BootStrap here if needed
655 // We are a valid Joomla entry point.
656 if ( ! defined( '_JEXEC' ) && $loadDefines ) {
657 define('_JEXEC', 1);
658 define('DS', DIRECTORY_SEPARATOR);
659 define('JPATH_BASE', $joomlaBase . '/administrator');
660 require $joomlaBase . '/administrator/includes/defines.php';
661 }
662
663 // Get the framework.
664 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
665 require $joomlaBase . '/libraries/import.legacy.php';
666 }
667 require $joomlaBase . '/libraries/import.php';
668 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
669 require $joomlaBase . '/configuration.php';
670
671 // Files may be in different places depending on Joomla version
672 if ( !defined('JVERSION') ) {
673 require $joomlaBase . '/libraries/cms/version/version.php';
674 $jversion = new JVersion;
675 define('JVERSION', $jversion->getShortVersion());
676 }
677
678 if( version_compare(JVERSION, '3.0', 'lt') ) {
679 require $joomlaBase . '/libraries/joomla/environment/uri.php';
680 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
681 }
682 else {
683 require $joomlaBase . '/libraries/cms.php';
684 require $joomlaBase . '/libraries/joomla/uri/uri.php';
685 }
686
687 jimport('joomla.application.cli');
688
689 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
690 $config = CRM_Core_Config::singleton();
691 CRM_Utils_Hook::config($config);
692
693 return TRUE;
694 }
695
696 /**
697 * Check is user logged in.
698 *
699 * @return boolean true/false.
700 */
701 public function isUserLoggedIn() {
702 $user = JFactory::getUser();
703 return ($user->guest) ? FALSE : TRUE;
704 }
705
706 /**
707 * Get currently logged in user uf id.
708 *
709 * @return int logged in user uf id.
710 */
711 public function getLoggedInUfID() {
712 $user = JFactory::getUser();
713 return ($user->guest) ? NULL : $user->id;
714 }
715
716 /**
717 * Get currently logged in user unique identifier - this tends to be the email address or user name.
718 *
719 * @return string $userID logged in user unique identifier
720 */
721 public function getLoggedInUniqueIdentifier() {
722 $user = JFactory::getUser();
723 return $this->getUniqueIdentifierFromUserObject($user);
724 }
725 /**
726 * Get User ID from UserFramework system (Joomla)
727 * @param object $user
728 * Object as described by the CMS.
729 * @return mixed <NULL, number>
730 */
731 public function getUserIDFromUserObject($user) {
732 return !empty($user->id) ? $user->id : NULL;
733 }
734
735 /**
736 * Get Unique Identifier from UserFramework system (CMS)
737 * @param object $user
738 * Object as described by the User Framework.
739 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
740 *
741 */
742 public function getUniqueIdentifierFromUserObject($user) {
743 return ($user->guest) ? NULL : $user->email;
744 }
745
746 /**
747 * Get a list of all installed modules, including enabled and disabled ones
748 *
749 * @return array CRM_Core_Module
750 */
751 public function getModules() {
752 $result = array();
753
754 $db = JFactory::getDbo();
755 $query = $db->getQuery(TRUE);
756 $query->select('type, folder, element, enabled')
757 ->from('#__extensions')
758 ->where('type =' . $db->Quote('plugin'));
759 $plugins = $db->setQuery($query)->loadAssocList();
760 foreach ($plugins as $plugin) {
761 // question: is the folder really a critical part of the plugin's name?
762 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
763 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
764 }
765
766 return $result;
767 }
768
769 /**
770 * Get user login URL for hosting CMS (method declared in each CMS system class)
771 *
772 * @param string $destination
773 * If present, add destination to querystring (works for Drupal only).
774 *
775 * @return string - loginURL for the current CMS
776 * @static
777 */
778 public function getLoginURL($destination = '') {
779 $config = CRM_Core_Config::singleton();
780 $loginURL = $config->userFrameworkBaseURL;
781 $loginURL = str_replace('administrator/', '', $loginURL);
782 $loginURL .= 'index.php?option=com_users&view=login';
783
784 //CRM-14872 append destination
785 if ( !empty($destination) ) {
786 $loginURL .= '&return='.urlencode(base64_encode($destination));
787 }
788 return $loginURL;
789 }
790
791 /**
792 * @param CRM_Core_Form $form
793 */
794 public function getLoginDestination(&$form) {
795 $args = NULL;
796
797 $id = $form->get('id');
798 if ($id) {
799 $args .= "&id=$id";
800 }
801 else {
802 $gid = $form->get('gid');
803 if ($gid) {
804 $args .= "&gid=$gid";
805 }
806 else {
807 // Setup Personal Campaign Page link uses pageId
808 $pageId = $form->get('pageId');
809 if ($pageId) {
810 $component = $form->get('component');
811 $args .= "&pageId=$pageId&component=$component&action=add";
812 }
813 }
814 }
815
816 $destination = NULL;
817 if ($args) {
818 // append destination so user is returned to form they came from after login
819 $args = 'reset=1'.$args;
820 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, TRUE, TRUE);
821 }
822
823 return $destination;
824 }
825
826 /**
827 * Return default Site Settings
828 *
829 * @param $dir
830 *
831 * @return array array
832 * - $url, (Joomla - non admin url)
833 * - $siteName,
834 * - $siteRoot
835 */
836 public function getDefaultSiteSettings($dir){
837 $config = CRM_Core_Config::singleton();
838 $url = preg_replace(
839 '|/administrator|',
840 '',
841 $config->userFrameworkBaseURL
842 );
843 $siteRoot = preg_replace(
844 '|/media/civicrm/.*$|',
845 '',
846 $config->imageUploadDir
847 );
848 return array($url, NULL, $siteRoot);
849 }
850
851 /**
852 * Get Url to view user record
853 * @param int $contactID
854 * Contact ID.
855 *
856 * @return string
857 */
858 public function getUserRecordUrl($contactID) {
859 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
860 $userRecordUrl = NULL;
861 // if logged in user is super user, then he can view other users joomla profile
862 if (JFactory::getUser()->authorise('core.admin')) {
863 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
864 }
865 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
866 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
867 }
868 }
869
870 /**
871 * Is the current user permitted to add a user
872 * @return bool
873 */
874 public function checkPermissionAddUser() {
875 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
876 return TRUE;
877 }
878 }
879
880 /**
881 * Output code from error function
882 * @param string $content
883 */
884 public function outputError($content) {
885 if (class_exists('JErrorPage')) {
886 $error = new Exception($content);
887 JErrorPage::render($error);
888 }
889 else if (class_exists('JError')) {
890 JError::raiseError('CiviCRM-001', $content);
891 }
892 else {
893 parent::outputError($content);
894 }
895 }
896
897 /**
898 * Append to coreResourcesList
899 */
900 public function appendCoreResources(&$list) {
901 $list[] = 'js/crm.joomla.js';
902 }
903 }