Guest User

Futallaby main script

a guest
Apr 10th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.22 KB | None | 0 0
  1. <?php
  2. # Futallaby 040103
  3. #
  4. # For setup instructions and latest version, please visit:
  5. # http://www.1chan.net/futallaby/
  6. #
  7. # Based on GazouBBS and Futaba
  8.  
  9. include "config.php";
  10. include "strings_e.php";        //String resource file
  11.  
  12.  
  13. extract($_POST);
  14. extract($_GET);
  15. extract($_COOKIE);
  16. $upfile_name=$_FILES["upfile"]["name"];
  17. $upfile=$_FILES["upfile"]["tmp_name"];
  18.  
  19. $path = realpath("./").'/'.IMG_DIR;
  20. ignore_user_abort(TRUE);
  21. $badstring = array("dummy_string","dummy_string2"); // Refused text
  22. $badfile = array("dummy","dummy2"); //Refused files (md5 hashes)
  23.  
  24. $badip = array("addr1\\.dummy\\.com","addr2\\.dummy\\.com"); //Refused hosts (IP bans)
  25.  
  26. if(!$con=mysqli_connect(SQLHOST,SQLUSER,SQLPASS)){
  27.   echo S_SQLCONF;   //unable to connect to DB (wrong user/pass?)
  28.   exit;
  29. }
  30.  
  31. $db_id=mysqli_select_db($con,SQLDB);
  32.   if(!$db_id){echo S_SQLDBSF;}
  33.  
  34. if (!table_exist($con, SQLLOG)) {
  35.   echo (SQLLOG.S_TCREATE);
  36.   $result = mysql_call($con,"create table ".SQLLOG." (primary key(no),
  37.    no    int not null auto_increment,
  38.    now   text,
  39.    name  text,
  40.    email text,
  41.    sub   text,
  42.    com   text,
  43.    host  text,
  44.    pwd   text,
  45.    ext   text,
  46.    w     int,
  47.    h     int,
  48.    tim   text,
  49.    time  int,
  50.    md5   text,
  51.    fsize int,
  52.    root  timestamp,
  53.    resto int)");
  54.   if(!$result){echo S_TCREATEF;}
  55. }
  56.  
  57. function updatelog($resno=0){
  58.   global $path;
  59.   global $con;
  60.  
  61.   $find = false;
  62.   $resno=(int)$resno;
  63.   if($resno){
  64.     $result = mysql_call($con, "select * from ".SQLLOG." where root>0 and no=$resno");
  65.     if($result){
  66.       $find = mysqli_fetch_row($result);
  67.       mysqli_free_result($result);
  68.     }
  69.     if(!$find) error(S_REPORTERR);
  70.   }
  71.   if($resno){
  72.     if(!$treeline=mysql_call($con, "select * from ".SQLLOG." where root>0 and no=".$resno." order by root desc")){echo S_SQLFAIL;}
  73.   }else{
  74.     if(!$treeline=mysql_call($con, "select * from ".SQLLOG." where root>0 order by root desc")){echo S_SQLFAIL;}
  75.   }
  76.  
  77.   //Finding the last entry number
  78.   if(!$result=mysql_call($con, "select max(no) from ".SQLLOG)){echo S_SQLFAIL;}
  79.   $row=mysqli_fetch_array($result);
  80.   $lastno=(int)$row[0];
  81.   mysqli_free_result($result);
  82.  
  83.   $counttree=mysqli_num_rows($treeline);
  84.   if(!$counttree){
  85.     $logfilename=PHP_SELF2;
  86.     $dat='';
  87.     head($dat);
  88.     form($dat,$resno);
  89.     $fp = fopen($logfilename, "w");
  90.     set_file_buffer($fp, 0);
  91.     rewind($fp);
  92.     fputs($fp, $dat);
  93.     fclose($fp);
  94.     chmod($logfilename,0666);
  95.   }
  96.   for($page=0;$page<$counttree;$page+=PAGE_DEF){
  97.     $dat='';
  98.     head($dat);
  99.     form($dat,$resno);
  100.     if(!$resno){
  101.       $st = $page;
  102.     }
  103.     $dat.='<form action="'.PHP_SELF.'" method="post">';
  104.  
  105.   for($i = $st; $i < $st+PAGE_DEF; $i++){
  106.     list($no,$now,$name,$email,$sub,$com,$host,$pwd,$ext,$w,$h,$tim,$time,$md5,$fsize,)=mysqli_fetch_row($treeline);
  107.     if(!$no){break;}
  108.  
  109.     // URL and link
  110.     if($email) $name = "<a href=\"mailto:$email\">$name</a>";
  111.     $com = auto_link($com);
  112.     $com = preg_replace("/(^|>)(&gt;[^<]*)/i", "\\1<div class=\"unkfunc\">\\2</div>", $com);
  113.     // Picture file name
  114.     $img = $path.$tim.$ext;
  115.     $src = IMG_DIR.$tim.$ext;
  116.     // img tag creation
  117.     $imgsrc = "";
  118.     if($ext){
  119.       $size = $fsize;//file size displayed in alt text
  120.       if($w && $h){//when there is size...
  121.         if(@is_file(THUMB_DIR.$tim.'s.jpg')){
  122.           $imgsrc = "    <span class=\"thumbnailmsg\">".S_THUMB."</span><br /><a href=\"".$src."\" target=\"_blank\"><img src=\"".THUMB_DIR.$tim.'s.jpg'.
  123.       "\" border=\"0\" align=\"left\" width=\"$w\" height=\"$h\" hspace=\"20\" alt=\"".$size." B\" /></a><br />";
  124.         }else{
  125.           $imgsrc = "<a href=\"".$src."\" target=\"_blank\"><img src=\"".$src.
  126.       "\" border=\"0\" align=\"left\" width=\"$w\" height=\"$h\" hspace=\"20\" alt=\"".$size." B\" /></a><br />";
  127.         }
  128.       }else{
  129.         $imgsrc = "<a href=\"".$src."\" target=\"_blank\"><img src=\"".$src.
  130.       "\" border=\"0\" align=\"left\" hspace=\"20\" alt=\"".$size." B\" /></a><br />";
  131.       }
  132.       $dat.="<span class=\"filesize\">".S_PICNAME."<a href=\"$src\" target=\"_blank\">$tim$ext</a>-($size B)</span>$imgsrc";
  133.     }
  134.     //  Main creation
  135.     $dat.="<input type=\"checkbox\" name=\"$no\" value=\"delete\" /><span class=\"filetitle\">$sub</span>   \n";
  136.     $dat.="Name <span class=\"postername\">$name</span> $now No.$no &nbsp; \n";
  137.     if(!$resno) $dat.="[<a href=\"".PHP_SELF."?res=$no\">".S_REPLY."</a>]";
  138.     $dat.="\n<blockquote>$com</blockquote>";
  139.  
  140.      // Deletion pending
  141.      if($lastno-LOG_MAX*0.95>$no){
  142.       $dat.="<span class=\"oldpost\">".S_OLD."</span><br />\n";
  143.      }
  144.  
  145.     if(!$resline=mysql_call($con, "select * from ".SQLLOG." where resto=".$no." order by no")){echo S_SQLFAIL;}
  146.     $countres=mysqli_num_rows($resline);
  147.  
  148.     if(!$resno){
  149.      $s=$countres - 10;
  150.      if($s<0){$s=0;}
  151.      elseif($s>0){
  152.       $dat.="<span class=\"omittedposts\">".S_RESU.$s.S_ABBR."</span><br />\n";
  153.      }
  154.     }else{$s=0;}
  155.  
  156.     while($resrow=mysqli_fetch_row($resline)){
  157.       if($s>0){$s--;continue;}
  158.       list($no,$now,$name,$email,$sub,$com,$host,$pwd,$ext,$w,$h,$tim,$time,$md5,$fsize,)=$resrow;
  159.       if(!$no){break;}
  160.  
  161.       // URL and e-mail
  162.       if($email) $name = "<a href=\"mailto:$email\">$name</a>";
  163.       $com = auto_link($com);
  164.       //$com = preg_replace("/(^|>)(&gt;[^<]*)/i", "\\1<font color=".RE_COL.">\\2</font>", $com);
  165.       $com = preg_replace("/(^|>)(&gt;[^<]*)/i", "\\1<div class=\"unkfunc\">\\2</div>", $com);
  166.       // Main creation
  167.       $dat.="<table><tr><td class=\"doubledash\">&gt;&gt;</td><td class=\"reply\">\n";
  168.       $dat.="<input type=\"checkbox\" name=\"$no\" value=\"delete\" /><span class=\"replytitle\">$sub</span> \n";
  169.       $dat.="Name <span class=\"commentpostername\">$name</span> $now No.$no &nbsp; \n";
  170.       $dat.="<blockquote>$com</blockquote>";
  171.       $dat.="</td></tr></table>\n";
  172.     }
  173.     $dat.="<br clear=\"left\" /><hr />\n";
  174.     clearstatcache();//clear stat cache of a file
  175.     mysqli_free_result($resline);
  176.     $p++;
  177.     if($resno){break;} //only one tree line at time of res
  178.   }
  179. $dat.='<table align="right"><tr><td nowrap="nowrap" align="center">
  180. <input type="hidden" name="mode" value="usrdel" />'.S_REPDEL.'[<input type="checkbox" name="onlyimgdel" value="on" />'.S_DELPICONLY.']<br />
  181. '.S_DELKEY.'<input type="password" name="pwd" size="8" maxlength="8" value="" />
  182. <input type="submit" value="'.S_DELETE.'" /></td></tr></table></form>
  183. <script language="JavaScript" type="script"><!--
  184. l();
  185. //--></script>';
  186.  
  187.     if(!$resno){ // if not in res display mode
  188.       $prev = $st - PAGE_DEF;
  189.       $next = $st + PAGE_DEF;
  190.     //  Page processing
  191.       $dat.="<table><tr>";
  192.       if($prev >= 0){
  193.         if($prev==0){
  194.           $dat.="<form action=\"".PHP_SELF2."\" method=\"get\" /><td>";
  195.         }else{
  196.           $dat.="<form action=\"".$prev/PAGE_DEF.PHP_EXT."\" method=\"get>\" /<td>";
  197.         }
  198.         $dat.="<input type=\"submit\" value=\"".S_PREV."\" />";
  199.         $dat.="</td></form>";
  200.       }else{$dat.="<td>".S_FIRSTPG."</td>";}
  201.  
  202.       $dat.="<td>";
  203.       for($i = 0; $i < $counttree ; $i+=PAGE_DEF){
  204.         if($i&&!($i%(PAGE_DEF*2))){$dat.=" ";}
  205.         if($st==$i){$dat.="[".($i/PAGE_DEF)."] ";}
  206.         else{
  207.           if($i==0){$dat.="[<a href=\"".PHP_SELF2."\">0</a>] ";}
  208.           else{$dat.="[<a href=\"".($i/PAGE_DEF).PHP_EXT."\">".($i/PAGE_DEF)."</a>] ";}
  209.         }
  210.       }
  211.       $dat.="</td>";
  212.  
  213.       if($p >= PAGE_DEF && $counttree > $next){
  214.         $dat.="<td><form action=\"".$next/PAGE_DEF.PHP_EXT."\" method=\"get\">";
  215.         $dat.="<input type=\"submit\" value=\"".S_NEXT."\" />";
  216.         $dat.="</form></td>";
  217.       }else{$dat.="<td>".S_LASTPG."</td>";}
  218.         $dat.="</tr></table><br clear=\"all\" />\n";
  219.     }
  220.     foot($dat);
  221.     if($resno){echo $dat;break;}
  222.     if($page==0){$logfilename=PHP_SELF2;}
  223.     else{$logfilename=$page/PAGE_DEF.PHP_EXT;}
  224.     $fp = fopen($logfilename, "w");
  225.     set_file_buffer($fp, 0);
  226.     rewind($fp);
  227.     fputs($fp, $dat);
  228.     fclose($fp);
  229.     chmod($logfilename,0666);
  230.   }
  231.   mysqli_free_result($treeline);
  232. }
  233.  
  234.  
  235. function mysql_call($link, $query){
  236.   $ret=mysqli_query($link, $query);
  237.   if(!$ret){
  238. #echo "error!!<br />";
  239.    echo $query."<br />";
  240. #    echo mysql_errno().": ".mysql_error()."<br />";
  241.  }
  242.   return $ret;
  243. }
  244.  
  245. /* head */
  246. function head(&$dat){
  247. $titlepart = '';
  248. if (SHOWTITLEIMG == 1) {
  249.     $titlepart.= '<img src="'.TITLEIMG.'" alt="'.TITLE.'" />';
  250.     if (SHOWTITLETXT == 1) {$titlepart.= '<br />';}
  251. } else if (SHOWTITLEIMG == 2) {
  252.     $titlepart.= '<img src="'.TITLEIMG.'" onclick="this.src=this.src;" alt="'.TITLE.'" />';
  253.     if (SHOWTITLETXT == 1) {$titlepart.= '<br />';}
  254. }
  255. if (SHOWTITLETXT == 1) {
  256.     $titlepart.= ''.TITLE.'';
  257. }
  258.   $dat.='
  259. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  260.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  261. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="jp"><head>
  262. <meta http-equiv="content-type"  content="text/html;charset=utf-8" />
  263. <!-- meta HTTP-EQUIV="pragma" CONTENT="no-cache" -->
  264. <link rel="stylesheet" type="text/css" href="'.CSSFILE.'" title="Standard Futaba" />
  265. <title>'.TITLE.'</title>
  266. <script language="JavaScript" type="script"><!--
  267. function l(e){var P=getCookie("pwdc"),N=getCookie("namec"),i;with(document){for(i=0;i<forms.length;i++){if(forms[i].pwd)with(forms[i]){if(!pwd.value)pwd.value=P;}if(forms[i].name)with(forms[i]){if(!name.value)name.value=N;}}}};function getCookie(key, tmp1, tmp2, xx1, xx2, xx3) {tmp1 = " " + document.cookie + ";";xx1 = xx2 = 0;len = tmp1.length;  while (xx1 < len) {xx2 = tmp1.indexOf(";", xx1);tmp2 = tmp1.substring(xx1 + 1, xx2);xx3 = tmp2.indexOf("=");if (tmp2.substring(0, xx3) == key) {return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));}xx1 = xx2 + 1;}return("");}
  268. //--></script>
  269. </head>
  270. <body>
  271. '.$titlebar.'
  272. <div class="adminbar">
  273. [<a href="'.HOME.'" target="_top">'.S_HOME.'</a>]
  274. [<a href="'.PHP_SELF.'?mode=admin">'.S_ADMIN.'</a>]
  275. </div>
  276. <div class="logo">'.$titlepart.'</div><hr /><br /><br />';
  277. }
  278. /* Contribution form */
  279. function form(&$dat,$resno,$admin=""){
  280.   $maxbyte = MAX_KB * 1024;
  281.   $no=$resno;
  282.   if($resno){
  283.     $msg .= "[<a href=\"".PHP_SELF2."\">".S_RETURN."</a>]\n";
  284.     $msg .= "<div class=\"theading\">".S_POSTING."</div>\n";
  285.   }
  286.   if($admin){
  287.     $hidden = "<input type=hidden name=admin value=\"".ADMIN_PASS."\">";
  288.     $msg = "<em>".S_NOTAGS."</em>"; /* Note to self:  Find out where this happened. */
  289.   }
  290.   $dat.=$msg.'<div align="center"><div class="postarea">
  291. <form action="'.PHP_SELF.'" method="post" enctype="multipart/form-data">
  292. <input type="hidden" name="mode" value="regist" />
  293. '.$hidden.'
  294. <input type="hidden" name="MAX_FILE_SIZE" value="'.$maxbyte.'" />
  295. ';
  296. if($no){$dat.='<input type="hidden" name="resto" value="'.$no.'" />
  297. ';}
  298. $dat.='<table>
  299. <tr><td class="postblock" align="left">'.S_NAME.'</td><td align="left"><input type="text" name="name" size="28" /></td></tr>
  300. <tr><td class="postblock" align="left">'.S_EMAIL.'</td><td align="left"><input type="text" name="email" size="28" /></td></tr>
  301. <tr><td class="postblock" align="left">'.S_SUBJECT.'</td><td align="left"><input type="text" name="sub" size="35" />
  302. <input type="submit" value="'.S_SUBMIT.'" /></td></tr>
  303. <tr><td class="postblock" align="left">'.S_COMMENT.'</td><td align="left"><textarea name="com" cols="48" rows="4"></textarea></td></tr>
  304. ';
  305. if(!$resno){
  306. $dat.='<tr><td class="postblock" align="left">'.S_UPLOADFILE.'</td>
  307. <td><input type="file" name="upfile" size="35" />
  308. [<label><input type="checkbox" name="textonly" value="on" />'.S_NOFILE.'</label>]</td></tr>
  309. ';}
  310. $dat.='<tr><td align="left" class="postblock" align="left">'.S_DELPASS.'</td><td align="left"><input type="password" name="pwd" size="8" maxlength="8" value="" />'.S_DELEXPL.'</td></tr>
  311. <tr><td colspan="2">
  312. <div align="left" class="rules">'.S_RULES.'</div></td></tr></table></form></div></div><hr />';
  313. }
  314.  
  315. /* Footer */
  316. function foot(&$dat){
  317.   $dat.='
  318. <div class="footer">'.S_FOOT.'</div>
  319.  
  320. </body></html>';
  321. }
  322. function error($mes,$dest=''){ /* Hey guys, what's going on in this function?  Since I don't see it so often, I'll leave the tags alone for now.*/
  323.   global $upfile_name,$path;
  324.   if(is_file($dest)) unlink($dest);
  325.   head($dat);
  326.   echo $dat;
  327.   echo "<br /><br /><hr size=1><br /><br />
  328.        <center><font color=blue size=5>$mes<br /><br /><a href=".PHP_SELF2.">".S_RELOAD."</a></b></font></center>
  329.        <br /><br /><hr size=1>";
  330.   die("</body></html>");
  331. }
  332. /* Auto Linker */
  333. function auto_link($proto){
  334.   $proto = preg_replace("[(https?|ftp|news)(://[[:alnum:]\+\$\;\?\.%,!#~*/:@&=_-]+)]","<a href=\"\\1\\2\"
  335. target=\"_blank\">\\1\\2</a>",$proto);
  336.   return $proto;
  337. }
  338.  
  339. function  proxy_connect($port) {
  340.   $fp = @fsockopen ($_SERVER["REMOTE_ADDR"], $port,$a,$b,2);
  341.   if(!$fp){return 0;}else{return 1;}
  342. }
  343. /* Regist */
  344. function regist($name,$email,$sub,$com,$url,$pwd,$upfile,$upfile_name,$resto){
  345.   global $path,$badstring,$badfile,$badip,$pwdc,$textonly;
  346.   global $con;
  347.  
  348.   // time
  349.   $time = time();
  350.   $tim = $time.substr(microtime(),2,3);
  351.  
  352.   // upload processing
  353.   if($upfile&&file_exists($upfile)){
  354.     $dest = $path.$tim.'.tmp';
  355.     move_uploaded_file($upfile, $dest);
  356.     //if an error in up, it changes to down (what?)
  357.     //copy($upfile, $dest);
  358.     $upfile_name = CleanStr($upfile_name);
  359.     if(!is_file($dest)) error(S_UPFAIL,$dest);
  360.     $isimage = 1;
  361.     $size = getimagesize($dest);
  362.     if(!is_array($size)) error(S_NOREC,$dest); //$isimage=0;     
  363.     $md5 = md5_of_file($dest);
  364.     foreach($badfile as $value){if(preg_match("/^$value/",$md5)){
  365.       error(S_SAMEPIC,$dest); //Refuse this image
  366.     }}
  367.     chmod($dest,0666);
  368.     $W = $isimage ? $size[0] : 0;
  369.     $H = $isimage ? $size[1] : 0;
  370.     $fsize = filesize($dest);
  371.     if($fsize>MAX_KB * 1024) error(S_TOOBIG,$dest);
  372.     if ($isimage) {
  373.       switch ($size[2]) {
  374.         case 1 : $ext=".gif";break;
  375.         case 2 : $ext=".jpg";break;
  376.         case 3 : $ext=".png";break;
  377.         case 4 : $ext=".swf";break;
  378.         case 5 : $ext=".psd";break;
  379.         case 6 : $ext=".bmp";break;
  380.         case 13 : $ext=".swf";break;
  381.         default : $ext=".xxx";break;
  382.       }
  383.     }
  384.  
  385.     // Picture reduction
  386.     if($W > MAX_W || $H > MAX_H){
  387.       $W2 = MAX_W / $W;
  388.       $H2 = MAX_H / $H;
  389.       ($W2 < $H2) ? $key = $W2 : $key = $H2;
  390.       $W = ceil($W * $key);
  391.       $H = ceil($H * $key);
  392.     }
  393.     $mes = S_UPGOOD;
  394.   }
  395.  
  396.   if($_FILES["upfile"]["error"]==2){
  397.     error(S_TOOBIG,$dest);
  398.   }
  399.   if($upfile_name&&$_FILES["upfile"]["size"]==0){
  400.     error(S_TOOBIGORNONE,$dest);
  401.   }
  402.  
  403.   //The last result number
  404.   if(!$result=mysql_call($con, "select max(no) from ".SQLLOG)){echo S_SQLFAIL;}
  405.   $row=mysqli_fetch_array($result);
  406.   $lastno=(int)$row[0];
  407.   mysqli_free_result($result);
  408.  
  409.   // Number of log lines
  410.   if(!$result=mysql_call($con, "select no,ext,tim from ".SQLLOG." where no<=".($lastno-LOG_MAX))){echo S_SQLFAIL;}
  411.   else{
  412.     while($resrow=mysqli_fetch_row($result)){
  413.       list($dno,$dext,$dtim)=$resrow;
  414.       if(!mysql_call($con, "delete from ".SQLLOG." where no=".$dno)){echo S_SQLFAIL;}
  415.       if($dext){
  416.         if(is_file($path.$dtim.$dext)) unlink($path.$dtim.$dext);
  417.         if(is_file(THUMB_DIR.$dtim.'s.jpg')) unlink(THUMB_DIR.$dtim.'s.jpg');
  418.       }
  419.     }
  420.     mysqli_free_result($result);
  421.   }
  422.  
  423.   $find = false;
  424.   $resto=(int)$resto;
  425.   if($resto){
  426.     if(!$result = mysql_call($con, "select * from ".SQLLOG." where root>0 and no=$resto")){echo S_SQLFAIL;}
  427.     else{
  428.       $find = mysqli_fetch_row($result);
  429.       mysqli_free_result($result);
  430.     }
  431.     if(!$find) error(S_NOTHREADERR,$dest);
  432.   }
  433.  
  434.   foreach($badstring as $value){if(preg_match("/$value/",$com)||preg_match("/$value/",$sub)||preg_match("/$value/",$name)||preg_match("/$value/",$email)){
  435.   error(S_STRREF,$dest);};}
  436.   if($_SERVER["REQUEST_METHOD"] != "POST") error(S_UNJUST,$dest);
  437.   // Form content check
  438.   if(!$name||preg_match("/^[ |@|]*$/",$name)) $name="";
  439.   if(!$com||preg_match("/^[ |@|\t]*$/",$com)) $com="";
  440.   if(!$sub||preg_match("/^[ |@|]*$/",$sub))   $sub="";
  441.  
  442.   if(!$resto&&!$textonly&&!is_file($dest)) error(S_NOPIC,$dest);
  443.   if(!$com&&!is_file($dest)) error(S_NOTEXT,$dest);
  444.  
  445.  $name=preg_replace("/".S_MANAGEMENT."/","\"".S_MANAGEMENT."\"",$name);
  446.  $name=preg_replace("/".S_DELETION."/","\"".S_DELETION."\"",$name);
  447.  
  448. if(strlen($com) > 1000) error(S_TOOLONG,$dest);
  449. if(strlen($name) > 100) error(S_TOOLONG,$dest);
  450. if(strlen($email) > 100) error(S_TOOLONG,$dest);
  451. if(strlen($sub) > 100) error(S_TOOLONG,$dest);
  452. if(strlen($resto) > 10) error(S_UNUSUAL,$dest);
  453. if(strlen($url) > 10) error(S_UNUSUAL,$dest);
  454.  
  455.   //host check
  456.   $host = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
  457.  
  458.   foreach($badip as $value){ //Refusal hosts
  459.    if(preg_match("/$value$/i",$host)){
  460.     error(S_BADHOST,$dest);
  461.   }}
  462.   if(preg_match("/^mail/i",$host)
  463.     || preg_match("/^ns/i",$host)
  464.     || preg_match("/^dns/i",$host)
  465.     || preg_match("/^ftp/",$host)
  466.     || preg_match("/^prox/i",$host)
  467.     || preg_match("/i^pc/i",$host)
  468.     || preg_match("/^[^\.]\.[^\.]$/i",$host)){
  469.     $pxck = "on";
  470.   }
  471.   if(preg_match("/ne\\.jp$/i",$host)||
  472.     preg_match("/ad\\.jp$/i",$host)||
  473.     preg_match("/bbtec\\.net$/i",$host)||
  474.     preg_match("/aol\\.com$/i",$host)||
  475.     preg_match("/uu\\.net$/i",$host)||
  476.     preg_match("/asahi-net\\.or\\.jp$/i",$host)||
  477.     preg_match("/irim\\.or\\.jp$/i",$host)
  478.     ){$pxck = "off";}
  479.   else{$pxck = "on";}
  480.  
  481.   if($pxck=="on" && PROXY_CHECK){
  482.     if(proxy_connect('80') == 1){
  483.       error(S_PROXY80,$dest);
  484.     } elseif(proxy_connect('8080') == 1){
  485.       error(S_PROXY8080,$dest);
  486.     }
  487.   }
  488.  
  489.   // No, path, time, and url format
  490.   srand((double)microtime()*1000000);
  491.   if($pwd==""){
  492.     if($pwdc==""){
  493.       $pwd=rand();$pwd=substr($pwd,0,8);
  494.     }else{
  495.       $pwd=$pwdc;
  496.     }
  497.   }
  498.  
  499.   $c_pass = $pwd;
  500.   $pass = ($pwd) ? substr(md5($pwd),2,8) : "*";
  501.  $youbi = array(S_SUN, S_MON, S_TUE, S_WED, S_THU, S_FRI, S_SAT);
  502.   $yd = $youbi[gmdate("w", $time+9*60*60)] ;
  503.   $now = gmdate("y/m/d",$time+9*60*60)."(".(string)$yd.")".gmdate("H:i",$time+9*60*60);
  504.   if(DISP_ID){
  505.     if($email&&DISP_ID==1){
  506.       $now .= " ID:???";
  507.     }else{
  508.       $now.=" ID:".substr(crypt(md5($_SERVER["REMOTE_ADDR"].'id'.gmdate("Ymd", $time+9*60*60)),'id'),-8);
  509.     }
  510.   }
  511.   //Text plastic surgery (rorororor)
  512.   $email= CleanStr($email);  $email=preg_replace("/[\r\n]/","",$email);
  513.   $sub  = CleanStr($sub);    $sub  =preg_replace("/[\r\n]/","",$sub);
  514.   $url  = CleanStr($url);    $url  =preg_replace("/[\r\n]/","",$url);
  515.   $resto= CleanStr($resto);  $resto=preg_replace("/[\r\n]/","",$resto);
  516.   $com  = CleanStr($com);
  517.   // Standardize new character lines
  518.   $com = str_replace( "\r\n",  "\n", $com);
  519.   $com = str_replace( "\r",  "\n", $com);
  520.   // Continuous lines
  521.   $com = preg_replace("/\n((!@| )*\n){3,}/","\n",$com);
  522.   if(!BR_CHECK || substr_count($com,"\n")<BR_CHECK){
  523.     $com = nl2br($com);     //br is substituted before newline char
  524.   }
  525.   $com = str_replace("\n",  "", $com);  //\n is erased
  526.  
  527.   //$name=preg_replace(TRIPKEY,"",$name);  //erase tripkeys in name
  528.   $name=preg_replace("/[\r\n]/","",$name);
  529.   $names=$name;
  530.   $name = trim($name);//blankspace removal
  531.   if (get_magic_quotes_gpc()) {//magic quotes is deleted (?)
  532.     $name = stripslashes($name);
  533.   }
  534.   $name = htmlspecialchars($name);//remove html special chars
  535.   $name = str_replace("&amp;", "&", $name);//remove ampersands
  536.   $name = str_replace(",", "&#44;", $name);//remove commas
  537.  
  538.  
  539.  
  540.  
  541.   if(preg_match("/(#|!)(.*)/",$names,$regs)){
  542.     $cap = $regs[2];
  543.     $cap=strtr($cap,"&amp;", "&");
  544.     $cap=strtr($cap,"&#44;", ",");
  545.     $name=preg_replace("/(#|!)(.*)/","",$name);
  546.     //$name=preg_replace(TRIPKEY,"",$name);  //erase tripkeys in name
  547.     $salt=substr($cap."H.",1,2);
  548.     $salt=preg_replace("/[^\.-z]/",".",$salt);
  549.     $salt=strtr($salt,":;<=>?@[\\]^_`","ABCDEFGabcdef");
  550.     $name.=TRIPKEY.substr(crypt($cap,$salt),-10)."";
  551.   }
  552.  
  553.  if(!$name) $name=S_ANONAME;
  554.  if(!$com) $com=S_ANOTEXT;
  555.  if(!$sub) $sub=S_ANOTITLE;
  556.  
  557.   // Read the log
  558.   $query="select time from ".SQLLOG." where com='".mysqli_escape_string($con, $com)."' ".
  559.          "and host='".mysqli_escape_string($con, $host)."' ".
  560.          "and no>".($lastno-20);  //the same
  561.   if(!$result=mysql_call($con, $query)){echo S_SQLFAIL;}
  562.   $row=mysqli_fetch_array($result);
  563.   mysqli_free_result($result);
  564.   if($row&&!$upfile_name)error(S_RENZOKU3,$dest);
  565.  
  566.   $query="select time from ".SQLLOG." where time>".($time - RENZOKU)." ".
  567.          "and host='".mysqli_escape_string($con, $host)."' ";  //from precontribution
  568.   if(!$result=mysql_call($con, $query)){echo S_SQLFAIL;}
  569.   $row=mysqli_fetch_array($result);
  570.   mysqli_free_result($result);
  571.   if($row&&!$upfile_name)error(S_RENZOKU3, $dest);
  572.  
  573.   // Upload processing
  574.   if($dest&&file_exists($dest)){
  575.  
  576.   $query="select time from ".SQLLOG." where time>".($time - RENZOKU2)." ".
  577.          "and host='".mysqli_escape_string($con, $host)."' ";  //from precontribution
  578.   if(!$result=mysql_call($con, $query)){echo S_SQLFAIL;}
  579.   $row=mysqli_fetch_array($result);
  580.   mysqli_free_result($result);
  581.   if($row&&$upfile_name)error(S_RENZOKU2,$dest);
  582.  
  583.   //Duplicate image check
  584.     $result = mysql_call($con, "select tim,ext,md5 from ".SQLLOG." where md5='".$md5."'");
  585.     if($result){
  586.       list($timp,$extp,$md5p) = mysqli_fetch_row($result);
  587.       mysqli_free_result($result);
  588. #      if($timp&&file_exists($path.$timp.$extp)){ #}
  589.      if($timp){
  590.         error(S_DUPE,$dest);
  591.       }
  592.     }
  593.   }
  594.  
  595.   $restoqu=(int)$resto;
  596.   if($resto){ //res,root processing
  597.     $rootqu="0";
  598.     if(!$resline=mysql_call($con, "select * from ".SQLLOG." where resto=".$resto)){echo S_SQLFAIL;}
  599.     $countres=mysqli_num_rows($resline);
  600.     mysqli_free_result($resline);
  601.     if(!stristr($email,'sage') && $countres < MAX_RES){
  602.       $query="update ".SQLLOG." set root=now() where no=$resto"; //age
  603.       if(!$result=mysql_call($con, $query)){echo S_SQLFAIL;}
  604.     }
  605.   }else{$rootqu="now()";} //now it is root
  606.  
  607.   $query="insert into ".SQLLOG." (now,name,email,sub,com,host,pwd,ext,w,h,tim,time,md5,fsize,root,resto) values (".
  608. "'".$now."',".
  609. "'".mysqli_escape_string($con, $name)."',".
  610. "'".mysqli_escape_string($con, $email)."',".
  611. "'".mysqli_escape_string($con, $sub)."',".
  612. "'".mysqli_escape_string($con, $com)."',".
  613. "'".mysqli_escape_string($con, $host)."',".
  614. "'".mysqli_escape_string($con, $pass)."',".
  615. "'".$ext."',".
  616. (int)$W.",".
  617. (int)$H.",".
  618. "'".$tim."',".
  619. (int)$time.",".
  620. "'".$md5."',".
  621. (int)$fsize.",".
  622. $rootqu.",".
  623. (int)$resto.")";
  624.   if(!$result=mysql_call($con, $query)){echo S_SQLFAIL;}  //post registration
  625.  
  626.     //Cookies
  627.   setcookie ("pwdc", $c_pass,time()+7*24*3600);  /* 1 week cookie expiration */
  628.   if(function_exists("mb_internal_encoding")&&function_exists("mb_convert_encoding")
  629.       &&function_exists("mb_substr")){
  630.     if(preg_match("/MSIE|Opera/",$_SERVER["HTTP_USER_AGENT"])){
  631.       $i=0;$c_name='';
  632.       mb_internal_encoding("SJIS");
  633.       while($j=mb_substr($names,$i,1)){
  634.         $j = mb_convert_encoding($j, "UTF-16", "SJIS");
  635.         $c_name.="%u".bin2hex($j);
  636.         $i++;
  637.       }
  638.       header("Set-Cookie: namec=$c_name; expires=".gmdate("D, d-M-Y H:i:s",time()+7*24*3600)." GMT",false);
  639.     }else{
  640.       $c_name=$names;
  641.       setcookie ("namec", $c_name,time()+7*24*3600);  /* 1 week cookie expiration */
  642.     }
  643.   }
  644.  
  645.   if($dest&&file_exists($dest)){
  646.     rename($dest,$path.$tim.$ext);
  647.     if(USE_THUMB && $isimage){thumb($path,$tim,$ext);}
  648.   }
  649.   updatelog();
  650.  
  651.   echo "<html><head><meta http-equiv=\"refresh\" content=\"1;URL=".PHP_SELF2."\" /></head>";
  652.   echo "<body>$mes ".S_SCRCHANGE."</body></html>";
  653. }
  654.  
  655. //thumbnails
  656. function thumb($path,$tim,$ext){
  657.   if(!function_exists("ImageCreate")||!function_exists("ImageCreateFromJPEG"))return;
  658.   $fname=$path.$tim.$ext;
  659.   $thumb_dir = THUMB_DIR;     //thumbnail directory
  660.   $width     = MAX_W;            //output width
  661.   $height    = MAX_H;            //output height
  662.   // width, height, and type are aquired
  663.   $size = GetImageSize($fname);
  664.   switch ($size[2]) {
  665.     case 1 :
  666.       if(function_exists("ImageCreateFromGIF")){
  667.         $im_in = @ImageCreateFromGIF($fname);
  668.         if($im_in){break;}
  669.       }
  670.       if(!is_executable(realpath("./gif2png"))||!function_exists("ImageCreateFromPNG"))return;
  671.       @exec(realpath("./gif2png")." $fname",$a);
  672.       if(!file_exists($path.$tim.'.png'))return;
  673.       $im_in = @ImageCreateFromPNG($path.$tim.'.png');
  674.       unlink($path.$tim.'.png');
  675.       if(!$im_in)return;
  676.       break;
  677.     case 2 : $im_in = @ImageCreateFromJPEG($fname);
  678.       if(!$im_in){return;}
  679.        break;
  680.     case 3 :
  681.       if(!function_exists("ImageCreateFromPNG"))return;
  682.       $im_in = @ImageCreateFromPNG($fname);
  683.       if(!$im_in){return;}
  684.       break;
  685.     default : return;
  686.   }
  687.   // Resizing
  688.   if ($size[0] > $width || $size[1] >$height) {
  689.     $key_w = $width / $size[0];
  690.     $key_h = $height / $size[1];
  691.     ($key_w < $key_h) ? $keys = $key_w : $keys = $key_h;
  692.     $out_w = ceil($size[0] * $keys) +1;
  693.     $out_h = ceil($size[1] * $keys) +1;
  694.   } else {
  695.     $out_w = $size[0];
  696.     $out_h = $size[1];
  697.   }
  698.   // the thumbnail is created
  699.   if(function_exists("ImageCreateTrueColor")&&get_gd_ver()=="2"){
  700.     $im_out = ImageCreateTrueColor($out_w, $out_h);
  701.   }else{$im_out = ImageCreate($out_w, $out_h);}
  702.   // copy resized original
  703.   ImageCopyResized($im_out, $im_in, 0, 0, 0, 0, $out_w, $out_h, $size[0], $size[1]);
  704.   // thumbnail saved
  705.   ImageJPEG($im_out, $thumb_dir.$tim.'s.jpg',60);
  706.   chmod($thumb_dir.$tim.'s.jpg',0666);
  707.   // created image is destroyed
  708.   ImageDestroy($im_in);
  709.   ImageDestroy($im_out);
  710. }
  711. //check version of gd
  712. function get_gd_ver(){
  713.   if(function_exists("gd_info")){
  714.     $gdver=gd_info();
  715.     $phpinfo=$gdver["GD Version"];
  716.   }else{ //earlier than php4.3.0
  717.     ob_start();
  718.     phpinfo(8);
  719.     $phpinfo=ob_get_contents();
  720.     ob_end_clean();
  721.     $phpinfo=strip_tags($phpinfo);
  722.     $phpinfo=stristr($phpinfo,"gd version");
  723.     $phpinfo=stristr($phpinfo,"version");
  724.   }
  725.   $end=strpos($phpinfo,".");
  726.   $phpinfo=substr($phpinfo,0,$end);
  727.   $length = strlen($phpinfo)-1;
  728.   $phpinfo=substr($phpinfo,$length);
  729.   return $phpinfo;
  730. }
  731. //md5 calculation for earlier than php4.2.0
  732. function md5_of_file($inFile) {
  733.  if (file_exists($inFile)){
  734.   if(function_exists('md5_file')){
  735.     return md5_file($inFile);
  736.   }else{
  737.     $fd = fopen($inFile, 'r');
  738.     $fileContents = fread($fd, filesize($inFile));
  739.     fclose ($fd);
  740.     return md5($fileContents);
  741.   }
  742.  }else{
  743.   return false;
  744. }}
  745. /* text plastic surgery */
  746. function CleanStr($str){
  747.   global $admin;
  748.   $str = trim($str);//blankspace removal
  749.   if (get_magic_quotes_gpc()) {//magic quotes is deleted (?)
  750.     $str = stripslashes($str);
  751.   }
  752.   if($admin!=ADMIN_PASS){//admins can use tags
  753.     $str = htmlspecialchars($str);//remove html special chars
  754.     $str = str_replace("&amp;", "&", $str);//remove ampersands
  755.   }
  756.   return str_replace(",", "&#44;", $str);//remove commas
  757. }
  758.  
  759. //check for table existance
  760. function table_exist($link, $table){
  761.   $result = mysql_call($link, "show tables like '$table'");
  762.   if(!$result){return 0;}
  763.   $a = mysqli_fetch_row($result);
  764.   mysqli_free_result($result);
  765.   return $a;
  766. }
  767.  
  768. /* user image deletion */
  769. function usrdel($no,$pwd){
  770.   global $path,$pwdc,$onlyimgdel;
  771.   global $con;
  772.   $host = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
  773.   $delno = array();
  774.   $delflag = FALSE;
  775.   reset($_POST);
  776.   while ($item = each($_POST)){
  777.     if($item[1]=='delete'){array_push($delno,$item[0]);$delflag=TRUE;}
  778.   }
  779.   if($pwd==""&&$pwdc!="") $pwd=$pwdc;
  780.   $countdel=count($delno);
  781.  
  782.   $flag = FALSE;
  783.   for($i = 0; $i<$countdel; $i++){
  784.     if(!$result=mysql_call($con, "select no,ext,tim,pwd,host from ".SQLLOG." where no=".$delno[$i])){echo S_SQLFAIL;}
  785.     else{
  786.       while($resrow=mysqli_fetch_row($result)){
  787.         list($dno,$dext,$dtim,$dpass,$dhost)=$resrow;
  788.         if(substr(md5($pwd),2,8) == $dpass || substr(md5($pwdc),2,8) == $dpass ||
  789.             $dhost == $host || ADMIN_PASS==$pwd){
  790.           $flag = TRUE;
  791.           $delfile = $path.$dtim.$dext; //path to delete
  792.           if(!$onlyimgdel){
  793.             if(!mysql_call($con, "delete from ".SQLLOG." where no=".$dno)){echo S_SQLFAIL;} //sql is broke
  794.           }
  795.           if(is_file($delfile)) unlink($delfile);//Deletion
  796.           if(is_file(THUMB_DIR.$dtim.'s.jpg')) unlink(THUMB_DIR.$dtim.'s.jpg');//Deletion
  797.         }
  798.       }
  799.       mysqli_free_result($result);
  800.     }
  801.   }
  802.   if(!$flag) error(S_BADDELPASS);
  803. }
  804.  
  805. /*password validation */
  806. function valid($pass){
  807.   if($pass && $pass != ADMIN_PASS) error(S_WRONGPASS);
  808.  
  809.   head($dat);
  810.   echo $dat;
  811.   echo "[<a href=\"".PHP_SELF2."\">".S_RETURNS."</a>]\n";
  812.   echo "[<a href=\"".PHP_SELF."\">".S_LOGUPD."</a>]\n";
  813.   echo "<div class=\"passvalid\">".S_MANAMODE."</div>\n";
  814.   echo "<p><form action=\"".PHP_SELF."\" method=\"post\">\n";
  815.   // Mana login form
  816.   if(!$pass){
  817.     echo "<div class=\passvalid\"><input type=radio name=admin value=del checked>".S_MANAREPDEL;
  818.     echo "<input type=radio name=admin value=post>".S_MANAPOST."<p>";
  819.     echo "<input type=hidden name=mode value=admin>\n";
  820.     echo "<input type=password name=pass size=8>";
  821.     echo "<input type=submit value=\"".S_MANASUB."\"></form></div>\n";
  822.     die("</body></html>");
  823.   }
  824. }
  825.  
  826. /* Admin deletion */
  827. function admindel($pass){
  828.   global $path,$onlyimgdel;
  829.   global $con;
  830.   $delno = array(dummy);
  831.   $delflag = FALSE;
  832.   reset($_POST);
  833.   while ($item = each($_POST)){
  834.    if($item[1]=='delete'){array_push($delno,$item[0]);$delflag=TRUE;}
  835.   }
  836.   if($delflag){
  837.     if(!$result=mysql_call($con, "select * from ".SQLLOG."")){echo S_SQLFAIL;}
  838.     $find = FALSE;
  839.     while($row=mysqli_fetch_row($result)){
  840.       list($no,$now,$name,$email,$sub,$com,$host,$pwd,$ext,$w,$h,$tim,$time,$md5,$fsize,)=$row;
  841.       if($onlyimgdel==on){
  842.         if(array_search($no,$delno)){//only a picture is deleted
  843.           $delfile = $path.$tim.$ext;   //only a picture is deleted
  844.           if(is_file($delfile)) unlink($delfile);//delete
  845.           if(is_file(THUMB_DIR.$tim.'s.jpg')) unlink(THUMB_DIR.$tim.'s.jpg');//delete
  846.         }
  847.       }else{
  848.         if(array_search($no,$delno)){//It is empty when deleting
  849.           $find = TRUE;
  850.           if(!mysql_call($con, "delete from ".SQLLOG." where no=".$no)){echo S_SQLFAIL;}
  851.           $delfile = $path.$tim.$ext;   //Delete file
  852.           if(is_file($delfile)) unlink($delfile);//Delete
  853.           if(is_file(THUMB_DIR.$tim.'s.jpg')) unlink(THUMB_DIR.$tim.'s.jpg');//Delete
  854.         }
  855.       }
  856.     }
  857.     mysqli_free_result($result);
  858.     if($find){//log renewal
  859.     }
  860.   }
  861.   // Deletion screen display
  862.   echo "<input type=hidden name=mode value=admin>\n";
  863.   echo "<input type=hidden name=admin value=del>\n";
  864.   echo "<input type=hidden name=pass value=\"$pass\">\n";
  865.   echo "<div class=\"dellist\">".S_DELLIST."</div>\n";
  866.   echo "<div class=\"delbuttons\"><input type=submit value=\"".S_ITDELETES."\">";
  867.   echo "<input type=reset value=\"".S_MDRESET."\">";
  868.   echo "[<input type=checkbox name=onlyimgdel value=on><!--checked-->".S_MDONLYPIC."]</div>";
  869.   echo "<table class=\"postlists\">\n";
  870.   echo "<tr class=\"managehead\">".S_MDTABLE1;
  871.   echo S_MDTABLE2;
  872.   echo "</tr>\n";
  873.  
  874.   if(!$result=mysql_call($con, "select * from ".SQLLOG." order by no desc")){echo S_SQLFAIL;}
  875.   $j=0;
  876.   while($row=mysqli_fetch_row($result)){
  877.     $j++;
  878.     $img_flag = FALSE;
  879.     list($no,$now,$name,$email,$sub,$com,$host,$pwd,$ext,$w,$h,$tim,$time,$md5,$fsize,$root,$resto)=$row;
  880.     // Format
  881.     $now=preg_replace('/.{2}/(.*)$/','\1',$now);
  882.     $now=preg_replace('/\(.*\)/',' ',$now);
  883.     if(strlen($name) > 10) $name = substr($name,0,9).".";
  884.     if(strlen($sub) > 10) $sub = substr($sub,0,9).".";
  885.     if($email) $name="<a href=\"mailto:$email\">$name</a>";
  886.     $com = str_replace("<br />"," ",$com);
  887.     $com = htmlspecialchars($com);
  888.     if(strlen($com) > 20) $com = substr($com,0,18) . ".";
  889.     // Link to the picture
  890.     if($ext && is_file($path.$tim.$ext)){
  891.       $img_flag = TRUE;
  892.       $clip = "<a href=\"".IMG_DIR.$tim.$ext."\" target=\"_blank\">".$tim.$ext."</a><br />";
  893.       $size = $fsize;
  894.       $all += $size;            //total calculation
  895.       $md5= substr($md5,0,10);
  896.     }else{
  897.       $clip = "";
  898.       $size = 0;
  899.       $md5= "";
  900.     }
  901.     $class = ($j % 2) ? "row1" : "row2";//BG color
  902.  
  903.     echo "<tr class=$class><td><input type=checkbox name=\"$no\" value=delete></td>";
  904.     echo "<td>$no</td><td>$now</td><td>$sub</td>";
  905.     echo "<td>$name</b></td><td>$com</td>";
  906.     echo "<td>$host</td><td>$clip($size)</td><td>$md5</td><td>$resto</td><td>$tim</td><td>$time</td>\n";
  907.     echo "</tr>\n";
  908.   }
  909.   mysqli_free_result($result);
  910.  
  911.   echo "</table><input type=submit value=\"".S_ITDELETES."$msg\">";
  912.   echo "<input type=reset value=\"".S_RESET."\"></form>";
  913.  
  914.   $all = (int)($all / 1024);
  915.   echo "[ ".S_IMGSPACEUSAGE.$all."</b> KB ]";
  916.   die("</body></html>");
  917. }
  918.  
  919. /*-----------Main-------------*/
  920. switch($mode){
  921.   case 'regist':
  922.     regist($name,$email,$sub,$com,'',$pwd,$upfile,$upfile_name,$resto);
  923.     break;
  924.   case 'admin':
  925.     valid($pass);
  926.     if($admin=="del") admindel($pass);
  927.     if($admin=="post"){
  928.       echo "</form>";
  929.       form($post,$res,1);
  930.       echo $post;
  931.       die("</body></html>");
  932.     }
  933.     break;
  934.   case 'usrdel':
  935.     usrdel($no,$pwd);
  936.   default:
  937.     if($res){
  938.       updatelog($res);
  939.     }else{
  940.       updatelog();
  941.       echo "<meta http-equiv=\"refresh\" content=\"0;URL=".PHP_SELF2."\" />";
  942.     }
  943. }
  944.  
  945. ?>
Advertisement
Add Comment
Please, Sign In to add comment