Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set("display_errors",0);
- ini_set("log_errors",1);
- ini_set("error_log",dirname(__FILE__) . '/error_log.txt');
- error_reporting(E_ALL);
- header('Cache-Control: no-cache, no-store, must-revalidate');
- header('Pragma: no-cache');
- // ADJUST THE PATHS TO SUIT YOUR OWN DIRECTORY STRUCTURE
- define('USERS', "data/_users.php");
- define('BOOKS', "data/_books.php");
- define('IMAGES', 'data/images/'); // PATH TO BOOK IMAGES FOLDER
- define('DESCRIPTIONS', 'data/descriptions/'); // PATH TO DESCRIPTIONS FOLDER
- define('EXCHANGE_RATE', 1.05);
- define('JS', 'extra.js');
- /**
- *
- */
- function updateJavascript() {
- //Open _books.php file to read
- $fh = fopen(BOOKS, "r");
- //Read the file and store in the $bookInfo variable
- $bookInfo = fread($fh, filesize(BOOKS));
- fclose($fh);
- //Explode the $bookInfo to put in the $lines array
- $lines = explode("\n", $bookInfo);
- //Take out the last line
- array_pop($lines);
- //To write
- array_shift($lines);
- $content = "function createLibrary(){\n";
- foreach ($lines as $line) {
- //Separate columns
- $bookColumns = explode(':', $line);
- //Store in the variables
- $isbn = $bookColumns[0];
- $title = $bookColumns[1];
- $desc = $bookColumns[2];
- $pages = $bookColumns[3];
- $price = $bookColumns[4];
- $image = $bookColumns[5];
- $content .= " books[\"$isbn\"] = new Book (\"$title\" ,\"$desc\", $pages,$price,\"$image\");\n";
- }
- $content .= "}\n";
- //Open the JS file to write the $content
- $fh = fopen(JS, "w");
- fwrite($fh, $content);
- flush();
- fclose($fh);
- }
- /*
- * Description: Deletes a book from the database. Removes the description file
- * and the book image if its not default.png
- * Uses BOOKS, IMAGES and DESCRIPTIONS constants
- * Receives:isbn
- * Returns: nothing
- */
- function deleteBook($isbn) {
- $fh = fopen(BOOKS, 'r');
- $content = fread($fh, filesize(BOOKS));
- fclose($fh);
- $lines = explode("\n", $content);
- array_pop($lines);
- array_shift($lines);
- $new_array = array("<?php");
- foreach ($lines as $line) {
- $bookColumn = explode(":", $line);
- if ($bookColumn[0] == $isbn) {
- unlink(DESCRIPTIONS . $bookColumn[2]);
- if ($bookColumn[5] != 'default.png') {
- unlink(IMAGES . $bookColumn[5]);
- }
- } else {
- array_push($new_array, $line);
- }
- }
- array_push($new_array, "?>");
- $fh = fopen(BOOKS, 'w');
- fwrite($fh, implode("\n", $new_array));
- fclose($fh);
- }
- function getAccessLevel($u, $p) {
- $fh = fopen(USERS, "r");
- $content = fread($fh, filesize(USERS));
- fclose($fh);
- $lines = explode("\n", $content);
- array_pop($lines);
- array_shift($lines);
- foreach ($lines as $line) {
- $userInfo = explode(':', $line);
- $user = $userInfo[0];
- $password = $userInfo[1];
- $level = $userInfo[2];
- if ($u == $user) {
- if ($password == $p) {
- return $level;
- }
- }
- }
- return 0;
- }
- function createFlatDatabase() {
- $fh = fopen(USERS, 'w');
- fwrite($fh, "<?php\nadmin:admin:2\n?>");
- fclose($fh);
- $fh = fopen(BOOKS, 'w');
- fwrite($fh, "<?php\n?>");
- fclose($fh);
- }
- function getBooks() {
- $content = "";
- $isbn = "";
- $title = "";
- $descriptionFilename = "";
- $pages = 0;
- $price = 0;
- $imagePath = "";
- $desc = "";
- $fh = fopen(BOOKS, "r");
- $bookInfo = fread($fh, filesize(BOOKS));
- fclose($fh);
- $lines = explode("\n", $bookInfo);
- array_pop($lines);
- array_shift($lines);
- foreach ($lines as $line) {
- $bookColumns = explode(':', $line);
- $isbn = $bookColumns[0];
- $title = $bookColumns[1];
- $descriptionFilename = $bookColumns[2];
- $pages = $bookColumns[3];
- $price = $bookColumns[4];
- $imagePath = IMAGES . $bookColumns[5];
- $fh = fopen(DESCRIPTIONS . $descriptionFilename, "r");
- $description = fread($fh, filesize(DESCRIPTIONS . $descriptionFilename));
- fclose($fh);
- $content .= '<div class="book"> <img src="';
- $content .= $imagePath;
- $content .= '" width="150" alt="$title" />';
- $content .= '<div class="text_left"><h4><span class="title">';
- $content .= $title;
- $content .= '</span></h4><p>';
- $content .= $description;
- $content .= '</p><div class="detail"> <span class="pages">';
- $content .= $pages;
- $content .= ' pages</span> <span class="price">US $';
- $content .= number_format($price * EXCHANGE_RATE,2,".",",");
- $content .= ', CDN $';
- $content .= number_format($price,2,".",",");
- $content .= '</span> <a href=" " id="';
- $content .= $isbn;
- $content .= '" class="addToCart">Add to Shopping Cart</a> </div></div></div>';
- }
- return $content;
- }
- function getBooksToDelete() {
- $content = "";
- $isbn = "";
- $title = "";
- $descriptionFilename = "";
- $pages = 0;
- $price = 0;
- $imagePath = "";
- $desc = "";
- $fh = fopen(BOOKS, "r");
- $bookInfo = fread($fh, filesize(BOOKS));
- fclose($fh);
- $lines = explode("\n", $bookInfo);
- array_pop($lines);
- array_shift($lines);
- foreach ($lines as $line) {
- $bookColumns = explode(':', $line);
- $isbn = $bookColumns[0];
- $title = $bookColumns[1];
- $descriptionFilename = $bookColumns[2];
- $pages = $bookColumns[3];
- $price = $bookColumns[4];
- $imagePath = IMAGES . $bookColumns[5];
- $fh = fopen(DESCRIPTIONS . $descriptionFilename, "r");
- $description = fread($fh, filesize(DESCRIPTIONS . $descriptionFilename));
- fclose($fh);
- $content .= '<form action="#" method="post" >';
- $content .= '<div class="book"> <img src="';
- $content .= $imagePath;
- $content .= '" width="150" alt="$title" />';
- $content .= '<div class="text_left"><h4><span class="title">';
- $content .= $title;
- $content .= '</span></h4><p>';
- $content .= $description;
- $content .= '</p><div class="detail"> <span class="pages">';
- $content .= $pages;
- $content .= ' pages</span> <span class="price">US $';
- $content .= $price * EXCHANGE_RATE;
- $content .= ', CDN $';
- $content .= $price;
- $content .= '</span> ';
- $content .= '<input name="delete" type="submit" value="Delete" />';
- $content .= ' </div></div></div>';
- $content .= '<input type="hidden" name="isbn" value="';
- $content .= $isbn;
- $content .= '" />';
- $content .= '</form>';
- }
- return $content;
- }
- /*
- * function tests if the book the user is entering already exists.
- * It it does, it returns true.
- * Receives: the book's isbn
- * Returns:true, if the book already exists.
- */
- function bookExists($isbn) {
- $fh = fopen(BOOKS, "r");
- $content = fread($fh, filesize(BOOKS));
- fclose($fh);
- $lines = explode("\n", $content);
- array_pop($lines);
- array_shift($lines);
- foreach ($lines as $line) {
- $bookInfo = explode(':', $line);
- $i = $bookInfo[0];
- if ($isbn == $i) {
- return true;
- }
- }
- return false;
- }
- /**
- * Description: Add a book to the database.
- * If no image is provided then a default image (default.png)is used.
- * If one is provided then it checks it's size and decides if
- * it is a valid image or not.
- * Will create a description file using the isbn number
- * Receives: isbn,title,description, number of pages, canadian price, FILE image array
- * Returns: 1 if the image exceeds 200k ,
- * 0 if the save was successful
- */
- function addBook($isbn, $title, $desc, $pages, $price, $imgFiles) {
- $imageName = "";
- if ($imgFiles['size'] > 300000) {
- return 1;
- }
- // SAVE IMAGE FILE
- if ($imgFiles['size'] == 0) {
- $imageName = "default.png";
- } else {
- $imageName = "p" . time() . $imgFiles['name'];
- $tempName = $imgFiles['tmp_name'];
- move_uploaded_file($tempName, IMAGES . $imageName);
- }
- // SAVE DESCRIPTION FILE
- $descriptionFilename = $isbn . ".txt";
- $fh = fopen(DESCRIPTIONS . $descriptionFilename, 'w');
- fwrite($fh, $desc);
- fclose($fh);
- // SAVE BOOK FILE
- $fh = fopen(BOOKS, "r");
- $content = fread($fh, filesize(BOOKS));
- fclose($fh);
- $lines = explode("\n", $content);
- array_pop($lines);
- array_push($lines, "$isbn:$title:$descriptionFilename:$pages:$price:$imageName");
- array_push($lines, "?>");
- $fh = fopen(BOOKS, 'w');
- fwrite($fh, implode("\n", $lines));
- fclose($fh);
- return 0;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement