snakevil
(snakevil)
中级会员
 
中级会员
UID 27209
精华
3
积分 261
帖子 201
金钱 231 喜悦币
威望 30
人脉 0
阅读权限 30
注册 2004-3-24
状态 离线
|
闲时写得一个 Ftp 状态程序
校内 FTP 状态查询页面
使用 GD2 生成 PNG 图片
使用 ICONV 以在图片中显示中文
使用缓存技术防止被 FTP 服务器 BAN 掉
<?
/*
* ::
* base definement
* ::
*/
// remote ftp server (use FTP for short below) ip
define("cHost", "10.1.206.86");
// FTP port, required!
define("cPort", 21);
// FTP monitor persisting time, required!
define("cBanMnt", 30);
// FTP allowd connect times
define("cBanTms", 10);
// cache filename
define("cCacheFile", "ftpstats.png");
// source image filename
define("cSrcFile", "back.jpg");
// blank pixels number between each 2 lines
define("cLineSpc", 5);
// notice:
// until the current moment, all definements were trusted from the program
// in this way, if one or more difinements were given with error values
// program would destroyed.
// so, in the future versions, we need to improve this section.
/*
* **
* function to write a text in a line with different colors, fonts and sizes
*
* syntax:
* imageWriteLine(<GD_stream_object>, <offset_x>, <offset_y>,
* <text_desribe_array> [, <text_desribe_array> [, ...]]);
*
* information:
* GD_stream_object : the GD stream which the text would be drawn
* offset_x : text block left border offset from the left border of the
* stream image when the text be thought as a color block or a layer
* offset_y : text block top border offset from the top border of the
* stream image when the text be thought as a color block or a layer
* text_describe_array : a array include text infomation, should be listed
* as font, size, color, content, shadowd, shadowcolor in order
*
* tip:
* you can put many text_describe_array in the parameters zone, but there
* should be 1 at least.
* with a pleasure in visual, all text_array would be displayed base the
* same pixel line.
* to be the return values, the status would be the first item in the array,
* and if its done, the text block width and height would be the second
* and the third items.
* **
*/
function imageWriteLine() {
$func_args = func_get_args();
$temp = count($func_args);
//verify the parameters
//quiz : how to check the first parameter whether is a gd stream image object?
if ($temp < 4 || !is_int($func_args[1]) || !is_int($func_args[2])) {
print('error_1');
unset($func_args, $temp);
return(array(FALSE));
}
for ($i = 3; $i < $temp; $i++) {
if(!is_array($func_args[$i]) || (count($func_args[$i]) != 4 && count($func_args[$i]) != 6) || !is_string($func_args[$i][0]) || !file_exists($func_args[$i][0]) || !is_int($func_args[$i][1]) || $func_args[$i][1] < 1 || !is_string($func_args[$i][3]) || (count($func_args[$i])== 6 && !is_bool($func_args[$i][4]))) {
print("error_2_$i");
unset($func_args, $temp, $i);
return(array(FALSE));
}
}
$img = array_shift($func_args);
$off_x = array_shift($func_args);
$off_y = array_shift($func_args);
$temp -= 3;
for ($i = 0; $i < $temp; $i++) {
// save font serials in fonts
$fonts[] = $func_args[$i][0];
// save size serials in sizes
$sizes[] = $func_args[$i][1];
// save color serials in colors
$colors[] = $func_args[$i][2];
// save content serials in contents
$contents[] = $func_args[$i][3];
// save shadowd serials in shadowd
$shadowds[] = $func_args[$i][4] ? TRUE : FALSE;
// save color2 serials in color2s
$color2s[] = $func_args[$i][4] ? $func_args[$i][5] : '';
// save width serials in width and height serials in heights
list($x1, $y2, $x2, , , $y1, , ) = imageTTFbBox($func_args[$i][1], 0, $func_args[$i][0], $func_args[$i][3]);
$widths[] = $x2 - $x1;
$heights[] = $y2 - $y1;
}
unset($func_args, $x1, $x2, $y1, $y2);
// get true y offset
$rslt = array(TRUE, array_sum($widths), max($heights));
unset($heights);
$off_y += $rslt[2];
// draw text
for ($i = 0; $i < $temp; $i++) {
// if need shadowed, draw shadow
if ($shadowds[$i]) imageTTFText($img, $sizes[$i], 0, ($off_x - 1), ($off_y - 1), $color2s[$i], $fonts[$i], $contents[$i]);
imageTTFText($img, $sizes[$i], 0, $off_x, $off_y, $colors[$i], $fonts[$i], $contents[$i]);
$off_x += $widths[$i];
}
// return reports
unset($img, $off_x, $off_y, $temp, $i, $fonts, $sizes, $colors, $contents, $widths);
return($rslt);
} /*
* **
* function to convert a string based on ?????? to unicode
*
* copyrights
* wroten by 6spring @ BiT.UNioN
* free for personal usage
* all rights reserved.
* **
*/
function ??????2uni($text) {
// return blank if parameter missed
if (!$slen = strlen($text)) return("");
$rslt = $tmp = "";
for ($i = 0; $i < $slen; $i++) {
$casc = ord($text[$i]);
if ($casc >= 160 && $i < $slen - 1) {
// tip : does your apache tell you that undefined function iconv() ?
// coz, you need to load the php_iconv.dll module in php.ini
// sounds terribly, you have to copy the iconv.dll from Dlls directory
// to system directory such as c:winndowssystem32 too
$tmp = bin2hex(iconv("??????", "UTF-8", substr($text, $i, 2)));
$rslt .= chr(base_convert(substr($tmp, 0, 2), 16, 10));
$rslt .= chr(base_convert(substr($tmp, 2, 2), 16, 10));
$rslt .= chr(base_convert(substr($tmp, 4, 2), 16, 10));
$i++;
} else $rslt .= $text[$i];
}
unset($slen, $tmp, $i, $casc, $text);
return($rslt);
}
/*
* ::
* task serials
* ::
*/
// if no cache exist, create it
if (!file_exists(cCacheFile)) createCache();
// push the cached image to client
pushImage();
// if cache expired, update it
if (ceil(cBanMnt / (cBanTms - 1)) + fileMTime(cCacheFile) + 1 < time()) {
// delete old expired cache
unlink(cCacheFile);
// create new cache
createCache();
}
/*
* ::
* function to push cached image to client
*
* personal usage in this file
* ::
*/
function pushImage() {
// use location direction for fewer treatments
header("Location: ".cCacheFile);
/*
// use a gd image sream for resist truly cache file from end user
$img = imageCreateFromPNG(cCacheFile);
imagePNG($img);
imageDestroy($img);
*/
} /*
* ::
* function to create a cache image file
*
* personal usage in this file
* ::
*/
function createCache() {
// create a gd image stream for source image file
list($img_w, $img_h, , ) = getImageSize(cSrcFile);
$img = imageCreateFromJPEG(cSrcFile);
// quiz : when source image file couldnt be recognized, what shall we do?
// report error? or just stop the program?
// add a black border for graphic stream
imageRectangle($img, 0, 0, --$img_w, --$img_h, imageColorAllocate($img, 0, 0, 0));
// simple socket connection for ftp info
$fsc = @fSockOpen(cHost, cPort, $ftp_e_no, $ftp_e_str, 1);
if ($ftp_e_no) {
// treat with the case which FTP is down
$ftp_stats = ??????2uni('服务器 维 护 检 修 中');
list($text_x1, $text_y2, $text_x2, , , $text_y1, , ) = imageTTFbBox(16, 0, 'c.ttf', $ftp_stats);
$text_x2 = intval(465 - ($text_x2 - $text_x1) / 2);
$text_y2 = intval(145 + ($text_y2 - $text_y1) / 2);
imageTTFText($img, 16, 0, ($text_x2 + 1), ($text_y2 + 1), imageColorAllocate($img, 0, 0, 0), 'c.ttf', $ftp_stats);
imageTTFText($img, 16, 0, $text_x2, $text_y2, imageColorAllocate($img, 255, 0, 0), 'c.ttf', $ftp_stats);
} else {
// ftp works ^^
// collect ftp info
socket_set_timeOut($fsc, 1);
$ftp_info = "";
do {
$ftp_info .= fRead($fsc, 4096);
} while (!strpos($ftp_info, 'Enjoy~~'));
fClose($fsc);
// treat with the FTP info
$ftp_info = preg_replace('/n/', '', $ftp_info);
// FTP downloaded info
list($ftp_d_data, $ftp_d_file) = split(',', preg_replace('/.+220- (d+) KB data in (d+) files been downloaded.+/', '$1,$2', $ftp_info));
// FTP uploaded info
list($ftp_u_data, $ftp_u_file) = split(',', preg_replace('/.+220- (d+) KB data in (d+) files been uploaded.+/', '$1,$2', $ftp_info));
// FTP speed info
list($ftp_s_avr, $ftp_s_cur) = split(',', preg_replace('/.+220- (d+.d)d{2} KBps speed averaged, current is (d+.d)d{2} KBps.+/', '$1,$2', $ftp_info));
// FTP connects info
list($ftp_c_usr, $ftp_c_log, $ftp_c_day) = split(',', preg_replace('/.+220- (d+) usrs connectd, (d+) logind, and (d+) last 24 hours.+/', '$1,$2,$3', $ftp_info));
// FTP current users info
list($ftp_n_usr, $ftp_a_usr) = split(',', preg_replace('/.+220- (d+) named and (d+) anonymous usrs connectd right ?????+/', '$1,$2', $ftp_info));
unset($ftp_info);
// ready for serialize info output
$text_x0 = 345;
$text_x_ = 585;
$text_y0 = 105;
$text_y_ = 185;
$color_static = imageColorAllocate($img, 51, 102, 153);
$color_static_ = imageColorAllocate($img, 0, 17, 34);
$color_var = imageColorAllocate($img, 51, 153, 102);
$color_var_ = imageColorAllocate($img, 0, 34, 17);
// get FTP status and flush out
if ($ftp_s_cur <= 500) $ftp_stats = '活 力 四 射';
elseif ($ftp_s_cur <= 1000) $ftp_stats = '努 力 工 作';
elseif ($ftp_s_cur <= 1500) $ftp_stats = '疲 劳 不 堪';
elseif ($ftp_s_cur <= 2000) $ftp_stats = '苟 延 残 喘';
else $ftp_stats = '生 命 垂 危';
$ftp_stats = ??????2uni('━┥ '.$ftp_stats.' ┝━');
$font = 'c.ttf';
list($text_x1, $text_y2, $text_x2, , , $text_y1, , ) = imageTTFbBox(13, 0, $font, $ftp_stats);
$text_x = intval(($text_x0 + $text_x_) / 2 - ($text_x2 - $text_x1) / 2);
$text_y = $text_y0 + $text_y2 - $text_y1;
imageTTFText($img, 13, 0, ($text_x - 1), ($text_y - 1), imageColorAllocate($img, 0, 0, 0), $font, $ftp_stats);
imageTTFText($img, 13, 0, $text_x, $text_y, imageColorAllocate($img, 153, 153, 153), $font, $ftp_stats);
$text_y0 = $text_y + cLineSpc * 2;
$fontb = 'e_b.ttf';
$fonti = 'ei_.ttf';
$font = 'e__.ttf';
// flush out connects
list(, , $text_y) = imageWriteLine($img, $text_x0, $text_y0,
array($fontb, 10, $color_static, 'Connects : ', TRUE, $color_static_),
array($font, 8, $color_var, $ftp_n_usr, TRUE, $color_var_),
array($fonti, 10, $color_static, ' of ', TRUE, $color_static_),
array($font, 8, $color_var, $ftp_c_usr, TRUE, $color_var_),
array($fonti, 10, $color_static, ' usrs', TRUE, $color_static_));
$text_y0 += $text_y + cLineSpc;
// flush out speed
list(, , $text_y) = imageWriteLine($img, $text_x0, $text_y0,
array($fontb, 10, $color_static, 'Speed : ', TRUE, $color_static_),
array($font, 8, $color_var, $ftp_s_cur, TRUE, $color_var_),
array($fonti, 10, $color_static, ' KBps ( ', TRUE, $color_static_),
array($font, 8, $color_var, $ftp_s_avr, TRUE, $color_var_),
array($fonti, 10, $color_static, ' avr.)', TRUE, $color_static_));
$text_y0 += $text_y + cLineSpc + 3;
// flush out downloaded
list(, , $text_y) = imageWriteLine($img, $text_x0, $text_y0,
array($fontb, 10, $color_static, 'Downloaded : ', TRUE, $color_static_),
array($font, 8, $color_var, preg_replace('/^(d+.d)d*$/', '$1', strval(max(0.1, $ftp_d_data / 1000000))), TRUE, $color_var_),
array($fonti, 10, $color_static, ' GB in ', TRUE, $color_static_),
array($font, 8, $color_var, $ftp_d_file, TRUE, $color_var_),
array($fonti, 10, $color_static, ' files', TRUE, $color_static_));
// free the mem that variables used
unset($ftp_d_data, $ftp_d_file, $ftp_u_data, $ftp_u_file, $ftp_s_avr, $ftp_s_cur, $ftp_c_usr, $ftp_c_log, $ftp_c_day, $ftp_n_usr, $ftp_a_usr);
unset($text_x0, $text_x_, $text_y0, $text_y_, $text_x, $text_y, $color_static, $color_static_, $color_var, $color_var_, $font, $fonti, $fontb);
}
// free the mem that variables used
unset($ftp_stats, $text_x1, $text_x2, $text_y1, $text_y2);
// write stream to file named cache image filename
imagePNG($img, cCacheFile);
imageDestroy($img);
}
?>
|
|