Guest User

Untitled

a guest
Sep 25th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2. // dont use passwd through GET, and a file can only be send through POST method :)
  3. // enctype="multipart/form-data <-= use this in the html form
  4.  
  5. function getRealIpAddr() {
  6.     if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
  7.       $ip=$_SERVER['HTTP_CLIENT_IP'];
  8.     } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
  9.       $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  10.     } else {
  11.       $ip=$_SERVER['REMOTE_ADDR'];
  12.     }
  13.     return $ip;
  14. }
  15.  
  16. function connect_db($user, $pass) {
  17.     $link = mysql_connect('localhost', $user, $pass);
  18.     if (!$link) {
  19.         die('error: '. mysql_error());
  20.     }
  21.     mysql_select_db("db", $link);
  22.     return $link
  23. }
  24.  
  25. function check_login($link, $username, $password) {
  26.     $query = sprintf("SELECT * FROM user WHERE username='%s'",
  27.                         mysql_real_escape_string($username));
  28.     $result = mysql_query($query, $link);
  29.     if ($result) {
  30.         // check user
  31.         $row = mysql_fetch_array($result, MYSQL_ASSOC);
  32.         if ($row['password'] == $password) {
  33.             $query = sprintf("UPDATE 'user' SET lastIP="%d" WHERE username='%s'", getRealIpAddr(),
  34.                                 mysql_real_escape_string($username));
  35.             mysql_query($query, $link);
  36.         } else {
  37.             ?> error, incorrect password <?php
  38.         }
  39.     } else {
  40.         // add new user
  41.         $query = sprintf("INSERT INTO table 'user' VALUES \(%s, %s, 0\)",
  42.                             mysql_real_escape_string($username),
  43.                             mysql_real_escape_string($password));
  44.         mysql_query($query, $link);
  45.     }
  46.    
  47. }
  48.  
  49. function update_count_db_upload($link, $username) {
  50.     // retrieve user upload count
  51.     $query = sprintf("SELECT 'count' FROM user WHERE username='%s'",
  52.                         mysql_real_escape_string($username));
  53.     $result = mysql_query($query, $link);
  54.  
  55.     if ($result) {
  56.         $row = mysql_fetch_array($result, MYSQL_ASSOC);
  57.         $row['count']++;
  58.        
  59.         // update user upload count
  60.         $query = sprintf("UPDATE 'user' SET count="%d" WHERE username='%s'", $row['count']
  61.                             mysql_real_escape_string($username));
  62.         mysql_query($query, $link);
  63.     } else {
  64.         echo "bloody hell, you shouldn't be here, you are here bc it let you go in but didn't find you";
  65.     }
  66. }
  67.  
  68. function save_file($file, $username) {
  69.     $fname = "/uploads/schematics/".addslashes($username)."_filename.schematic";
  70.     move_uploaded_file($file['tmp_name'], $fname);
  71.     echo "done uploading");
  72. }
  73.  
  74. function list_schematics() {
  75.     if ($handle = opendir('/uploads/schematics/')) {
  76.         echo "Directory handle: $handle\n <br />";
  77.         echo "Entries:\n <br />";
  78.  
  79.         /* This is the correct way to loop over the directory. */
  80.         while (false !== ($entry = readdir($handle))) {
  81.             echo "$entry\n <br />";
  82.         }
  83.  
  84.         closedir($handle);
  85.     }
  86. }
  87. // -------------------------------------------------------------
  88. $link = connect_db($db_username,$db_password);
  89.  
  90. $username = $_GET['username'];
  91. $password = $_GET['password'];
  92. $file = $_FILES['file'];
  93.  
  94. if !empty($username) {
  95.     check_login($link, $username, $password);
  96.     if !empty($file) {
  97.         save_file($file, $username);
  98.         update_count_db_upload($link, $username);
  99.     } else {
  100.         ?> error in .schematic file <?php
  101.     }
  102. } else {
  103.     list_schematics();
  104. }
  105. ?>
Add Comment
Please, Sign In to add comment