2
/////////////////////////////////////////////////////////
4
// include/write_sinc.DB.inc
6
// (C)Copyright 2002-2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
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
12
/////////////////////////////////////////////////////////
14
/********************************************************
17
1. Generate session ID
20
$user_name - User name
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)
29
...assumes the following tables are present:
32
...following are optional:
35
If $DB_PREFS_TABLE or $DB_COLORS_TABLE is empty, a file based backend is used.
37
********************************************************/
39
function GetPrefsFolder($user, $host){
43
$path = $USER_DIR.ereg_replace("[\\/]", "", $user.".".$host);
44
if (file_exists(realpath($path))){
47
if (mkdir($path, 0700)) $result=$path;
52
function GetSettings($result, $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);
66
include_once("../include/array2php.inc");
67
include_once("../include/array2sql.inc");
68
include_once("../conf/db_conf.php");
70
// initialize some vars
72
$colors_saved = false;
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);
82
if (!isset($session)){
83
$session=time()."-".GenerateRandomString(5,"0123456789");
87
// generate random session key
88
$key=GenerateMessage(strlen($password)+5);
90
$ipkey = InitSessionEncKey($session);
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);
99
include_once("../include/idba.$DB_TYPE.inc");
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);
106
if ($db->num_rows($r)<1){
107
// if user not in db, insert
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";
115
$dataID = $db->insert_id();
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)){
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'");
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;
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'");
150
$dataID = $session_dataID = $db->result($r, 0, "id");
151
$userLevel = $db->result($r, 0, "userLevel");
152
$colors_saved = true;
155
//echo "<!-- Selected: $dataID -->";
158
$error.="DB error: Couldn't access users table <br>\n";
161
// Initialize session
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";
171
$error .= "DB connection failed. <br>\n";
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");
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");