<?php
function db_get_PDO()
{
return new PDO(SQL_SERVER_DATABASE_PATH,
SQL_DATABASE_CLIENT_NAME,
SQL_DATABASE_PASSWORD,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
function dbConnect()
{
// próba połączenia z bazą
try
{
$db = db_get_PDO();
}
catch(PDOException $e)
{
echo 'Nieudane połączenie z bazą danych: <br />';
die ($e->getMessage());
return false;
}
return $db;
}
function dbLogin($username, $password)
{
$user = dbGetUserByName($username);
if($user != null)
{
if($username == $user['username'] && md5($password) == $user['password']) {
return true;
}
}
header('Location: ./?c=login&login_failure=1');
exit;
}
function dbGetUserByName($username)
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('SELECT * FROM users WHERE
users.username = :username');
$result->bindValue(':username',$username,PDO::PARAM_STR);
$result->execute();
$user = $result->fetch();
return $user;
}
catch (PDOException $pe)
{
echo $pe->getMessage();
return false;
}
return false;
}
function dbGetMoviesList()
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('SELECT * FROM films');
$result->execute();
foreach($result as $film)
{
$films[] = $film;
}
return $films;
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbGetFilmById ($id)
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('SELECT * FROM films WHERE films.id = :id');
$result->bindValue(':id', $id, PDO::PARAM_INT);
$result->execute();
$film = $result->fetch();
$imagePathArray = explode ('/', $film['imagePath']);
$imagePathArray[ count($imagePathArray) ] = $imagePathArray[ count($imagePathArray) - 1 ];
$imagePathArray[ count($imagePathArray) - 2 ] = 'tn';
$film['tinyImagePath'] = implode('/', $imagePathArray);
return $film;
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbBorrowItems ($basketItems, $username)
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
$db->beginTransaction();
$result = $db->prepare('UPDATE films SET isBorrowed = 1, borrowedBy = :username WHERE id = :id');
foreach($basketItems as $item)
{
$result->bindValue(':id',$item['id'],PDO::PARAM_INT);
$result->bindValue(':username',$username,PDO::PARAM_STR);
$result->execute();
}
$db->commit();
return 1;
}
catch (PDOException $t)
{
$t->getMessage();
$db->rollBack();
return -1; // transakcja nieudana
}
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbSetFilmAvailable ($item)
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('UPDATE films SET isBorrowed = 0, borrowedBy = :username WHERE id = :id');
$result->bindValue(':id',$item,PDO::PARAM_INT);
$result->bindValue(':username','',PDO::PARAM_STR);
$result->execute();
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbAddFilm()
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$imagePath = FILE_UPLOAD_PATH.$_FILES['image']['name'];
$result = $db->prepare('INSERT INTO films (titleOriginal, titlePolish, director, scriptwriter, year, description, cast, imagePath) VALUES
(
:titleO,
:titlePL,
:director,
:scriptwriter,
:year,
:description,
:cast,
:imagePath
)');
$result->bindValue(':titleO',$_POST['titleOriginal'],PDO::PARAM_STR);
$result->bindValue(':titlePL',$_POST['titlePolish'],PDO::PARAM_STR);
$result->bindValue(':director',$_POST['director'],PDO::PARAM_STR);
$result->bindValue(':scriptwriter',$_POST['scriptwriter'],PDO::PARAM_STR);
$result->bindValue(':year',$_POST['year'],PDO::PARAM_STR);
$result->bindValue(':description',$_POST['description'],PDO::PARAM_STR);
$result->bindValue(':cast',$_POST['cast'],PDO::PARAM_STR);
$result->bindValue(':imagePath',$imagePath,PDO::PARAM_STR);
$result->execute();
uploadFile();
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbEditFilm()
{
try
{
$imagePath = FILE_UPLOAD_PATH.$_FILES['image']['name'];
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('UPDATE films SET titleOriginal = :titleO, titlePolish = :titlePL, director = :director, scriptwriter = :scriptwriter, year = :year, description = :description,
cast = :cast, imagePath = :imagePath WHERE id = :id');
$result->bindValue(':id',$_POST['id'],PDO::PARAM_INT);
$result->bindValue(':titleO',$_POST['titleOriginal'],PDO::PARAM_STR);
$result->bindValue(':titlePL',$_POST['titlePolish'],PDO::PARAM_STR);
$result->bindValue(':director',$_POST['director'],PDO::PARAM_STR);
$result->bindValue(':scriptwriter',$_POST['scriptwriter'],PDO::PARAM_STR);
$result->bindValue(':year',$_POST['year'],PDO::PARAM_STR);
$result->bindValue(':description',$_POST['description'],PDO::PARAM_STR);
$result->bindValue(':cast',$_POST['cast'],PDO::PARAM_STR);
$result->bindValue(':imagePath',$imagePath,PDO::PARAM_STR);
$result->execute();
uploadFile();
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function dbDeleteFilm($item)
{
try
{
$db = dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $db->prepare('DELETE FROM films WHERE id = :id');
$result->bindValue(':id',$item,PDO::PARAM_INT);
$result->execute();
}
catch (PDOException $pe)
{
$pe->getMessage();
}
}
function uploadFile() {
$filetypes = Array('image/jpeg'=>1,'image/png'=>1,'image/gif'=>1);
if(isset($_FILES['image']))
{
$file_size=$_FILES['image']['size'];//pojemnosc pliku
$file_type=$_FILES['image']['type']; // typ pliku
$file_name=$_FILES['image']['name']; // nazwa pliku
$file_temporary_name=$_FILES['image']['tmp_name']; // chwilowa nazwa pliku
$file_extension= array_pop(explode(".", $file_name));
$max_size = 1024 * 1024;
$file_new_name = $file_name;
$folder=FILE_UPLOAD_PATH;
if ($file_size <= 0)
{
echo ("File is empty!");
}
elseif(!isset($filetypes[$file_type]))
{
echo "Wrong type of file!";
}
elseif ($file_size > $max_size)
{
echo "File is too big!";
}
elseif(file_exists($folder.$file_new_name))
{
echo "File already exists!";
}
else
{
if(!move_uploaded_file($file_temporary_name, $folder.$file_new_name))
{
echo "File transfer error!";
}
}
}
}
?>