?? articletools.php
字號:
<?php
//-------------------------------------------------------------------------
// 程序名稱:神之居所 V1.0
// 作者:竹一木
// 時間:2007-09-22
// demo: http://god.512j.com
// Email:yiwimi@163.com
// Thanks: keep this information if possible.
//-------------------------------------------------------------------------
// string iconv(string in_charset, string out_charset, string str)
// 編碼轉(zhuǎn)換
function gbk2utf($text)
{
return iconv('gbk', 'utf-8', $text);
}
function utf2gbk($text)
{
return iconv('utf-8', 'gbk', $text);
}
//處理大于小于號
function html2text($text)
{
$text = str_replace("\r\n\r\n", "\r\n", $text);
$text = str_replace("\r\n", "[br]", $text);
if(substr($text, 0, 4) == "[br]")
$text = substr($text, 4);
$Len = strlen($text);
if(substr($text, $Len - 4, 4) == "[br]")
$text = substr($text, 0, $Len - 4);
$text = htmlspecialchars($text);
return $text;
}
function content2html($content)
{
// [img]http://god.512j.com/images/head1.gif[/img]
// <img src='http://god.512j.com/images/head1.gif' class='godimage'>
$TextLines = explode("[br]", $content);
$LineNum = count($TextLines);
for($i=0; $i < $LineNum; $i ++)
{
if(substr($TextLines[$i], 0, 2) == " " || substr($TextLines[$i], 0, 1) == " ")
$text .= "<p>".$TextLines[$i]."</p>";
else
$text .= "<p> ".$TextLines[$i]."</p>";
}
$text = str_replace("[img]", "<img src='", $text);
$text = str_replace("[/img]", "' class='godimage'>", $text);
return $text;
}
function text2html($text)
{
return $text;
}
// 獲取XML文件中的文章數(shù)
function getArticleNum($file_name)
{
$doc = new DOMDocument();
$doc->load($file_name);
$id_space = $doc->getElementsByTagName("id");
return $id_space->length;
}
// 設(shè)置文章頁碼
function getPageIndexString($file_name, $gid, $page_index, $index_span)
{
$article_num = getArticleNum($file_name);
$max_index = ($article_num % $index_span) > 0 ? ((int)($article_num / $index_span)) + 1 : (int)($article_num / $index_span);
if ($page_index != 1)
{
$page_url_first = "<a href='index.php?gid=".$gid."&pageindex=1' title='首頁'>|<</a>";
}
if ($page_index > 1)
{
$temp_index = $page_index - 1;
$page_url_former = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."' title='前一頁'><<</a>";
}
$base_num = -1;
if ($page_index % 5 == 0)
{
$base_num = $page_index - 4;
}
else
{
$base_num = $page_index - ($page_index % 5) + 1;
}
if ($base_num <= $max_index)
{
if ($page_index == $base_num)
{
$page_url_one = "<Font color=red>".$base_num."</Font>";
}
else
{
$page_url_one = "<a href='index.php?gid=".$gid."&pageindex=".$base_num."'>".$base_num."</a>";
}
}
if ($base_num + 1 <= $max_index)
{
$temp_index = $base_num + 1;
if ($page_index == $temp_index)
{
$page_url_one = "<Font color=red>".$temp_index."</Font>";
}
else
{
$page_url_one = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."'>".$temp_index."</a>";
}
}
if ($base_num + 2 <= $max_index)
{
$temp_index = $base_num + 2;
if ($page_index == $temp_index)
{
$page_url_one = "<Font color=red>".$temp_index."</Font>";
}
else
{
$page_url_one = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."'>".$temp_index."</a>";
}
}
if ($base_num + 3 <= $max_index)
{
$temp_index = $base_num + 3;
if ($page_index == $temp_index)
{
$page_url_one = "<Font color=red>".$temp_index."</Font>";
}
else
{
$page_url_one = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."'>".$temp_index."</a>";
}
}
if ($base_num + 4 <= $max_index)
{
$temp_index = $base_num + 4;
if ($page_index == $temp_index)
{
$page_url_one = "<Font color=red>".$temp_index."</Font>";
}
else
{
$page_url_one = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."'>".$temp_index."</a>";
}
}
if ($page_index < $max_index)
{
$temp_index = $page_index + 1;
$page_url_next = "<a href='index.php?gid=".$gid."&pageindex=".$temp_index."' title ='下一頁'>>></a>";
}
if ($page_index != $max_index)
{
$page_url_end = "<a href='index.php?gid=".$gid."&pageindex=".$max_index."' title='最后一頁'>>|</a>";
}
$page_index_string = $page_url_first." ".$page_url_former." ".$page_url_one." ".$page_url_two." ".$page_url_three;
$page_index_string .= " ".$page_url_four." ".$page_url_five." ".$page_url_next." ".$page_url_end;
return $page_index_string;
}
// ------------------以上為添加文章用到的類或函數(shù)--------------------
// 獲取當前最大文章ID值,以便+1當做新文章ID
function getMaxID($file_name)
{
$xmlReader = new XMLReader();
$xmlReader->open($file_name);
$xmlReader->read(); // Skip root node
$flag = "";
$id = array();
while ($xmlReader->read())
{
switch($xmlReader->nodeType)
{
case XMLReader::ELEMENT:
$flag = $xmlReader->name;
break;
case XMLReader::TEXT:
if ($flag == "id")
{
array_push($id, utf2gbk($xmlReader->value));
$flag = "";
}
break;
}
}
$xmlReader->close();
return $id[count($id)-1];
}
// 添加文章類
class GodDocument extends DomDocument
{
function __construct()
{
//必須調(diào)用!
parent::__construct("1.0", "gbk");
$this->preserveWhiteSpace = FALSE;
$this->formatOutput =true;
}
function addArticle($id, $name, $create_time, $title, $content)
{
$article_space = $this->createElement("article");
// id
$id_space = $this->createElement("id");
$id_text = $this->createTextNode(gbk2utf($id));
$id_space->appendChild($id_text);
// 作者
$name_space = $this->createElement("name");
$name_text = $this->createTextNode(gbk2utf($name));
$name_space->appendChild($name_text);
// 時間
$time_space = $this->createElement("time");
$time_text = $this->createTextNode(gbk2utf($create_time));
$time_space->appendChild($time_text);
// 標題
$title_space = $this->createElement("title");
$title_text = $this->createTextNode(gbk2utf($title));
$title_space->appendChild($title_text);
// 內(nèi)容
$content_space = $this->createElement("content");
$content_text = $this->createTextNode(gbk2utf($content));
$content_space->appendChild($content_text);
$article_space->appendChild($id_space);
$article_space->appendChild($name_space);
$article_space->appendChild($time_space);
$article_space->appendChild($title_space);
$article_space->appendChild($content_space);
$this->documentElement->appendChild($article_space);
}
function createNewDataFile($file_name)
{
// we want a nice output
$this->formatOutput = true;
$root = $this->createElement('articles');
$this->appendChild($root);
$this->save($file_name);
}
}
// ------------------以上為添加文章用到的類或函數(shù)--------------------
// ------------------以下為讀取文章用到函數(shù)--------------------
function isKey($name)
{
if ($name == "name" || $name == "title" || $name == "id"
|| $name == "time" || $name == "content")
{
return true;
}
return false;
}
function readArticle($file_name, $page_index, $index_span)
{
$output = "";
if (!file_exists($file_name))
{
$output .= "<tr bgcolor=green><td><b>".此版塊還沒有任何內(nèi)容."</b></td></tr>";
$output .= "<tr bgcolor=#CCCCCC><td><br>There is nothing now.";
$output .= "</td></tr><tr bgcolor=#555555><td> </td></tr>";
echo $output;
return;
}
$xmlReader = new XMLReader();
$xmlReader->open($file_name);
$xmlReader->read(); // Skip root node
$flag = "";
$id = array();
$name = array();
$time = array();
$title = array();
$content = array();
/* Go through the nodes */
while ($xmlReader->read())
{
/* We're only parsing begin and #text nodes right now */
if($xmlReader->nodeType != XMLReader::END_ELEMENT && $xmlReader->name != "article")
{
switch($xmlReader->nodeType)
{
case XMLReader::ELEMENT:
$flag = $xmlReader->name;
break;
case XMLReader::TEXT:
if ($flag != "" && isKey($flag))
{
array_push($$flag, utf2gbk($xmlReader->value));
$flag = "";
}
break;
}
}
}
$xmlReader->close();
$is_god = isGod();
$index = 0;
for($i= count($name)-1; $i >= 0; $i--)
{
$index++;
if ($index <= ($page_index-1) * $index_span)
{
continue;
}
if ($index > $page_index * $index_span)
{
break;
}
$block_id = 1;
if ($_REQUEST["gid"] != NULL)
{
$block_id = $_REQUEST["gid"];
}
if ($is_god)
{
$output .= "<tr bgcolor=green><td><b>".text2html($title[$i])."</b></td>";
$output .= "<td align=right width=45>";
$output .= "<a href='delete_article.php?gid=".$block_id."&id=".$id[$i];
$output .= "' onclick='return confirmOK()'>刪除</a></td>";
$output .= "<td align=right width=45>";
$output .= "<a href='edit_article.php?gid=".$block_id."&id=".$id[$i]."'>編輯</a></td></tr>";
$output .= "<tr bgcolor=#CCCCCC><td colspan=3><br>".content2html($content[$i])."<br>";
$output .= "<p align=right>".text2html($name[$i])." 發(fā)表于 ".text2html($time[$i])."</p></td></tr>";
$output .= "<tr bgcolor=#555555><td colspan=3> </td></tr>";
}
else
{
$output .= "<tr bgcolor=green><td><b>".text2html($title[$i])."</b></td></tr>";
$output .= "<tr bgcolor=#CCCCCC><td><br>".content2html($content[$i])."<br>";
$output .= "<p align=right>".text2html($name[$i])." 發(fā)表于 ".text2html($time[$i])."</p></td></tr>";
$output .= "<tr bgcolor=#555555><td> </td></tr>";
}
echo $output;
$output = "";
}
}
// ------------------以上為讀取文章用到函數(shù)--------------------
// ------------------以下是編輯或刪除文章用到的函數(shù)-----------------------
// 刪除一個文章
function deleteArticle($file_name, $id)
{
$doc = new DOMDocument();
$doc->load($file_name);
$id_space = $doc->getElementsByTagName("id");
foreach($id_space as $node)
{
if ($node->textContent == $id)
{
$node->parentNode->parentNode->removeChild($node->parentNode);
}
}
$doc->save($file_name);
}
// 編輯更新一個文章
function updateArticle($file_name, $id, $name, $title, $content)
{
$doc = new DOMDocument();
$doc->load($file_name);
$node_list = NULL;
$id_space = $doc->getElementsByTagName("id");
foreach($id_space as $node)
{
if ($node->textContent == $id)
{
$node_list = $node->parentNode->childNodes;
}
}
foreach ($node_list as $n)
{
if ($n->nodeName == "name")
{
$n->nodeValue = gbk2utf($name);
}
else if ($n->nodeName == "title")
{
$n->nodeValue = gbk2utf($title);
}
else if ($n->nodeName == "content")
{
$n->nodeValue = gbk2utf($content);
}
}
$doc->save($file_name);
}
// 編輯之前把要編輯的文章加載上來
function loadArticle($file_name, $id)
{
$doc = new DOMDocument();
$doc->load($file_name);
$node_list = NULL;
$id_space = $doc->getElementsByTagName("id");
foreach($id_space as $node)
{
if ($node->textContent == $id)
{
$node_list = $node->parentNode->childNodes;
}
}
$result = array();
foreach ($node_list as $n)
{
if ($n->nodeName == "name")
{
$result["name"] = utf2gbk($n->nodeValue);
}
else if ($n->nodeName == "title")
{
$result["title"] = utf2gbk($n->nodeValue);
}
else if ($n->nodeName == "content")
{
$result["content"] = utf2gbk($n->nodeValue);
}
}
return $result;
}
// -----------------------以上是編輯或刪除文章用到的函數(shù)----------------------------
?>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -