PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/db/db.php

https://bitbucket.org/houmanf/meyoubook
PHP | 479 lines | 402 code | 74 blank | 3 comment | 70 complexity | 5f582a725246531bd9b853ef159f53d0 MD5 | raw file
  1. <?php
  2. include_once $_SERVER['DOCUMENT_ROOT']."/config.php";
  3. class db {
  4. public static function insertBook($book) {
  5. $err = false;
  6. $isbns = explode(", ", $book["isbn"]);
  7. $isbn10 = $isbns[0];
  8. $isbn13 = $isbns[1];
  9. $length = str_replace(" pages","",$book['length']);
  10. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  11. if (!$con) {
  12. $err = true;
  13. }
  14. mysql_select_db(DBNAME, $con);
  15. if (!$err) {
  16. // scape the bad chars
  17. $isbn10 = mysql_escape_string($isbn10);
  18. $isbn13 = mysql_escape_string($isbn13);
  19. $title = mysql_escape_string($book['title']);
  20. $authors = mysql_escape_string($book['authors']);
  21. $publisher = mysql_escape_string($book['publisher']);
  22. $subjects = mysql_escape_string($book['subjects']);
  23. $editors = mysql_escape_string($book['editors']);
  24. $year = mysql_escape_string($book['year']);
  25. $cloud = mysql_escape_string($book['cloud']);
  26. $length = mysql_escape_string($book['length']);
  27. $bookGoogleID = mysql_escape_string($book['bookGoogleID']);
  28. $frontCover = mysql_escape_string($book['frontCover']);
  29. // save in the db
  30. $q1 = "INSERT INTO `book` (`isbn10`,`isbn13`,`title`,`authors`,`publisher`,`subjects`,`editors`,`year`,`cloud`,`length`,`bookgoogleid`,`frontcover`) VALUES ('" .$isbn10. "','" .$isbn13. "','" .$title. "','" .$authors. "','" .$publisher. "','" .$subjects. "','" .$editors. "','" .$year. "','" .$cloud. "','" .$length. "','" .$bookGoogleID. "','" .$frontCover. "')";
  31. $r1 = mysql_query($q1);
  32. }
  33. mysql_close($con);
  34. if ($err) {
  35. return false;
  36. } else {
  37. return true;
  38. }
  39. }
  40. public static function searchByISBN ($isbn) {
  41. $err = false;
  42. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  43. if (!$con) {
  44. $err = true;
  45. }
  46. mysql_select_db(DBNAME, $con);
  47. if (!$err) {
  48. $isbn = mysql_real_escape_string($isbn);
  49. $q1 = "SELECT `title`,`authors`,`publisher`,`length`,`isbn10`, `isbn13`, `frontcover` FROM `book` WHERE `isbn10`='".$isbn."' OR `isbn13`='".$isbn."';";
  50. $r1 = mysql_query($q1);
  51. if (mysql_num_rows($r1)==1) {
  52. $row = mysql_fetch_array($r1);
  53. $length = "n/a";
  54. if ($row["length"]>0) {
  55. $length = $row["length"]." pages";
  56. }
  57. $book = array("isbn" => $row["isbn10"].", ".$row["isbn13"], "title" => $row["title"], "authors" => $row["authors"], "publisher" => $row["publisher"], "length" => $length, "frontCover" => $row["frontcover"]);
  58. } else {
  59. $book = false;
  60. }
  61. }
  62. mysql_close($con);
  63. if (!$err) {
  64. return $book;
  65. } else {
  66. return false;
  67. }
  68. }
  69. public static function insertDistances($values) {
  70. $err = array();
  71. $q = "INSERT INTO `distances` (`zipcode1`,`zipcode2`,`distance`) VALUES ".$values.";";
  72. $r = mysql_query($q);
  73. if (!$r) {
  74. $err['insert'] = 1;
  75. }
  76. if ($err==null) {
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. public static function addSellingBook($isbn, $email, $zipcode, $price, $condition, $pin) {
  83. $err = array();
  84. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  85. if (!$con) {
  86. $err['sqlConnection'] = 1;
  87. }
  88. mysql_select_db(DBNAME, $con);
  89. if ($err==null) {
  90. if (!is_numeric($price)) {
  91. $err['price']=1;
  92. }
  93. if ($err==null) {
  94. $isbn = mysql_real_escape_string($isbn);
  95. $email = mysql_real_escape_string($email);
  96. $zipcode = mysql_real_escape_string($zipcode);
  97. $price = mysql_real_escape_string($price);
  98. $condition = mysql_real_escape_string($condition);
  99. $pin = mysql_real_escape_string($pin);
  100. $startTrans = "START TRANSACTION";
  101. mysql_query($startTrans);
  102. $autoCommit = "SET autocommit = 0";
  103. mysql_query($autoCommit);
  104. $q1 = "INSERT IGNORE INTO `user` (`email`) VALUES ('" .$email. "');";
  105. $q2 = "INSERT IGNORE INTO `UB` (`uid`,`bid`,`zipcode`,`price`, `condition`, `pin`) VALUES ((SELECT uid FROM `user` WHERE email='" .$email. "'),(SELECT bid FROM `book` WHERE isbn10='" .$isbn. "' OR isbn13='" .$isbn. "'),'" .$zipcode. "','" .$price. "','" .$condition. "','" .$pin. "');";
  106. $r1 = mysql_query($q1);
  107. $r2 = mysql_query($q2);
  108. $ubid = mysql_insert_id($con);
  109. if (!$r1 || !$r2) {
  110. $err['insert'] = 1;
  111. $rollbackTrans = "ROLLBACK;";
  112. mysql_query($rollbackTrans);
  113. } else {
  114. $commitTrans = "COMMIT;";
  115. mysql_query($commitTrans);
  116. }
  117. }
  118. }
  119. // print_r($err);
  120. mysql_close($con);
  121. if ($err==null) {
  122. return $ubid;
  123. } else {
  124. return false;
  125. }
  126. }
  127. public static function searchByTitle ($title,$zipcode) {
  128. $err = array();
  129. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  130. if (!$con) {
  131. $err['sqlConnection'] = 1;
  132. }
  133. mysql_select_db(DBNAME, $con);
  134. if ($err==null) {
  135. $title = mysql_real_escape_string($title);
  136. $zipcode = mysql_real_escape_string($zipcode);
  137. if (strlen($title)==0) {
  138. $q1 = "SELECT * FROM (SELECT zipcode2, distance FROM `distances` WHERE zipcode1='".$zipcode."') d INNER JOIN `UB` ub INNER JOIN `book` b INNER JOIN `user` u
  139. ON ub.uid = u.uid AND ub.bid = b.bid AND d.zipcode2=ub.zipcode
  140. WHERE ub.available = 1
  141. ORDER BY b.title, price ASC, d.distance LIMIT ".SEARCH_TITLE_LIMIT.";";
  142. } else {
  143. $q1 = "SELECT * FROM (SELECT zipcode2, distance FROM `distances` WHERE zipcode1='".$zipcode."') d INNER JOIN `UB` ub INNER JOIN `book` b INNER JOIN `user` u INNER JOIN `book_FT` bft
  144. ON ub.uid = u.uid AND ub.bid = b.bid AND d.zipcode2=ub.zipcode AND bft.bid=ub.bid
  145. WHERE ub.available = 1 AND MATCH (bft.title) AGAINST ('".$title."')
  146. ORDER BY b.title, price ASC, d.distance LIMIT ".SEARCH_TITLE_LIMIT.";";
  147. }
  148. $r1 = mysql_query($q1);
  149. if (!$r1) {
  150. $err['select'] = 1;
  151. } else {
  152. $books = $r1;
  153. }
  154. }
  155. mysql_close($con);
  156. if ($err==null) {
  157. return $books;
  158. } else {
  159. return false;
  160. }
  161. }
  162. static function insertUBUandUBUE ($buyerEmail, $ubid, $message, $spin, $bpin) {
  163. $err = null;
  164. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  165. if (!$con) {
  166. $err['sqlConnection'] = 1;
  167. }
  168. mysql_select_db(DBNAME, $con);
  169. if ($err==null) {
  170. $buyerEmail = mysql_real_escape_string($buyerEmail);
  171. $ubid = mysql_real_escape_string($ubid);
  172. $message = mysql_real_escape_string($message);
  173. $spin = mysql_real_escape_string($spin);
  174. $bpin = mysql_real_escape_string($bpin);
  175. $startTrans = "START TRANSACTION";
  176. mysql_query($startTrans);
  177. $autoCommit = "SET autocommit = 0";
  178. mysql_query($autoCommit);
  179. $q1 = "INSERT IGNORE INTO `user` (`email`) VALUES ('" .$buyerEmail. "');";
  180. $r1 = mysql_query($q1);
  181. $q2 = "INSERT IGNORE INTO `UBU` (`ubid`,`uid`,`spin`,`bpin`) VALUES (" .$ubid. ", (SELECT uid FROM `user` WHERE email='" .$buyerEmail. "'),'".$spin."','".$bpin."');";
  182. $r2 = mysql_query($q2);
  183. $ubuid = mysql_insert_id($con);
  184. $q3 = "INSERT INTO `email` (`message`) VALUES ('" .$message. "');";
  185. $r3 = mysql_query($q3);
  186. $eid = mysql_insert_id($con);
  187. $q4 = "INSERT INTO `UBUE` (`ubuid`,`eid`) VALUES (".$ubuid.",".$eid.");";
  188. $r4 = mysql_query($q4);
  189. if (!$r1 || !$r2 || !$r3 || !$r4) {
  190. $err['insert'] = 1;
  191. $rollbackTrans = "ROLLBACK;";
  192. mysql_query($rollbackTrans);
  193. } else {
  194. $commitTrans = "COMMIT;";
  195. mysql_query($commitTrans);
  196. }
  197. }
  198. return (($err==null) ? $ubuid : false);
  199. }
  200. static function insertUBUE ($ubuid, $message) {
  201. $err = array();
  202. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  203. if (!$con) {
  204. $err['sqlConnection'] = 1;
  205. }
  206. mysql_select_db(DBNAME, $con);
  207. if ($err==null) {
  208. $ubuid = mysql_real_escape_string($ubuid);
  209. $message = mysql_real_escape_string($message);
  210. mysql_query("START TRANSACTION");
  211. mysql_query("SET autocommit = 0");
  212. $q1 = "INSERT INTO `email` (`message`) VALUES ('" .$message. "');";
  213. $r1 = mysql_query($q1);
  214. $eid = mysql_insert_id($con);
  215. $q2 = "INSERT INTO `UBUE` (`ubuid`,`eid`) VALUES (".$ubuid.",".$eid.");";
  216. $r2 = mysql_query($q2);
  217. if (!$r1 || !$r2) {
  218. $err['insert'] = 1;
  219. mysql_query("ROLLBACK;");
  220. } else {
  221. mysql_query("COMMIT;");
  222. }
  223. }
  224. return (($err==null) ? $ubuid : false);
  225. }
  226. static function getUB ($ubid) {
  227. $err = array();
  228. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  229. if (!$con) {
  230. $err['sqlConnection'] = 1;
  231. }
  232. mysql_select_db(DBNAME, $con);
  233. if ($err==null) {
  234. $ubid = mysql_real_escape_string($ubid);
  235. $q1 = "SELECT `frontCover`, `price`, `condition`, `title`, `authors`, `isbn10`, `isbn13`, `publisher`, `length`, `email`, `zipcode`, ub.`available`, `pin` as ubpin FROM `UB` ub INNER JOIN `book` b INNER JOIN `user` u ON ub.bid = b.bid AND ub.uid = u.uid
  236. WHERE ub.id='".$ubid."';";
  237. $r1 = mysql_query($q1);
  238. if (mysql_num_rows($r1)==1) {
  239. $row = mysql_fetch_array($r1);
  240. switch ($row['condition']) {
  241. case 'n' :
  242. $condition = 'New';
  243. break;
  244. case 'ln' :
  245. $condition = 'Like new';
  246. break;
  247. case 'vg' :
  248. $condition = 'Very good';
  249. break;
  250. case 'g' :
  251. $condition = 'Good';
  252. break;
  253. case 'b' :
  254. $condition = 'Bad';
  255. break;
  256. }
  257. $length = "n/a";
  258. if ($row["length"]>0) {
  259. $length = $row["length"]." pages";
  260. }
  261. $ub = array("frontCover" => $row["frontCover"], "price" => $row["price"], "condition" => $condition, "title" => $row["title"], "authors" => $row["authors"], "isbn10" => $row["isbn10"], "isbn13" => $row["isbn13"], "publisher" => $row["publisher"], "length" => $length, "email" => $row["email"], "zipcode" => $row["zipcode"], "available" => $row["available"], "ubPin" => $row["ubpin"]);
  262. } else {
  263. $ub = false;
  264. }
  265. }
  266. return $ub;
  267. }
  268. static function getUBdoubleCheck ($ubid, $pin) {
  269. $err = array();
  270. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  271. if (!$con) {
  272. $err['sqlConnection'] = 1;
  273. }
  274. mysql_select_db(DBNAME, $con);
  275. if ($err==null) {
  276. $ubid = mysql_real_escape_string($ubid);
  277. $pin = mysql_real_escape_string($pin);
  278. $q1 = "SELECT `frontCover`, `price`, `condition`, `title`, `authors`, `isbn10`, `isbn13`, `publisher`, `length`, `email`, `zipcode`, ub.`available`, `pin` as ubpin FROM `UB` ub INNER JOIN `book` b INNER JOIN `user` u ON ub.bid = b.bid AND ub.uid = u.uid
  279. WHERE ub.id='".$ubid."' AND ub.pin='".$pin."';";
  280. $r1 = mysql_query($q1);
  281. if (mysql_num_rows($r1)==1) {
  282. $row = mysql_fetch_array($r1);
  283. switch ($row['condition']) {
  284. case 'n' :
  285. $condition = 'New';
  286. break;
  287. case 'ln' :
  288. $condition = 'Like new';
  289. break;
  290. case 'vg' :
  291. $condition = 'Very good';
  292. break;
  293. case 'g' :
  294. $condition = 'Good';
  295. break;
  296. case 'b' :
  297. $condition = 'Bad';
  298. break;
  299. }
  300. $length = "n/a";
  301. if ($row["length"]>0) {
  302. $length = $row["length"]." pages";
  303. }
  304. $ub = array("frontCover" => $row["frontCover"], "price" => $row["price"], "condition" => $condition, "title" => $row["title"], "authors" => $row["authors"], "isbn10" => $row["isbn10"], "isbn13" => $row["isbn13"], "publisher" => $row["publisher"], "length" => $length, "email" => $row["email"], "zipcode" => $row["zipcode"], "available" => $row["available"], "ubPin" => $row["ubpin"]);
  305. } else {
  306. $ub = false;
  307. }
  308. }
  309. return $ub;
  310. }
  311. static function removeUB ($ubid, $pin) {
  312. $err = array();
  313. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  314. if (!$con) {
  315. $err['sqlConnection'] = 1;
  316. }
  317. mysql_select_db(DBNAME, $con);
  318. if ($err==null) {
  319. $ubid = mysql_real_escape_string($ubid);
  320. $pin = mysql_real_escape_string($pin);
  321. $q1 = "UPDATE `UB` ub SET available = '0'
  322. WHERE id='".$ubid."' AND pin='".$pin."';";
  323. $r1 = mysql_query($q1);
  324. if (mysql_affected_rows() != 1) {
  325. $err['alreadyRemoved'] = false;
  326. }
  327. if ($err==null) {
  328. $out = true;
  329. }
  330. } else {
  331. $our = false;
  332. }
  333. return $out;
  334. }
  335. static function getCommunicationInfo($ubuid,$pin) {
  336. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  337. if (!$con) {
  338. $err['sqlConnection'] = 1;
  339. }
  340. mysql_select_db(DBNAME, $con);
  341. if ($err==null) {
  342. $ubuid = mysql_real_escape_string($ubuid);
  343. $pin = mysql_real_escape_string($pin);
  344. $q1 = "SELECT useller.email semail, ub.id as ubid, ub.pin as ubpin, ubuyer.email bemail, e.message, e.date, spin, bpin, title, `condition`, price FROM `UBU` ubu INNER JOIN `UBUE` ubue INNER JOIN `UB` ub INNER JOIN `book` b INNER JOIN `email` e INNER JOIN `user` ubuyer INNER JOIN `user` useller
  345. ON ubu.ubid = ub.id AND ub.uid = useller.uid AND b.bid=ub.bid AND ubu.uid = ubuyer.uid AND ubue.ubuid = ubu.id AND ubue.eid = e.eid
  346. WHERE ubu.id = ".$ubuid." AND (spin = '".$pin."' OR bpin = '".$pin."') ORDER BY e.date DESC";
  347. $r1 = mysql_query($q1);
  348. $row = mysql_fetch_array($r1);
  349. $sEmail = $row['semail'];
  350. $bEmail = $row['bemail'];
  351. $sPin = $row['spin'];
  352. $bPin = $row['bpin'];
  353. $title = $row['title'];
  354. switch ($row['condition']) {
  355. case 'n' :
  356. $condition = 'New';
  357. break;
  358. case 'ln' :
  359. $condition = 'Like new';
  360. break;
  361. case 'vg' :
  362. $condition = 'Very good';
  363. break;
  364. case 'g' :
  365. $condition = 'Good';
  366. break;
  367. case 'b' :
  368. $condition = 'Bad';
  369. break;
  370. }
  371. $price = $row['price'];
  372. $history = $row['message'];
  373. $ubPin = $row['ubpin'];
  374. $ubid = $row['ubid'];
  375. while ($row = mysql_fetch_array($r1)) {
  376. $history .= "\r\n--------------------\r\n".$row['message'];
  377. }
  378. $out = array('sEmail' => $sEmail, 'bEmail' => $bEmail, 'history' => $history, 'sPin' => $sPin, 'bPin' => $bPin, 'title' => $title, 'price' => $price, 'condition' => $condition, 'ubPin' => $ubPin, 'ubid' => $ubid);
  379. }
  380. return (($err==null) ? $out : false);
  381. }
  382. static function validZipcode($zipcode) {
  383. $err = array();
  384. $con = mysql_connect(SQLSERVER,SQLUSER,SQLPASS);
  385. if (!$con) {
  386. $err['sqlConnection'] = 1;
  387. }
  388. mysql_select_db(DBNAME, $con);
  389. if ($err==null) {
  390. $zipcode = mysql_real_escape_string($zipcode);
  391. $q1 = "SELECT `zipcode` FROM `zipcode` WHERE zipcode='".$zipcode."';";
  392. $r1 = mysql_query($q1);
  393. if (!$r1) {
  394. $err['select'] = 1;
  395. }
  396. if (mysql_num_rows($r1)==1) {
  397. $out = true;
  398. }
  399. }
  400. if (!$err==null) {
  401. $out = false;
  402. }
  403. return $out;
  404. }
  405. }