PX : code

phpODP Class by Ryan Heggem
Download this code


<?php

        
// REMEMBER TO RENAME TO: phpodp.inc

        // phpODP.inc - Version 1.0 - PHP Class phpODP for use with ODP (dmoz.org) data retrieval
        // Copyright (c) 2000, Ryan Heggem.
        //      URL:    http://www.gren.addr.com/
        //      EMAIL:  gren@addr.com
        //
        // Please send questions, comments, whatever to the email address above.
        //
        // A demo of what this class can do can be found at:
        // http://www.gren.addr.com/scripts/PHP/phpODP/directory.php
        //
        // Released under the GNU GPL.
        // http://www.gnu.org/copyleft/gpl.html
        // 
        // This program is free software; you can redistribute it and/or
        // modify it under the terms of the GNU General Public License
        // as published by the Free Software Foundation; either version 2
        // of the License, or (at your option) any later version.
        // 
        // This program is distributed in the hope that it will be useful,
        // but WITHOUT ANY WARRANTY; without even the implied warranty of
        // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        // GNU General Public License for more details.
        // 
        // You should have received a copy of the GNU General Public License
        // along with this program; if not, write to the Free Software
        // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


        // usage:
        //              require('phpodp.inc');
        //              $odp = new phpODP;
        //              $odp->_init();

        // returns:
        //              [ variable ]            [ type ]        [ description ]
        //              $odp->dirs              array           array of the current dir level chopped up err somethin...
        //              $odp->contents          array           dmoz.org gets split up into sections and put into this array
        //              $odp->page_title        string          the page title of the catagory fetched
        //              $odp->desc_file         string          html <a> tag w/ link to catagory description file if available
        //              $odp->faq_file          string          html <a> tag w/ link to catagory faq file if available
        //              $odp->a2z               string          html <a> tags w/ links to the single lettered catagories if available
        //              $odp->catagories        string          html chunk of the sub-catagory listing
        //              $odp->other_catagories  string          another (?) html chunk of the sub-directory listing
        //              $odp->links             string          html chunk of all the links in the current catagory

        // TODO(s):
        //              1)      make a function to replace this line of code to check/set a cache'd (is that even a word?) version of the current catagory
        //              ^------ SEE TODO#1 IN THE CODE BELOW
        //              2)      add a little search box like the real ODP catagories
        //              3)      add an editors variable (array?) to help list out all the editors of a certain catagory

        
class phpODP {

                function 
_init() {

                        global 
$SERVER_NAME$SCRIPT_NAME$PATH_INFO$PHP_SELF;

                        if(empty(
$PATH_INFO)){
                                
header('Location: http://' $SERVER_NAME '' $SCRIPT_NAME '/Computers/');
                        }

                        
// misc stuff...
                        
$this->dirs explode('/'preg_replace('|^/(.*)/$|''\\1'$PATH_INFO));

                        
// spit out each section of a dmoz.org catagory into an array
                        // TODO #1: 
                        
$this->contents explode('<hr>'implode(''file('http://dmoz.org' $PATH_INFO) ) );

                        
// get page title
                        
eregi('<title>(.*)</title>'$this->contents[0], $tmp_page_title);
                        
$this->page_title $tmp_page_title[1];

                        
// check for description and faq files, then set up links to them
                        
$this->desc_file = (preg_match('|desc.html|i'$this->contents[0])) ? ' <a href="http://dmoz.org' $PATH_INFO 'desc.html">Description</a> ' '';
                        
$this->faq_file = (preg_match('|faq.html|i'$this->contents[0])) ? ' <a href="http://dmoz.org' $PATH_INFO 'faq.html">FAQ</a> ' '';

                        
// check for an A-Z catagory listing, then set up links to each one
                        
$a2zA explode(','strtoupper('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'));
                        if(
preg_match('|/[A-Z]/|i'$this->contents[0])){
                                for(
$z=0;$z<=25;$z++){
                                        
$sfx = ($z != 25) ? ' / ' '';
                                        
$this->a2z .= '<a href="' $PHP_SELF $a2zA[$z] . '/">' $a2zA[$z] . '</a>' $sfx;
                                }
                        } else{ 
$this->a2z ''; }

                        
// this chunk of code...
                        //  goes through each array element of $contents to see what's what
                        //   replaces all the dmoz.org relative links with a local version
                        //    sets a <font> tag in <td> tags to make the text in them look nice
                        //     sets <img> tags to point to dmoz.org
                        
if(preg_match('|href="/|i'$this->contents[1])){
                                
$this->catagories preg_replace('|href="/(.*)"|im''href="' $SCRIPT_NAME '/\\1"'preg_replace('|(<td valign=top>)|im''\\1<font face="Arial, Helvetica" size="2">'preg_replace('|(</td>)|im''</font>\\1'$this->contents[1])));
                                if(
preg_match('|href="/|i'$this->contents[2])){
                                        
$this->other_catagories preg_replace('|href="/(.*)"|im''href="' $SCRIPT_NAME '/\\1"'preg_replace('|(<td valign=top>)|im''\\1<font face="Arial, Helvetica" size="2">'preg_replace('|(</td>)|im''</font>\\1'$this->contents[2])));
                                        if(
preg_match('|<li><a href="http://|i'$this->contents[3])){
                                                
$this->links preg_replace('|src="/(.*)"|im''src="http://dmoz.org/\\1"'$this->contents[3]);
                                        } else{ 
$this->links ''; }
                                } elseif(
preg_match('|<li><a href="http://|i'$this->contents[2])){
                                        
$this->links preg_replace('|src="/(.*)"|im''src="http://dmoz.org/\\1"'$this->contents[2]);
                                } else{ 
$this->other_catagories ''$this->links ''; }
                        } elseif(
preg_match('|<li><a href="http://|i'$this->contents[1])){
                                
$this->links preg_replace('|src="/(.*)"|im''src="http://dmoz.org/\\1"'$this->contents[1]);
                        } else{ 
$this->catagories ''$this->other_catagories ''$this->links ''; }

                }

        }
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.