CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions (Moderator: Dave Greenberg) »
  • Avoiding hardcoded filepaths in civicrm.settings.php
Pages: [1]

Author Topic: Avoiding hardcoded filepaths in civicrm.settings.php  (Read 1869 times)

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Avoiding hardcoded filepaths in civicrm.settings.php
November 20, 2012, 04:34:51 pm
Currently in civicrm.settings.php you need to hardcode things like the civicrm root path and the civicrm conf dir.
You need to change this if you move the civicrm install to a different directory.

Solution:

Code: [Select]
global $civicrm_root;

if (function_exists('drupal_get_path') && function_exists('conf_path')) {
  $drupal_root = DRUPAL_ROOT;
  $civicrm_module_dir = drupal_get_path('module', 'civicrm');
  if (preg_match('#^(.*/)drupal$#', $civicrm_module_dir, $m)) {
    $civicrm_root = $m[1];
  }
  else {
    throw new Exception('Unable to determine CiviCRM root path.');
  }
  $civicrm_conf_dir = conf_path();
}
elseif (preg_match('#^((.*)/sites/([^/]+)/modules/(|[^/]+/)civicrm)/extern/([^/]+)\.php$#', $_SERVER['SCRIPT_FILENAME'], $m)) {
  $drupal_root = $m[2];
  $civicrm_root = $m[1] . '/';
  if ($m[3] === 'all') {
    $civicrm_conf_dir = $m[2] . '/sites/default';
  }
  else {
    $civicrm_conf_dir = $m[2] . '/sites/' . $m[3];
  }
}
else {
  throw new Exception('Unable to determine CiviCRM root path.');
}

define( 'CIVICRM_TEMPLATE_COMPILEDIR', $civicrm_conf_dir . '/files/civicrm/templates_c/' );

This allows to determine all you need both when running it from Drupal, and when running it from /extern/.

Maybe there is a better place for this than civicrm.settings.php..

Note:
$_SERVER['SCRIPT_FILENAME'] is better than __FILE__, because it allows having civicrm in a symlinked directory.

Maybe I missed something?
Do we need to support things other than the /extern/ scripts?

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: Avoiding hardcoded filepaths in civicrm.settings.php
November 20, 2012, 04:36:30 pm
And yes, that's pure heuristic. But probably works in 99%.
In the rare edge case, one could still override the paths in civicrm.settings.php.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Avoiding hardcoded filepaths in civicrm.settings.php
November 20, 2012, 10:31:45 pm
Pretty risky assumption to use site/defaults for the config, it will fail/destroy silently if multisite.

I prefer the way it fails early now.

The normal solution is to create a settings_location.php file in your civicrm_root

Code: [Select]
<?php

// the directory name for your 'sites' location in drupal. We use the below location
// along with drupal 'conf_init()' function to figure out where the settings files
// are located. This is primarily used when we hit CiviCRM outside of 'Drupal' (via mail/soap
// etc)
define( 'CIVICRM_CONFDIR', '/var/www/drupal/4.2/sites' );


works fine. Might be worthwhile to modify the error message to point out to this solution
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

donquixote

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 3
  • CiviCRM version: none
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: Avoiding hardcoded filepaths in civicrm.settings.php
November 22, 2012, 11:08:21 am
Ok..
so actually we have two situations here:
1. civicrm/extern/*.php stuff
2. Calls from within Drupal.

For 1., our assumption of sites/default might indeed be too risky.
However, I wonder how good an idea it is in the first place, to have this extern/*.php, why not make those things regular urls with Drupal, e.g. like civicrm/ajax/json, etc?
Can the extern stuff do anything that civicrm/ajax can't? Or do we want to skip the expensive Drupal bootstrap?

For 2., I think we assume a lot less, if we use the proposed solution.
So this could be actually safe.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions (Moderator: Dave Greenberg) »
  • Avoiding hardcoded filepaths in civicrm.settings.php

This forum was archived on 2017-11-26.