Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
b8c71ffa | 35 | * Joomla specific stuff goes here. |
6a488035 TO |
36 | */ |
37 | class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { | |
bb3a214a | 38 | /** |
b8c71ffa | 39 | * Class constructor. |
bb3a214a | 40 | */ |
00be9182 | 41 | public function __construct() { |
4caaa696 EM |
42 | /** |
43 | * 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 | |
44 | * functions and leave the codebase oblivious to the type of CMS | |
45 | * @deprecated | |
46 | * @var bool | |
47 | */ | |
6a488035 TO |
48 | $this->is_drupal = FALSE; |
49 | } | |
50 | ||
51 | /** | |
17f443df | 52 | * @inheritDoc |
6a488035 | 53 | */ |
00be9182 | 54 | public function createUser(&$params, $mail) { |
6a488035 TO |
55 | $baseDir = JPATH_SITE; |
56 | require_once $baseDir . '/components/com_users/models/registration.php'; | |
57 | ||
58 | $userParams = JComponentHelper::getParams('com_users'); | |
353ffa53 TO |
59 | $model = new UsersModelRegistration(); |
60 | $ufID = NULL; | |
6a488035 TO |
61 | |
62 | // get the default usertype | |
63 | $userType = $userParams->get('new_usertype'); | |
64 | if (!$userType) { | |
65 | $userType = 2; | |
66 | } | |
67 | ||
68 | if (isset($params['name'])) { | |
69 | $fullname = trim($params['name']); | |
70 | } | |
71 | elseif (isset($params['contactID'])) { | |
72 | $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID'])); | |
73 | } | |
74 | else { | |
75 | $fullname = trim($params['cms_name']); | |
76 | } | |
77 | ||
78 | // Prepare the values for a new Joomla user. | |
353ffa53 TO |
79 | $values = array(); |
80 | $values['name'] = $fullname; | |
81 | $values['username'] = trim($params['cms_name']); | |
6a488035 | 82 | $values['password1'] = $values['password2'] = $params['cms_pass']; |
353ffa53 | 83 | $values['email1'] = $values['email2'] = trim($params[$mail]); |
6a488035 TO |
84 | |
85 | $lang = JFactory::getLanguage(); | |
86 | $lang->load('com_users', $baseDir); | |
87 | ||
88 | $register = $model->register($values); | |
89 | ||
90 | $ufID = JUserHelper::getUserId($values['username']); | |
91 | return $ufID; | |
92 | } | |
93 | ||
f4aaa82a | 94 | /** |
17f443df | 95 | * @inheritDoc |
6a488035 | 96 | */ |
00be9182 | 97 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
98 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); |
99 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); | |
100 | ||
101 | $values = array(); | |
e851ce06 | 102 | $user = JUser::getInstance($ufID); |
6a488035 TO |
103 | |
104 | $values['email'] = $ufName; | |
105 | $user->bind($values); | |
106 | ||
107 | $user->save(); | |
108 | } | |
109 | ||
110 | /** | |
94f9f81a | 111 | * Check if username and email exists in the Joomla db. |
6a488035 | 112 | * |
77855840 TO |
113 | * @param array $params |
114 | * Array of name and mail values. | |
115 | * @param array $errors | |
116 | * Array of errors. | |
117 | * @param string $emailName | |
118 | * Field label for the 'email'. | |
6a488035 | 119 | */ |
00be9182 | 120 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
121 | $config = CRM_Core_Config::singleton(); |
122 | ||
353ffa53 TO |
123 | $dao = new CRM_Core_DAO(); |
124 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
125 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
126 | //don't allow the special characters and min. username length is two | |
127 | //regex \\ to match a single backslash would become '/\\\\/' | |
128 | $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name); | |
129 | if ($isNotValid || strlen($name) < 2) { | |
130 | $errors['cms_name'] = ts('Your username contains invalid characters or is too short'); | |
131 | } | |
132 | ||
6a488035 TO |
133 | $JUserTable = &JTable::getInstance('User', 'JTable'); |
134 | ||
135 | $db = $JUserTable->getDbo(); | |
136 | $query = $db->getQuery(TRUE); | |
137 | $query->select('username, email'); | |
138 | $query->from($JUserTable->getTableName()); | |
139 | $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))'); | |
140 | $db->setQuery($query, 0, 10); | |
141 | $users = $db->loadAssocList(); | |
142 | ||
94f9f81a | 143 | $row = array(); |
6a488035 TO |
144 | if (count($users)) { |
145 | $row = $users[0]; | |
146 | } | |
147 | ||
148 | if (!empty($row)) { | |
149 | $dbName = CRM_Utils_Array::value('username', $row); | |
150 | $dbEmail = CRM_Utils_Array::value('email', $row); | |
151 | if (strtolower($dbName) == strtolower($name)) { | |
152 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', | |
153 | array(1 => $name) | |
154 | ); | |
155 | } | |
156 | if (strtolower($dbEmail) == strtolower($email)) { | |
157 | $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset'; | |
89374eb2 | 158 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
6a488035 TO |
159 | array(1 => $email, 2 => $resetUrl) |
160 | ); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
17f443df | 166 | * @inheritDoc |
6a488035 | 167 | */ |
00be9182 | 168 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
169 | if (!$pageTitle) { |
170 | $pageTitle = $title; | |
171 | } | |
172 | ||
173 | $template = CRM_Core_Smarty::singleton(); | |
174 | $template->assign('pageTitle', $pageTitle); | |
175 | ||
176 | $document = JFactory::getDocument(); | |
177 | $document->setTitle($title); | |
6a488035 TO |
178 | } |
179 | ||
180 | /** | |
17f443df | 181 | * @inheritDoc |
6a488035 | 182 | */ |
00be9182 | 183 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
184 | $template = CRM_Core_Smarty::singleton(); |
185 | $bc = $template->get_template_vars('breadcrumb'); | |
186 | ||
187 | if (is_array($breadCrumbs)) { | |
188 | foreach ($breadCrumbs as $crumbs) { | |
189 | if (stripos($crumbs['url'], 'id%%')) { | |
190 | $args = array('cid', 'mid'); | |
191 | foreach ($args as $a) { | |
192 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
193 | FALSE, NULL, $_GET | |
194 | ); | |
195 | if ($val) { | |
196 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
197 | } | |
198 | } | |
199 | } | |
200 | $bc[] = $crumbs; | |
201 | } | |
202 | } | |
203 | $template->assign_by_ref('breadcrumb', $bc); | |
6a488035 TO |
204 | } |
205 | ||
206 | /** | |
17f443df | 207 | * @inheritDoc |
6a488035 | 208 | */ |
00be9182 | 209 | public function resetBreadCrumb() { |
6a488035 TO |
210 | } |
211 | ||
212 | /** | |
17f443df | 213 | * @inheritDoc |
6a488035 | 214 | */ |
17f443df | 215 | public function addHTMLHead($string = NULL) { |
6a488035 TO |
216 | if ($string) { |
217 | $document = JFactory::getDocument(); | |
218 | $document->addCustomTag($string); | |
219 | } | |
220 | } | |
221 | ||
222 | /** | |
17f443df | 223 | * @inheritDoc |
6a488035 TO |
224 | */ |
225 | public function addStyleUrl($url, $region) { | |
226 | if ($region == 'html-header') { | |
227 | $document = JFactory::getDocument(); | |
228 | $document->addStyleSheet($url); | |
229 | return TRUE; | |
230 | } | |
231 | return FALSE; | |
232 | } | |
233 | ||
234 | /** | |
17f443df | 235 | * @inheritDoc |
6a488035 TO |
236 | */ |
237 | public function addStyle($code, $region) { | |
238 | if ($region == 'html-header') { | |
239 | $document = JFactory::getDocument(); | |
240 | $document->addStyleDeclaration($code); | |
241 | return TRUE; | |
242 | } | |
243 | return FALSE; | |
244 | } | |
245 | ||
246 | /** | |
17f443df | 247 | * @inheritDoc |
6a488035 | 248 | */ |
e7483cbe | 249 | public function url( |
17f443df CW |
250 | $path = NULL, |
251 | $query = NULL, | |
252 | $absolute = FALSE, | |
253 | $fragment = NULL, | |
254 | $htmlize = TRUE, | |
255 | $frontend = FALSE, | |
256 | $forceBackend = FALSE | |
6a488035 | 257 | ) { |
353ffa53 | 258 | $config = CRM_Core_Config::singleton(); |
6a488035 | 259 | $separator = $htmlize ? '&' : '&'; |
353ffa53 TO |
260 | $Itemid = ''; |
261 | $script = ''; | |
262 | $path = CRM_Utils_String::stripPathChars($path); | |
6a488035 TO |
263 | |
264 | if ($config->userFrameworkFrontend) { | |
265 | $script = 'index.php'; | |
266 | if (JRequest::getVar("Itemid")) { | |
267 | $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid"); | |
268 | } | |
269 | } | |
270 | ||
271 | if (isset($fragment)) { | |
272 | $fragment = '#' . $fragment; | |
273 | } | |
274 | ||
6a488035 TO |
275 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; |
276 | ||
277 | if (!empty($query)) { | |
278 | $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}"; | |
279 | } | |
280 | else { | |
281 | $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}"; | |
282 | } | |
283 | ||
284 | // gross hack for joomla, we are in the backend and want to send a frontend url | |
285 | if ($frontend && $config->userFramework == 'Joomla') { | |
286 | // handle both joomla v1.5 and v1.6, CRM-7939 | |
287 | $url = str_replace('/administrator/index2.php', '/index.php', $url); | |
288 | $url = str_replace('/administrator/index.php', '/index.php', $url); | |
289 | ||
290 | // CRM-8215 | |
291 | $url = str_replace('/administrator/', '/index.php', $url); | |
292 | } | |
293 | elseif ($forceBackend) { | |
294 | if (defined('JVERSION')) { | |
295 | $joomlaVersion = JVERSION; | |
0db6c3e1 TO |
296 | } |
297 | else { | |
e7483cbe | 298 | $jversion = new JVersion(); |
6a488035 TO |
299 | $joomlaVersion = $jversion->getShortVersion(); |
300 | } | |
301 | ||
302 | if (version_compare($joomlaVersion, '1.6') >= 0) { | |
303 | $url = str_replace('/index.php', '/administrator/index.php', $url); | |
304 | } | |
305 | } | |
306 | return $url; | |
307 | } | |
308 | ||
6a488035 | 309 | /** |
fe482240 | 310 | * Set the email address of the user. |
6a488035 | 311 | * |
77855840 TO |
312 | * @param object $user |
313 | * Handle to the user object. | |
6a488035 | 314 | */ |
00be9182 | 315 | public function setEmail(&$user) { |
6a488035 | 316 | global $database; |
94f9f81a EW |
317 | $query = $db->getQuery(TRUE); |
318 | $query->select($db->quoteName('email')) | |
319 | ->from($db->quoteName('#__users')) | |
320 | ->where($db->quoteName('id') . ' = ' . $user->id); | |
6a488035 TO |
321 | $database->setQuery($query); |
322 | $user->email = $database->loadResult(); | |
323 | } | |
324 | ||
325 | /** | |
17f443df | 326 | * @inheritDoc |
6a488035 | 327 | */ |
17f443df | 328 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
329 | require_once 'DB.php'; |
330 | ||
331 | $config = CRM_Core_Config::singleton(); | |
ebc28bab | 332 | $user = NULL; |
6a488035 TO |
333 | |
334 | if ($loadCMSBootstrap) { | |
335 | $bootStrapParams = array(); | |
336 | if ($name && $password) { | |
337 | $bootStrapParams = array( | |
338 | 'name' => $name, | |
339 | 'pass' => $password, | |
340 | ); | |
341 | } | |
bec3fc7c | 342 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE); |
6a488035 TO |
343 | } |
344 | ||
345 | jimport('joomla.application.component.helper'); | |
346 | jimport('joomla.database.table'); | |
c1f3c6da BS |
347 | jimport('joomla.user.helper'); |
348 | ||
349 | $JUserTable = JTable::getInstance('User', 'JTable'); | |
350 | ||
351 | $db = $JUserTable->getDbo(); | |
352 | $query = $db->getQuery(TRUE); | |
353 | $query->select('id, name, username, email, password'); | |
354 | $query->from($JUserTable->getTableName()); | |
355 | $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)'); | |
356 | $db->setQuery($query, 0, 0); | |
357 | $users = $db->loadObjectList(); | |
358 | ||
359 | $row = array(); | |
360 | if (count($users)) { | |
361 | $row = $users[0]; | |
362 | } | |
6a488035 | 363 | |
e46506b2 | 364 | $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))))); |
481a74f4 | 365 | if (!defined('JVERSION')) { |
ebc28bab | 366 | require $joomlaBase . '/libraries/cms/version/version.php'; |
e7483cbe | 367 | $jversion = new JVersion(); |
ebc28bab BS |
368 | define('JVERSION', $jversion->getShortVersion()); |
369 | } | |
6a488035 | 370 | |
c1f3c6da BS |
371 | if (!empty($row)) { |
372 | $dbPassword = $row->password; | |
373 | $dbId = $row->id; | |
374 | $dbEmail = $row->email; | |
6a488035 | 375 | |
481a74f4 TO |
376 | if (version_compare(JVERSION, '2.5.18', 'lt') || |
377 | (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt')) | |
c1f3c6da | 378 | ) { |
ebc28bab | 379 | // now check password |
009eff21 EW |
380 | list($hash, $salt) = explode(':', $dbPassword); |
381 | $cryptpass = md5($password . $salt); | |
382 | if ($hash != $cryptpass) { | |
01c77fa9 EW |
383 | return FALSE; |
384 | } | |
6a488035 | 385 | } |
c1f3c6da | 386 | else { |
4f99ca55 TO |
387 | if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) { |
388 | return FALSE; | |
e7292422 | 389 | } |
9d735153 BS |
390 | |
391 | //include additional files required by Joomla 3.2.1+ | |
481a74f4 | 392 | if (version_compare(JVERSION, '3.2.1', 'ge')) { |
90eac10a BS |
393 | require_once $joomlaBase . '/libraries/cms/application/helper.php'; |
394 | require_once $joomlaBase . '/libraries/cms/application/cms.php'; | |
395 | require_once $joomlaBase . '/libraries/cms/application/administrator.php'; | |
9d735153 | 396 | } |
6a488035 TO |
397 | } |
398 | ||
c1f3c6da | 399 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla'); |
6a488035 TO |
400 | $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId); |
401 | if (!$contactID) { | |
402 | return FALSE; | |
403 | } | |
404 | return array($contactID, $dbId, mt_rand()); | |
405 | } | |
c1f3c6da | 406 | |
6a488035 TO |
407 | return FALSE; |
408 | } | |
409 | ||
bec3fc7c | 410 | /** |
fe482240 | 411 | * Set a init session with user object. |
bec3fc7c | 412 | * |
77855840 TO |
413 | * @param array $data |
414 | * Array with user specific data. | |
bec3fc7c | 415 | */ |
00be9182 | 416 | public function setUserSession($data) { |
bec3fc7c | 417 | list($userID, $ufID) = $data; |
481a74f4 | 418 | $user = new JUser($ufID); |
2d8f9c75 | 419 | $session = JFactory::getSession(); |
bec3fc7c BS |
420 | $session->set('user', $user); |
421 | ||
cb0e36de | 422 | parent::setUserSession($data); |
bec3fc7c BS |
423 | } |
424 | ||
6a488035 | 425 | /** |
17f443df | 426 | * FIXME: Do something |
6a488035 | 427 | */ |
00be9182 | 428 | public function setMessage($message) { |
6a488035 TO |
429 | } |
430 | ||
bb3a214a | 431 | /** |
17f443df | 432 | * FIXME: Do something |
bb3a214a | 433 | */ |
00be9182 | 434 | public function loadUser($user) { |
6a488035 TO |
435 | return TRUE; |
436 | } | |
437 | ||
17f443df CW |
438 | /** |
439 | * FIXME: Use CMS-native approach | |
440 | */ | |
00be9182 | 441 | public function permissionDenied() { |
0499b0ad | 442 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
443 | } |
444 | ||
17f443df CW |
445 | /** |
446 | * @inheritDoc | |
447 | */ | |
00be9182 | 448 | public function logout() { |
6a488035 | 449 | session_destroy(); |
d42a224c | 450 | CRM_Utils_System::setHttpHeader("Location", "index.php"); |
6a488035 TO |
451 | } |
452 | ||
453 | /** | |
17f443df | 454 | * @inheritDoc |
6a488035 | 455 | */ |
00be9182 | 456 | public function getUFLocale() { |
6a488035 TO |
457 | if (defined('_JEXEC')) { |
458 | $conf = JFactory::getConfig(); | |
4965d8e9 | 459 | $locale = $conf->get('language'); |
6a488035 TO |
460 | return str_replace('-', '_', $locale); |
461 | } | |
462 | return NULL; | |
463 | } | |
464 | ||
fd1f3a26 SV |
465 | /** |
466 | * @inheritDoc | |
467 | */ | |
468 | public function setUFLocale($civicrm_language) { | |
469 | // TODO | |
470 | return TRUE; | |
471 | } | |
472 | ||
bb3a214a | 473 | /** |
17f443df | 474 | * @inheritDoc |
bb3a214a | 475 | */ |
00be9182 | 476 | public function getVersion() { |
6a488035 | 477 | if (class_exists('JVersion')) { |
e7483cbe | 478 | $version = new JVersion(); |
6a488035 TO |
479 | return $version->getShortVersion(); |
480 | } | |
481 | else { | |
482 | return 'Unknown'; | |
483 | } | |
484 | } | |
485 | ||
f4aaa82a | 486 | /** |
fe482240 | 487 | * Load joomla bootstrap. |
6a488035 | 488 | * |
5a4f6742 CW |
489 | * @param array $params |
490 | * with uid or name and password. | |
491 | * @param bool $loadUser | |
492 | * load cms user?. | |
f4aaa82a EM |
493 | * @param bool|\throw $throwError throw error on failure? |
494 | * @param null $realPath | |
495 | * @param bool $loadDefines | |
496 | * | |
497 | * @return bool | |
6a488035 | 498 | */ |
00be9182 | 499 | public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) { |
6a488035 TO |
500 | // Setup the base path related constant. |
501 | $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))))); | |
502 | ||
503 | // load BootStrap here if needed | |
504 | // We are a valid Joomla entry point. | |
353ffa53 | 505 | if (!defined('_JEXEC') && $loadDefines) { |
6a488035 TO |
506 | define('_JEXEC', 1); |
507 | define('DS', DIRECTORY_SEPARATOR); | |
508 | define('JPATH_BASE', $joomlaBase . '/administrator'); | |
509 | require $joomlaBase . '/administrator/includes/defines.php'; | |
510 | } | |
511 | ||
512 | // Get the framework. | |
2cb7adde | 513 | if (file_exists($joomlaBase . '/libraries/import.legacy.php')) { |
2efcf0c2 | 514 | require $joomlaBase . '/libraries/import.legacy.php'; |
2cb7adde | 515 | } |
6a488035 TO |
516 | require $joomlaBase . '/libraries/import.php'; |
517 | require $joomlaBase . '/libraries/joomla/event/dispatcher.php'; | |
6a488035 TO |
518 | require $joomlaBase . '/configuration.php'; |
519 | ||
6fde79f5 | 520 | // Files may be in different places depending on Joomla version |
481a74f4 | 521 | if (!defined('JVERSION')) { |
6fde79f5 | 522 | require $joomlaBase . '/libraries/cms/version/version.php'; |
e7483cbe | 523 | $jversion = new JVersion(); |
6fde79f5 BS |
524 | define('JVERSION', $jversion->getShortVersion()); |
525 | } | |
526 | ||
481a74f4 | 527 | if (version_compare(JVERSION, '3.0', 'lt')) { |
6fde79f5 BS |
528 | require $joomlaBase . '/libraries/joomla/environment/uri.php'; |
529 | require $joomlaBase . '/libraries/joomla/application/component/helper.php'; | |
530 | } | |
531 | else { | |
87cdafb0 | 532 | require $joomlaBase . '/libraries/cms.php'; |
6fde79f5 | 533 | require $joomlaBase . '/libraries/joomla/uri/uri.php'; |
6fde79f5 BS |
534 | } |
535 | ||
6a488035 | 536 | jimport('joomla.application.cli'); |
f4aaa82a | 537 | |
182f835d | 538 | // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes. |
539 | $config = CRM_Core_Config::singleton(); | |
540 | CRM_Utils_Hook::config($config); | |
6a488035 TO |
541 | |
542 | return TRUE; | |
543 | } | |
544 | ||
545 | /** | |
17f443df | 546 | * @inheritDoc |
6a488035 TO |
547 | */ |
548 | public function isUserLoggedIn() { | |
549 | $user = JFactory::getUser(); | |
550 | return ($user->guest) ? FALSE : TRUE; | |
551 | } | |
552 | ||
553 | /** | |
17f443df | 554 | * @inheritDoc |
6a488035 TO |
555 | */ |
556 | public function getLoggedInUfID() { | |
557 | $user = JFactory::getUser(); | |
558 | return ($user->guest) ? NULL : $user->id; | |
559 | } | |
560 | ||
2b617cb0 | 561 | /** |
17f443df | 562 | * @inheritDoc |
2b617cb0 | 563 | */ |
00be9182 | 564 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
565 | $user = JFactory::getUser(); |
566 | return $this->getUniqueIdentifierFromUserObject($user); | |
567 | } | |
353ffa53 | 568 | |
32998c82 | 569 | /** |
17f443df | 570 | * @inheritDoc |
32998c82 | 571 | */ |
00be9182 | 572 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
573 | return !empty($user->id) ? $user->id : NULL; |
574 | } | |
575 | ||
2b617cb0 | 576 | /** |
17f443df | 577 | * @inheritDoc |
2b617cb0 | 578 | */ |
00be9182 | 579 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
580 | return ($user->guest) ? NULL : $user->email; |
581 | } | |
582 | ||
6a488035 TO |
583 | /** |
584 | * Get a list of all installed modules, including enabled and disabled ones | |
585 | * | |
a6c01b45 CW |
586 | * @return array |
587 | * CRM_Core_Module | |
6a488035 | 588 | */ |
00be9182 | 589 | public function getModules() { |
6a488035 TO |
590 | $result = array(); |
591 | ||
592 | $db = JFactory::getDbo(); | |
e7292422 | 593 | $query = $db->getQuery(TRUE); |
6a488035 TO |
594 | $query->select('type, folder, element, enabled') |
595 | ->from('#__extensions') | |
596 | ->where('type =' . $db->Quote('plugin')); | |
597 | $plugins = $db->setQuery($query)->loadAssocList(); | |
598 | foreach ($plugins as $plugin) { | |
599 | // question: is the folder really a critical part of the plugin's name? | |
600 | $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element'])); | |
601 | $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE); | |
602 | } | |
603 | ||
604 | return $result; | |
605 | } | |
606 | ||
607 | /** | |
17f443df | 608 | * @inheritDoc |
6a488035 TO |
609 | */ |
610 | public function getLoginURL($destination = '') { | |
611 | $config = CRM_Core_Config::singleton(); | |
612 | $loginURL = $config->userFrameworkBaseURL; | |
613 | $loginURL = str_replace('administrator/', '', $loginURL); | |
614 | $loginURL .= 'index.php?option=com_users&view=login'; | |
091412ab BS |
615 | |
616 | //CRM-14872 append destination | |
481a74f4 | 617 | if (!empty($destination)) { |
92fcb95f | 618 | $loginURL .= '&return=' . urlencode(base64_encode($destination)); |
091412ab | 619 | } |
6a488035 TO |
620 | return $loginURL; |
621 | } | |
f813f78e | 622 | |
bb3a214a | 623 | /** |
17f443df | 624 | * @inheritDoc |
bb3a214a | 625 | */ |
6a488035 | 626 | public function getLoginDestination(&$form) { |
091412ab BS |
627 | $args = NULL; |
628 | ||
629 | $id = $form->get('id'); | |
630 | if ($id) { | |
631 | $args .= "&id=$id"; | |
632 | } | |
633 | else { | |
634 | $gid = $form->get('gid'); | |
635 | if ($gid) { | |
636 | $args .= "&gid=$gid"; | |
637 | } | |
638 | else { | |
639 | // Setup Personal Campaign Page link uses pageId | |
640 | $pageId = $form->get('pageId'); | |
641 | if ($pageId) { | |
642 | $component = $form->get('component'); | |
643 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
644 | } | |
645 | } | |
646 | } | |
647 | ||
648 | $destination = NULL; | |
649 | if ($args) { | |
650 | // append destination so user is returned to form they came from after login | |
92fcb95f | 651 | $args = 'reset=1' . $args; |
48341e06 | 652 | $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE); |
091412ab BS |
653 | } |
654 | ||
655 | return $destination; | |
6a488035 | 656 | } |
9977c6f5 | 657 | |
658 | /** | |
17f443df | 659 | * @inheritDoc |
9977c6f5 | 660 | */ |
9b873358 | 661 | public function getDefaultSiteSettings($dir) { |
9977c6f5 | 662 | $config = CRM_Core_Config::singleton(); |
663 | $url = preg_replace( | |
664 | '|/administrator|', | |
665 | '', | |
666 | $config->userFrameworkBaseURL | |
667 | ); | |
668 | $siteRoot = preg_replace( | |
669 | '|/media/civicrm/.*$|', | |
670 | '', | |
671 | $config->imageUploadDir | |
672 | ); | |
673 | return array($url, NULL, $siteRoot); | |
674 | } | |
59f97da6 EM |
675 | |
676 | /** | |
17f443df | 677 | * @inheritDoc |
59f97da6 | 678 | */ |
00be9182 | 679 | public function getUserRecordUrl($contactID) { |
59f97da6 EM |
680 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
681 | $userRecordUrl = NULL; | |
682 | // if logged in user is super user, then he can view other users joomla profile | |
683 | if (JFactory::getUser()->authorise('core.admin')) { | |
684 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid; | |
685 | } | |
686 | elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) { | |
687 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid; | |
688 | } | |
689 | } | |
690 | ||
691 | /** | |
17f443df | 692 | * @inheritDoc |
59f97da6 | 693 | */ |
00be9182 | 694 | public function checkPermissionAddUser() { |
59f97da6 EM |
695 | if (JFactory::getUser()->authorise('core.create', 'com_users')) { |
696 | return TRUE; | |
697 | } | |
698 | } | |
f85b1d20 EM |
699 | |
700 | /** | |
fe482240 | 701 | * Output code from error function. |
f85b1d20 EM |
702 | * @param string $content |
703 | */ | |
00be9182 | 704 | public function outputError($content) { |
f85b1d20 EM |
705 | if (class_exists('JErrorPage')) { |
706 | $error = new Exception($content); | |
707 | JErrorPage::render($error); | |
708 | } | |
4c9b6178 | 709 | elseif (class_exists('JError')) { |
f85b1d20 EM |
710 | JError::raiseError('CiviCRM-001', $content); |
711 | } | |
712 | else { | |
713 | parent::outputError($content); | |
714 | } | |
715 | } | |
e7292422 | 716 | |
f58e4c2e | 717 | /** |
fe482240 | 718 | * Append Joomla js to coreResourcesList. |
ad37ac8e | 719 | * |
720 | * @param array $list | |
f58e4c2e | 721 | */ |
00be9182 | 722 | public function appendCoreResources(&$list) { |
f58e4c2e DC |
723 | $list[] = 'js/crm.joomla.js'; |
724 | } | |
96025800 | 725 | |
6a488035 | 726 | } |