$item)
{
if (is_array($item))
{
$sections.= '['.$key."]\n";
foreach ($item as $s_key => $s_item)
{
if (is_numeric($s_item) || is_bool($s_item))
$sections.= $s_key.' = '.$s_item."\n";
else
{
$s_item = str_replace("\r", '', $s_item);
$sections.= $s_key.' = "'.str_replace("\n", '\\n', $s_item)."\"\n";
}
}
$sections.= "\n";
}
else
{
if (is_numeric($item) || is_bool($item))
$out.= $key.' = '.$item."\n";
else
{
$item = str_replace("\r", '', $item);
$out.= $key.' = "'.str_replace("\n", '\\n', $item)."\"\n";
}
//$out.= "\n";
}
}
$out.= $sections;
$fp = fopen($file, 'w');
if(!$fp) return false;
fputs($fp, $out);
fclose($fp);
return true;
}
class NanoKubbe
{
private $version = '0.1.0';
private $sessions_started = false;
private $basePath = false;
private $allowed_html = array(
// Inline
'strong'=> array('class'),
'em' => array('class'),
'sup' => array('class'),
'sub' => array('class'),
'span' => array('class'),
'abbr' => array('class', 'title'),
'acronym' => array('class', 'title'),
'a' => array('class', 'href', 'name'),
'img' => array('class', 'src', 'alt', 'width', 'height'),
'code' => array('class'),
'cite' => array('class'),
'del' => array('class'),
'ins' => array('class'),
'kbd' => array('class'),
'samp' => array('class'),
'br' => array(),
// Block
'p' => array('class'),
'blockquote' => array('class'),
'ul' => array('class'),
'li' => array('class'),
'ol' => array('class'),
'h4' => array('class'),
'h5' => array('class'),
'h6' => array('class'),
'pre' => array('class'),
'dl' => array('class'),
'dt' => array('class'),
'dd' => array('class'),
'hr' => array('class'),
// Table elements
'table' => array('class'),
'caption' => array('class'),
'thead' => array('class'),
'tbody' => array('class'),
'tfoot' => array('class'),
'tr' => array('class'),
'td' => array('class', 'rowspan', 'colspan'),
'th' => array('class', 'rowspan', 'colspan'),
);
public function __construct()
{
$base_path = dirname($_SERVER['SCRIPT_NAME']);
$base_path = preg_replace('/\/+$/', '', trim($base_path));
$base_path.= '/';
$this->basePath = $base_path;
}
private function _startSession()
{
if(!$this->sessions_started) {
@session_start();
$this->sessions_started = true;
}
}
public function login($login, $password)
{
if($login != USER_LOGIN)
return false;
if($password != USER_PASSWORD)
return false;
$this->_startSession();
$_SESSION['is_logged'] = true;
return true;
}
public function is_logged()
{
$this->_startSession();
if(!empty($_SESSION['is_logged']))
return true;
return false;
}
public function logout()
{
$this->_startSession();
$_SESSION = array();
session_write_close();
return true;
}
private function _checkPageName($page)
{
if (!preg_match('/^[a-z0-9A-Z\/_-]+$/', $page)) return false;
return true;
}
public function getPage($page)
{
if (!$this->_checkPageName($page)) return false;
if (!file_exists(PATH.$page.'.html'))
return false;
$content = file_get_contents(PATH.$page.'.html');
if (file_exists(PATH.$page.'.ini'))
{
$metas = parse_ini_file_quotes_safe(PATH.$page.'.ini');
}
else
$metas = array();
$metas['name'] = $page;
$metas['date'] = @filemtime(PATH.$page.'.html');
return array('metas' => $metas, 'content' => $content);
}
public function writePage($page, $content, $metas=false)
{
if (!$this->_checkPageName($page)) return false;
$dirs = explode('/', $page);
unset($dirs[count($dirs)-1]);
$path = PATH.'/';
foreach ($dirs as $dir)
{
$path.= $dir;
if (!file_exists($path))
@mkdir($path);
if (!is_dir($path))
die('Pas répertoire?!');
$path.= '/';
}
file_put_contents(PATH.$page.'.html', stripslashes($content));
if (!empty($metas))
write_ini_file(PATH.$page.'.ini', $metas);
return true;
}
public function deletePage($page)
{
if (!$this->_checkPageName($page)) return false;
@unlink(PATH.$page.'.html');
@unlink(PATH.$page.'.ini');
}
public function getPageURI($page)
{
return $this->basePath.$page;
}
public function showPage($page_name)
{
$page = $this->getPage($page_name);
if (!$page) return false;
$data_path = $this->basePath.PATH;
$current_path = dirname($page_name).'/';
if (!empty($page['metas']['template']))
$tpl_file = $page['metas']['template'];
else
$tpl_file = 'default.tpl';
if (empty($page['metas']['title']))
$page['metas']['title'] = preg_replace('/^(.*\/)*([^\/]+)$/', '\\2', $page_name);
$tpl = new jTpl();
$tpl->assign('style', empty($page['metas']['style']) ? 'default.css' : $page['metas']['style']);
$tpl->assign('template', $tpl_file);
$tpl->assign('title', $page['metas']['title']);
$tpl->assign('date', $page['metas']['date']);
$tpl->assign('content', $page['content']);
$tpl->assign('page', $page_name);
require_once('class.blockformat.php');
require_once('class.garbage2xhtml.php');
$g2x = new Garbage2xhtml();
$g2x->allowed_tags =& $this->allowed_html;
$g2x->remove_forbidden_tags = false;
$text = $g2x->Process($page['content']);
// Make paragraphs
$block_format = new BlockFormat();
$text = $block_format->Process($text);
$text = preg_replace('/PAGE\(\/([^\)]+)\)/', $this->basePath.'\\1', $text);
$text = preg_replace('/PAGE\(([^\)]+)\)/', $this->basePath.$current_path.'\\1', $text);
$text = preg_replace('/(IMAGE|DATA|IMG)\(\/([^\)]+)\)/', $data_path.'\\2', $text);
$text = preg_replace('/(IMAGE|DATA|IMG)\(([^\)]+)\)/', $data_path.$current_path.'\\2', $text);
$text = preg_replace('/\[:([^\]]+)\]/', '\\1', $text);
$text = preg_replace('/\[([^\|]+)\|:([^\]]+)\]/', '\\1', $text);
$tpl->assign('content_html', $text);
$tpl->assign('base_path', $this->basePath);
$tpl->assign('data_path', $data_path);
$tpl->assign('logged', $this->is_logged());
define('NK_WEB_PATH', $this->basePath);
if (!headers_sent())
header('HTTP/1.1 200 OK');
$tpl->display($tpl_file);
return true;
}
public function listFiles($dir='.')
{
$dir = preg_replace('/(^\/|\/$)/', '', $dir);
$path = PATH . $dir . '/';
$dirs = array();
$files = array();
$pages = array();
$handle = opendir(PATH . $dir);
while($file = readdir($handle))
{
if (preg_match('/^\./', $file))
continue;
if (is_dir($path.$file))
$dirs[strtolower($file)] = array('type' => 'dir', 'name' => $file, 'path' => $dir.'/'.$file);
else
{
$ext = preg_replace('/.*\.([^\.]+)$/', '\\1', $file);
$name = preg_replace('/^(.+)\.[^\.]*$/', '\\1', $file);
$path2 = $dir . '/' . $file;
switch ($ext)
{
case 'ini':
case 'html':
{
if (!empty($pages[$name]))
{
$type = 'page';
unset($pages[$name]);
$path2 = $dir . '/' . $name;
}
else
{
$pages[$name] = true;
continue 2;
}
break;
}
case 'png':
case 'jpg':
case 'svg':
case 'jpeg':
case 'gif':
{
$type = 'image';
break;
}
case 'txt':
case 'tpl':
case 'css':
{
$type = 'text';
break;
}
default:
$type = 'unknow';
break;
}
$files[strtolower($file)] = array('file' => $file, 'name' => $name, 'type' => $type, 'path' => $path2);
}
}
closedir($handle);
ksort($files);
ksort($dirs);
return array_merge($dirs, $files);
}
public function createDir($base, $dir)
{
$dir = preg_replace('/^[^a-z0-9A-Z_-]+$/', '', $dir);
@mkdir(PATH.$base.'/'.$dir);
return $dir;
}
public function writeFile($file, $content)
{
file_put_contents(PATH.$file, stripslashes($content));
}
public function getFile($file)
{
if (file_exists(PATH.$file))
return file_get_contents(PATH.$file);
return false;
}
public function deleteFile($file)
{
$file = preg_replace('/^[^a-z0-9A-Z\/._-]+$/', '', $file);
if (preg_match('/\.\./', $file)) return false;
if (!file_exists(PATH.$file))
return false;
@unlink(PATH.$file);
}
public function uploadFile($base, $handler)
{
if (empty($handler['tmp_name']) || empty($handler['name']) || empty($handler['size']))
return false;
if ($handler['size'] > (1024 * 1024))
die('TOO_BIG');
$file_name = preg_replace('/^[^a-z0-9A-Z_\.-]+$/', '', $handler['name']);
if (file_exists(PATH.$base.'/'.$file_name))
die('FILE_EXISTS');
if (!@move_uploaded_file($handler['tmp_name'], PATH.$base.'/'.$file_name))
return false;
return true;
}
public function getVersion()
{
return $this->version;
}
}
?>