PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/methods.php

https://bitbucket.org/rich90usa/war-of-worlds
PHP | 435 lines | 371 code | 53 blank | 11 comment | 30 complexity | f113a9a4b17e506fde8427a46132f676 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once('lib/phpass/PasswordHash.php');
  3. require_once('./smarty/smarty_main.inc');
  4. session_start();
  5. $db = null;
  6. $SUPERSECRETSALT = 'tariflovesthecolorpink';
  7. function getDB() {
  8. $link = mysql_connect('127.0.0.1:3306', 'wowuser', 'opensesame!');
  9. if (!$link) {
  10. die('Could not connect: ' . mysql_error());
  11. }
  12. //echo 'Connected successfully<br />';
  13. mysql_select_db('wowdb', $link);
  14. return $link;
  15. }
  16. function closeDB($link) {
  17. mysql_close($link);
  18. }
  19. function sql2json($query) {
  20. $data_sql = mysql_query($query) or die("'';//" . mysql_error());// If an error has occurred,
  21. // make the error a js comment so that a javascript error will NOT be invoked
  22. $json_str = ""; //Init the JSON string.
  23. if($total = mysql_num_rows($data_sql)) { //See if there is anything in the query
  24. $json_str .= "[\n";
  25. $row_count = 0;
  26. while($data = mysql_fetch_assoc($data_sql)) {
  27. if(count($data) > 1) $json_str .= "{\n";
  28. $count = 0;
  29. foreach($data as $key => $value) {
  30. //If it is an associative array we want it in the format of "key":"value"
  31. if(count($data) > 1) $json_str .= "\"$key\":\"$value\"";
  32. else $json_str .= "\"$value\"";
  33. //Make sure that the last item don't have a ',' (comma)
  34. $count++;
  35. if($count < count($data)) $json_str .= ",\n";
  36. }
  37. $row_count++;
  38. if(count($data) > 1) $json_str .= "}\n";
  39. //Make sure that the last item don't have a ',' (comma)
  40. if($row_count < $total) $json_str .= ",\n";
  41. }
  42. $json_str .= "]\n";
  43. }
  44. //Replace the '\n's - make it faster - but at the price of bad redability.
  45. $json_str = str_replace("\n","",$json_str); //Comment this out when you are debugging the script
  46. //Finally, output the data
  47. return $json_str;
  48. }
  49. function sql2array($query) {
  50. $result = mysql_query($query);
  51. $rows = array();
  52. while($r = mysql_fetch_assoc($result)) {
  53. $rows['result'][] = $r;
  54. }
  55. return $rows;
  56. }
  57. function login($username, $password) {
  58. $pwdHasher = new PasswordHash(8, FALSE);
  59. $result = null;
  60. $hashedPassword = $pwdHasher->HashPassword($password);
  61. $query = sprintf("SELECT UserID, Username, Password, isTechnician, LastLatitude, LastLongitude FROM users WHERE Username='%s'", $username, $hashedPassword);
  62. $result = sql2array($query);
  63. if (!isset($result['result'][0])) {
  64. return false;
  65. }
  66. $result = $result['result'][0];
  67. $checked = $pwdHasher->CheckPassword($password, $result['Password']);
  68. if ($checked === true) {
  69. $_SESSION['UserID'] = $result['UserID'];
  70. $_SESSION['isTechnician'] = $result['isTechnician'];
  71. $_SESSION['Latitude'] = $result['LastLatitude'];
  72. $_SESSION['Longitude'] = $result['LastLongitude'];
  73. return $result;
  74. } else {
  75. return false;
  76. }
  77. }
  78. function register($username, $password, $isTechnician) {
  79. $pwdHasher = new PasswordHash(8, FALSE);
  80. $result = null;
  81. $hashedPassword = $pwdHasher->HashPassword($password);
  82. $query = sprintf("INSERT INTO users (Username, Password, IsTechnician) VALUES('%s', '%s', '%d')", $username, $hashedPassword, $isTechnician);
  83. mysql_query($query);
  84. if (mysql_affected_rows() <= 0) {
  85. return array('result' => array('error' => "Couldn't Register Account"));
  86. } else {
  87. $userID = mysql_insert_id();
  88. $_SESSION['UserID'] = $userID;
  89. $_SESSION['isTechnician'] = $isTechnician;
  90. return array('result' => array('success' => true));
  91. }
  92. $checked = $pwdHasher->CheckPassword($password, $result['Password']);
  93. /*if ($checked === true) {
  94. $_SESSION['UserID'] = $result['UserID'];
  95. print_r($_SESSION);
  96. return $result;
  97. } else {
  98. return false;
  99. }*/
  100. }
  101. function setLocation($latitude, $longitude) {
  102. if (isset($_SESSION['UserID'])) {
  103. $userID = $_SESSION['UserID'];
  104. $result = null;
  105. $query = sprintf("UPDATE users SET LastLatitude='%s', LastLongitude='%s' WHERE UserID=%d", $latitude, $longitude, $userID);
  106. print_r($query);
  107. mysql_query($query);
  108. $_SESSION['Latitude'] = $latitude;
  109. $_SESSION['Longitude'] = $longitude;
  110. return array('result' => array('success' => true));
  111. } else {
  112. return array('result' => array('error' => "Must Be Logged In"));
  113. }
  114. return getItemByID($itemID);
  115. //$itemID = mysql_insert_id();
  116. //return getItemByID($itemID);
  117. }
  118. function addItem($name, $description, $vendorname, $scenario, $class, $cost) {
  119. $result = null;
  120. $query = sprintf("INSERT INTO items (Name, Description, VendorName, Scenario, Class, Cost) VALUES('%s', '%s', '%s', '%s', '%s', %d)", $name, $description, $vendorname, $scenario, $class, (int) $cost);
  121. mysql_query($query);
  122. if (mysql_affected_rows() <= 0) {
  123. return array('result' => array('error' => "Couldn't add Item"));
  124. } else {
  125. return array('result' => array('success' => $true));
  126. }
  127. }
  128. function editItem($itemID, $name, $description, $vendorname, $scenario, $class, $cost) {
  129. $result = null;
  130. $query = sprintf("UPDATE items SET Name='%s', Description='%s', VendorName='%s', Scenario='%s', Class='%s', Cost=%d WHERE ItemID=%d", $name, $description, $vendorname, $scenario, $class, $cost, $itemID);
  131. mysql_query($query);
  132. return getItemByID($itemID);
  133. //$itemID = mysql_insert_id();
  134. //return getItemByID($itemID);
  135. }
  136. function addItemImage($itemID, $itemImage, $fromTechnician) {
  137. $itemID = (int) $itemID;
  138. $query = sprintf("INSERT INTO itemImages (ItemID, ImageURL, fromTechnician) VALUES('%d', '%s', '%d')", $itemID, $itemImage, $fromTechnician);
  139. $result = mysql_query($query);
  140. if (mysql_affected_rows() <= 0) {
  141. return array('result' => array('error' => "Couldn't add itemImage"));
  142. } else {
  143. return array('result' => array('success' => true));
  144. }
  145. }
  146. function deleteItem($itemID) {
  147. $result = null;
  148. $query = sprintf("DELETE FROM items WHERE ItemID=%d", $itemID);
  149. $result = mysql_query($query);
  150. if (mysql_affected_rows() <= 0) {
  151. return array('result' => array('error' => "No affected item"));
  152. } else {
  153. return array('result' => array('success' => $result));
  154. }
  155. }
  156. function getAllItems() {
  157. $result = array('result' => null);
  158. $query = sprintf("SELECT * FROM items");
  159. $result = sql2array($query);
  160. return $result;
  161. }
  162. function getAttributesByItemID($itemID) {
  163. $result = array('result' => null);
  164. $itemID = (int) $itemID;
  165. $query = sprintf("SELECT * FROM itemAttributes WHERE ItemID='%d' ORDER BY Score DESC", $itemID);
  166. $result = sql2array($query);
  167. if (count($result) <= 0) {
  168. $result = array('error' => 'No itemAttributes for that ID');
  169. }
  170. return $result;
  171. }
  172. function getItemByID($itemID) {
  173. $result = array('result' => null);
  174. $itemID = (int) $itemID;
  175. //$query = sprintf("SELECT * FROM items WHERE ItemID=%d", $itemID);
  176. $query = sprintf("SELECT *, items.VendorName FROM items LEFT JOIN vendors ON vendors.VendorName=items.VendorName WHERE ItemID=%d", $itemID);
  177. $result = sql2array($query);
  178. if (count($result) <= 0) {
  179. $result = array('error' => 'No item for that ID');
  180. }
  181. return $result;
  182. }
  183. function getImagesByItemID($itemID) {
  184. $result = array('result' => null);
  185. $itemID = (int) $itemID;
  186. $query = sprintf("SELECT * FROM itemImages WHERE ItemID=%d ORDER BY fromTechnician DESC", $itemID);
  187. $result = sql2array($query);
  188. if (count($result) <= 0) {
  189. $result = array('error' => 'No images for that ID');
  190. }
  191. return $result;
  192. }
  193. function getItemsByVendorName($vendorName) {
  194. $result = array('result' => null);
  195. $query = sprintf("SELECT * FROM items WHERE VendorName='%s'", $vendorName);
  196. $result = sql2array($query);
  197. if (count($result) <= 0) {
  198. $result = array('error' => 'No items for vendor');
  199. }
  200. return $result;
  201. }
  202. function searchItemsByName($name) {
  203. $result = array('result' => null);
  204. $name = mysql_escape_string($name);
  205. $query = sprintf("SELECT * FROM items WHERE Name LIKE '%%%s%%' OR Description LIKE '%%%s%%' OR VendorName LIKE '%%%s%%' OR Scenario LIKE '%%%s%%' OR Class LIKE '%%%s%%'", $name, $name, $name, $name, $name);
  206. $result = sql2array($query);
  207. if (count($result) <= 0) {
  208. $result = array('error' => 'No Results');
  209. }
  210. return $result;
  211. }
  212. function searchItemsByPrice($minVal, $maxVal) {
  213. $result = array('result' => null);
  214. $name = mysql_escape_string($name);
  215. $query = sprintf("SELECT * FROM items WHERE Cost>='%d' AND Cost<='%d'", $minVal, $maxVal);
  216. $result = sql2array($query);
  217. if (count($result) <= 0) {
  218. $result = array('error' => 'No Results');
  219. }
  220. return $result;
  221. }
  222. function getAllVendors() {
  223. $result = array('result' => null);
  224. $query = sprintf("SELECT * FROM vendors");
  225. $result = sql2array($query);
  226. return $result;
  227. }
  228. function getVendorByID($vendorID) {
  229. $result = array('result' => null);
  230. $query = sprintf("SELECT * FROM vendors WHERE VendorID=%d", (int)$vendorID);
  231. $result = sql2array($query);
  232. return $result;
  233. }
  234. function addAttribute($itemID, $attributeName) {
  235. $result = null;
  236. $itemID = (int) $itemID;
  237. $query = sprintf("INSERT INTO itemAttributes (ItemID, AttributeName) VALUES('%d', '%s')", $itemID, $attributeName);
  238. mysql_query($query);
  239. if (mysql_affected_rows() <= 0) {
  240. return array('result' => array('error' => "Couldn't add Attribute"));
  241. } else {
  242. return array('result' => array('success' => true));
  243. }
  244. }
  245. function upVoteAttribute($attributeID) {
  246. $result = null;
  247. $attributeID = (int) $attributeID;
  248. $query = sprintf("UPDATE itemAttributes SET Score=Score + 1 WHERE AttributeID=%s", $attributeID);
  249. mysql_query($query);
  250. if (mysql_affected_rows() <= 0) {
  251. return array('result' => array('error' => "Couldn't upVote Attribute"));
  252. } else {
  253. return array('result' => array('success' => true));
  254. }
  255. }
  256. function downVoteAttribute($attributeID) {
  257. $result = null;
  258. $attributeID = (int) $attributeID;
  259. $query = sprintf("UPDATE itemAttributes SET Score=Score - 1 WHERE AttributeID=%s", $attributeID);
  260. mysql_query($query);
  261. if (mysql_affected_rows() <= 0) {
  262. return array('result' => array('error' => "Couldn't downVote Attribute"));
  263. } else {
  264. return array('result' => array('success' => true));
  265. }
  266. }
  267. function deleteAttribute($attributeID) {
  268. $result = null;
  269. $attributeID = (int) $attributeID;
  270. $query = sprintf("DELETE FROM itemAttributes WHERE AttributeID=%s AND Score<=0", $attributeID);
  271. mysql_query($query);
  272. if (mysql_affected_rows() <= 0) {
  273. return array('result' => array('error' => "Couldn't delete Attribute"));
  274. } else {
  275. return array('result' => array('success' => true));
  276. }
  277. }
  278. function addVendor($vendorName, $latitude, $longitude, $address, $phone, $email) {
  279. $result = null;
  280. $latitude = (float) $latitude;
  281. $longitude = (float) $longitude;
  282. $query = sprintf("INSERT INTO vendors (VendorName, Latitude, Longitude, Address, Phone, EMail) VALUES('%s', '%s', '%s', '%s', '%d', '%s')", $vendorName, $latitude, $longitude, $address, $phone, $email);
  283. mysql_query($query);
  284. if (mysql_affected_rows() <= 0) {
  285. return array('result' => array('error' => "Couldn't add Vendor"));
  286. } else {
  287. return array('result' => array('success' => true));
  288. }
  289. }
  290. function editVendor($vendorID, $vendorName, $latitude, $longitude, $address, $phone, $email) {
  291. $result = null;
  292. $vendorID = (int) $vendorID;
  293. $latitude = (float) $latitude;
  294. $longitude = (float) $longitude;
  295. $phone = (int) $phone;
  296. $query = sprintf("UPDATE vendors SET VendorName='%s', Latitude='%s', Longitude='%s', Address='%s', Phone='%d', EMail='%s' WHERE VendorID=%s", $vendorName, $latitude, $longitude, $address, $phone, $email, $vendorID);
  297. mysql_query($query);
  298. return getVendorByID($vendorID);
  299. //$itemID = mysql_insert_id();
  300. //return getItemByID($itemID);
  301. }
  302. function deleteVendor($vendorID) {
  303. $result = null;
  304. $vendorID = (int) $vendorID;
  305. $query = sprintf("DELETE FROM vendors WHERE VendorID=%d", $vendorID);
  306. $result = mysql_query($query);
  307. if (mysql_affected_rows() <= 0) {
  308. return array('result' => array('error' => "Unable to delete vendor"));
  309. } else {
  310. return array('result' => array('success' => $result));
  311. }
  312. }
  313. function claimVendor($vendorID) {
  314. $result = null;
  315. $vendorID = (int) $vendorID;
  316. if (isset($_SESSION['UserID']) && isset($_SESSION['isTechnician'])) {
  317. $query = sprintf("UPDATE vendors SET Owner='%d' WHERE VendorID=%s", $_SESSION['UserID'], $vendorID);
  318. mysql_query($query);
  319. if (mysql_affected_rows() <= 0) {
  320. return array('result' => array('error' => "Couldn't claim vendor"));
  321. } else {
  322. $email = "richard@localhost";
  323. $title = "User-> " . $_SESSION['UserID'] . "| Claiming Vendor-> " . $vendorID;
  324. $message = "Successful Claim";
  325. mail($email, $title, $message);
  326. return array('result' => array('success' => true));
  327. }
  328. } else {
  329. return array('result' => array('error' => "Access Control: Privileged login required"));
  330. }
  331. }
  332. function disownVendor($vendorID) {
  333. $result = null;
  334. $vendorID = (int) $vendorID;
  335. if (isset($_SESSION['UserID']) && isset($_SESSION['isTechnician'])) {
  336. $query = sprintf("UPDATE vendors SET Owner='' WHERE VendorID=%s", $vendorID);
  337. mysql_query($query);
  338. if (mysql_affected_rows() <= 0) {
  339. return array('result' => array('error' => "Couldn't disown vendor"));
  340. } else {
  341. return array('result' => array('success' => true));
  342. }
  343. } else {
  344. return array('result' => array('error' => "Access Control: Privileged login required"));
  345. }
  346. }
  347. ?>