drupal.plugin.php

Go to the documentation of this file.
00001 <?php
00002 
00017 include_once("embient.plugin.php");
00018 
00025 function embient_create_drupal_plugin()
00026 {
00027   return new EmbientDrupal();
00028 }
00029 
00040 function embient_drupal_encrypt($password)
00041 {
00042   return md5($password);
00043 }
00044 
00048 class EmbientDrupal extends EmbientPlugin
00049 {
00051   var $conf;
00052 
00054   var $vars;
00055 
00056   // -------------------------
00057 
00061   function EmbientDrupal()
00062   {
00063     $this->EmbientPlugin('drupal');
00064 
00065     // user field mapping for Drupal
00066     $this->_mapping = array
00067     (
00068       'name'       => 'name',
00069       'passwd'     => 'pass',
00070       'mail'       => 'mail',
00071       'signature'  => 'signature',
00072       'timezone'   => 'timezone',
00073     );
00074   }
00075 
00079   function Init($cfg)
00080   {
00081     // load settings to class's properties
00082     $this->conf = $cfg;
00083 
00084     // load database params
00085     $db_params = array(
00086                   'db_host'   => $this->conf['db_server']['value'],
00087                   'db_user'   => $this->conf['db_user']['value'],
00088                   'db_passwd' => $this->conf['db_passwd']['value'],
00089                   'db_name'   => $this->conf['db_name']['value'],
00090                   'prefix'    => $this->conf['db_prefix']['value'],
00091                  );
00092 
00093     // connect to database
00094     if ( ! parent::Init($db_params) )
00095     {
00096       return false;
00097     }
00098 
00099     // load settings from database
00100     $query = "SELECT name, value FROM {variable}";
00101 
00102     if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00103     {
00104       return false;
00105     }
00106 
00107     $this->vars = array();
00108 
00109     $rows = $this->_db->GetRowscount();
00110 
00111     for ( $i = 0; $i < $rows; $i++ )
00112     {
00113       $var = $this->_db->FetchArray();
00114       $this->vars[$var['name']] = $var['value'];
00115     }
00116 
00117     return ! empty($this->vars);
00118   }
00119 
00123   function Login($username)
00124   {
00125     // load user information
00126     if ( ! $this->_Load($username) )
00127     {
00128       embient_error("User ". $username ." cannot be loaded", __FILE__, __LINE__);
00129     }
00130 
00131     $sid = session_id();
00132 
00133     // check, if session is already available in database?
00134     $query = "SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid "
00135            . "WHERE s.sid = '". $sid ."' AND u.status < 3";
00136     
00137     if ( ! $this->_db->Query($query, __FILE__, __LINE__) || $this->_db->GetRowsCount() <= 0 )
00138     {
00139       // create one, if not exists
00140       $query = "INSERT INTO {sessions} (sid, uid, hostname, timestamp) "
00141              . "VALUES ('". $sid ."', 0, '". $_SERVER["REMOTE_ADDR"] ."', ". time() .")";
00142 
00143       if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00144       {
00145         embient_error("Cannot create session for ". $this->user['name']);
00146         return;
00147       }
00148 
00149     }
00150     else
00151     {
00152       $query = "UPDATE {sessions} SET uid = '". $this->user['uid'] ."' WHERE sid = '". session_id() ."'";
00153 
00154       if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00155       {
00156         embient_error("Cannot update session for ". $this->user['name']);
00157         return;
00158       }
00159     }
00160 
00161     // check, if cookie is available?
00162     $cookie_name = session_name();
00163 
00164     if ( ! isset($_COOKIE[$cookie_name]) )
00165     {
00166       setcookie($cookie_name, $sid, session_get_cookie_params());
00167       $_COOKIE[$cookie_name] = $sid;
00168     }
00169   }
00170 
00174   function Logout($username)
00175   {
00176     $query = "UPDATE {sessions} SET uid = '0' WHERE sid = '". session_id() ."'";
00177 
00178     $this->_db->Query($query, __FILE__, __LINE__);
00179     $this->user = array();
00180   }
00181 
00185   function Add($user)
00186   {
00187     // initialise the user info
00188     $this->user = Array();
00189     $this->_ConvertUserInfo($user, true);
00190 
00191     // encrypt password
00192     $this->user['pass'] = embient_drupal_encrypt($this->user['pass']);
00193 
00194     // format query
00195     $val = '';
00196     foreach( $this->user as $v)
00197     {
00198       $val = $val.($val != '' ? ', ' : '')."'$v'";
00199     }    
00200 
00201     $query = "INSERT INTO {users} "
00202                  . "(" . implode(', ', array_keys($this->user)) . ") "
00203            . "VALUES (". $val .')';
00204 
00205     if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00206     {
00207       embient_error("Cannot add new user [". $this->user['name'] ."] into database");
00208       return;
00209     }
00210 
00211     // apply Drupal default role (authenticated user) to user
00212     $query = "INSERT INTO {users_roles} "
00213            . "(uid, rid) "
00214            . "VALUES ( '". $this->user['uid'] ."', '2')";
00215 
00216     $this->_db->Query($query, __FILE__, __LINE__);
00217   }
00218 
00222   function Update($username, $user)
00223   {
00224 
00225     // get database key for this user
00226     $this->_Load($username);
00227     $uid = $this->user['uid'];
00228 
00229     // if new password is supplied, re-encrypt the password
00230     $newpass = '';
00231     if ( ! empty($user['passwd']) )
00232     {
00233 //      embient_debug("password for ". $user['name'] ." (currently known as ". $username .") is set to ". $user['passwd']);
00234       $newpass = embient_drupal_encrypt($user['passwd']);
00235     }
00236 
00237     // load value
00238     $this->user = Array();                 // clear buffer
00239     $this->_ConvertUserInfo($user, false); // not load default
00240 
00241     // load new encrpyted password, if available
00242     if ( ! empty($newpass) )
00243     {
00244       $this->user['pass'] = $newpass;
00245     }
00246 
00247     // create query
00248     $fields = array();
00249 
00250     foreach ($this->user as $key => $val)
00251     {
00252       array_push($fields, "$key = '$val'");
00253     }
00254 
00255     $query = "UPDATE {users} "
00256            . "SET "
00257            . implode(', ', $fields) ." "
00258            . "WHERE uid = '$uid'";
00259 
00260     $this->_db->Query($query, __FILE__, __LINE__);
00261   }
00262 
00266   function Delete($username)
00267   {
00268     $this->_Load($username);
00269 
00270     $query = "DELETE FROM {users} "
00271            . "WHERE uid = '". $this->user['uid'] ."'";
00272 
00273     $this->_db->Query($query, __FILE__, __LINE__);
00274 
00275     $query = "DELETE FROM {users_roles} "
00276            . "WHERE uid = '". $this->user['uid'] ."'";
00277 
00278     $this->_db->Query($query, __FILE__, __LINE__);
00279 
00280     $query = "DELETE FROM {sessions} "
00281            . "WHERE uid = '". $this->user['uid'] ."'";
00282     
00283     $this->_db->Query($query, __FILE__, __LINE__);
00284 
00285     $query = "DELETE FROM {authmap} "
00286            . "WHERE uid = '". $this->user['uid'] ."'";
00287 
00288     $this->_db->Query($query, __FILE__, __LINE__);
00289   }
00290 
00291   // -------------------------
00292 
00297   function _Load($username)
00298   {
00299     if ( $this->_db->connection == false )
00300     {
00301       embient_error("Drupal Database is not connected", __FILE__, __LINE__);
00302       return false;
00303     }
00304 
00305     $query = "SELECT u.* FROM {users} u WHERE u.name = '$username' LIMIT 1";
00306 
00307     if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00308     {
00309       return false;
00310     }
00311 
00312     if ( $this->_db->GetRowsCount() <= 0 )
00313     {
00314       embient_error("User [$username] could not be found in Drupal database.", __FILE__, __LINE__);
00315       return false;
00316     }
00317 
00318     // fetch info
00319     $this->user = $this->_db->FetchArray();
00320 
00321     foreach ($this->user as $key => $val)
00322     {
00323       $this->user[$key] = $val;
00324     }
00325 
00326 /*
00327     // load user roles
00328     $this->user['roles'] = array();
00329 
00330     $query = "SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid "
00331            . "WHERE ur.uid = '". $this->user['uid'] ."' ";
00332 
00333     if ( ! $this->_db->Query($query, __FILE__, __LINE__) )
00334     {
00335       return false;
00336     }
00337 
00338     $c = $this->_db->GetRowsCount();
00339 
00340     for ($i = 0; $i < $c; $i++)
00341     {
00342       $roles = $this->_db->FetchArray();
00343       $this->user['roles'][$roles['rid']] = $roles['name'];
00344     }
00345 */
00346 
00347     return true;
00348   }
00349 
00354   function _ConvertUserInfo($user, $default = true)
00355   {
00356     // -----------------------
00357     // convert Embient info to SMF info
00358     parent::_ConvertUserInfo($user, $default);
00359 
00360     // -----------------------
00361     // Postprocessing
00362 
00363     // -----------------------
00364     // load default value
00365     if ( $default )
00366     {
00367       // load next uid from database, if required
00368       if ( ! isset($this->user['uid']) )
00369       {
00370         $this->user['uid'] = $this->_db->GetNextID('{sequences}', '{users}_uid');
00371       }
00372 
00373       $this->user['mode']      = embient_load_with_default( $this->user['mode'],      "0"    );
00374       $this->user['sort']      = embient_load_with_default( $this->user['sort'],      "0"    );
00375       $this->user['theme']     = embient_load_with_default( $this->user['theme'],     $this->vars['theme_default'] );
00376       $this->user['signature'] = embient_load_with_default( $this->user['signature'], ""     );
00377       $this->user['created']   = embient_load_with_default( $this->user['created'],   time() );
00378       $this->user['access']    = embient_load_with_default( $this->user['access'],    time() );
00379       $this->user['login']     = embient_load_with_default( $this->user['login'],     "0"    );
00380       $this->user['status']    = embient_load_with_default( $this->user['status'],    "1"    );
00381       $this->user['timezone']  = embient_load_with_default( $this->user['timezone'],  $this->vars['date_default_timezone'] );
00382       $this->user['language']  = embient_load_with_default( $this->user['language'],  ""     );
00383       $this->user['picture']   = embient_load_with_default( $this->user['picture'],   ""     );
00384       $this->user['init']      = embient_load_with_default( $this->user['init'],      $this->user['mail']);
00385       $this->user['data']      = embient_load_with_default( $this->user['data'],      ""     );
00386     }
00387   }
00388 }

Generated on Fri Feb 10 15:05:54 2006 for Embient by  doxygen 1.4.6-NO