Всем привет! Я решил переделать скрипт гостевой, в скрипт для добавления и редактирования новостей. Т.к. в пхп я полный -0-, я запутался и наделал сотню, или больше ошибок. Если кто может помогите исправить и поубирать лишнее.
Вот файл конфига из гостевой conf.php в нем половина лишнего:
| Код | <?php $_GB_CONFIG['RootPath'] = str_replace("\\", "/", dirname(__FILE__)).'/'; $_GB_CONFIG['EnginePath'] = $_GB_CONFIG['RootPath'].'engine/'; $_GB_CONFIG['ShellPath'] = $_GB_CONFIG['RootPath'].'shell/'; $_GB_CONFIG['DBPath'] = $_GB_CONFIG['RootPath'].'data/'; $_GB_CONFIG['MaxMessagesToPage'] = 4; $_GB_CONFIG['ShowQuickReplyForm'] = 0; $_GB_CONFIG['SeparateMessageInDB'] = '^'; $_GB_CONFIG['SeparateLogsInDB'] = '|'; ?>
|
Вот файл index.php
| Код | <html><head><title>NEWS</title><link href='includes/main.css' type=text/css rel=stylesheet></head><body scroll='auto'>
<?php define('_GB_INITIALIZED', true);
include_once('config.php');
function ReadDB($File, $Reverse = 1) { return ($Reverse == 1) ? array_reverse(file($File)) : file($File); }
function CalculatePages($CurrentPage, $MaxMessagesToPage, $MessagesCount) { $PagesCount = ceil($MessagesCount / $MaxMessagesToPage); for($i = 0; $i < $PagesCount; $i++) { $Pages[] = ($i == $CurrentPage) ? '['.($i+1).']' : '<a href="?offset='.$i.'">['.($i+1).']</a>'; } return $Pages; }
function AddUserMessage() { global $_POST; $fp = fopen('data/messages.data', 'a+') or die('Невозможно открыть файл. Убедитесь что к файлу имеется доступ.'); $Message = array( str_replace(" ", "_", microtime()), str_replace(array("\r\n", "\r", "\n", "^"), "", stripslashes($_POST['name'])), $_POST['email'], $_POST['www'], $_POST['icq'], str_replace(array("\r\n", "\r", "\n", "^"), "", nl2br(stripslashes($_POST['message']))), $_SERVER['REMOTE_ADDR'], time(), ); fputs($fp, implode("^", $Message)."\r\n") or die('Невозможно произвести запись в файл. Проверьте права доступа на запись.'); fclose($fp); }
function DeleteRecord($Messages, $ID) { global $_GB_CONFIG; for($i = 0; $i < sizeof($Messages); $i++) { $ex = explode("^", trim($Messages[$i])); if($ex[0] != $ID) $Temp[] = implode("^", $ex); } return $Temp; }
function EditRecord($Messages, $ID) { global $_GB_CONFIG, $_POST; for($i = 0; $i < sizeof($Messages); $i++) { $ex = explode("^", trim($Messages[$i])); if($ex[0] != $ID) { $Temp[] = implode("^", $ex); } else { $Temp[] = implode("^", array( $ID, $_POST['name'], $_POST['title'], str_replace(array("\r\n", "\r", "\n", "^"), "", nl2br($_POST['message'])), $ex[6], $ex[7], ) ); } } return $Temp; }
function ReWriteDB($Messages) { global $_GB_CONFIG; $fp = fopen('data/messages.data', "w+"); flock($fp, LOCK_EX); if(sizeof($Messages) > 0) fputs($fp, implode("\r\n", $Messages)."\r\n"); flock($fp, LOCK_UN); fclose($fp); }
function GetMessageOnID($Messages, $ID) { global $_GB_CONFIG; for($i = 0; $i < sizeof($Messages); $i++) { $ex = explode("^", trim($Messages[$i])); if($ex[0] == $ID) return $ex; } return false; }
switch((isset($_GET['action'])) ? $_GET['action'] : 'show') { case 'add': { if(AddUserMessage()); include_once('shell/redirect.php'); break; } case 'edit': { $Messages = ReadDB('data/messages.data', 1); if(!isset($_POST['apply'])) { if($msg = GetMessageOnID($Messages, (isset($_GET['id'])) ? $_GET['id'] : 0)) { include_once('shell/editform.php'); } else { echo "<center><p>Сообщений не найдено</p></center>"; } } else { ReWriteDB( EditRecord($Messages, (isset($_GET['id'])) ? $_GET['id'] : false)); $Offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0; header('location: ?action=show&offset='.$Offset); } break; } case 'delete': { ReWriteDB( DeleteRecord( ReadDB('data/messages.data', 1), (isset($_GET['id'])) ? $_GET['id'] : false) ); $Offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0; header('location: ?action=show&offset='.$Offset); break; } case 'reply': { include_once('shell/replyform.php'); break; }
default: { $Messages = ReadDB('data/messages.data'); $CurrentPage = (isset($_GET['offset'])) ? $_GET['offset'] : 0; $MessagesCount = sizeof($Messages); $Pages = CalculatePages($CurrentPage, 4, $MessagesCount); $PagesCount = ceil($MessagesCount / 4); if(($CurrentPage * 4 > 1) && ($CurrentPage * 4 < $MessagesCount)) { $StartShow = $CurrentPage * 4; if($CurrentPage * 4 + 4 > $MessagesCount) { $EndShow = $MessagesCount; } else { $EndShow = $CurrentPage * 4 + 4; } } else { $StartShow = 0; $EndShow = (4 > $MessagesCount) ? $MessagesCount : 4; } include_once('shell/showmessages.php'); } } ?> </body></html>
|
Вот showmessages.php
| Код | <?php if(!defined('_GB_INITIALIZED')) exit; ?> <table border="1" cellspacing="2" width="65%" align="center"> <tr> <th colspan="2">Новости.</th> </tr> <?php while($StartShow < $EndShow) { $msg = explode($_GB_CONFIG['SeparateMessageInDB'], $Messages[$StartShow]); ?> <tr> <td valign="top"><?php echo date("d.m.Y H:i", $msg[7]); ?> | <b><?php if(!empty($msg[3])) { echo $msg[3].'<br>'; } ?></b> <?php echo $msg[5].'<br>'; ?>Разместил: <?php echo $msg[1]; ?></td> <td align="center">
<a href="?action=edit&id=<?php echo $msg[0] ?>&offset=<?php echo $CurrentPage; ?>"><img src=icon_edit.bmp border=0></a> <a href="?action=delete&id=<?php echo $msg[0] ?>&offset=<?php echo $CurrentPage; ?>"><img src=icon_delete.bmp border=0></a></td> </tr> <?php $StartShow++; } ?> </table> <p align="center">Всего страниц: <?php echo implode(' | ', $Pages); ?></p> <br> <form name="replyform" action="?action=add" method="post"> <table border="0" class="quickform" cellspacing="2" width="50%" align="center"> <tr> <th colspan="2">Добавить новость.</th> </tr> <tr> <td width="25%"> Редактор: </td> <td align="right"> <input class="msgtext" type="text" maxlength="30" value="" name="name"> </td> </tr> <tr> <td> Заголовок: </td> <td align="right"> <input class="msgtext" type="text" maxlength="30" value="" name="title"> </td> </tr><tr> <td colspan="2" align="right" valign="top"> <textarea class="msgtext" id=news_start_text name="message"></textarea> </td> </tr> <tr> <td colspan="2" align="center"><input name="submit_btn" class="msgbtn" onClick="javascript:this.disabled=1;document.replyform.submit();" type="submit" value="Добавить"> <input class="msgbtn" type="reset" value="Отменить"></td> </tr> </table> </form>
|
replyform.php
| Код | <?php if(!defined('_GB_INITIALIZED')) exit; ?> <br> <form name="replyform" action="?action=add" method="post"> <table border="0" class="quickform" cellspacing="2" width="50%" align="center"> <tr> <th colspan="2">Добавить новость.</th> </tr> <tr> <td width="25%"> Админ: </td> <td align="right"> <input class="msgtext" type="text" maxlength="30" value="" name="name"> </td> </tr> <tr> <td> Заголовок: </td> <td align="right"> <input class="msgtext" type="text" maxlength="30" value="<?php echo GetCookie('_gb_user_www'); ?>" name="title"> </td> </tr><tr> <td colspan="2" align="right" valign="top"> <textarea class="msgtext" name="message"></textarea> </td> </tr> <tr> <td colspan="2" align="center"><input name="submit_btn" class="msgbtn" onClick="javascript:this.disabled=1;document.replyform.submit();" type="submit" value="Добавить"> <input class="msgbtn" type="reset" value="Отменить"></td> </tr> </table> </form> <script> /* В Opera при возврате на страницу где была заблокирована кнопка она остается заблокированной. В IE все работает нормально. Netscape не проверял. */ if(document.replyform.submit_btn.disabled = 1) { document.replyform.submit_btn.disabled = 0; } </script>
|
redirect.php
| Код | <?php if(!defined('_GB_INITIALIZED')) exit; ?> <center><div style="width:65%;" align="left"> Спасибо, Ваше сообщение добавлено.
<script> document.write('Подождите, сейчас Вас переадресует на страницу просмотра сообщений.'); function GoShowPage() { window.location.href="?action=show"; } window.setTimeout('GoShowPage()', 1000); </script> <noscript>Чтобы просмотреть отправленное Вами сообщение, нажмите на <a href="?action=show">эту</a> ссылку.</noscript> </div> </center>
| |