Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: PHP | Size: 7.28 KB | Hits: 67 | Expires: Never
Copy text to clipboard
  1.  
  2. <?php
  3.        
  4.         function db_get_PDO()
  5.         {
  6.                 return new PDO(SQL_SERVER_DATABASE_PATH,
  7.                                                 SQL_DATABASE_CLIENT_NAME,
  8.                                                 SQL_DATABASE_PASSWORD,
  9.                                                 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  10.         }
  11.        
  12.         function dbConnect()
  13.         {
  14.                 // próba połączenia z bazą
  15.                 try
  16.                 {
  17.                         $db = db_get_PDO();
  18.                 }
  19.                 catch(PDOException $e)
  20.                 {
  21.                         echo 'Nieudane połączenie z bazą danych: <br />';
  22.                         die ($e->getMessage());
  23.                         return false;
  24.                 }
  25.                
  26.                 return $db;
  27.         }
  28.        
  29.        
  30.         function dbLogin($username, $password)
  31.         {
  32.                 $user = dbGetUserByName($username);
  33.                                
  34.                 if($user != null)
  35.                 {
  36.                         if($username == $user['username'] && md5($password) == $user['password']) {
  37.                                 return true;
  38.                         }
  39.                 }
  40.                
  41.                 header('Location: ./?c=login&login_failure=1');
  42.                 exit;
  43.         }
  44.        
  45.        
  46.         function dbGetUserByName($username)
  47.         {
  48.                 try
  49.                 {
  50.                         $db = dbConnect();
  51.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  52.                        
  53.                         $result = $db->prepare('SELECT * FROM users WHERE
  54.                                                                                 users.username = :username');
  55.                         $result->bindValue(':username',$username,PDO::PARAM_STR);
  56.                         $result->execute();
  57.                        
  58.                         $user = $result->fetch();
  59.                        
  60.                         return $user;
  61.                 }
  62.                 catch (PDOException $pe)
  63.                 {
  64.                         echo $pe->getMessage();
  65.                         return false;
  66.                 }
  67.                 return false;
  68.         }
  69.        
  70.        
  71.         function dbGetMoviesList()
  72.         {
  73.                 try
  74.                 {
  75.                         $db = dbConnect();
  76.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  77.                        
  78.                         $result = $db->prepare('SELECT * FROM films');
  79.                         $result->execute();
  80.                         foreach($result as $film)
  81.                         {
  82.                                
  83.                                 $films[] = $film;
  84.                         }
  85.                        
  86.                         return $films;
  87.                 }
  88.                 catch (PDOException $pe)
  89.                 {
  90.                         $pe->getMessage();
  91.                 }
  92.         }
  93.        
  94.         function dbGetFilmById ($id)
  95.         {
  96.                 try
  97.                 {
  98.                         $db = dbConnect();
  99.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  100.                                                
  101.                         $result = $db->prepare('SELECT * FROM films WHERE films.id = :id');
  102.                         $result->bindValue(':id', $id, PDO::PARAM_INT);
  103.                         $result->execute();
  104.                        
  105.                         $film = $result->fetch();
  106.                        
  107.                         $imagePathArray = explode ('/', $film['imagePath']);
  108.                        
  109.                         $imagePathArray[ count($imagePathArray) ] = $imagePathArray[ count($imagePathArray) - 1 ];
  110.                         $imagePathArray[ count($imagePathArray) - 2 ] = 'tn';
  111.                        
  112.                         $film['tinyImagePath'] = implode('/', $imagePathArray);
  113.                        
  114.                         return $film;
  115.                 }
  116.                 catch (PDOException $pe)
  117.                 {
  118.                         $pe->getMessage();
  119.                 }
  120.         }
  121.        
  122.         function dbBorrowItems ($basketItems, $username)
  123.         {
  124.                 try
  125.                 {
  126.                         $db = dbConnect();
  127.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  128.                                                
  129.                         try
  130.                         {      
  131.                                 $db->beginTransaction();
  132.                                 $result = $db->prepare('UPDATE films SET isBorrowed = 1, borrowedBy = :username WHERE id = :id');
  133.                                        
  134.                                 foreach($basketItems as $item)
  135.                                 {
  136.                                         $result->bindValue(':id',$item['id'],PDO::PARAM_INT);
  137.                                         $result->bindValue(':username',$username,PDO::PARAM_STR);
  138.                                         $result->execute();    
  139.                                 }
  140.                                
  141.                                 $db->commit();
  142.                                 return 1;
  143.                         }
  144.                        
  145.                         catch (PDOException $t)
  146.                         {
  147.                                 $t->getMessage();
  148.                                 $db->rollBack();
  149.                                 return -1; // transakcja nieudana
  150.                         }
  151.                 }
  152.                 catch (PDOException $pe)
  153.                 {
  154.                         $pe->getMessage();
  155.                 }
  156.         }
  157.        
  158.         function dbSetFilmAvailable ($item)
  159.         {
  160.                 try
  161.                 {
  162.                         $db = dbConnect();
  163.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  164.                                                
  165.                         $result = $db->prepare('UPDATE films SET isBorrowed = 0, borrowedBy = :username WHERE id = :id');
  166.                         $result->bindValue(':id',$item,PDO::PARAM_INT);
  167.                         $result->bindValue(':username','',PDO::PARAM_STR);
  168.                         $result->execute();    
  169.  
  170.                 }
  171.                 catch (PDOException $pe)
  172.                 {
  173.                         $pe->getMessage();
  174.                 }
  175.         }
  176.        
  177.         function dbAddFilm()
  178.         {
  179.                 try
  180.                 {
  181.                         $db = dbConnect();
  182.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  183.                
  184.                         $imagePath = FILE_UPLOAD_PATH.$_FILES['image']['name'];
  185.                         $result = $db->prepare('INSERT INTO films (titleOriginal, titlePolish, director, scriptwriter, year, description, cast, imagePath) VALUES
  186.                                 (
  187.                                         :titleO,
  188.                                         :titlePL,
  189.                                         :director,
  190.                                         :scriptwriter,
  191.                                         :year,
  192.                                         :description,
  193.                                         :cast,
  194.                                         :imagePath
  195.                                 )');
  196.                         $result->bindValue(':titleO',$_POST['titleOriginal'],PDO::PARAM_STR);
  197.                         $result->bindValue(':titlePL',$_POST['titlePolish'],PDO::PARAM_STR);
  198.                         $result->bindValue(':director',$_POST['director'],PDO::PARAM_STR);
  199.                         $result->bindValue(':scriptwriter',$_POST['scriptwriter'],PDO::PARAM_STR);
  200.                         $result->bindValue(':year',$_POST['year'],PDO::PARAM_STR);
  201.                         $result->bindValue(':description',$_POST['description'],PDO::PARAM_STR);
  202.                         $result->bindValue(':cast',$_POST['cast'],PDO::PARAM_STR);
  203.                         $result->bindValue(':imagePath',$imagePath,PDO::PARAM_STR);
  204.                         $result->execute();    
  205.                        
  206.                         uploadFile();
  207.  
  208.                 }
  209.                 catch (PDOException $pe)
  210.                 {
  211.                         $pe->getMessage();
  212.                 }
  213.         }
  214.        
  215.         function dbEditFilm()
  216.         {
  217.                 try
  218.                 {
  219.                         $imagePath = FILE_UPLOAD_PATH.$_FILES['image']['name'];
  220.  
  221.                         $db = dbConnect();
  222.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  223.                        
  224.                         $result = $db->prepare('UPDATE films SET titleOriginal = :titleO, titlePolish = :titlePL, director = :director, scriptwriter = :scriptwriter, year = :year, description = :description,
  225.                                                                         cast = :cast, imagePath = :imagePath WHERE id = :id');
  226.                         $result->bindValue(':id',$_POST['id'],PDO::PARAM_INT);
  227.                         $result->bindValue(':titleO',$_POST['titleOriginal'],PDO::PARAM_STR);
  228.                         $result->bindValue(':titlePL',$_POST['titlePolish'],PDO::PARAM_STR);
  229.                         $result->bindValue(':director',$_POST['director'],PDO::PARAM_STR);
  230.                         $result->bindValue(':scriptwriter',$_POST['scriptwriter'],PDO::PARAM_STR);
  231.                         $result->bindValue(':year',$_POST['year'],PDO::PARAM_STR);
  232.                         $result->bindValue(':description',$_POST['description'],PDO::PARAM_STR);
  233.                         $result->bindValue(':cast',$_POST['cast'],PDO::PARAM_STR);
  234.                         $result->bindValue(':imagePath',$imagePath,PDO::PARAM_STR);
  235.                         $result->execute();    
  236.                        
  237.                         uploadFile();
  238.                 }
  239.                 catch (PDOException $pe)
  240.                 {
  241.                         $pe->getMessage();
  242.                 }
  243.         }
  244.        
  245.         function dbDeleteFilm($item)
  246.         {
  247.                 try
  248.                 {
  249.                         $db = dbConnect();
  250.                         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  251.                                                
  252.                         $result = $db->prepare('DELETE FROM films WHERE id = :id');
  253.                         $result->bindValue(':id',$item,PDO::PARAM_INT);
  254.                         $result->execute();    
  255.  
  256.                 }
  257.                 catch (PDOException $pe)
  258.                 {
  259.                         $pe->getMessage();
  260.                 }
  261.         }
  262.        
  263.         function uploadFile() {
  264.                
  265.                
  266.                 $filetypes = Array('image/jpeg'=>1,'image/png'=>1,'image/gif'=>1);
  267.  
  268.                 if(isset($_FILES['image']))
  269.                 {
  270.  
  271.                         $file_size=$_FILES['image']['size'];//pojemnosc pliku
  272.                         $file_type=$_FILES['image']['type']; // typ pliku
  273.                        
  274.                        
  275.                         $file_name=$_FILES['image']['name']; // nazwa pliku
  276.                         $file_temporary_name=$_FILES['image']['tmp_name']; // chwilowa nazwa pliku
  277.        
  278.                         $file_extension= array_pop(explode(".", $file_name));
  279.                         $max_size = 1024 * 1024;
  280.        
  281.                         $file_new_name = $file_name;
  282.                         $folder=FILE_UPLOAD_PATH;
  283.                        
  284.                        
  285.                         if ($file_size <= 0)
  286.                         {
  287.                         echo ("File is empty!");
  288.                         }
  289.                         elseif(!isset($filetypes[$file_type]))
  290.                         {
  291.                                 echo "Wrong type of file!";
  292.                         }
  293.                         elseif ($file_size > $max_size)
  294.                         {
  295.                             echo "File is too big!";
  296.                         }
  297.                         elseif(file_exists($folder.$file_new_name))
  298.                         {
  299.                             echo "File already exists!";
  300.                         }
  301.                         else
  302.                         {
  303.                         if(!move_uploaded_file($file_temporary_name, $folder.$file_new_name))
  304.                         {
  305.                                 echo "File transfer error!";
  306.                         }
  307.                         }
  308.                        
  309.                 }
  310.         }
  311.        
  312. ?>