К началу');
/*Подпись которой проверяется истинность пришедшей информации, должна быть такой же как и в конфиг-файлах админпанели */
define('SIGN', 'f4e904b1045dcb60a8d5e6b2508fc9dc');
/* ------------------------Do not edit below this line! - ------------------*/
$config = array(
'd_folder' => DIR_FOLDER,
'a_folder' => ARTICLES_FOLDER,
'a_list' => ARTICLES_LIST,
't_template' => CATALOG_TEMPLATE,
'page_side' => PAGE_SIDE,
'default_title' => DEFAULT_TITLE,
'per_page' => PER_PAGE,
'page_word' => PAGE_WORD,
'add_link' => ADD_LINK,
'main_link' => MAIN_LINK,
'sign' => SIGN
);
$config['d_folder'] = ( !empty($config['d_folder']) && preg_match('~[^/]$~', $config['d_folder'])) ? $config['d_folder'] . '/' : $config['d_folder'];
$config['main_link'] = ( !empty($config['d_folder']) && !empty($config['main_link'])) ? str_replace('./', '/' . $config['d_folder'], $config['main_link']) : $config['main_link'];
$config['d_folder'] = ( !empty($config['d_folder']) && $config['d_folder'] != './' ) ? '/' . $config['d_folder'] : './';
function strips(&$el) {
if (is_array($el))
foreach($el as $k=>$v)
strips($el[$k]);
else $el = stripslashes($el);
}
if (get_magic_quotes_gpc()) {
strips($_GET);
strips($_POST);
strips($_COOKIE);
strips($_REQUEST);
if (isset($_SERVER['PHP_AUTH_USER'])) strips($_SERVER['PHP_AUTH_USER']);
if (isset($_SERVER['PHP_AUTH_PW'])) strips($_SERVER['PHP_AUTH_PW']);
}
$catalog = new Catalog( $config );
$catalog->run_engine() ;
/**
paginate() method based on paginate() function from http://www.codewalkers.com/c/a/Database-Articles/Multiple-Pages-of-Data-from-a-Text-File/3/
read_file() and write_file() method from file_helper.php Codeigniter framework www.codeigniter.com
*/
class Catalog{
function Catalog( $config = null){
$this->post = ( isset($_POST) ) ? $_POST : null;
$this->get = ( isset($_GET) ) ? $_GET : null;
if ( isset($config) && is_array($config) ){
foreach ($config as $key => $value){
$this->$key = trim($value);
}
$this->a_fullpath = $this->article_folder() . $this->a_list;
}
}
function run_engine(){
if ( $this->post('info') ) $this->add_article();
elseif ( $this->get('articleid') ) echo $this->get_simple_article_page( );
else echo $this->get_article_list_page();
}
function article_list_initialization(){
$this->pg = ($this->get('pg') && ctype_digit($this->get('pg'))) ? $this->get('pg') : 1;
$this->display = $this->per_page;
$this->data = $this->read_file_as_array( $this->a_fullpath );
$this->total = count($this->data);
$this->start = $this->display * $this->pg - $this->display;
if ( $this->start > $this->total ) $this->set404();
}
function get_article_array(){
$files_array = $this->get_files_for_page( $this->data, $this->start, $this->display);
foreach ( $files_array as $key => $value ) {
$articles[(int)trim($key)] = $this->get_articles_parts( $value );
}
return ( isset($articles) ) ? $articles : array();
}
function get_articles_list_html(){
$article_array = $this->get_article_array();
$result = '';
foreach ( $article_array as $key => $value ){
if ( !empty($value) ){
$link = '';
$result .= ''.$this->set_template_variable( $value[2], '{{ARTICLE_THEME}}', $link ) . '
';
}
}
$link = ( !empty($this->add_link) && !empty($this->main_link) ) ? '{{MAIN_LINK}}' : '';
return '' . $link. $result . '{{PAGINATION}}
';
}
function read_file_as_array( $file ){
if ( file_exists($file) ) {
$file = @file( $file );
if (is_array($file)) {
$file = array_reverse($file);
}
return $file;
}
return array();
}
function get_files_for_page( $data, $start, $display){
$array = array_slice($data, $start, $display);
$result = array();
foreach ($array as $value) {
$result[(int) trim( $value )] = (int) trim( $value );
}
return $result;
}
function get_pagination(){
return $this->paginate($this->display, $this->page_side, $this->pg, $this->total);
}
function paginate($display, $sides, $pg, $total) {
/* make sure pagination doesn't interfere with other query
string variables */
if(isset($_SERVER['QUERY_STRING']) && trim( $_SERVER['QUERY_STRING']) != '' ) {
if(stristr($_SERVER['QUERY_STRING'], 'pg=')){
$query_str = '?'.preg_replace('/pg=\d+/', 'pg=', strip_tags($_SERVER['QUERY_STRING']));
}
else {
$query_str = '?' . strip_tags($_SERVER['QUERY_STRING']).'&pg=';
}
}
else{
$query_str = '?pg=';
}
/* find out how many pages we have */
$pages = ($total <= $display) ? 1 : ceil($total / $display);
$pg = ( $pg > $pages ) ? $pages : $pg;
/* create the links */
$first = '«';
$prev = '‹';
$next = '›';
$last = '»';
/* display opening navigation */
$result = '';
$result .= ($pg > 1 && $pages > 1 ) ? "$first : $prev :" : '';
/* limit the number of page links displayed */
$begin = $pg - $sides;
while($begin < 1){
$begin++;
}
$end = $pg + $sides;
while($end > $pages){
$end--;
}
for($i=$begin; $i<=$end; $i++){
if ( $pages > 1 ) {
$result .= ($i == $pg) ? ' ['.$i.'] ' : ' '.$i.' ';
}
}
/* display ending navigation */
$result .= ($pg < $pages) ? ": $next : $last" : '';
return ( !empty($result) ) ? '' . $result . '
' : '';
}
function read_file($file){
if ( ! file_exists($file)){
return FALSE;
}
if (function_exists('file_get_contents')){
return file_get_contents($file);
}
if ( ! $fp = @fopen($file, FOPEN_READ)){
return FALSE;
}
flock($fp, LOCK_SH);
$data = '';
if (filesize($file) > 0){
$data =& fread($fp, filesize($file));
}
flock($fp, LOCK_UN);
fclose($fp);
return $data;
}
function write_file($path, $data, $mode = 'a')
{
if ( ! $fp = @fopen($path, $mode))
{
return FALSE;
}
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
return TRUE;
}
function get_template( $path ){
return $this->read_file( $this->template_fullpath($path) );
}
function set_template_variable( $string, $variable, $value ){
return str_replace($variable, $value, $string);
}
function template_fullpath( $path ){
if ( empty($path) ) return '';
return ( !preg_match('~^http://~i', $path) ) ? realpath('.') . '/' . $path : $path;
}
function article_fullpath( $article ){
return $this->article_folder() . $article . '.txt' ;
}
function article_folder(){
$article_folder = preg_replace('~^/|/$~', '', $this->a_folder);
return realpath('.') . '/' . $article_folder . '/' ;
}
function post( $value ){
return ( isset($this->post[$value]) ) ? $this->post[$value] : null;
}
function get( $value ){
return ( isset($this->get[$value]) ) ? $this->get[$value] : null;
}
function get_articles_parts( $article ){
$article_text = $this->read_file( $this->article_fullpath( trim($article) ) );
$article_text = trim($article_text);
preg_match('~(.+?)
~si', $article_text, $matches);
if (isset($matches[1])){
$article_parts[0] = $matches[0];
$article_parts[1] = $matches[1];
$article_text = preg_replace('~'. $matches[0] .'~i', '{{ARTICLE_THEME}}', $article_text, 1);
$article_parts[2] = strip_tags( substr($article_text, 0 , 400) ) . '...' ;
$article_parts[3] = $article_text;
}
return ( isset($article_parts) ) ? $article_parts : array();
}
function get_article_list_page(){
$this->article_list_initialization();
$article_list = $this->set_template_variable( $this->get_articles_list_html(), '{{PAGINATION}}', $this->get_pagination() ) ;
$article_list = $this->set_template_variable( $article_list, '{{MAIN_LINK}}', ( ( $this->pg > 1 ) ? $this->main_link :' ' )) ;
$templates = $this->get_template( $this->t_template );
$page_string = ( $this->pg > 1 ) ? htmlspecialchars($this->page_word) . ' ' . htmlspecialchars($this->pg) : '';
$article_list = $this->set_template_variable( $templates, '{{ARTICLES}}', $article_list);
return $this->set_template_variable( $article_list, '{{TITLE}}', $this->default_title . ' ' . $page_string);
}
function get_simple_article_page(){
$id = $this->get('articleid');
$article_parts = $this->get_articles_parts( $id ) ;
if ( empty($article_parts) ) $this->set404();
$link = ( !empty($this->add_link) && !empty($this->main_link) ) ? $this->main_link : '';
$article = '' . $link .$this->set_template_variable( $article_parts[3], '{{ARTICLE_THEME}}', $article_parts[0]) . '
';
$templates = $this->get_template( $this->t_template );
$article = $this->set_template_variable( $templates, '{{ARTICLES}}', $article);
return $this->set_template_variable( $article, '{{TITLE}}', $article_parts[1]);
}
function set404(){
header("HTTP/1.0 404 Not Found");
die("\n\n404 Not Found\n
\nNot Found
\nThe requested URL ".$_SERVER["REQUEST_URI"]."
was not found on this server.\n
\nSpectrum 128 OS/2 QBasic for web 1.015\n");
}
function add_article(){
$sign = $this->sign;
$number = $this->post('number');
$article =$this->post('article');
if ( ($this->post('info') != md5($sign . $number . $article) ) || empty($number) || empty($article ) ) {
echo "No 5"; /* Данные неполные или несоответствуют подписи */
}
elseif (!is_writable( $this->article_folder() ) && !is_writable( $this->a_fullpath ) ){
echo "NO 6"; /* Папка или файл с данными о статьях недоступны для записи */
}
elseif ( $this->write_file($this->a_fullpath, ( ($this->read_file($this->a_fullpath)) ? "\n" : '' ) . $this->post('number')) ) {
if ( !$this->write_file($this->article_fullpath($this->post('number')), $this->post('article'), 'w') ){
echo "No 7"; /* Произошла ошибка записи файла статьи */
}
else{
echo "Yes";
}
}
else {
echo "No 8"; /* Произошла ошибка записи в файл с данными о статьях */
}
}
}
?>