|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @version 2015-04-11 11:47:49 39b14.2 |
|
5
|
|
|
* @copyright 2008-2015 Gorlum for Project "SuperNova.WS" |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
defined('INSIDE') || die(); |
|
9
|
|
|
|
|
10
|
|
|
require_once('db/db_queries.php'); |
|
11
|
|
|
|
|
12
|
|
|
function db_change_units_perform($query, $tablename, $object_id) { |
|
13
|
|
|
$query = implode(',', $query); |
|
14
|
|
|
if($query && $object_id) { |
|
15
|
|
|
return classSupernova::db_upd_record_by_id($tablename == 'users' ? LOC_USER : LOC_PLANET, $object_id, $query); |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// TODO: THIS FUNCTION IS OBSOLETE AND SHOULD BE REPLACED! |
|
20
|
|
|
// TODO - ТОЛЬКО ДЛЯ РЕСУРСОВ |
|
21
|
|
|
// $unit_list should have unique entrances! Recompress non-uniq entrances before pass param! |
|
22
|
|
|
function db_change_units(&$user, &$planet, $unit_list = array(), $query = null) { |
|
23
|
|
|
$query = is_array($query) ? $query : array( |
|
24
|
|
|
LOC_USER => array(), |
|
25
|
|
|
LOC_PLANET => array(), |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$group = sn_get_groups('resources_loot'); |
|
29
|
|
|
|
|
30
|
|
|
foreach($unit_list as $unit_id => $unit_amount) { |
|
31
|
|
|
if(!in_array($unit_id, $group)) { |
|
32
|
|
|
// TODO - remove later |
|
33
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>'); |
|
34
|
|
|
pdump(debug_backtrace()); |
|
35
|
|
|
die('db_change_units() вызван для не-ресурсов!'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if(!$unit_amount) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$unit_db_name = pname_resource_name($unit_id); |
|
43
|
|
|
|
|
44
|
|
|
$unit_location = sys_get_unit_location($user, $planet, $unit_id); |
|
45
|
|
|
|
|
46
|
|
|
// Changing value in object |
|
47
|
|
|
switch($unit_location) { |
|
48
|
|
|
case LOC_USER: |
|
49
|
|
|
$user[$unit_db_name] += $unit_amount; |
|
50
|
|
|
break; |
|
51
|
|
|
case LOC_PLANET: |
|
52
|
|
|
$planet[$unit_db_name] += $unit_amount; |
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$unit_amount = $unit_amount < 0 ? $unit_amount : "+{$unit_amount}"; // Converting positive unit_amount to string '+unit_amount' |
|
57
|
|
|
$query[$unit_location][$unit_id] = "`{$unit_db_name}`=`{$unit_db_name}`{$unit_amount}"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
db_change_units_perform($query[LOC_USER], 'users', $user['id']); |
|
61
|
|
|
db_change_units_perform($query[LOC_PLANET], 'planets', $planet['id']); |
|
62
|
|
|
} |
|
63
|
|
|
function sn_db_perform($table, $values, $type = 'insert', $options = false) { |
|
64
|
|
|
$field_set = ''; |
|
65
|
|
|
|
|
66
|
|
|
switch($type) { |
|
67
|
|
|
case 'delete': |
|
68
|
|
|
$query = 'DELETE FROM'; |
|
69
|
|
|
break; |
|
70
|
|
|
|
|
71
|
|
|
case 'insert': |
|
72
|
|
|
$query = 'INSERT INTO'; |
|
73
|
|
|
if(isset($options['__multi'])) { |
|
74
|
|
|
// Here we generate mass-insert set |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
case 'update': |
|
78
|
|
|
if(!$query) { |
|
|
|
|
|
|
79
|
|
|
$query = 'UPDATE'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
foreach($values as $field => &$value) { |
|
83
|
|
|
$value_type = gettype($value); |
|
84
|
|
|
if ($value_type == TYPE_STRING) { |
|
85
|
|
|
$value = "'" . db_escape($value) . "'"; |
|
86
|
|
|
} |
|
87
|
|
|
$value = "`{$field}` = {$value}"; |
|
88
|
|
|
} |
|
89
|
|
|
$field_set = 'SET ' . implode(', ', $values); |
|
90
|
|
|
break; |
|
91
|
|
|
|
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
$query .= " {$table} {$field_set}"; |
|
95
|
|
|
return classSupernova::$db->doExecute($query); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
function sn_db_field_set_is_safe(&$field_set) { |
|
101
|
|
|
return !empty($field_set['__IS_SAFE']); |
|
102
|
|
|
} |
|
103
|
|
|
function sn_db_field_set_safe_flag_clear(&$field_set) { |
|
104
|
|
|
unset($field_set['__IS_SAFE']); |
|
105
|
|
|
} |
|
106
|
|
|
function sn_db_field_set_safe_flag_set(&$field_set) { |
|
107
|
|
|
$field_set['__IS_SAFE'] = true; |
|
108
|
|
|
} |
|
109
|
|
|
function sn_db_field_set_make_safe($field_set, $serialize = false) { |
|
110
|
|
|
if(!is_array($field_set)) { |
|
111
|
|
|
die('$field_set is not an array!'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$result = array(); |
|
115
|
|
|
foreach($field_set as $field => $value) { |
|
116
|
|
|
$field = db_escape(trim($field)); |
|
117
|
|
|
switch (true) { |
|
118
|
|
|
case is_int($value): |
|
119
|
|
|
case is_double($value): |
|
120
|
|
|
break; |
|
121
|
|
|
|
|
122
|
|
|
case is_bool($value): |
|
123
|
|
|
$value = intval($value); |
|
124
|
|
|
break; |
|
125
|
|
|
|
|
126
|
|
|
case is_array($value): |
|
127
|
|
|
case is_object($value): |
|
128
|
|
|
$serialize ? $value = serialize($value) : die('$value is object or array with no $serialize'); |
|
129
|
|
|
|
|
130
|
|
|
case is_string($value): |
|
131
|
|
|
$value = '"' . db_escape($value) . '"'; |
|
132
|
|
|
break; |
|
133
|
|
|
|
|
134
|
|
|
case is_null($value): |
|
135
|
|
|
$value = 'NULL'; |
|
136
|
|
|
break; |
|
137
|
|
|
|
|
138
|
|
|
default: |
|
139
|
|
|
die('unsupported operand type'); |
|
140
|
|
|
} |
|
141
|
|
|
$result[$field] = $value; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
sn_db_field_set_safe_flag_set($field_set); |
|
145
|
|
|
|
|
146
|
|
|
return $result; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
function sn_db_unit_changeset_prepare($unit_id, $unit_value, $user, $planet_id = null) { |
|
151
|
|
|
return classSupernova::db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
function sn_db_transaction_check($transaction_should_be_started = null) { |
|
157
|
|
|
return classSupernova::$gc->db->getTransaction()->check($transaction_should_be_started); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
function sn_db_transaction_start($level = '') { |
|
160
|
|
|
return classSupernova::$gc->db->getTransaction()->start($level); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
function sn_db_transaction_commit() { |
|
163
|
|
|
return classSupernova::$gc->db->getTransaction()->commit(); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
function sn_db_transaction_rollback() { |
|
166
|
|
|
return classSupernova::$gc->db->getTransaction()->rollback(); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
function db_fetch(&$query) { |
|
172
|
|
|
return classSupernova::$gc->db->db_fetch($query); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
function db_escape($unescaped_string) { |
|
175
|
|
|
return classSupernova::$gc->db->db_escape($unescaped_string); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: