Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 { | |
6714d8d2 | 38 | |
bb3a214a | 39 | /** |
b8c71ffa | 40 | * Class constructor. |
bb3a214a | 41 | */ |
00be9182 | 42 | public function __construct() { |
4caaa696 EM |
43 | /** |
44 | * 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 | |
45 | * functions and leave the codebase oblivious to the type of CMS | |
46 | * @deprecated | |
47 | * @var bool | |
48 | */ | |
6a488035 TO |
49 | $this->is_drupal = FALSE; |
50 | } | |
51 | ||
52 | /** | |
17f443df | 53 | * @inheritDoc |
6a488035 | 54 | */ |
00be9182 | 55 | public function createUser(&$params, $mail) { |
6a488035 TO |
56 | $baseDir = JPATH_SITE; |
57 | require_once $baseDir . '/components/com_users/models/registration.php'; | |
58 | ||
59 | $userParams = JComponentHelper::getParams('com_users'); | |
353ffa53 TO |
60 | $model = new UsersModelRegistration(); |
61 | $ufID = NULL; | |
6a488035 TO |
62 | |
63 | // get the default usertype | |
64 | $userType = $userParams->get('new_usertype'); | |
65 | if (!$userType) { | |
66 | $userType = 2; | |
67 | } | |
68 | ||
69 | if (isset($params['name'])) { | |
70 | $fullname = trim($params['name']); | |
71 | } | |
72 | elseif (isset($params['contactID'])) { | |
73 | $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID'])); | |
74 | } | |
75 | else { | |
76 | $fullname = trim($params['cms_name']); | |
77 | } | |
78 | ||
79 | // Prepare the values for a new Joomla user. | |
be2fb01f | 80 | $values = []; |
353ffa53 TO |
81 | $values['name'] = $fullname; |
82 | $values['username'] = trim($params['cms_name']); | |
6a488035 | 83 | $values['password1'] = $values['password2'] = $params['cms_pass']; |
353ffa53 | 84 | $values['email1'] = $values['email2'] = trim($params[$mail]); |
6a488035 TO |
85 | |
86 | $lang = JFactory::getLanguage(); | |
87 | $lang->load('com_users', $baseDir); | |
88 | ||
89 | $register = $model->register($values); | |
90 | ||
91 | $ufID = JUserHelper::getUserId($values['username']); | |
92 | return $ufID; | |
93 | } | |
94 | ||
f4aaa82a | 95 | /** |
17f443df | 96 | * @inheritDoc |
6a488035 | 97 | */ |
00be9182 | 98 | public function updateCMSName($ufID, $ufName) { |
6a488035 TO |
99 | $ufID = CRM_Utils_Type::escape($ufID, 'Integer'); |
100 | $ufName = CRM_Utils_Type::escape($ufName, 'String'); | |
101 | ||
be2fb01f | 102 | $values = []; |
e851ce06 | 103 | $user = JUser::getInstance($ufID); |
6a488035 TO |
104 | |
105 | $values['email'] = $ufName; | |
106 | $user->bind($values); | |
107 | ||
108 | $user->save(); | |
109 | } | |
110 | ||
111 | /** | |
94f9f81a | 112 | * Check if username and email exists in the Joomla db. |
6a488035 | 113 | * |
77855840 TO |
114 | * @param array $params |
115 | * Array of name and mail values. | |
116 | * @param array $errors | |
117 | * Array of errors. | |
118 | * @param string $emailName | |
119 | * Field label for the 'email'. | |
6a488035 | 120 | */ |
00be9182 | 121 | public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
6a488035 TO |
122 | $config = CRM_Core_Config::singleton(); |
123 | ||
353ffa53 TO |
124 | $dao = new CRM_Core_DAO(); |
125 | $name = $dao->escape(CRM_Utils_Array::value('name', $params)); | |
6a488035 TO |
126 | $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); |
127 | //don't allow the special characters and min. username length is two | |
128 | //regex \\ to match a single backslash would become '/\\\\/' | |
129 | $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name); | |
130 | if ($isNotValid || strlen($name) < 2) { | |
131 | $errors['cms_name'] = ts('Your username contains invalid characters or is too short'); | |
132 | } | |
133 | ||
6a488035 TO |
134 | $JUserTable = &JTable::getInstance('User', 'JTable'); |
135 | ||
136 | $db = $JUserTable->getDbo(); | |
137 | $query = $db->getQuery(TRUE); | |
138 | $query->select('username, email'); | |
139 | $query->from($JUserTable->getTableName()); | |
b27d1855 | 140 | |
141 | // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. | |
6a488035 TO |
142 | $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))'); |
143 | $db->setQuery($query, 0, 10); | |
144 | $users = $db->loadAssocList(); | |
145 | ||
be2fb01f | 146 | $row = []; |
6a488035 TO |
147 | if (count($users)) { |
148 | $row = $users[0]; | |
149 | } | |
150 | ||
151 | if (!empty($row)) { | |
152 | $dbName = CRM_Utils_Array::value('username', $row); | |
153 | $dbEmail = CRM_Utils_Array::value('email', $row); | |
154 | if (strtolower($dbName) == strtolower($name)) { | |
155 | $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.', | |
be2fb01f | 156 | [1 => $name] |
6a488035 TO |
157 | ); |
158 | } | |
159 | if (strtolower($dbEmail) == strtolower($email)) { | |
160 | $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset'; | |
89374eb2 | 161 | $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>', |
be2fb01f | 162 | [1 => $email, 2 => $resetUrl] |
6a488035 TO |
163 | ); |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | /** | |
17f443df | 169 | * @inheritDoc |
6a488035 | 170 | */ |
00be9182 | 171 | public function setTitle($title, $pageTitle = NULL) { |
6a488035 TO |
172 | if (!$pageTitle) { |
173 | $pageTitle = $title; | |
174 | } | |
175 | ||
176 | $template = CRM_Core_Smarty::singleton(); | |
177 | $template->assign('pageTitle', $pageTitle); | |
178 | ||
179 | $document = JFactory::getDocument(); | |
180 | $document->setTitle($title); | |
6a488035 TO |
181 | } |
182 | ||
183 | /** | |
17f443df | 184 | * @inheritDoc |
6a488035 | 185 | */ |
00be9182 | 186 | public function appendBreadCrumb($breadCrumbs) { |
6a488035 TO |
187 | $template = CRM_Core_Smarty::singleton(); |
188 | $bc = $template->get_template_vars('breadcrumb'); | |
189 | ||
190 | if (is_array($breadCrumbs)) { | |
191 | foreach ($breadCrumbs as $crumbs) { | |
192 | if (stripos($crumbs['url'], 'id%%')) { | |
be2fb01f | 193 | $args = ['cid', 'mid']; |
6a488035 TO |
194 | foreach ($args as $a) { |
195 | $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject, | |
196 | FALSE, NULL, $_GET | |
197 | ); | |
198 | if ($val) { | |
199 | $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']); | |
200 | } | |
201 | } | |
202 | } | |
203 | $bc[] = $crumbs; | |
204 | } | |
205 | } | |
206 | $template->assign_by_ref('breadcrumb', $bc); | |
6a488035 TO |
207 | } |
208 | ||
209 | /** | |
17f443df | 210 | * @inheritDoc |
6a488035 | 211 | */ |
00be9182 | 212 | public function resetBreadCrumb() { |
6a488035 TO |
213 | } |
214 | ||
215 | /** | |
17f443df | 216 | * @inheritDoc |
6a488035 | 217 | */ |
17f443df | 218 | public function addHTMLHead($string = NULL) { |
6a488035 TO |
219 | if ($string) { |
220 | $document = JFactory::getDocument(); | |
221 | $document->addCustomTag($string); | |
222 | } | |
223 | } | |
224 | ||
225 | /** | |
17f443df | 226 | * @inheritDoc |
6a488035 TO |
227 | */ |
228 | public function addStyleUrl($url, $region) { | |
229 | if ($region == 'html-header') { | |
230 | $document = JFactory::getDocument(); | |
231 | $document->addStyleSheet($url); | |
232 | return TRUE; | |
233 | } | |
234 | return FALSE; | |
235 | } | |
236 | ||
237 | /** | |
17f443df | 238 | * @inheritDoc |
6a488035 TO |
239 | */ |
240 | public function addStyle($code, $region) { | |
241 | if ($region == 'html-header') { | |
242 | $document = JFactory::getDocument(); | |
243 | $document->addStyleDeclaration($code); | |
244 | return TRUE; | |
245 | } | |
246 | return FALSE; | |
247 | } | |
248 | ||
249 | /** | |
17f443df | 250 | * @inheritDoc |
6a488035 | 251 | */ |
e7483cbe | 252 | public function url( |
17f443df CW |
253 | $path = NULL, |
254 | $query = NULL, | |
255 | $absolute = FALSE, | |
256 | $fragment = NULL, | |
17f443df CW |
257 | $frontend = FALSE, |
258 | $forceBackend = FALSE | |
6a488035 | 259 | ) { |
353ffa53 | 260 | $config = CRM_Core_Config::singleton(); |
c80e2dbf | 261 | $separator = '&'; |
353ffa53 TO |
262 | $Itemid = ''; |
263 | $script = ''; | |
264 | $path = CRM_Utils_String::stripPathChars($path); | |
6a488035 TO |
265 | |
266 | if ($config->userFrameworkFrontend) { | |
267 | $script = 'index.php'; | |
378e2b4c | 268 | if (JRequest::getVar("Itemid") && (strpos($path, 'civicrm/payment/ipn') === FALSE)) { |
6a488035 TO |
269 | $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid"); |
270 | } | |
271 | } | |
272 | ||
273 | if (isset($fragment)) { | |
274 | $fragment = '#' . $fragment; | |
275 | } | |
276 | ||
6a488035 TO |
277 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; |
278 | ||
279 | if (!empty($query)) { | |
280 | $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}"; | |
281 | } | |
282 | else { | |
283 | $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}"; | |
284 | } | |
285 | ||
286 | // gross hack for joomla, we are in the backend and want to send a frontend url | |
287 | if ($frontend && $config->userFramework == 'Joomla') { | |
288 | // handle both joomla v1.5 and v1.6, CRM-7939 | |
289 | $url = str_replace('/administrator/index2.php', '/index.php', $url); | |
290 | $url = str_replace('/administrator/index.php', '/index.php', $url); | |
291 | ||
292 | // CRM-8215 | |
293 | $url = str_replace('/administrator/', '/index.php', $url); | |
294 | } | |
295 | elseif ($forceBackend) { | |
296 | if (defined('JVERSION')) { | |
297 | $joomlaVersion = JVERSION; | |
0db6c3e1 TO |
298 | } |
299 | else { | |
e7483cbe | 300 | $jversion = new JVersion(); |
6a488035 TO |
301 | $joomlaVersion = $jversion->getShortVersion(); |
302 | } | |
303 | ||
304 | if (version_compare($joomlaVersion, '1.6') >= 0) { | |
305 | $url = str_replace('/index.php', '/administrator/index.php', $url); | |
306 | } | |
307 | } | |
308 | return $url; | |
309 | } | |
310 | ||
6a488035 | 311 | /** |
fe482240 | 312 | * Set the email address of the user. |
6a488035 | 313 | * |
77855840 TO |
314 | * @param object $user |
315 | * Handle to the user object. | |
6a488035 | 316 | */ |
00be9182 | 317 | public function setEmail(&$user) { |
6a488035 | 318 | global $database; |
94f9f81a EW |
319 | $query = $db->getQuery(TRUE); |
320 | $query->select($db->quoteName('email')) | |
6714d8d2 SL |
321 | ->from($db->quoteName('#__users')) |
322 | ->where($db->quoteName('id') . ' = ' . $user->id); | |
6a488035 TO |
323 | $database->setQuery($query); |
324 | $user->email = $database->loadResult(); | |
325 | } | |
326 | ||
327 | /** | |
17f443df | 328 | * @inheritDoc |
6a488035 | 329 | */ |
17f443df | 330 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
6a488035 TO |
331 | require_once 'DB.php'; |
332 | ||
333 | $config = CRM_Core_Config::singleton(); | |
ebc28bab | 334 | $user = NULL; |
6a488035 TO |
335 | |
336 | if ($loadCMSBootstrap) { | |
be2fb01f | 337 | $bootStrapParams = []; |
6a488035 | 338 | if ($name && $password) { |
be2fb01f | 339 | $bootStrapParams = [ |
6a488035 TO |
340 | 'name' => $name, |
341 | 'pass' => $password, | |
be2fb01f | 342 | ]; |
6a488035 | 343 | } |
bec3fc7c | 344 | CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE); |
6a488035 TO |
345 | } |
346 | ||
347 | jimport('joomla.application.component.helper'); | |
348 | jimport('joomla.database.table'); | |
c1f3c6da BS |
349 | jimport('joomla.user.helper'); |
350 | ||
351 | $JUserTable = JTable::getInstance('User', 'JTable'); | |
352 | ||
353 | $db = $JUserTable->getDbo(); | |
354 | $query = $db->getQuery(TRUE); | |
355 | $query->select('id, name, username, email, password'); | |
356 | $query->from($JUserTable->getTableName()); | |
357 | $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)'); | |
358 | $db->setQuery($query, 0, 0); | |
359 | $users = $db->loadObjectList(); | |
360 | ||
be2fb01f | 361 | $row = []; |
c1f3c6da BS |
362 | if (count($users)) { |
363 | $row = $users[0]; | |
364 | } | |
6a488035 | 365 | |
e1d37cef MW |
366 | $joomlaBase = self::getBasePath(); |
367 | self::getJVersion($joomlaBase); | |
6a488035 | 368 | |
c1f3c6da BS |
369 | if (!empty($row)) { |
370 | $dbPassword = $row->password; | |
371 | $dbId = $row->id; | |
372 | $dbEmail = $row->email; | |
6a488035 | 373 | |
481a74f4 TO |
374 | if (version_compare(JVERSION, '2.5.18', 'lt') || |
375 | (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt')) | |
c1f3c6da | 376 | ) { |
ebc28bab | 377 | // now check password |
009eff21 EW |
378 | list($hash, $salt) = explode(':', $dbPassword); |
379 | $cryptpass = md5($password . $salt); | |
380 | if ($hash != $cryptpass) { | |
01c77fa9 EW |
381 | return FALSE; |
382 | } | |
6a488035 | 383 | } |
c1f3c6da | 384 | else { |
4f99ca55 TO |
385 | if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) { |
386 | return FALSE; | |
e7292422 | 387 | } |
9d735153 | 388 | |
3a8c9bb6 MW |
389 | if (version_compare(JVERSION, '3.8.0', 'ge')) { |
390 | jimport('joomla.application.helper'); | |
391 | jimport('joomla.application.cms'); | |
392 | jimport('joomla.application.administrator'); | |
393 | } | |
9d735153 | 394 | //include additional files required by Joomla 3.2.1+ |
3a8c9bb6 | 395 | elseif (version_compare(JVERSION, '3.2.1', 'ge')) { |
90eac10a BS |
396 | require_once $joomlaBase . '/libraries/cms/application/helper.php'; |
397 | require_once $joomlaBase . '/libraries/cms/application/cms.php'; | |
398 | require_once $joomlaBase . '/libraries/cms/application/administrator.php'; | |
9d735153 | 399 | } |
6a488035 TO |
400 | } |
401 | ||
c1f3c6da | 402 | CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla'); |
6a488035 TO |
403 | $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId); |
404 | if (!$contactID) { | |
405 | return FALSE; | |
406 | } | |
be2fb01f | 407 | return [$contactID, $dbId, mt_rand()]; |
6a488035 | 408 | } |
c1f3c6da | 409 | |
6a488035 TO |
410 | return FALSE; |
411 | } | |
412 | ||
bec3fc7c | 413 | /** |
fe482240 | 414 | * Set a init session with user object. |
bec3fc7c | 415 | * |
77855840 TO |
416 | * @param array $data |
417 | * Array with user specific data. | |
bec3fc7c | 418 | */ |
00be9182 | 419 | public function setUserSession($data) { |
bec3fc7c | 420 | list($userID, $ufID) = $data; |
481a74f4 | 421 | $user = new JUser($ufID); |
2d8f9c75 | 422 | $session = JFactory::getSession(); |
bec3fc7c BS |
423 | $session->set('user', $user); |
424 | ||
cb0e36de | 425 | parent::setUserSession($data); |
bec3fc7c BS |
426 | } |
427 | ||
6a488035 | 428 | /** |
17f443df | 429 | * FIXME: Do something |
ea3ddccf | 430 | * |
431 | * @param string $message | |
6a488035 | 432 | */ |
00be9182 | 433 | public function setMessage($message) { |
6a488035 TO |
434 | } |
435 | ||
bb3a214a | 436 | /** |
b596c3e9 | 437 | * @param \string $username |
438 | * @param \string $password | |
ea3ddccf | 439 | * |
440 | * @return bool | |
bb3a214a | 441 | */ |
b596c3e9 | 442 | public function loadUser($username, $password = NULL) { |
443 | $uid = JUserHelper::getUserId($username); | |
444 | if (empty($uid)) { | |
445 | return FALSE; | |
446 | } | |
447 | $contactID = CRM_Core_BAO_UFMatch::getContactId($uid); | |
448 | if (!empty($password)) { | |
449 | $instance = JFactory::getApplication('site'); | |
be2fb01f | 450 | $params = [ |
b596c3e9 | 451 | 'username' => $username, |
452 | 'password' => $password, | |
be2fb01f | 453 | ]; |
b596c3e9 | 454 | //perform the login action |
455 | $instance->login($params); | |
456 | } | |
457 | ||
8f407be0 AS |
458 | // Save details in Joomla session |
459 | $user = JFactory::getUser($uid); | |
460 | $jsession = JFactory::getSession(); | |
461 | $jsession->set('user', $user); | |
462 | ||
463 | // Save details in Civi session | |
b596c3e9 | 464 | $session = CRM_Core_Session::singleton(); |
465 | $session->set('ufID', $uid); | |
466 | $session->set('userID', $contactID); | |
6a488035 TO |
467 | return TRUE; |
468 | } | |
469 | ||
17f443df CW |
470 | /** |
471 | * FIXME: Use CMS-native approach | |
472 | */ | |
00be9182 | 473 | public function permissionDenied() { |
0499b0ad | 474 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
475 | } |
476 | ||
17f443df CW |
477 | /** |
478 | * @inheritDoc | |
479 | */ | |
00be9182 | 480 | public function logout() { |
6a488035 | 481 | session_destroy(); |
d42a224c | 482 | CRM_Utils_System::setHttpHeader("Location", "index.php"); |
6a488035 TO |
483 | } |
484 | ||
485 | /** | |
17f443df | 486 | * @inheritDoc |
6a488035 | 487 | */ |
00be9182 | 488 | public function getUFLocale() { |
6a488035 TO |
489 | if (defined('_JEXEC')) { |
490 | $conf = JFactory::getConfig(); | |
4965d8e9 | 491 | $locale = $conf->get('language'); |
6a488035 TO |
492 | return str_replace('-', '_', $locale); |
493 | } | |
494 | return NULL; | |
495 | } | |
496 | ||
fd1f3a26 SV |
497 | /** |
498 | * @inheritDoc | |
499 | */ | |
500 | public function setUFLocale($civicrm_language) { | |
501 | // TODO | |
502 | return TRUE; | |
503 | } | |
504 | ||
bb3a214a | 505 | /** |
17f443df | 506 | * @inheritDoc |
bb3a214a | 507 | */ |
00be9182 | 508 | public function getVersion() { |
6a488035 | 509 | if (class_exists('JVersion')) { |
e7483cbe | 510 | $version = new JVersion(); |
6a488035 TO |
511 | return $version->getShortVersion(); |
512 | } | |
513 | else { | |
514 | return 'Unknown'; | |
515 | } | |
516 | } | |
517 | ||
e1d37cef MW |
518 | public function getJVersion($joomlaBase) { |
519 | // Files may be in different places depending on Joomla version | |
520 | if (!defined('JVERSION')) { | |
521 | // Joomla 3.8.0+ | |
522 | $versionPhp = $joomlaBase . '/libraries/src/Version.php'; | |
523 | if (!file_exists($versionPhp)) { | |
524 | // Joomla < 3.8.0 | |
525 | $versionPhp = $joomlaBase . '/libraries/cms/version/version.php'; | |
526 | } | |
527 | require $versionPhp; | |
528 | $jversion = new JVersion(); | |
529 | define('JVERSION', $jversion->getShortVersion()); | |
530 | } | |
531 | } | |
532 | ||
f3884007 MW |
533 | /** |
534 | * Setup the base path related constant. | |
535 | * @return mixed | |
536 | */ | |
e1d37cef | 537 | public function getBasePath() { |
f3884007 | 538 | global $civicrm_root; |
e23eb52f | 539 | $joomlaPath = explode(DIRECTORY_SEPARATOR . 'administrator', $civicrm_root); |
f3884007 MW |
540 | $joomlaBase = $joomlaPath[0]; |
541 | return $joomlaBase; | |
e1d37cef MW |
542 | } |
543 | ||
f4aaa82a | 544 | /** |
fe482240 | 545 | * Load joomla bootstrap. |
6a488035 | 546 | * |
5a4f6742 CW |
547 | * @param array $params |
548 | * with uid or name and password. | |
549 | * @param bool $loadUser | |
550 | * load cms user?. | |
f4aaa82a EM |
551 | * @param bool|\throw $throwError throw error on failure? |
552 | * @param null $realPath | |
553 | * @param bool $loadDefines | |
554 | * | |
555 | * @return bool | |
6a488035 | 556 | */ |
be2fb01f | 557 | public function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) { |
e1d37cef | 558 | $joomlaBase = self::getBasePath(); |
6a488035 TO |
559 | |
560 | // load BootStrap here if needed | |
561 | // We are a valid Joomla entry point. | |
353ffa53 | 562 | if (!defined('_JEXEC') && $loadDefines) { |
6a488035 TO |
563 | define('_JEXEC', 1); |
564 | define('DS', DIRECTORY_SEPARATOR); | |
565 | define('JPATH_BASE', $joomlaBase . '/administrator'); | |
566 | require $joomlaBase . '/administrator/includes/defines.php'; | |
567 | } | |
568 | ||
569 | // Get the framework. | |
2cb7adde | 570 | if (file_exists($joomlaBase . '/libraries/import.legacy.php')) { |
2efcf0c2 | 571 | require $joomlaBase . '/libraries/import.legacy.php'; |
2cb7adde | 572 | } |
3a8c9bb6 | 573 | require $joomlaBase . '/libraries/cms.php'; |
e1d37cef | 574 | self::getJVersion($joomlaBase); |
6fde79f5 | 575 | |
8421b502 | 576 | if (version_compare(JVERSION, '3.8', 'lt')) { |
577 | require $joomlaBase . '/libraries/import.php'; | |
578 | require $joomlaBase . '/libraries/joomla/event/dispatcher.php'; | |
579 | } | |
580 | ||
581 | require_once $joomlaBase . '/configuration.php'; | |
582 | ||
481a74f4 | 583 | if (version_compare(JVERSION, '3.0', 'lt')) { |
6fde79f5 BS |
584 | require $joomlaBase . '/libraries/joomla/environment/uri.php'; |
585 | require $joomlaBase . '/libraries/joomla/application/component/helper.php'; | |
586 | } | |
8421b502 | 587 | elseif (version_compare(JVERSION, '3.8', 'lt')) { |
3a8c9bb6 | 588 | jimport('joomla.environment.uri'); |
6fde79f5 BS |
589 | } |
590 | ||
8421b502 | 591 | if (version_compare(JVERSION, '3.8', 'lt')) { |
592 | jimport('joomla.application.cli'); | |
593 | } | |
f4aaa82a | 594 | |
a5291777 TO |
595 | if (!defined('JDEBUG')) { |
596 | define('JDEBUG', FALSE); | |
597 | } | |
26915930 MWMC |
598 | |
599 | // Set timezone for Joomla on Cron | |
600 | $config = JFactory::getConfig(); | |
601 | $timezone = $config->get('offset'); | |
602 | if ($timezone) { | |
603 | date_default_timezone_set($timezone); | |
604 | CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); | |
605 | } | |
a5291777 | 606 | |
182f835d | 607 | // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes. |
608 | $config = CRM_Core_Config::singleton(); | |
609 | CRM_Utils_Hook::config($config); | |
6a488035 TO |
610 | |
611 | return TRUE; | |
612 | } | |
613 | ||
614 | /** | |
17f443df | 615 | * @inheritDoc |
6a488035 TO |
616 | */ |
617 | public function isUserLoggedIn() { | |
618 | $user = JFactory::getUser(); | |
619 | return ($user->guest) ? FALSE : TRUE; | |
620 | } | |
621 | ||
8caad0ce | 622 | /** |
623 | * @inheritDoc | |
624 | */ | |
625 | public function isUserRegistrationPermitted() { | |
626 | $userParams = JComponentHelper::getParams('com_users'); | |
627 | if (!$userParams->get('allowUserRegistration')) { | |
628 | return FALSE; | |
629 | } | |
630 | return TRUE; | |
631 | } | |
632 | ||
63df6889 HD |
633 | /** |
634 | * @inheritDoc | |
635 | */ | |
1a6630be | 636 | public function isPasswordUserGenerated() { |
63df6889 HD |
637 | return TRUE; |
638 | } | |
639 | ||
6a488035 | 640 | /** |
17f443df | 641 | * @inheritDoc |
6a488035 TO |
642 | */ |
643 | public function getLoggedInUfID() { | |
644 | $user = JFactory::getUser(); | |
645 | return ($user->guest) ? NULL : $user->id; | |
646 | } | |
647 | ||
2b617cb0 | 648 | /** |
17f443df | 649 | * @inheritDoc |
2b617cb0 | 650 | */ |
00be9182 | 651 | public function getLoggedInUniqueIdentifier() { |
2b617cb0 EM |
652 | $user = JFactory::getUser(); |
653 | return $this->getUniqueIdentifierFromUserObject($user); | |
654 | } | |
353ffa53 | 655 | |
cff0c9aa DS |
656 | /** |
657 | * @inheritDoc | |
658 | */ | |
659 | public function getUser($contactID) { | |
660 | $user_details = parent::getUser($contactID); | |
661 | $user = JFactory::getUser($user_details['id']); | |
662 | $user_details['name'] = $user->name; | |
663 | return $user_details; | |
664 | } | |
665 | ||
32998c82 | 666 | /** |
17f443df | 667 | * @inheritDoc |
32998c82 | 668 | */ |
00be9182 | 669 | public function getUserIDFromUserObject($user) { |
32998c82 EM |
670 | return !empty($user->id) ? $user->id : NULL; |
671 | } | |
672 | ||
2b617cb0 | 673 | /** |
17f443df | 674 | * @inheritDoc |
2b617cb0 | 675 | */ |
00be9182 | 676 | public function getUniqueIdentifierFromUserObject($user) { |
2b617cb0 EM |
677 | return ($user->guest) ? NULL : $user->email; |
678 | } | |
679 | ||
29864da4 | 680 | /** |
681 | * @inheritDoc | |
682 | */ | |
683 | public function getTimeZoneString() { | |
684 | $timezone = JFactory::getConfig()->get('offset'); | |
685 | return !$timezone ? date_default_timezone_get() : $timezone; | |
686 | } | |
687 | ||
6a488035 TO |
688 | /** |
689 | * Get a list of all installed modules, including enabled and disabled ones | |
690 | * | |
a6c01b45 CW |
691 | * @return array |
692 | * CRM_Core_Module | |
6a488035 | 693 | */ |
00be9182 | 694 | public function getModules() { |
be2fb01f | 695 | $result = []; |
6a488035 TO |
696 | |
697 | $db = JFactory::getDbo(); | |
e7292422 | 698 | $query = $db->getQuery(TRUE); |
6a488035 TO |
699 | $query->select('type, folder, element, enabled') |
700 | ->from('#__extensions') | |
701 | ->where('type =' . $db->Quote('plugin')); | |
702 | $plugins = $db->setQuery($query)->loadAssocList(); | |
703 | foreach ($plugins as $plugin) { | |
704 | // question: is the folder really a critical part of the plugin's name? | |
be2fb01f | 705 | $name = implode('.', ['joomla', $plugin['type'], $plugin['folder'], $plugin['element']]); |
6a488035 TO |
706 | $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE); |
707 | } | |
708 | ||
709 | return $result; | |
710 | } | |
711 | ||
712 | /** | |
17f443df | 713 | * @inheritDoc |
6a488035 TO |
714 | */ |
715 | public function getLoginURL($destination = '') { | |
716 | $config = CRM_Core_Config::singleton(); | |
717 | $loginURL = $config->userFrameworkBaseURL; | |
718 | $loginURL = str_replace('administrator/', '', $loginURL); | |
719 | $loginURL .= 'index.php?option=com_users&view=login'; | |
091412ab BS |
720 | |
721 | //CRM-14872 append destination | |
481a74f4 | 722 | if (!empty($destination)) { |
92fcb95f | 723 | $loginURL .= '&return=' . urlencode(base64_encode($destination)); |
091412ab | 724 | } |
6a488035 TO |
725 | return $loginURL; |
726 | } | |
f813f78e | 727 | |
bb3a214a | 728 | /** |
17f443df | 729 | * @inheritDoc |
bb3a214a | 730 | */ |
6a488035 | 731 | public function getLoginDestination(&$form) { |
091412ab BS |
732 | $args = NULL; |
733 | ||
734 | $id = $form->get('id'); | |
735 | if ($id) { | |
736 | $args .= "&id=$id"; | |
737 | } | |
738 | else { | |
739 | $gid = $form->get('gid'); | |
740 | if ($gid) { | |
741 | $args .= "&gid=$gid"; | |
742 | } | |
743 | else { | |
744 | // Setup Personal Campaign Page link uses pageId | |
745 | $pageId = $form->get('pageId'); | |
746 | if ($pageId) { | |
747 | $component = $form->get('component'); | |
748 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | $destination = NULL; | |
754 | if ($args) { | |
755 | // append destination so user is returned to form they came from after login | |
92fcb95f | 756 | $args = 'reset=1' . $args; |
48341e06 | 757 | $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE); |
091412ab BS |
758 | } |
759 | ||
760 | return $destination; | |
6a488035 | 761 | } |
9977c6f5 | 762 | |
214fbc2c | 763 | /** |
764 | * Determine the location of the CMS root. | |
765 | * | |
766 | * @return string|NULL | |
767 | * local file system path to CMS root, or NULL if it cannot be determined | |
768 | */ | |
769 | public function cmsRootPath() { | |
a93a0366 TO |
770 | global $civicrm_paths; |
771 | if (!empty($civicrm_paths['cms.root']['path'])) { | |
772 | return $civicrm_paths['cms.root']['path']; | |
773 | } | |
774 | ||
214fbc2c | 775 | list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings(); |
e1d37cef | 776 | if (file_exists("$siteRoot/administrator/index.php")) { |
214fbc2c | 777 | return $siteRoot; |
778 | } | |
779 | return NULL; | |
780 | } | |
781 | ||
9977c6f5 | 782 | /** |
17f443df | 783 | * @inheritDoc |
9977c6f5 | 784 | */ |
70e8beda | 785 | public function getDefaultSiteSettings($dir = NULL) { |
9977c6f5 | 786 | $config = CRM_Core_Config::singleton(); |
787 | $url = preg_replace( | |
788 | '|/administrator|', | |
789 | '', | |
790 | $config->userFrameworkBaseURL | |
791 | ); | |
ad2d96ba | 792 | // CRM-19453 revisited. Under Windows, the pattern wasn't recognised. |
793 | // This is the original pattern, but it doesn't work under Windows. | |
794 | // By setting the pattern to the one used before the change first and only | |
795 | // changing it means that the change code only affects Windows users. | |
796 | $pattern = '|/media/civicrm/.*$|'; | |
797 | if (DIRECTORY_SEPARATOR == '\\') { | |
798 | // This regular expression will handle Windows as well as Linux | |
799 | // and any combination of forward and back slashes in directory | |
800 | // separators. We only apply it if the directory separator is the one | |
801 | // used by Windows. | |
802 | $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|'; | |
803 | } | |
9977c6f5 | 804 | $siteRoot = preg_replace( |
ad2d96ba | 805 | $pattern, |
9977c6f5 | 806 | '', |
807 | $config->imageUploadDir | |
808 | ); | |
be2fb01f | 809 | return [$url, NULL, $siteRoot]; |
9977c6f5 | 810 | } |
59f97da6 EM |
811 | |
812 | /** | |
17f443df | 813 | * @inheritDoc |
59f97da6 | 814 | */ |
00be9182 | 815 | public function getUserRecordUrl($contactID) { |
59f97da6 EM |
816 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
817 | $userRecordUrl = NULL; | |
55904b50 | 818 | // if logged in user has user edit access, then allow link to other users joomla profile |
a8e5af2a | 819 | if (JFactory::getUser()->authorise('core.edit', 'com_users')) { |
59f97da6 EM |
820 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid; |
821 | } | |
822 | elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) { | |
823 | return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid; | |
824 | } | |
825 | } | |
826 | ||
827 | /** | |
17f443df | 828 | * @inheritDoc |
59f97da6 | 829 | */ |
00be9182 | 830 | public function checkPermissionAddUser() { |
59f97da6 EM |
831 | if (JFactory::getUser()->authorise('core.create', 'com_users')) { |
832 | return TRUE; | |
833 | } | |
834 | } | |
f85b1d20 | 835 | |
03d5592a CW |
836 | /** |
837 | * @inheritDoc | |
838 | */ | |
839 | public function synchronizeUsers() { | |
840 | $config = CRM_Core_Config::singleton(); | |
841 | if (PHP_SAPI != 'cli') { | |
842 | set_time_limit(300); | |
843 | } | |
844 | $id = 'id'; | |
845 | $mail = 'email'; | |
846 | $name = 'name'; | |
847 | ||
848 | $JUserTable = &JTable::getInstance('User', 'JTable'); | |
849 | ||
850 | $db = $JUserTable->getDbo(); | |
851 | $query = $db->getQuery(TRUE); | |
852 | $query->select($id . ', ' . $mail . ', ' . $name); | |
853 | $query->from($JUserTable->getTableName()); | |
854 | $query->where($mail != ''); | |
855 | ||
856 | $db->setQuery($query); | |
857 | $users = $db->loadObjectList(); | |
858 | ||
859 | $user = new StdClass(); | |
860 | $uf = $config->userFramework; | |
861 | $contactCount = 0; | |
862 | $contactCreated = 0; | |
863 | $contactMatching = 0; | |
864 | for ($i = 0; $i < count($users); $i++) { | |
865 | $user->$id = $users[$i]->$id; | |
866 | $user->$mail = $users[$i]->$mail; | |
867 | $user->$name = $users[$i]->$name; | |
868 | $contactCount++; | |
869 | if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, | |
870 | $users[$i]->$id, | |
871 | $users[$i]->$mail, | |
872 | $uf, | |
873 | 1, | |
874 | 'Individual', | |
875 | TRUE | |
876 | ) | |
877 | ) { | |
878 | $contactCreated++; | |
879 | } | |
880 | else { | |
881 | $contactMatching++; | |
882 | } | |
03d5592a CW |
883 | } |
884 | ||
be2fb01f | 885 | return [ |
03d5592a CW |
886 | 'contactCount' => $contactCount, |
887 | 'contactMatching' => $contactMatching, | |
888 | 'contactCreated' => $contactCreated, | |
be2fb01f | 889 | ]; |
03d5592a CW |
890 | } |
891 | ||
6a488035 | 892 | } |