示例#1
0
 public function index($page = 1)
 {
     $page = (int) $page;
     $sortField = Request::get('field', 'created_at');
     $orderMethod = strtoupper(Request::get('order', 'desc'));
     $notify = ['error' => null, 'message' => null];
     $comment = new \App\Model\Comment();
     try {
         if (Request::post('comment')) {
             $name = Request::post('name');
             $email = Request::post('email');
             $homepage = Request::post('homepage');
             $captcha = Request::post('captcha');
             $csrToken = Request::post('csrf_token');
             $ip = Request::getIp();
             $agent = Request::getUseAgent();
             $message = Request::post('message');
             if (!Protection::validateCsrfToken($csrToken)) {
                 throw new \Exception('Error token validation');
             }
             if (empty($name)) {
                 throw new \Exception('Empty name');
             }
             if (!Captcha::validate($captcha)) {
                 throw new \Exception('Error validate captcha');
             }
             if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 throw new \Exception('Email not valid');
             }
             if (!empty($homepage) && !filter_var($homepage, FILTER_VALIDATE_URL)) {
                 throw new \Exception('Homepage is not valid');
             }
             if (empty($message)) {
                 throw new \Exception('Empty message');
             }
             $params = ['name' => $name, 'email' => $email, 'homepage' => $homepage, 'ip' => $ip, 'agent' => $agent, 'message' => $message, 'created_at' => time()];
             if (!$comment->save($params)) {
                 throw new \Exception('Error save comment');
             }
             $notify['message'] = 'Comment success add';
             unset($_POST);
         }
     } catch (\Exception $e) {
         $notify['error'] = $e->getMessage();
     }
     $commentList = [];
     try {
         if (!in_array($sortField, $comment->sortListAllow)) {
             throw new \Exception('Error validate field');
         }
         if (!in_array($orderMethod, $comment->orderListAllow)) {
             throw new \Exception('Error validate order parametr');
         }
         $commentList = $comment->commentList($page, $sortField, $orderMethod);
     } catch (\Exception $e) {
         $notify['error'] = $e->getMessage();
     }
     $this->render('index', ['commentList' => $commentList, 'commentCount' => $comment->getCount()['count'], 'pageCount' => \App\Model\Comment::PAGE_COUNT, 'page' => $page, 'notify' => $notify]);
 }
示例#2
0
<h3> Send message gust book </h3>

<?php 
if (!empty($notify['message'])) {
    ?>
    <div style="color: green"><?php 
    echo $notify['message'];
    ?>
 </div>
<?php 
}
?>
<form method="post" id="contact-form" style="margin-left: 10px">
    <input type="hidden" name="comment" value="1">
    <input type="hidden" name="csrf_token" value="<?php 
echo Protection::getCsrfToken();
?>
">
    <div class="control-group">
        <label class="control-label" for="name">Name</label>
        <div class="controls">
            <input name="name" type="text" id="name" placeholder="Your name" required value="<?php 
echo Html::escape(Request::post('name'));
?>
">
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="email">Email</label>
        <div class="controls">