Skip to content

Commit 80cfff8

Browse files
committed
Webdev project
0 parents  commit 80cfff8

Some content is hidden

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

45 files changed

+17820
-0
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
config.ini
2+
3+
vendor/
4+
node_modules/
5+
6+
# Laravel 4 specific
7+
bootstrap/compiled.php
8+
app/storage/
9+
10+
# Laravel 5 & Lumen specific
11+
bootstrap/cache/
12+
.env.*.php
13+
.env.php
14+
.env
15+
16+
# Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer
17+
.rocketeer/
18+
19+
# PHPStorm
20+
.idea

.htaccess

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# remove file extension from url
2+
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
3+
4+
<FilesMatch "\.(?:ini)$">
5+
Order allow,deny
6+
Deny from all
7+
</FilesMatch>

adminer.php

Lines changed: 1807 additions & 0 deletions
Large diffs are not rendered by default.

app/config.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
session_start();
3+
4+
set_error_handler('errorHandler');
5+
function errorHandler($error_number, $error_message, $filename, $error_line_number)
6+
{
7+
$date = date('Y-m-d H:i:s (T)');
8+
$f = fopen('error.txt', 'a');
9+
if (!empty($f)) {
10+
$filename = str_replace($_SERVER['DOCUMENT_ROOT'], '', $filename);
11+
$err = "$date: $error_message = $filename = $error_line_number\r\n";
12+
fwrite($f, $err);
13+
fclose($f);
14+
}
15+
}
16+
17+
// read main config file
18+
$config_file = 'config.ini';
19+
$config = parse_ini_file($config_file);
20+
if (!$config) {
21+
echo 'Could not read config file - please check the config file';
22+
die();
23+
}
24+
25+
// DB PARAMETERS
26+
define('DB_HOST', $config['db_host']);
27+
define('DB_USERNAME', $config['db_username']);
28+
define('DB_PASSWORD', $config['db_password']);
29+
define('DB_NAME', $config['db_name']);
30+
31+
class DB
32+
{
33+
private static $instance = null;
34+
35+
static public function getInstance()
36+
{
37+
if (self::$instance == null) {
38+
self::$instance = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
39+
mysqli_set_charset(self::$instance, "utf8");
40+
}
41+
return self::$instance;
42+
}
43+
44+
private function __construct()
45+
{
46+
}
47+
48+
private function __clone()
49+
{
50+
}
51+
}
52+
53+
// VK API parameters
54+
$vk_app_id = $config['vk_app_id'];
55+
$vk_api_secure_key = $config['vk_api_secure_key'];
56+
$vk_api_redirect_uri = $config['vk_api_redirect_uri'];
57+
$vk_api_url = $config['vk_api_url'];
58+
$vk_api_token_url = $config['vk_api_token_url'];
59+
$vk_api_user_info_url = $config['vk_api_user_info_url'];
60+
$vk_api_parameters = array(
61+
'client_id' => $vk_app_id,
62+
'redirect_uri' => $vk_api_redirect_uri,
63+
'response_type' => 'code'
64+
);
65+
66+
// Facebook API parameters
67+
$facebook_app_id = $config['facebook_app_id'];
68+
$facebook_api_secure_key = $config['facebook_api_secure_key'];
69+
$facebook_api_redirect_uri = $config['facebook_api_redirect_uri'];
70+
$facebook_api_url = $config['facebook_api_url'];
71+
$facebook_api_token_url = $config['facebook_api_token_url'];
72+
$facebook_api_user_info_url = $config['facebook_api_user_info_url'];
73+
$facebook_api_parameters = array(
74+
'client_id' => $facebook_app_id,
75+
'redirect_uri' => $facebook_api_redirect_uri,
76+
'response_type' => 'code',
77+
'scope' => 'public_profile'
78+
);
79+
80+
// Google API parameters
81+
$google_app_id = $config['google_app_id'];
82+
$google_api_secure_key = $config['google_api_secure_key'];
83+
$google_api_redirect_uri = $config['google_api_redirect_uri'];
84+
$google_api_url = $config['google_api_url'];
85+
$google_api_token_url = $config['google_api_token_url'];
86+
$google_api_user_info_url = $config['google_api_user_info_url'];
87+
88+
$google_api_profile_scope = ' https://www.googleapis.com/auth/userinfo.profile';
89+
$google_api_email_scope = 'https://www.googleapis.com/auth/userinfo.email';
90+
91+
$google_api_parameters = array(
92+
'redirect_uri' => $google_api_redirect_uri,
93+
'response_type' => 'code',
94+
'client_id' => $google_app_id,
95+
'scope' => $google_api_profile_scope
96+
);

app/lib.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
function buildTree(array $messages, $parent_id)
3+
{
4+
$result = array();
5+
foreach ($messages as $message) {
6+
if ($message['parent_id'] == $parent_id) {
7+
$children = buildTree($messages, $message['id']);
8+
if ($children) {
9+
$message['children'] = $children;
10+
}
11+
$result[] = $message;
12+
}
13+
}
14+
return $result;
15+
}

app/message.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
require_once('config.php');
3+
4+
class Message
5+
{
6+
const MESSAGE_TYPE = 0; // message
7+
const COMMENT_TYPE = 1; // comment of message/comment of comment
8+
9+
function __construct($user_id, $type, $parent_id, $text)
10+
{
11+
$created_at = date('Y-m-d H:i:s');
12+
13+
$this->id = 0;
14+
$this->user_id = $user_id;
15+
$this->type = $type;
16+
$this->parent_id = $parent_id;
17+
$this->text = $text;
18+
$this->created_at = $created_at;
19+
}
20+
21+
public function save()
22+
{
23+
$sql_query = "INSERT INTO messages (user_id,type,parent_id,text,created_at) VALUES (?,?,?,?,?)";
24+
$stmt = mysqli_prepare(DB::getInstance(), $sql_query);
25+
mysqli_stmt_bind_param($stmt, 'iiiss', $this->user_id, $this->type, $this->parent_id, $this->text, $this->created_at);
26+
if (mysqli_stmt_execute($stmt)) {
27+
$id = mysqli_stmt_insert_id($stmt);
28+
$this->id = $id;
29+
}
30+
}
31+
32+
public static function get($message_id)
33+
{
34+
$result = [];
35+
$query_result = mysqli_query(DB::getInstance(), "SELECT * FROM messages where id = $message_id LIMIT 1");
36+
$rows = mysqli_fetch_assoc($query_result);
37+
if ($rows) {
38+
$result = $rows;
39+
}
40+
return $result;
41+
}
42+
43+
public static function getAll()
44+
{
45+
$result = [];
46+
$query_result = mysqli_query(DB::getInstance(), "select messages.*, users.first_name, users.last_name from messages" .
47+
" left join users on messages.user_id = users.id" .
48+
" ORDER BY messages.created_at ASC");
49+
while ($row = mysqli_fetch_assoc($query_result)) {
50+
$rows[] = $row;
51+
}
52+
if ($rows) {
53+
$result = $rows;
54+
}
55+
56+
return $result;
57+
}
58+
59+
public static function updateText($id, $text)
60+
{
61+
$message = self::get($id);
62+
$user = User::getAttributes();
63+
if ($message AND $user AND $message['user_id'] == $user['user_id']) {
64+
$sql_query = "UPDATE messages SET text = ? where id = ?";
65+
$stmt = mysqli_prepare(DB::getInstance(), $sql_query);
66+
mysqli_stmt_bind_param($stmt, 'ss', $text, $id);
67+
mysqli_stmt_execute($stmt);
68+
mysqli_stmt_close($stmt);
69+
}
70+
}
71+
}

app/user.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
require_once('config.php');
3+
require_once('lib.php');
4+
5+
class User
6+
{
7+
const ACCOUNT_TYPE_EMPTY = 0;
8+
const ACCOUNT_TYPE_VK = 1;
9+
const ACCOUNT_TYPE_FACEBOOK = 2;
10+
const ACCOUNT_TYPE_GOOGLE = 3;
11+
12+
function __construct($account_type, $uid, $first_name, $last_name)
13+
{
14+
// ACCOUNT_TYPE_GOOGLE not implemented yet
15+
if ($account_type != self::ACCOUNT_TYPE_VK AND
16+
$account_type != self::ACCOUNT_TYPE_FACEBOOK AND
17+
$account_type != self::ACCOUNT_TYPE_GOOGLE) {
18+
throw new Exception("Unsupported account type!");
19+
}
20+
21+
$this->id = 0;
22+
$this->uid = $uid;
23+
$this->account_type = $account_type;
24+
$this->first_name = $first_name;
25+
$this->last_name = $last_name;
26+
27+
// save user to DB if user doesn't exist
28+
if (!$this->isExist()) {
29+
$this->saveToDb();
30+
}
31+
}
32+
33+
function authenticate()
34+
{
35+
$_SESSION['user'] = (array)$this;
36+
}
37+
38+
static function isAuthenticate()
39+
{
40+
$result = false;
41+
42+
if (isset($_SESSION['user']) && isset($_SESSION['user']['id'])) {
43+
$result = true;
44+
}
45+
46+
return $result;
47+
}
48+
49+
function isExist()
50+
{
51+
$result = false;
52+
53+
$sql_query = "SELECT * FROM users where account_type = $this->account_type AND uid = $this->uid LIMIT 1";
54+
$stmt = mysqli_prepare(DB::getInstance(), $sql_query);
55+
$stmt->execute();
56+
$res = $stmt->get_result();
57+
$row = $res->fetch_assoc();
58+
if ($row) {
59+
$this->id = $row['id'];
60+
// reset user name by db values
61+
$this->first_name = $row['first_name'];
62+
$this->last_name = $row['last_name'];
63+
$result = true;
64+
}
65+
66+
return $result;
67+
}
68+
69+
function saveToDb()
70+
{
71+
$sql_query = "INSERT INTO users (uid,account_type,first_name,last_name) VALUES (?,?,?,?)";
72+
$stmt = mysqli_prepare(DB::getInstance(), $sql_query);
73+
mysqli_stmt_bind_param($stmt, 'ssss', $this->uid, $this->account_type, $this->first_name, $this->last_name);
74+
if (mysqli_stmt_execute($stmt)) {
75+
$id = mysqli_stmt_insert_id($stmt);
76+
$this->id = $id;
77+
}
78+
}
79+
80+
static function getAttributes()
81+
{
82+
$result = [];
83+
84+
if (isset($_SESSION['user'])) {
85+
$user = $_SESSION['user'];
86+
$result['user_id'] = $user['id'];
87+
$result['account_type'] = $user['account_type'];
88+
$result['full_name'] = $user['first_name'] . ' ' . $user['last_name'];
89+
$result['first_name'] = $user['first_name'];
90+
$result['last_name'] = $user['last_name'];
91+
}
92+
93+
return $result;
94+
}
95+
}

app/webdev.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
SET NAMES utf8;
2+
SET time_zone = '+00:00';
3+
SET foreign_key_checks = 0;
4+
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
5+
6+
DROP TABLE IF EXISTS `messages`;
7+
CREATE TABLE `messages` (
8+
`id` int(11) NOT NULL AUTO_INCREMENT,
9+
`user_id` int(11) NOT NULL,
10+
`type` int(11) NOT NULL,
11+
`parent_id` int(11) NOT NULL DEFAULT '0',
12+
`text` text CHARACTER SET utf8 NOT NULL,
13+
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
14+
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
15+
PRIMARY KEY (`id`)
16+
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
17+
18+
19+
DROP TABLE IF EXISTS `users`;
20+
CREATE TABLE `users` (
21+
`id` int(11) NOT NULL AUTO_INCREMENT,
22+
`uid` varchar(30) NOT NULL,
23+
`account_type` int(11) NOT NULL,
24+
`first_name` text NOT NULL,
25+
`last_name` text NOT NULL,
26+
`email` text NOT NULL,
27+
`ip` text NOT NULL,
28+
`hash` text NOT NULL,
29+
PRIMARY KEY (`id`)
30+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

error.txt

Whitespace-only changes.

facebook.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
require_once('app/config.php');
3+
require_once('app/user.php');
4+
5+
if( count($_GET) == 0){
6+
header('Location: index.php');
7+
}
8+
9+
if (isset($_GET['code'])) {
10+
$result = false;
11+
$params = array(
12+
'client_id' => $facebook_app_id,
13+
'client_secret' => $facebook_api_secure_key,
14+
'code' => $_GET['code'],
15+
'redirect_uri' => $facebook_api_redirect_uri
16+
);
17+
$url_with_params = $facebook_api_token_url . '?' . urldecode(http_build_query($params));
18+
$content = file_get_contents($url_with_params);
19+
// Warning: use only parse_str on get token step,
20+
// because facebook return string NOT JSON
21+
parse_str($content, $token);
22+
23+
if (isset($token['access_token'])) {
24+
$params = array(
25+
'access_token' => $token['access_token'],
26+
'fields' => 'id,first_name,last_name'
27+
);
28+
$user_info_url = $facebook_api_user_info_url . '?' . urldecode(http_build_query($params));
29+
$content = file_get_contents($user_info_url);
30+
$user_info = json_decode($content, true);
31+
if (isset($user_info['id'])) {
32+
$result = true;
33+
}
34+
}
35+
36+
if ($result) {
37+
try {
38+
$user = new User(User::ACCOUNT_TYPE_FACEBOOK, $user_info['id'], $user_info['first_name'], $user_info['last_name']);
39+
} catch (Exception $e) {
40+
header('Location: index.php');
41+
}
42+
$user->authenticate();
43+
44+
// Redirect auth user to messages page
45+
header('Location: messages.php');
46+
} else {
47+
header('Location: index.php');
48+
}
49+
}

0 commit comments

Comments
 (0)