Skip to content

Commit b1152ca

Browse files
authored
Merge pull request #27 from torbengb/develop
toolpool 0.1.0.prototype
2 parents 8c2503b + a95d8ed commit b1152ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3140
-1210
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
common/config.php
2+
todo.txt
3+
.idea/
14

2-
config/config.php
5+
common/dbconfig.php

.htaccess

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
DirectoryIndex index.php
22

3-
# ErrorDocument 403 error.php
4-
# ErrorDocument 404 error.php
5-
# ## ErrorDocument 500 error.php
3+
# ErrorDocument 403 /common/error.php
4+
# ErrorDocument 404 /common/error.php
5+
# ## ErrorDocument 500 /common/error.php
66
#
77
# php_flag display_startup_errors on
88
# php_flag display_errors on
@@ -33,4 +33,4 @@ DirectoryIndex index.php
3333
# Deny from all
3434
# Satisfy All
3535
# </Files>
36-
# ## end
36+
# ## end

LICENSE

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
The MIT License (MIT)
22

3-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
49

5-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
612

7-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This is a prototype!!
1+
# README
22

33
This prototype of `Tool Pool` aims to provide a proof of concept for a community of people wanting to share their tools for DIY home-improvement projects.
44

common.php

-16
This file was deleted.

common/common.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
session_start();
3+
$_SESSION['debug'] =
4+
0; // set to 1 for extra debug output; 0 for nothing.
5+
//if ($_SESSION['debug']==1) echo __LINE__ . " in " . __FILE__ ."<br>";
6+
if ($_SESSION['debug']==1) echo "Debug is ON: line ".__LINE__ . " in " . __FILE__ ."<br>";
7+
if (empty($_SESSION['csrf'])) {
8+
if (function_exists('random_bytes')) {
9+
$_SESSION['csrf'] = bin2hex(random_bytes(32));
10+
} elseif (function_exists('mcrypt_create_iv')) {
11+
$_SESSION['csrf'] = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
12+
} else {
13+
$_SESSION['csrf'] = bin2hex(openssl_random_pseudo_bytes(32));
14+
}
15+
}
16+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17+
// connect to the database:
18+
require "dbconfig.php";
19+
$dsn = "mysql:host=$host;dbname=$dbname";
20+
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
21+
$connection = new PDO($dsn, $username, $password, $options);
22+
$sql = "USE " . $dbname;
23+
$connection->exec($sql);
24+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25+
// common functions:
26+
function escape($html)
27+
{
28+
return htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
29+
}
30+
31+
function showMessage($line = 0, $file = "not specified", $message = "")
32+
{
33+
// usage: showMessage( __LINE__ , __FILE__ , "optional hint message" )
34+
// or: showMessage( __LINE__ , __FILE__ )
35+
echo "An error occurred at line " . $line . " in file " . $file . "!" . ($message ? "<br>Additional information:<br>" . $message : "<br>No additional details were provided. Sorry about that.");
36+
}
37+
38+
$statement = $connection->prepare("
39+
SELECT id, username FROM users
40+
WHERE ( deleted = '0000-00-00 00:00:00' OR deleted IS NULL )
41+
ORDER BY username
42+
");
43+
$statement->execute();
44+
$users = $statement->fetchAll();
45+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46+
// user management
47+
if (isset($_POST["login"])) {
48+
//if ($_SESSION['debug']) echo __LINE__ . " in " . __FILE__ . "<br>";
49+
if (!hash_equals($_SESSION['csrf'], $_POST['csrf']))
50+
if ($_SESSION['debug']==1) {
51+
echo __LINE__ . " in " . __FILE__ . "<br>";
52+
//die();
53+
}
54+
$currentuserid = $_POST["user"];
55+
$statement = $connection->prepare("
56+
SELECT id, username
57+
FROM users
58+
WHERE id = :id
59+
AND ( deleted = '0000-00-00 00:00:00' OR deleted IS NULL )
60+
");
61+
$statement->bindValue(':id', $currentuserid);
62+
$statement->execute();
63+
$result = $statement->fetch(PDO::FETCH_ASSOC);
64+
$_SESSION["currentuserid"] = $result['id'];
65+
$_SESSION["currentusername"] = $result['username'];
66+
unset($_POST["login"]);
67+
}
68+
if (isset($_POST["logout"])) {
69+
//if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
70+
// remove all session variables
71+
session_unset();
72+
// destroy the session
73+
session_destroy();
74+
}
75+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

common/error.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php require "/common/footer.php"; ?>
2+
3+
<?php
4+
$code = $_SERVER['REDIRECT_STATUS'];
5+
$codes = array(
6+
403 => 'Forbidden',
7+
404 => 'Not Found',
8+
500 => 'Internal Server Error'
9+
);
10+
$source_url = 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
11+
if (array_key_exists($code, $codes) && is_numeric($code)) {
12+
die("Error $code: {$codes[$code]}");
13+
} else {
14+
die('Unknown error');
15+
}
16+
?>
17+
18+
<?php require "/common/footer.php"; ?>

common/footer.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
</div> <!-- end body -->
2+
<div class="footer">
3+
<div class="navbar">
4+
<span class="topics">
5+
|| <a href="/index.php"><strong>Home</strong></a>
6+
|| <a href="/tools/list.php"><strong>Tools</strong></a>
7+
|| <a href="/users/list.php"><strong>Members</strong></a>
8+
|| <a href="/loans/list.php"><strong>Loans</strong></a>
9+
|| <a href="/taxonomy/list.php"><strong>Taxonomy</strong></a>
10+
||
11+
</span>
12+
</div>
13+
<div class="meta">
14+
<span>
15+
|| <strong alt="* opens in new page" title="* opens in new page"><a href="https://github.com/torbengb/toolpool#README" target="_new">About</a></strong>
16+
|| <strong alt="* opens in new page" title="* opens in new page"><a href="https://github.com/torbengb/toolpool" target="_new">Github</a></strong>
17+
|| <strong alt="* opens in new page" title="* opens in new page"><a href="https://github.com/torbengb/toolpool/issues?q=is%3Aopen+is%3Aissue+label%3Abug" target="_new">Known bugs</a></strong>
18+
|| <strong alt="* opens in new page" title="* opens in new page"><a href="https://github.com/torbengb/toolpool/issues/new" target="_new">Report a bug</a></strong>
19+
||
20+
</span>
21+
</div>
22+
</div>
23+
<form method="post" action="/">
24+
<input type="hidden" name="csrf" value="<?php echo escape($_SESSION['csrf']); ?>">
25+
<input type="hidden" name="id" value="<?php echo escape($user['id']); ?>">
26+
<label class="label" for="user"><span class="labeltext">select user:</span>
27+
<select class="input" name="user" id="user">
28+
<?php foreach ($users as $row) : ?>
29+
<option
30+
name="user"
31+
id="user"
32+
value="<?php echo escape($row['id']); ?>"
33+
<?php echo(escape($row["id"]) == escape($_SESSION["currentuserid"]) ? "selected='selected'" : NULL) ?>
34+
><?php echo escape($row['username']); ?></option>
35+
<?php endforeach; ?>
36+
</select>
37+
</label>
38+
<button class="button submit" type="submit" name="login" value="login">Switch!</button>
39+
</form>
40+
</body>
41+
</html>

common/header.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="x-ua-compatible" content="ie=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
9+
<title>TOOL||POOL</title>
10+
11+
<link rel="stylesheet" href="/common/style.css">
12+
</head>
13+
14+
<body>
15+
<div class="header">
16+
<h1 class="sitename">TOOL||POOL on <?php echo $_SERVER['HTTP_HOST']; ?></h1>
17+
<div class="navbar">
18+
<div class="topics">
19+
|| <a href="/index.php"><strong>Home</strong></a>
20+
|| <a href="/tools/list.php"><strong>Tools</strong></a>
21+
|| <a href="/profile"><strong>My account</strong></a>
22+
|| <span style="float:right"><?php
23+
if (isset($_SESSION['currentusername'])) {
24+
echo '<a href="/profile/">Hello <b>' . escape($_SESSION['currentusername']) . '</b>!</a>';
25+
} else {
26+
echo '<a href="/profile/">Login</a> or <a href="/users/new.php">register!</a>';
27+
}
28+
?></span>
29+
</div>
30+
</div>
31+
</div>
32+
<div class="mainbody">

common/install.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
require "common.php";
3+
4+
function runsqlfile($file) {
5+
//echo __LINE__ . "<br>";
6+
$sql = file_get_contents($file);
7+
$connection->exec($sql);
8+
//echo __LINE__ . "<br>";
9+
return;
10+
}
11+
12+
/**
13+
* @param PDO $connection
14+
*/
15+
function arst(PDO $connection, str $file): void
16+
{
17+
echo __LINE__ . "<br>";
18+
$sql = file_get_contents($file);
19+
$connection->exec($sql);
20+
echo __LINE__ . "<br>";
21+
}
22+
23+
try {
24+
// first create database:
25+
/* INSTALLER CANNOT CREATE DATABASE on DreamHost!! (and neither can a local SQL client!)
26+
* The database MUST exist before the installer is run!
27+
* The installer can do everything after that.
28+
$sql = "CREATE DATABASE IF NOT EXISTS " . $dbname;
29+
$connection->exec($sql);
30+
*/
31+
echo __LINE__ . "<br>";
32+
// then open database:
33+
$sql = "USE " . $dbname;
34+
$connection->exec($sql);
35+
echo __LINE__ . "<br>";
36+
// and then create tables:
37+
$sql = file_get_contents("../database/countries.sql");
38+
$connection->exec($sql);
39+
echo __LINE__ . "<br>";
40+
$sql = file_get_contents("../database/regions.sql");
41+
$connection->exec($sql);
42+
$sql = file_get_contents("../database/users.sql");
43+
$connection->exec($sql);
44+
$sql = file_get_contents("../database/taxonomy.sql");
45+
$connection->exec($sql);
46+
$sql = file_get_contents("../database/tools.sql");
47+
$connection->exec($sql);
48+
$sql = file_get_contents("../database/loans.sql");
49+
$connection->exec($sql);
50+
echo __LINE__ . "Successfully created tables and inserted base data.<br>";
51+
52+
// now load test data unless it's in production:
53+
switch ($dbname) {
54+
case "toolpool":
55+
// do nothing in production!
56+
break;
57+
case "toolpool_dev":
58+
case "toolpool_test":
59+
// runsqlfile("../database/testdata.sql");
60+
echo "Successfully inserted test data.<br>";
61+
break;
62+
default:
63+
die('Cannot insert test data into unknown environment!<br>');
64+
}
65+
66+
echo "Now <a href='../index.php'>go to the homepage</a>.";
67+
} catch(PDOException $error) {
68+
echo /* $sql . "<br>" . */ $error->getMessage() . " Try visiting <a href='../index.php'>the homepage</a>.";
69+
}

0 commit comments

Comments
 (0)