Combu Server  3.1.1
PHP API Documentation
AdminAccount.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
5 class AdminAccount extends DataClass {
6 
7  const TABLE_NAME = "AdminAccount";
8 
9  public $Id = 0;
10  public $Username = "";
11  public $Password = "";
12 
16  public function __construct($src = null, $stripSlashes = false) {
17  global $Database;
18  if ($src == null)
19  return;
20  if (is_array($src)) {
21  // Load by array
22  $this->_loadByRow($src, $stripSlashes);
23  } else if (is_numeric($src) && intval($src) > 0) {
24  // Load by Id
25  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
26  } else {
27  // Load by Username
28  $this->_loadFilter(self::GetTableName(__CLASS__), "Username = '" . $Database->Escape($src) . "'");
29  }
30  }
31 
37  public function ExistsUsername() {
38  global $Database;
39  $sql = "SELECT Id FROM " . self::GetTableName(__CLASS__) . " WHERE Username = '" . $Database->Escape($this->Username) . "'";
40  if ($this->Id > 0)
41  $sql .= " AND Id <> " . $this->Id;
42  $res = $Database->Query($sql);
43  if ($res) {
44  $row = $Database->FetchAssoc($res);
45  if ($row)
46  return TRUE;
47  }
48  return FALSE;
49  }
50 
56  public function IsLogged() {
57  global $AdminLogged;
58  return ($this->Id > 0 && $this->Id == $AdminLogged->Id);
59  }
60 
71  public static function Load($username = "", $limit = null, $offset = null, &$count = null, $returnArray = false) {
72  global $Database;
73  $where = "";
74  if ($username != "")
75  $where .= ($where == "" ? "" : " AND ") . "Username LIKE '" . $Database->Escape($username) . "%'";
76  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Username", $limit, $offset, $count);
77  }
78 
87  public static function CheckLogin($username, $password, &$account = null) {
88  global $Database;
89  $account = null;
90  $query = sprintf("SELECT * FROM " . self::GetTableName(__CLASS__) . " WHERE Username = '%s' AND Password = '%s'", $Database->Escape($username), $Database->Escape(md5($password)));
91  $res = $Database->Query($query);
92  if ($res) {
93  $row = $Database->FetchAssoc($res);
94  if ($row) {
95  $account = new self($row);
96  return TRUE;
97  }
98  }
99  return FALSE;
100  }
101 
107  public static function GetSession() {
108  $id = (!isset($_SESSION["AdminId"]) ? 0 : intval($_SESSION["AdminId"]));
109  $account = new self($id);
110  return $account;
111  }
112 
118  public static function SetSession(&$account) {
119  global $AdminLogged, $Database;
120  if ($account && is_a($account, __CLASS__) && $account->Id > 0) {
121  $AdminLogged = $account;
122  $_SESSION["AdminId"] = $account->Id;
123  }
124  }
125 
129  public static function UnsetSession() {
130  global $AdminLogged;
131  $AdminLogged = new self();
132  $_SESSION["AdminId"] = 0;
133  }
134 
138  public static function Logout() {
139  self::UnsetSession();
140  }
141 
147  public function Save() {
148  global $Database;
149  if ($this->Id < 1) {
150  $query = sprintf("INSERT INTO %s (Username, Password) VALUES ('%s', '%s')",
151  self::GetTableName(__CLASS__),
152  $Database->Escape($this->Username),
153  $Database->Escape($this->Password));
154  } else {
155  $query = sprintf("UPDATE %s SET Username = '%s', Password = '%s' WHERE Id = %d",
156  self::GetTableName(__CLASS__),
157  $Database->Escape($this->Username),
158  $Database->Escape($this->Password),
159  $this->Id);
160  }
161  $saved = $Database->Query($query);
162  if ($saved) {
163  if ($this->Id <= 0)
164  $this->Id = $Database->InsertedId();
165  return TRUE;
166  }
167  return FALSE;
168  }
169 
175  public function Delete() {
176  if ($this->Id < 1)
177  return FALSE;
178  return $this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id);
179  }
180 
181  public function ToJson () {
182  if ($this->Id < 1)
183  return json_decode(array());
184  $array = Utils::ObjectToArray($this);
185  // Remove password from response
186  unset($array["Password"]);
187  return json_encode($array);
188  }
189 }
__construct($src=null, $stripSlashes=false)
static CheckLogin($username, $password, &$account=null)
static Load($username="", $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: Account.php:3
static SetSession(&$account)