Commit | Line | Data |
---|---|---|
d3e88312 EM |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
d3e88312 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
d3e88312 EM |
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 |
d3e88312 EM |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Drupal specific stuff goes here | |
38 | */ | |
39 | class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { | |
40 | ||
7e9cadcf | 41 | /** |
100fef9d | 42 | * Create a user in Drupal. |
7e9cadcf | 43 | * |
77855840 | 44 | * @param array $params |
77855840 TO |
45 | * @param string $mail |
46 | * Email id for cms user. | |
7e9cadcf | 47 | * |
72b3a70c CW |
48 | * @return int|bool |
49 | * uid if user exists, false otherwise | |
7e9cadcf | 50 | */ |
00be9182 | 51 | public function createUser(&$params, $mail) { |
7e9cadcf EM |
52 | $user = \Drupal::currentUser(); |
53 | $user_register_conf = \Drupal::config('user.settings')->get('register'); | |
54 | $verify_mail_conf = \Drupal::config('user.settings')->get('verify_mail'); | |
55 | ||
56 | // Don't create user if we don't have permission to. | |
57 | if (!$user->hasPermission('administer users') && $user_register_conf == 'admin_only') { | |
58 | return FALSE; | |
59 | } | |
60 | ||
61 | $account = entity_create('user'); | |
62 | $account->setUsername($params['cms_name'])->setEmail($params[$mail]); | |
63 | ||
64 | // Allow user to set password only if they are an admin or if | |
65 | // the site settings don't require email verification. | |
66 | if (!$verify_mail_conf || $user->hasPermission('administer users')) { | |
67 | // @Todo: do we need to check that passwords match or assume this has already been done for us? | |
68 | $account->setPassword($params['cms_pass']); | |
69 | } | |
70 | ||
71 | // Only activate account if we're admin or if anonymous users don't require | |
72 | // approval to create accounts. | |
73 | if ($user_register_conf != 'visitors' && !$user->hasPermission('administer users')) { | |
74 | $account->block(); | |
75 | } | |
76 | ||
77 | // Validate the user object | |
78 | $violations = $account->validate(); | |
79 | if (count($violations)) { | |
80 | return FALSE; | |
81 | } | |
82 | ||
83 | try { | |
84 | $account->save(); | |
85 | } | |
86 | catch (\Drupal\Core\Entity\EntityStorageException $e) { | |
87 | return FALSE; | |
88 | } | |
89 | ||
90 | // Send off any emails as required. | |
91 | // Possible values for $op: | |
92 | // - 'register_admin_created': Welcome message for user created by the admin. | |
93 | // - 'register_no_approval_required': Welcome message when user | |
94 | // self-registers. | |
95 | // - 'register_pending_approval': Welcome message, user pending admin | |
96 | // approval. | |
97 | // @Todo: Should we only send off emails if $params['notify'] is set? | |
98 | switch (TRUE) { | |
99 | case $user_register_conf == 'admin_only' || $user->isAuthenticated(): | |
100 | _user_mail_notify('register_admin_created', $account); | |
101 | break; | |
e7292422 | 102 | |
7e9cadcf EM |
103 | case $user_register_conf == 'visitors': |
104 | _user_mail_notify('register_no_approval_required', $account); | |
105 | break; | |
e7292422 | 106 | |
7e9cadcf EM |
107 | case 'visitors_admin_approval': |
108 | _user_mail_notify('register_pending_approval', $account); | |
109 | break; | |
110 | } | |
111 | ||
112 | return $account->id(); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Update the Drupal user's email address. | |
117 | * | |
77855840 TO |
118 | * @param int $ufID |
119 | * User ID in CMS. | |
120 | * @param string $email | |
121 | * Primary contact email address. | |
7e9cadcf | 122 | */ |
00be9182 | 123 | public function updateCMSName($ufID, $email) { |
7e9cadcf EM |
124 | $user = user_load($ufID); |
125 | if ($user && $user->getEmail() != $email) { | |
126 | $user->setEmail($email); | |
127 | ||
128 | if (!count($user->validate())) { | |
129 | $user->save(); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | /** | |
135 | * Check if username and email exists in the drupal db | |
136 | * | |
77855840 TO |
137 | * @param array $params |
138 | * Array of name and mail values. | |
139 | * @param array $errors | |
140 | * Errors. | |
141 | * @param string $emailName | |
142 | * Field label for the 'email'. | |
7e9cadcf EM |
143 | * |
144 | * | |
145 | * @return void | |
146 | */ | |
00be9182 | 147 | public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { |
7e9cadcf EM |
148 | // If we are given a name, let's check to see if it already exists. |
149 | if (!empty($params['name'])) { | |
150 | $name = $params['name']; | |
151 | ||
152 | $user = entity_create('user'); | |
153 | $user->setUsername($name); | |
154 | ||
155 | // This checks for both username uniqueness and validity. | |
156 | $violations = iterator_to_array($user->validate()); | |
157 | // We only care about violations on the username field; discard the rest. | |
353ffa53 TO |
158 | $violations = array_filter($violations, function ($v) { |
159 | return $v->getPropertyPath() == 'name.0.value'; | |
e7292422 | 160 | }); |
7e9cadcf EM |
161 | if (count($violations) > 0) { |
162 | $errors['cms_name'] = $violations[0]->getMessage(); | |
163 | } | |
164 | } | |
165 | ||
166 | // And if we are given an email address, let's check to see if it already exists. | |
167 | if (!empty($params[$emailName])) { | |
168 | $mail = $params[$emailName]; | |
169 | ||
170 | $user = entity_create('user'); | |
171 | $user->setEmail($mail); | |
172 | ||
173 | // This checks for both email uniqueness. | |
174 | $violations = iterator_to_array($user->validate()); | |
175 | // We only care about violations on the email field; discard the rest. | |
353ffa53 TO |
176 | $violations = array_filter($violations, function ($v) { |
177 | return $v->getPropertyPath() == 'mail.0.value'; | |
e7292422 | 178 | }); |
7e9cadcf EM |
179 | if (count($violations) > 0) { |
180 | $errors[$emailName] = $violations[0]->getMessage(); | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | /** | |
100fef9d | 186 | * Get the drupal destination string. When this is passed in the |
7e9cadcf EM |
187 | * URL the user will be directed to it after filling in the drupal form |
188 | * | |
77855840 TO |
189 | * @param CRM_Core_Form $form |
190 | * Form object representing the 'current' form - to which the user will be returned. | |
a6c01b45 CW |
191 | * @return string |
192 | * destination value for URL | |
7e9cadcf | 193 | */ |
00be9182 | 194 | public function getLoginDestination(&$form) { |
7e9cadcf EM |
195 | $args = NULL; |
196 | ||
197 | $id = $form->get('id'); | |
198 | if ($id) { | |
199 | $args .= "&id=$id"; | |
200 | } | |
201 | else { | |
202 | $gid = $form->get('gid'); | |
203 | if ($gid) { | |
204 | $args .= "&gid=$gid"; | |
205 | } | |
206 | else { | |
207 | // Setup Personal Campaign Page link uses pageId | |
208 | $pageId = $form->get('pageId'); | |
209 | if ($pageId) { | |
210 | $component = $form->get('component'); | |
211 | $args .= "&pageId=$pageId&component=$component&action=add"; | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | $destination = NULL; | |
217 | if ($args) { | |
218 | // append destination so user is returned to form they came from after login | |
219 | $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args; | |
220 | } | |
221 | return $destination; | |
222 | } | |
223 | ||
d3e88312 EM |
224 | /** |
225 | * Get user login URL for hosting CMS (method declared in each CMS system class) | |
226 | * | |
77855840 TO |
227 | * @param string $destination |
228 | * If present, add destination to querystring (works for Drupal only). | |
d3e88312 | 229 | * |
a6c01b45 CW |
230 | * @return string |
231 | * loginURL for the current CMS | |
d3e88312 EM |
232 | */ |
233 | public function getLoginURL($destination = '') { | |
7e9cadcf EM |
234 | $query = $destination ? array('destination' => $destination) : array(); |
235 | return \Drupal::url('user.page', array(), array('query' => $query)); | |
236 | } | |
237 | ||
238 | ||
239 | /** | |
100fef9d | 240 | * Sets the title of the page |
7e9cadcf EM |
241 | * |
242 | * @param string $title | |
243 | * @param string $pageTitle | |
244 | * | |
245 | * @return void | |
7e9cadcf | 246 | */ |
00be9182 | 247 | public function setTitle($title, $pageTitle = NULL) { |
7e9cadcf EM |
248 | if (!$pageTitle) { |
249 | $pageTitle = $title; | |
250 | } | |
251 | ||
252 | \Drupal::service('civicrm.page_state')->setTitle($pageTitle); | |
253 | } | |
254 | ||
255 | /** | |
256 | * Append an additional breadcrumb tag to the existing breadcrumb | |
257 | * | |
258 | * @param $breadcrumbs | |
259 | * | |
260 | * @internal param string $title | |
261 | * @internal param string $url | |
262 | * | |
263 | * @return void | |
7e9cadcf | 264 | */ |
00be9182 | 265 | public function appendBreadCrumb($breadcrumbs) { |
7e9cadcf EM |
266 | $civicrmPageState = \Drupal::service('civicrm.page_state'); |
267 | foreach ($breadcrumbs as $breadcrumb) { | |
268 | $civicrmPageState->addBreadcrumb($breadcrumb['title'], $breadcrumb['url']); | |
269 | } | |
270 | } | |
271 | ||
272 | /** | |
273 | * Reset an additional breadcrumb tag to the existing breadcrumb | |
274 | * | |
275 | * @return void | |
7e9cadcf | 276 | */ |
00be9182 | 277 | public function resetBreadCrumb() { |
7e9cadcf EM |
278 | \Drupal::service('civicrm.page_state')->resetBreadcrumbs(); |
279 | } | |
280 | ||
281 | /** | |
282 | * Append a string to the head of the html file | |
283 | * | |
77855840 TO |
284 | * @param string $header |
285 | * The new string to be appended. | |
7e9cadcf EM |
286 | * |
287 | * @return void | |
7e9cadcf | 288 | */ |
00be9182 | 289 | public function addHTMLHead($header) { |
7e9cadcf EM |
290 | \Drupal::service('civicrm.page_state')->addHtmlHeader($header); |
291 | } | |
292 | ||
293 | /** | |
294 | * Add a script file | |
295 | * | |
353ffa53 | 296 | * @param $url : string, absolute path to file |
5a4f6742 CW |
297 | * @param string $region |
298 | * location within the document: 'html-header', 'page-header', 'page-footer'. | |
7e9cadcf EM |
299 | * |
300 | * Note: This function is not to be called directly | |
301 | * @see CRM_Core_Region::render() | |
302 | * | |
a6c01b45 CW |
303 | * @return bool |
304 | * TRUE if we support this operation in this CMS, FALSE otherwise | |
7e9cadcf EM |
305 | */ |
306 | public function addScriptUrl($url, $region) { | |
307 | $options = array('group' => JS_LIBRARY, 'weight' => 10); | |
308 | switch ($region) { | |
309 | case 'html-header': | |
310 | case 'page-footer': | |
311 | $options['scope'] = substr($region, 5); | |
312 | break; | |
e7292422 | 313 | |
7e9cadcf EM |
314 | default: |
315 | return FALSE; | |
316 | } | |
317 | // If the path is within the drupal directory we can use the more efficient 'file' setting | |
318 | $options['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; | |
319 | \Drupal::service('civicrm.page_state')->addJS($url, $options); | |
320 | return TRUE; | |
321 | } | |
322 | ||
323 | /** | |
324 | * Add an inline script | |
325 | * | |
353ffa53 | 326 | * @param $code : string, javascript code |
5a4f6742 CW |
327 | * @param string $region |
328 | * location within the document: 'html-header', 'page-header', 'page-footer'. | |
7e9cadcf EM |
329 | * |
330 | * Note: This function is not to be called directly | |
331 | * @see CRM_Core_Region::render() | |
332 | * | |
a6c01b45 CW |
333 | * @return bool |
334 | * TRUE if we support this operation in this CMS, FALSE otherwise | |
7e9cadcf EM |
335 | */ |
336 | public function addScript($code, $region) { | |
337 | $options = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10); | |
338 | switch ($region) { | |
339 | case 'html-header': | |
340 | case 'page-footer': | |
341 | $options['scope'] = substr($region, 5); | |
342 | break; | |
e7292422 | 343 | |
7e9cadcf EM |
344 | default: |
345 | return FALSE; | |
346 | } | |
347 | \Drupal::service('civicrm.page_state')->addJS($code, $options); | |
348 | return TRUE; | |
349 | } | |
350 | ||
351 | /** | |
352 | * Add a css file | |
353 | * | |
353ffa53 | 354 | * @param $url : string, absolute path to file |
5a4f6742 CW |
355 | * @param string $region |
356 | * location within the document: 'html-header', 'page-header', 'page-footer'. | |
7e9cadcf EM |
357 | * |
358 | * Note: This function is not to be called directly | |
359 | * @see CRM_Core_Region::render() | |
360 | * | |
a6c01b45 CW |
361 | * @return bool |
362 | * TRUE if we support this operation in this CMS, FALSE otherwise | |
7e9cadcf EM |
363 | */ |
364 | public function addStyleUrl($url, $region) { | |
365 | if ($region != 'html-header') { | |
366 | return FALSE; | |
d3e88312 | 367 | } |
7e9cadcf EM |
368 | $options = array(); |
369 | // If the path is within the drupal directory we can use the more efficient 'file' setting | |
370 | $options['type'] = $this->formatResourceUrl($url) ? 'file' : 'external'; | |
371 | \Drupal::service('civicrm.page_state')->addCSS($url, $options); | |
372 | return TRUE; | |
d3e88312 EM |
373 | } |
374 | ||
7e9cadcf EM |
375 | /** |
376 | * Add an inline style | |
377 | * | |
353ffa53 | 378 | * @param $code : string, css code |
5a4f6742 CW |
379 | * @param string $region |
380 | * location within the document: 'html-header', 'page-header', 'page-footer'. | |
7e9cadcf EM |
381 | * |
382 | * Note: This function is not to be called directly | |
383 | * @see CRM_Core_Region::render() | |
384 | * | |
a6c01b45 CW |
385 | * @return bool |
386 | * TRUE if we support this operation in this CMS, FALSE otherwise | |
7e9cadcf EM |
387 | */ |
388 | public function addStyle($code, $region) { | |
389 | if ($region != 'html-header') { | |
390 | return FALSE; | |
391 | } | |
392 | $options = array('type' => 'inline'); | |
393 | \Drupal::service('civicrm.page_state')->addCSS($code, $options); | |
394 | return TRUE; | |
395 | } | |
396 | ||
397 | /** | |
398 | * Check if a resource url is within the drupal directory and format appropriately | |
399 | * | |
400 | * This seems to be a legacy function. We assume all resources are within the drupal | |
401 | * directory and always return TRUE. As well, we clean up the $url. | |
402 | * | |
403 | * @param $url | |
404 | * | |
405 | * @return bool | |
406 | */ | |
00be9182 | 407 | public function formatResourceUrl(&$url) { |
7e9cadcf EM |
408 | // Remove leading slash if present. |
409 | $url = ltrim($url, '/'); | |
410 | ||
411 | // Remove query string — presumably added to stop intermediary caching. | |
412 | if (($pos = strpos($url, '?')) !== FALSE) { | |
413 | $url = substr($url, 0, $pos); | |
414 | } | |
415 | ||
416 | return TRUE; | |
417 | } | |
418 | ||
419 | /** | |
420 | * Rewrite various system urls to https | |
421 | * | |
422 | * This function does nothing in Drupal 8. Changes to the base_url should be made | |
423 | * in settings.php directly. | |
424 | * | |
425 | * @param null | |
426 | * | |
427 | * @return void | |
7e9cadcf | 428 | */ |
00be9182 | 429 | public function mapConfigToSSL() { |
7e9cadcf EM |
430 | } |
431 | ||
432 | /** | |
77855840 TO |
433 | * @param string $path |
434 | * The base path (eg. civicrm/search/contact). | |
435 | * @param string $query | |
436 | * The query string (eg. reset=1&cid=66) but html encoded(?) (optional). | |
437 | * @param bool $absolute | |
438 | * Produce an absolute including domain and protocol (optional). | |
439 | * @param string $fragment | |
440 | * A named anchor (optional). | |
441 | * @param bool $htmlize | |
442 | * Produce a html encoded url (optional). | |
443 | * @param bool $frontend | |
444 | * A joomla hack (unused). | |
445 | * @param bool $forceBackend | |
446 | * A joomla jack (unused). | |
7e9cadcf EM |
447 | * @return string |
448 | */ | |
00be9182 | 449 | public function url($path = '', $query = '', $absolute = FALSE, $fragment = '', $htmlize = FALSE, $frontend = FALSE, $forceBackend = FALSE) { |
7e9cadcf EM |
450 | $query = html_entity_decode($query); |
451 | $url = \Drupal\civicrm\CivicrmHelper::parseURL("{$path}?{$query}"); | |
452 | ||
453 | try { | |
454 | $url = \Drupal::url($url['route_name'], array(), array( | |
455 | 'query' => $url['query'], | |
456 | 'absolute' => $absolute, | |
457 | 'fragment' => $fragment, | |
458 | )); | |
459 | } | |
460 | catch (Exception $e) { | |
461 | $url = ''; | |
462 | } | |
463 | ||
464 | if ($htmlize) { | |
465 | $url = htmlentities($url); | |
466 | } | |
467 | return $url; | |
468 | } | |
469 | ||
470 | ||
471 | /** | |
472 | * Authenticate the user against the drupal db | |
473 | * | |
77855840 TO |
474 | * @param string $name |
475 | * The user name. | |
476 | * @param string $password | |
477 | * The password for the above user name. | |
478 | * @param bool $loadCMSBootstrap | |
479 | * Load cms bootstrap?. | |
7e9cadcf EM |
480 | * @param NULL|string $realPath filename of script |
481 | * | |
72b3a70c CW |
482 | * @return array|bool |
483 | * [contactID, ufID, uniqueString] if success else false if no auth | |
7e9cadcf EM |
484 | * |
485 | * This always bootstraps Drupal | |
486 | */ | |
00be9182 | 487 | public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) { |
7e9cadcf EM |
488 | (new CRM_Utils_System_Drupal8())->loadBootStrap(array(), FALSE); |
489 | ||
490 | $uid = \Drupal::service('user.auth')->authenticate($name, $password); | |
491 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
492 | ||
493 | return array($contact_id, $uid, mt_rand()); | |
494 | } | |
495 | ||
496 | /** | |
497 | * Load user into session | |
498 | */ | |
00be9182 | 499 | public function loadUser($username) { |
7e9cadcf EM |
500 | $user = user_load_by_name($username); |
501 | if (!$user) { | |
502 | return FALSE; | |
503 | } | |
504 | ||
505 | // Set Drupal's current user to the loaded user. | |
506 | \Drupal::currentUser()->setAccount($user); | |
507 | ||
508 | $uid = $user->id(); | |
509 | $contact_id = CRM_Core_BAO_UFMatch::getContactId($uid); | |
510 | ||
511 | // Store the contact id and user id in the session | |
512 | $session = CRM_Core_Session::singleton(); | |
513 | $session->set('ufID', $uid); | |
514 | $session->set('userID', $contact_id); | |
515 | return TRUE; | |
516 | } | |
517 | ||
518 | /** | |
519 | * Determine the native ID of the CMS user | |
520 | * | |
100fef9d | 521 | * @param string $username |
7e9cadcf EM |
522 | * @return int|NULL |
523 | */ | |
00be9182 | 524 | public function getUfId($username) { |
7e9cadcf EM |
525 | if ($id = user_load_by_name($username)->id()) { |
526 | return $id; | |
527 | } | |
528 | } | |
529 | ||
530 | /** | |
531 | * Set a message in the UF to display to a user | |
532 | * | |
77855840 TO |
533 | * @param string $message |
534 | * The message to set. | |
7e9cadcf | 535 | */ |
00be9182 | 536 | public function setMessage($message) { |
7e9cadcf EM |
537 | drupal_set_message($message); |
538 | } | |
539 | ||
00be9182 | 540 | public function permissionDenied() { |
7e9cadcf EM |
541 | \Drupal::service('civicrm.page_state')->setAccessDenied(); |
542 | } | |
543 | ||
544 | /** | |
545 | * In previous versions, this function was the controller for logging out. In Drupal 8, we rewrite the route | |
546 | * to hand off logout to the standard Drupal logout controller. This function should therefore never be called. | |
547 | */ | |
00be9182 | 548 | public function logout() { |
7e9cadcf EM |
549 | // Pass |
550 | } | |
551 | ||
552 | /** | |
100fef9d | 553 | * Load drupal bootstrap |
7e9cadcf | 554 | * |
77855840 TO |
555 | * @param array $params |
556 | * Either uid, or name & pass. | |
557 | * @param bool $loadUser | |
558 | * Boolean Require CMS user load. | |
559 | * @param bool $throwError | |
560 | * If true, print error on failure and exit. | |
561 | * @param bool|string $realPath path to script | |
7e9cadcf EM |
562 | * |
563 | * @return bool | |
564 | * @Todo Handle setting cleanurls configuration for CiviCRM? | |
565 | */ | |
00be9182 | 566 | public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) { |
7e9cadcf | 567 | static $run_once = FALSE; |
a3e55d9c TO |
568 | if ($run_once) { |
569 | return TRUE; | |
0db6c3e1 TO |
570 | } |
571 | else { | |
a3e55d9c | 572 | $run_once = TRUE; |
e7292422 | 573 | } |
7e9cadcf EM |
574 | |
575 | if (!($root = $this->cmsRootPath())) { | |
576 | return FALSE; | |
577 | } | |
578 | chdir($root); | |
579 | ||
580 | // Create a mock $request object | |
581 | $autoloader = require_once $root . '/core/vendor/autoload.php'; | |
582 | // @Todo: do we need to handle case where $_SERVER has no HTTP_HOST key, ie. when run via cli? | |
583 | $request = new \Symfony\Component\HttpFoundation\Request(array(), array(), array(), array(), array(), $_SERVER); | |
584 | ||
585 | // Create a kernel and boot it. | |
586 | \Drupal\Core\DrupalKernel::createFromRequest($request, $autoloader, 'prod')->prepareLegacyRequest($request); | |
587 | ||
588 | // Initialize Civicrm | |
589 | \Drupal::service('civicrm'); | |
590 | ||
591 | // We need to call the config hook again, since we now know | |
592 | // all the modules that are listening on it (CRM-8655). | |
593 | CRM_Utils_Hook::config($config); | |
594 | ||
595 | if ($loadUser) { | |
596 | if (!empty($params['uid']) && $username = \Drupal\user\Entity\User::load($uid)->getUsername()) { | |
597 | $this->loadUser($username); | |
598 | } | |
599 | elseif (!empty($params['name']) && !empty($params['pass']) && $this->authenticate($params['name'], $params['pass'])) { | |
600 | $this->loadUser($params['name']); | |
601 | } | |
602 | } | |
603 | return TRUE; | |
604 | } | |
605 | ||
606 | /** | |
607 | * Determine the location of the CMS root. | |
608 | * @param null $path | |
609 | * | |
610 | * @return NULL|string | |
611 | */ | |
00be9182 | 612 | public function cmsRootPath($path = NULL) { |
7e9cadcf EM |
613 | if (defined('DRUPAL_ROOT')) { |
614 | return DRUPAL_ROOT; | |
615 | } | |
616 | ||
617 | // It looks like Drupal hasn't been bootstrapped. | |
618 | // We're going to attempt to discover the root Drupal path | |
619 | // by climbing out of the folder hierarchy and looking around to see | |
620 | // if we've found the Drupal root directory. | |
621 | if (!$path) { | |
622 | $path = $_SERVER['SCRIPT_FILENAME']; | |
623 | } | |
624 | ||
625 | // Normalize and explode path into its component paths. | |
626 | $paths = explode(DIRECTORY_SEPARATOR, realpath($path)); | |
627 | ||
628 | // Remove script filename from array of directories. | |
629 | array_pop($paths); | |
630 | ||
631 | while (count($paths)) { | |
632 | $candidate = implode('/', $paths); | |
633 | if (file_exists($candidate . "/core/includes/bootstrap.inc")) { | |
634 | return $candidate; | |
635 | } | |
636 | ||
637 | array_pop($paths); | |
638 | } | |
639 | } | |
640 | ||
641 | /** | |
642 | * Check if user is logged in. | |
643 | * | |
644 | * @return bool | |
645 | */ | |
646 | public function isUserLoggedIn() { | |
647 | return \Drupal::currentUser()->isAuthenticated(); | |
648 | } | |
649 | ||
650 | /** | |
651 | * Get currently logged in user uf id. | |
652 | * | |
a6c01b45 CW |
653 | * @return int |
654 | * $userID logged in user uf id. | |
7e9cadcf EM |
655 | */ |
656 | public function getLoggedInUfID() { | |
657 | if ($id = \Drupal::currentUser()->id()) { | |
658 | return $id; | |
659 | } | |
660 | } | |
624142d4 EM |
661 | |
662 | /** | |
663 | * Get the default location for CiviCRM blocks | |
664 | * | |
665 | * @return string | |
624142d4 | 666 | */ |
00be9182 | 667 | public function getDefaultBlockLocation() { |
624142d4 EM |
668 | return 'sidebar_first'; |
669 | } | |
7e9cadcf | 670 | } |