/**
* The required files
*/
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
/**
* The Writer class used during writing to files on the filesystem
* <code>
* Usage:
* $writer = new Writer();
* $content = $writer->save();
* Or
* $domainname = 'finn-rasmussen.com';
* $language = 'en';
* $filename = 'demo.html';
* $filepath = '.'; // Current directory
* $content = Writer::save($domainname,$language,$filename,$filepath);
* Or
* $file = '/var/www/html/test.php';
* $content = Writer::save($file);
* </code>
*
* @package base
*/
class Writer {
/**
* Constructor
*/
function Writer() {
}
/**
* Returns the name of the cache file name
* If empty name, create a cache file name
* @param String $path The Path, Where to store the cache files
* @param String $className The class name to use for debug info
* @return String The path/name of the cache file
*/
function getCacheFileName($path, $className) {
$cachefilename = '';
if (count($_POST)==0) {
$ext = '';
if (count($_GET)==0) {
// No request params
$cachefilename = ($path!=''?$path:OBJECT_CACHE_PATH).'/'.$className.'-'.md5($className).$ext.'.php';
} else {
/**
* Build the language extension
*/
if (defined('REQUEST_LANGUAGE') && isset($_GET[REQUEST_LANGUAGE]) && $_GET[REQUEST_LANGUAGE]!='') {
switch ($_GET[REQUEST_LANGUAGE]) {
case LANGUAGE_DA:
case LANGUAGE_DE:
case LANGUAGE_EN:
case LANGUAGE_FR:
case LANGUAGE_NO:
case LANGUAGE_SE:
case LANGUAGE_US:
$ext .= '-'.$_GET[REQUEST_LANGUAGE];
break;
default:
// Ignore, unknown language
$msg = $className.'->getCacheFileName($path), not a valid language (da,de,en,se), found '.REQUEST_LANGUAGE.'='.$_GET[REQUEST_LANGUAGE];
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
} else {
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".$msg);
}
break;
}
}
/**
* Build the tab extension
*/
// if (isset($_GET[REQUEST_TAB]) && $_GET[REQUEST_TAB]!='') {
// $ext .= '-'.$_GET[REQUEST_TAB];
// }
$cachefilename = ($path!=''?$path:OBJECT_CACHE_PATH).'/'.$className.'-'.md5($className).$ext.'.php';
}
}
return $cachefilename;
}
/**
* Save the cache file
* @param String $content The html content to save as sql cache
* @param String $cachefile The cachefile to use
* @param String $className The class name to use for debug info
* @return boolean True on succes, false if failure
*/
function save($content, $cachefile, $className='') {
$html = "";
$rc = true;
if (HTTP_USER_AGENT === HTTP_USER_AGENT_P900) {
// Skip Sony Ericsson P900
} else {
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) {
$file = basename(getcwd()).'/'.basename($cachefile);
$html .= "<!--\r\n/"."**\r\n";
$html .= " * Autocreated \tcache file\r\n";
$html .= " * @file \t$file\r\n";
$html .= " * @copyright (c) http:/"."/".MY_SELF.".com\r\n";
$html .= " * @author http:/"."/".MY_SELF.".com\r\n";
$html .= " * @date \t".date('d-M-Y H:i:s')."\r\n";
$html .= " * @classname \t".$className."\r\n";
if (!empty($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']!='') {
$html .= " * @uri \t".Htmlspecialchars::striptags($_SERVER['QUERY_STRING'])."\r\n";
}
$html .= " *"."/\r\n-->\r\n";
}
$html .= $content."\r\n";
$rc = Writer::write($html, $cachefile, $className);
if ($rc) {
$msg = '<!-- '.$className.'->save(), OK, Created: file='.$cachefile.' -->';
if (defined('HTML_LOG_UTIL_PATH')) {
Log::debug(__FILE__,__LINE__,$msg);
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) {
Message::add($msg,__FILE__,__LINE__);
}
} else {
Message::add($msg,__FILE__,__LINE__);
}
} else {
$msg = $className.'->save(), Error Writing: file='.$cachefile;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::error(__FILE__,__LINE__,$msg);
} else {
Message::add($msg,__FILE__,__LINE__);
}
}
}
return $rc;
}
/**
* Write the content to the file opened by fp
* @param String $content The content to write
* @param String $cachefile The cachefile to use
* @param String $className The class name to use for debug info
* @return boolean Returns true on success
*/
function write($content, $cachefile, $className='') {
if ($cachefile!='') {
$fp = fopen($cachefile,'w');
if (!$fp) {
$msg = $className.'->write(), Not found, file='.$cachefile;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
} else {
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n$msg");
}
return false;
} else {
// ok
}
if (!fwrite($fp,$content)) {
$msg = $className.'->write(), Unable to write to file='.$cachefile;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
} else {
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n$msg");
}
return false;
} else {
// ok
}
if (!fclose($fp)) {
$msg = $className.'->write(), Unable to close file='.$cachefile;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
} else {
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n$msg");
}
return false;
} else {
// ok
}
} else {
//print $className."->write(), cachefile is empty<br />\r\n";
}
return true;
}
/**
* Returns the html for the element
* @sample
* @return String The complete html
*/
function getHtml() {
$html = "<h1>Writer.php</h2>\r\n";
$html .= "<p>Write the content of the specified filename and language\r\n";
$html .= "<p>Use default filename, if no language found\r\n";
$html .= "<p>Usage:</p>\r\n";
$html .= "<ul>\r\n";
$html .= '<li>$filename = "/var/doc/root/www/dir/content.txt";</li>'."\r\n";
$html .= '<li>$rc = Write::save($content, $filename);</li>'."\r\n";
$html .= "</ul>\r\n";
$html .= "<p>Write the content of the specified filename</p>\r\n";
$html .= "<p>Usage:</p>\r\n";
$html .= "<ul>\r\n";
$html .= '<li>$rc = Write::write($content, $filename);</li>'."\r\n";
$html .= "</ul>\r\n";
return $html;
}
}
?>
HTML source code
Den fulde HTML kildekode for Writer klassen
<?
<h1>Writer.php</h2>
<p>Write the content of the specified filename and language
<p>Use default filename, if no language found
<p>Usage:</p>
<ul>
<li>$filename = "/var/doc/root/www/dir/content.txt";</li>
<li>$rc = Write::save($content, $filename);</li>
</ul>
<p>Write the content of the specified filename</p>
<p>Usage:</p>
<ul>
<li>$rc = Write::write($content, $filename);</li>
</ul>