Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. <?php
  2. ini_set("display_errors",0);
  3. ini_set("log_errors",1);
  4. ini_set("error_log",dirname(__FILE__) . '/error_log.txt');
  5. error_reporting(E_ALL);
  6.  
  7. header('Cache-Control: no-cache, no-store, must-revalidate');
  8. header('Pragma: no-cache');
  9.  
  10. // ADJUST THE PATHS TO SUIT YOUR OWN DIRECTORY STRUCTURE
  11. define('USERS', "data/_users.php");
  12. define('BOOKS', "data/_books.php");
  13. define('IMAGES', 'data/images/'); // PATH TO BOOK IMAGES FOLDER
  14. define('DESCRIPTIONS', 'data/descriptions/'); // PATH TO DESCRIPTIONS FOLDER
  15. define('EXCHANGE_RATE', 1.05);
  16. define('JS', 'extra.js');
  17.  
  18. /**
  19. *
  20. */
  21. function updateJavascript() {
  22.  
  23.  
  24.  
  25. //Open _books.php file to read
  26. $fh = fopen(BOOKS, "r");
  27.  
  28. //Read the file and store in the $bookInfo variable
  29. $bookInfo = fread($fh, filesize(BOOKS));
  30. fclose($fh);
  31.  
  32. //Explode the $bookInfo to put in the $lines array
  33. $lines = explode("\n", $bookInfo);
  34.  
  35. //Take out the last line
  36. array_pop($lines);
  37.  
  38. //To write
  39. array_shift($lines);
  40.  
  41. $content = "function createLibrary(){\n";
  42.  
  43. foreach ($lines as $line) {
  44.  
  45. //Separate columns
  46. $bookColumns = explode(':', $line);
  47.  
  48. //Store in the variables
  49. $isbn = $bookColumns[0];
  50. $title = $bookColumns[1];
  51. $desc = $bookColumns[2];
  52. $pages = $bookColumns[3];
  53. $price = $bookColumns[4];
  54. $image = $bookColumns[5];
  55.  
  56. $content .= " books[\"$isbn\"] = new Book (\"$title\" ,\"$desc\", $pages,$price,\"$image\");\n";
  57.  
  58. }
  59.  
  60. $content .= "}\n";
  61.  
  62. //Open the JS file to write the $content
  63. $fh = fopen(JS, "w");
  64. fwrite($fh, $content);
  65. flush();
  66. fclose($fh);
  67.  
  68.  
  69. }
  70.  
  71.  
  72. /*
  73. * Description: Deletes a book from the database. Removes the description file
  74. * and the book image if its not default.png
  75. * Uses BOOKS, IMAGES and DESCRIPTIONS constants
  76. * Receives:isbn
  77. * Returns: nothing
  78. */
  79.  
  80. function deleteBook($isbn) {
  81. $fh = fopen(BOOKS, 'r');
  82. $content = fread($fh, filesize(BOOKS));
  83. fclose($fh);
  84. $lines = explode("\n", $content);
  85. array_pop($lines);
  86. array_shift($lines);
  87. $new_array = array("<?php");
  88. foreach ($lines as $line) {
  89. $bookColumn = explode(":", $line);
  90. if ($bookColumn[0] == $isbn) {
  91. unlink(DESCRIPTIONS . $bookColumn[2]);
  92. if ($bookColumn[5] != 'default.png') {
  93. unlink(IMAGES . $bookColumn[5]);
  94. }
  95. } else {
  96. array_push($new_array, $line);
  97. }
  98. }
  99. array_push($new_array, "?>");
  100. $fh = fopen(BOOKS, 'w');
  101. fwrite($fh, implode("\n", $new_array));
  102. fclose($fh);
  103. }
  104.  
  105. function getAccessLevel($u, $p) {
  106. $fh = fopen(USERS, "r");
  107. $content = fread($fh, filesize(USERS));
  108. fclose($fh);
  109. $lines = explode("\n", $content);
  110. array_pop($lines);
  111. array_shift($lines);
  112. foreach ($lines as $line) {
  113. $userInfo = explode(':', $line);
  114. $user = $userInfo[0];
  115. $password = $userInfo[1];
  116. $level = $userInfo[2];
  117.  
  118. if ($u == $user) {
  119. if ($password == $p) {
  120. return $level;
  121. }
  122. }
  123. }
  124. return 0;
  125. }
  126.  
  127. function createFlatDatabase() {
  128. $fh = fopen(USERS, 'w');
  129. fwrite($fh, "<?php\nadmin:admin:2\n?>");
  130. fclose($fh);
  131.  
  132. $fh = fopen(BOOKS, 'w');
  133. fwrite($fh, "<?php\n?>");
  134. fclose($fh);
  135. }
  136.  
  137. function getBooks() {
  138.  
  139. $content = "";
  140. $isbn = "";
  141. $title = "";
  142. $descriptionFilename = "";
  143. $pages = 0;
  144. $price = 0;
  145. $imagePath = "";
  146. $desc = "";
  147.  
  148. $fh = fopen(BOOKS, "r");
  149. $bookInfo = fread($fh, filesize(BOOKS));
  150. fclose($fh);
  151.  
  152. $lines = explode("\n", $bookInfo);
  153.  
  154. array_pop($lines);
  155. array_shift($lines);
  156.  
  157. foreach ($lines as $line) {
  158.  
  159. $bookColumns = explode(':', $line);
  160.  
  161. $isbn = $bookColumns[0];
  162. $title = $bookColumns[1];
  163. $descriptionFilename = $bookColumns[2];
  164. $pages = $bookColumns[3];
  165. $price = $bookColumns[4];
  166. $imagePath = IMAGES . $bookColumns[5];
  167.  
  168.  
  169. $fh = fopen(DESCRIPTIONS . $descriptionFilename, "r");
  170. $description = fread($fh, filesize(DESCRIPTIONS . $descriptionFilename));
  171. fclose($fh);
  172.  
  173. $content .= '<div class="book"> <img src="';
  174. $content .= $imagePath;
  175. $content .= '" width="150" alt="$title" />';
  176. $content .= '<div class="text_left"><h4><span class="title">';
  177. $content .= $title;
  178. $content .= '</span></h4><p>';
  179. $content .= $description;
  180. $content .= '</p><div class="detail"> <span class="pages">';
  181. $content .= $pages;
  182. $content .= ' pages</span> <span class="price">US $';
  183. $content .= number_format($price * EXCHANGE_RATE,2,".",",");
  184. $content .= ', CDN $';
  185. $content .= number_format($price,2,".",",");
  186. $content .= '</span> <a href=" " id="';
  187. $content .= $isbn;
  188. $content .= '" class="addToCart">Add to Shopping Cart</a> </div></div></div>';
  189. }
  190.  
  191. return $content;
  192. }
  193.  
  194. function getBooksToDelete() {
  195.  
  196. $content = "";
  197. $isbn = "";
  198. $title = "";
  199. $descriptionFilename = "";
  200. $pages = 0;
  201. $price = 0;
  202. $imagePath = "";
  203. $desc = "";
  204.  
  205. $fh = fopen(BOOKS, "r");
  206. $bookInfo = fread($fh, filesize(BOOKS));
  207. fclose($fh);
  208.  
  209. $lines = explode("\n", $bookInfo);
  210.  
  211. array_pop($lines);
  212. array_shift($lines);
  213.  
  214. foreach ($lines as $line) {
  215.  
  216. $bookColumns = explode(':', $line);
  217.  
  218. $isbn = $bookColumns[0];
  219. $title = $bookColumns[1];
  220. $descriptionFilename = $bookColumns[2];
  221. $pages = $bookColumns[3];
  222. $price = $bookColumns[4];
  223. $imagePath = IMAGES . $bookColumns[5];
  224.  
  225.  
  226. $fh = fopen(DESCRIPTIONS . $descriptionFilename, "r");
  227. $description = fread($fh, filesize(DESCRIPTIONS . $descriptionFilename));
  228. fclose($fh);
  229. $content .= '<form action="#" method="post" >';
  230. $content .= '<div class="book"> <img src="';
  231. $content .= $imagePath;
  232. $content .= '" width="150" alt="$title" />';
  233. $content .= '<div class="text_left"><h4><span class="title">';
  234. $content .= $title;
  235. $content .= '</span></h4><p>';
  236. $content .= $description;
  237. $content .= '</p><div class="detail"> <span class="pages">';
  238. $content .= $pages;
  239. $content .= ' pages</span> <span class="price">US $';
  240. $content .= $price * EXCHANGE_RATE;
  241. $content .= ', CDN $';
  242. $content .= $price;
  243. $content .= '</span> ';
  244. $content .= '<input name="delete" type="submit" value="Delete" />';
  245. $content .= ' </div></div></div>';
  246.  
  247. $content .= '<input type="hidden" name="isbn" value="';
  248. $content .= $isbn;
  249. $content .= '" />';
  250.  
  251.  
  252. $content .= '</form>';
  253. }
  254.  
  255. return $content;
  256. }
  257.  
  258. /*
  259. * function tests if the book the user is entering already exists.
  260. * It it does, it returns true.
  261. * Receives: the book's isbn
  262. * Returns:true, if the book already exists.
  263. */
  264.  
  265. function bookExists($isbn) {
  266. $fh = fopen(BOOKS, "r");
  267. $content = fread($fh, filesize(BOOKS));
  268. fclose($fh);
  269. $lines = explode("\n", $content);
  270. array_pop($lines);
  271. array_shift($lines);
  272. foreach ($lines as $line) {
  273. $bookInfo = explode(':', $line);
  274. $i = $bookInfo[0];
  275.  
  276. if ($isbn == $i) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282.  
  283. /**
  284. * Description: Add a book to the database.
  285. * If no image is provided then a default image (default.png)is used.
  286. * If one is provided then it checks it's size and decides if
  287. * it is a valid image or not.
  288. * Will create a description file using the isbn number
  289. * Receives: isbn,title,description, number of pages, canadian price, FILE image array
  290. * Returns: 1 if the image exceeds 200k ,
  291. * 0 if the save was successful
  292. */
  293. function addBook($isbn, $title, $desc, $pages, $price, $imgFiles) {
  294. $imageName = "";
  295.  
  296. if ($imgFiles['size'] > 300000) {
  297. return 1;
  298. }
  299.  
  300. // SAVE IMAGE FILE
  301. if ($imgFiles['size'] == 0) {
  302. $imageName = "default.png";
  303. } else {
  304.  
  305. $imageName = "p" . time() . $imgFiles['name'];
  306. $tempName = $imgFiles['tmp_name'];
  307. move_uploaded_file($tempName, IMAGES . $imageName);
  308. }
  309.  
  310.  
  311. // SAVE DESCRIPTION FILE
  312. $descriptionFilename = $isbn . ".txt";
  313. $fh = fopen(DESCRIPTIONS . $descriptionFilename, 'w');
  314. fwrite($fh, $desc);
  315. fclose($fh);
  316.  
  317. // SAVE BOOK FILE
  318. $fh = fopen(BOOKS, "r");
  319. $content = fread($fh, filesize(BOOKS));
  320. fclose($fh);
  321. $lines = explode("\n", $content);
  322. array_pop($lines);
  323. array_push($lines, "$isbn:$title:$descriptionFilename:$pages:$price:$imageName");
  324. array_push($lines, "?>");
  325.  
  326. $fh = fopen(BOOKS, 'w');
  327. fwrite($fh, implode("\n", $lines));
  328. fclose($fh);
  329. return 0;
  330. }
  331.  
  332. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement