home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / write_sinc.DB.inc < prev    next >
Encoding:
Text File  |  2003-08-24  |  7.1 KB  |  197 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    include/write_sinc.DB.inc
  5. //
  6. //    (C)Copyright 2002-2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //    This file is part of IlohaMail. IlohaMail is free software released 
  9. //    under the GPL license.  See enclosed file COPYING for details, or 
  10. //    see http://www.fsf.org/copyleft/gpl.html
  11. //
  12. /////////////////////////////////////////////////////////
  13.  
  14. /********************************************************
  15.  
  16.     PURPOSE:
  17.         1.  Generate session ID
  18.         2.  Initialize session
  19.     PRE-CONDITIONS:
  20.         $user_name - User name
  21.         $host - IMAP server
  22.     POST-CONDITIONS:
  23.         $user - Session ID
  24.         $new_user - true if new user, else false
  25.         session info is stored in database
  26.         prefs and colors stored in backend (DB or FS)
  27.     NOTE:
  28.         In conf/db_conf.php
  29.         ...assumes the following tables are present:
  30.             $DB_USERS_TABLE
  31.             $DB_SESSIONS_TABLE
  32.         ...following are optional:
  33.             $DB_PREFS_TABLE
  34.             $DB_COLORS_TABLE
  35.         If $DB_PREFS_TABLE or $DB_COLORS_TABLE is empty, a file based backend is used.
  36.  
  37. ********************************************************/
  38.  
  39. function GetPrefsFolder($user, $host){
  40.     global $UESR_DIR;
  41.     
  42.     $result=false;
  43.     $path = $USER_DIR.ereg_replace("[\\/]", "", $user.".".$host);
  44.     if (file_exists(realpath($path))){
  45.         $result=$path;
  46.     }else{
  47.         if (mkdir($path, 0700)) $result=$path;
  48.     }
  49.     return $result;
  50. }
  51.  
  52. function GetSettings($result, $file){
  53.     $lines = file($file);
  54.     if (is_array($lines)){
  55.                 while ( list($k, $line) = each($lines) ){
  56.             list($key, $val) = explode(":", $line);
  57.             $result[$key] = base64_decode($val);
  58.         }
  59.     }else{
  60.         $result=false;
  61.     }
  62.     
  63.     return $result;
  64. }
  65.  
  66.     include_once("../include/array2php.inc");
  67.     include_once("../include/array2sql.inc");
  68.     include_once("../conf/db_conf.php");
  69.  
  70.     // initialize some vars
  71.     $prefs_saved = false;
  72.     $colors_saved = false;
  73.     $new_user = false;
  74.  
  75.     // we only need user dirs if contacts, prefs, or colors table aren't specified
  76.     if ((empty($DB_CONTACTS_TABLE)) || (empty($DB_PREFS_TABLE)) || (empty($DB_COLORS_TABLE))){
  77.         //if needed, look for a path, or create one
  78.         $path=GetPrefsFolder($user_name, $host);
  79.     }
  80.  
  81.     // create session ID
  82.     if (!isset($session)){
  83.         $session=time()."-".GenerateRandomString(5,"0123456789");
  84.         $user=$session;    
  85.     }
  86.     
  87.     // generate random session key
  88.     $key=GenerateMessage(strlen($password)+5);
  89.  
  90.     $ipkey = InitSessionEncKey($session);        
  91.     
  92.     // encrypt login ID, host, and passwords
  93.     $encpass = EncryptMessage($ipkey, $password);
  94.     $encHost = EncryptMessage($ipkey, $host);
  95.     $encUser = EncryptMessage($ipkey, $user_name);
  96.     if (!empty($path)) $encPath = EncryptMessage($ipkey, $path);
  97.     
  98.     //connect to database
  99.     include_once("../include/idba.$DB_TYPE.inc");
  100.     $db = new idba_obj;
  101.     if ($db->connect()){
  102.             // check users table, create entry if necessary
  103.             $sql = "select id,userLevel from $DB_USERS_TABLE where (login='$user_name') and (host='$host')";
  104.             $r = $db->query($sql);
  105.             if ($r){
  106.                 if ($db->num_rows($r)<1){
  107.                     // if user not in db, insert
  108.                     $now = time();
  109.                     $sql = "insert into $DB_USERS_TABLE (login, host, dateCreated, lastLogin, userLevel) ";
  110.                     $sql .= "values ('$user_name', '$host', '$now', '$now', 0)";
  111.                     if (!$db->query($sql)){
  112.                         $error.="DB error: Couldn't add user to users table<br>\n";
  113.                         echo "\n<!--\nSQL:$sql\nERROR:".$db->error()."\n//-->\n";
  114.                     }else{
  115.                         $dataID = $db->insert_id();
  116.                         $userLevel = 0;
  117.                         $new_user = true;
  118.                     }
  119.  
  120.                     // create record in prefs
  121.                     if ((empty($error)) && (!empty($DB_PREFS_TABLE))){
  122.                         $my_prefs = $default_prefs;
  123.                         $my_prefs["id"] = $dataID;
  124.                         $sql = Array2SQL($DB_PREFS_TABLE, $my_prefs, "INSERT");
  125.                         if ($db->query($sql)){
  126.                             $prefs_saved = true;
  127.                         }else{
  128.                             $error .= "DB error: Couldn't insert into $DB_PREFS_TABLE<br>\n";
  129.                             echo "\n<!--\nSQL:$sql\nERROR:".$db->error()."\n//-->\n";
  130.                             $db->query("delete from $DB_USERS_TABLE where id='$dataID'");
  131.                         }
  132.                     }
  133.                     
  134.                     // create record in colors
  135.                     if ((empty($error)) && (!empty($DB_COLORS_TABLE))){
  136.                         $my_colors = $default_colors;
  137.                         $my_colors["id"] = $dataID;
  138.                         $sql = Array2SQL($DB_COLORS_TABLE, $my_colors, "INSERT");
  139.                         if ($db->query($sql)){
  140.                             $colors_saved = true;
  141.                         }else{
  142.                             $error .= "DB error: Couldn't insert into $DB_COLORS_TABLE<br>\n";
  143.                             echo "\n<!--\nSQL:$sql\nERROR:".$db->error()."\n//-->\n";
  144.                             if ($prefs_saved) $db->query("delete from $DB_COLORS_TABLE where id='$dataID'");
  145.                             $db->query("delete from $DB_USERS_TABLE where id='$dataID'");
  146.                         }
  147.                     }
  148.  
  149.                 }else{
  150.                     $dataID = $session_dataID = $db->result($r, 0, "id");
  151.                     $userLevel = $db->result($r, 0, "userLevel");
  152.                     $colors_saved = true;
  153.                     $prefs_saved = true;
  154.                 }
  155.                 //echo "<!-- Selected: $dataID -->";
  156.                 
  157.             }else{
  158.                 $error.="DB error: Couldn't access users table <br>\n";
  159.             }
  160.             
  161.             // Initialize session
  162.             if (empty($error)){
  163.                 if (empty($port)) $port = 143;
  164.                 $sql = "insert into $DB_SESSIONS_TABLE (sid, login, password, host, path, dataID, port, userLevel, inTime)";
  165.                 $sql.= " values ('$user', '$encUser', '$encpass', '$encHost', '$encPath', '$dataID', '$port', '$userLevel', ".time().")";
  166.                 if (!$db->query($sql)) $error .= "DB Insert failed: ".$db->error()." <br>\n";
  167.                 $sql = "update $DB_USERS_TABLE set lastLogin='".time()."' where id='$dataID'";
  168.                 if (!$db->query($sql)) $error .= "DB Update failed: ".$db->error()." <br>\n";
  169.             }
  170.     }else{
  171.         $error .= "DB connection failed. <br>\n";
  172.     }
  173.  
  174.     if (!empty($path)){
  175.         if (!$prefs_saved){
  176.             echo "\n<!-- Saving prefs to FS backend -->\n";
  177.             // initialize $my_prefs, and create $userPath/prefs.inc file
  178.             if (file_exists(realpath($path."/prefs"))) $my_prefs = GetSettings($init["my_prefs"], $path."/prefs");
  179.             else $my_prefs = $init["my_prefs"];
  180.             include("../include/save_prefs.inc");
  181.         }
  182.     
  183.         if (!$colors_saved){
  184.             echo "\n<!-- Saving colors to FS backend -->\n";
  185.             // initialize $my_colors, and create $userPath/colors.inc file
  186.             if (file_exists(realpath($path."/colors"))) $my_colors = GetSettings($init["my_colors"], $path."/colors");
  187.             else $my_colors = $init["my_colors"];
  188.             include("../include/save_colors.inc");
  189.         }
  190.     }
  191.     
  192.     if (!empty($error)){
  193.         $session="";
  194.         $user = $user_name;
  195.     }
  196. ?>
  197.