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