PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/system/server/flex/UsersService.php

https://github.com/ffiadmin/Denial-Detour
PHP | 259 lines | 121 code | 54 blank | 84 comment | 7 complexity | fd46581df7867e14616b9e97a8e07749 MD5 | raw file
  1. <?php
  2. /**
  3. * README for sample service
  4. *
  5. * This generated sample service contains functions that illustrate typical service operations.
  6. * Use these functions as a starting point for creating your own service implementation. Modify the
  7. * function signatures, references to the database, and implementation according to your needs.
  8. * Delete the functions that you do not use.
  9. *
  10. * Save your changes and return to Flash Builder. In Flash Builder Data/Services View, refresh
  11. * the service. Then drag service operations onto user interface components in Design View. For
  12. * example, drag the getAllItems() operation onto a DataGrid.
  13. *
  14. * This code is for prototyping only.
  15. *
  16. * Authenticate the user prior to allowing them to call these methods. You can find more
  17. * information at http://www.adobe.com/go/flex_security
  18. *
  19. */
  20. class UsersService {
  21. var $username = "root";
  22. var $password = "Oliver99";
  23. var $server = "localhost";
  24. var $port = "3306";
  25. var $databasename = "denial-detour";
  26. var $tablename = "users";
  27. var $connection;
  28. /**
  29. * The constructor initializes the connection to database. Everytime a request is
  30. * received by Zend AMF, an instance of the service class is created and then the
  31. * requested method is invoked.
  32. */
  33. public function __construct() {
  34. $this->connection = mysqli_connect(
  35. $this->server,
  36. $this->username,
  37. $this->password,
  38. $this->databasename,
  39. $this->port
  40. );
  41. $this->throwExceptionOnError($this->connection);
  42. }
  43. /**
  44. * Returns all the rows from the table.
  45. *
  46. * Add authroization or any logical checks for secure access to your data
  47. *
  48. * @return array
  49. */
  50. public function getAllUsers() {
  51. $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");
  52. $this->throwExceptionOnError();
  53. mysqli_stmt_execute($stmt);
  54. $this->throwExceptionOnError();
  55. $rows = array();
  56. mysqli_stmt_bind_result($stmt, $row->id, $row->username, $row->password, $row->first, $row->last);
  57. while (mysqli_stmt_fetch($stmt)) {
  58. $rows[] = $row;
  59. $row = new stdClass();
  60. mysqli_stmt_bind_result($stmt, $row->id, $row->username, $row->password, $row->first, $row->last);
  61. }
  62. mysqli_stmt_free_result($stmt);
  63. mysqli_close($this->connection);
  64. return $rows;
  65. }
  66. /**
  67. * Returns the item corresponding to the value specified for the primary key.
  68. *
  69. * Add authorization or any logical checks for secure access to your data
  70. *
  71. *
  72. * @return stdClass
  73. */
  74. public function getUsersByID($itemID) {
  75. $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where id=?");
  76. $this->throwExceptionOnError();
  77. mysqli_stmt_bind_param($stmt, 'i', $itemID);
  78. $this->throwExceptionOnError();
  79. mysqli_stmt_execute($stmt);
  80. $this->throwExceptionOnError();
  81. mysqli_stmt_bind_result($stmt, $row->id, $row->username, $row->password, $row->first, $row->last);
  82. if(mysqli_stmt_fetch($stmt)) {
  83. return $row;
  84. } else {
  85. return null;
  86. }
  87. }
  88. /**
  89. * Returns the item corresponding to the value specified for the primary key.
  90. *
  91. * Add authorization or any logical checks for secure access to your data
  92. *
  93. *
  94. * @return stdClass
  95. */
  96. public function createUsers($item) {
  97. $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (username, password, first, last) VALUES (?, ?, ?, ?)");
  98. $this->throwExceptionOnError();
  99. mysqli_stmt_bind_param($stmt, 'ssss', $item->username, $item->password, $item->first, $item->last);
  100. $this->throwExceptionOnError();
  101. mysqli_stmt_execute($stmt);
  102. $this->throwExceptionOnError();
  103. $autoid = mysqli_stmt_insert_id($stmt);
  104. mysqli_stmt_free_result($stmt);
  105. mysqli_close($this->connection);
  106. return $autoid;
  107. }
  108. /**
  109. * Updates the passed item in the table.
  110. *
  111. * Add authorization or any logical checks for secure access to your data
  112. *
  113. * @param stdClass $item
  114. * @return void
  115. */
  116. public function updateUsers($item) {
  117. $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET username=?, password=?, first=?, last=? WHERE id=?");
  118. $this->throwExceptionOnError();
  119. mysqli_stmt_bind_param($stmt, 'ssssi', $item->username, $item->password, $item->first, $item->last, $item->id);
  120. $this->throwExceptionOnError();
  121. mysqli_stmt_execute($stmt);
  122. $this->throwExceptionOnError();
  123. mysqli_stmt_free_result($stmt);
  124. mysqli_close($this->connection);
  125. }
  126. /**
  127. * Deletes the item corresponding to the passed primary key value from
  128. * the table.
  129. *
  130. * Add authorization or any logical checks for secure access to your data
  131. *
  132. *
  133. * @return void
  134. */
  135. public function deleteUsers($itemID) {
  136. $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE id = ?");
  137. $this->throwExceptionOnError();
  138. mysqli_stmt_bind_param($stmt, 'i', $itemID);
  139. mysqli_stmt_execute($stmt);
  140. $this->throwExceptionOnError();
  141. mysqli_stmt_free_result($stmt);
  142. mysqli_close($this->connection);
  143. }
  144. /**
  145. * Returns the number of rows in the table.
  146. *
  147. * Add authorization or any logical checks for secure access to your data
  148. *
  149. *
  150. */
  151. public function count() {
  152. $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
  153. $this->throwExceptionOnError();
  154. mysqli_stmt_execute($stmt);
  155. $this->throwExceptionOnError();
  156. mysqli_stmt_bind_result($stmt, $rec_count);
  157. $this->throwExceptionOnError();
  158. mysqli_stmt_fetch($stmt);
  159. $this->throwExceptionOnError();
  160. mysqli_stmt_free_result($stmt);
  161. mysqli_close($this->connection);
  162. return $rec_count;
  163. }
  164. /**
  165. * Returns $numItems rows starting from the $startIndex row from the
  166. * table.
  167. *
  168. * Add authorization or any logical checks for secure access to your data
  169. *
  170. *
  171. *
  172. * @return array
  173. */
  174. public function getUsers_paged($startIndex, $numItems) {
  175. $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
  176. $this->throwExceptionOnError();
  177. mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
  178. mysqli_stmt_execute($stmt);
  179. $this->throwExceptionOnError();
  180. $rows = array();
  181. mysqli_stmt_bind_result($stmt, $row->id, $row->username, $row->password, $row->first, $row->last);
  182. while (mysqli_stmt_fetch($stmt)) {
  183. $rows[] = $row;
  184. $row = new stdClass();
  185. mysqli_stmt_bind_result($stmt, $row->id, $row->username, $row->password, $row->first, $row->last);
  186. }
  187. mysqli_stmt_free_result($stmt);
  188. mysqli_close($this->connection);
  189. return $rows;
  190. }
  191. /**
  192. * Utility function to throw an exception if an error occurs
  193. * while running a mysql command.
  194. */
  195. private function throwExceptionOnError($link = null) {
  196. if($link == null) {
  197. $link = $this->connection;
  198. }
  199. if(mysqli_error($link)) {
  200. $msg = mysqli_errno($link) . ": " . mysqli_error($link);
  201. throw new Exception('MySQL Error - '. $msg);
  202. }
  203. }
  204. }
  205. ?>