Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

include/classes/setting.class.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class Setting extends Base {
5
  protected $table = 'settings';
6
  private $cache = array();
7
8
  /**
9
   * Fetch all values available and cache them in this class
10
   * That way we don't fetch them from DB for each call
11
   */
12
  public function createCache() {
13
    if ($aSettings = $this->getAllAssoc()) {
14
      foreach ($aSettings as $key => $aData) {
15
        $this->cache[$aData['name']] = $aData['value'];
16
      }
17
      return true;
18
    }
19
    return false;
20
  }
21
22
  /**
23
   * Flush our local cache, may be required for upgrades
24
   * or other places where we need live data
25
   **/
26
  public function flushCache() {
27
    $this->cache = array();
28
    return true;
29
  }
30
31
  /**
32
   * Fetch a value from our table
33
   * @param name string Setting name
34
   * @return value string Value
35
   **/
36
  public function getValue($name, $default="") {
37
    // Try our class cache first
38
    if (isset($this->cache[$name])) return $this->cache[$name];
39
    $stmt = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name = ? LIMIT 1");
40
    if ($this->checkStmt($stmt) && $stmt->bind_param('s', $name) && $stmt->execute() && $result = $stmt->get_result()) {
41
      if ($result->num_rows > 0) {
42
        return $result->fetch_object()->value;
43
      } else {
44
        return $default;
45
      }
46
    }
47
    // Log error but return empty string
48
    $this->sqlError();
49
    return $default;
50
  }
51
52
  /**
53
   * Insert or update a setting
54
   * @param name string Name of the variable
55
   * @param value string Variable value
56
   * @return bool
57
   **/
58 View Code Duplication
  public function setValue($name, $value) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    // Update local cache too
60
    $this->cache[$name] = $value;
61
    $stmt = $this->mysqli->prepare("
62
      INSERT INTO $this->table (name, value)
63
      VALUES (?, ?)
64
      ON DUPLICATE KEY UPDATE value = ?");
65
    if ($stmt && $stmt->bind_param('sss', $name, $value, $value) && $stmt->execute())
66
      return true;
67
    return $this->sqlError();
68
  }
69
}
70
71
$setting = new Setting($debug, $mysqli);
72
$setting->setDebug($debug);
73
$setting->setMysql($mysqli);
74
$setting->setErrorCodes($aErrorCodes);
75
// Fill our class cache with data so we don't have to run SQL queries all the time
76
$setting->createCache();
77