forked from kestasjk/webDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ajax.php
executable file
·150 lines (117 loc) · 4.78 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
<?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
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');
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 )
{
$results['process']='Checked';
$Game = libVariant::$Variant->Game($O->gameID);
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());
}
}
header('X-JSON: ('.json_encode($results).')');
close();
?>