#!/usr/local/bin/perl # # Original script : xml2json.cgi by nitoyon # http://tech.nitoyon.com/javascript/xml2json.html # # Modified by Yasuhiro (Spiegel/Baldanders) ARAKAWA, 2006 # # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # ############################## use CGI; use Jcode; use LWP::UserAgent; use XML::Simple; use Digest::MD5 qw(md5_hex); use Data::Dumper; my $cache_dir = "./cache/"; #my $cache_lifetime = 0; # for debug my $cache_lifetime = 21600; # 60min my $q = new CGI; # param my $type = $q->param('type'); &error("type not specified") if !defined $type or $type!~/^[a-zA-Z_]+$/; my $cpid = $q->param('cpid'); $cpid = "" if !defined $cpid or $cpid!~/^[a-zA-Z0-9]+$/; my $var = $q->param('var'); $var = $type if !defined $var or $var!~/^[a-zA-Z_]+$/; my $url; if($type eq 'version' or $type eq 'client') { $url = "http://boinc.berkeley.edu/download.php?xml=1"; } elsif($type eq 'devclient') { $url = "http://boinc.berkeley.edu/download.php?dev=1&xml=1"; } elsif($type eq 'boincstat') { if($cpid eq "") { &error("cpid not specified"); } $url = "http://boinc.netsoft-online.com/get_user.php?cpid=".$cpid; } elsif($type eq 'boinc') { $url = "http://boinc.berkeley.edu/rss_main.php"; } elsif($type eq 'seti') { $url = "http://setiathome.berkeley.edu/rss_main.php"; } elsif($type eq 'einstein') { $url = "http://einstein.phys.uwm.edu/rss_main.php"; } else { &error("type not specified") } my $json; my $cache = $cache_dir.md5_hex($url); if(-f $cache && (stat($cache))[9] + $cache_lifetime > time){ open(CACHE, $cache); $json = join("", ); close(CACHE); } else{ # request my $ua = LWP::UserAgent->new; $ua->agent("XML2JSON/0.1 "); my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); &error("cannot get url: ".$url) if !$res->is_success; &error("cotnent is not xml (".$res->headers->header("Content-Type").")") if $res->headers->header("Content-Type")!~m!/xml!; # XML Dump $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $xmlobj = XMLin($res->content, KeepRoot => 1); $Data::Dumper::Indent = 1; $Data::Dumper::Useqq = "utf8"; $json = Dumper($xmlobj); # Write cache my $cache = $cache_dir.md5_hex($url); if(open(CACHE, ">$cache")){ print CACHE $json; close(CACHE); } } $json=~s/^\$VAR1/$var.data/; $json=~s/([^\\])" => ("|{|\[|[0-9])/$1" : $2/g; # output print $q->header(-type => "text/plain", -charset => "utf-8"); print "if (typeof($var) == 'undefined') $var = {};\n"; print $json."\n"; print "if (typeof($var.onload) == 'function') $var.onload($var.data);"; sub error{ print $q->header(-type => "text/plain", -charset => "utf-8"); print "var $var = \"$_[0]\";"; exit; }