-
Notifications
You must be signed in to change notification settings - Fork 115
/
ajax.php
executable file
·208 lines (168 loc) · 6.56 KB
/
ajax.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
Copyright (C) 2004-2010 Kestas J. Kuliukas
This file is part of webDiplomacy.
webDiplomacy is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
webDiplomacy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with webDiplomacy. If not, see <http://www.gnu.org/licenses/>.
*/
define('AJAX', true); // Makes header.php ignore some of the unneeded stuff, mainly loading $User
define('IN_CODE', 1);
require_once('config.php');
if( Config::isOnPlayNowDomain() ) define('PLAYNOW',true);
require_once('header.php');
/*
* This function logs a huge amount of javascript errors, sent from javascript/utility.js on error,
* but it seems to log trivial errors in lots of different languages, and isn't very useful except
* perhaps for development.
function logJavaScriptError() {
$errorVars=array('Location','Message','URL','Line');
$errorVals=array();
foreach($errorVars as $varName)
{
if( !isset($_REQUEST['error'.$varName]) ) return;
$errorVals[$varName] = $_REQUEST['error'.$varName];
}
if( isset($_SERVER['HTTP_USER_AGENT']) )
$errorVars['UserAgent'] = $_SERVER['HTTP_USER_AGENT'];
trigger_error('JavaScript error logged');
}
logJavaScriptError();
*/
$results = array('status'=>'Invalid', 'notice'=>'No valid action specified');
// footer JS needs to contain a cached
// Check for group link changes. Has to be a user at least, and specifying a group type:
if( isset($User) && $User->type['User'] && isset($_GET['groupType']) && isset($_GET['userID']) && isset($_GET['groupID']) )
{
// Someone is making a group link change.
if( isset($_GET['setActive']) )
{
}
}
if( isset($_GET['groupID']) && isset($_GET['userID']) && isset($User) && $User->type['User'] )
{
}
else if( isset($_GET['sendSMSToken']) ) {
if( libAuth::sendSMSToken_Valid($_GET['sendSMSToken']) ) {
$token = explode('_', $_GET['sendSMSToken']);
$number = $token[0];
$message = $token[1];
require_once('lib/sms.php');
libSMS::send($number, $message);
$results = "Success";
}
}
else if( isset($_GET['likeMessageToggleToken']) ) {
if( libAuth::likeToggleToken_Valid($_GET['likeMessageToggleToken']) ) {
$token = explode('_', $_GET['likeMessageToggleToken']);
$userID = (int) $token[0];
$likeMessageID = (int) $token[1];
$DB->sql_put("BEGIN");
list($likeExists) = $DB->sql_row("SELECT COUNT(*) FROM wD_LikePost WHERE userID = ".$userID." AND likeMessageID = ".$likeMessageID);
if( $likeExists == 0 )
{
$DB->sql_put("UPDATE wD_ForumMessages SET likeCount = likeCount + 1 WHERE id = ".$likeMessageID);
$DB->sql_put("INSERT INTO wD_LikePost ( userID, likeMessageID ) VALUES ( ".$userID.", ".$likeMessageID." )");
}
else
{
$DB->sql_put("UPDATE wD_ForumMessages SET likeCount = likeCount - 1 WHERE id = ".$likeMessageID);
$DB->sql_put("DELETE FROM wD_LikePost WHERE userID = ".$userID." AND likeMessageID = ".$likeMessageID);
}
$DB->sql_put("COMMIT");
}
}
elseif( isset($_REQUEST['context']) && isset($_REQUEST['contextKey']) && isset($_REQUEST['orderUpdates']) )
{
require_once(l_r('board/orders/orderinterface.php'));
try
{
$O = OrderInterface::newJSON($_REQUEST['contextKey'], $_REQUEST['context']);
$O->load();
$newReady=$oldReady=$O->orderStatus->Ready;
if( $O->orderStatus->Ready && isset($_REQUEST['notready']) )
$newReady=$O->readyToggle();
$O->set($_REQUEST['orderUpdates']);
$O->validate();
if( !$O->orderStatus->Ready && isset($_REQUEST['ready']) )
$newReady=$O->readyToggle();
$O->writeOrders();
$O->writeOrderStatus();
$DB->sql_put("COMMIT");
$results = $O->getResults();
if( $newReady != $oldReady) {
$Game = libVariant::$Variant->Game($O->gameID);//, UPDATE); // No need to lock game for update to check whether it needs a process
require_once('lib/pusher.php');
libPusher::trigger("private-game" . $Game->id, 'overview', 'processed');
}
if( $newReady && !$oldReady )
{
$results['process']='Checked';
$Game = libVariant::$Variant->Game($O->gameID);//, UPDATE); // No need to lock game for update to check whether it needs a process
if( $Game->needsProcess() )
{
$MC->append('processHint',','.$Game->id);
}
/*
Old instant-process code which was disabled as it was causing deadlocks
\
if( $Game->processStatus!='Crashed' && $Game->attempts > count($Game->Members->ByID)*2 )
{
$DB->sql_put("COMMIT");
require_once(l_r('gamemaster/game.php'));
$Game =libVariant::$Variant->processGame($Game->id);
$Game->crashed();
$DB->sql_put("COMMIT");
}
elseif( $Game->needsProcess() )
{
$DB->sql_put("UPDATE wD_Games SET attempts=attempts+1 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
$results['process']='Attempted';
require_once(l_r('gamemaster/game.php'));
$Game = libVariant::$Variant->processGame($O->gameID);
if( $Game->needsProcess() )
{
$Game->process();
$DB->sql_put("UPDATE wD_Games SET attempts=0 WHERE id=".$Game->id);
$DB->sql_put("COMMIT");
$results['process']='Success';
$results['notice']=l_t('Game processed, click <a href="%s">here</a> to refresh..','board.php?gameID='.$Game->id.'&nocache='.rand(0,1000));
}
}*/
}
}
catch(Exception $e)
{
if( $e->getMessage() == "Abandoned" || $e->getMessage() == "Cancelled" )
$DB->sql_put("COMMIT");
else
$DB->sql_put("ROLLBACK");
$results = array('invalid'=>true, 'statusIcon'=>'<img src="'.l_s('images/icons/alert.png').'" alt="'.l_t('Error').'" title="'.l_t('Error alert').'" />',
'statusText'=>'', 'notice'=>l_t('Exception: ').$e->getMessage(), 'orders'=>array());
}
}
/*
file_put_contents(
'bot_ajax_requestlog.txt',
date('l jS \of F Y h:i:s A')."\n".
"UserID: ".$User->id."\n".
"-------------------\n".
$_SERVER['REQUEST_URI']."\n".
"-------------------\n".
json_encode($results, JSON_PRETTY_PRINT)."\n".
"-------------------\n\n",
FILE_APPEND);
*/
header('Content-Type: application/json');
header('X-JSON: ('.json_encode($results).')');
print json_encode($results);
close();
?>