home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / MenuBrowser.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.7 KB  |  281 lines

  1. <?php
  2. /**
  3.  * Simple filesystem browser that can be used to generate menu hashes
  4.  * 
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @category    HTML
  14.  * @package     HTML_Menu
  15.  * @author      Ulf Wendel <ulf.wendel@phpdoc.de>
  16.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  17.  * @author      Alexey Borzov <avb@php.net>
  18.  * @copyright   2001-2007 The PHP Group
  19.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20.  * @version     CVS: $Id: MenuBrowser.php,v 1.5 2007/05/18 20:54:33 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_Menu
  22.  */
  23.  
  24. /**
  25. * Simple filesystem browser that can be used to generate menu (3) hashes based on the directory structure.
  26. *
  27. * Together with menu (3) and the (userland) cache you can use this
  28. * browser to generate simple fusebox like applications / content systems.
  29. *
  30. * Let the menubrowser scan your document root and generate a menu (3) structure
  31. * hash which maps the directory structure, pass it to menu's setMethod() and optionally
  32. * wrap the cache around all this to save script runs. If you do so, it looks
  33. * like this:
  34. *
  35. * <code>
  36. * // document root directory
  37. * define('DOC_ROOT', '/home/server/www.example.com/');
  38. *
  39. * // instantiate the menubrowser
  40. * $browser = new HTML_MenuBrowser(DOC_ROOT);
  41. *
  42. * // instantiate menu (3)
  43. * $menu = new HTML_Menu($browser->getMenu());
  44. *
  45. * // output the sitemap
  46. * $menu->show('sitemap');
  47. * </code>
  48. *
  49. * Now, use e.g. simple XML files to store your content and additional menu informations
  50. * (title!). Subclass exploreFile() depending on your file format.
  51. *
  52. * @category     HTML
  53. * @package      HTML_Menu
  54. * @author       Ulf Wendel <ulf.wendel@phpdoc.de>
  55. * @version      Release: 2.1.4
  56. */
  57. class HTML_MenuBrowser 
  58. {
  59.    /**
  60.     * Filesuffix of your XML files.
  61.     *
  62.     * @var  string
  63.     * @see  HTML_MenuBrowser()
  64.     */
  65.     var $file_suffix = 'xml';
  66.  
  67.    /**
  68.     * Number of characters of the file suffix.
  69.     *
  70.     * @var  int
  71.     * @see  HTML_MenuBrowser()
  72.     */
  73.     var $file_suffix_length = 3;
  74.  
  75.    /**
  76.     * Filename (without suffix) of your index / start pages.
  77.     *
  78.     * @var  string
  79.     * @see  HTML_MenuBrowser()
  80.     */
  81.     var $index = 'index';
  82.  
  83.    /**
  84.     * Full filename of your index / start pages.
  85.     *
  86.     * @var  string
  87.     * @see  $file_suffix, $index
  88.     */
  89.     var $index_file = '';
  90.  
  91.    /**
  92.     * Directory to scan.
  93.     *
  94.     * @var  string
  95.     * @see  setDirectory()
  96.     */
  97.     var $dir = '';
  98.  
  99.    /**
  100.     * Prefix for every menu hash entry.
  101.     *
  102.     * Set the ID prefix if you want to merge the browser menu
  103.     * hash with another (static) menu hash so that there're no
  104.     * name clashes with the ids.
  105.     *
  106.     * @var  string
  107.     * @see  setIDPrefix()
  108.     */
  109.     var $id_prefix = '';
  110.  
  111.     /**
  112.     * Menu (3)'s setMenu() hash.
  113.     *
  114.     * @var  array
  115.     */
  116.     var $menu = array();
  117.  
  118.    /**
  119.     * Creates the object and optionally sets the directory to scan.
  120.     *
  121.     * @param    string  Directory to scan
  122.     * @param    string  Filename of index pages
  123.     * @param    string  Suffix for files containing the additional data
  124.     * @see      $dir
  125.     */
  126.     function HTML_MenuBrowser($dir = '', $index = '', $file_suffix = '')
  127.     {
  128.         if ($dir) {
  129.             $this->dir = $dir;
  130.         }
  131.         if ($index) {
  132.             $this->index = $index;
  133.         }
  134.         if ($file_suffix) {
  135.             $this->file_suffix = $file_suffix;
  136.         }
  137.  
  138.         $this->index_file = $this->index . '.' . $this->file_suffix;
  139.         $this->file_suffix_length = strlen($this->file_suffix);
  140.     }
  141.  
  142.  
  143.    /**
  144.     * Sets the directory to scan.
  145.     *
  146.     * @param    string  directory to scan
  147.     * @access   public
  148.     */
  149.     function setDirectory($dir) 
  150.     {
  151.         $this->dir = $dir;
  152.     }
  153.  
  154.  
  155.    /**
  156.     * Sets the prefix for every id in the menu hash.
  157.     *
  158.     * @param    string
  159.     * @access   public
  160.     */
  161.     function setIDPrefix($prefix) 
  162.     {
  163.         $this->id_prefix = $prefix;
  164.     }
  165.  
  166.  
  167.    /**
  168.     * Returns a hash to be used with menu(3)'s setMenu().
  169.     *
  170.     * @param    string  directory to scan
  171.     * @param    string  id prefix
  172.     * @access   public
  173.     */
  174.     function getMenu($dir = '', $prefix = '') 
  175.     {
  176.         if ($dir) {
  177.             $this->setDirectory($dir);
  178.         }
  179.         if ($prefix) {
  180.             $this->setIDPrefix($prefix);
  181.         }
  182.  
  183.         // drop the result of previous runs
  184.         $this->files = array();
  185.  
  186.         $this->menu = $this->browse($this->dir);
  187.         $this->menu = $this->addFileInfo($this->menu);
  188.  
  189.         return $this->menu;
  190.     }
  191.  
  192.  
  193.    /**
  194.     * Recursive function that does the scan and builds the menu (3) hash.
  195.     *
  196.     * @param    string  directory to scan
  197.     * @param    integer entry id - used only for recursion
  198.     * @param    boolean ??? - used only for recursion
  199.     * @return   array
  200.     */
  201.     function browse($dir, $id = 0, $noindex = false)
  202.     {
  203.         $struct = array();
  204.         $dh = opendir($dir);
  205.         while ($file = readdir($dh)) {
  206.             if ('.' == $file || '..' == $file) {
  207.                 continue;
  208.             }
  209.             $ffile = $dir . $file;
  210.             if (is_dir($ffile)) {
  211.                 $ffile .= '/';
  212.                 if (file_exists($ffile . $this->index_file)) {
  213.                     $id++;
  214.                     $struct[$this->id_prefix . $id]['url'] = $ffile . $this->index_file;
  215.  
  216.                     $sub = $this->browse($ffile, $id + 1, true);
  217.                     if (0 != count($sub)) {
  218.                         $struct[$this->id_prefix . $id]['sub'] = $sub;
  219.                     }
  220.                 }
  221.             } else {
  222.                 if ($this->file_suffix == substr($file, strlen($file) - $this->file_suffix_length, $this->file_suffix_length)
  223.                     && !($noindex && $this->index_file == $file) )
  224.                 {
  225.                     $id++;
  226.                     $struct[$this->id_prefix . $id]['url'] = $dir . $file;
  227.                 }
  228.             }
  229.         }
  230.         return $struct;
  231.     }
  232.  
  233.  
  234.    /**
  235.     * Adds further informations to the menu hash gathered from the files in it
  236.     *
  237.     * @param    array   Menu hash to examine
  238.     * @return   array   Modified menu hash with the new informations
  239.     */
  240.     function addFileInfo($menu) 
  241.     {
  242.         // no foreach - it works on a copy - the recursive
  243.         // structure requires already lots of memory
  244.         reset($menu);
  245.         while (list($id, $data) = each($menu)) {
  246.             $menu[$id] = array_merge($data, $this->exploreFile($data['url']));
  247.             if (isset($data['sub'])) {
  248.                 $menu[$id]['sub'] = $this->addFileInfo($data['sub']);
  249.             }
  250.         }
  251.  
  252.         return $menu;
  253.     }
  254.  
  255.  
  256.    /**
  257.     * Returns additional menu informations decoded in the file that appears in the menu.
  258.     *
  259.     * You should subclass this method to make it work with your own
  260.     * file formats. I used a simple XML format to store the content.
  261.     *
  262.     * @param    string  filename
  263.     */
  264.     function exploreFile($file) 
  265.     {
  266.         $xml = join('', @file($file));
  267.         if (!$xml) {
  268.             return array();
  269.         }
  270.  
  271.         $doc = xmldoc($xml);
  272.         $xpc = xpath_new_context($doc);
  273.  
  274.         $menu = xpath_eval($xpc, '//menu');
  275.         $node = &$menu->nodeset[0];
  276.  
  277.         return array('title' => $node->content);
  278.     }
  279. }
  280. ?>
  281.