Hallo,
habe mir das folgende Script gezogen. Es soll meinen Opera-Bookmark-File einlesen und dann als HTML-Seite ausgeben. Ich weiß halt eben nur nicht wie ich den generierten Code ausgeben kann. Relevant sind meiner Meinung nach hierfür lediglich die letzten Zeilen des Scripts.
Kann mir jemand weiterhelfen ?
habe mir das folgende Script gezogen. Es soll meinen Opera-Bookmark-File einlesen und dann als HTML-Seite ausgeben. Ich weiß halt eben nur nicht wie ich den generierten Code ausgeben kann. Relevant sind meiner Meinung nach hierfür lediglich die letzten Zeilen des Scripts.
Kann mir jemand weiterhelfen ?
PHP-Code:
<?php
//---Private vars---
var $filename = "";
var $file = 0;
var $folders = array();
var $output = "";
var $fld_personalbar = "Personal bar";
var $fld_findresults = "Find results";
var $dofilter = FALSE;
//-------------------------------------------------------------------------
//---Public functions------------------------------------------------------
//-------------------------------------------------------------------------
/**
* Convert the given hotlist file into HTML code.
*
* Returns the Opera hotlist as HTML code created by using the "templates
* (tpl_xxx vars).
* Can be collaed more than once for different hotlist files.
*
* @param string $hotlistfile URL of the Opera hotlist file
* @param boolean $withfolderlist Enables/disables the list of folders at the end
* @param string $customtitle Title of the output bookmark list
* @return string
* @access public
*/
function ConvertToHtml( $hotlistfile = "opera6.adr", $withfolderlist = FALSE, $customtitle = "Opera Hotlist" ) {
// Check file
if ((trim($hotlistfile) == "") || !file_exists($hotlistfile)) return FALSE;
$this->filename = $hotlistfile;
// Init variables
$this->folders = array();
$this->output = "";
$arr = array();
$dofilter = (strlen($this->folderfilter) > 0);
// Load bookmark file
$this->file = fopen($this->filename, "r");
if ($this->file === FALSE) return FALSE;
// Check if valid bookmark file
$line = $this->getNextLine();
if ($line != "Opera Hotlist version 2.0") {
fclose($this->file);
return FALSE;
}
if (strlen($this->tpl_header) > 0) {
$this->addLine(str_replace('{TITLE}', $customtitle, $this->tpl_header));
}
if ($withfolderlist) $this->addLine($this->tpl_fldlistlink);
$this->addLine($this->tpl_beginlist);
$arr = $this->parseFolder(FALSE);
fclose($this->file);
$this->outputArray($arr);
if (strlen($this->tpl_footer) > 0) {
$this->addLine(str_replace('{TITLE}', $customtitle, $this->tpl_footer));
}
if ($withfolderlist) {
$this->addLine($this->tpl_fldlistheader);
while (list($key, $folder) = each($this->folders)) {
$this->addLine(str_replace("{FOLDER}", $folder, $this->tpl_fldlistentry));
}
$this->addLine($this->tpl_fldlistfooter);
}
return $this->output;
}
/**
* Set all template variables for the html output at once.
*
* Set all template variables (tpl_xxx) which are used for the html
* output at once.
* Note that some of the template vars uses placeholders like {TITLE}.
* e.g. $tpl_arr = array("tpl_header" -> "<h1>{TITLE}</h1>\n", ...);
*
* @param array $tpl_arr Array of the template vars
* @access public
*/
function setTemplateVars($tpl_arr) {
$this->tpl_header = $tpl_arr["tpl_header"];
$this->tpl_footer = $tpl_arr["tpl_footer"];
$this->tpl_folder = $tpl_arr["tpl_folder"];
$this->tpl_entry = $tpl_arr["tpl_entry"];
$this->tpl_beginlist = $tpl_arr["tpl_beginlist"];
$this->tpl_endlist = $tpl_arr["tpl_endlist"];
$this->tpl_fldlistlink = $tpl_arr["tpl_fldlistlink"];
$this->tpl_fldlistheader = $tpl_arr["tpl_fldlistheader"];
$this->tpl_fldlistfooter = $tpl_arr["tpl_fldlistfooter"];
$this->tpl_fldlistentry = $tpl_arr["tpl_fldlistentry"];
}
//-------------------------------------------------------------------------
//---Private functions-----------------------------------------------------
//-------------------------------------------------------------------------
/**
* Returns the next line of the file
*
* @return string
* @access private
*/
function getNextLine() {
if (feof($this->file)) {
return "";
} else {
return trim(utf8_decode(fgets($this->file, 4096)));
}
}
/**
* Adds the line to the output code
*
* @param string $line
* @access private
*/
function addLine( $line ) {
$this->output .= $line;
}
/**
* Parse the next URL from the hotlist and returns an output line
*
* @return string
* @access private
*/
function parseURL () {
$line = $this->getNextLine();
$bookmarkname = "";
$url = "";
while (!feof($this->file) and ($line != "")) {
if (!(strpos($line, "=") === FALSE)) {
list($name, $value) = split("=", $line, 2);
if ($name == "NAME") $bookmarkname = $value;
if ($name == "URL") $url = $value;
}
$line = $this->getNextLine();
}
$bookmarkname = htmlentities($bookmarkname);
return preg_replace(array('({URL})', '({BOOKMARKNAME})'),
array($url, $bookmarkname),
$this->tpl_entry);
}
function add_lah(&$arr, $line) {
if ($this->linksafterheader) {
array_push($arr, $line);
} else {
$this->addLine($line);
}
}
/**
* Parse all the folder entries and the subfolders
*
* @param boolean $withfolder if the folder data shuold be added to the output
* @return array
* @access private
*/
function parseFolder( $withfolder = TRUE ) {
$arr = array();
$arrf = array();
$filtered = FALSE;
$filtercount = 0;
if ($withfolder) {
// Seek to the next folder line
$foldername = "";
$line = $this->getNextLine();
while (!feof($this->file) and ($line != "")) {
if (!(strpos($line, "=") === FALSE)) {
list($name, $value) = split("=", $line);
if ($name == "NAME") $foldername = $value;
}
$line = $this->getNextLine();
} //while
$foldername = htmlentities($foldername);
// Check if foldername matches the folderfilter
if ($dofilter) {
$filtered = preg_match($this->folderfilter, $foldername) ||
preg_match("/^$this->fld_findresults|^$this->fld_personalbar/", $foldername);
} else {
$filtered = preg_match("/^$this->fld_findresults|^$this->fld_personalbar/", $foldername);
}
if (!$filtered) {
// Folder header
array_push($this->folders, $foldername);
$this->add_lah($arr, str_replace('{FOLDERNAME}', $foldername, $this->tpl_folder));
$this->add_lah($arr, $this->tpl_beginlist);
}
}
if ($this->linksafterheader && !$filtered) {
array_push($arr, $arrf);
$idx = count($arr) - 1;
}
// Loop throught the lines
while (!feof($this->file)) {
$line = $this->getNextLine();
switch ($line) {
case "#FOLDER":
if ($filtered && $this->filterwithsubs) {
$filtercount++;
} else {
array_push($arr, $this->parseFolder());
}
break;
case "#URL":
if (!$filtered) $this->add_lah($arrf, $this->parseURL());
break;
case "-":
if ($filtered && $this->filterwithsubs && ($filtercount > 0)) {
$filtercount--;
break;
} else {
break 2;
}
}
}
// Folder footer
if ($withfolder && !$filtered) $this->add_lah($arr, $this->tpl_endlist);
if ($this->linksafterheader && !$filtered) $arr[$idx] = $arrf;
return $arr;
}
/**
* Parse the complete hotlist file
*
* @access private
*/
function parse() {
while (!feof($this->file)) {
$line = $this->getNextLine();
switch ($line) {
case "#FOLDER":
$this->parseFolder();
break;
case "#URL":
$this->parseURL();
break;
}
}
}
/**
* Write the stored data to the output var
*
* @param array $outarray the array holding the formatted html lines
* @access private
*/
function outputArray( $outarray ) {
while (list($name, $value) = each($outarray)) {
if (!is_array($value)) {
$this->addLine($value);
} else {
$this->outputArray($value);
}
}
}
}
?>
Kommentar