-1

I have a custom page(Ex:usertest.php) in root folder and externally loaded drupal. If I print $user->uid it is displaying the user id. Upto now it is fine. But if I included the user-profile.tpl.php which exists in core "modules/user" folder, it is not working. From below code, it is not going to if loop. menu_get_object('user') is not wokring here. Any suggestions..

if($account = menu_get_object('user')) { 
    echo "User Profile Page"; 
} 

Code

<?php
global $user;
define('DRUPAL_ROOT',  '/var/www/sitename/doctors');
$drupal_url  = 'http://sitename/doctors'; // no trailing slashes
drupal_external_load($drupal_path, $drupal_url);
echo $uid = $user->uid;

/**************************************  Start - External Login related functions *********************************/
// load drupal stuff
function drupal_external_load($drupal_url) {
  global $base_url;

  // set drupal base_url (if not set set in settings.php)
  // because it's used in session name
  $base_url = $drupal_url;

  // save current path
  $current_path = getcwd();

  // move to drupal path, because it uses relative path for its includes
  chdir(DRUPAL_ROOT);

  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  // to use the drupal global user var (instead of session hack)
  // drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

  // return the current path
  chdir($current_path);
}


// load a drupal user into the user obj
function drupal_external_userload($user_info = array()) {
  // Dynamically compose a SQL query:
  $query = array();
  $params = array();

  if (is_numeric($user_info)) {
    $user_info = array('uid' => $user_info);
  }
  elseif (!is_array($user_info)) {
    return FALSE;
  }

  foreach ($user_info as $key => $value) {
    if ($key == 'uid' || $key == 'status') {
      $query[] = "$key = %d";
      $params[] = $value;
    }
    else if ($key == 'pass') {
      $query[] = "pass = '%s'";
      $params[] = md5($value);
    }
    else {
      $query[]= "LOWER($key) = LOWER('%s')";
      $params[] = $value;
    }
  }
  $result = db_query('SELECT * FROM {users} u WHERE '. implode(' AND ', $query), $params);

  if ($user = db_fetch_object($result)) {
    $user = drupal_unpack($user);

    $user->roles = array();
    if ($user->uid) {
      $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
    }
    else {
      $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
    }
    $result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $user->uid);
    while ($role = db_fetch_object($result)) {
      $user->roles[$role->rid] = $role->name;
    }
    //user_module_invoke('load', $user_info, $user);
  }
  else {
    $user = FALSE;
  }

  return $user;
}


// don't send any headers before calling this
function drupal_external_login($username, $password) {
  global $user;

  if ( $user->uid > 0 ) {
    if ( $user->name != $username ) {
      drupal_external_logout();
    } else {
      return true;
    }
  }

  require DRUPAL_ROOT. '/includes/password.inc' ;
  $account = user_load_by_name($username);
  if ( user_check_password($password, $account) ) {
    $user = $account;
    drupal_session_regenerate();
    return true;
  }

  return false;
}


function drupal_external_logout() {
  global $user;
  session_destroy();
  $user = drupal_anonymous_user();
}

/**************************************  Start - External Login related functions *********************************/
?>

closed as unclear what you're asking by Mołot, Jimajamma, Free Radical, mpdonadio Feb 11 '14 at 2:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you show the code that you use to load Drupal? – berliner Feb 6 '14 at 12:44
  • I edited the question with code. Please check. In the 5th line of code, I need to include "User profile page" (i.e., user-profile.tpl.php) – J Sinha Feb 6 '14 at 12:52
  • you are calling drupal_external_load($drupal_path, $drupal_url); yet you haven't defined $drupal_path yet and then drupal_external_load() is defined with only one argument and then you try to use $uid that hasn't been defined and then have a bunch of other functions that don't seem to be ever called so..... it really is hard to figure out what you are asking here – Jimajamma Feb 6 '14 at 14:32
1

If you only want to load the user object, then I would suggest using user_load().

To stay in your logic with menu_get_object(), I guess this fails, because you omit the third argument which sets the path, see the doc. If the path is not given it will default to the current request path which is probably not containing the users id in your case?! Try this:

menu_get_object('user', 1, 'user/' . $user->uid);

But this will result in the same as calling user_load().

In order to use user.module funcitonality, which I guess is what you want, you could try to fake the request by manipulating $_GET, but even if that works, it's afaik considered bad practice.

  • Above you said is helpful for showing all field values in array. It's ok. In user-profile.tpl.php page, $pic = $user_profile['field_enterprise_blog_picture']; $pic variable will show the user picture. This is not working here. Can we get it here? – J Sinha Feb 7 '14 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.