2

I wrote PHP script which has to bootstrap ex1.mydrupalsite.pl

The sites are hosted using Aegir platform.

The structure looks like this:

/var/aegir/platforms/drupal-7.31/sites/ex1.mydrupalsite.pl
/var/aegir/platforms/drupal-7.31/sites/ex2.mydrupalsite.pl
# etc...

I've been trying:

define('DRUPAL_ROOT', '/var/aegir/platforms/drupal-7.31');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$_SERVER['HTTP_HOST'] = 'ex1.mydrupalsite.pl';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['SCRIPT_NAME'] = '/' . basename(__FILE__);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

But no luck.

tvl
  • 3,868
  • 2
  • 16
  • 35
szikael
  • 364
  • 5
  • 13

2 Answers2

2

I did not find the answer for this anywhere, so after some effort, here is how to do this:

<?php
  # Set username and password
  $username = 'NAME';
  $password = 'PASSWORD';

  # Drupal Bootstrap code
  define('DRUPAL_ROOT', '/var/aegir/platforms/drupal_7');
  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

  # My attempt at injecting variables for multisite stuff to fit aegir
  $variables['url'] = 'http://example.com/';

  $databases['default']['default'] = array(
    'driver' => 'mysql',
    'database' => 'mydb',
    'username' => 'myuser',
    'password' => 'mypass',
    'host' => 'localhost',
    'port' => '3306',
  );

  $db_url['default'] = 'mysql://sqluser:sqlpass@localhost:3306/sqldb';

  drupal_override_server_variables($variables);

  # Try to bootstrap
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  # Try to login
  $uid = FALSE;
  $account = user_load_by_name($username);
  if ($account) {
    // Allow alternate password hashing schemes.
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
    if (user_check_password($password, $account)) {
      // Successful authentication.
      $uid = $account->uid;

     // Update user to new password scheme if needed.
      if (user_needs_new_hash($account)) {
        user_save($account, array('pass' => $password));
      }
    }
  }
  debug($uid);
Tarek Loubani
  • 202
  • 3
  • 10
-1

The problem is that in an Aegir installation, the database access information is -not- stored in settings.php, but in /var/aegir/config/server_master/apache/vhost.d/[sitename] as $_SERVER environment variables. You need to specifically declare them in your script.

Or better yet, use "Drush scr" to run the script. It'll do the bootstrapping work for you! See http://www.oliverdavies.uk/blog/dont-bootstrap-drupal-use-drush/ for a more detailed explanation.

Eloi George
  • 186
  • 1
  • 5
  • This didn't work for me, I got the error: 'Could not find a Drupal settings.php at ./sites/default/settings.php' – user1015214 Jul 25 '16 at 16:47