"{$this->titlebook}", "isbn" => "{$this->isbnbook}", "weight" => "{$this->weightbook}", "description" => "{$this->introbook}" ); $lijst = $this->decode(); $i = count($lijst); if($i >=1) { usort($lijst, $this->sortISBN('isbn')); array_push($lijst,$newbook); } else { $lijst = array($newbook); } $this->storeBook($lijst); } public function editBook($id) { // todo! } public function checkForm() { isset($_POST['title']) ? $this->titlebook = $this->cleanInput($_POST['title']) : $titlebook = false; isset($_POST['isbn']) ? $this->isbnbook = $this->cleanInput($_POST['isbn']) : $isbnbook = false; isset($_POST['weight']) ? $this->weightbook = $this->cleanInput($_POST['weight']) : $weightbook = false; isset($_POST['description']) ? $this->introbook = $this->cleanInput($_POST['description']) : $introbook = false; $_SESSION['messages'] = array(); if($this->titlebook != false) { if(strlen($this->titlebook) > 60 ) { $this->message('Title may not be longer than 60 characters.'); return false; } } else { $this->message('Title may not be empty.'); return false; } if($this->isbnbook != false) { if(!preg_match("/[a-zA-Z]/i",$this->isbnbook)) { if(strlen($this->isbnbook) > 13 || strlen($this->isbnbook) < 13) { $this->message('ISBN number is wrong. (13 digits.)'); return false; } } else { $this->message('ISBN number may only contain numbers!'); return false; } } if($this->weightbook != false) { if(!is_int((int)$this->weightbook) || preg_match("/[a-zA-Z]/i",$this->weightbook)) { $this->message('Weight may not contain characters.'); return false; } } else { $this->message('Weight must not be empty.'); return false; } if($this->introbook != false) { if(strlen($this->introbook) > 60 ) { $this->message('Description may not be longer than 60 characters.'); return false; } } else { $this->message('Description magy not be empty.'); return false; } } public function tooHeavy($weight) { return ($weight > self::MAXWEIGHT) ? true:false; } public function message($value) { if(isset($_SESSION['messages'])) { array_push($_SESSION['messages'],$value); } else { $_SESSION['messages'] = array(); } } public function showmessage() { if(isset($_SESSION['messages'])) { echo "
"; 
			echo "Message:\r\n"; 
			foreach($_SESSION['messages'] as $message) { 
				echo $message . "\r\n" ; 
			} echo "
"; } $_SESSION['messages'] = array(); } /** * Store book into BookLibrary * @param array $book * @return boolean, true for success, false for failure. */ public function storeBook($book) { // make a backup before doing anything. $file = self::BOOKCASE; $copy = self::BOOKCASE.'.bak'; @copy($file, $copy); // convert encoding $json = mb_convert_encoding($this->encode($book), self::FILE_ENC, self::FILE_OS); // write file. file_put_contents(self::BOOKCASE,$json, LOCK_EX); } public function deleteBook($book) { $lijst = $this->decode(); if($lijst !== null) { $libraylist = usort($lijst, $this->sortISBN('isbn')); $books = array(); foreach($lijst as $c) { echo $book."
"; if($c['isbn'] != $book) { array_push($books,$c); } } } $this->storeBook($books); } public function sortISBN($key) { return function ($a, $b) use ($key) { return strnatcmp($a[$key], $b[$key]); }; } // We don't use this, but you could call it to encrypt the JSON data. public function encrypt($plaintext) { $key = self::PWD; // Password is set above at the Constants $ivlen = openssl_cipher_iv_length($cipher="AES-256-CTR"); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); $hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); $ciphertext = base64_encode($iv.$hmac.$ciphertext_raw ); return bin2hex($ciphertext); } // We don't use this, but you could call it to decrypt the JSON data. public function decrypt($ciphertext) { $key = self::PWD; // Password is set above at the Constants $ciphertext = hex2bin($ciphertext); $c = base64_decode($ciphertext); $ivlen = openssl_cipher_iv_length($cipher="AES-256-CTR"); $iv = substr($c, 0, $ivlen); $hmac = substr($c, $ivlen, $sha2len=32); $ciphertext_raw = substr($c, $ivlen+$sha2len); $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); if (hash_equals($hmac, $calcmac)) { //PHP 5.6+ timing attack safe comparison return $original_plaintext; } } } ?>