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

  1. <?php  // -*- C++ -*- 
  2.  
  3. /* Here are the database definitions (for Solid) that i use in this code. 
  4.  * It should not be hard to adapt it to another database. 
  5.  */ 
  6.  
  7. /* 
  8. CREATE TABLE dirent_types ( 
  9.     id    INTEGER NOT NULL, 
  10.     icon    VARCHAR(50), 
  11.     name    VARCHAR(50), 
  12.     PRIMARY KEY(id) 
  13. ); 
  14.  
  15. INSERT INTO dirent_types VALUES(1, 'folderclosed', 'Directory'); 
  16. INSERT INTO dirent_types VALUES(2, 'document', 'File'); 
  17.  
  18. CREATE TABLE directory ( 
  19.     id    INTEGER NOT NULL, 
  20.     parent    INTEGER REFERENCES directory(id), 
  21.     name    VARCHAR(200), 
  22.     icon    VARCHAR(50), 
  23.     type    INTEGER REFERENCES dirent_types(id), 
  24.     url    VARCHAR(200), 
  25.     PRIMARY KEY(id) 
  26. ); 
  27.  
  28. DROP INDEX directory_idx; 
  29.  
  30. CREATE UNIQUE INDEX directory_idx ON directory(parent, name); 
  31.  
  32. CREATE SEQUENCE dirent_id; 
  33.  
  34. "CREATE PROCEDURE insert_dir_entry 
  35.     (name VARCHAR, parent INTEGER, type INTEGER) 
  36.     RETURNS(id INTEGER) 
  37. BEGIN 
  38.     EXEC SQL WHENEVER SQLERROR ABORT; 
  39.     EXEC SEQUENCE dirent_id.NEXT INTO id; 
  40.     EXEC SQL PREPARE c_insert 
  41.         INSERT INTO directory 
  42.             (id, parent, type, name) 
  43.             VALUES(?, ?, ?, ?); 
  44.     EXEC SQL EXECUTE c_insert USING (id, parent, type, name); 
  45.     EXEC SQL DROP c_insert; 
  46. END"; 
  47.  
  48. CALL insert_dir_entry('My Computer', NULL, 1); 
  49. CALL insert_dir_entry('Network Neighbourhood', NULL, 1); 
  50. CALL insert_dir_entry('lucifer.guardian.no', 2, 1); 
  51. CALL insert_dir_entry('rafael.guardian.no', 2, 1); 
  52. CALL insert_dir_entry('uriel.guardian.no', 2, 1); 
  53. CALL insert_dir_entry('Control Panel', NULL, 1); 
  54. CALL insert_dir_entry('Services', 6, 1); 
  55. CALL insert_dir_entry('Apache', 7, 2); 
  56. CALL insert_dir_entry('Solid Server 2.2', 7, 2); 
  57.  
  58. */ 
  59.  
  60. function icon($icon, $name =  '', $width = 0, $height = 0) { 
  61.     global $DOCUMENT_ROOT; 
  62.     $icon_loc =  '/pics/menu'; 
  63.     $file =  "$DOCUMENT_ROOT$icon_loc/$icon.gif"; 
  64.     if (!$width || !$height) { 
  65.     $iconinfo = getimagesize($file); 
  66.     if (!$width) { 
  67.         $width = $iconinfo[0]; 
  68.     } 
  69.     if (!$height) { 
  70.         $height = $iconinfo[1]; 
  71.     } 
  72.     } 
  73.     printf( '<img%s border=0 align=top src="/pics/menu/%s.gif" '. 
  74.         'width="%d" height="%d">', $name ?  " name=\"$name\"" :  '', 
  75.        $icon, $width, $height); 
  76.  
  77. /* 
  78.  * Displays, recursively, the contents of a tree given a starting 
  79.  * point. 
  80.  * 
  81.  * Parameters: 
  82.  *   $parent - the parent node (not listed in the directory).  Node 
  83.  *     0 is the root node. 
  84.  * 
  85.  *   $maxdepth (optional) - maximum number of recursion levels.  -1 
  86.  *     (the default value) means no limits. 
  87.  * 
  88.  *   $ancestors (optional) - an array of the ancestor nodes in the 
  89.  *     current branch of the tree, with the node closest to the 
  90.  *     top at index 0. 
  91.  * 
  92.  * Global variables used: 
  93.  *   $child_nodes 
  94.  *   $node_data 
  95.  *   $last_child 
  96.  * 
  97.  * Global variables modified: 
  98.  *   The array pointers in $child_nodes will be modified. 
  99.  */ 
  100. function display_directory($parent, $showdepth = 0, $ancestors = false) { 
  101.     global $child_nodes, $node_data, $last_child; 
  102.     reset($child_nodes[$parent]); 
  103.     $size = sizeof($child_nodes[$parent]); 
  104.     $lastindex = $size - 1; 
  105.     if (!$ancestors) { 
  106.     $ancestors = array(); 
  107.     } 
  108.     $depth = sizeof($ancestors); 
  109.     printf( '<div id="node_%d" class="dirEntry" visibility="%s">', 
  110.        $parent, $showdepth > 0 ?  'show' :  'hide'); 
  111.     while (list($index, $node) = each($child_nodes[$parent])) { 
  112.      /* 
  113.       For each of the uptree nodes: 
  114.       If an uptree node is not the last one on its depth 
  115.       of the branch, there should be a line instead of a blank 
  116.       before this node's icon. 
  117.      */ 
  118.     for ($i = 0; $i < $depth; $i++) { 
  119.         $up_parent = (int)$node_data[$ancestors[$i]][ 'parent']; 
  120.         $last_node_on_generation = $last_child[$up_parent]; 
  121.         $uptree_node_on_generation = $ancestors[$i]; 
  122.         if ($last_node_on_generation == $uptree_node_on_generation) { 
  123.         icon( "blank"); 
  124.         } else { 
  125.         icon( "line"); 
  126.         } 
  127.     } 
  128.     if ($child_nodes[$node]) {  // has children, i.e. it is a folder 
  129.         $conn_icon =  "plus"; 
  130.         $expand = true; 
  131.     } else { 
  132.         $conn_icon =  "join"; 
  133.         $expand = false; 
  134.     } 
  135.     if ($index == $lastindex) { 
  136.         $conn_icon .=  "bottom"; 
  137.     } elseif ($depth == 0 && $index == 0) { 
  138.         $conn_icon .=  "top"; 
  139.     } 
  140.     if ($expand) { 
  141.         printf( "<a href=\"javascript:document.layers['node_%d'].visibility='show'\">", $node); 
  142.     } 
  143.     icon($conn_icon,  "connImg_$node"); 
  144.     if ($expand) { 
  145.         print( "</a>"); 
  146.     } 
  147.     $icon = $node_data[$node][ 'icon']; 
  148.     if (!$icon) { 
  149.         $type = $node_data[$node][ 'type']; 
  150.         $icon = $GLOBALS[ 'dirent_icons'][$type]; 
  151.     } 
  152.     icon($icon,  "nodeImg_$node"); 
  153.  
  154.     $name = $node_data[$node][ 'name']; 
  155.     printf( ' <font size="%d">%s</font><br%c>', -1, $name, 10); 
  156.     if ($child_nodes[$node]) { 
  157.         $newdepth = $showdepth; 
  158.         if ($newdepth > 0) { 
  159.         $newdepth--; 
  160.         } 
  161.         $new_ancestors = $ancestors; 
  162.         $new_ancestors[] = $node; 
  163.         display_directory($node, $newdepth, $new_ancestors); 
  164.     } 
  165.     } 
  166.     print( "</div\n>"); 
  167.  
  168. function setup_directory($parent, $maxdepth) 
  169.     global $dirent_icons, $child_nodes, $node_data, $last_child; 
  170.  
  171.     $dirent_icons = sql_assoc( 'SELECT id,icon FROM dirent_types'); 
  172.  
  173.     $query =  'SELECT id,parent,type,icon,name '. 
  174.           'FROM directory '. 
  175.           'ORDER BY parent,name'; 
  176.  
  177.     $child_nodes = array(); 
  178.     $node_data = array(); 
  179.     $res = sql($query); 
  180.     while (list($id, $parent, $type, $icon, $name) = db_fetch_row($res)) { 
  181.     $child_nodes[(int)$parent][] = $id; 
  182.     $node_data[$id] = array( 'id' => $id, 
  183.                  'parent' => $parent, 
  184.                  'type' => $type, 
  185.                  'icon' => $icon, 
  186.                  'name' => $name); 
  187.     $last_child[(int)$parent] = $id; 
  188.     } 
  189.  
  190. ?> 
  191.