| Current Path : /var/www/element/data/www/proverki.net/articles_client/ |
| Current File : /var/www/element/data/www/proverki.net/articles_client/articles.class.php |
<?
class Articles {
var $config;
function Start() {
if($this->config['db_type'] == 'file') return true;
if($this->config['db_type'] == 'mysql') {
mysql_connect($this->config['mysql_host'],$this->config['mysql_user'],$this->config['mysql_password']);
mysql_select_db($this->config['mysql_db_name']);
}
}
function GetConfig($config_name) {
include($config_name);
$this->config = $config;
}
function CompileTemplate($template_name,$params, $i = false) {
$template = $params;
include($this->config['template_dir'] . $template_name . ".html");
}
function GetFileItemParams( $article_name ) {
$file_name = $this->config['article_dir'] . $article_name . ".txt";
if($file_handle = fopen($file_name, "r")) {
$serialized_data = fgets($file_handle);
fclose($file_handle);
$data = unserialize($serialized_data);
return $data;
}
return false;
}
function GetFileItemData( $article_name ) {
$file_name = $this->config['article_dir'] . $article_name . ".txt";
$file_handle = fopen($file_name, "r");
$serialized_data = fgets($file_handle);
$contents = '';
while(!feof($file_handle)) {
$contents .= fgets($file_handle);
}
fclose($file_handle);
$params = unserialize($serialized_data);
$contentsAr = explode($this->config['content_delimeter'],$contents);
for($i=0;$i<=count($contents);$i++) {
$contentsAr[$i] = trim($contentsAr[$i],"\n\r");
}
$contentsAr['announce'] = $contentsAr[0];
$contentsAr['content'] = $contentsAr[1];
unset($contentsAr[0],$contentsAr[1]);
$data = array_merge($params,$contentsAr);
foreach($data as $k => $v) {
$data[$k] = stripcslashes($v);
}
return $data;
}
function MakeWriteData($title, $announce, $content, $image, $outlink, $date = false, $id = false) {
$data = '';
if(!$date) $date = date('Y-m-d H:i:s');
$page_params = array(
"id" => $id,
"title" => $title,
"date" => $date,
"image" => $image,
"outlink" => $outlink
);
$page_params = serialize($page_params);
$data .= $page_params;
$data .= "\r\n";
$data .= $announce;
$data .= "\r\n";
$data .= $this->config['content_delimeter'];
$data .= "\r\n";
$data .= $content;
return $data;
}
function UploadImage($image) {
if(!empty($image)) {
$uploadfile = $this->config['upload_dir'] . basename($image['name']);
if(move_uploaded_file($image['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0644);
$image = str_replace($_SERVER['DOCUMENT_ROOT'],'',$this->config['upload_dir']) . basename($image['name']);
return $image['name'];
} else {
return false;
}
} else {
return false;
}
}
function AddArticle($name, $title, $announce, $content, $image, $outlink, $id = false) {
switch($this->config['db_type']) {
case "file":
$list = $this->GetList();
$is_exist_by_id = false;
foreach($list as $article) {
if(isset($article['id']) && $article['id'] == $id) {
$is_exist_by_id = true;
$existed_array = $article;
}
}
$errors = '';
if($is_exist_by_id) {
$this->UpdateArticle( $name, $title, $announce, $content, '', '', $id );
} else {
$fullpath = $this->config['article_dir'] . $name . ".txt";
if(!file_exists($fullpath)) {
$image = $this->UploadImage($image);
$data = $this->MakeWriteData($title, $announce, $content, $image, $outlink, false, $id);
$fp = fopen ($fullpath, "w+");
if(!$fp) $errors = '303';
fwrite ($fp, $data);
fclose ($fp);
chmod($fullpath, 0777);
} else {
$errors = "304";
}
}
if(empty($errors)) {
return false;
} else {
return $errors;
}
break;
case "mysql":
return true;
break;
}
}
function GetById($id) {
$list = $this->GetList();
$data = false;
foreach($list as $article) {
if(isset($article['id']) && $article['id'] == $id) {
$data = $article;
}
}
return $data;
}
function GetList() {
$i=0;
$articles_list = array();
if ($handle = opendir($this->config['article_dir'])) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && preg_match('/.*\.txt/',$file)) {
$article_keyword = preg_replace('/(.*)\.txt$/','$1',$file);
$item_data = $this->GetFileItemData( $article_keyword );
$sortdate = preg_replace('/(..)\.(..)\.(..) (..):(..):(..)/','$1$2$3$4$5$6',$item_data['date']);
$item_data['name'] = $article_keyword;
$item_data['detail_url'] = str_replace('{%name%}', $article_keyword, $this->config['detail_link']);
$non_converted_date = $item_data['date'];
$item_data['date'] = date($this->config['dateformat'] , strtotime($item_data['date']) );
$articles_list[$non_converted_date] = $item_data;
$item_data = array();
$i++;
}
}
closedir($handle);
krsort($articles_list);
return $articles_list;
} else return false;
}
function UpdateArticle( $name, $title, $announce, $content, $image = false, $outlink = false, $id ) {
switch($this->config['db_type']) {
case "file":
$article = $this->GetById( $id );
$fullpath = $this->config['article_dir'] . $name . '.txt';
if($name != $article['name']) {
$oldpath = $this->config['article_dir'] . $article['name'] . ".txt";
$newpath = $this->config['article_dir'] . $name . ".txt";
$fullpath = $newpath;
rename($oldpath, $newpath);
}
$old_data = $this->GetFileItemData($name);
$image = $this->UploadImage($image);
$data = $this->MakeWriteData($title, $announce, $content, $image, $outlink, $old_data['date'], $id);
if(!$image) $image = $old_data['image'];
$fp = fopen ($fullpath, "w+");
if(!$fp) echo "couldn't open file";
fwrite ($fp, $data);
fclose ($fp);
chmod($fullpath, 0777);
break;
case "mysql":
return true;
break;
}
}
function ShowSummary($template_name,$count=5) {
switch($this->config['db_type']) {
case "file":
$i=0;
$articles_list = array();
if ($handle = opendir($this->config['article_dir'])) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && preg_match('/.*\.txt/',$file)) {
$article_keyword = preg_replace('/(.*)\.txt$/','$1',$file);
$item_data = $this->GetFileItemData( $article_keyword );
$sortdate = preg_replace('/(..)\.(..)\.(..) (..):(..):(..)/','$1$2$3$4$5$6',$item_data['date']);
$item_data['name'] = $article_keyword;
$item_data['detail_url'] = str_replace('{%name%}', $article_keyword, $this->config['detail_link']);
$non_converted_date = $item_data['date'];
$item_data['date'] = date($this->config['dateformat'] , strtotime($item_data['date']) );
$articles_list[$non_converted_date] = $item_data;
$item_data = array();
$i++;
}
}
closedir($handle);
krsort($articles_list);
$i = 1;
foreach($articles_list as $params) {
if($i > $count) continue;
$this->CompileTemplate($template_name, $params, $i);
$i++;
}
}
break;
case "mysql":
return true;
break;
}
}
function ShowDetail($article_name, $template_name, $admin=false) {
switch($this->config['db_type']) {
case "file":
$item_data = $this->GetFileItemData( $article_name );
$this->CompileTemplate($template_name , $item_data);
break;
case "mysql":
return true;
break;
}
}
function DeleteById($id) {
if($data = $this->GetById($id)) {
$file_name = $data['name'] . '.txt';
$full_path = $this->config['article_dir'] . $file_name;
if(unlink($full_path)) {
return false;
} else {
return '405';
}
} else {
return '406';
}
}
}
?>