--- /dev/null
+<?
+/*(c) Konrad Rosenbaum, 2007; see COPYING for details*/
+
+/**this class can be used to access a customers data*/
+class Customer
+{
+ private $id;
+
+ /**construct an empty customer; if $id is given it tries to pre-load from the database*/
+ public function __construct($id=false)
+ {
+ $this->id=false;
+ if($id!==false){
+ $this->getByID($id);
+ }
+ }
+
+ /**tries to get the customer by its ID, returns false if it fails*/
+ public function getByID($id)
+ {
+ global $db;
+ $res=$db->select("customer","customerid","where customerid=".$db->escapeInt($id));
+ if(count($res)>0){
+ $this->id=$id+0;
+ return true;
+ }else
+ return false;
+ }
+
+ /**tries to get the customer by its email address, returns false if it fails*/
+ public function getByMail($mail)
+ {
+ global $db;
+ $res=$db->select("customer","customerid","where email=".$db->escapeString($mail));
+ if(count($res)>0){
+ $this->id=$res[0]["customerid"];
+ return true;
+ }else
+ return false;
+ }
+
+ /**checks whether the customer exists in the database; getByID or getByMail must have been called first*/
+ public function exists()
+ {
+ return $this->id !== false;
+ }
+
+ /**creates the customer in the database; getByID or getByMail must not have been called yet;
+ returns the new ID on success or false on failure*/
+ public function create($name)
+ {
+ if($this->id!==false)return;
+ global $db;
+ $this->id=$db->insert("customer",array("name"=>$name));
+ return $this->id;
+ }
+
+ /**sets the email of this customer*/
+ public function setMail($mail)
+ {
+ if($this->id===false)return;
+ global $db;
+ $db->update("customer",array("email"=>$mail),"customerid=".$db->escapeInt($this->id));
+ }
+
+ /**sets the password of this customer*/
+ public function setPassword($pwd)
+ {
+ if($this->id===false)return;
+ global $db;
+ $pass=calcPasswd($pwd,getSalt());
+ $db->update("customer",array("passwd"=>$pass),"customerid=".$db->escapeInt($this->id));
+ }
+
+ /**checks whether $password matches the stored password for this customer; returns true on success*/
+ public function authenticate($passwd)
+ {
+ if($this->id===false)return false;
+ //get record
+ global $db;
+ $res=$db->select("customer","passwd","customerid=".$db->escapeInt($this->id));
+ //found anything?
+ if(count($res)<0)return false;
+ //is it a password
+ if(!is_string($res[0]["passwd"]) || strlen($res[0]["passwd"])<10)return false;
+ //check
+ $pwd=explode(":",$res[0]["passwd"]);
+ $pwd2=calcPasswd($passwd,$pwd[0]);
+ return $pwd2 == $res[0]["passwd"];
+ }
+};
+
+?>
\ No newline at end of file