Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21088: Changing drupal calls to backdrop calls #10884

Merged
merged 1 commit into from Aug 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
166 changes: 146 additions & 20 deletions CRM/Utils/System/Backdrop.php
Expand Up @@ -73,12 +73,12 @@ public function createUser(&$params, $mail) {
// we also need to redirect b
$config->inCiviCRM = TRUE;

$form = drupal_retrieve_form('user_register_form', $form_state);
$form = backdrop_retrieve_form('user_register_form', $form_state);
$form_state['process_input'] = 1;
$form_state['submitted'] = 1;
$form['#array_parents'] = array();
$form['#tree'] = FALSE;
drupal_process_form('user_register_form', $form, $form_state);
backdrop_process_form('user_register_form', $form, $form_state);

$config->inCiviCRM = FALSE;

Expand All @@ -103,7 +103,7 @@ public function updateCMSName($ufID, $email) {
}

/**
* Check if username and email exists in the drupal db.
* Check if username and email exists in the Backdrop db.
*
* @param array $params
* Array of name and mail values.
Expand All @@ -115,7 +115,7 @@ public function updateCMSName($ufID, $email) {
public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
$errors = form_get_errors();
if ($errors) {
// unset drupal messages to avoid twice display of errors
// unset Backdrop messages to avoid twice display of errors
unset($_SESSION['messages']);
}

Expand Down Expand Up @@ -164,15 +164,15 @@ public function setTitle($title, $pageTitle = NULL) {
$pageTitle = $title;
}

drupal_set_title($pageTitle, PASS_THROUGH);
backdrop_set_title($pageTitle, PASS_THROUGH);
}
}

/**
* @inheritDoc
*/
public function appendBreadCrumb($breadCrumbs) {
$breadCrumb = drupal_get_breadcrumb();
$breadCrumb = backdrop_get_breadcrumb();

if (is_array($breadCrumbs)) {
foreach ($breadCrumbs as $crumbs) {
Expand All @@ -190,15 +190,15 @@ public function appendBreadCrumb($breadCrumbs) {
$breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
}
}
drupal_set_breadcrumb($breadCrumb);
backdrop_set_breadcrumb($breadCrumb);
}

/**
* @inheritDoc
*/
public function resetBreadCrumb() {
$bc = array();
drupal_set_breadcrumb($bc);
backdrop_set_breadcrumb($bc);
}

/**
Expand All @@ -212,7 +212,7 @@ public function addHTMLHead($header) {
'#type' => 'markup',
'#markup' => $header,
);
drupal_add_html_head($data, $key);
backdrop_add_html_head($data, $key);
}
}

Expand All @@ -230,9 +230,9 @@ public function addScriptUrl($url, $region) {
default:
return FALSE;
}
// If the path is within the drupal directory we can use the more efficient 'file' setting
// If the path is within the Backdrop directory we can use the more efficient 'file' setting
$params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
drupal_add_js($url, $params);
backdrop_add_js($url, $params);
return TRUE;
}

Expand All @@ -250,7 +250,7 @@ public function addScript($code, $region) {
default:
return FALSE;
}
drupal_add_js($code, $params);
backdrop_add_js($code, $params);
return TRUE;
}

Expand All @@ -262,9 +262,9 @@ public function addStyleUrl($url, $region) {
return FALSE;
}
$params = array();
// If the path is within the drupal directory we can use the more efficient 'file' setting
// If the path is within the Backdrop directory we can use the more efficient 'file' setting
$params['type'] = $this->formatResourceUrl($url) ? 'file' : 'external';
drupal_add_css($url, $params);
backdrop_add_css($url, $params);
return TRUE;
}

Expand All @@ -276,7 +276,7 @@ public function addStyle($code, $region) {
return FALSE;
}
$params = array('type' => 'inline');
drupal_add_css($code, $params);
backdrop_add_css($code, $params);
return TRUE;
}

Expand Down Expand Up @@ -331,7 +331,7 @@ public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realP
}
else {
// CRM-8638
// SOAP cannot load drupal bootstrap and hence we do it the old way
// SOAP cannot load Backdrop bootstrap and hence we do it the old way
// Contact CiviSMTP folks if we run into issues with this :)
$cmsPath = $this->cmsRootPath();

Expand Down Expand Up @@ -414,8 +414,8 @@ public function userLoginFinalize($params = array()) {
* @inheritDoc
*/
public function getUFLocale() {
// return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
// (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
// return CiviCRM’s xx_YY locale that either matches Backdrop’s Chinese locale
// (for CRM-6281), Backdrop’s xx_YY or is retrieved based on Backdrop’s xx
// sometimes for CLI based on order called, this might not be set and/or empty
global $language;

Expand Down Expand Up @@ -515,11 +515,11 @@ public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError =
}
return FALSE;
}
// load drupal bootstrap
// load Backdrop bootstrap
chdir($cmsPath);
define('BACKDROP_ROOT', $cmsPath);

// For drupal multi-site CRM-11313
// For Backdrop multi-site CRM-11313
if ($realPath && strpos($realPath, 'sites/all/modules/') === FALSE) {
preg_match('@sites/([^/]*)/modules@s', $realPath, $matches);
if (!empty($matches[1])) {
Expand Down Expand Up @@ -890,4 +890,130 @@ public function permissionEmails($permissionName) {
return array();
}

/**
* @inheritdoc
*/
public function getDefaultFileStorage() {
$config = CRM_Core_Config::singleton();
$baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);

$siteName = $this->parseBackdropSiteNameFromRequest('/files/civicrm');
if ($siteName) {
$filesURL = $baseURL . "sites/$siteName/files/civicrm/";
}
else {
$filesURL = $baseURL . "files/civicrm/";
}

return array(
'url' => $filesURL,
'path' => CRM_Utils_File::baseFilePath(),
);
}

/**
* Check if a resource url is within the Backdrop directory and format appropriately.
*
* @param $url (reference)
*
* @return bool
* TRUE for internal paths, FALSE for external. The backdrop_add_js fn is able to add js more
* efficiently if it is known to be in the Backdrop site
*/
public function formatResourceUrl(&$url) {
$internal = FALSE;
$base = CRM_Core_Config::singleton()->resourceBase;
global $base_url;
// Handle absolute urls
// compares $url (which is some unknown/untrusted value from a third-party dev) to the CMS's base url (which is independent of civi's url)
// to see if the url is within our Backdrop dir, if it is we are able to treated it as an internal url
if (strpos($url, $base_url) === 0) {
$file = trim(str_replace($base_url, '', $url), '/');
// CRM-18130: Custom CSS URL not working if aliased or rewritten
if (file_exists(BACKDROP_ROOT . $file)) {
$url = $file;
$internal = TRUE;
}
}
// Handle relative urls that are within the CiviCRM module directory
elseif (strpos($url, $base) === 0) {
$internal = TRUE;
$url = $this->appendCoreDirectoryToResourceBase(dirname(backdrop_get_path('module', 'civicrm')) . '/') . trim(substr($url, strlen($base)), '/');
}
// Strip query string
$q = strpos($url, '?');
if ($q && $internal) {
$url = substr($url, 0, $q);
}
return $internal;
}

/**
* @inheritDoc
*/
public function setMessage($message) {
backdrop_set_message($message);
}

/**
* @inheritDoc
*/
public function permissionDenied() {
backdrop_access_denied();
}

/**
* @inheritDoc
*/
public function flush() {
backdrop_flush_all_caches();
}

/**
* Determine if Backdrop multi-site applies to the current request -- and,
* specifically, determine the name of the multisite folder.
*
* @param string $flagFile
* Check if $flagFile exists inside the site dir.
* @return null|string
* string, e.g. `bar.example.com` if using multisite.
* NULL if using the default site.
*/
private function parseBackdropSiteNameFromRequest($flagFile = '') {
$phpSelf = array_key_exists('PHP_SELF', $_SERVER) ? $_SERVER['PHP_SELF'] : '';
$httpHost = array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : '';
if (empty($httpHost)) {
$httpHost = parse_url(CIVICRM_UF_BASEURL, PHP_URL_HOST);
if (parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT)) {
$httpHost .= ':' . parse_url(CIVICRM_UF_BASEURL, PHP_URL_PORT);
}
}

$confdir = $this->cmsRootPath() . '/sites';

if (file_exists($confdir . "/sites.php")) {
include $confdir . "/sites.php";
}
else {
$sites = array();
}

$uri = explode('/', $phpSelf);
$server = explode('.', implode('.', array_reverse(explode(':', rtrim($httpHost, '.')))));
for ($i = count($uri) - 1; $i > 0; $i--) {
for ($j = count($server); $j > 0; $j--) {
$dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
if (file_exists("$confdir/$dir" . $flagFile)) {
\Civi::$statics[__CLASS__]['drupalSiteName'] = $dir;
return \Civi::$statics[__CLASS__]['drupalSiteName'];
}
// check for alias
if (isset($sites[$dir]) && file_exists("$confdir/{$sites[$dir]}" . $flagFile)) {
\Civi::$statics[__CLASS__]['drupalSiteName'] = $sites[$dir];
return \Civi::$statics[__CLASS__]['drupalSiteName'];
}
}
}
}

}