home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / themesorgparser.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  10.1 KB  |  223 lines

  1. Themes.org parser and navigator 
  2.  
  3. This code parses XML files produced by the themes.org web site and outputs nice formated display quicker to render than the real site. The parser is used by a navigator here : http://php3.free.fr/mkrus/tobrowser.php3 
  4.  
  5.  
  6. <?
  7.  
  8. // 
  9. // TOPARSER 0.1 - Mike Krus  mwkrus@yahoo.com   http://mkrus.free.fr/ 
  10. // 
  11. // You may use this code for whatever purpose as long you keep this message 
  12. // and don't blame me if does not work!! 
  13. // 
  14.  
  15. // error_reporting(E_ALL); 
  16.  
  17. function themeparser($themeorg, $ODBY =  "upd", $SRTHEMES =  "0", 
  18.                      $NUMTHEMES =  "6", $cols = 3)
  19. {
  20.     $hostname = $themeorg .  ".themes.org";
  21.     $port = 80;
  22.     $uri =  "/themes.rdf.phtml?odby=$ODBY&sc=desc&srtheme=$SRTHEMES&numthemes=$NUMTHEMES&moderated=off";
  23.  
  24.     $header_passed  = 0;
  25.     $cache =  "cache/cache.to." . $themeorg . $ODBY . $SRTHEMES . $NUMTHEMES .  ".txt";
  26.  
  27.     $comment =  "(cached for one hour)";
  28.  
  29.     $ctime = fileMtime($cache);
  30.     $ntime = Time();
  31.     $dtime = $ntime - $ctime;
  32.     $filedate =  "No date given";
  33.  
  34.     $themecount = 0;
  35.     $linelength = 1024;
  36.  
  37.     $colwidth = 100 / $cols;
  38.  
  39.     if($dtime > $ntime || $dtime > 3600 || fileSize($cache) < 20) {
  40.         $cfile = fopen($cache,  "w");
  41.         if(!$cfile) {
  42.             echo ( "Could not create cache file " . $cache .  "\n");
  43.             exit;
  44.         }
  45.  
  46.         $fpread = fsockopen( "$hostname", $port, &$errno, &$errstr);
  47.         if(!$fpread) {
  48.                echo  "Error connecting to " . $hostname .  ": " . $errstr .  "<br>\n";
  49.         } else {
  50.  
  51.             fputs($fpread,  "GET " . $uri .  " HTTP/1.0\n");
  52.             fputs($fpread,  "User-Agent: PHP TOParser/0.1  mkrus@free.fr\n");
  53.             fputs($fpread,  "Host: " . $hostname .  ":80\n");
  54.             fputs($fpread,  "Accept: */*\n\n");
  55.  
  56.             $str = fgets($fpread, $linelength);
  57.             if(strstr($str,  "404") !=  "") {
  58.                 echo  "Document <A HREF=\"http://" . $hostname . $uri.  "\">http://" . $hostname . $uri .  "</A> not found\n";
  59.                 fclose($cfile);
  60.                 exit;
  61.             }
  62.  
  63.             while (!feof($fpread)) {
  64.                 if ($header_passed == 1) {
  65.                     $str = trim(fgets($fpread, $linelength));
  66.                     
  67.                     if($str ==  "") { continue; }
  68.  
  69.                     if(strstr($str,  "<channel>") !=  "") {
  70.  
  71.                         $keepon = 1;
  72.                         while(1) {
  73.                             $locstr = trim(fgets($fpread, $linelength));
  74.                             if($locstr ==  "") { continue; }
  75.  
  76.                             if(strstr($locstr,  "<title>") !=  "") {
  77.                                 $channeltitle = strtok($locstr,  "<>");
  78.                                 $channeltitle = strtok( "<>");
  79.                                 $channeltitle = strtok( "<>");
  80.                             } elseif(strstr($locstr,  "<link>") !=  "") {
  81.                                 $channelurl = strtok($locstr,  "<>");
  82.                                 $channelurl = strtok( "<>");
  83.                                 $channelurl = strtok( "<>");
  84.                             } elseif(strstr($locstr,  "</channel>") !=  "") {
  85.                                 break;
  86.                             }
  87.                         }
  88.                         continue;
  89.                     }
  90.  
  91.                     if(strstr($str,  "<item>") !=  "") {
  92.  
  93.                         if($themecount % $cols == 0) { 
  94.                             fputs($cfile,  "<table width=\"100%\" cellpadding=5 cols=$cols><TR valign=top>");
  95.                         }
  96.  
  97.                         fputs($cfile,  "<TH width=\"$colwidth%\"><table cellpadding=0 cellspacing=0 border=0 bgcolor=\"#000000\" width=\"100%\"><tr><td>\n");
  98.                         fputs($cfile,  "<table cellpadding=4 cellspacing=1 border=0 width=\"100%\">\n");
  99.                         fputs($cfile,  "<tr><td class=\"NBT\" background=\"Images/news_title_background.gif\"><p><font size=\"3\">\n");
  100.  
  101.                         $invers =  "";
  102.                         if(isset($vertab)) unset($vertab);
  103.  
  104.                         while(1) {
  105.  
  106.                             $str = trim(fgets($fpread, $linelength));
  107.  
  108.                             if($invers !=  "") {
  109.                                 if(strstr($str,  "</ver") !=  "") {
  110.                                     $invers =  "";
  111.                                 } elseif(strstr($str,  "<link>") !=  "") {
  112.                                     $tmp = strtok($str,  "<>");
  113.                                     $tmp = strtok( "<>");
  114.                                     $vertab[] = $invers;
  115.                                     $vertab[] = strtok( "<>");
  116.                                 } elseif(strstr($str,  "</item>") !=  "") {
  117.                                     break;
  118.                                 }
  119.                             } else  {
  120.                                 if(strstr($str,  "<name>") !=  "") {
  121.                                     $name = strtok($str,  "<>");
  122.                                     $name = strtok( "<>");
  123.                                     $name = strtok( "<>");
  124.                                 } elseif(strstr($str,  "<updated>") !=  "") {
  125.                                     $update = strtok($str,  "<>");
  126.                                     $update = strtok( "<>");
  127.                                     $update = strtok( "<>");
  128.                                     $update = Date( "m/d/y", $update);
  129.                                 } elseif(strstr($str,  "<id>") !=  "") {
  130.                                     $tid = strtok($str,  "<>");
  131.                                     $tid = strtok( "<>");
  132.                                     $tid = strtok( "<>");
  133.                                 } elseif(strstr($str,  "<link>") !=  "") {
  134.                                     $link = strtok($str,  "<>");
  135.                                     $link = strtok( "<>");
  136.                                     $link = strtok( "<>");
  137.                                 } elseif(strstr($str,  "<shot>") !=  "") {
  138.                                     $shot = strtok($str,  "<>");
  139.                                     $shot = strtok( "<>");
  140.                                     $shot = strtok( "<>");
  141.                                 } elseif(strstr($str,  "<thumb>") !=  "") {
  142.                                     $thumb = strtok($str,  "<>");
  143.                                     $thumb = strtok( "<>");
  144.                                     $thumb = strtok( "<>");
  145.                                 } elseif(strstr($str,  "<vers>") !=  "") {
  146.                                     $vers = strtok($str,  "<>");
  147.                                     $vers = strtok( "<>");
  148.                                     $vers = strtok( "<>");
  149.                                 } elseif(strstr($str,  "<ver ") !=  "") {
  150.                                     $toto = strtok($str,  " ");
  151.                                     $toto = strtok( " ");
  152.                                     $invers = strtok($toto,  "<>");
  153.                                 } elseif(strstr($str,  "</item>") !=  "") {
  154.                                     break;
  155.                                 }
  156.                             }
  157.                         }
  158.  
  159.                         fputs($cfile,  "<A class=\"NBT\" HREF=\"$link\">$name</A> </font></p></td></tr>\n");
  160.                         fputs($cfile,  "<TR><TH class=\"NBB\" bgcolor=\"#cccccc\"><A HREF=\"$shot\"><IMG BORDER=0 SRC=\"$thumb\" WIDTH=150 HEIGHT=120 aLT=\"Screen Shot\"></A><BR></TH></TR>\n");
  161.  
  162.                         fputs($cfile,  "<tr><td class=\"NBC\"><table width=\"100%\" cellpadding=0 cellspacing=0 border=0><TR>\n");
  163.                         fputs($cfile,  "<td class=\"NBC\"><font size=\"1\">Last update: " . $update .  "</font></td>");
  164.                         fputs($cfile,  "<td class=\"NBC\" align=right><font size=\"2\">");
  165.  
  166.                         if(isset($vertab)) {
  167.                             $i=0; $j=0;
  168.                             while($i < count($vertab)) {
  169.                                 $verkey = $vertab[$i];
  170.                                 $verlink = $vertab[$i + 1];
  171.                                 if($j > 0) { fputs($cfile,  "| "); }
  172.                                 fputs($cfile,  "<A HREF=\"http://" . $hostname .  "/php/download.phtml?query=download&rev=" . $verkey .  "&server=themes.org&object=" . $themeorg .  ".theme." . $tid.  "\">" . $verkey .  "</A> ");
  173.                                 $j++;
  174.                                 $i+=2;
  175.                             }
  176.                         }
  177.  
  178.                         fputs($cfile,  "</font></td></tr></table></td></tr>\n");
  179.                         fputs($cfile,  "</table></td></tr></table></TH>\n");
  180.  
  181.                         if($themecount % $cols == $cols - 1) { 
  182.                             fputs($cfile,  "</TR></table>"); 
  183.                         }
  184.  
  185.                         $themecount++;
  186.                     }
  187.  
  188.                 } else {
  189.                     $str = ltrim(Chop(fgets($fpread, $linelength)));
  190.  
  191.                     if ($str ==  "") {
  192.                         $header_passed = 1;
  193.                     }
  194.                 }
  195.             }
  196.  
  197.             for($i = $themecount % $cols; $i < $ncols; $i++)
  198.                 fputs($cfile,  "<TD width=\"$colwidth%\"> </tD>");
  199.             if($themecount % $cols) { fputs($cfile,  "</TR></table>"); }
  200.  
  201.             fputs($cfile,  "<table width=\"100%\" cellpadding=5>");
  202.             fputs($cfile,  "<TR><Td><table cellpadding=0 cellspacing=0 border=0 bgcolor=\"#000000\" width=\"100%\"><tr><td>\n");
  203.             fputs($cfile,  "<table cellpadding=4 cellspacing=1 border=0 width=\"100%\">\n");
  204.             $filedate = Date( "H:i m/d/y");
  205.             fputs($cfile,  "<tr><td class=\"NBC\"><p>Last update: " . $filedate .  " GMT");
  206.             fputs($cfile,  " -- <A HREF=\"http://" . $hostname . $uri .  "\">RDF</A> -- " . $themecount .  " themes\n");
  207.  
  208.         }
  209.         fclose($fpread);
  210.         fclose($cfile);
  211.  
  212.         $comment =  "";
  213.  
  214.     }
  215.  
  216.     include  "$cache"; 
  217.  
  218.     echo( "$comment</TD></TR></table></td></tr></table></td></tr></table>");
  219.     flush();
  220. }
  221.  
  222. ?>
  223.