INFRA-132 - Remove extra newlines from the bottom of docblocks
[civicrm-core.git] / CRM / Utils / System / Drupal8.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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
41 /**
42 * Create a user in Drupal.
43 *
44 * @param array $params
45 * Associated array.
46 * @param string $mail
47 * Email id for cms user.
48 *
49 * @return uid if user exists, false otherwise
50 */
51 public function createUser(&$params, $mail) {
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;
102
103 case $user_register_conf == 'visitors':
104 _user_mail_notify('register_no_approval_required', $account);
105 break;
106
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 *
118 * @param int $ufID
119 * User ID in CMS.
120 * @param string $email
121 * Primary contact email address.
122 */
123 public function updateCMSName($ufID, $email) {
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 *
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'.
143 *
144 *
145 * @return void
146 */
147 public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
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.
158 $violations = array_filter($violations, function ($v) { return $v->getPropertyPath() == 'name.0.value';
159 });
160 if (count($violations) > 0) {
161 $errors['cms_name'] = $violations[0]->getMessage();
162 }
163 }
164
165 // And if we are given an email address, let's check to see if it already exists.
166 if (!empty($params[$emailName])) {
167 $mail = $params[$emailName];
168
169 $user = entity_create('user');
170 $user->setEmail($mail);
171
172 // This checks for both email uniqueness.
173 $violations = iterator_to_array($user->validate());
174 // We only care about violations on the email field; discard the rest.
175 $violations = array_filter($violations, function ($v) { return $v->getPropertyPath() == 'mail.0.value';
176 });
177 if (count($violations) > 0) {
178 $errors[$emailName] = $violations[0]->getMessage();
179 }
180 }
181 }
182
183 /**
184 * Get the drupal destination string. When this is passed in the
185 * URL the user will be directed to it after filling in the drupal form
186 *
187 * @param CRM_Core_Form $form
188 * Form object representing the 'current' form - to which the user will be returned.
189 * @return string
190 * destination value for URL
191 */
192 public function getLoginDestination(&$form) {
193 $args = NULL;
194
195 $id = $form->get('id');
196 if ($id) {
197 $args .= "&id=$id";
198 }
199 else {
200 $gid = $form->get('gid');
201 if ($gid) {
202 $args .= "&gid=$gid";
203 }
204 else {
205 // Setup Personal Campaign Page link uses pageId
206 $pageId = $form->get('pageId');
207 if ($pageId) {
208 $component = $form->get('component');
209 $args .= "&pageId=$pageId&component=$component&action=add";
210 }
211 }
212 }
213
214 $destination = NULL;
215 if ($args) {
216 // append destination so user is returned to form they came from after login
217 $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args;
218 }
219 return $destination;
220 }
221
222 /**
223 * Get user login URL for hosting CMS (method declared in each CMS system class)
224 *
225 * @param string $destination
226 * If present, add destination to querystring (works for Drupal only).
227 *
228 * @return string
229 * loginURL for the current CMS
230 * @static
231 */
232 public function getLoginURL($destination = '') {
233 $query = $destination ? array('destination' => $destination) : array();
234 return \Drupal::url('user.page', array(), array('query' => $query));
235 }
236
237
238 /**
239 * Sets the title of the page
240 *
241 * @param string $title
242 * @param string $pageTitle
243 *
244 * @return void
245 */
246 public function setTitle($title, $pageTitle = NULL) {
247 if (!$pageTitle) {
248 $pageTitle = $title;
249 }
250
251 \Drupal::service('civicrm.page_state')->setTitle($pageTitle);
252 }
253
254 /**
255 * Append an additional breadcrumb tag to the existing breadcrumb
256 *
257 * @param $breadcrumbs
258 *
259 * @internal param string $title
260 * @internal param string $url
261 *
262 * @return void
263 */
264 public function appendBreadCrumb($breadcrumbs) {
265 $civicrmPageState = \Drupal::service('civicrm.page_state');
266 foreach ($breadcrumbs as $breadcrumb) {
267 $civicrmPageState->addBreadcrumb($breadcrumb['title'], $breadcrumb['url']);
268 }
269 }
270
271 /**
272 * Reset an additional breadcrumb tag to the existing breadcrumb
273 *
274 * @return void
275 */
276 public function resetBreadCrumb() {
277 \Drupal::service('civicrm.page_state')->resetBreadcrumbs();
278 }
279
280 /**
281 * Append a string to the head of the html file
282 *
283 * @param string $header
284 * The new string to be appended.
285 *
286 * @return void
287 */
288 public function addHTMLHead($header) {
289 \Drupal::service('civicrm.page_state')->addHtmlHeader($header);
290 }
291
292 /**
293 * Add a script file
294 *
295 * @param $url: string, absolute path to file
296 * @param string $region
297 * location within the document: 'html-header', 'page-header', 'page-footer'.
298 *
299 * Note: This function is not to be called directly
300 * @see CRM_Core_Region::render()
301 *
302 * @return bool
303 * TRUE if we support this operation in this CMS, FALSE otherwise
304 */
305 public function addScriptUrl($url, $region) {
306 $options = array('group' => JS_LIBRARY, 'weight' => 10);
307 switch ($region) {
308 case 'html-header':
309 case 'page-footer':
310 $options['scope'] = substr($region, 5);
311 break;
312
313 default:
314 return FALSE;
315 }
316 // If the path is within the drupal directory we can use the more efficient 'file' setting
317 $options['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
318 \Drupal::service('civicrm.page_state')->addJS($url, $options);
319 return TRUE;
320 }
321
322 /**
323 * Add an inline script
324 *
325 * @param $code: string, javascript code
326 * @param string $region
327 * location within the document: 'html-header', 'page-header', 'page-footer'.
328 *
329 * Note: This function is not to be called directly
330 * @see CRM_Core_Region::render()
331 *
332 * @return bool
333 * TRUE if we support this operation in this CMS, FALSE otherwise
334 */
335 public function addScript($code, $region) {
336 $options = array('type' => 'inline', 'group' => JS_LIBRARY, 'weight' => 10);
337 switch ($region) {
338 case 'html-header':
339 case 'page-footer':
340 $options['scope'] = substr($region, 5);
341 break;
342
343 default:
344 return FALSE;
345 }
346 \Drupal::service('civicrm.page_state')->addJS($code, $options);
347 return TRUE;
348 }
349
350 /**
351 * Add a css file
352 *
353 * @param $url: string, absolute path to file
354 * @param string $region
355 * location within the document: 'html-header', 'page-header', 'page-footer'.
356 *
357 * Note: This function is not to be called directly
358 * @see CRM_Core_Region::render()
359 *
360 * @return bool
361 * TRUE if we support this operation in this CMS, FALSE otherwise
362 */
363 public function addStyleUrl($url, $region) {
364 if ($region != 'html-header') {
365 return FALSE;
366 }
367 $options = array();
368 // If the path is within the drupal directory we can use the more efficient 'file' setting
369 $options['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
370 \Drupal::service('civicrm.page_state')->addCSS($url, $options);
371 return TRUE;
372 }
373
374 /**
375 * Add an inline style
376 *
377 * @param $code: string, css code
378 * @param string $region
379 * location within the document: 'html-header', 'page-header', 'page-footer'.
380 *
381 * Note: This function is not to be called directly
382 * @see CRM_Core_Region::render()
383 *
384 * @return bool
385 * TRUE if we support this operation in this CMS, FALSE otherwise
386 */
387 public function addStyle($code, $region) {
388 if ($region != 'html-header') {
389 return FALSE;
390 }
391 $options = array('type' => 'inline');
392 \Drupal::service('civicrm.page_state')->addCSS($code, $options);
393 return TRUE;
394 }
395
396 /**
397 * Check if a resource url is within the drupal directory and format appropriately
398 *
399 * This seems to be a legacy function. We assume all resources are within the drupal
400 * directory and always return TRUE. As well, we clean up the $url.
401 *
402 * @param $url
403 *
404 * @return bool
405 */
406 public function formatResourceUrl(&$url) {
407 // Remove leading slash if present.
408 $url = ltrim($url, '/');
409
410 // Remove query string — presumably added to stop intermediary caching.
411 if (($pos = strpos($url, '?')) !== FALSE) {
412 $url = substr($url, 0, $pos);
413 }
414
415 return TRUE;
416 }
417
418 /**
419 * Rewrite various system urls to https
420 *
421 * This function does nothing in Drupal 8. Changes to the base_url should be made
422 * in settings.php directly.
423 *
424 * @param null
425 *
426 * @return void
427 */
428 public function mapConfigToSSL() {
429 }
430
431 /**
432 * @param string $path
433 * The base path (eg. civicrm/search/contact).
434 * @param string $query
435 * The query string (eg. reset=1&cid=66) but html encoded(?) (optional).
436 * @param bool $absolute
437 * Produce an absolute including domain and protocol (optional).
438 * @param string $fragment
439 * A named anchor (optional).
440 * @param bool $htmlize
441 * Produce a html encoded url (optional).
442 * @param bool $frontend
443 * A joomla hack (unused).
444 * @param bool $forceBackend
445 * A joomla jack (unused).
446 * @return string
447 */
448 public function url($path = '', $query = '', $absolute = FALSE, $fragment = '', $htmlize = FALSE, $frontend = FALSE, $forceBackend = FALSE) {
449 $query = html_entity_decode($query);
450 $url = \Drupal\civicrm\CivicrmHelper::parseURL("{$path}?{$query}");
451
452 try {
453 $url = \Drupal::url($url['route_name'], array(), array(
454 'query' => $url['query'],
455 'absolute' => $absolute,
456 'fragment' => $fragment,
457 ));
458 }
459 catch (Exception $e) {
460 $url = '';
461 }
462
463 if ($htmlize) {
464 $url = htmlentities($url);
465 }
466 return $url;
467 }
468
469
470 /**
471 * Authenticate the user against the drupal db
472 *
473 * @param string $name
474 * The user name.
475 * @param string $password
476 * The password for the above user name.
477 * @param bool $loadCMSBootstrap
478 * Load cms bootstrap?.
479 * @param NULL|string $realPath filename of script
480 *
481 * @return mixed false if no auth
482 * array(
483 * contactID, ufID, unique string ) if success
484 *
485 * This always bootstraps Drupal
486 */
487 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
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 */
499 public function loadUser($username) {
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 *
521 * @param string $username
522 * @return int|NULL
523 */
524 public function getUfId($username) {
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 *
533 * @param string $message
534 * The message to set.
535 */
536 public function setMessage($message) {
537 drupal_set_message($message);
538 }
539
540 public function permissionDenied() {
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 */
548 public function logout() {
549 // Pass
550 }
551
552 /**
553 * Load drupal bootstrap
554 *
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
562 *
563 * @return bool
564 * @Todo Handle setting cleanurls configuration for CiviCRM?
565 */
566 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
567 static $run_once = FALSE;
568 if ($run_once) {
569 return TRUE;
570 }
571 else {
572 $run_once = TRUE;
573 }
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 */
612 public function cmsRootPath($path = NULL) {
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 *
653 * @return int
654 * $userID logged in user uf id.
655 */
656 public function getLoggedInUfID() {
657 if ($id = \Drupal::currentUser()->id()) {
658 return $id;
659 }
660 }
661
662 /**
663 * Get the default location for CiviCRM blocks
664 *
665 * @return string
666 */
667 public function getDefaultBlockLocation() {
668 return 'sidebar_first';
669 }
670 }