summaryrefslogtreecommitdiff
path: root/user.class.php
blob: 0643ee36ca983367baa7a9561f1fdd079e921b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?

$access = array("Administrator"	=> 0x00000001,
		"User manager"	=> 0x00000002,
		"News manager"	=> 0x00000004);

class User {
  function User($userid, $passwd="") {
    $result = db_query("SELECT * FROM users WHERE LOWER(userid)=LOWER('$userid') && passwd=PASSWORD('$passwd') && STATUS=0");
    if (db_num_rows($result) == 1) {
      foreach (db_fetch_row($result) as $key=>$value) { $field = mysql_field_name($result, $key); $this->$field = stripslashes($value); $this->field[] = $field; }
    }
  }

  function save() {
    ### Compose query to update user record:
    $query .= "UPDATE users SET ";
    foreach ($this->field as $key=>$field) { $value = $this->$field; $query .= "$field = '". addslashes($value) ."', "; }
    $query .= " id = $this->id WHERE id = $this->id";
    ### Perform query:
    db_query($query);
  }

  function rehash() {
    $result = db_query("SELECT * FROM users WHERE id=$this->id");
    if (db_num_rows($result) == 1) {
      foreach (db_fetch_array($result) as $key=>$value) { $this->$key = stripslashes($value); }
    }
  }

  function valid($access = 0) {
    if ($this->userid) {
      $this->rehash();  // synchronisation purpose
      $this->last_access = time();
      $this->last_host = (!empty($GLOBALS[REMOTE_HOST]) ? $GLOBALS[REMOTE_HOST] : $GLOBALS[REMOTE_ADDR]);
      db_query("UPDATE users SET last_access = '$this->last_access', last_host = '$this->last_host' WHERE id = $this->id");
      if ($this->access & $access || $access == 0) return 1;
    }
    return 0;
  }

  function getHistory($field) {
    return getHistory($this->history, $field);
  }

  function setHistory($field, $value) {
    $this->history = setHistory($this->history, $field, $value);
  }
}

function getHistory($history, $field) {
  $data = explode(";", $history);
  for (reset($data); current($data); next($data)) {
    $entry = explode(":", current($data));
    if (reset($entry) == $field) $rval = end($entry);
  }
  return $rval;
} 

function setHistory($history, $field, $value) {
  if (!$value) {
    ### remove entry:
    $data = explode(";", $history);
    for (reset($data); current($data); next($data)) {
      $entry = explode(":", current($data));
      if ($entry[0] != $field) $rval .= "$entry[0]:$entry[1];";
    }
  }
  else if (strstr($history, "$field:")) {
    ### found: update exsisting entry:
    $data = explode(";", $history);
    for (reset($data); current($data); next($data)) {
      $entry = explode(":", current($data));
      if ($entry[0] == $field) $entry[1] = $value;
      $rval .= "$entry[0]:$entry[1];";
    } 
  }
  else {
    ### not found: add new entry:
    $rval = "$history$field:$value;";
  }
  return $rval;
}

?>