home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / cms_site.class.inc < prev    next >
Text File  |  2004-03-08  |  34KB  |  1,108 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12. require_once($GO_CONFIG->class_path.'cms.class.inc');
  13.  
  14. class cms_site extends cms
  15. {
  16.     var $site;
  17.     var $search_words;
  18.     var $publish_url;
  19.     var $publish_path;
  20.     var $publish_static = false;
  21.     var $template_file_replacements = array();
  22.  
  23.     function cms_site($site_id)
  24.     {
  25.         global $GO_CONFIG;
  26.         $this->db();
  27.  
  28.         if (!$this->site = $this->get_site($site_id))
  29.         {
  30.             return false;
  31.         }
  32.  
  33.         $publish_path = $this->site['publish_path'];
  34.         $root_publish_path = $GO_CONFIG->get_setting('cms_publish_path');
  35.         $this->publish_path = $root_publish_path.$publish_path;
  36.         $this->publish_url = $GO_CONFIG->get_setting('cms_publish_url').$publish_path;
  37.     }
  38.  
  39.     function publish()
  40.     {
  41.         global $GO_CONFIG;
  42.  
  43.         require_once($GO_CONFIG->class_path.'filesystem.class.inc');
  44.         $fs = new filesystem(true);
  45.  
  46.         if (!file_exists($this->publish_path))
  47.         {
  48.             return false;
  49.         }
  50.  
  51.         $fs->delete($this->publish_path);
  52.         mkdir($this->publish_path);
  53.  
  54.         if ($this->site['publish_style'] == '2')
  55.         {
  56.             $this->publish_static = true;
  57.  
  58.             mkdir($this->publish_path.'_site_data');
  59.             mkdir($this->publish_path.'_search');
  60.  
  61.             $file = '<?php $site_id='.$this->site["id"].';
  62.             require("'.$GO_CONFIG->root_path.'Group-Office.php");
  63.             if ($cms_module = $GO_MODULES->get_module("cms"))
  64.             {
  65.                 $published = true;
  66.                 require($GO_CONFIG->root_path.$cms_module["path"]."view.inc");
  67.             }else
  68.             {
  69.                 die("Failed to get Content Management Module");
  70.             }
  71.             ?>';
  72.  
  73.             if (!$fp = fopen($this->publish_path.'_search/index.php', 'w+'))
  74.             {
  75.                 return false;
  76.             }elseif(!fwrite($fp, $file))
  77.             {
  78.                 fclose($fp);
  79.                 return false;
  80.             }else
  81.             {
  82.                 fclose($fp);
  83.             }
  84.  
  85.             $this->create_stylesheet($this->publish_path.'_site_data'.$GO_CONFIG->slash.'style.css');
  86.             $this->create_template_files();
  87.  
  88.             return $this->create_files($this->site['root_folder_id'], $this->publish_path);
  89.  
  90.         }else
  91.         {
  92.             $file = '<?php $site_id='.$this->site["id"].';
  93.             require("'.$GO_CONFIG->root_path.'Group-Office.php");
  94.             if ($cms_module = $GO_MODULES->get_module("cms"))
  95.             {
  96.                 $published = true;
  97.                 require($GO_CONFIG->root_path.$cms_module["path"]."view.inc");
  98.             }else
  99.             {
  100.                 die("Failed to get Content Management Module");
  101.             }
  102.             ?>';
  103.  
  104.             if (!$fp = fopen($this->publish_path.'index.php', 'w+'))
  105.             {
  106.                 return false;
  107.             }elseif(!fwrite($fp, $file))
  108.             {
  109.                 fclose($fp);
  110.                 return false;
  111.             }else
  112.             {
  113.                 return fclose($fp);
  114.             }
  115.         }
  116.     }
  117.  
  118.     function create_stylesheet($path)
  119.     {
  120.         global $GO_CONFIG;
  121.  
  122.         $file = '';
  123.         if ($template = $this->get_template($this->site['template_id']))
  124.         {
  125.             $file = $template['style'].$template['additional_style'];
  126.         }
  127.  
  128.         if (!$fp = fopen($path, 'w+'))
  129.         {
  130.             return false;
  131.         }elseif(!fwrite($fp, $file))
  132.         {
  133.             fclose($fp);
  134.             return false;
  135.         }else
  136.         {
  137.             fclose($fp);
  138.         }
  139.         return true;
  140.     }
  141.  
  142.     function create_template_files()
  143.     {
  144.         global $GO_CONFIG, $GO_MODULES;
  145.  
  146.         $cms_module = $GO_MODULES->get_module('cms');
  147.  
  148.         $this->get_template_files($this->site['template_id']);
  149.  
  150.         while ($this->next_record())
  151.         {
  152.             if (!$fp = fopen($this->publish_path.'_site_data'.$GO_CONFIG->slash.$this->f('name'), 'w+'))
  153.             {
  154.                 return false;
  155.             }elseif(!fwrite($fp, $this->f('content')))
  156.             {
  157.                 fclose($fp);
  158.                 return false;
  159.             }else
  160.             {
  161.                 fclose($fp);
  162.             }
  163.  
  164.             $tpl_file['dynamic'] = $GO_CONFIG->host.$cms_module['path'].'template_file.php?template_file_id='.$this->f('id');
  165.             $tpl_file['static'] = $this->publish_url.'_site_data/'.$this->f('name');
  166.             $this->template_file_replacements[] = $tpl_file;
  167.         }
  168.     }
  169.  
  170.     function create_files($folder_id, $path)
  171.     {
  172.         global $GO_CONFIG, $GO_MODULES;
  173.  
  174.         $cms_module = $GO_MODULES->get_module("cms");
  175.  
  176.         $cms_site = new cms_site($this->site['id']);
  177.  
  178.         $cms_site->get_folders($folder_id);
  179.  
  180.         while($cms_site->next_record())
  181.         {
  182.             $new_path = $path.urlencode($cms_site->f('name')).$GO_CONFIG->slash;;
  183.             if (!file_exists($new_path))
  184.             {
  185.                 mkdir($new_path);
  186.             }
  187.  
  188.             if (!is_writable($new_path) || !$this->create_files($cms_site->f('id'), $new_path))
  189.             {
  190.                 return false;
  191.             }
  192.         }
  193.  
  194.         $index_created = false;
  195.         $cms_site->get_files($folder_id, 'priority', 'ASC');
  196.         while ($cms_site->next_record())
  197.         {
  198.             if ($folder_id == $this->site['root_folder_id'] && !$index_created)
  199.             {
  200.                 $name = 'index.html';
  201.                 $index_created = true;
  202.             }else
  203.             {
  204.                 $name = $cms_site->f('name');
  205.             }
  206.  
  207.             $full_path = $path.urlencode($name);
  208.             $file = $this->generate_page($cms_site->f('id'));
  209.             if (!$fp = fopen($full_path, 'w+'))
  210.             {
  211.                 return false;
  212.             }elseif(!fwrite($fp, $file))
  213.             {
  214.                 fclose($fp);
  215.                 return false;
  216.             }else
  217.             {
  218.                 fclose($fp);
  219.             }
  220.  
  221.             $tpl_file['dynamic'] = $GO_CONFIG->host.$cms_module['path'].'view.php?site_id='.$this->site['id'].'&file_id='.$cms_site->f('id');
  222.             $tpl_file['static'] = str_replace($this->publish_path, $this->publish_url, $full_path);
  223.             $this->template_file_replacements[] = $tpl_file;
  224.         }
  225.         return true;
  226.     }
  227.  
  228.  
  229.     function generate_page($_file_id=0, $task='', $search_word_id='')
  230.     {
  231.         global $GO_CONFIG, $GO_MODULES, $GO_LANGUAGE;
  232.         //set the folder id we are in
  233.         $file_id = $_file_id;
  234.         $folder_id = isset($_REQUEST['folder_id']) ? $_REQUEST['folder_id'] : $this->site['root_folder_id'];
  235.  
  236.         require($GO_LANGUAGE->get_language_file('cms'));
  237.  
  238.         if ($file_id > 0)
  239.         {
  240.             $file = $this->get_file($file_id);
  241.             if (!eregi('htm', $file['extension']))
  242.             {
  243.                 return $file['content'];
  244.             }
  245.  
  246.             if ($file['description'] == '')
  247.             {
  248.                 $file['description'] = $this->site['description'];
  249.             }
  250.  
  251.             if ($file['keywords'] == '')
  252.             {
  253.                 $file['keywords'] = $this->site['keywords'];
  254.             }
  255.  
  256.             if ($file['title'] == '')
  257.             {
  258.                 $file['title'] = $this->site['name'];
  259.             }
  260.             $folder_id = $file['folder_id'];
  261.  
  262.             $this->get_attached_search_words($file_id);
  263.             while($this->next_record())
  264.             {
  265.                 $file['keywords'] = $this->f('search_word').', '.$file['keywords'];
  266.             }
  267.         }else
  268.         {
  269.             $file['title'] = $this->site['name'];
  270.             $file['keywords'] = $this->site['keywords'];
  271.             $file['description'] = $this->site['description'];
  272.         }
  273.  
  274.         $main_template_item = $this->get_main_template_item($this->site['template_id']);
  275.  
  276.         //get the search words so we can highlight them
  277.         $this->search_words=array();
  278.         $this->get_search_words($this->site['id']);
  279.         while ($this->next_record())
  280.         {
  281.             $search_word1 = $this->f('search_word');
  282.             $search_word2 = htmlentities($this->f('search_word'));
  283.             $this->search_words[] = $search_word1;
  284.             if ($search_word1 != $search_word2)
  285.             {
  286.                 $this->search_words[] = $search_word2;
  287.             }
  288.         }
  289.  
  290.         $browser = detect_browser();
  291.  
  292.         $cms_module = isset($cms_module) ? $cms_module : $GO_MODULES->get_module('cms');
  293.         //start output
  294. $page = '<html>
  295. <head>
  296. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  297. <meta name="keywords" content="'.$file['keywords'].'" />
  298. <meta name="description" content="'.$file['description'].'" />
  299. <meta name="Generator" content="Group-Office '.$GO_CONFIG->version.'" />
  300. <title>'.$file['title'].'</title>';
  301.  
  302.         if ($this->publish_static)
  303.         {
  304.             $page .= '<link href="'.$this->publish_url.'_site_data/style.css" rel="stylesheet" type="text/css" />';
  305.         }else
  306.         {
  307.             $page .= '<link href="'.$GO_CONFIG->host.$cms_module['path'].'stylesheet.php?template_id='.$this->site['template_id'].'" rel="stylesheet" type="text/css" />';
  308.         }
  309.  
  310.         $page .= '<script type="text/javascript" src="'.$GO_CONFIG->host.'javascript/common.js"></script>
  311.         </head>
  312.         <body>';
  313.  
  314.         switch($task)
  315.         {
  316.             case 'search':
  317.                 if (isset($main_template_item))
  318.                 {
  319.                     $file_id=0;
  320.                     $folder_id=0;
  321.                     $main_template_content =  $this->parse_template($main_template_item['content']);
  322.                     $page .= str_replace($this->get_tag('<content', $main_template_content), $this->print_search_words(), $main_template_content);
  323.                 }else
  324.                 {
  325.                     $page .= print_search_words();
  326.                 }
  327.             break;
  328.  
  329.             case 'search_results':
  330.                 $file_id=0;
  331.                 $folder_id=0;
  332.                 $count = $this->get_attached_files($search_word_id);
  333.                 $result = '<b>';
  334.                 if ($count == 1)
  335.                 {
  336.                     $result .= $cms_found_one;
  337.                 }else
  338.                 {
  339.                     $result .= $count.' '.$cms_found;
  340.                 }
  341.                 $result .= '</b><br /><br />';
  342.  
  343.                 while($this->next_record())
  344.                 {
  345.                     $title = ($this->f('title') != '') ? $this->f('title') : strip_extension($this->f('name'));
  346.                     $result .= '<table border="0"><tr><td>';
  347.  
  348.                     if (!$this->publish_static)
  349.                     {
  350.                         $this->publish_static = ($this->site['publish_style'] == '2') ? true : false;
  351.                         $result .= '<a href="'.$this->create_url($this->f('folder_id'),$this->f('id')).'">'.$title.'</a>';
  352.                         $this->publish_static = false;
  353.                     }else
  354.                     {
  355.                         $result .= '<a href="'.$this->create_url($this->f('folder_id'),$this->f('id')).'">'.$title.'</a>';
  356.                     }
  357.                     $result .= '</td></tr></table>';
  358.                 }
  359.                 if (isset($main_template_item))
  360.                 {
  361.                     $main_template_content =  $this->parse_template($main_template_item['content']);
  362.                     $page .= str_replace($this->get_tag('<content', $main_template_content), $result, $main_template_content);
  363.                 }else
  364.                 {
  365.                     $page .= $this->print_search_words();
  366.                 }
  367.  
  368.  
  369.             break;
  370.  
  371.             default:
  372.  
  373.                 if ($file_id > 0)
  374.                 {
  375.                     $content = $this->parse_template($file['content']);
  376.                     require_once($GO_CONFIG->class_path.'highlighting_skipphtml.class.inc');
  377.  
  378.                     $hl = new highlighting_skipphtml();
  379.  
  380.                     $content = $hl->dohighlight($this->search_words,$content,'<span class="search">','</span>',1);
  381.  
  382.                     if (!$this->publish_static)
  383.                     {
  384.                         $content = str_replace($GO_CONFIG->host.$GO_MODULES->f('path').'view.php', $_SERVER['PHP_SELF'], $content);
  385.                     }
  386.                     if (isset($main_template_item))
  387.                     {
  388.                         if ($main_template_item)
  389.                         {
  390.                             $main_template_content =  $this->parse_template($main_template_item['content']);
  391.                             $page .= str_replace($this->get_tag('<content', $main_template_content), $content, $main_template_content);
  392.                         }else
  393.                         {
  394.                             $page .= $content;
  395.                         }
  396.                     }else
  397.                     {
  398.                         $page .= $content;
  399.                     }
  400.                 }else
  401.                 {
  402.                     //get the language file
  403.                     require($GO_LANGUAGE->get_language_file('cms'));
  404.                     if (isset($main_template_item))
  405.                     {
  406.                         $main_template_content =  $this->parse_template($main_template_item['content']);
  407.                         $page .= str_replace($this->get_tag('<content', $main_template_content), $cms_empty, $main_template_content);
  408.                     }else
  409.                     {
  410.                         $page .= $cms_empty;
  411.                     }
  412.                 }
  413.             break;
  414.         }
  415.  
  416.         $page .= '</body>
  417.         </html>
  418.         ';
  419.  
  420.         if ($this->publish_static)
  421.         {
  422.             for ($i=0; $i < count($this->template_file_replacements);$i++)
  423.             {
  424.                 $page = str_replace($this->template_file_replacements[$i]['dynamic'], $this->template_file_replacements[$i]['static'], $page);
  425.             }
  426.         }
  427.  
  428.         return $page;
  429.     }
  430.  
  431.     function create_url($folder_id,$file_id=0)
  432.     {
  433.         $cms_site = new cms_site($this->site['id']);
  434.  
  435.         if ($this->publish_static)
  436.         {
  437.             $url = '';
  438.             if ($file_id == 0 && $folder_id > 0)
  439.             {
  440.                 $cms_site->get_files($folder_id, 'priority', 'ASC');
  441.                 while($cms_site->next_record())
  442.                 {
  443.                     if (eregi('htm',$cms_site->f('extension')))
  444.                     {
  445.                         $file_id = $cms_site->f('id');
  446.                         break;
  447.                     }
  448.                 }
  449.             }
  450.  
  451.             if ($file_id > 0)
  452.             {
  453.                 $file = $cms_site->get_file($file_id);
  454.                 $folder_id = $file['folder_id'];
  455.                 if ($folder_id != $this->site['root_folder_id'])
  456.                 {
  457.                     $url = urlencode(urlencode($file['name']));
  458.                 }else
  459.                 {
  460.                     $url = 'index.html';
  461.                 }
  462.             }
  463.  
  464.             if ($folder_id > 0 && $folder_id != $this->site['root_folder_id'])
  465.             {
  466.                 while($folder_id != $this->site['root_folder_id'])
  467.                 {
  468.                     $folder = $cms_site->get_folder($folder_id);
  469.                     $url = urlencode(urlencode($folder['name'])).'/'.$url;
  470.                     $folder_id = $folder['parent_id'];
  471.                 }
  472.             }
  473.             return $this->publish_url.$url;
  474.         }else
  475.         {
  476.             return $_SERVER['PHP_SELF'].'?folder_id='.$folder_id.'&file_id='.$file_id;
  477.         }
  478.     }
  479.  
  480.     function print_icon()
  481.     {
  482.         global $extension, $GO_CONFIG;
  483.         return  '<img border="0" align="absmiddle" src="'.$GO_CONFIG->control_url.'icon.php?extension='.$extension.'" />';
  484.     }
  485.  
  486.     function print_home($text, $template_name, $active_template_name, $class, $active_class)
  487.     {
  488.         global $folder_id;
  489.         $home = '';
  490.  
  491.         if ($folder_id == $this->site['root_folder_id'])
  492.         {
  493.             $template = $this->get_template_item_by_name($this->site['template_id'], $active_template_name);
  494.             $class=$active_class;
  495.         }else
  496.         {
  497.             $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  498.         }
  499.         if ($template)
  500.         {
  501.             $home = $this->parse_template($template['content']);
  502.         }
  503.  
  504.         return str_replace($this->get_tag('<content', $home), '<a class="'.$class.'" href="'.$this->create_url($this->site['root_folder_id']).'">'.$text.'</a>', $home);
  505.     }
  506.  
  507.     function print_path($template_name, $item_template_name, $active_item_template_name, $class, $active_class)
  508.     {
  509.         global $folder_id;
  510.         $path = '';
  511.         $id = $folder_id;
  512.  
  513.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  514.         $item_template_passive = $this->get_template_item_by_name($this->site['template_id'], $item_template_name);
  515.         $item_template_active = $this->get_template_item_by_name($this->site['template_id'], $active_item_template_name);
  516.  
  517.         $folder = $this->get_folder($id);
  518.  
  519.         while($folder['parent_id'] != 0)
  520.         {
  521.             $item_template = ($folder_id == $id) ? $item_template_active : $item_template_passive;
  522.             $_class = ($folder_id == $id) ? $active_class : $class;
  523.             if ($item_template)
  524.             {
  525.                 $item_template_content = $this->parse_template($item_template['content']);
  526.                 $path = str_replace($this->get_tag('<content', $item_template_content), '<a class="'.$class.'" href="'.$this->create_url($folder['id']).'">'.$folder['name'].'</a>', $item_template_content).$path;
  527.             }else
  528.             {
  529.                 $path = '<a class="'.$_class.'" href="'.$this->create_url($folder['id']).'">'.$folder['name'].'</a>'.$path;
  530.             }
  531.  
  532.             $id = $folder['parent_id'];
  533.             $folder = $this->get_folder($id);
  534.         }
  535.  
  536.         if ($template)
  537.         {
  538.             $template_content = $this->parse_template($template['content']);
  539.             $path = str_replace($this->get_tag('<content', $template_content), $path, $template_content);
  540.         }
  541.  
  542.         return $path;
  543.     }
  544.  
  545.     function print_active_doc($template_name, $class)
  546.     {
  547.         global $file_id, $extension;
  548.  
  549.         $current_file = '';
  550.  
  551.         if(isset($file_id) && $file = $this->get_file($file_id))
  552.         {
  553.             $extension = get_extension($file['name']);
  554.             $title = ($file['title'] == '') ? strip_extension($file['name']) : $file['title'];
  555.  
  556.             if ($template = $this->get_template_item_by_name($this->site['template_id'], $template_name))
  557.             {
  558.                 $current_file = $this->parse_template($template['content']);
  559.             }
  560.  
  561.             $current_file = str_replace($this->get_tag('<content', $current_file), '<span class="'.$class.'">'.$title.'</span>', $current_file);
  562.         }
  563.         return $current_file;
  564.     }
  565.  
  566.     function print_folders($template_name, $item_template_name, $class, $print_rootfolders='true')
  567.     {
  568.         global $folder_id;
  569.         $folders = '';
  570.         $item_template = $this->get_template_item_by_name($this->site['template_id'], $item_template_name);
  571.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  572.  
  573.         if ($print_rootfolders == 'true' || $folder_id != $this->site['root_folder_id'])
  574.         {
  575.             $count = $this->get_visible_folders($folder_id);
  576.             if ($count > 0)
  577.             {
  578.                 while($this->next_record())
  579.                 {
  580.                     if ($item_template)
  581.                     {
  582.                         $item_template_content =  $this->parse_template($item_template['content']);
  583.                         $folders .= str_replace($this->get_tag('<content', $item_template_content),'<a class="'.$class.'" href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a>', $item_template_content);
  584.                     }else
  585.                     {
  586.                         $folders .= '<a class="'.$class.'" href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a>';
  587.                     }
  588.                 }
  589.                 if ($template)
  590.                 {
  591.                     $template_content = $this->parse_template($template['content']);
  592.                     $folders = str_replace($this->get_tag('<content', $template_content), $folders, $template_content);
  593.                 }
  594.             }
  595.         }
  596.         return $folders;
  597.     }
  598.  
  599.     function print_back($text, $template_name, $class, $print_rootfolders='true')
  600.     {
  601.         global $folder_id;
  602.         $back = '';
  603.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  604.  
  605.         $folder = $this->get_folder($folder_id);
  606.  
  607.         if ($folder['parent_id'] != 0 && ($print_rootfolders == 'true' || $folder['parent_id'] != $this->site['root_folder_id']))
  608.         {
  609.             $back = '<a class="'.$class.'" href="'.$this->create_url($folder['parent_id']).'">'.$text.'</a>';
  610.             if ($template)
  611.             {
  612.                 $template_content = $this->parse_template($template['content']);
  613.                 $back = str_replace($this->get_tag('<content', $template_content), $back, $template_content);
  614.             }
  615.         }
  616.         return $back;
  617.     }
  618.  
  619.     function print_rootfolders($template_name, $item_template_name, $active_item_template_name, $class, $active_class)
  620.     {
  621.         global $folder_id;
  622.         $folders = '';
  623.         $active_item_template = $this->get_template_item_by_name($this->site['template_id'], $active_item_template_name);
  624.         $passive_item_template = $this->get_template_item_by_name($this->site['template_id'], $item_template_name);
  625.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  626.  
  627.         $count = $this->get_visible_folders($this->site['root_folder_id']);
  628.         if ($count > 0)
  629.         {
  630.  
  631.             while($this->next_record())
  632.             {
  633.                 $item_template = ($this->f('id') == $folder_id) ? $active_item_template : $passive_item_template;
  634.                 $_class = ($this->f('id') == $folder_id) ? $active_class : $class;
  635.  
  636.                 if ($item_template)
  637.                 {
  638.                     $item_template_content =  $this->parse_template($item_template['content']);
  639.                     $folders .= str_replace($this->get_tag('<content', $item_template_content),'<a class="'.$_class.'" href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a>', $item_template_content);
  640.                 }else
  641.                 {
  642.                     $folders .= '<a class="'.$_class.'" href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a>';
  643.                 }
  644.             }
  645.             if ($template)
  646.             {
  647.                 $template_content = $this->parse_template($template['content']);
  648.                 $folders = str_replace($this->get_tag('<content', $template_content), $folders, $template_content);
  649.             }
  650.         }
  651.         return $folders;
  652.     }
  653.  
  654.     function print_folderlist($id, $path)
  655.     {
  656.         global $folder_id, $file_id, $GO_CONFIG, $GO_MODULES;
  657.  
  658.         $folders = '';
  659.         if ($resolved_path = $this->resolve_url($path, $this->site['id']))
  660.         {
  661.             $count = $this->get_visible_folders($resolved_path['folder_id']);
  662.             if ($count > 0)
  663.             {
  664.                 $folders .= '<ul id="'.$id.'">';
  665.                 while($this->next_record())
  666.                 {
  667.                     if ($this->f('id') == $folder_id)
  668.                     {
  669.                         $folders .= '<li><strong><a href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a></strong>';
  670.                         $cms = new cms();
  671.  
  672.                         $folder_count = $cms->get_visible_folders($this->f('id'));
  673.                         if ($folder_count > 0)
  674.                         {
  675.                             $folders .= '<ul>';
  676.                             while($cms->next_record())
  677.                             {
  678.                                 $folders .= '<li><a href="'.$this->create_url($cms->f('id')).'">'.$cms->f('name').'</a></li>';
  679.                             }
  680.                         }
  681.  
  682.                         $file_count = $cms->get_files($folder_id, 'priority', 'ASC');
  683.  
  684.                         if ($file_count > 1)
  685.                         {
  686.                             $browser = detect_browser();
  687.  
  688.                             $cms_module = $GO_MODULES->get_module('cms');
  689.                             if ($folder_count == 0)
  690.                             {
  691.                                 $folders .= '<ul>';
  692.                             }
  693.                             if ($browser['name'] == 'MSIE')
  694.                             {
  695.                                 $target = '_blank';
  696.                             }else
  697.                             {
  698.                                 $target = '_self';
  699.                             }
  700.  
  701.                             while($cms->next_record())
  702.                             {
  703.                                 if ($file_id == $cms->f('id'))
  704.                                 {
  705.                                     $folders .= '<li><strong>';
  706.                                 }else
  707.                                 {
  708.                                     $folders .= '<li>';
  709.                                 }
  710.                                 $extension = $cms->f('extension');
  711.  
  712.                                 if ($this->publish_static || eregi('htm',$cms->f('extension')))
  713.                                 {
  714.                                     $folders .= '<a href="'.$this->create_url($folder_id, $cms->f('id')).'">'.strip_extension($cms->f('name')).'</a>';
  715.                                 }else
  716.                                 {
  717.                                     $folders .= '<a target="'.$target.'" href="'.$GO_CONFIG->host.$cms_module['path'].'download.php?site_id='.$this->site['id'].'&file_id='.$cms->f('id').'">'.strip_extension($cms->f('name')).'</a>';
  718.                                 }
  719.  
  720.                                 if ($file_id == $cms->f('id'))
  721.                                 {
  722.                                     $folders .= '</strong></li>';
  723.                                 }else
  724.                                 {
  725.                                     $folders .= '</li>';
  726.                                 }
  727.                             }
  728.                             if ($folder_count > 0 || $file_count > 0)
  729.                             {
  730.                                 $folders .= '</ul>';
  731.                             }
  732.                             $folders .='</li>';
  733.                         }
  734.                     }else
  735.                     {
  736.                         $folders .= '<li><a href="'.$this->create_url($this->f('id')).'">'.$this->f('name').'</a></li>';
  737.                     }
  738.                 }
  739.  
  740.                 $folders .= '</ul>';
  741.             }
  742.             return $folders;
  743.         }
  744.     }
  745.  
  746.     function print_files($template_name, $item_template_name, $active_item_template_name, $class, $active_class, $print_single=false)
  747.     {
  748.         global $folder_id, $file_id, $GO_CONFIG, $extension, $GO_MODULES;
  749.  
  750.         $cms_module = $GO_MODULES->get_module('cms');
  751.  
  752.         $files = '';
  753.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  754.         $item_template_passive = $this->get_template_item_by_name($this->site['template_id'], $item_template_name);
  755.         $item_template_active = $this->get_template_item_by_name($this->site['template_id'], $active_item_template_name);
  756.  
  757.         $count = $this->get_files($folder_id, 'priority', 'ASC');
  758.         $browser = detect_browser();
  759.         if ($browser['name'] == 'MSIE')
  760.         {
  761.             $target = '_blank';
  762.         }else
  763.         {
  764.             $target = '_self';
  765.         }
  766.  
  767.         if ($count > 1 || ($count == 1 && $print_single == 'true'))
  768.         {
  769.             while($this->next_record())
  770.             {
  771.                 $extension = $this->f('extension');
  772.  
  773.                 $item_template = ($file_id == $this->f('id')) ? $item_template_active : $item_template_passive;
  774.                 $_class = ($file_id == $this->f('id')) ? $active_class : $class;
  775.  
  776.                 if ($this->publish_static || eregi('htm',$this->f('extension')))
  777.                 {
  778.                     $content = '<a class="'.$_class.'" href="'.$this->create_url($folder_id, $this->f('id')).'">'.strip_extension($this->f('name')).'</a>';
  779.                 }else
  780.                 {
  781.                     $content = '<a target="'.$target.'" class="'.$_class.'" href="'.$GO_CONFIG->host.$cms_module['path'].'download.php?site_id='.$this->site['id'].'&file_id='.$this->f('id').'">'.strip_extension($this->f('name')).'</a>';
  782.                 }
  783.                 if ($item_template)
  784.                 {
  785.                     $item_template_content = $this->parse_template($item_template['content']);
  786.                     $files .= str_replace($this->get_tag('<content', $item_template_content),$content, $item_template_content);
  787.                 }else
  788.                 {
  789.                     $files .= $content;
  790.                 }
  791.             }
  792.  
  793.             if ($template)
  794.             {
  795.                 $template_content = $this->parse_template($template['content']);
  796.                 $files = str_replace($this->get_tag('<content', $template_content), $files, $template_content);
  797.             }
  798.         }
  799.         return $files;
  800.     }
  801.  
  802.     function print_login($text, $template_name, $class)
  803.     {
  804.         global $GO_CONFIG;
  805.  
  806.         $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  807.         $login = '<a class="'.$class.'" href="javascript:popup(\''.$GO_CONFIG->host.'\');">'.$text.'</a>';
  808.         if($template)
  809.         {
  810.             $template_content = $this->parse_template($template['content']);
  811.             $login = str_replace($this->get_tag('<content', $template_content), $login, $template_content);
  812.         }
  813.         return $login;
  814.     }
  815.  
  816.     function get_attributes($tag)
  817.     {
  818.         $attributes = array();
  819.         $in_value = false;
  820.         $in_name = false;
  821.         $name = '';
  822.         $value = '';
  823.         $length = strlen($tag);
  824.         for($i=0;$i<$length;$i++)
  825.         {
  826.             $char = $tag[$i];
  827.             switch ($char)
  828.             {
  829.                 case '"':
  830.                     if ($in_value)
  831.                     {
  832.                         $in_value = false;
  833.  
  834.                         $attributes[trim($name)] = trim($value);
  835.                         $name = '';
  836.                         $value= '';
  837.                     }else
  838.                     {
  839.                         $i++;
  840.                         $char = $tag[$i];
  841.                         $in_value = true;
  842.                     }
  843.  
  844.                 break;
  845.  
  846.                 case ' ':
  847.                     $i++;
  848.                     $char = $tag[$i];
  849.                     $in_name = true;
  850.                 break;
  851.  
  852.                 case '=':
  853.                     $in_name = false;
  854.                 break;
  855.             }
  856.  
  857.             if ($in_name)
  858.             {
  859.                 $name .= $char;
  860.             }
  861.  
  862.             if ($in_value)
  863.             {
  864.                 $value .= $char;
  865.             }
  866.         }
  867.         return $attributes;
  868.     }
  869.  
  870.     function print_search_words()
  871.     {
  872.         $this->search_words = '';
  873.         $columns=3;
  874.         $column_count = 0;
  875.         $last_char = '';
  876.         $this->get_search_words($this->site['id']);
  877.  
  878.         $this->search_words .= '<table border="0" cellspacing="8"><tr>';
  879.         while ($this->next_record())
  880.         {
  881.             $name = $this->f('search_word');
  882.             $char = strtoupper($name[0]);
  883.             if ($char != $last_char)
  884.             {
  885.                 if ($last_char != '')
  886.                 {
  887.                     $this->search_words .= '</td>';
  888.                     $column_count++;
  889.                 }
  890.  
  891.                 if ($column_count == $columns)
  892.                 {
  893.                     $this->search_words .= '</tr><tr>';
  894.                     $column_count=0;
  895.                 }
  896.  
  897.                 $this->search_words .= '<td valign="top" width="150"><h3>'.$char.'</h3>';
  898.             }
  899.  
  900.             $this->search_words .= '<table border="0" cellpadding="0" cellspacing="2"><tr><td><a href="'.$_SERVER['PHP_SELF'].'?task=search_results&search_word_id='.$this->f('id').'">'.$name.'</a></td>';
  901.             $this->search_words .= '</tr></table>';
  902.             $last_char = $char;
  903.         }
  904.  
  905.         for ($i=$column_count+1;$i<$columns;$i++)
  906.         {
  907.             $this->search_words .= '</td><td width="150"> ';
  908.         }
  909.  
  910.         $this->search_words .= '</td></tr></table><br />';
  911.         return $this->search_words;
  912.     }
  913.  
  914.     function print_search($text, $template_name, $active_template_name, $class, $active_class)
  915.     {
  916.         global $GO_MODULES, $GO_CONFIG, $task;
  917.  
  918.         $cms_module = $GO_MODULES->get_module('cms');
  919.  
  920.         if (isset($task) && $task == 'search')
  921.         {
  922.             $template = $this->get_template_item_by_name($this->site['template_id'], $active_template_name);
  923.             $class = $active_class;
  924.         }else
  925.         {
  926.             $template = $this->get_template_item_by_name($this->site['template_id'], $template_name);
  927.         }
  928.         if($this->publish_static)
  929.         {
  930.             $search = '<a class="'.$class.'" href="'.$this->publish_url.'_search/index.php?task=search&site_id='.$this->site['id'].'">'.$text.'</a>';
  931.         }else
  932.         {
  933.             $search = '<a class="'.$class.'" href="'.$GO_CONFIG->host.$cms_module['path'].'view.php?task=search&site_id='.$this->site['id'].'">'.$text.'</a>';
  934.         }
  935.         if($template)
  936.         {
  937.             $template_content = $this->parse_template($template['content']);
  938.             $search = str_replace($this->get_tag('<content', $template_content), $search, $template_content);
  939.         }
  940.  
  941.         return $search;
  942.     }
  943.  
  944.     function print_document($path)
  945.     {
  946.         global $GO_CONFIG, $GO_MODULES;
  947.  
  948.         if ($resolved_path = $this->resolve_url($path, $this->site['id']))
  949.         {
  950.             if ($resolved_path['file_id'] > 0)
  951.             {
  952.                 $file = $this->get_file($resolved_path['file_id']);
  953.                 $content = $this->parse_template($file['content']);
  954.                 $content = preg_replace($this->search_words, '<span class="search">\\0</span>', $content);
  955.                 $content = str_replace($GO_CONFIG->host.$GO_MODULES->f('path').'view.php', $_SERVER['PHP_SELF'], $content);
  956.                 return $content;
  957.             }else
  958.             {
  959.                 return "<p><b>Error: Could not resolve file from '$path'</b></p>";
  960.             }
  961.  
  962.         }else
  963.         {
  964.             return "<p><b>Error: Could not resolve url to '$path'</b></p>";
  965.         }
  966.     }
  967.  
  968.     function get_tag($tag, $content)
  969.     {
  970.         $start_pos = strpos($content, $tag);
  971.         if($start_pos !== false)
  972.         {
  973.             $content_length= strlen($content);
  974.             $tag_length=0;
  975.             $char=$content[$start_pos+$tag_length];
  976.             $tag = $char;
  977.             while($char != '>' && ($start_pos+$tag_length) < $content_length)
  978.             {
  979.                 $tag_length++;
  980.                 $char = $content[$start_pos+$tag_length];
  981.                 $tag .= $char;
  982.             }
  983.             return $tag;
  984.         }
  985.         return false;
  986.     }
  987.  
  988.     function parse_template($content)
  989.     {
  990.         while($tag = $this->get_tag('<home', $content))
  991.         {
  992.             $attributes = $this->get_attributes($tag);
  993.             $text = isset($attributes['text']) ? $attributes['text'] : '';
  994.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  995.             $active_template = isset($attributes['active_template']) ? $attributes['active_template'] : '';
  996.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  997.             $active_class = isset($attributes['active_class']) ? $attributes['active_class'] : '';
  998.  
  999.             $content = str_replace($tag, $this->print_home($text, $template, $active_template, $class, $active_class), $content);
  1000.         }
  1001.  
  1002.         while($tag = $this->get_tag('<folders', $content))
  1003.         {
  1004.             $attributes = $this->get_attributes($tag);
  1005.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1006.             $item_template = isset($attributes['item_template']) ? $attributes['item_template'] : '';
  1007.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1008.             $print_rootfolders = isset($attributes['print_rootfolders']) ? $attributes['print_rootfolders'] : 'true';
  1009.             $content = str_replace($tag, $this->print_folders($template, $item_template, $class, $print_rootfolders), $content);
  1010.         }
  1011.  
  1012.         while($tag = $this->get_tag('<rootfolders', $content))
  1013.         {
  1014.             $attributes = $this->get_attributes($tag);
  1015.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1016.             $item_template = isset($attributes['item_template']) ? $attributes['item_template'] : '';
  1017.             $active_item_template = isset($attributes['active_item_template']) ? $attributes['active_item_template'] : '';
  1018.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1019.             $active_class = isset($attributes['active_class']) ? $attributes['active_class'] : '';
  1020.             $content = str_replace($tag, $this->print_rootfolders($template, $item_template, $active_item_template, $class, $active_class), $content);
  1021.         }
  1022.  
  1023.         while($tag = $this->get_tag('<files', $content))
  1024.         {
  1025.             $attributes = $this->get_attributes($tag);
  1026.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1027.             $item_template = isset($attributes['item_template']) ? $attributes['item_template'] : '';
  1028.             $active_item_template = isset($attributes['active_item_template']) ? $attributes['active_item_template'] : '';
  1029.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1030.             $active_class = isset($attributes['active_class']) ? $attributes['active_class'] : '';
  1031.             $print_single = isset($attributes['print_single']) ? $attributes['print_single'] : 'false';
  1032.             $content = str_replace($tag, $this->print_files($template, $item_template, $active_item_template, $class, $active_class, $print_single), $content);
  1033.         }
  1034.  
  1035.         while($tag = $this->get_tag('<path', $content))
  1036.         {
  1037.             $attributes = $this->get_attributes($tag);
  1038.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1039.             $item_template = isset($attributes['item_template']) ? $attributes['item_template'] : '';
  1040.             $active_item_template = isset($attributes['active_item_template']) ? $attributes['active_item_template'] : '';
  1041.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1042.             $active_class = isset($attributes['active_class']) ? $attributes['active_class'] : '';
  1043.             $content = str_replace($tag, $this->print_path($template, $item_template, $active_item_template, $class, $active_class), $content);
  1044.         }
  1045.  
  1046.         while($tag = $this->get_tag('<icon', $content))
  1047.         {
  1048.             $content = str_replace($tag, $this->print_icon(), $content);
  1049.         }
  1050.  
  1051.         while($tag = $this->get_tag('<login', $content))
  1052.         {
  1053.             $attributes = $this->get_attributes($tag);
  1054.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1055.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1056.             $text = isset($attributes['text']) ? $attributes['text'] : '';
  1057.             $content = str_replace($tag, $this->print_login($text,$template, $class), $content);
  1058.         }
  1059.  
  1060.         while($tag = $this->get_tag('<search', $content))
  1061.         {
  1062.             $attributes = $this->get_attributes($tag);
  1063.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1064.             $active_template = isset($attributes['active_template']) ? $attributes['active_template'] : '';
  1065.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1066.             $active_class = isset($attributes['active_class']) ? $attributes['active_class'] : '';
  1067.             $text = isset($attributes['text']) ? $attributes['text'] : '';
  1068.             $content = str_replace($tag, $this->print_search($text,$template, $active_template, $class, $active_class), $content);
  1069.         }
  1070.  
  1071.         while($tag = $this->get_tag('<back', $content))
  1072.         {
  1073.             $attributes = $this->get_attributes($tag);
  1074.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1075.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1076.             $text = isset($attributes['text']) ? $attributes['text'] : '';
  1077.             $print_rootfolders = isset($attributes['print_rootfolders']) ? $attributes['print_rootfolders'] : '';
  1078.             $content = str_replace($tag, $this->print_back($text,$template, $class, $print_rootfolders), $content);
  1079.         }
  1080.  
  1081.         while($tag = $this->get_tag('<active_doc', $content))
  1082.         {
  1083.             $attributes = $this->get_attributes($tag);
  1084.             $template = isset($attributes['template']) ? $attributes['template'] : '';
  1085.             $class = isset($attributes['class']) ? $attributes['class'] : '';
  1086.             $content = str_replace($tag, $this->print_active_doc($template, $class), $content);
  1087.         }
  1088.  
  1089.         while($tag = $this->get_tag('<document', $content))
  1090.         {
  1091.             $attributes = $this->get_attributes($tag);
  1092.             $path = isset($attributes['path']) ? $attributes['path'] : '';
  1093.             $content = str_replace($tag, $this->print_document($path), $content);
  1094.         }
  1095.  
  1096.         while($tag = $this->get_tag('<folderlist', $content))
  1097.         {
  1098.             $attributes = $this->get_attributes($tag);
  1099.             $path = isset($attributes['path']) ? $attributes['path'] : '';
  1100.             $id = isset($attributes['id']) ? $attributes['id'] : '';
  1101.             $content = str_replace($tag, $this->print_folderlist($id, $path), $content);
  1102.         }
  1103.         return $content;
  1104.  
  1105.     }
  1106.  
  1107. }
  1108. ?>