- 积分
- 10664
- UID
- 5
- 在线时间
- 小时
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2004-11-23 22:49:15
|
显示全部楼层
二楼签名档的源码
- <?php
- $version = "1.10";
- // setistats XML/GIF banner
- // written by Multi-Dimensional Visual Echo (Roger C. Venable), [url]http://www.visualecho.org[/url]
- // Licensed under LGPL, see [url]http://www.gnu.org/copyleft/lesser.html[/url]
- //
- // Features:
- // * XML fetch from SETI is proxyable
- // * caches SETI XML to file so the SETI's XML feed is not overused
- // * recovers well from unavailable XML data
- // * usable in Linux or Windows PHP enabled web sites
- // * uses a minimum of external/additional components
- //
- // Installation:
- // To use, you need the 'curl' and Imagemagick 'convert' commands:
- // See [url]http://curl.haxx.se/[/url] and [url]http://www.imagemagick.com/[/url]
- //
- // CHANGE all the lines marked with *** CHANGE THIS ***
- // CHECK all the lines marked with *** CHECK THIS ***
- //
- // This micro-engine can easily be modified to use other background
- // graphics and other XML feeds (i.e. Planetside). Enjoy!
- //
- $debug = false; // *** CHECK THIS ***
- $scrap_dir = "/var/www/tmp/"; // *** CHANGE THIS ***
- $refresh_age = 86400; // twenty-four hours (DON'T SPAM SETI!)
- // this is the Imagemagick convert command, see [url]http://www.imagemagick.com[/url]
- $command = "/usr/bin/convert"; // *** CHANGE THIS ***
- // Usable system font directory
- $fontdir = "/usr/share/fonts/"; // *** CHANGE THIS ***
- $default_sysfont = "arial.ttf"; // *** CHANGE THIS ***
- // this is the base URL for SETI XML data
- $urlbase = "http://setiathome2.ssl.berkeley.edu/fcgi-bin/fcgi".'?'."cmd=user_xml".'&'."email=";
- // make proxy empty ("") to disable
- $proxy = ""; // *** CHANGE THIS ***
- // this is the curl command, See [url]http://curl.haxx.se/[/url]
- $curlcmd = "/usr/bin/curl"; // *** CHANGE THIS ***
- // this is the background graphic
- $infile = $scrap_dir."seti_template.gif"; // *** CHECK THIS ***
-
- $email = $_GET['email'];
- if ($email == '')
- exit(0);
- // clean
- $email = substr(urlencode( $email ), 0, 255); // change if your email address is > 255
- $email = strip_tags( $email );
- $url = $urlbase.$email;
- // this is the XML file which will be updated if old
- $xmlfile = $scrap_dir."setistats_".str_replace("%", "Z", $email).".xml"; // *** CHECK THIS ***
- // files which will be deleted on next access after $refresh_age seconds
- $globber1 = $scrap_dir."setistats_*.xml"; // *** CHECK THIS ***
- // this is the output file
- $outfile = $scrap_dir."setistatgif_".str_replace("%", "Z", $email).".gif"; // *** CHECK THIS ***
- // files which will be deleted on next access after $refresh_age seconds
- $globber2 = $scrap_dir."setistatgif_*.gif"; // *** CHECK THIS ***
- if (!function_exists("file_get_contents")) {
- function file_get_contents($filename, $use_include_path = 0) {
- $data = ""; // just to be safe. Dunno, if this is really needed
- $file = @fopen($filename, "rb", $use_include_path);
- if ($file) {
- while (!feof($file)) $data .= fread($file, 1024);
- fclose($file);
- }
- return $data;
- }
- }
- if ($debug)
- {
- header("Content-type: text/html\n\n");
- print "setistats.php version ".$version."<BR>";
- print "Images will be refreshed after ".$refresh_age." seconds<BR>";
- print "Using convert command: ".$command."<BR>";
- print "Using background image: ".$infile."<BR>";
- print "Using fonts in directory: ".$fontdir."<BR>";
- }
- else
- header("Content-type: image/gif\n\n");
- if ($debug)
- {
- print "Using XML file: ".$xmlfile."<BR>";
- if (file_exists($xmlfile))
- print " File mod time: ".filemtime($xmlfile)." (".(time() - filemtime($xmlfile))." seconds old), will refresh in: ".(filemtime($xmlfile) + $refresh_age - time())." seconds<BR>";
- else
- print " File does not exist, now: ".time()."<BR>";
- }
- if (!file_exists($xmlfile) || ((filemtime($xmlfile) + $refresh_age) <= time()))
- {
- if (file_exists($xmlfile))
- unlink($xmlfile);
- if ( $proxy )
- $cmd = $curlcmd." -x ".$proxy." -o ".$xmlfile." "".$url.""";
- else
- $cmd = $curlcmd." -o ".$xmlfile." "".$url.""";
-
- if ($debug)
- print "Fetch command: ( $cmd )<BR>";
- exec ($cmd,$result,$value);
- if ($debug)
- print "Should have created file: ".$xmlfile."<BR>";
- }
- if ($debug)
- {
- print "Using output file: ".$outfile."<BR>";
- if (file_exists($outfile))
- print " File mod time: ".filemtime($outfile)." (".(time() - filemtime($outfile))." seconds old), will refresh in: ".(filemtime($outfile) + $refresh_age - time())." seconds<BR>";
- else
- print " File does not exist, now: ".time()."<BR>";
- }
- $kill_bad_gif = 0;
- if (!file_exists($outfile) || ((filemtime($outfile) + $refresh_age) <= time()))
- {
- if (file_exists($outfile))
- unlink($outfile);
- $fillcolor = "white";
- $last_color = "";
- $last_pointsize = "";
- $last_font = "";
-
- $txt = "";
- $last_data = "";
- $use_next_a = 1;
- $elements = array(
- "numresults" => array("format" => "%s", "font" => $default_sysfont, "pointsize" => "18", "color" => $fillcolor,
- "x" => "150", "y" => "29", "func" => "intformat"),
- "a" => array("format" => "%s", "font" => $default_sysfont, "pointsize" => "12", "color" => "yellow",
- "x" => "150", "y" => "12", "func" => "use_only_first_a"),
- "resultsperday" => array("format" => "%s Results per day", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "280", "y" => "31", "func" => ""),
- "lastresulttime" => array("format" => "Last Result: %s GMT", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "280", "y" => "40", "func" => ""),
- "num_samerank" => array("format" => "Results Processed", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "150", "y" => "40", "func" => ""),
- "name" => array("format" => "%s", "font" => $default_sysfont, "pointsize" => "12", "color" => "yellow",
- "x" => "150", "y" => "12", "func" => "got_name"),
- "avecpu" => "0",
- "usertime" => array("format" => "%s user time", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "280", "y" => "22", "func" => ""),
- "regdate" => "0",
- "rank" => "0",
- "ranktotalusers" => "0",
- "cputime" => array("format" => "%s CPU", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "400", "y" => "22", "func" => ""),
- "top_rankpct" => array("format" => "Top Rank %s", "font" => $default_sysfont, "pointsize" => "10", "color" => $fillcolor,
- "x" => "400", "y" => "31", "func" => "neg_percent")
- );
-
- $data = file_get_contents($xmlfile) or ($data = "x0YgnRp");
- if ($data == "x0YgnRp")
- {
- if ($debug)
- print "<B>Error reading XML data</B><BR>";
- $txt = " -pointsize 32 -font ".$fontdir.$default_sysfont." -draw "text 170,25 'Error'" -pointsize 10";
- $txt = "$txt -font ".$fontdir.$default_sysfont." -draw "text 170,35 'XML DATA UNAVAILABLE'"";
- }
- else if ($data !== false)
- {
- if ($debug)
- print "Got ".strlen($data)." bytes of XML data from $xmlfile<BR>";
- $xml_parser = xml_parser_create();
- xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
- xml_set_element_handler($xml_parser, "startElement", "endElement");
- xml_set_character_data_handler($xml_parser, "characterData");
-
- if (!xml_parse($xml_parser, $data, TRUE))
- {
- $error = sprintf("XML error at line %d: %s", xml_get_current_line_number($xml_parser),
- xml_error_string(xml_get_error_code($xml_parser)));
- if ($debug)
- {
- print "<B>Error parsing XML data: $error</B><BR>";
- }
- if ( $error == "XML error at line 4: syntax error" )
- $error = "XML feed from SETI@home is temporarily down or disabled";
- if ( $error == "XML error at line 4: junk after document element" )
- $error = "XML feed from SETI@home is temporarily down or disabled";
- unlink($xmlfile);
- $kill_bad_gif = 1;
- $txt = " -pointsize 32 -font ".$fontdir.$default_sysfont." -draw "text 170,25 'Error'" -pointsize 10";
- $txt = "$txt -font ".$fontdir.$default_sysfont." -draw "text 170,35 '$error'"";
- }
-
- xml_parser_free($xml_parser);
- }
-
- $command = $command . " -antialias ";
- $command = $command . $txt . " " . $infile . " " . $outfile;
-
- if ($debug)
- print "Command: ".$command."<BR>";
- exec ($command,$result,$value);
- if ($debug)
- print "Should have created file: ".$outfile."<BR>";
- }
-
- if (!$debug)
- {
- $img = file_get_contents($outfile);
- echo $img;
- }
- if ( $kill_bad_gif > 0 )
- unlink($outfile);
- // wipe old files
- foreach (glob($globber1) as $outfile)
- {
- if ((filemtime($outfile) + $refresh_age) <= time())
- {
- if ($debug)
- print "Destroying old file: ".$outfile."<BR>";
- unlink($outfile);
- }
- }
- foreach (glob($globber2) as $outfile)
- {
- if ((filemtime($outfile) + $refresh_age) <= time())
- {
- if ($debug)
- print "Destroying old file: ".$outfile."<BR>";
- unlink($outfile);
- }
- }
- exit();
- function startElement($parser, $name, $attrs)
- {
- }
- function endElement($parser, $name)
- {
- global $last_data, $elements, $txt, $fontdir, $debug, $last_pointsize, $last_font, $last_color;
- $last_data = ltrim($last_data);
- if ( $last_data && is_array($elements[$name]) )
- {
- if ($elements[$name]["func"] != "" && function_exists($elements[$name]["func"]))
- {
- $func = $elements[$name]["func"];
- $last_data = $func($last_data);
- }
- if ($debug)
- print "Parsed XML element: ".$name." = ".$last_data."<BR>";
- if ( $last_data )
- {
- $element = sprintf( $elements[$name]["format"], $last_data );
- if ( $elements[$name]["pointsize"] != $last_pointsize )
- $txt = "$txt -pointsize ".$elements[$name]["pointsize"];
- $last_pointsize = $elements[$name]["pointsize"];
- if ( $elements[$name]["font"] != $last_font )
- $txt = "$txt -font ".$fontdir.$elements[$name]["font"];
- $last_font = $elements[$name]["font"];
- if ( $elements[$name]["color"] != $last_color )
- $txt = "$txt -fill ".$elements[$name]["color"];
- $last_color = $elements[$name]["color"];
- $txt = "$txt -draw "text ";
- $txt = $txt.$elements[$name]["x"].",".$elements[$name]["y"]." '$element'"";
- }
- }
- $last_data = "";
- }
- function characterData($parser, $data)
- {
- global $last_data;
- if ( trim($data) != "" )
- $last_data = $data;
- }
- function use_only_first_a($data)
- {
- global $use_next_a;
- if ( $use_next_a > 0 )
- {
- $use_next_a = 0;
- return( $data );
- }
- else
- return( "" );
- }
- function got_name($data)
- {
- global $use_next_a;
- $use_next_a = 0;
- return( $data );
- }
- function intformat($data)
- {
- return( number_format($data) );
- }
- function neg_percent($data)
- {
- return( strval( 100.0 - floatval($data) )."%%" );
- }
- ?>
复制代码 |
|