Advertisement
Guest User

Untitled

a guest
May 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.07 KB | None | 0 0
  1. <?
  2. //ob_start();
  3. session_start();
  4. $auth_id=$_SESSION['userid'];
  5. if (!$auth_id || empty($auth_id) || $auth_id==""){
  6. $auth_id = 0;
  7. }
  8. require_once("config.php");
  9. require_once("limits.php");
  10. require_once("ftp.class.php");
  11. require_once("func.php");
  12.  
  13. $link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect to the database.");
  14.  
  15. mysql_select_db($db_name) or die("Could not select the database.");
  16.  
  17.  
  18. $query = "select count(*) as total from ftp where status=1";
  19.  
  20. $result = mysql_query($query) or die("Query failed.");
  21.  
  22.  
  23. while ($row = mysql_fetch_array($result))
  24.  
  25. {
  26.  
  27. $total=$row[total];
  28. }
  29.  
  30.  
  31. if($total<=0)
  32.  
  33.     {
  34.  
  35.     $no_server="1";
  36.  
  37.     $ftpid=0;
  38.  
  39.     $url=$server_url."/images/";
  40.  
  41.     }
  42.  
  43. else
  44.  
  45. {
  46.  
  47. $query = "select * from ftp where status=1 ORDER BY RAND() limit 1";
  48.  
  49. $result = mysql_query($query) or die("Query failed.");
  50.  
  51. while ($row = mysql_fetch_array($result))
  52.  
  53.     {
  54.  
  55.             $no_server="0";
  56.  
  57.         $ftpid=$row['ftpid'];
  58.  
  59.         $path=$row['name'];        
  60.  
  61.         $url=$row['dir'];        
  62.  
  63.         $host=$row['host'];        
  64.  
  65.         $user=$row['user'];        
  66.  
  67.         $pass=$row['ftppass'];        
  68.  
  69.     }    
  70.  
  71.  }
  72.  
  73. // get variables for fields on upload screen                
  74.  
  75.         $tos = $_POST['tos'];        
  76.  
  77.         $prv = $_POST['prv'];        
  78.  
  79.                 if($prv!="1")
  80.  
  81.                 $prv=0;
  82.  
  83.        
  84.  
  85.         $uploaderip = $_SERVER['REMOTE_ADDR'];
  86.        
  87.         $messages="";
  88.  
  89.         $msg="";
  90.  
  91.         $newID="";
  92.  
  93.         $FileName="";
  94.  
  95.         $FileFile="";
  96.  
  97.         $FileUrl="";
  98.  
  99.         $FileUrlLink="";
  100.  
  101.         $FiletnUrl="";
  102.  
  103.         // check for blocked ip address
  104.  
  105.         if ($uploaderip != "") {
  106.      
  107.                 $query = "select ip from blocked where ip = '$uploaderip'";
  108.  
  109.             $result = mysql_query($query) or die("Query failed.");
  110.  
  111.                 $num_rows = mysql_num_rows($result);
  112.  
  113.  
  114.  
  115.                 if ($num_rows > 0) {
  116.  
  117.                         $msg= "Your IP address (".$uploaderip.") has been blocked from using this service.";
  118.  
  119.                 }
  120.  
  121.  
  122.  
  123.         }
  124.  
  125.  
  126. if ($config[AcceptTerms]=="1"){
  127.  
  128. if ($tos=="")
  129.  
  130.                 {
  131.  
  132.                  $msg= "Du må akseptere reglene før du får laste opp bilder.";
  133.  
  134.                     echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";
  135.  
  136.                 }
  137.                
  138.                 }
  139.  
  140. if($msg=="")
  141. {
  142.  
  143.  
  144.  
  145. // check for a file
  146.  
  147. for($i=0;$i<=14;$i++)
  148.  
  149. {
  150.  
  151.         $err="0";
  152.  
  153.         $thefile = $_FILES['thefile'.$i];        
  154.  
  155.                
  156.                
  157.  
  158.         if ($thefile['name']!="")
  159.  
  160.                 {
  161.  
  162.  
  163.         // check for valid file extension
  164.  
  165. $path_parts = pathinfo($thefile['name']);
  166.        
  167. $file_ext = strtolower($path_parts['extension']);
  168.              
  169.        
  170.         if ($err == "0")
  171.  
  172.                 {
  173.  
  174.                         // check for valid file type
  175.  
  176.                         if (!in_array_nocase($file_ext, $valid_file_ext))
  177.  
  178.                                 {
  179.  
  180.                                         $messages.= "|<em>".$thefile['name']."</em> is not in a valid format (".$valid_mime_types_display.")";
  181.  
  182.                                        
  183.  
  184.                                         $err="1";
  185.  
  186.                                 }                        
  187.  
  188.                 }
  189.                
  190.  
  191.  if ($err == "0") {        
  192.    // check for valid image file
  193. $imageinfo = getimagesize($_FILES['thefile0']['tmp_name']);
  194.  
  195. if(!eregi('image',$imageinfo['mime'])) {
  196.  
  197. $messages.="|". "Sorry, This is not a valid image file!";
  198.   $err="1";  } }
  199.  
  200.  
  201.  
  202.                 if ($err == "0")
  203.  
  204.                 {        
  205.  
  206.                    
  207.  
  208.                      // check for valid file size
  209.  
  210.                         if ($thefile['size'] > ($max_file_size_b))
  211.  
  212.                                 {
  213.                 $filesizemb =($thefile['size']/1048576);
  214.                 $filesizemb = number_format($filesizemb, 3);
  215.                
  216. $messages.="Sorry but this image size is ".$filesizemb." MB which is bigger than the max allowed file size of ".$max_file_size_mb." MB.";
  217.  
  218.                                         $err="1";        
  219.  
  220.                                 }
  221.  
  222.                 }
  223.                
  224.                
  225.                
  226.                
  227.                
  228.                
  229.  
  230.  
  231.        
  232.  
  233.        // save the file, if no error messages
  234.  
  235.         if ($err == "0")
  236.  
  237.                 {
  238.  
  239.    
  240.     // replace special chars with  spaces
  241.     $thefile['name'] = eregi_replace("[^a-z0-9.]", " ", $thefile['name']);
  242.    
  243.     // Replace multiple spaces with one space
  244.     $thefile['name'] = ereg_replace(' +', ' ', $thefile['name']);
  245.     // Replace spaces with underscore
  246.     $thefile['name'] = str_replace(' ', '_', $thefile['name']);
  247.     // Replace hyphens with underscore
  248.     $thefile['name'] = str_replace('-', '_', $thefile['name']);  
  249.    
  250.     // Replace multiple underscores with one underscore
  251.     $thefile['name'] = ereg_replace('_+', '_', $thefile['name']);
  252.  
  253.  
  254. $path_parts = pathinfo($thefile['name']);
  255.  
  256. // if php < 5.2
  257.               if(!isset($path_parts['filename'])){
  258.                 $path_parts['filename'] = substr($path_parts['basename'], 0,strpos($path_parts['basename'],'.'));
  259.               }
  260.  
  261.  
  262. $thefile['name'] = strpos($path_parts['filename'], '.');
  263.  
  264.  
  265. $thefile['name'] = substr($path_parts['filename'], 0, 22); // limit file name length to 22 chars from the beginning
  266.  
  267. $thefile['name'] = $thefile['name'] . "." . strtolower($path_parts['extension']);    
  268.  
  269.  
  270.     // Generate prefix to add to file name
  271.    
  272.      $prefix = rand(11,99);
  273.  
  274.     // Add prefix to file name
  275.                        
  276.     $newFileName = $prefix . $thefile['name'];    
  277.  
  278.  
  279.  
  280.                         // SAVE THE PICTURE
  281.  
  282.  
  283.                         $FileName.="|". newImageName($thefile['name']);
  284.  
  285.                         $FileFile.="|". $server_dir . $newFileName;
  286.  
  287.                         $newFile = $server_dir . $newFileName;                        
  288.  
  289.                         $newFileUrl = $url . $newFileName;
  290.  
  291.                         $FileUrl.="|". $url . $newFileName;
  292.  
  293.                         $newFileUrlLink = $server_save_directory . $newFileName;
  294.  
  295.                         $FileUrlLink.="|". $newFileName;
  296.  
  297.  
  298.                          if (in_array_nocase($file_ext, $valid_file_ext))
  299.  
  300.                        
  301.                                 {
  302.  
  303. $lx = 3;
  304. if ($file_ext == "jpeg") {
  305. $lx = 4; }
  306.  
  307. $tnFileName = substr($newFileName, 0, strlen($newFileName) - $lx) . "jpg";
  308. $tnFileName = str_replace('.', '_tn.', $tnFileName);
  309.  
  310.  
  311.                                         $tnFile = $server_dir . $tnFileName;
  312.  
  313.                                         $FiletnUrl.="|". $url . $tnFileName;
  314.  
  315.                                         $tnFileUrl = $url . $tnFileName;
  316.  
  317.                                 }
  318.  
  319.                         else
  320.  
  321.                                 {
  322.  
  323.                                         $tnFileName = "";
  324.  
  325.                                         $tnFile = "";
  326.  
  327.                                         $tnFileUrl = "";
  328.  
  329.                                 }
  330.  
  331.                         $filesize = $thefile['size'];
  332.  
  333.                         $newID = "";
  334.  
  335.                         if (!@copy($thefile['tmp_name'], $newFile))
  336.  
  337.                                 {
  338.  
  339.                                         $messages.="|". "Please check site settings in admin panel and set proper value for server local path.<br><br>Also please make sure the images folder is chmodded to 0777";
  340.  
  341.                                 }
  342.  
  343.                         else
  344.  
  345.                                 {
  346.  
  347.                                         // add to database
  348.  
  349.                                         if($auth_id)        
  350.  
  351.                                         $uid=$auth_id;
  352.  
  353.                                         else $uid=0;
  354.  
  355.                                        
  356.  
  357.                                         //ftpupload($host,$user,$pass,$path."/".$dir."/".$newFileName,$newFileUrl);                                        
  358.  
  359.                                         //ftpupload
  360.  
  361.                                         if($no_server=="0")
  362.  
  363.                                          {
  364.  
  365.                                         $ftp =& new FTP();
  366.  
  367.                                         if ($ftp->connect($host)) {
  368.  
  369.                                             if ($ftp->login($user,$pass)) {
  370.  
  371.                                                 $ftp->chdir($path);
  372.  
  373.                                                 $ftp->put($newFileName,$newFile);
  374.  
  375.                                             }
  376.  
  377.                                         }
  378.                                        
  379.                         //  unlink($newFile);
  380.  
  381.  
  382.                                           }
  383.  
  384.                                         //ftpupload
  385.  
  386.                                         $date_add=time();
  387.  
  388.  
  389.                                         $query = "INSERT INTO images (prv,ftpid,userid,filename, tn_filename, filepath, ip, filesize,added) VALUES ($prv,$ftpid,$uid,'$newFileName', '$tnFileName', '$url', '$uploaderip', $filesize,$date_add)";
  390.  
  391.                                        
  392.  
  393.                                         mysql_query($query) or die("Database entry failed.");
  394.  
  395.                                         $newID.="|". mysql_insert_id();
  396.  
  397.                                 }
  398.  
  399.          
  400.                                  if ($file_ext == "jpeg" ||$file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif" || $file_ext == "bmp")
  401.  
  402.  
  403.                                 {
  404.  
  405.                                         if ($file_ext == "jpg")
  406.  
  407.                                                 {
  408.  
  409.                                                         $source_id = imagecreatefromjpeg($newFile);
  410.  
  411.                                                 }
  412.  
  413.                                         if ($file_ext == "jpeg")
  414.  
  415.                                                 {
  416.  
  417.                                                         $source_id = imagecreatefromjpeg($newFile);
  418.  
  419.                                                 }                                              
  420.  
  421.                                         elseif ($file_ext == "png")
  422.  
  423.                                                 {
  424.  
  425.                                                         $source_id = imagecreatefrompng($newFile);
  426.  
  427.                                                 }
  428.  
  429.                                         elseif ($file_ext == "gif")
  430.  
  431.                                                 {
  432.  
  433.                                                         $source_id = imagecreatefromgif($newFile);
  434.  
  435.                                                 }
  436.                                                
  437.                                          elseif ($file_ext == "bmp")
  438.  
  439.                                                 {
  440.  
  441.                                                         $source_id = ImageCreateFromBMP($newFile);
  442.  
  443.                                                 }
  444.  
  445.                                         $true_width = imagesx($source_id);
  446.  
  447.                                         $true_height = imagesy($source_id);
  448.  
  449.                                         // create thumb
  450.  
  451.                                         if ($true_width > $thumbnail_size_max || $true_height > $thumbnail_size_max)
  452.  
  453.                                                 {
  454.  
  455. $ratio1=$true_width/$dest_width;
  456. $ratio2=$true_height/$dest_height;
  457. if($ratio1>$ratio2) {
  458. $thumb_w=$dest_width;
  459. $thumb_h=$true_height/$ratio1;
  460. }
  461. else {
  462. $thumb_h=$dest_height;
  463. $thumb_w=$true_width/$ratio2;
  464. }
  465.  
  466.  
  467.  $target_id = imagecreatetruecolor($thumb_w, $thumb_h);
  468.  
  469.  $target_pic = imagecopyresized($target_id, $source_id, 0, 0, 0, 0, $thumb_w, $thumb_h, $true_width, $true_height);
  470.  
  471.  
  472.                                                         // create a thumbnail in JPEG format
  473.  
  474.                                                         imagejpeg($target_id, $tnFile, 100);
  475.  
  476.                                                         imagedestroy($target_id);
  477.  
  478.                                                 }
  479.  
  480.                                         else
  481.  
  482.                                                 {
  483.  
  484.                                                         copy($newFile, $tnFile);
  485.  
  486.                                                 }
  487.  
  488.                                         if($no_server=="0")
  489.  
  490.                                          {
  491.  
  492.                                             $ftp->put($tnFileName,$tnFile);
  493.  
  494.                                             $ftp->close();
  495.  
  496.                                             if(file_exists($newFile))
  497.  
  498.                                             {
  499.  
  500.                                              unlink($newFile);
  501.  
  502.                                             }
  503.  
  504.                                             if(file_exists($tnFile))
  505.  
  506.                                             {
  507.  
  508.                                              unlink($tnFile);
  509.  
  510.                                             }
  511.  
  512.                                             }
  513.  
  514.                                 }
  515.  
  516.                 }
  517.  
  518.                
  519.  
  520. }
  521.              
  522.  
  523. }
  524.  
  525. mysql_close($link);
  526.  
  527.         // create URL links to display to user
  528.  
  529.  
  530.  
  531.         $showURL1 = false;  // image on hosted page - image only
  532.  
  533.  
  534.  
  535.         $showURL2 = false;  // direct link to file - all
  536.  
  537.  
  538.  
  539.         $showURL3 = false;  // HTML for img - image only
  540.  
  541.  
  542.  
  543.         $showURL4 = false;  // [img][/img] tags - image only
  544.  
  545.  
  546.  
  547.         $showURL5 = false;  // thumbnail pic - image only
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.         // determine flags
  556.  
  557.  
  558.  
  559.         $showURL2 = true;
  560.  
  561.  
  562.  
  563.         if ($file_ext == "jpg" || $file_ext == "jpeg"|| $file_ext == "gif" || $file_ext == "png" || $file_ext == "bmp") {
  564.  
  565.  
  566.  
  567.                 $showURL1 = true;
  568.  
  569.  
  570.  
  571.                 $showURL3 = true;
  572.  
  573.  
  574.  
  575.                 $showURL4 = true;
  576.  
  577.  
  578.  
  579.         }
  580.  
  581.  
  582.  
  583.         if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png"|| $file_ext == "jpeg" || $file_ext == "bmp") {
  584.  
  585.                         $showURL5 = true;                        
  586.  
  587.         }
  588.  
  589.                     echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";
  590.  
  591.  
  592. }
  593. else
  594. {
  595.     echo "<script language='javascript'>parent.uploaderror('".$msg."');</script>";
  596.     exit;
  597. }
  598.  
  599.  
  600.  
  601.         function newImageName($fname) {
  602.  
  603.  
  604.  
  605.                 $timestamp = time();
  606.  
  607.  
  608.  
  609.                 $new_image_file_ext = substr($fname, strlen($fname) - 3, strlen($fname));
  610.  
  611.  
  612.  
  613.                 if ($new_image_file_ext == "peg") {
  614.  
  615.  
  616.  
  617.                         $ext = ".jpg";
  618.  
  619.  
  620.  
  621.                 } else {
  622.  
  623.  
  624.  
  625.                         $ext = "." . $new_image_file_ext;
  626.  
  627.  
  628.  
  629.                 }
  630.  
  631.  
  632.  
  633.                 $newfilename = randString() . substr($timestamp, strlen(timestamp) - 4, strlen(timestamp)) . $ext;
  634.  
  635.                
  636.  
  637.                 return $newfilename;
  638.  
  639.  
  640.  
  641.         }
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.         function randString() {
  650.  
  651.  
  652.  
  653.                 $newstring="";
  654.  
  655.  
  656.  
  657.                 while(strlen($newstring) < 3) {
  658.  
  659.  
  660.  
  661.                         $randnum = mt_rand(0,61);
  662.  
  663.  
  664.  
  665.                         if ($randnum < 10) {
  666.  
  667.  
  668.  
  669.                                 $newstring .= chr($randnum + 48);
  670.  
  671.  
  672.  
  673.                         } elseif ($randnum < 36) {
  674.  
  675.  
  676.  
  677.                                 $newstring .= chr($randnum + 55);
  678.  
  679.  
  680.  
  681.                         } else {
  682.  
  683.  
  684.  
  685.                                 $newstring .= chr($randnum + 61);
  686.  
  687.  
  688.  
  689.                         }
  690.  
  691.  
  692.  
  693.                 }
  694.  
  695.  
  696.  
  697.                 return $newstring;
  698.  
  699.  
  700.  
  701.         }
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.         function in_array_nocase($item, $array) {
  710.  
  711.  
  712.  
  713.                 $item = &strtoupper($item);
  714.  
  715.  
  716.  
  717.                 foreach($array as $element) {
  718.  
  719.  
  720.  
  721.                         if ($item == strtoupper($element)) {
  722.  
  723.  
  724.  
  725.                                 return true;
  726.  
  727.  
  728.  
  729.                         }
  730.  
  731.  
  732.  
  733.                 }
  734.  
  735.  
  736.  
  737.                 return false;
  738.  
  739.  
  740.  
  741.         }
  742.  
  743.  
  744.  
  745. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement