This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
152 lines (151 loc) · 5.16 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once('card.php');
require_once('parse.php');
require_once('Twig/Autoloader.php');
require_once('login/Auth.php');
session_start();
Site::main();
class Site{
private static $settings;
private static $db;
public static function main()
{
self::$settings = json_decode(file_get_contents('settings.json'), true);
if(self::$settings['debug'])
{
require_once('debug/krumo/krumo.php');
}
$tempvars['settings'] = self::$settings;
$auth = new Hybrid_Auth(self::$settings['auth']);
//dateabase
self::$db = new PDO(self::$settings['database']['driver'].':'.
'host=' .self::$settings['database']['host'].';'.
'dbname=' .self::$settings['database']['name'].';'.
'charset='.self::$settings['database']['charset'],
self::$settings['database']['username'],
self::$settings['database']['password']);
//check login stuff
if (isset($_POST['login'])) {
switch ($_POST['login']) {
case 'twitter':
$_SESSION['provider'] = 'Twitter';
break;
case 'google':
$_SESSION['provider'] = 'Google';
break;
case 'facebook':
$_SESSION['provider'] = 'Facebook';
break;
case 'steam':
$_SESSION['provider'] = 'Steam';
break;
}
$auth->authenticate($_SESSION['provider']);
}
if (isset($_SESSION['provider']) && Hybrid_Auth::isConnectedWith($_SESSION['provider'])) {
$user = $auth->authenticate($_SESSION['provider']);
$profile = $user->getUserProfile();
$query = 'SELECT id, username FROM ws_users WHERE provider = ? AND provider_id = ?';
$stmt = self::$db->prepare($query);
$stmt->execute(array($_SESSION['provider'], $profile->identifier));
if($user = $stmt->fetch(PDO::FETCH_ASSOC))
{
$_SESSION['user'] = $user;
unset($_SESSION['provider']);
}
else
{
if (isset($_POST['username']) && preg_match('|^[a-zA-Z]{3,15}$|', $_POST['username']))
{
$stmt = self::$db->prepare('INSERT INTO ws_users (provider, provider_id, username) VALUES (?,?,?)');
$stmt->execute(array($_SESSION['provider'], $profile->identifier, $_POST['username']));
$_SESSION['user'] = array(self::$db->lastInsertId(), $_POST['username']);
unset($_SESSION['provider']);
}
else
{
$tempvars['user'] = $profile;
$tempvars['validated'] = false;
}
}
}
//page output
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates');
$twigop['cache'] = getcwd().'/cache';
if (self::$settings['debug'])
{
$twigop['cache'] = false;
}
$twig = new Twig_Environment($loader, $twigop);
$path = Parse::parsepath();
if(isset($path[0]))
{
switch($path[0])
{
case "card":
$cardno = Card::tocardno($path[1]);
$query = 'SELECT
wc.cardno cardno, wc.name name, wc.kana kana, wc.rarity rarity, wc.side side, wc.color color,
wc.type type, wc.level level, wc.cost cost, wc.power power, wc.soul soul, wc.triggers triggers,
wc.traits traits, wc.text text, wc.flavor flavor, wc.locale locale, CASE
WHEN wc.locale = "en" THEN we.NAME
WHEN wc.locale = "jp" THEN wj.NAME
END expansion FROM ws_cards wc
LEFT JOIN ws_ensets we ON wc.expansion = we.id
LEFT JOIN ws_jpsets wj ON wc.expansion = wj.id WHERE wc.cardno = ?';
$stmt = self::$db->prepare($query);
$stmt->execute(array($cardno));
$card = $stmt->fetch(PDO::FETCH_ASSOC);
$card = new Card($card);
//check for another language version
$relations;
switch($card->getlocale())
{
case 'en':
$relations = 'SELECT `jpcardno` FROM `ws_relations` WHERE `encardno` = ?';
break;
case 'jp':
$relations = 'SELECT `encardno` FROM `ws_relations` WHERE `jpcardno` = ?';
break;
}
$stmt = self::$db->prepare($relations);
$stmt->execute(array($cardno));
$altcard = $stmt->fetch(PDO::FETCH_NUM);
$tempvars['card'] = $card->getvars();
if($altcard)
{
$tempvars['altcard'] = Card::tourl($altcard[0]);
}
//get alternate rarities
$rarities = 'SELECT `cardno` FROM `ws_cards` WHERE `name` = ? AND `cardno` != ?';
$stmt = self::$db->prepare($rarities);
$stmt->execute(array($card->getname(), $card->getcardno()));
$altrarity = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($altrarity as $value) {
$value['cardlink'] = Card::tourl($value['cardno']);
$tempvars['altrarity'][] = $value;
}
$template = $twig->loadTemplate('card.html');
echo $template->render($tempvars);
break;
case 'login':
require_once( "login/Endpoint.php" );
Hybrid_Endpoint::process();
break;
case 'api':
require_once( "api.php" );
api::main($path, self::$db);
break;
default: //404
header('Location: /');
exit;
}
}
else
{
$template = $twig->loadTemplate('index.html');
echo $template->render($tempvars);
}
}
}