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