forked from entomb/dragonchan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
139 lines (115 loc) · 3.04 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
<?php
/**
* DragonRaid - A prototype script to transform any /bant/ thread into a dragon slaying match.
*
* @author Jonathan Tavares <[email protected]>
* @license GNU General Public License, version 3
* @link https://github.com/entomb/dragonchan GitHub Source
* @filesource
*
*
*/
//Include game and version info
include('lib/globalinfo.php');
//no errors on production please
error_reporting(0);
/**
* @route /info
* Load the "more info" page template
*/
if(isset($_GET['id']) && $_GET['id'] === "info" || !isset($_GET['id'])){
include 'template/info.tpl';
exit();
}
/**
* The thread ID
* @var int
*/
$thread_id = (isset($_GET['id']) ? (int)$_GET['id'] : 0);
/**
* the API url for the thread FEED
* IDEA: extend this to other boards
* @var string
*/
$api_url = "http://a.4cdn.org/bant/thread/$thread_id.json";
/**
* @try
* decode the 4chan-api response.
*/
if($thread_id > 0) {
try
{
/**
* MEMCACHE implementation
*/
if(getenv("MEMCACHIER_SERVERS")){
include('lib/MemcacheSASL.php');
$server_pieces = explode(':', getenv("MEMCACHIER_SERVERS"));
$m = new MemcacheSASL;
$m->addServer($server_pieces[0], $server_pieces[1]);
$m->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD"));
$thread_cache_key = "4chan_thread_$thread_id";
if(!($THREAD = $m->get($thread_cache_key))){
$_is_cached_request = false;
//no cache is set
$JSON = file_get_contents($api_url);
$THREAD = json_decode($JSON);
$m->add($thread_cache_key,$THREAD,getenv("MEMCACHIER_EXPIRE"));
}else{
$_is_cached_request = true;
}
}else{
$_is_cached_request = false;
//no cache servers are set (localhost?)
$JSON = file_get_contents($api_url);
$THREAD = json_decode($JSON);
}
}
catch(Exeption $e){
/**
* An error parsing the thread (possibly HTTP/404)
*/
include("template/invalid_thread.tpl");
exit();
}
}
/**
* Thread has no posts
*/
if(!isset($THREAD->posts) || count($THREAD->posts)<1){
include("template/invalid_thread.tpl");
exit();
}
//require the main class
include("lib/dragonraid.php");
/**
* This is the main instance
* @var Raid
*/
$Raid = new DragonRaid($THREAD);
$Raid->cache_status = isset($_is_cached_request) ? $_is_cached_request : false;
//process the game rules
$Raid->play();
if(isset($_GET['id']) && strpos($_GET['id'],'/json')>0){
/**
* loads the fight HTML template
*/
$Raid->jsonAPI();
}elseif(isset($_GET['id']) && strpos($_GET['id'],'/status')>0){
/**
* loads the status iframe
*/
$Raid->displayStatus();
}elseif(isset($_GET['id']) && strpos($_GET['id'],'/ajax')>0){
/**
* loads the status iframe
*/
$Raid->displayStatusAjax();
}else{
/**
* loads the fight json feed
*/
$Raid->display();
}
//EOF
?>