Create a gist now

Instantly share code, notes, and snippets.

@Sobak /index.php Secret
Created Apr 4, 2016

What would you like to do?
Pierwsza wersja Codice, 2011 rok. Wtedy jeszcze pod nazwą "Tablica".
<?php
ob_start();
session_start();
setlocale(LC_TIME, 'pl_PL');
// Konfiguracja
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
{
$db_host = 'localhost';
$db_name = 'tablica';
$db_user = 'root';
$db_pass = '';
}
else
{
$db_host = 'db28.1and1.pl';
$db_name = 'db342895925';
$db_user = 'dbo342895925';
$db_pass = '[ciach - tak, bylo zahardcodowane]';
}
$user = 'Sobak';
$pass = '[zahardcodowany hash sha256]';
// Dwie szybkie funkcje do DB
function query ($query)
{
$resource = @mysql_query($query) or die('<p>Nie udało się wykonać zapytania!<br><code>'.mysql_error().'</code></p>');
return $resource;
}
function fetch ($result)
{
return @mysql_fetch_array($result);
}
// F-cja do pokazywania linków do etykiet
function returnLabelsLinks ()
{
global $note;
$labels = explode(' ', $note['labels']);
$links = '';
foreach ($labels as $label)
{
$links .= '<a href="?m=label&amp;id='.$label.'">'.$label.'</a> ';
}
return $links;
}
// Główna f-cja
function showNotes ($result)
{
global $note;
while($note = fetch($result))
{
if ($note['end_time'] == '0' or ($note['end_time'] != '0' and time() < $note['end_time'] and $note['is_done'] != '1'))
echo '<div class="note normal">';
elseif ($note['is_done'] == '1')
echo '<div class="note done">';
elseif ($note['end_time'] != '0' and time() > $note['end_time'] and $note['is_done'] != '1')
echo '<div class="note undone">';
echo str_replace(PHP_EOL, '<br>', $note['content']);
echo '<span class="details"><span class="details-left"><a href="?m=del&amp;id='.$note['id'].'" onclick="return confirm(\'Czy na pewno usunąć?\')">Usuń</a> <a href="?m=edit&amp;id='.$note['id'].'">Edytuj</a> ';
if ($note['end_time'] != '0' and $note['is_done'] != '1') echo '<a href="?m=done&amp;id='.$note['id'].'">Zrobione</a>';
echo '</span><span class="details-right">';
echo date('d-m-Y H:i', $note['time']);
if ($note['end_time'] != '0') echo ' - '.date('d-m-Y', $note['end_time']);
echo '<br>'.returnLabelsLinks().'</span></span>';
echo "</div>\n\n";
}
}
// Połącz z db
@mysql_connect($db_host, $db_user, $db_pass) or die('<p>Nie udalo sie polaczyc z db</p>');
@mysql_select_db($db_name) or die('<p>Nie udalo sie wybrac db</p>');
?>
<!DOCTYPE html>
<html lang="pl" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Tablica</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<?php
// Sprawdź czy zalogowany
if ($_SESSION['user'] != $user or $_SESSION['pass'] != $pass)
{
// Pokaż formularz logowania
?>
<h3>Logowanie</h3>
<form action="" method="post" id="login">
<b>Login:</b> <input type="text" name="fuser" /><br>
<b>Hasło:</b> <input type="password" name="fpass" /><br>
<input type="hidden" name="fsend" value="yes" />
<input type="submit" value="Zaloguj" />
</form>
<?php
// Sprawdź czy wysłano formularz
// Jeśli tak, to sprawdź dane
$fuser = $_POST['fuser'];
$fpass = $_POST['fpass'];
if ($_POST['fsend'] == 'yes') {
if ($fuser != $user or hash('sha256', $fpass) != $pass)
{
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
query("INSERT INTO log (login, status, time, ip) VALUES('$fuser', 0, $time, '$ip');");
echo '<p class="error">Nieprawidłowe dane!</p>';
}
elseif ($fuser == $user and hash('sha256', $fpass) == $pass)
{
$_SESSION['user'] = $fuser;
$_SESSION['pass'] = hash('sha256', $fpass);
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
query("INSERT INTO log (login, status, time, ip) VALUES('$fuser', 1, $time, '$ip');");
header('Location: index.php');
}
}
}
// Zalogowany, pokaż tablicę ;)
elseif ($_SESSION['user'] == $user && $_SESSION['pass'] == $pass)
{
// Pokaż menu
echo '<p><a href="index.php?m=logout" class="logout">Wyloguj</a></p>';
// Sprawdź czy zdefiniowano zmienną m - mode
switch ($_GET['m'])
{
case 'logout':
session_destroy();
header('Location: index.php');
break;
case 'del':
$id = mysql_real_escape_string($_GET['id']);
query("DELETE FROM notes WHERE id = $id");
header('Location: index.php');
break;
case 'done':
$id = mysql_real_escape_string($_GET['id']);
query("UPDATE notes SET is_done = '1' WHERE id = $id");
header('Location: index.php');
break;
case 'show_log':
$result = query("SELECT * FROM log ORDER BY time DESC");
echo '<table border="1"><tr><td>Login</td> <td>Status</td> <td>IP</td> <td>Czas</td></tr>';
while ($log = fetch($result))
{
echo '<tr><td>'.$log['login'].'</td> <td>';
echo $log['status'] == '1' ? 'Poprawne' : 'Niepoprawne';
echo '</td> <td>'.$log['ip'].'</td> <td>'.date('d-m-Y H:i', $log['time']).'</td></tr>';
}
echo '</table>';
echo '<p><a href="?m=clear_log">Wyczyść</a><br><a href="index.php">Wróć</a></p>';
break;
case 'clear_log':
query("TRUNCATE TABLE log");
header('Location: index.php?m=show_log');
break;
case 'label':
$id = urldecode(mysql_real_escape_string($_GET['id']));
$result = query("SELECT id, content, labels, end_time, is_done, time FROM notes WHERE labels LIKE '%$id%' ORDER BY time DESC");
echo "<h3>Notki z etykietą '$id'</h3>\n";
showNotes($result);
echo '<p><a href="index.php">Wróć</a></p>';
break;
case 'edit':
$id = mysql_real_escape_string($_GET['id']);
$form = fetch(query("SELECT * FROM notes WHERE id = $id"))
?>
<h3>Edytuj notkę</h3>
<form action="" method="post" id="addNote">
<textarea name="content" id="newNote" required autofocus><?php echo $form['content']; ?></textarea><br>
<b>Etykiety, oddziel spacją:</b> <input type="text" name="labels" maxlength="255" value="<?php echo $form['labels']; ?>">
<b>Data końcowa:</b> <input type="text" name="end_time" value="<?php if ($form['end_time'] != 0) echo date('d-m-Y', $form['end_time']); ?>"><br>
<input type="hidden" name="send" value="yes">
<input type="submit" value="Wyślij" id="sendNote">
</form>
<?php
// Jeśli wysłano to zedytuj
if ($_POST['send'] == 'yes')
{
$content = mysql_real_escape_string($_POST['content']);
$labels = $_POST['labels'];
$end_time = strtotime($_POST['end_time']);
query("UPDATE notes SET content = '$content', labels = '$labels', end_time = '$end_time' WHERE id = $id");
header('Location: index.php');
}
$result = query("SELECT id, content, labels, end_time, is_done, time FROM notes ORDER BY time DESC");
showNotes($result);
break;
case 'do_backup':
echo '<div style="height: 250px; overflow: auto; font-family: monospace; width: 700px;">';
$createTable = mysql_fetch_row(query("SHOW CREATE TABLE notes"));
echo $createTable[1].'<br><br>';
$notesResult = query("SELECT * FROM notes");
while ($note = fetch($notesResult))
{
echo "INSERT INTO notes (id, content, labels, time, end_time, is_done) VALUES(".$note['id'].", '".str_replace(PHP_EOL, '<br>', htmlspecialchars($note['content']))."', '".$note['labels']."', ".$note['time'].", ".$note['end_time'].", ".$note['is_done'].");<br>\n";
}
echo '</div>';
echo '<p><a href="index.php">Wróć</a></p>';
break;
// Domyślne - pokaż notki i formularz dodawania nowych
default:
?>
<h3>Tablica</h3>
<form action="" method="post" id="addNote">
<textarea name="content" id="newNote" required placeholder="Treść notatki..." autofocus></textarea><br>
<b>Etykiety, oddziel spacją:</b> <input type="text" name="labels" maxlength="255">
<b>Data końcowa:</b> <input type="text" name="end_time"><br>
<input type="hidden" name="send" value="yes">
<input type="submit" id="sendNote">
</form>
<?php
// Jeśli wysłano to dodaj
if ($_POST['send'] == 'yes')
{
$content = mysql_real_escape_string($_POST['content']);
$labels = $_POST['labels'];
$end_time = strtotime($_POST['end_time']);
$time = time();
query("INSERT INTO notes (content, labels, time, end_time) VALUES('$content', '$labels', $time, '$end_time');");
header('Location: index.php');
}
$result = query("SELECT id, content, labels, end_time, is_done, time FROM notes ORDER BY time DESC");
showNotes($result);
break;
}
echo '<p id="log"><a href="index.php?m=do_backup">Zrób backup</a> | <a href="index.php?m=show_log">Rejestr logowań</a></p>';
}
?>
</div>
</body>
</html>
<?php
ob_end_flush();
* {
margin: 0;
padding: 0;
}
body {
background: #f8dd9b;
}
#container {
width: 750px;
background: #fbe1c8;
margin: 0 auto;
padding: 10px;
padding-top: 20px;
margin-top: 10px;
border: 5px double #eab595;
border-radius: 7px;
-o-border-radius: 7px;
-webkit-border-radius: 7px;
-khtml-border-radius: 7px;
}
/* Wylogowywanie */
a.logout {
float: right;
font-size: 80%;
}
/* Formularz dodawania */
form#addNote {
width: 700px;
margin: 0 auto;
margin-bottom: 20px;
}
textarea#newNote {
width: 650px;
margin: 0 auto;
border: 1px solid black;
height: 100px;
background: #f9eee7;
font-weight: bold;
color: #000;
}
input#sendNote {
border: 2px solid black;
background: #f9eee7;
color: #000;
width: 100px;
}
form#addNote b {
font-size: 85%
}
form#addNote input {
background: #f9eee7;
font-weight: bold;
color: #000;
border: 1px solid black;
}
/* Wpisy */
div.note {
width: 700px;
margin: 0 auto;
padding: 7px;
padding-bottom: 15px;
overflow: auto;
}
div.normal:hover {
background: #faeb9a;
}
div.undone {
background: #e66262;
}
div.undone:hover {
background: #f57e7e;
}
div.done {
background: #afeb6e;
}
div.done:hover {
background: #c0fc7f;
}
/* Detale */
.details {
font-size: 80%;
display: block;
}
.details-left {
float: left;
}
.details-right {
float: right;
text-align: right;
}
/* Formularz logowania */
form#login {
margin: 0 auto;
}
form#login input[type="text"], form#login input[type="password"] {
background: #f9eee7;
font-weight: bold;
color: #000;
border: 1px solid black;
}
form#login input[type="submit"] {
border: 2px solid black;
background: #f9eee7;
color: #000;
width: 100px;
}
/* Link do rejestru */
p#log {
margin-top: 20px;
font-size: 75%;
text-align: right;
}
a:link, a:visited {
color: #696867;
}
a:hover {
text-decoration: none;
color: #8e8e8e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment