PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Access/inc/class.AccessUserExtended.inc.php

http://flaimo-php.googlecode.com/
PHP | 389 lines | 239 code | 39 blank | 111 comment | 72 complexity | 264dbaa7eec7957db685df1153e8818e MD5 | raw file
  1. <?php
  2. /**
  3. * load base class which takes care of all the other includes via it's autoload function
  4. */
  5. require_once 'class.AccessBase.inc.php';
  6. /**
  7. * enhances the user object with more data
  8. * @author Michael Wimmer <flaimo@gmx.net>
  9. * @category FLP
  10. * @copyright Copyright ? 2002-2006, Michael Wimmer
  11. * @license Free for non-commercial use
  12. * @link http://flp.sf.net/
  13. * @package Access
  14. * @version 1.0
  15. */
  16. class AccessUserExtended extends AccessUser {
  17. /**#@+
  18. * @var string
  19. */
  20. protected $firstname;
  21. protected $lastname;
  22. protected $email;
  23. protected $address;
  24. protected $zip;
  25. protected $town;
  26. protected $phone;
  27. /**#@-*/
  28. /**
  29. * @var int
  30. */
  31. protected $country;
  32. /**
  33. * @var array
  34. */
  35. protected $history_extended;
  36. /**
  37. * Constructor
  38. * @param int $id id of the user in the database
  39. * @return void
  40. */
  41. function __construct($id = 0) {
  42. parent::__construct($id);
  43. } // end constructor
  44. /**
  45. * fetched the data from the database and writes it to the class vars
  46. * @return boolean
  47. */
  48. protected function fetchData() {
  49. $r_value = FALSE;
  50. if ($this->datafetched != FALSE) { return TRUE;}
  51. $sp = 'CALL getUserDataExtended(' . parent::getConn()->real_escape_string($this->getID()) . ');';
  52. if (!parent::getConn()->multi_query($sp)) {return FALSE;}
  53. $result = parent::getConn()->store_result();
  54. if (!$result) {return FALSE;}
  55. $this->datafetched = TRUE;
  56. while($row = $result->fetch_row()) {
  57. $this->original_data = $row;
  58. $this->username = $row[0];
  59. $this->password = $row[1];
  60. $this->status = $row[2];
  61. $this->identifier = $row[3];
  62. $this->token = $row[4];
  63. $this->timeout = $row[5];
  64. $this->firstname = $row[6];
  65. $this->lastname = $row[7];
  66. $this->original_email = $row[8];
  67. $this->email = $row[8];
  68. $this->address = $row[9];
  69. $this->zip = $row[10];
  70. $this->town = $row[11];
  71. $this->country = $row[12];
  72. $this->phone = $row[13];
  73. $_value = TRUE;
  74. } // end while
  75. $result->close();
  76. do {/*thorw away other results*/} while (parent::getConn()->next_result());
  77. return $r_value;
  78. } // end function
  79. /**
  80. * writes the data in the class vars to the database
  81. *
  82. * return codes:
  83. * -1 = snapshot of old data couldn't be written to the db
  84. * -2 = data couldn't be written to db
  85. * -3 = metadata couldn't be written to the db
  86. * 1 = data successfully updated, confirmation mail was send
  87. * 2 = data successfully updated
  88. *
  89. * @param boolean $bypass bypass writing history of data
  90. * @return int
  91. */
  92. public function updateData($bypass = FALSE) {
  93. if ($bypass == FALSE && parent::getAccessSetting('use_data_history') == TRUE) {
  94. if ($this->saveSnapshot() != TRUE) {return -1;} // end if
  95. } // end if
  96. if (parent::getAccessSetting('confirm_email') == TRUE && ($this->getEmail() != $this->original_data[8])) {
  97. if ($this->saveSnapshot(TRUE) != TRUE) {return -1;} // end if
  98. $this->setStatus(ACCESS_STATUS_CONFIRM_CHANGE);
  99. $success = parent::setConfirmation($this->getID(), ACCESS_STATUS_CONFIRM_CHANGE);
  100. if ($success < 1) {
  101. return 'm:' . $success; // errorcode vom versenden weitergeben
  102. } // end if
  103. if (parent::updateDataBase() == FALSE || $this->updateDataExtended() == FALSE) {return -2;} // end if
  104. if (parent::updateMetadataLastChange() == FALSE) {return -3;} // end if
  105. return 1;
  106. } // end if
  107. if (parent::updateDataBase() == FALSE || $this->updateDataExtended() == FALSE) {return -2;} // end if
  108. if (parent::updateMetadataLastChange() == FALSE) {return -3;} // end if
  109. return 2;
  110. } // end function
  111. /**
  112. * writes the extended data to the database
  113. * @return boolean
  114. */
  115. protected function updateDataExtended() {
  116. $sql = 'UPDATE `' . $this->tables['extended'] . '` SET ';
  117. $sql .= '`firstname` = "' . parent::getConn()->real_escape_string($this->getFirstname()) . '", `lastname` = "' . parent::getConn()->real_escape_string($this->getLastname()) . '", `email` = "' . parent::getConn()->real_escape_string($this->getEmail()) . '", `address` = "' . parent::getConn()->real_escape_string($this->getAddress()) . '", `zip` = "' . parent::getConn()->real_escape_string($this->getZIP()) . '", `town` = "' . parent::getConn()->real_escape_string($this->getTown()) . '", `country` = ' . parent::getConn()->real_escape_string($this->getCountry()) . ', `phone` = "' . parent::getConn()->real_escape_string($this->getPhone()) . '" ';
  118. $sql .= 'WHERE `user` = ' . parent::getConn()->real_escape_string(parent::getID());
  119. $result = parent::getConn()->query($sql);
  120. if (!$result) {return FALSE;}
  121. return TRUE;
  122. } // end function
  123. /**
  124. * writes a backup of the original data to the database
  125. * @return mixed true or errorcode
  126. */
  127. protected function saveSnapshot($bypass = FALSE) {
  128. $this->fetchData();
  129. if ($bypass == FALSE) {
  130. parent::saveSnapshot();
  131. } // end if
  132. $sql = 'INSERT INTO `' . $this->tables['history_extended'] . '` ';
  133. $sql .= '(`user`, `firstname`, `lastname`, `email`, `address`, `zip`, `town`, `country`, `phone`, `date`, `ip`) ';
  134. $sql .= 'VALUES (' . parent::getConn()->real_escape_string(parent::getID()) . ', "' . $this->original_data[6] . '", "' . $this->original_data[7] . '", "' . $this->original_data[8] . '", "' . $this->original_data[9] . '", "' . $this->original_data[10] . '", "' . $this->original_data[11] . '", ' . $this->original_data[12] . ', "' . $this->original_data[13] . '", UNIX_TIMESTAMP(), INET_ATON("' . parent::getConn()->real_escape_string(parent::getCurrentIP()) . '"))';
  135. $result = parent::getConn()->query($sql);
  136. if (!$result) {return -1;}
  137. return TRUE;
  138. } // end function
  139. /**
  140. * sets the first name. checks if min/max length is ok
  141. * @param string $string username
  142. * @return boolean
  143. */
  144. public function setFirstname($string = '') {
  145. $this->fetchData();
  146. $length = strlen(trim($string));
  147. if ($length < 0 || $length > 200) { return FALSE; }
  148. $this->firstname = (string) $string;
  149. return TRUE;
  150. } // end function
  151. /**
  152. * sets the last name. checks if min/max length is ok
  153. * @param string $string username
  154. * @return boolean
  155. */
  156. public function setLastname($string = '') {
  157. $length = strlen(trim($string));
  158. $this->fetchData();
  159. if ($length < 0 || $length > 200) { return FALSE; }
  160. $this->lastname = (string) $string;
  161. return TRUE;
  162. } // end function
  163. /**
  164. * sets the email address. checks if it is a valid string and if it is unique in the DB
  165. * @param string $string string
  166. * @param boolean $bypass whether to bypass validation and DB check or not
  167. * @return int -1=not a vlaid string, -2=already exists in the DB, 1=OK
  168. */
  169. public function setEmail($string = '', $bypass = FALSE) {
  170. $length = strlen(trim($string));
  171. $this->fetchData();
  172. if ($length < 0 || $length > 255) { return FALSE; }
  173. if ($bypass == FALSE) {
  174. if (parent::isEmailString($string) == FALSE) {
  175. return -1;
  176. } // end if
  177. if (parent::getAccessSetting('unique_email') == TRUE) {
  178. $sql = 'SELECT COUNT(*) FROM `' . $this->tables['extended'] . '` WHERE `email` = "' . parent::getConn()->real_escape_string($string) . '" AND `user` <> ' . parent::getConn()->real_escape_string(parent::getID());
  179. $result = parent::getConn()->query($sql);
  180. $row = $result->fetch_row();
  181. if ($row[0] > 0) { return -2; } // end if
  182. $result->close();
  183. } // end if
  184. } // end if
  185. $this->email = (string) $string;
  186. return 1;
  187. } // end function
  188. /**
  189. * sets the address. checks if min/max length is ok
  190. * @param string $string string
  191. * @return boolean
  192. */
  193. public function setAddress($string = '') {
  194. $length = strlen(trim($string));
  195. $this->fetchData();
  196. if ($length < 0 || $length > 200) { return FALSE; }
  197. $this->address = (string) $string;
  198. return TRUE;
  199. } // end function
  200. /**
  201. * sets the ZIP code. checks if min/max length is ok
  202. * @param string $string string
  203. * @return boolean
  204. */
  205. public function setZIP($string = '') {
  206. $length = strlen(trim($string));
  207. $this->fetchData();
  208. if ($length < 0 || $length > 10) { return FALSE; }
  209. $this->zip = (string) $string;
  210. return TRUE;
  211. } // end function
  212. /**
  213. * sets the town. checks if min/max length is ok
  214. * @param string $string string
  215. * @return boolean
  216. */
  217. public function setTown($string = '') {
  218. $length = strlen(trim($string));
  219. $this->fetchData();
  220. if ($length < 0 || $length > 200) { return FALSE; }
  221. $this->town = (string) $string;
  222. return TRUE;
  223. } // end function
  224. /**
  225. * sets the country code
  226. * @param int $int the country code
  227. * @param boolean $bypass whether to bypass validation and DB check or not
  228. * @return boolean
  229. */
  230. public function setCountry($int = 0, $bypass = FALSE) {
  231. $int = (int) $int;
  232. if ($int < 0 || $int > 9999) { return FALSE; }
  233. if ($bypass == FALSE) {
  234. // hier ev code f?r country?berpr?fung einbauen
  235. } // end if
  236. $this->fetchData();
  237. $this->country = (int) $int;
  238. return TRUE;
  239. } // end function
  240. /**
  241. * sets the phone number
  242. * @param string $string
  243. * @return boolean
  244. */
  245. public function setPhone($string = '') {
  246. $length = strlen(trim($string));
  247. if ($length < 0 || $length > 50) { return FALSE; }
  248. $this->fetchData();
  249. $this->phone = (string) $string;
  250. return TRUE;
  251. } // end function
  252. /**#@+
  253. * getter methods for user vars
  254. * @return mixed
  255. */
  256. public function getFirstname() {
  257. $this->fetchData();
  258. return $this->firstname;
  259. } // end function
  260. public function getLastname() {
  261. $this->fetchData();
  262. return $this->lastname;
  263. } // end function
  264. public function getEmail() {
  265. $this->fetchData();
  266. return $this->email;
  267. } // end function
  268. public function getAddress() {
  269. $this->fetchData();
  270. return $this->address;
  271. } // end function
  272. public function getZIP() {
  273. $this->fetchData();
  274. return $this->zip;
  275. } // end function
  276. public function getTown() {
  277. $this->fetchData();
  278. return $this->town;
  279. } // end function
  280. public function getCountry() {
  281. $this->fetchData();
  282. return $this->country;
  283. } // end function
  284. public function getPhone() {
  285. $this->fetchData();
  286. return $this->phone;
  287. } // end function
  288. /**#@-*/
  289. /**
  290. * returns an array with all the data history for the user
  291. * @param boolean $force_update
  292. * @return array
  293. */
  294. public function getDataHistoryExtended($force_update = FALSE) {
  295. if ($force_update == FALSE && isset($this->history_extended)) {
  296. return $this->history_extended;
  297. } // end if
  298. $sql = 'SELECT `firstname`, `lastname`, `email`, `address`, `zip`, `town`, `country`, `phone`, `date`, INET_NTOA(`ip`) as `ip` FROM `' . $this->tables['history_extended'] . '` WHERE `user` = ' . parent::getConn()->real_escape_string($this->getID()) . ' ORDER BY `date` DESC';
  299. $result = parent::getConn()->query($sql);
  300. $this->history_extended = array();
  301. while($row = $result->fetch_row()) {
  302. $entry['firstname'] = $row[0];
  303. $entry['lastname'] = $row[1];
  304. $entry['email'] = $row[2];
  305. $entry['address'] = $row[3];
  306. $entry['zip'] = $row[4];
  307. $entry['town'] = $row[5];
  308. $entry['country'] = $row[6];
  309. $entry['phone'] = $row[7];
  310. $entry['date'] = $row[8];
  311. $entry['ip'] = $row[9];
  312. $this->history_extended[] = $entry;
  313. } // end while
  314. $result->close();
  315. return $this->history_extended;
  316. } // end function
  317. /**
  318. * rewrites the data before the current changes back to the user table
  319. * @param boolean $bypass whether the base data should be revived too or not
  320. * @return boolean returns false if there was a DB write error
  321. */
  322. public function reviveLastData($bypass = TRUE) {
  323. $bypass = (boolean) $bypass;
  324. if ($bypass == FALSE) {
  325. if (parent::reviveLastData() == FALSE) {return FALSE;} // end if
  326. } else {
  327. $this->saveSnapshot(TRUE);
  328. } // end if
  329. $old_rec = FALSE;
  330. $sql = 'SELECT `firstname`, `lastname`, `email`, `address`, `zip`, `town`, `country`, `phone` FROM `' . $this->tables['history_extended'] . '` WHERE `user` = ' . parent::getConn()->real_escape_string($this->getID()) . ' ORDER BY `date` DESC LIMIT 2';
  331. $result = parent::getConn()->query($sql);
  332. while($row = $result->fetch_row()) {
  333. if ($old_rec == FALSE) {
  334. $old_rec = TRUE;
  335. continue;
  336. } // end if
  337. $sql2 = 'UPDATE `' . $this->tables['extended'] . '` SET ';
  338. $sql2 .= '`firstname` = "' . $row[0] . '", `lastname` = "' . $row[1] . '", `email` = "' . $row[2] . '", `address` = "' . $row[3] . '", `zip` = "' . $row[4] . '", `town` = "' . $row[5] . '", `country` = ' . $row[6] . ', `phone` = "' . $row[7] . '" ';
  339. $sql2 .= 'WHERE `user` = ' . parent::getConn()->real_escape_string($this->getID());
  340. } // end while
  341. $result->close();
  342. $result = parent::getConn()->query($sql2);
  343. if (!$result) {return FALSE;}
  344. if (parent::updateMetadataLastChange() == FALSE) {
  345. return FALSE;
  346. } // end if
  347. return TRUE;
  348. } // end function
  349. } // end class
  350. ?>