Volkova

Image Upload Index

Jun 6th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.40 KB | None | 0 0
  1. <?php
  2. $user_name = "username";
  3. $password = "password";
  4. $database = "database";
  5. $server = "127.0.0.1";
  6.  
  7. $db_handle= mysql_connect($server, $user_name, $password);
  8. $db_found = mysql_select_db($database, $db_handle);
  9.  
  10. $amount_page = 9;
  11.  
  12. $pages = "<div id=\"pages\">";
  13.  
  14. $SQL = "SELECT COUNT(img_id) from tb_images";
  15. $result = mysql_query($SQL);
  16. $row = mysql_fetch_row($result);
  17. $total_records = $row[0];
  18. $total_pages = ceil($total_records / $amount_page);
  19.  
  20. if (isset($_GET["page"])) { $currentpage = $_GET["page"]; } else { $currentpage = 1; };
  21. $currentpage = mysql_real_escape_string(htmlentities($currentpage));
  22.  
  23. $pages .= "<span class=\"pages\">";
  24.  
  25. if ($total_pages > 0 && $currentpage > 1) {
  26.     $pages .= "<a href=\"?page=" . (intval($currentpage) - 1) . "\">Prev &laquo; </a>";
  27. }
  28. if ($total_pages > 1) {
  29.     for ( $i= 1; $i <= $total_pages; $i++) {
  30.         $pages .= "<a href=\"?page=$i\"> $i </a>  ";
  31.         if ($i != $total_pages) {
  32.             $pages .=  " - ";
  33.         }
  34.     }
  35. }
  36.  
  37. if ($total_pages > 0 && $currentpage != $total_pages && $total_pages > 1) {
  38.     $pages .= "<a href=\"?page=" . (intval($currentpage) + 1) . "\"> &raquo; Next </a>";
  39. }
  40.  
  41. $pages .= "</span>\n</div>";
  42.  
  43. if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; };
  44.  
  45. $loging = "
  46.         <div id=\"upload\">
  47.         <form enctype=\"multipart/form-data\" action=\"updity.php\" method=\"POST\" name=\"form\"  onsubmit='return validate ()'>
  48.        
  49.         Title: <br>
  50.             <input type=\"text\" name=\"title\" id=\"title\" class=\"input\" value=\"\"/> <br>
  51.        
  52.         <input type=\"file\" name=\"image\"> <br>
  53.         <input name=\"Submit\" type=\"submit\" value=\"Upload image\"> <br>
  54.         <span id=\"explain\" class=\"explain\">File must be .jpg, .gif or .png</span>
  55.         </form>
  56.         </div>
  57.        
  58.         <div id='imgwrap'>
  59. ";
  60.  
  61.  
  62. $start_from = ($page-1) * $amount_page;
  63.  
  64.     $SQL = "SELECT * FROM tb_images ORDER BY filename DESC LIMIT $start_from, $amount_page";
  65.    
  66.     $result = mysql_query($SQL);
  67.    
  68.     //print "<div id='wallcontainer'>";
  69.    
  70.    
  71.     while ($db_field = mysql_fetch_assoc($result)) {
  72.         $title = $db_field['title'];
  73.         $filename = $db_field['filename'];
  74.         $size = getimagesize("uploads/$filename");
  75.         $thumb = "uploads/thumbs/" . $filename;
  76.         $id = $db_field['img_id'];
  77.         $endurl = $_SERVER['REQUEST_URI'];
  78.         if (($pos = strpos($endurl, '?')) !== false) {
  79.             $endurl = substr($endurl, 0, $pos);
  80.         }
  81.         $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$endurl;
  82.         $loging .= "<div class='img'>\n";
  83.         $loging .= "<img src='uploads/thumbs/$filename' onClick=\"SelectAll('$id');\"><br>\n";
  84.         $loging .= "<input type='text' value='$url"."uploads/$filename' id='$id' class='inp' onClick=\"SelectAll('$id');\">\n";
  85.         $loging .= "<input type=\"button\" value=\"Delete\" class='inputdelete' onClick=\"parent.location='?action=delete&id=$id';\" >\n";
  86.         $loging .= "</div>\n";
  87.     }
  88.  
  89. $loging .= "</div>\n";
  90.  
  91.  
  92. function Logout($user_id) {
  93.     $SQL = "UPDATE tb_users SET online=0 WHERE user_id=$user_id";
  94.     $result = mysql_query($SQL);
  95.     setcookie('username','',time()-3600);
  96.     setcookie('password','',time()-3600);
  97.     $prev = $_SERVER['HTTP_REFERER'];
  98.     header("Location: $prev");
  99. }
  100.  
  101. function delete($img_id) {
  102.     $SQL = "SELECT * FROM tb_images WHERE img_id=$img_id";
  103.     $result = mysql_query($SQL);
  104.     while ($db_field = mysql_fetch_assoc($result)) {
  105.         $filename = $db_field['filename'];
  106.     }
  107.     $SQL = "DELETE FROM tb_images WHERE img_id=$img_id";
  108.     $result = mysql_query($SQL);
  109.     unlink("uploads/".$filename);
  110.     unlink("uploads/thumbs/".$filename);
  111.     $prev = $_SERVER['HTTP_REFERER'];
  112.     header("Location: $prev");
  113. }
  114.  
  115.  
  116. if (isset($_GET['action'])) {
  117.     if ($_GET['action'] == 'delete') {
  118.         $img_id = mysql_real_escape_string(htmlentities($_GET['id']));
  119.         delete($img_id);
  120.     }
  121. }
  122.  
  123. if (isset($_GET['action'])) {
  124.     if ($_GET['action'] == 'logout') {
  125.         $usersend = mysql_real_escape_string(htmlentities($_COOKIE['username']));
  126.         Logout($usersend);
  127.     }
  128. } elseif (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
  129.     $usersend = mysql_real_escape_string(htmlentities($_COOKIE['username']));
  130.     $passsend = mysql_real_escape_string(htmlentities($_COOKIE['password']));
  131.     $md5pass = $passsend;
  132.    
  133.     $SQL = "SELECT user_id FROM tb_users WHERE username='$usersend' and password='$md5pass'";
  134.     $result=mysql_query($SQL);
  135.     $count=mysql_num_rows($result);
  136.         while ($db_field = mysql_fetch_assoc($result)) {
  137.             $id = $db_field['user_id'];
  138.             $stamp = $db_field['timestamp'];
  139.         }
  140.     if($count==1) {
  141.         //correct
  142.         $time = time();
  143.         $SQL = "UPDATE tb_users SET timestamp=$time WHERE user_id=$id";
  144.         $result=mysql_query($SQL);
  145.         $SQL = "UPDATE tb_users SET online=1 WHERE user_id=$id";
  146.         $result=mysql_query($SQL);
  147.         setcookie('username', $usersend, time()+60*60*24*365);
  148.         setcookie('password', $md5pass, time()+60*60*24*365);
  149.         $content = $loging;
  150.     } else {
  151.         //incorrect log
  152.         $content = "Incorrect login";
  153.     }
  154.    
  155. } elseif (isset($_POST['username'])) {
  156.     $usersend = mysql_real_escape_string(htmlentities($_POST['username']));
  157.     $passsend = mysql_real_escape_string(htmlentities($_POST['password']));
  158.     $md5pass = md5($passsend);
  159.    
  160.     $SQL = "SELECT user_id FROM tb_users WHERE username='$usersend' and password='$md5pass'";
  161.     $result=mysql_query($SQL);
  162.     $count=mysql_num_rows($result);
  163.     while ($db_field = mysql_fetch_assoc($result)) {
  164.         $id = $db_field['user_id'];
  165.         $stamp = $db_field['timestamp'];
  166.     }
  167.     if($count==1) {
  168.         $time = time();
  169.         $SQL = "UPDATE tb_users SET timestamp=$time WHERE user_id=$id";
  170.         $result=mysql_query($SQL);
  171.         $SQL = "UPDATE tb_users SET online=1 WHERE user_id=$id";
  172.         $result=mysql_query($SQL);
  173.         setcookie('username', $usersend, time()+60*60*24*365);
  174.         setcookie('password', $md5pass, time()+60*60*24*365);
  175.         $content = $loging;
  176.     } else {
  177.         $content = "Incorrect login";
  178.     }
  179.    
  180. } elseif ($online == 1) {
  181.     $time = time();
  182.     $SQL = "UPDATE tb_users SET timestamp=$time WHERE user_id=$id";
  183.     mysql_query($SQL);
  184.  
  185. } else {
  186.     $content = "
  187.  
  188. <div id=\"Login\"><h1>Sign In</h1>
  189.  
  190. <form id=\"Form_User_SignIn\" method=\"post\" action=\"" . $_SERVER['REQUEST_URI'] . "\">
  191.  
  192. <li>
  193. <label for=\"Form_Username\">Username</label>
  194. <input id=\"Form_Email\" name=\"username\" value=\"\" class=\"InputBox\" type=\"text\">      </li>
  195.  
  196. <li>
  197. <label for=\"Form_Password\">Password</label>
  198. <input id=\"Form_Password\" name=\"password\" value=\"\" class=\"InputBox Password\" type=\"password\"><a href=\"/\" class=\"ForgotPassword\">?</a></li>
  199. <li class=\"Buttons\">
  200. <input id=\"Form_SignIn\" name=\"signin\" value=\"Sign In\" class=\"Button\" type=\"submit\">
  201. </li>
  202.  
  203. </ul>
  204. </div>
  205. </form></div></div>
  206.  
  207. ";
  208. }
  209. ?>
  210. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  211. "http://www.w3.org/TR/html4/strict.dtd\">
  212. <html>
  213. <head>
  214. <title>Image Upload</title>
  215. <link rel='stylesheet' type='text/css' href='style.css'>
  216. <script>
  217. function SelectAll(id)
  218. {
  219.     document.getElementById(id).focus();
  220.     document.getElementById(id).select();
  221. }
  222.  
  223.  
  224. </script>
  225. </head>
  226. <body>
  227.  
  228. <div id="wrapper">
  229. <div id="wrap">
  230. <div class="left">
  231.  
  232.         <ul class="ulmenu">
  233.             <li><a href="/" title="Home">Home</a></li>
  234.             <li><a href="#" title="News">General News</a></li>
  235.             <li><a href="/imgboard" title="Imageboard">Imageboard</a></li>
  236.         </ul>
  237.  
  238. </div>
  239. <div class="corner">
  240. </div>
  241. </div>
  242. <div id="content">
  243.  
  244. <div id="stuff">
  245. <? echo $content; ?>
  246. </div>
  247.  
  248.  
  249. <div class="clear"></div>
  250.  
  251. </div>
  252. <div id="footer">
  253. <a href='?action=logout'> Logout </a>
  254.  
  255. <? echo $pages; ?>
  256. </div>
  257.  
  258. </div>
  259.  
  260. </body>
  261. </html>
Advertisement
Add Comment
Please, Sign In to add comment