phpScheduleIt
May 24, 2013, 11:33:00 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: phpScheduleIt 2.4.2 has been released!
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Drupal login integration modifications  (Read 3062 times)
tgramil
Newbie
*

Karma: 0
Posts: 6


« on: July 09, 2010, 08:15:45 PM »

Modifications posted here change phpscheduleit (1.2.12) to permit integration with Drupal.  The scheduleit login system is bypassed, and users are only permitted access when already logged into Drupal.  Drupal provides the email address and its user ID number to phpscheduleit.  If civicrm is also used (a drupal module) first name, last name, and phone are also determined and passed to phpscheduleit.  Note: this does not make phpscheduleit a drupal module, it instead allows phpscheduleit to remain a standalone program while using drupal logins.

The quick version: to use phpscheduleit with drupal, apply the patches in the posts below to the appropriate phpscheduleit files, and modify the phpscheduleit configuration file to denote drupal usage.  The new variables in config.php for drupal integration are:
    
     $conf['drupal']['authentication'] = true;
     $conf['drupal']['baseloc'] = '/home2/uacrewor/public_html/drupal';
     $conf['civicrm']['database'] = true;

you must also set $conf['app']['useLogonName'] = 0;

If these parameters are not set the modifications discussed here do not change the behavior of phpscheduleit.

IMPORTANT!! -- if you are using civicrm you must also have the phpscheduleit PEAR.php be linked to (or replaced by) the more recent civicrm version.  this can be done by changing to the lib/pear directory of phpscheduleit and then:

    mv PEAR.php PEAR.php.bak
    ln -s PEAR.php "drupal base location"/sites/all/modules/civicrm/packages/PEAR.php
(or just copying the civicrm version to the phpscheduleit location.)
    -- The modification will fail without this change if civicrm is used. --

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The phpscheduleit files altered are:

    config/config.php
    config/init.php
    index.php
    lib/Auth.class.php
    lib/db/AuthDB.class.php
    templates/auth.template.php
    lang/en_US.lang.php

The config settings are described above, just a single line in init.php has been changed to permit drupal sessions and phpscheduleit sessions to "play nice" (more on this next...)

The file index.php is where most of the action takes place.  The Drupal code that permits probing of the drupal login is included in the drupal distribution: drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION). This command starts a web session that contains the info of whether the user is logged into drupal, but that session interferes with the web session used for phpscheduleit.  The work around I use is to perform the drupal probe first, using appropriate calls and initializations, thereby opening the 'troublesome' new web session.  After passing the drupal login info to the php script, the script then starts a NEW web session for phpscheduleit, and then closes both sessions. The id of the NEW session is stored so that when the regular phpscheduleit init happens, it uses this NEW session.  Civicrm calls are also used, (if its installed and noted in config.php) to get names and phone numbers.

If the user is logged into drupal, the phpscheduleit command $auth->doLogin is called (from lib/Auth.class.php) using the user info collected. If drupal login is enabled, (for all the description that follows) that function uses the drupal ID as the login name, and runs through a similar set of steps that is used for LDAP logins: creating a new user if it doesn't exist, and updating the user if their vitals have changed.  Cookies are explicitly disabled in this function when drupal logins are used.  New functions in lib/db/AuthDB.class.php are used for creating and updating the user info, since certain info is maintained by drupal/etc rather than phpscheduleit. The user is warned ( by output generated in templates/auth.template.php) that that info cant be changed by the user.  Those warnings are added to the language file (but only the eng_US file has been modified by me.)

The logout and 'not logged in' functions of index.php have also been changed to warn the user to log out of drupal, and to use drupal to log in, respectively (using portions of templates/auth.template.php).  A link to the drupal home page is presented on the pages displaying those warnings.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Modifications are listed as diff files: Create a drupal_patches directory in the main phpscheduleit location, and copy each listing to that directory using the file named above the code box.   The script in the last post/codebox can hopefully) be used to patch the files (used from the main phpscheduleit directory)

Please post any code change suggestions or comments!
      

drupal_config.php
Code:
*** config/config_local_non_drupal.php  2010-07-09 16:22:17.000000000 -0400
--- config/config.php   2010-06-21 19:15:32.000000000 -0400
***************
*** 269,272 ****
--- 269,282 ----
  $conf['ldap']['basedn'] = "ou=people,o=domain.com";
  
+ // DRUPAL/Civicrm Settings
+ // Should we use DRUPAL for authentication and enable transparent user registration.
+ //  User registration data(mail, phone, etc.) is pulled from Civicrm.
+ //  If true the user will have to login with Drupal prior to acessing PHPScheduleit.
+ //  must also have set $conf['app']['useLogonName'] = 0;
+ $conf['drupal']['authentication'] = true;
+ $conf['drupal']['baseloc'] = '/home2/uacrewor/public_html/drupal';
+ $conf['civicrm']['database'] = true;
+ // DRUPAL directory on the server. Set this to where drupal was installed.
+
  /// START COPY FOR VERSION 1.2.0 ///

drupal_init.php
Code:
*** original/config/init.php    2009-12-26 21:37:00.000000000 -0500
--- config/init.php     2010-06-24 18:39:20.000000000 -0400
***************
*** 35,38 ****
--- 35,39 ----
  // Set session name
  ini_set('session.name', $conf['app']['sessionName']);
+ ini_set("session.save_handler", "files");
  
  // Start the session (do not edit!)

drupal_index.php
Code:
*** original/index.php  2009-12-26 21:17:48.000000000 -0500
--- index.php   2010-06-24 18:47:03.000000000 -0400
***************
*** 14,19 ****
--- 14,26 ----
  * Include Template class
  */
+
+ $drupal_data = drupal_setup();  // permit use of drupal login, must happen first or session get broken!
+ include("config/config.php");   // must be included unconditionally here, since drupal_setup already loads it once.
  include_once('lib/Template.class.php');
  
+ if  ($conf['drupal']['authentication'] && ($drupal_data['logon_name'] != 0) && $conf['civicrm']['database']) {
+   $drupal_data =  array_merge($drupal_data, get_civicrm_info($conf['drupal']['baseloc'], $user->uid));
+  }  // this has to be early or databases dont play nice....
+
  // Auth included in Template.php
  $auth = new Auth();
***************
*** 22,26 ****
  
  $resume = (isset($_POST['resume'])) ? $_POST['resume'] : '';
!
  // Logging user out
  if (isset($_GET['logout'])) {
--- 29,40 ----
  
  $resume = (isset($_POST['resume'])) ? $_POST['resume'] : '';
!
! if ($conf['drupal']['authentication']) {
!   if (($drupal_data['logon_name'] != 0) && (!isset($_GET['logout'])) ){
!     $msg = $auth->doLogin($drupal_data['logon_name'], '', null, false, $resume);   // cookies never used, logon name = drupal user ID
!     echo "$msg";
!   }
! }
!
  // Logging user out
  if (isset($_GET['logout'])) {
***************
*** 28,32 ****
  }
  else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
!       $msg = $auth->doLogin($_POST['email'], $_POST['password'], (isset($_POST['setCookie']) ? 'y' : null), false, $resume, $_POST['language']);
  }
  else if (isset($_COOKIE['ID'])) {
--- 42,46 ----
  }
  else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
!       $msg = $auth->doLogin($_POST['email'], $_POST['password'], (isset($_POST['setCookie']) ? 'y' : null), false, $resume, $_POST['language']);
  }
  else if (isset($_COOKIE['ID'])) {
***************
*** 43,47 ****
  $t->startMain();
  
! if (isset($_GET['auth'])) {
        $auth->printLoginForm(translate('You are not logged in!'), $_GET['resume']);
  }
--- 57,65 ----
  $t->startMain();
  
! if ($conf['drupal']['authentication'])  {
!   if (isset($_GET['logout'])) { $auth->printDrupalForm(true); }
!   else {  $auth->printDrupalForm('');  }
!  }
!  else if (isset($_GET['auth'])) {
        $auth->printLoginForm(translate('You are not logged in!'), $_GET['resume']);
  }
***************
*** 53,55 ****
  // Print HTML footer
  $t->printHTMLFooter();
! ?>
\ No newline at end of file
--- 71,125 ----
  // Print HTML footer
  $t->printHTMLFooter();
!
! function drupal_setup () {
!   $start_loc =  getcwd();
!   include("config/config.php");
!   if ($conf['drupal']['authentication']) {
!     $drupal_loc = $conf['drupal']['baseloc'];
!     include_once($drupal_loc."/includes/bootstrap.inc");
!     include_once($drupal_loc."/includes/module.inc");
!     //    include_once($drupal_loc."sites/default/settings.php");
!     chdir ("$drupal_loc");
!       drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
!     chdir ("$start_loc");
!     global $user;
!     $data = array(
!             'logon_name' => $user->uid,
!             'emailaddress' => $user->mail,
!             'lang' => $conf['app']['defaultLanguage'],
!           'timezone' => $conf['app']['timezone'],
!           'title' => null,
!             'password' => $conf['app']['defaultPassword'],
!             'password2' => $conf['app']['defaultPassword'],
!             'position' => null,
!             'institution' => null,
!           //            'drupal_url' => $base_url,  // used not for logging in, but for redirection to drupal
!           );
!     //  start a new session; but leave old drupal one available for others, new session re-opened later by config/init.php
!     session_regenerate_id();
!     $sid = session_id();
!     session_write_close();
!     session_id($sid);
!   }
!   return $data;
! }
!
! function get_civicrm_info($drupal_loc, $civic_id) {
! require_once ($drupal_loc.'/sites/default/civicrm.settings.php');
! $cdsn = CIVICRM_DSN;
!  $civic_db = DB::Connect( $cdsn );
! if (PEAR::isError($civic_db)) { die($civic_db->getMessage()); }
! $res = $civic_db->query("SELECT  first_name, last_name  FROM civicrm_contact WHERE id = ".$civic_id );
! if (PEAR::isError($res)) {die($res->getMessage()); }
! $res->fetchInto($c_names, DB_FETCHMODE_ASSOC);
!  $civic_data = array('fname' => $c_names["first_name"], 'lname' => $c_names["last_name"],  );
! $res = $civic_db->query("SELECT  phone, is_primary FROM civicrm_phone WHERE contact_id  = ".$civic_id );
! if (PEAR::isError($res)) {die($res->getMessage()); }
!  while($res->fetchInto($c_phones, DB_FETCHMODE_ASSOC)){
!  if ($c_phones['is_primary'] == "1") {$civic_data['phone'] = $c_phones['phone'];}
!  }
! $civic_db->disconnect();
! return $civic_data;
! }
!
! ?>

drupal_Auth.class.php
Code:
*** original/lib/Auth.class.php 2010-02-12 23:17:30.000000000 -0500
--- lib/Auth.class.php  2010-06-25 12:13:25.000000000 -0400
***************
*** 110,118 ****
                                $msg .= translate('That cookie seems to be invalid') . '<br/>';
                        }
!               }
!               else {
!
!                 if( $conf['ldap']['authentication'] ) {
!
                    // Include LDAPEngine class
              include_once('LDAPEngine.class.php');
--- 110,131 ----
                                $msg .= translate('That cookie seems to be invalid') . '<br/>';
                        }
!               } else {
!                 if( $conf['drupal']['authentication'] ) { // drupal uid is used for scheduleit logon name
!                    global $drupal_data;
!                      $id = $this->db->userExists( $uname, true );
!                      if( $id ) {
!                         // check if Drupal and local DB are in consistancy.
!                         if( $this->db->check_updates( $id, $drupal_data ) ) {
!                             $this->db->logon_update_user( $id, $drupal_data );
!                         }
!                    } else {
!                      $id = $this->do_register_user( $drupal_data, false );
!                      if (!$conf['civicrm']['database']) {
!                        $resume = 'register.php?edit=true';            // redirect to finish registration data
!                      }
!                    }
!                    $ok_user = true; $ok_pass = true;
!                 }
!                 else if( $conf['ldap']['authentication'] ) {
                    // Include LDAPEngine class
              include_once('LDAPEngine.class.php');
***************
*** 246,251 ****
                // Check for valid session
                if (!$this->is_logged_in()) {
!                       $this->print_login_msg();
!                       die;
                }
                else {
--- 259,267 ----
                // Check for valid session
                if (!$this->is_logged_in()) {
!                 global $conf;
!                 if( !$conf['drupal']['authentication'] ) { // drupal uid is used for scheduleit logon name
!                   $this->print_login_msg();
!                   die;
!                 } else { return; }
                }
                else {
***************
*** 260,265 ****
                        setcookie('ID', '', time()-3600, '/');
  
!                       // Refresh page
!                       CmnFns::redirect($_SERVER['PHP_SELF']);
                }
        }
--- 276,284 ----
                        setcookie('ID', '', time()-3600, '/');
  
!                       if( $conf['drupal']['authentication'] ) {  // no redirection if drupal authorization in use
!                         return;
!                       } else {                        // Refresh login page
!                         CmnFns::redirect($_SERVER['PHP_SELF']);
!                       }
                }
        }
***************
*** 277,281 ****
                global $conf;
  
!               if( !$conf['ldap']['authentication'] ) {
                        // Verify user data
                        $msg = $this->check_all_values($data, false);
--- 296,300 ----
                global $conf;
  
!               if(( !$conf['ldap']['authentication'] ) && ( !$conf['drupal']['authentication'] )) {
                        // Verify user data
                        $msg = $this->check_all_values($data, false);
***************
*** 356,360 ****
                                                . ' position- ' . $data['position'], $id);
  
!               if( !$conf['ldap']['authentication'] ) {
                        $url = 'ctrlpnl.php';
                        if ($adminCreated){
--- 375,379 ----
                                                . ' position- ' . $data['position'], $id);
  
!               if( !$conf['ldap']['authentication'] &&  !$conf['drupal']['authentication'] ) {
                        $url = 'ctrlpnl.php';
                        if ($adminCreated){
***************
*** 557,560 ****
--- 576,587 ----
  
        /**
+       * Wrapper function to call template 'printDrupalForm' function
+       * @param string $status true if was logged in, now logging out
+       */
+       function printDrupalForm($status = '') {
+               printDrupalForm($status);
+       }
+
+       /**
        * Prints a message telling the user to log in
        * @param boolean $kill whether to end the program or not


(I've overrun max post size, continued in the next post!!)
             Tom G.

« Last Edit: July 09, 2010, 08:19:49 PM by tgramil » Logged
fighterman
Newbie
*

Karma: 0
Posts: 10


WWW
« Reply #1 on: July 11, 2010, 06:24:13 PM »

Thanks
You.
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2006-2007, Simple Machines Valid XHTML 1.0! Valid CSS!