Permalink
Browse files

Autoriz, Validation, Notification

  • Loading branch information...
zak
zak committed Aug 23, 2010
1 parent f28d482 commit 4a2d40c3949f7c0aec9f8631b324cda4b6a1c128
View
@@ -1,2 +1,4 @@
+/config/database.php
/nbproject
/.hg
+
View
0 .htaccess 100755 → 100644
No changes.
View
44 README
@@ -1,25 +1,42 @@
Системные требования
- PHP >= 5.2.0
+ PHP >= 5.3.0
MySQL >= 5.1
Apache (mod_write) 2.*
Установка
-Для установки необходимо прописать данные для коннекта к базе в файле config/database.php
-Также нужно создать саму базу
+Проверить наличие файла .htaccess
+Его содержимое должно соответствовать
+Allow from all
-CREATE TABLE `articles` (
- `id` int(12) NOT NULL auto_increment,
- `user_id` int(12) default NULL,
- `title` varchar(255) default NULL,
+RewriteEngine On
+
+RewriteBase /
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteCond %{REQUEST_FILENAME} !-s
+RewriteCond %{REQUEST_FILENAME} !-l
+
+RewriteRule ^(.+)$ /index.php?%{QUERY_STRING} [L]
+
+Далее нужно настроеть соединение с базой данных зайдя на сайт заполнить предложенную форму либо прописать данные для коннекта в ручную в файле config/database.php
+Если выбран второй способ необходимо создать саму базу
+
+
+CREATE TABLE IF NOT EXISTS `miniskel`.`articles` (
+ `id` int(12) NOT NULL AUTO_INCREMENT,
+ `user_id` int(12) DEFAULT NULL,
+ `title` varchar(255) DEFAULT NULL,
+ `teaser` text NOT NULL,
`body` text NOT NULL,
- `updated_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
- `created_at` timestamp NOT NULL default '0000-00-00 00:00:00',
- PRIMARY KEY (`id`)
-) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+ `public_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8
-CREATE TABLE `comments` (
+CREATE TABLE IF NOT EXISTS `comments` (
`id` int(12) NOT NULL auto_increment,
`article_id` int(12) default NULL,
`user_id` int(12) NOT NULL,
@@ -30,7 +47,7 @@ CREATE TABLE `comments` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-CREATE TABLE `users` (
+CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`login` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
@@ -43,3 +60,4 @@ CREATE TABLE `users` (
`session_ip` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
+
View
@@ -7,17 +7,33 @@
*/
class Articles extends Model {
- public static function find($arg=null) {
- return parent::find($arg, get_class());
+ private $fields = array(
+ 'id' => array(),
+ 'title' => array(),
+ 'teaser' => array(),
+ 'body' => array(),
+ 'user_id' => array('numeric'),
+ 'public_at' => array(),
+ 'created_at' => array(),
+ 'updated_at' => array()
+ );
+
+ protected static function getMyClass() {
+ return get_class();
}
public static function paginate($page, $per_page) {
- $result = self::find(array('LIMIT' => (($page - 1) * $per_page).','.$per_page,));
+ $result = self::find(array('WHERE' => 'public_at < NOW()', 'ORDER BY' => 'updated_at DESC', 'LIMIT' => (($page - 1) * $per_page).','.$per_page));
$count = ORM::instance()->query('SELECT COUNT(*) AS count FROM articles');
$total = $count[0]['count'];
$result['page'] = array('page'=>$page, 'total_entries' => $total, 'total_page' => (integer)($total / $per_page)+1);
return $result;
}
+ function user() {
+ return User::find("id = {$this->user_id}");
+ }
+
}
?>
+
View
@@ -3,10 +3,23 @@
class Comments extends Model {
- public static function find($arg=null) {
- return parent::find($arg, get_class());
+ private $fields = array(
+ 'id' => array(),
+ 'article_id' => array(),
+ 'user_id' => array(),
+ 'name' => array(),
+ 'email' => array('email'),
+ 'url' => array(),
+ 'body' => array(),
+ 'public' => array(),
+ 'created_at' => array()
+ );
+
+ protected static function getMyClass() {
+ return get_class();
}
-
+
}
?>
+
View
@@ -10,11 +10,18 @@
class Page extends Controller {
+ function before($request) {
+ $except_page = Array('index', 'show', 'comment');
+ if (!in_array($request['method'], $except_page) && $request['user'] == false) {
+ Header("Location: /login");
+ }
+ }
+
function index($request) {
$articles = Articles::paginate((isset($request['get']['page']) ? $request['get']['page'] : 1), 10);
$this->build('index.html.php', 'Главная', $articles);
}
-
+
function show($request) {
$articles = Articles::find("id = {$request['id']}");
$articles[0]->comments = Comments::find("article_id = {$request['id']}");
@@ -27,6 +34,11 @@ function add() {
function create($request) {
$article = new Articles($request['post']);
+ $article->user_id = $request['user']['id'];
+ $article->created_at = date('Y-m-d H:i:s');
+ if (!isset($request['post']['public_at']) || empty($request['post']['public_at'])) {
+ $article->public_at = date('Y-m-d H:i:s');
+ }
$article->create();
Header("Location: /page/{$article->id}");
}
@@ -58,7 +70,7 @@ function update($request) {
$articles = Articles::find("id = {$request['id']}");
$articles[0]->update_attributes($request['post']);
$articles[0]->save();
- $this->build('show.html.php', $articles[0]->title, $articles[0]);
+ Header("Location: /page/{$articles[0]->id}");
}
function delete($request) {
@@ -68,3 +80,4 @@ function delete($request) {
}
}
?>
+
View
@@ -6,8 +6,8 @@
*/
class User extends Model {
- public static function find($arg=null) {
- return parent::find($arg, 'users');
+ protected static function getMyClass() {
+ return get_class();
}
public static function signup($params) {
@@ -22,17 +22,17 @@ public static function signup($params) {
return false; // 'Логин или эл.адресс не уникален.';
}
}
-
+
public static function authenticate($login, $pass) {
$table_name = 'users';
$query = "SELECT * FROM {$table_name} WHERE login = '{$login}' LIMIT 0, 1";
$db = ORM::instance();
$query_result = $db->query($query);
- $user = $query_result[0];
- if ($user['password'] == md5($pass.$user['salt'])) {
+ //$user = $query_result[0];
+ if ((!empty($query_result)) && ($user = $query_result[0]) && ($user['password'] == md5($pass.$user['salt']))) {
$session_token = md5(base64_encode(time()).$user['salt']);
-
+
$db->insert_query("UPDATE {$table_name} SET session_token = '{$session_token}', session_token_expires_at = '".(time()+$user['session_expires_time_shift']).'\', session_ip = \''.$_SERVER['REMOTE_ADDR']."' WHERE id = '{$user['id']}'");
setcookie("session_token", $session_token, time()+$user['session_expires_time_shift']);
return true;
@@ -51,3 +51,4 @@ public static function authenticate_by_cookie($session_token) {
}
}
?>
+
View
@@ -21,7 +21,7 @@ function create($request) {
}
function login($request) {
- if (User::authenticate($request['post']['login'], $request['post']['password'])) {
+ if (isset($request['post']['login']) && isset($request['post']['password']) && (User::authenticate($request['post']['login'], $request['post']['password']))) {
Header("Location: /page");
} else {
$this->build('login.html.php', 'Вход');
@@ -51,3 +51,4 @@ function update($request) {
}
}
?>
+
View
@@ -2,15 +2,16 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title><?php (isset($title)) ? $title.' ' : '' ?>Гостевая книга ))</title>
+ <title><?php print (isset($title)) ? $title.' - ' : '' ?>Гостевая книга</title>
<link href="/stylesheets/style.css?1230116979" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div id="header">
+ <img id="logo" src="http://www.softlinegroup.com/images/softline_logo.png" alt="SoftLine"/>
<h1 id="title">Тестовое задание "Гостевая книга переросток"</h1>
<div id="header-nav">
- <?php if ($params->user) { ?>
+ <?php if ((isset($params)) && ($params->user)) { ?>
<p><a href="/page">Главная</a> | <a href="/page/add">Добавить новую заметку</a> | <a href="/logoff">Выход</a></p>
<?php } else { ?>
<div id="header-nav-login-forms">
@@ -26,9 +27,9 @@
<?php } ?>
</div>
</div>
-
+
<div id="content">
- <?php if (isset($template)) { include 'app/template/'.strtolower(get_class($this))."/{$template}"; } ?>
+ <?php if (isset($template)) { include 'app/template/'.$classname."/{$template}"; } ?>
</div>
<div style="height:71px;"></div>
</div>
@@ -39,4 +40,5 @@
</div>
</div>
</body>
-</html>
+</html>
+
@@ -2,9 +2,24 @@
<div class="post_comment">
<div class="inner">
<h2>Написать заметку.</h2>
- <p class="h"><label>Заголовок</label><input class="required" type="text" name="title" value="<?php print isset($data) ? $data->title : '' ?>"/></p>
- <p><label>Текст</label><textarea name="body" rows="10" cols="70"><?php print isset($data) ? $data->body : '' ?></textarea></p>
+ <p class="h">
+ <label for="title">Заголовок</label>
+ <input class="required" type="text" name="title" value="<?php print isset($data) ? $data->title : '' ?>"/>
+ </p>
+ <p class="h">
+ <label for="public_at">Дата публикации</label>
+ <input type="text" name="public_at" value="<?php print isset($data) ? $data->public_at : '' ?>"/>
+ </p>
+ <p>
+ <label for="teaser">Аннонс</label>
+ <textarea name="teaser" rows="10" cols="70"><?php print isset($data) ? $data->teaser : '' ?></textarea>
+ </p>
+ <p>
+ <label for="body">Текст</label>
+ <textarea name="body" rows="10" cols="70"><?php print isset($data) ? $data->body : '' ?></textarea>
+ </p>
<p class="button"><input type="submit" value="Опубликовать"/></p>
</div>
</div>
</form>
+
@@ -10,11 +10,14 @@
<a href="/page/{$article->id}/delete">Удалить</a>
</div>
EOD;
+ $content = $article->teaser == '' ? $article->body : $article->teaser;
print <<<EOD
<div id="article-{$article->id}" class="article">
<h2><a href="/page/{$article->id}">{$article->title}</a></h2>
+ <span class="data">{$article->public_at}</span>
+ <span class="author"></span>
{$action}
- <p>{$article->body}</p>
+ <p>{$content}</p>
</div>
EOD;
}
@@ -6,7 +6,7 @@
print "<div class=\"comment{$cclass}\" id=\"comment-{$comment->id}\"><h2>";
if ($comment->url) {
print "<a href=\"{$comment->url}\" class=\"url\">{$comment->name}</a>";
- if ($comment->user_id == $params->user['id'] || $data->user_id == $params->user['id'])
+ if ($params->user && ($comment->user_id == $params->user['id'] || $data->user_id == $params->user['id']))
print "<a href=\"/comment/{$comment->id}/delete\">удалить</a>";
} else
print $comment->name;
@@ -0,0 +1,29 @@
+<ul>
+ <li>PHP 5.3 - <?php $version = phpversion(); print '<span>'.$version.'</span>'; $req = strpos($version, '5.3.'); print ' - <span class="system'.($req === false ? ' red' : ' green').'">'.($req === false ? 'r' : 'a').'</span>'; ?></li>
+ <li>MySQL 5.1</li>
+</ul>
+<form action="/" method="post">
+ <div class="post_comment">
+ <div class="inner">
+ <h2>Данные для подключения к базе данных.</h2>
+ <p class="h">
+ <label for="host">Host</label>
+ <input class="required" type="text" name="host" value="<?php print isset($data) ? $data->host : '' ?>"/>
+ </p>
+ <p class="h">
+ <label for="user">User</label>
+ <input class="required" type="text" name="user" value="<?php print isset($data) ? $data->user : '' ?>"/>
+ </p>
+ <p class="h">
+ <label for="pass">Password</label>
+ <input class="required" type="text" name="pass" value="<?php print isset($data) ? $data->pass : '' ?>"/>
+ </p>
+ <p class="h">
+ <label for="name">Database name</label>
+ <input class="required" type="text" name="name" value="<?php print isset($data) ? $data->name : '' ?>"/>
+ </p>
+ <p class="button"><input type="submit" value="Сохранить"/></p>
+ </div>
+ </div>
+</form>
+
View
@@ -1,10 +1,6 @@
-<?php
-
-$database = array(
+<?php $database = array (
'host' => 'localhost',
'user' => 'root',
- 'pass' => '',
- 'name' => 'miniskel'
-);
-
-?>
+ 'pass' => 'zakzlo',
+ 'name' => 'miniskel',
+) ?>
View
@@ -0,0 +1,11 @@
+<?php
+
+$database = array(
+ 'host' => 'localhost',
+ 'user' => 'root',
+ 'pass' => 'zakzlo',
+ 'name' => 'miniskel'
+);
+
+?>
+
Oops, something went wrong.

0 comments on commit 4a2d40c

Please sign in to comment.