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 | /** |
||
| 4 | * Generate test data in MySQL database tables for chess module. |
||
| 5 | * |
||
| 6 | * This script is designed to be run from the command line, not from a web browser. |
||
| 7 | * |
||
| 8 | * @package chess |
||
| 9 | * @subpackage test |
||
| 10 | */ |
||
| 11 | error_reporting(E_ALL); |
||
| 12 | |||
| 13 | /**#@+ |
||
| 14 | */ |
||
| 15 | define('DBHOST', 'localhost'); |
||
| 16 | define('DBNAME', 'test'); |
||
| 17 | define('DBUSER', 'root'); |
||
| 18 | define('DBPASS', ''); |
||
| 19 | |||
| 20 | define('NUM_USERS', 3); |
||
| 21 | define('NUM_CHALLENGES', 1000); |
||
| 22 | define('NUM_GAMES', 10000); |
||
| 23 | define('NUM_RATINGS', NUM_USERS / 2); |
||
| 24 | /**#@-*/ |
||
| 25 | |||
| 26 | perform(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Generate the test data. |
||
| 30 | */ |
||
| 31 | function perform() |
||
| 32 | { |
||
| 33 | $challenges_table = 'chess_challenges'; |
||
| 34 | |||
| 35 | $games_table = 'chess_games'; |
||
| 36 | |||
| 37 | $ratings_table = 'chess_ratings'; |
||
| 38 | |||
| 39 | mysqli_connect(DBHOST, DBUSER, DBPASS) or trigger_error('[' . $GLOBALS['xoopsDB']->errno() . '] ' . $GLOBALS['xoopsDB']->error(), E_USER_ERROR); |
||
| 40 | |||
| 41 | mysqli_select_db($GLOBALS['xoopsDB']->conn, DBNAME) or trigger_error('[' . $GLOBALS['xoopsDB']->errno() . '] ' . $GLOBALS['xoopsDB']->error(), E_USER_ERROR); |
||
| 42 | |||
| 43 | // For safety, don't generate test data unless the tables are empty. |
||
| 44 | |||
| 45 | if (!table_empty($challenges_table) || !table_empty($games_table) || !table_empty($ratings_table)) { |
||
| 46 | echo "Tables already contain data - no action performed.\n"; |
||
| 47 | |||
| 48 | exit; |
||
|
0 ignored issues
–
show
|
|||
| 49 | } |
||
| 50 | |||
| 51 | // Generate the challenges table |
||
| 52 | |||
| 53 | $game_types = ['open', 'user']; |
||
| 54 | |||
| 55 | $color_options = ['player2', 'random', 'white', 'black']; |
||
| 56 | |||
| 57 | for ($i = 0; $i < NUM_CHALLENGES; ++$i) { |
||
| 58 | $game_type = rand_array_value($game_types); |
||
| 59 | |||
| 60 | $fen_index = random_int(1, 10); |
||
| 61 | |||
| 62 | $fen = 10 == $fen_index ? 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' : ''; |
||
| 63 | |||
| 64 | $color_option = rand_array_value($color_options); |
||
| 65 | |||
| 66 | $notify_move_player1 = random_int(0, 1); |
||
| 67 | |||
| 68 | $player1_uid = random_int(1, NUM_USERS); |
||
| 69 | |||
| 70 | if ('open' == $game_type) { |
||
| 71 | $player2_uid = 0; |
||
| 72 | } else { |
||
| 73 | // select $player2_uid != $player1_uid |
||
| 74 | |||
| 75 | do { |
||
| 76 | $player2_uid = random_int(1, NUM_USERS); |
||
| 77 | } while ($player2_uid == $player1_uid); |
||
| 78 | } |
||
| 79 | |||
| 80 | $create_date_max = time(); |
||
| 81 | |||
| 82 | $create_date_min = $create_date_max - 30 * 24 * 3600; |
||
| 83 | |||
| 84 | $create_date = date('Y-m-d H:i:s', random_int($create_date_min, $create_date_max)); |
||
| 85 | |||
| 86 | $is_rated = random_int(0, 1); |
||
| 87 | |||
| 88 | do_query( |
||
| 89 | " |
||
| 90 | INSERT INTO $challenges_table |
||
| 91 | SET |
||
| 92 | game_type = '$game_type', |
||
| 93 | fen = '$fen', |
||
| 94 | color_option = '$color_option', |
||
| 95 | notify_move_player1 = '$notify_move_player1', |
||
| 96 | player1_uid = '$player1_uid', |
||
| 97 | player2_uid = '$player2_uid', |
||
| 98 | create_date = '$create_date', |
||
| 99 | is_rated = '$is_rated' |
||
| 100 | " |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Generate the games table |
||
| 105 | |||
| 106 | $pgn_results = ['*', '0-1', '1-0', '1/2-1/2']; |
||
| 107 | |||
| 108 | $suspended_explains = ['foo', 'bar', 'baz', 'quux']; |
||
| 109 | |||
| 110 | for ($i = 0; $i < NUM_GAMES; ++$i) { |
||
| 111 | $white_uid = random_int(1, NUM_USERS); |
||
| 112 | |||
| 113 | $black_uid = random_int(1, NUM_USERS); |
||
| 114 | |||
| 115 | // Force some games to be self-play. |
||
| 116 | |||
| 117 | if (10 == random_int(1, 10)) { |
||
| 118 | $black_uid = $white_uid; |
||
| 119 | } |
||
| 120 | |||
| 121 | $create_date_max = time(); |
||
| 122 | |||
| 123 | $create_date_min = $create_date_max - 365 * 24 * 3600; |
||
| 124 | |||
| 125 | $create_date_sec = random_int($create_date_min, $create_date_max); |
||
| 126 | |||
| 127 | $create_date = date('Y-m-d H:i:s', $create_date_sec); |
||
| 128 | |||
| 129 | $is_started = random_int(1, 4) < 4; |
||
| 130 | |||
| 131 | $start_date_sec = $is_started ? $create_date_sec + random_int(3600, 3 * 24 * 3600) : 0; |
||
| 132 | |||
| 133 | $start_date = $is_started ? date('Y-m-d H:i:s', $start_date_sec) : '0000-00-00 00:00:00'; |
||
| 134 | |||
| 135 | $multiple_moves = $is_started && random_int(1, 10) < 10; |
||
| 136 | |||
| 137 | $last_date_sec = $multiple_moves ? $start_date_sec + random_int(3600, 90 * 24 * 3600) : 0; |
||
| 138 | |||
| 139 | $last_date = $multiple_moves ? date('Y-m-d H:i:s', $last_date_sec) : '0000-00-00 00:00:00'; |
||
| 140 | |||
| 141 | $pgn_result = $multiple_moves ? rand_array_value($pgn_results) : '*'; |
||
| 142 | |||
| 143 | if ($multiple_moves && '*' == $pgn_result && 5 == random_int(1, 5)) { |
||
| 144 | $suspended_date = date('Y-m-d H:i:s', $last_date_sec + random_int(60, 72 * 3600)); |
||
| 145 | |||
| 146 | $suspended_uids = [1, $white_uid, $black_uid]; |
||
| 147 | |||
| 148 | $suspended_uid = rand_array_value($suspended_uids); |
||
| 149 | |||
| 150 | $suspended_type = 1 == $suspended_uid ? 'arbiter_suspend' : 'want_arbitration'; |
||
| 151 | |||
| 152 | $suspended_explain = rand_array_value($suspended_explains); |
||
| 153 | |||
| 154 | $suspended = "$suspended_date|$suspended_uid|$suspended_type|$suspended_explain"; |
||
| 155 | } else { |
||
| 156 | $suspended = ''; |
||
| 157 | } |
||
| 158 | |||
| 159 | $is_rated = $white_uid != $black_uid ? random_int(0, 1) : 0; |
||
| 160 | |||
| 161 | do_query( |
||
| 162 | " |
||
| 163 | INSERT INTO $games_table |
||
| 164 | SET |
||
| 165 | white_uid = '$white_uid', |
||
| 166 | black_uid = '$black_uid', |
||
| 167 | create_date = '$create_date', |
||
| 168 | start_date = '$start_date', |
||
| 169 | last_date = '$last_date', |
||
| 170 | pgn_result = '$pgn_result', |
||
| 171 | suspended = '$suspended', |
||
| 172 | is_rated = '$is_rated' |
||
| 173 | " |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | $GLOBALS['xoopsDB']->close(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check whether table is empty. |
||
| 182 | * |
||
| 183 | * @param string $table Table name |
||
| 184 | * @return bool True if table is empty |
||
| 185 | */ |
||
| 186 | function table_empty($table) |
||
| 187 | { |
||
| 188 | $result = do_query("SELECT COUNT(*) FROM $table"); |
||
| 189 | |||
| 190 | [$num_rows] = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 191 | |||
| 192 | $GLOBALS['xoopsDB']->freeRecordSet($result); |
||
| 193 | |||
| 194 | return 0 == $num_rows; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Perform MySQL query. |
||
| 199 | * |
||
| 200 | * If the result from $GLOBALS['xoopsDB']->queryF() is false, trigger_error() is called to display the error. |
||
| 201 | * |
||
| 202 | * @param string $query The query to perform |
||
| 203 | * @return bool Return from $GLOBALS['xoopsDB']->queryF() |
||
| 204 | */ |
||
| 205 | function do_query($query) |
||
| 206 | { |
||
| 207 | $result = $GLOBALS['xoopsDB']->queryF($query); |
||
| 208 | |||
| 209 | if (false === $result) { |
||
| 210 | $errno = $GLOBALS['xoopsDB']->errno(); |
||
| 211 | |||
| 212 | $error = $GLOBALS['xoopsDB']->error(); |
||
| 213 | |||
| 214 | trigger_error("[$errno] $error\n$query", E_USER_ERROR); |
||
| 215 | } |
||
| 216 | |||
| 217 | return $result; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $array |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | function rand_array_value($array) |
||
| 225 | { |
||
| 226 | return $array[array_rand($array)]; |
||
| 227 | } |
||
| 228 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.