Advertisement
Guest User

porcodio

a guest
Feb 11th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. public function caricaUser($username, $password) {
  2. $mysqli = DataBase::getInstance()->connectDb();
  3. if (!isset($mysqli)) {
  4. error_log("[loadUser] impossibile inizializzare il database");
  5. $mysqli->close();
  6. return null;
  7. }
  8. // cerco corrispondenza nella tabella admin
  9. $query = "select
  10. admin.id admin_id,
  11. user.username admin_username,
  12. user.password admin_password
  13. from admin
  14. join user on admin.user_id = user.id
  15. where user.username = ? and user.password = ?";
  16. $stmt = $mysqli->stmt_init();
  17. $stmt->prepare($query);
  18. if (!$stmt) {
  19. error_log("[loadUser] impossibile" .
  20. " inizializzare il prepared statement");
  21. $mysqli->close();
  22. return null;
  23. }
  24. if (!$stmt->bind_param('ss',$username, $password)) {
  25. error_log("[loadUser] impossibile".
  26. " effettuare il binding in input");
  27. $mysqli->close();
  28. return null;
  29. }
  30. //print_r($stmt->prepare($query));
  31. $admin = self::caricaAdminDaStmt($stmt);
  32. if (isset($admin)) {
  33. // ho trovato un utente admin
  34. $mysqli->close();
  35. return $admin;
  36. }
  37. // cerco corrispondenza nella tabella direttore
  38. $query = "select"
  39. ."direttore.id direttore_id,"
  40. ."user.username direttore_username,"
  41. ."user.password direttore_password"
  42. ."from direttore"
  43. ."join user on direttore.user_id = user.id"
  44. ."where user.username = ? and user.password = ?";
  45. $stmt = $mysqli->stmt_init();
  46. $stmt->prepare($query);
  47. if (!$stmt) {
  48. error_log("[loadUser] impossibile" .
  49. " inizializzare il prepared statement");
  50. $mysqli->close();
  51. return null;
  52. }
  53. if (!$stmt->bind_param('ss', $username, $password)) {
  54. error_log("[loadUser] impossibile" .
  55. " effettuare il binding in input");
  56. $mysqli->close();
  57. return null;
  58. }
  59. $direttore = self::caricaDirettoreDaStmt($stmt);
  60. if (isset($direttore)) {
  61. // ho trovato un utente admin
  62. $mysqli->close();
  63. return $direttore;
  64. }
  65. // cerco corrispondenza nella tabella preposto
  66. $query = "select
  67. preposto.id preposto_id,
  68. user.username preposto_username,
  69. user.password preposto_password
  70. from preposto
  71. join user on preposto.user_id = user.id
  72. where user.username = ? and user.password = ?";
  73. $stmt = $mysqli->stmt_init();
  74. $stmt->prepare($query);
  75. if (!$stmt) {
  76. error_log("[loadUser] impossibile" .
  77. " inizializzare il prepared statement");
  78. $mysqli->close();
  79. return null;
  80. }
  81. if (!$stmt->bind_param('ss', $username, $password)) {
  82. error_log("[loadUser] impossibile" .
  83. " effettuare il binding in input");
  84. $mysqli->close();
  85. return null;
  86. }
  87. $preposto = self::caricaPrepostoDaStmt($stmt);
  88. if (isset($preposto)) {
  89. // ho trovato un utente admin
  90. $mysqli->close();
  91. return $preposto;
  92. }
  93. // cerco corrispondenza nella tabella user
  94. $query = "select
  95. id user_id,
  96. username user_username,
  97. password user_password
  98. from user where username = ? and password = ?";
  99. $stmt = $mysqli->stmt_init();
  100. $stmt->prepare($query);
  101. if (!$stmt) {
  102. error_log("[loadUser] impossibile" .
  103. " inizializzare il prepared statement");
  104. $mysqli->close();
  105. return null;
  106. }
  107. if (!$stmt->bind_param('ss', $username, $password)) {
  108. error_log("[loadUser] impossibile" .
  109. " effettuare il binding in input");
  110. $mysqli->close();
  111. return null;
  112. }
  113. $user = self::caricaUserDaStmt($stmt);
  114. if (isset($user)) {
  115. // ho trovato un utente
  116. $mysqli->close();
  117. return $user;
  118. }
  119. }
  120.  
  121.  
  122. public function cercaUtentePerId($id, $role) {
  123. $intval = filter_var($id, FILTER_NULL_ON_FAILURE);
  124. if (!isset($intval)) {
  125. return null;
  126. }
  127. $mysqli = DataBase::getInstance()->connectDb();
  128. if (!isset($mysqli)) {
  129. error_log("[cercaUtentePerId] impossibile inizializzare il database");
  130. $mysqli->close();
  131. return null;
  132. }
  133. //controllo il ruolo
  134. switch ($role) {
  135. case User::Utente:
  136. $query = "select
  137. id user_id,
  138. username user_username,
  139. password user_password
  140. from user
  141. where id = ?";
  142. $stmt = $mysqli->stmt_init();
  143. $stmt->prepare($query);
  144. if (!$stmt) {
  145. error_log("[cercaUtentePerId] impossibile" .
  146. " inizializzare il prepared statement");
  147. $mysqli->close();
  148. return null;
  149. }
  150. if (!$stmt->bind_param('i', $intval)) {
  151. error_log("[cercaUtentePerId] impossibile" .
  152. " effettuare il binding in input");
  153. $mysqli->close();
  154. return null;
  155. }
  156. $toRet = self::caricaUserDaStmt($stmt);
  157. $mysqli->close();
  158. return $toRet;
  159. break;
  160. case User::Preposto:
  161. $query = "select
  162. preposto.id preposto_id,
  163. username preposto_username,
  164. password preposto_password
  165. from preposto
  166. join user on preposto.user_id = user.id
  167. where preposto.id = ?";
  168. $stmt = $mysqli->stmt_init();
  169. $stmt->prepare($query);
  170. if (!$stmt) {
  171. error_log("[cercaPrepostoPerId] impossibile" .
  172. " inizializzare il prepared statement");
  173. $mysqli->close();
  174. return null;
  175. }
  176. if (!$stmt->bind_param('i', $intval)) {
  177. error_log("[cercaPrepostoPerId] impossibile" .
  178. " effettuare il binding in input");
  179. $mysqli->close();
  180. return null;
  181. }
  182. $toRet = self::caricaPrepostoDaStmt($stmt);
  183. $mysqli->close();
  184. return $toRet;
  185. break;
  186. case User::Direttore:
  187. $query = "select
  188. direttore.id direttore_id,
  189. username direttore_username,
  190. password direttore_password
  191. from direttore
  192. join user on direttore.user_id = user.id
  193. where direttore.id = ?";
  194. $stmt = $mysqli->stmt_init();
  195. $stmt->prepare($query);
  196. if (!$stmt) {
  197. error_log("[cercaDirettorePerId] impossibile" .
  198. " inizializzare il prepared statement");
  199. $mysqli->close();
  200. return null;
  201. }
  202. if (!$stmt->bind_param('i', $intval)) {
  203. error_log("[cercaDirettorePerId] impossibile" .
  204. " effettuare il binding in input");
  205. $mysqli->close();
  206. return null;
  207. }
  208. $toRet = self::caricaDirettoreDaStmt($stmt);
  209. $mysqli->close();
  210. return $toRet;
  211. break;
  212. case User::Amministratore:
  213. $query = "select
  214. admin.id admin_id,
  215. user.username admin_username,
  216. user.password admin_password
  217. from admin
  218. join user on admin.user_id = user.id
  219. where admin.id = ?";
  220. $stmt = $mysqli->stmt_init();
  221. $stmt->prepare($query);
  222. if (!$stmt) {
  223. error_log("[cercaAdminPerId] impossibile" .
  224. " inizializzare il prepared statement");
  225. $mysqli->close();
  226. return null;
  227. }
  228. if (!$stmt->bind_param('i', $intval)) {
  229. error_log("[loadAdmin] impossibile" .
  230. " effettuare il binding in input");
  231. $mysqli->close();
  232. return null;
  233. }
  234. $toRet = self::caricaAdminDaStmt($stmt);
  235. $mysqli->close();
  236. return $toRet;
  237. break;
  238. default: return null;
  239. }
  240. }
  241.  
  242. private function caricaAdminDaStmt(mysqli_stmt $stmt) {
  243. if (!$stmt->execute()) {
  244. error_log("[caricaAdminDaStmt] impossibile" .
  245. " eseguire lo statement");
  246. return null;
  247. }
  248. $row = array();
  249. $bind = $stmt->bind_result(
  250. $row['admin_id'],
  251. $row['admin_username'],
  252. $row['admin_password'] );
  253. if (!$bind) {
  254. error_log("[caricaAdminDaStmt] impossibile" .
  255. " effettuare il binding in output");
  256. return null;
  257. }
  258. if (!$stmt->fetch()) {
  259. return null;
  260. }
  261. $stmt->close();
  262. return self::creaAdminDaArray($row);
  263. }
  264.  
  265. public function creaAdminDaArray($row) {
  266. $admin = new Admin();
  267. $admin->setId($row['admin_id']);
  268. $admin->setUsername($row['admin_username']);
  269. $admin->setPassword($row['admin_password']);
  270. $admin->setRuolo(User::Amministratore);
  271. return $admin;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement