mambax7 /
chess
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | // ------------------------------------------------------------------------- // |
||
| 3 | // This program is free software; you can redistribute it and/or modify // |
||
| 4 | // it under the terms of the GNU General Public License as published by // |
||
| 5 | // the Free Software Foundation; either version 2 of the License, or // |
||
| 6 | // (at your option) any later version. // |
||
| 7 | // // |
||
| 8 | // You may not change or alter any portion of this comment or credits // |
||
| 9 | // of supporting developers from this source code or any supporting // |
||
| 10 | // source code which is considered copyrighted (c) material of the // |
||
| 11 | // original comment or credit authors. // |
||
| 12 | // // |
||
| 13 | // This program is distributed in the hope that it will be useful, // |
||
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
||
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
||
| 16 | // GNU General Public License for more details. // |
||
| 17 | // // |
||
| 18 | // You should have received a copy of the GNU General Public License // |
||
| 19 | // along with this program; if not, write to the Free Software // |
||
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
||
| 21 | // ------------------------------------------------------------------------ // |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Ratings functions. |
||
| 25 | * |
||
| 26 | * @package chess |
||
| 27 | * @subpackage ratings |
||
| 28 | */ |
||
| 29 | |||
| 30 | /**#@+ |
||
| 31 | */ |
||
| 32 | require_once XOOPS_ROOT_PATH . '/modules/chess/include/functions.php'; |
||
| 33 | /**#@-*/ |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Update the players' ratings for the specified game. |
||
| 37 | * |
||
| 38 | * @param int $gid Game ID |
||
| 39 | * @return bool True if ratings updated, otherwise false |
||
| 40 | */ |
||
| 41 | function chess_ratings_adj($gid) |
||
| 42 | { |
||
| 43 | global $xoopsDB; |
||
| 44 | |||
| 45 | $rating_system = chess_moduleConfig('rating_system'); |
||
| 46 | $init_rating = chess_moduleConfig('initial_rating'); |
||
| 47 | |||
| 48 | if ('none' == $rating_system) { |
||
| 49 | return false; |
||
| 50 | } |
||
| 51 | |||
| 52 | // determine function for calculating new ratings using configured rating system |
||
| 53 | $func = chess_ratings_get_func_adj($rating_system); |
||
| 54 | |||
| 55 | $games_table = $xoopsDB->prefix('chess_games'); |
||
| 56 | $ratings_table = $xoopsDB->prefix('chess_ratings'); |
||
| 57 | |||
| 58 | // get the game info |
||
| 59 | $result = $xoopsDB->query( |
||
| 60 | " |
||
| 61 | SELECT |
||
| 62 | g.white_uid AS white_uid, g.black_uid AS black_uid, g.pgn_result AS pgn_result, w.rating AS white_rating, b.rating AS black_rating, |
||
| 63 | (w.games_won+w.games_lost+w.games_drawn) AS white_games, (b.games_won+b.games_lost+b.games_drawn) AS black_games |
||
| 64 | FROM $games_table AS g |
||
| 65 | LEFT JOIN $ratings_table AS w ON w.player_uid = g.white_uid |
||
| 66 | LEFT JOIN $ratings_table AS b ON b.player_uid = g.black_uid |
||
| 67 | WHERE g.game_id = '$gid' AND g.is_rated = '1' AND g.pgn_result != '*' |
||
| 68 | AND (w.player_uid IS NULL OR b.player_uid IS NULL OR w.player_uid != b.player_uid) |
||
| 69 | " |
||
| 70 | ); |
||
| 71 | |||
| 72 | // check that game exists and is rated |
||
| 73 | if (1 != $xoopsDB->getRowsNum($result)) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | $row = $xoopsDB->fetchArray($result); |
||
| 78 | $xoopsDB->freeRecordSet($result); |
||
| 79 | |||
| 80 | #var_dump($row);#*#DEBUG# |
||
| 81 | // make sure the users are in the players' table |
||
| 82 | $value_list = []; |
||
| 83 | if (!isset($row['white_rating'])) { |
||
| 84 | $row['white_rating'] = $init_rating; |
||
| 85 | $row['white_games'] = 0; |
||
| 86 | $value_list[] = "('{$row['white_uid']}','{$row['white_rating']}')"; |
||
| 87 | } |
||
| 88 | if (!isset($row['black_rating'])) { |
||
| 89 | $row['black_rating'] = $init_rating; |
||
| 90 | $row['black_games'] = 0; |
||
| 91 | $value_list[] = "('{$row['black_uid']}','{$row['black_rating']}')"; |
||
| 92 | } |
||
| 93 | if (!empty($value_list)) { |
||
| 94 | $values = implode(',', $value_list); |
||
| 95 | $xoopsDB->query("INSERT INTO $ratings_table (player_uid, rating) VALUES $values"); |
||
| 96 | $xoopsDB->errno() and trigger_error($xoopsDB->errno() . ':' . $xoopsDB->error(), E_USER_ERROR); |
||
| 97 | } |
||
| 98 | |||
| 99 | // calculate new ratings using configured rating system |
||
| 100 | [$white_rating_new, $black_rating_new] = $func($row['white_rating'], $row['white_games'], $row['black_rating'], $row['black_games'], $row['pgn_result']); |
||
| 101 | |||
| 102 | // determine game-count columns to increment |
||
| 103 | [$white_col, $black_col] = chess_ratings_get_columns($row['pgn_result']); |
||
| 104 | |||
| 105 | $xoopsDB->query( |
||
| 106 | " |
||
| 107 | UPDATE $ratings_table |
||
| 108 | SET rating = '$white_rating_new', $white_col = $white_col + 1 |
||
| 109 | WHERE player_uid = '{$row['white_uid']}' |
||
| 110 | " |
||
| 111 | ); |
||
| 112 | $xoopsDB->errno() and trigger_error($xoopsDB->errno() . ':' . $xoopsDB->error(), E_USER_ERROR); |
||
| 113 | |||
| 114 | $xoopsDB->query( |
||
| 115 | " |
||
| 116 | UPDATE $ratings_table |
||
| 117 | SET rating = '$black_rating_new', $black_col = $black_col + 1 |
||
| 118 | WHERE player_uid = '{$row['black_uid']}' |
||
| 119 | " |
||
| 120 | ); |
||
| 121 | $xoopsDB->errno() and trigger_error($xoopsDB->errno() . ':' . $xoopsDB->error(), E_USER_ERROR); |
||
| 122 | |||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Recalculate all the players' ratings. |
||
| 128 | * |
||
| 129 | * @return bool True if ratings updated, otherwise false |
||
| 130 | */ |
||
| 131 | function chess_recalc_ratings() |
||
| 132 | { |
||
| 133 | global $xoopsDB; |
||
| 134 | |||
| 135 | $rating_system = chess_moduleConfig('rating_system'); |
||
| 136 | $init_rating = chess_moduleConfig('initial_rating'); |
||
| 137 | |||
| 138 | if ('none' == $rating_system) { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | // determine function for calculating new ratings using configured rating system |
||
| 143 | $func = chess_ratings_get_func_adj($rating_system); |
||
| 144 | |||
| 145 | $games_table = $xoopsDB->prefix('chess_games'); |
||
| 146 | $ratings_table = $xoopsDB->prefix('chess_ratings'); |
||
| 147 | |||
| 148 | // Nuke the current ratings. #*#TBD# - don't want to empty this table, since there will be other info in it besides ratings (?) |
||
| 149 | $xoopsDB->query("DELETE FROM $ratings_table"); |
||
| 150 | |||
| 151 | // get all games |
||
| 152 | $result = $xoopsDB->query( |
||
| 153 | " |
||
| 154 | SELECT white_uid, black_uid, pgn_result |
||
| 155 | FROM $games_table |
||
| 156 | WHERE is_rated = '1' AND pgn_result != '*' AND white_uid != black_uid |
||
| 157 | ORDER BY last_date ASC |
||
| 158 | " |
||
| 159 | ); |
||
| 160 | |||
| 161 | $players = []; |
||
| 162 | |||
| 163 | // process the games |
||
| 164 | while ($row = $xoopsDB->fetchArray($result)) { |
||
| 165 | #var_dump($row);#*#DEBUG# |
||
| 166 | if (!isset($players[$row['white_uid']])) { |
||
| 167 | $players[$row['white_uid']] = ['rating' => $init_rating, 'games_won' => 0, 'games_lost' => 0, 'games_drawn' => 0]; |
||
| 168 | } |
||
| 169 | if (!isset($players[$row['black_uid']])) { |
||
| 170 | $players[$row['black_uid']] = ['rating' => $init_rating, 'games_won' => 0, 'games_lost' => 0, 'games_drawn' => 0]; |
||
| 171 | } |
||
| 172 | |||
| 173 | $player_white = $players[$row['white_uid']]; |
||
| 174 | $player_black = $players[$row['black_uid']]; |
||
| 175 | |||
| 176 | // calculate new ratings using configured rating system |
||
| 177 | [$white_rating_new, $black_rating_new] = $func( |
||
| 178 | $player_white['rating'], |
||
| 179 | $player_white['games_won'] + $player_white['games_lost'] + $player_white['games_drawn'], |
||
| 180 | $player_black['rating'], |
||
| 181 | $player_black['games_won'] + $player_black['games_lost'] + $player_black['games_drawn'], |
||
| 182 | $row['pgn_result'] |
||
| 183 | ); |
||
| 184 | |||
| 185 | // determine game-count columns to increment |
||
| 186 | [$white_col, $black_col] = chess_ratings_get_columns($row['pgn_result']); |
||
| 187 | |||
| 188 | $player_white['rating'] = $white_rating_new; |
||
| 189 | ++$player_white[$white_col]; |
||
| 190 | |||
| 191 | $player_black['rating'] = $black_rating_new; |
||
| 192 | ++$player_black[$black_col]; |
||
| 193 | } |
||
| 194 | |||
| 195 | $xoopsDB->freeRecordSet($result); |
||
| 196 | |||
| 197 | if (!empty($players)) { |
||
| 198 | $value_list = []; |
||
| 199 | foreach ($players as $player_uid => $player) { |
||
| 200 | $value_list[] = "('$player_uid', '{$player['rating']}', '{$player['games_won']}', '{$player['games_lost']}', '{$player['games_drawn']}')"; |
||
| 201 | } |
||
| 202 | $values = implode(',', $value_list); |
||
| 203 | |||
| 204 | $xoopsDB->query("INSERT INTO $ratings_table (player_uid, rating, games_won, games_lost, games_drawn) VALUES $values"); |
||
| 205 | $xoopsDB->errno() and trigger_error($xoopsDB->errno() . ':' . $xoopsDB->error(), E_USER_ERROR); |
||
| 206 | } |
||
| 207 | |||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return the number of provisional games. |
||
| 213 | * |
||
| 214 | * @return int Number of provisional games for configured rating system, or '0' if no rating system. |
||
| 215 | */ |
||
| 216 | function chess_ratings_num_provisional_games() |
||
| 217 | { |
||
| 218 | $rating_system = chess_moduleConfig('rating_system'); |
||
| 219 | |||
| 220 | if ('none' == $rating_system) { |
||
| 221 | return 0; |
||
| 222 | } |
||
| 223 | |||
| 224 | // determine function for getting number of provisional games using configured rating system |
||
| 225 | $file = XOOPS_ROOT_PATH . "/modules/chess/include/ratings_{$rating_system}.inc.php"; |
||
| 226 | file_exists($file) or trigger_error("missing file '$file' for rating system '$rating_system'", E_USER_ERROR); |
||
| 227 | require_once $file; |
||
| 228 | $func = "chess_ratings_num_provisional_games_{$rating_system}"; |
||
| 229 | function_exists($func) or trigger_error("missing function '$func' for rating system '$rating_system'", E_USER_ERROR); |
||
| 230 | |||
| 231 | return $func(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Determine function for calculating new ratings using specified rating system. |
||
| 236 | * |
||
| 237 | * @param string $rating_system |
||
| 238 | * @return string Function name |
||
| 239 | */ |
||
| 240 | function chess_ratings_get_func_adj($rating_system) |
||
| 241 | { |
||
| 242 | $file = XOOPS_ROOT_PATH . "/modules/chess/include/ratings_{$rating_system}.inc.php"; |
||
| 243 | file_exists($file) or trigger_error("missing file '$file' for rating system '$rating_system'", E_USER_ERROR); |
||
| 244 | require_once $file; |
||
| 245 | $func = "chess_ratings_adj_{$rating_system}"; |
||
| 246 | function_exists($func) or trigger_error("missing function '$func' for rating system '$rating_system'", E_USER_ERROR); |
||
| 247 | return $func; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determine game-count columns in chess_ratings table to increment. |
||
| 252 | * |
||
| 253 | * This function was created to avoid having to repeat code that's used in two places. |
||
| 254 | * |
||
| 255 | * @param string $pgn_result Game result |
||
| 256 | * @return array Array with two elements: |
||
| 257 | * - $white_col: name of column in white's row to increment |
||
| 258 | * - $black_col: name of column in black's row to increment |
||
| 259 | */ |
||
| 260 | function chess_ratings_get_columns($pgn_result) |
||
| 261 | { |
||
| 262 | switch ($pgn_result) { |
||
| 263 | case '1-0': |
||
| 264 | $white_col = 'games_won'; |
||
| 265 | $black_col = 'games_lost'; |
||
| 266 | break; |
||
| 267 | case '1/2-1/2': |
||
| 268 | default: // should not occur |
||
| 269 | $white_col = 'games_drawn'; |
||
| 270 | $black_col = 'games_drawn'; |
||
| 271 | break; |
||
| 272 | case '0-1': |
||
| 273 | $white_col = 'games_lost'; |
||
| 274 | $black_col = 'games_won'; |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | |||
| 278 | return [$white_col, $black_col]; |
||
| 279 | } |
||
| 280 | |||
| 281 | ?> |
||
|
0 ignored issues
–
show
|
|||
| 282 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.