Browse Source

Upload files to 'classes'

Stortebeker 6 years ago
parent
commit
d82bfe06c0

+ 319
- 294
classes/mysql.class.php View File

@@ -1,4 +1,4 @@
1
-<?
1
+<?php
2 2
 //-----------------------------------------------------------------------------------
3 3
 /////////////////////////////////////////////////////////////////////////////////////
4 4
 /*//-- MySQL wrapper class ----------------------------------------------------------
@@ -116,333 +116,358 @@ set_query_id($ResultSet)
116 116
 *///---------------------------------------------------------------------------------
117 117
 
118 118
 if (!extension_loaded('mysqli')) {
119
-  die('Mysqli Extension not loaded.');
119
+    die('Mysqli Extension not loaded.');
120 120
 }
121 121
 
122 122
 //Handles escaping
123
-function db_string($String, $DisableWildcards = false) {
124
-  global $DB;
125
-  //Escape
126
-  $String = $DB->escape_str($String);
127
-  //Remove user input wildcards
128
-  if ($DisableWildcards) {
129
-    $String = str_replace(array('%','_'), array('\%','\_'), $String);
130
-  }
131
-  return $String;
123
+function db_string($String, $DisableWildcards = false)
124
+{
125
+    global $DB;
126
+    //Escape
127
+    $String = $DB->escape_str($String);
128
+    //Remove user input wildcards
129
+    if ($DisableWildcards) {
130
+        $String = str_replace(array('%','_'), array('\%','\_'), $String);
131
+    }
132
+    return $String;
132 133
 }
133 134
 
134
-function db_array($Array, $DontEscape = [], $Quote = false) {
135
-  foreach ($Array as $Key => $Val) {
136
-    if (!in_array($Key, $DontEscape)) {
137
-      if ($Quote) {
138
-        $Array[$Key] = '\''.db_string(trim($Val)).'\'';
139
-      } else {
140
-        $Array[$Key] = db_string(trim($Val));
141
-      }
135
+function db_array($Array, $DontEscape = [], $Quote = false)
136
+{
137
+    foreach ($Array as $Key => $Val) {
138
+        if (!in_array($Key, $DontEscape)) {
139
+            if ($Quote) {
140
+                $Array[$Key] = '\''.db_string(trim($Val)).'\'';
141
+            } else {
142
+                $Array[$Key] = db_string(trim($Val));
143
+            }
144
+        }
142 145
     }
143
-  }
144
-  return $Array;
146
+    return $Array;
145 147
 }
146 148
 
147 149
 //TODO: revisit access levels once Drone is replaced by ZeRobot
148
-class DB_MYSQL {
149
-  public $LinkID = false;
150
-  protected $QueryID = false;
151
-  protected $StatementID = false;
152
-  protected $PreparedQuery = false;
153
-  protected $Record = [];
154
-  protected $Row;
155
-  protected $Errno = 0;
156
-  protected $Error = '';
157
-
158
-  public $Queries = [];
159
-  public $Time = 0.0;
160
-
161
-  protected $Database = '';
162
-  protected $Server = '';
163
-  protected $User = '';
164
-  protected $Pass = '';
165
-  protected $Port = 0;
166
-  protected $Socket = '';
167
-
168
-  function __construct($Database = SQLDB, $User = SQLLOGIN, $Pass = SQLPASS, $Server = SQLHOST, $Port = SQLPORT, $Socket = SQLSOCK) {
169
-    $this->Database = $Database;
170
-    $this->Server = $Server;
171
-    $this->User = $User;
172
-    $this->Pass = $Pass;
173
-    $this->Port = $Port;
174
-    $this->Socket = $Socket;
175
-  }
176
-
177
-  function halt($Msg) {
178
-    global $Debug, $argv;
179
-    $DBError = 'MySQL: '.strval($Msg).' SQL error: '.strval($this->Errno).' ('.strval($this->Error).')';
180
-    if ($this->Errno == 1194) {
181
-      send_irc('PRIVMSG '.ADMIN_CHAN.' :'.$this->Error);
150
+class DB_MYSQL
151
+{
152
+    public $LinkID = false;
153
+    protected $QueryID = false;
154
+    protected $StatementID = false;
155
+    protected $PreparedQuery = false;
156
+    protected $Record = [];
157
+    protected $Row;
158
+    protected $Errno = 0;
159
+    protected $Error = '';
160
+
161
+    public $Queries = [];
162
+    public $Time = 0.0;
163
+
164
+    protected $Database = '';
165
+    protected $Server = '';
166
+    protected $User = '';
167
+    protected $Pass = '';
168
+    protected $Port = 0;
169
+    protected $Socket = '';
170
+
171
+    public function __construct($Database = SQLDB, $User = SQLLOGIN, $Pass = SQLPASS, $Server = SQLHOST, $Port = SQLPORT, $Socket = SQLSOCK)
172
+    {
173
+        $this->Database = $Database;
174
+        $this->Server = $Server;
175
+        $this->User = $User;
176
+        $this->Pass = $Pass;
177
+        $this->Port = $Port;
178
+        $this->Socket = $Socket;
182 179
     }
183
-    $Debug->analysis('!dev DB Error', $DBError, 3600 * 24);
184
-    if (DEBUG_MODE || check_perms('site_debug') || isset($argv[1])) {
185
-      echo '<pre>'.display_str($DBError).'</pre>';
186
-      if (DEBUG_MODE || check_perms('site_debug')) {
187
-        print_r($this->Queries);
188
-      }
189
-      die();
190
-    } else {
191
-      error('-1');
180
+
181
+    public function halt($Msg)
182
+    {
183
+        global $Debug, $argv;
184
+        $DBError = 'MySQL: '.strval($Msg).' SQL error: '.strval($this->Errno).' ('.strval($this->Error).')';
185
+        if ($this->Errno == 1194) {
186
+            send_irc('PRIVMSG '.ADMIN_CHAN.' :'.$this->Error);
187
+        }
188
+        $Debug->analysis('!dev DB Error', $DBError, 3600 * 24);
189
+        if (DEBUG_MODE || check_perms('site_debug') || isset($argv[1])) {
190
+            echo '<pre>'.display_str($DBError).'</pre>';
191
+            if (DEBUG_MODE || check_perms('site_debug')) {
192
+                print_r($this->Queries);
193
+            }
194
+            die();
195
+        } else {
196
+            error('-1');
197
+        }
192 198
     }
193
-  }
194
-
195
-  function connect() {
196
-    if (!$this->LinkID) {
197
-      $this->LinkID = mysqli_connect($this->Server, $this->User, $this->Pass, $this->Database, $this->Port, $this->Socket); // defined in config.php
198
-      if (!$this->LinkID) {
199
-        $this->Errno = mysqli_connect_errno();
200
-        $this->Error = mysqli_connect_error();
201
-        $this->halt('Connection failed (host:'.$this->Server.':'.$this->Port.')');
202
-      }
199
+
200
+    public function connect()
201
+    {
202
+        if (!$this->LinkID) {
203
+            $this->LinkID = mysqli_connect($this->Server, $this->User, $this->Pass, $this->Database, $this->Port, $this->Socket); // defined in config.php
204
+            if (!$this->LinkID) {
205
+                $this->Errno = mysqli_connect_errno();
206
+                $this->Error = mysqli_connect_error();
207
+                $this->halt('Connection failed (host:'.$this->Server.':'.$this->Port.')');
208
+            }
209
+        }
210
+        mysqli_set_charset($this->LinkID, "utf8mb4");
203 211
     }
204
-    mysqli_set_charset($this->LinkID, "utf8mb4");
205
-  }
206
-
207
-  function prepare_query($Query, &...$BindVars) {
208
-    $this->connect();
209
-
210
-    $this->StatementID = mysqli_prepare($this->LinkID, $Query);
211
-    if (!empty($BindVars)) {
212
-      $Types = '';
213
-      $TypeMap = ['string'=>'s', 'double'=>'d', 'integer'=>'i', 'boolean'=>'i'];
214
-      foreach ($BindVars as $BindVar) {
215
-        $Types .= $TypeMap[gettype($BindVar)] ?? 'b';
216
-      }
217
-      mysqli_stmt_bind_param($this->StatementID, $Types, ...$BindVars);
212
+
213
+    public function prepare_query($Query, &...$BindVars)
214
+    {
215
+        $this->connect();
216
+
217
+        $this->StatementID = mysqli_prepare($this->LinkID, $Query);
218
+        if (!empty($BindVars)) {
219
+            $Types = '';
220
+            $TypeMap = ['string'=>'s', 'double'=>'d', 'integer'=>'i', 'boolean'=>'i'];
221
+            foreach ($BindVars as $BindVar) {
222
+                $Types .= $TypeMap[gettype($BindVar)] ?? 'b';
223
+            }
224
+            mysqli_stmt_bind_param($this->StatementID, $Types, ...$BindVars);
225
+        }
226
+        $this->PreparedQuery = $Query;
227
+        return $this->StatementID;
218 228
     }
219
-    $this->PreparedQuery = $Query;
220
-    return $this->StatementID;
221
-  }
222
-
223
-  function exec_prepared_query() {
224
-    $QueryStartTime = microtime(true);
225
-    mysqli_stmt_execute($this->StatementID);
226
-    $this->QueryID = mysqli_stmt_get_result($this->StatementID);
227
-    $QueryRunTime = (microtime(true) - $QueryStartTime) * 1000;
228
-    $this->Queries[] = [$this->PreppedQuery, $QueryRunTime, null];
229
-    $this->Time += $QueryRunTime;
230
-  }
231
-
232
-  function query($Query, &...$BindVars) {
233
-    global $Debug;
234
-    /*
235
-     * If there was a previous query, we store the warnings. We cannot do
236
-     * this immediately after mysqli_query because mysqli_insert_id will
237
-     * break otherwise due to mysqli_get_warnings sending a SHOW WARNINGS;
238
-     * query. When sending a query, however, we're sure that we won't call
239
-     * mysqli_insert_id (or any similar function, for that matter) later on,
240
-     * so we can safely get the warnings without breaking things.
241
-     * Note that this means that we have to call $this->warnings manually
242
-     * for the last query!
243
-     */
244
-    if ($this->QueryID) {
245
-      $this->warnings();
229
+
230
+    public function exec_prepared_query()
231
+    {
232
+        $QueryStartTime = microtime(true);
233
+        mysqli_stmt_execute($this->StatementID);
234
+        $this->QueryID = mysqli_stmt_get_result($this->StatementID);
235
+        $QueryRunTime = (microtime(true) - $QueryStartTime) * 1000;
236
+        $this->Queries[] = [$this->PreppedQuery, $QueryRunTime, null];
237
+        $this->Time += $QueryRunTime;
246 238
     }
247
-    $QueryStartTime = microtime(true);
248
-    $this->connect();
249
-
250
-    // In the event of a MySQL deadlock, we sleep allowing MySQL time to unlock, then attempt again for a maximum of 5 tries
251
-    for ($i = 1; $i < 6; $i++) {
252
-      $this->StatementID = mysqli_prepare($this->LinkID, $Query);
253
-      if (!empty($BindVars)) {
254
-        $Types = '';
255
-        $TypeMap = ['string'=>'s', 'double'=>'d', 'integer'=>'i', 'boolean'=>'i'];
256
-        foreach ($BindVars as $BindVar) {
257
-          $Types .= $TypeMap[gettype($BindVar)] ?? 'b';
239
+
240
+    public function query($Query, &...$BindVars)
241
+    {
242
+        global $Debug;
243
+        /*
244
+         * If there was a previous query, we store the warnings. We cannot do
245
+         * this immediately after mysqli_query because mysqli_insert_id will
246
+         * break otherwise due to mysqli_get_warnings sending a SHOW WARNINGS;
247
+         * query. When sending a query, however, we're sure that we won't call
248
+         * mysqli_insert_id (or any similar function, for that matter) later on,
249
+         * so we can safely get the warnings without breaking things.
250
+         * Note that this means that we have to call $this->warnings manually
251
+         * for the last query!
252
+         */
253
+        if ($this->QueryID) {
254
+            $this->warnings();
258 255
         }
259
-        mysqli_stmt_bind_param($this->StatementID, $Types, ...$BindVars);
260
-      }
261
-      mysqli_stmt_execute($this->StatementID);
262
-      $this->QueryID = mysqli_stmt_get_result($this->StatementID);
263
-
264
-      if (DEBUG_MODE) {
265
-        // in DEBUG_MODE, return the full trace on a SQL error (super useful
266
-        // for debugging). do not attempt to retry to query
267
-        if (!$this->QueryID) {
268
-          echo '<pre>' . mysqli_error($this->LinkID) . '<br><br>';
269
-          debug_print_backtrace();
270
-          echo '</pre>';
271
-          die();
256
+        $QueryStartTime = microtime(true);
257
+        $this->connect();
258
+
259
+        // In the event of a MySQL deadlock, we sleep allowing MySQL time to unlock, then attempt again for a maximum of 5 tries
260
+        for ($i = 1; $i < 6; $i++) {
261
+            $this->StatementID = mysqli_prepare($this->LinkID, $Query);
262
+            if (!empty($BindVars)) {
263
+                $Types = '';
264
+                $TypeMap = ['string'=>'s', 'double'=>'d', 'integer'=>'i', 'boolean'=>'i'];
265
+                foreach ($BindVars as $BindVar) {
266
+                    $Types .= $TypeMap[gettype($BindVar)] ?? 'b';
267
+                }
268
+                mysqli_stmt_bind_param($this->StatementID, $Types, ...$BindVars);
269
+            }
270
+            mysqli_stmt_execute($this->StatementID);
271
+            $this->QueryID = mysqli_stmt_get_result($this->StatementID);
272
+
273
+            if (DEBUG_MODE) {
274
+                // in DEBUG_MODE, return the full trace on a SQL error (super useful
275
+                // for debugging). do not attempt to retry to query
276
+                if (!$this->QueryID) {
277
+                    echo '<pre>' . mysqli_error($this->LinkID) . '<br><br>';
278
+                    debug_print_backtrace();
279
+                    echo '</pre>';
280
+                    die();
281
+                }
282
+            }
283
+
284
+            if (!in_array(mysqli_errno($this->LinkID), array(1213, 1205))) {
285
+                break;
286
+            }
287
+            $Debug->analysis('Non-Fatal Deadlock:', $Query, 3600 * 24);
288
+            trigger_error("Database deadlock, attempt $i");
289
+
290
+            sleep($i * rand(2, 5)); // Wait longer as attempts increase
272 291
         }
273
-      }
274 292
 
275
-      if (!in_array(mysqli_errno($this->LinkID), array(1213, 1205))) {
276
-        break;
277
-      }
278
-      $Debug->analysis('Non-Fatal Deadlock:', $Query, 3600 * 24);
279
-      trigger_error("Database deadlock, attempt $i");
293
+        $QueryEndTime = microtime(true);
294
+        $this->Queries[] = array($Query, ($QueryEndTime - $QueryStartTime) * 1000, null);
295
+        $this->Time += ($QueryEndTime - $QueryStartTime) * 1000;
296
+
297
+        if (!$this->QueryID && !$this->StatementID) {
298
+            $this->Errno = mysqli_errno($this->LinkID);
299
+            $this->Error = mysqli_error($this->LinkID);
300
+            $this->halt("Invalid Query: $Query");
301
+        }
280 302
 
281
-      sleep($i * rand(2, 5)); // Wait longer as attempts increase
303
+        $this->Row = 0;
304
+        return $this->QueryID;
282 305
     }
283 306
 
284
-    $QueryEndTime = microtime(true);
285
-    $this->Queries[] = array($Query, ($QueryEndTime - $QueryStartTime) * 1000, null);
286
-    $this->Time += ($QueryEndTime - $QueryStartTime) * 1000;
307
+    public function query_unb($Query)
308
+    {
309
+        $this->connect();
310
+        mysqli_real_query($this->LinkID, $Query);
311
+    }
287 312
 
288
-    if (!$this->QueryID && !$this->StatementID) {
289
-      $this->Errno = mysqli_errno($this->LinkID);
290
-      $this->Error = mysqli_error($this->LinkID);
291
-      $this->halt("Invalid Query: $Query");
313
+    public function inserted_id()
314
+    {
315
+        if ($this->LinkID) {
316
+            return mysqli_insert_id($this->LinkID);
317
+        }
292 318
     }
293 319
 
294
-    $this->Row = 0;
295
-    return $this->QueryID;
296
-  }
320
+    public function next_record($Type = MYSQLI_BOTH, $Escape = true)
321
+    {
322
+        // $Escape can be true, false, or an array of keys to not escape
323
+        if ($this->LinkID) {
324
+            $this->Record = mysqli_fetch_array($this->QueryID, $Type);
325
+            $this->Row++;
326
+            if (!is_array($this->Record)) {
327
+                $this->QueryID = false;
328
+            } elseif ($Escape !== false) {
329
+                $this->Record = Misc::display_array($this->Record, $Escape);
330
+            }
331
+            return $this->Record;
332
+        }
333
+    }
297 334
 
298
-  function query_unb($Query) {
299
-    $this->connect();
300
-    mysqli_real_query($this->LinkID, $Query);
301
-  }
335
+    public function close()
336
+    {
337
+        if ($this->LinkID) {
338
+            if (!mysqli_close($this->LinkID)) {
339
+                $this->halt('Cannot close connection or connection did not open.');
340
+            }
341
+            $this->LinkID = false;
342
+        }
343
+    }
302 344
 
303
-  function inserted_id() {
304
-    if ($this->LinkID) {
305
-      return mysqli_insert_id($this->LinkID);
345
+    /*
346
+     * returns an integer with the number of rows found
347
+     * returns a string if the number of rows found exceeds MAXINT
348
+     */
349
+    public function record_count()
350
+    {
351
+        if ($this->QueryID) {
352
+            return mysqli_num_rows($this->QueryID);
353
+        }
306 354
     }
307
-  }
308
-
309
-  function next_record($Type = MYSQLI_BOTH, $Escape = true) { // $Escape can be true, false, or an array of keys to not escape
310
-    if ($this->LinkID) {
311
-      $this->Record = mysqli_fetch_array($this->QueryID, $Type);
312
-      $this->Row++;
313
-      if (!is_array($this->Record)) {
314
-        $this->QueryID = false;
315
-      } elseif ($Escape !== false) {
316
-        $this->Record = Misc::display_array($this->Record, $Escape);
317
-      }
318
-      return $this->Record;
355
+
356
+    /*
357
+     * returns true if the query exists and there were records found
358
+     * returns false if the query does not exist or if there were 0 records returned
359
+     */
360
+    public function has_results()
361
+    {
362
+        return ($this->QueryID && $this->record_count() !== 0);
319 363
     }
320
-  }
321
-
322
-  function close() {
323
-    if ($this->LinkID) {
324
-      if (!mysqli_close($this->LinkID)) {
325
-        $this->halt('Cannot close connection or connection did not open.');
326
-      }
327
-      $this->LinkID = false;
364
+
365
+    public function affected_rows()
366
+    {
367
+        if ($this->LinkID) {
368
+            return mysqli_affected_rows($this->LinkID);
369
+        }
370
+    }
371
+
372
+    public function info()
373
+    {
374
+        return mysqli_get_host_info($this->LinkID);
375
+    }
376
+
377
+    // You should use db_string() instead.
378
+    public function escape_str($Str)
379
+    {
380
+        $this->connect(0);
381
+        if (is_array($Str)) {
382
+            trigger_error('Attempted to escape array.');
383
+            return '';
384
+        }
385
+        return mysqli_real_escape_string($this->LinkID, $Str);
328 386
     }
329
-  }
330
-
331
-  /*
332
-   * returns an integer with the number of rows found
333
-   * returns a string if the number of rows found exceeds MAXINT
334
-   */
335
-  function record_count() {
336
-    if ($this->QueryID) {
337
-      return mysqli_num_rows($this->QueryID);
387
+
388
+    // Creates an array from a result set
389
+    // If $Key is set, use the $Key column in the result set as the array key
390
+    // Otherwise, use an integer
391
+    public function to_array($Key = false, $Type = MYSQLI_BOTH, $Escape = true)
392
+    {
393
+        $Return = [];
394
+        while ($Row = mysqli_fetch_array($this->QueryID, $Type)) {
395
+            if ($Escape !== false) {
396
+                $Row = Misc::display_array($Row, $Escape);
397
+            }
398
+            if ($Key !== false) {
399
+                $Return[$Row[$Key]] = $Row;
400
+            } else {
401
+                $Return[] = $Row;
402
+            }
403
+        }
404
+        mysqli_data_seek($this->QueryID, 0);
405
+        return $Return;
338 406
     }
339
-  }
340
-
341
-  /*
342
-   * returns true if the query exists and there were records found
343
-   * returns false if the query does not exist or if there were 0 records returned
344
-   */
345
-  function has_results() {
346
-    return ($this->QueryID && $this->record_count() !== 0);
347
-  }
348
-
349
-  function affected_rows() {
350
-    if ($this->LinkID) {
351
-      return mysqli_affected_rows($this->LinkID);
407
+
408
+    //  Loops through the result set, collecting the $ValField column into an array with $KeyField as keys
409
+    public function to_pair($KeyField, $ValField, $Escape = true)
410
+    {
411
+        $Return = [];
412
+        while ($Row = mysqli_fetch_array($this->QueryID)) {
413
+            if ($Escape) {
414
+                $Key = display_str($Row[$KeyField]);
415
+                $Val = display_str($Row[$ValField]);
416
+            } else {
417
+                $Key = $Row[$KeyField];
418
+                $Val = $Row[$ValField];
419
+            }
420
+            $Return[$Key] = $Val;
421
+        }
422
+        mysqli_data_seek($this->QueryID, 0);
423
+        return $Return;
352 424
     }
353
-  }
354
-
355
-  function info() {
356
-    return mysqli_get_host_info($this->LinkID);
357
-  }
358
-
359
-  // You should use db_string() instead.
360
-  function escape_str($Str) {
361
-    $this->connect(0);
362
-    if (is_array($Str)) {
363
-      trigger_error('Attempted to escape array.');
364
-      return '';
425
+
426
+    //  Loops through the result set, collecting the $Key column into an array
427
+    public function collect($Key, $Escape = true)
428
+    {
429
+        $Return = [];
430
+        while ($Row = mysqli_fetch_array($this->QueryID)) {
431
+            $Return[] = $Escape ? display_str($Row[$Key]) : $Row[$Key];
432
+        }
433
+        mysqli_data_seek($this->QueryID, 0);
434
+        return $Return;
365 435
     }
366
-    return mysqli_real_escape_string($this->LinkID, $Str);
367
-  }
368
-
369
-  // Creates an array from a result set
370
-  // If $Key is set, use the $Key column in the result set as the array key
371
-  // Otherwise, use an integer
372
-  function to_array($Key = false, $Type = MYSQLI_BOTH, $Escape = true) {
373
-    $Return = [];
374
-    while ($Row = mysqli_fetch_array($this->QueryID, $Type)) {
375
-      if ($Escape !== false) {
376
-        $Row = Misc::display_array($Row, $Escape);
377
-      }
378
-      if ($Key !== false) {
379
-        $Return[$Row[$Key]] = $Row;
380
-      } else {
381
-        $Return[] = $Row;
382
-      }
436
+
437
+    public function set_query_id(&$ResultSet)
438
+    {
439
+        $this->QueryID = $ResultSet;
440
+        $this->Row = 0;
383 441
     }
384
-    mysqli_data_seek($this->QueryID, 0);
385
-    return $Return;
386
-  }
387
-
388
-  //  Loops through the result set, collecting the $ValField column into an array with $KeyField as keys
389
-  function to_pair($KeyField, $ValField, $Escape = true) {
390
-    $Return = [];
391
-    while ($Row = mysqli_fetch_array($this->QueryID)) {
392
-      if ($Escape) {
393
-        $Key = display_str($Row[$KeyField]);
394
-        $Val = display_str($Row[$ValField]);
395
-      } else {
396
-        $Key = $Row[$KeyField];
397
-        $Val = $Row[$ValField];
398
-      }
399
-      $Return[$Key] = $Val;
442
+
443
+    public function get_query_id()
444
+    {
445
+        return $this->QueryID;
400 446
     }
401
-    mysqli_data_seek($this->QueryID, 0);
402
-    return $Return;
403
-  }
404
-
405
-  //  Loops through the result set, collecting the $Key column into an array
406
-  function collect($Key, $Escape = true) {
407
-    $Return = [];
408
-    while ($Row = mysqli_fetch_array($this->QueryID)) {
409
-      $Return[] = $Escape ? display_str($Row[$Key]) : $Row[$Key];
447
+
448
+    public function beginning()
449
+    {
450
+        mysqli_data_seek($this->QueryID, 0);
451
+        $this->Row = 0;
410 452
     }
411
-    mysqli_data_seek($this->QueryID, 0);
412
-    return $Return;
413
-  }
414
-
415
-  function set_query_id(&$ResultSet) {
416
-    $this->QueryID = $ResultSet;
417
-    $this->Row = 0;
418
-  }
419
-
420
-  function get_query_id() {
421
-    return $this->QueryID;
422
-  }
423
-
424
-  function beginning() {
425
-    mysqli_data_seek($this->QueryID, 0);
426
-    $this->Row = 0;
427
-  }
428
-
429
-  /**
430
-   * This function determines whether the last query caused warning messages
431
-   * and stores them in $this->Queries.
432
-   */
433
-  function warnings() {
434
-    $Warnings = [];
435
-    if (!is_bool($this->LinkID) && mysqli_warning_count($this->LinkID)) {
436
-      $e = mysqli_get_warnings($this->LinkID);
437
-      do {
438
-        if ($e->errno == 1592) {
439
-          // 1592: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.
440
-          continue;
453
+
454
+    /**
455
+     * This function determines whether the last query caused warning messages
456
+     * and stores them in $this->Queries.
457
+     */
458
+    public function warnings()
459
+    {
460
+        $Warnings = [];
461
+        if (!is_bool($this->LinkID) && mysqli_warning_count($this->LinkID)) {
462
+            $e = mysqli_get_warnings($this->LinkID);
463
+            do {
464
+                if ($e->errno == 1592) {
465
+                    // 1592: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.
466
+                    continue;
467
+                }
468
+                $Warnings[] = 'Code ' . $e->errno . ': ' . display_str($e->message);
469
+            } while ($e->next());
441 470
         }
442
-        $Warnings[] = 'Code ' . $e->errno . ': ' . display_str($e->message);
443
-      } while ($e->next());
471
+        $this->Queries[count($this->Queries) - 1][2] = $Warnings;
444 472
     }
445
-    $this->Queries[count($this->Queries) - 1][2] = $Warnings;
446
-  }
447 473
 }
448
-?>

+ 538
- 503
classes/notificationsmanager.class.php
File diff suppressed because it is too large
View File


+ 42
- 41
classes/notificationsmanagerview.class.php View File

@@ -1,31 +1,32 @@
1
-<?
1
+<?php
2 2
 
3
-class NotificationsManagerView {
4
-  private static $Settings;
3
+class NotificationsManagerView
4
+{
5
+    private static $Settings;
5 6
 
6
-  public static function load_js() {
7
-    $JSIncludes = array(
8
-      'noty/noty.js',
9
-      'noty/layouts/bottomRight.js',
10
-      'noty/themes/default.js',
11
-      'user_notifications.js');
12
-    foreach ($JSIncludes as $JSInclude) {
13
-      $Path = STATIC_SERVER."functions/$JSInclude";
14
-?>
7
+    public static function load_js()
8
+    {
9
+        $JSIncludes = array(
10
+        'noty/noty.js',
11
+        'noty/layouts/bottomRight.js',
12
+        'noty/themes/default.js',
13
+        'user_notifications.js');
14
+        foreach ($JSIncludes as $JSInclude) {
15
+            $Path = STATIC_SERVER."functions/$JSInclude"; ?>
15 16
   <script src="<?=$Path?>?v=<?=filemtime(SERVER_ROOT."/$Path")?>" type="text/javascript"></script>
16
-<?
17
+            <?php
18
+        }
17 19
     }
18
-  }
19 20
 
20
-  public static function render_settings($Settings) {
21
-    self::$Settings = $Settings;
22
-?>
21
+    public static function render_settings($Settings)
22
+    {
23
+        self::$Settings = $Settings; ?>
23 24
     <tr>
24 25
       <td class="label">
25 26
         <strong>News announcements</strong>
26 27
       </td>
27 28
       <td>
28
-<?        self::render_checkbox(NotificationsManager::NEWS); ?>
29
+        <?php        self::render_checkbox(NotificationsManager::NEWS); ?>
29 30
       </td>
30 31
     </tr>
31 32
     <tr>
@@ -33,7 +34,7 @@ class NotificationsManagerView {
33 34
         <strong>Blog announcements</strong>
34 35
       </td>
35 36
       <td>
36
-<?        self::render_checkbox(NotificationsManager::BLOG); ?>
37
+        <?php        self::render_checkbox(NotificationsManager::BLOG); ?>
37 38
       </td>
38 39
     </tr>
39 40
     <tr>
@@ -41,7 +42,7 @@ class NotificationsManagerView {
41 42
         <strong>Inbox messages</strong>
42 43
       </td>
43 44
       <td>
44
-<?        self::render_checkbox(NotificationsManager::INBOX, true); ?>
45
+        <?php        self::render_checkbox(NotificationsManager::INBOX, true); ?>
45 46
       </td>
46 47
     </tr>
47 48
     <tr>
@@ -49,7 +50,7 @@ class NotificationsManagerView {
49 50
         <strong>Staff messages</strong>
50 51
       </td>
51 52
       <td>
52
-<?        self::render_checkbox(NotificationsManager::STAFFPM, false, false); ?>
53
+        <?php        self::render_checkbox(NotificationsManager::STAFFPM, false, false); ?>
53 54
       </td>
54 55
     </tr>
55 56
     <tr>
@@ -57,7 +58,7 @@ class NotificationsManagerView {
57 58
         <strong>Thread subscriptions</strong>
58 59
       </td>
59 60
       <td>
60
-<?        self::render_checkbox(NotificationsManager::SUBSCRIPTIONS, false, false); ?>
61
+        <?php        self::render_checkbox(NotificationsManager::SUBSCRIPTIONS, false, false); ?>
61 62
       </td>
62 63
     </tr>
63 64
     <tr>
@@ -65,50 +66,50 @@ class NotificationsManagerView {
65 66
         <strong>Quote notifications</strong>
66 67
       </td>
67 68
       <td>
68
-<?        self::render_checkbox(NotificationsManager::QUOTES); ?>
69
+        <?php        self::render_checkbox(NotificationsManager::QUOTES); ?>
69 70
       </td>
70 71
     </tr>
71
-<?    if (check_perms('site_torrents_notify')) { ?>
72
+        <?php    if (check_perms('site_torrents_notify')) { ?>
72 73
       <tr>
73 74
         <td class="label tooltip" title="Enabling this will give you a notification when the torrent notification filters you have established are triggered.">
74 75
           <strong>Torrent notifications</strong>
75 76
         </td>
76 77
         <td>
77
-<?          self::render_checkbox(NotificationsManager::TORRENTS, true, false); ?>
78
+            <?php          self::render_checkbox(NotificationsManager::TORRENTS, true, false); ?>
78 79
         </td>
79 80
       </tr>
80
-<?    } ?>
81
+        <?php    } ?>
81 82
 
82 83
     <tr>
83 84
       <td class="label tooltip" title="Enabling this will give you a notification when a torrent is added to a collage you are subscribed to.">
84 85
         <strong>Collage subscriptions</strong>
85 86
       </td>
86 87
       <td>
87
-<?        self::render_checkbox(NotificationsManager::COLLAGES. false, false); ?>
88
+        <?php        self::render_checkbox(NotificationsManager::COLLAGES. false, false); ?>
88 89
       </td>
89 90
     </tr>
90
-<?  }
91
-
92
-  private static function render_checkbox($Name, $Traditional = false) {
93
-    $Checked = self::$Settings[$Name];
94
-    $PopupChecked = $Checked == NotificationsManager::OPT_POPUP || !isset($Checked) ? ' checked="checked"' : '';
95
-    $TraditionalChecked = $Checked == NotificationsManager::OPT_TRADITIONAL ? ' checked="checked"' : '';
91
+        <?php
92
+    }
96 93
 
97
-?>
94
+    private static function render_checkbox($Name, $Traditional = false)
95
+    {
96
+        $Checked = self::$Settings[$Name];
97
+        $PopupChecked = $Checked == NotificationsManager::OPT_POPUP || !isset($Checked) ? ' checked="checked"' : '';
98
+        $TraditionalChecked = $Checked == NotificationsManager::OPT_TRADITIONAL ? ' checked="checked"' : ''; ?>
98 99
     <label>
99 100
       <input type="checkbox" name="notifications_<?=$Name?>_popup" id="notifications_<?=$Name?>_popup"<?=$PopupChecked?> />
100 101
       Pop-up
101 102
     </label>
102
-<?    if ($Traditional) { ?>
103
+        <?php    if ($Traditional) { ?>
103 104
     <label>
104 105
       <input type="checkbox" name="notifications_<?=$Name?>_traditional" id="notifications_<?=$Name?>_traditional"<?=$TraditionalChecked?> />
105 106
       Traditional
106 107
     </label>
107
-<?    }
108
-  }
109
-
110
-  public static function format_traditional($Contents) {
111
-    return "<a href=\"$Contents[url]\">$Contents[message]</a>";
112
-  }
108
+        <?php    }
109
+    }
113 110
 
111
+    public static function format_traditional($Contents)
112
+    {
113
+        return "<a href=\"$Contents[url]\">$Contents[message]</a>";
114
+    }
114 115
 }

+ 61
- 51
classes/paranoia.class.php View File

@@ -1,4 +1,4 @@
1
-<?
1
+<?php
2 2
 
3 3
 // Note: at the time this file is loaded, check_perms is not defined. Don't
4 4
 // call check_paranoia in /classes/script_start.php without ensuring check_perms has been defined
@@ -45,59 +45,69 @@
45 45
 define("PARANOIA_ALLOWED", 1);
46 46
 define("PARANOIA_OVERRIDDEN", 2);
47 47
 
48
-function check_paranoia($Property, $Paranoia = false, $UserClass = false, $UserID = false) {
49
-  global $Classes;
50
-  if ($Property == false) {
51
-    return false;
52
-  }
53
-  if (!is_array($Paranoia)) {
54
-    $Paranoia = json_decode($Paranoia, true);
55
-  }
56
-  if (!is_array($Paranoia)) {
57
-    $Paranoia = [];
58
-  }
59
-  if (is_array($Property)) {
60
-    $all = true;
61
-    foreach ($Property as $P) {
62
-      $all = $all && check_paranoia($P, $Paranoia, $UserClass, $UserID);
48
+function check_paranoia($Property, $Paranoia = false, $UserClass = false, $UserID = false)
49
+{
50
+    global $Classes;
51
+    if ($Property == false) {
52
+        return false;
63 53
     }
64
-    return $all;
65
-  } else {
66
-    if (($UserID !== false) && (G::$LoggedUser['ID'] == $UserID)) {
67
-      return PARANOIA_ALLOWED;
54
+    if (!is_array($Paranoia)) {
55
+        $Paranoia = json_decode($Paranoia, true);
68 56
     }
57
+    if (!is_array($Paranoia)) {
58
+        $Paranoia = [];
59
+    }
60
+    if (is_array($Property)) {
61
+        $all = true;
62
+        foreach ($Property as $P) {
63
+            $all = $all && check_paranoia($P, $Paranoia, $UserClass, $UserID);
64
+        }
65
+        return $all;
66
+    } else {
67
+        if (($UserID !== false) && (G::$LoggedUser['ID'] == $UserID)) {
68
+            return PARANOIA_ALLOWED;
69
+        }
69 70
 
70
-    $May = !in_array($Property, $Paranoia) && !in_array($Property . '+', $Paranoia);
71
-    if ($May)
72
-      return PARANOIA_ALLOWED;
71
+        $May = !in_array($Property, $Paranoia) && !in_array($Property . '+', $Paranoia);
72
+        if ($May) {
73
+            return PARANOIA_ALLOWED;
74
+        }
73 75
 
74
-    if (check_perms('users_override_paranoia', $UserClass)) {
75
-      return PARANOIA_OVERRIDDEN;
76
-    }
77
-    $Override=false;
78
-    switch ($Property) {
79
-      case 'downloaded':
80
-      case 'ratio':
81
-      case 'uploaded':
82
-      case 'lastseen':
83
-        if (check_perms('users_mod', $UserClass))
84
-          return PARANOIA_OVERRIDDEN;
85
-        break;
86
-      case 'snatched': case 'snatched+':
87
-        if (check_perms('users_view_torrents_snatchlist', $UserClass))
88
-          return PARANOIA_OVERRIDDEN;
89
-        break;
90
-      case 'uploads': case 'uploads+':
91
-      case 'seeding': case 'seeding+':
92
-      case 'leeching': case 'leeching+':
93
-        if (check_perms('users_view_seedleech', $UserClass))
94
-          return PARANOIA_OVERRIDDEN;
95
-        break;
96
-      case 'invitedcount':
97
-        if (check_perms('users_view_invites', $UserClass))
98
-          return PARANOIA_OVERRIDDEN;
99
-        break;
76
+        if (check_perms('users_override_paranoia', $UserClass)) {
77
+            return PARANOIA_OVERRIDDEN;
78
+        }
79
+        $Override=false;
80
+        switch ($Property) {
81
+            case 'downloaded':
82
+            case 'ratio':
83
+            case 'uploaded':
84
+            case 'lastseen':
85
+                if (check_perms('users_mod', $UserClass)) {
86
+                    return PARANOIA_OVERRIDDEN;
87
+                }
88
+                break;
89
+            case 'snatched':
90
+            case 'snatched+':
91
+                if (check_perms('users_view_torrents_snatchlist', $UserClass)) {
92
+                    return PARANOIA_OVERRIDDEN;
93
+                }
94
+                break;
95
+            case 'uploads':
96
+            case 'uploads+':
97
+            case 'seeding':
98
+            case 'seeding+':
99
+            case 'leeching':
100
+            case 'leeching+':
101
+                if (check_perms('users_view_seedleech', $UserClass)) {
102
+                    return PARANOIA_OVERRIDDEN;
103
+                }
104
+                break;
105
+            case 'invitedcount':
106
+                if (check_perms('users_view_invites', $UserClass)) {
107
+                    return PARANOIA_OVERRIDDEN;
108
+                }
109
+                break;
110
+        }
111
+        return false;
100 112
     }
101
-    return false;
102
-  }
103 113
 }

+ 88
- 79
classes/permissions.class.php View File

@@ -1,97 +1,106 @@
1
-<?
2
-class Permissions {
3
-  /* Check to see if a user has the permission to perform an action
4
-   * This is called by check_perms in util.php, for convenience.
5
-   *
6
-   * @param string PermissionName
7
-   * @param string $MinClass Return false if the user's class level is below this.
8
-   */
9
-  public static function check_perms($PermissionName, $MinClass = 0) {
10
-    if (G::$LoggedUser['EffectiveClass'] >= 1000) return true; // Sysops can do anything
11
-    if (G::$LoggedUser['EffectiveClass'] < $MinClass) return false; // MinClass failure
12
-    return G::$LoggedUser['Permissions'][$PermissionName] ?? false; // Return actual permission
13
-  }
1
+<?php
2
+class Permissions
3
+{
4
+    /* Check to see if a user has the permission to perform an action
5
+     * This is called by check_perms in util.php, for convenience.
6
+     *
7
+     * @param string PermissionName
8
+     * @param string $MinClass Return false if the user's class level is below this.
9
+     */
10
+    public static function check_perms($PermissionName, $MinClass = 0)
11
+    {
12
+        if (G::$LoggedUser['EffectiveClass'] >= 1000) {
13
+            return true; // Sysops can do anything
14
+        }
15
+        if (G::$LoggedUser['EffectiveClass'] < $MinClass) {
16
+            return false; // MinClass failure
17
+        }
18
+        return G::$LoggedUser['Permissions'][$PermissionName] ?? false; // Return actual permission
19
+    }
14 20
 
15
-  /**
16
-   * Gets the permissions associated with a certain permissionid
17
-   *
18
-   * @param int $PermissionID the kind of permissions to fetch
19
-   * @return array permissions
20
-   */
21
-  public static function get_permissions($PermissionID) {
22
-    $Permission = G::$Cache->get_value("perm_$PermissionID");
23
-    if (empty($Permission)) {
24
-      $QueryID = G::$DB->get_query_id();
25
-      G::$DB->query("
21
+    /**
22
+     * Gets the permissions associated with a certain permissionid
23
+     *
24
+     * @param int $PermissionID the kind of permissions to fetch
25
+     * @return array permissions
26
+     */
27
+    public static function get_permissions($PermissionID)
28
+    {
29
+        $Permission = G::$Cache->get_value("perm_$PermissionID");
30
+        if (empty($Permission)) {
31
+            $QueryID = G::$DB->get_query_id();
32
+            G::$DB->query("
26 33
         SELECT Level AS Class, `Values` AS Permissions, Secondary, PermittedForums
27 34
         FROM permissions
28 35
         WHERE ID = '$PermissionID'");
29
-      $Permission = G::$DB->next_record(MYSQLI_ASSOC, ['Permissions']);
30
-      G::$DB->set_query_id($QueryID);
31
-      $Permission['Permissions'] = unserialize($Permission['Permissions']);
32
-      G::$Cache->cache_value("perm_$PermissionID", $Permission, 2592000);
36
+            $Permission = G::$DB->next_record(MYSQLI_ASSOC, ['Permissions']);
37
+            G::$DB->set_query_id($QueryID);
38
+            $Permission['Permissions'] = unserialize($Permission['Permissions']);
39
+            G::$Cache->cache_value("perm_$PermissionID", $Permission, 2592000);
40
+        }
41
+        return $Permission;
33 42
     }
34
-    return $Permission;
35
-  }
36 43
 
37
-  /**
38
-   * Get a user's permissions.
39
-   *
40
-   * @param $UserID
41
-   * @param array|false $CustomPermissions
42
-   *  Pass in the user's custom permissions if you already have them.
43
-   *  Leave false if you don't have their permissions. The function will fetch them.
44
-   * @return array Mapping of PermissionName=>bool/int
45
-   */
46
-  public static function get_permissions_for_user($UserID, $CustomPermissions = false) {
47
-    $UserInfo = Users::user_info($UserID);
44
+    /**
45
+     * Get a user's permissions.
46
+     *
47
+     * @param $UserID
48
+     * @param array|false $CustomPermissions
49
+     *  Pass in the user's custom permissions if you already have them.
50
+     *  Leave false if you don't have their permissions. The function will fetch them.
51
+     * @return array Mapping of PermissionName=>bool/int
52
+     */
53
+    public static function get_permissions_for_user($UserID, $CustomPermissions = false)
54
+    {
55
+        $UserInfo = Users::user_info($UserID);
48 56
 
49
-    // Fetch custom permissions if they weren't passed in.
50
-    if ($CustomPermissions === false) {
51
-      $QueryID = G::$DB->get_query_id();
52
-      G::$DB->query('
57
+        // Fetch custom permissions if they weren't passed in.
58
+        if ($CustomPermissions === false) {
59
+            $QueryID = G::$DB->get_query_id();
60
+            G::$DB->query('
53 61
         SELECT CustomPermissions
54 62
         FROM users_main
55 63
         WHERE ID = ' . (int)$UserID);
56
-      list($CustomPermissions) = G::$DB->next_record(MYSQLI_NUM, false);
57
-      G::$DB->set_query_id($QueryID);
58
-    }
64
+            list($CustomPermissions) = G::$DB->next_record(MYSQLI_NUM, false);
65
+            G::$DB->set_query_id($QueryID);
66
+        }
59 67
 
60
-    if (!empty($CustomPermissions) && !is_array($CustomPermissions)) {
61
-      $CustomPermissions = unserialize($CustomPermissions);
62
-    }
68
+        if (!empty($CustomPermissions) && !is_array($CustomPermissions)) {
69
+            $CustomPermissions = unserialize($CustomPermissions);
70
+        }
63 71
 
64
-    $Permissions = self::get_permissions($UserInfo['PermissionID']);
72
+        $Permissions = self::get_permissions($UserInfo['PermissionID']);
65 73
 
66
-    // Manage 'special' inherited permissions
67
-    $BonusPerms = [];
68
-    $BonusCollages = 0;
69
-    foreach ($UserInfo['ExtraClasses'] as $PermID => $Value) {
70
-      $ClassPerms = self::get_permissions($PermID);
71
-      $BonusCollages += $ClassPerms['Permissions']['MaxCollages'];
72
-      unset($ClassPerms['Permissions']['MaxCollages']);
73
-      $BonusPerms = array_merge($BonusPerms, $ClassPerms['Permissions']);
74
-    }
74
+        // Manage 'special' inherited permissions
75
+        $BonusPerms = [];
76
+        $BonusCollages = 0;
77
+        foreach ($UserInfo['ExtraClasses'] as $PermID => $Value) {
78
+            $ClassPerms = self::get_permissions($PermID);
79
+            $BonusCollages += $ClassPerms['Permissions']['MaxCollages'];
80
+            unset($ClassPerms['Permissions']['MaxCollages']);
81
+            $BonusPerms = array_merge($BonusPerms, $ClassPerms['Permissions']);
82
+        }
75 83
 
76
-    if (empty($CustomPermissions)) {
77
-      $CustomPermissions = [];
78
-    }
84
+        if (empty($CustomPermissions)) {
85
+            $CustomPermissions = [];
86
+        }
79 87
 
80
-    $MaxCollages = ($Permissions['Permissions']['MaxCollages'] ?? 0) + $BonusCollages;
81
-    if (isset($CustomPermissions['MaxCollages'])) {
82
-      $MaxCollages += $CustomPermissions['MaxCollages'];
83
-      unset($CustomPermissions['MaxCollages']);
88
+        $MaxCollages = ($Permissions['Permissions']['MaxCollages'] ?? 0) + $BonusCollages;
89
+        if (isset($CustomPermissions['MaxCollages'])) {
90
+            $MaxCollages += $CustomPermissions['MaxCollages'];
91
+            unset($CustomPermissions['MaxCollages']);
92
+        }
93
+        $Permissions['Permissions']['MaxCollages'] = $MaxCollages;
94
+        // Combine the permissions
95
+        return array_merge(
96
+            $Permissions['Permissions'],
97
+            $BonusPerms,
98
+            $CustomPermissions
99
+        );
84 100
     }
85
-    $Permissions['Permissions']['MaxCollages'] = $MaxCollages;
86
-    // Combine the permissions
87
-    return array_merge(
88
-        $Permissions['Permissions'],
89
-        $BonusPerms,
90
-        $CustomPermissions);
91
-  }
92 101
 
93
-  public static function is_mod($UserID) {
94
-    return self::get_permissions_for_user($UserID)['users_mod'] ?? false;
95
-  }
102
+    public static function is_mod($UserID)
103
+    {
104
+        return self::get_permissions_for_user($UserID)['users_mod'] ?? false;
105
+    }
96 106
 }
97
-?>

Loading…
Cancel
Save