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
|
|
|
/** |
13
|
|
|
* @param $adjust |
14
|
|
|
* @param $location |
15
|
|
|
* @param $object_id |
16
|
|
|
*/ |
17
|
|
|
function db_change_units_perform($adjust, $location, $object_id) { |
18
|
|
|
if (!empty($object_id) && !empty($adjust)) { |
19
|
|
|
classSupernova::$gc->cacheOperator->db_upd_record_by_id( |
|
|
|
|
20
|
|
|
$location, |
21
|
|
|
$object_id, |
22
|
|
|
array(), |
23
|
|
|
$adjust |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// TODO: THIS FUNCTION IS OBSOLETE AND SHOULD BE REPLACED! |
29
|
|
|
// TODO - ТОЛЬКО ДЛЯ РЕСУРСОВ |
30
|
|
|
// $unit_list should have unique entrances! Recompress non-uniq entrances before pass param! |
31
|
|
|
/** |
32
|
|
|
* @param $user |
33
|
|
|
* @param $planet |
34
|
|
|
* @param array $unit_list |
35
|
|
|
* @param null $query |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
function db_change_units(&$user, &$planet, $unit_list) { |
38
|
|
|
$query = array( |
39
|
|
|
LOC_USER => array(), |
40
|
|
|
LOC_PLANET => array(), |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$group = sn_get_groups('resources_loot'); |
44
|
|
|
|
45
|
|
|
foreach($unit_list as $unit_id => $unit_amount) { |
46
|
|
|
if(!in_array($unit_id, $group)) { |
47
|
|
|
// TODO - remove later |
48
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>'); |
49
|
|
|
pdump(debug_backtrace()); |
50
|
|
|
die('db_change_units() вызван для не-ресурсов!'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if(empty($unit_amount)) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$unit_db_name = pname_resource_name($unit_id); |
58
|
|
|
|
59
|
|
|
$unit_location = sys_get_unit_location($user, $planet, $unit_id); |
60
|
|
|
|
61
|
|
|
// Changing value in object |
62
|
|
|
switch($unit_location) { |
63
|
|
|
case LOC_USER: |
64
|
|
|
$user[$unit_db_name] += $unit_amount; |
65
|
|
|
break; |
66
|
|
|
case LOC_PLANET: |
67
|
|
|
$planet[$unit_db_name] += $unit_amount; |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$query[$unit_location][$unit_db_name] = $unit_amount; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
db_change_units_perform($query[LOC_USER], LOC_USER, $user['id']); |
75
|
|
|
db_change_units_perform($query[LOC_PLANET], LOC_PLANET, $planet['id']); |
76
|
|
|
} |
77
|
|
|
function sn_db_perform($table, $values, $type = 'insert', $options = false) { |
78
|
|
|
$field_set = ''; |
79
|
|
|
|
80
|
|
|
switch($type) { |
81
|
|
|
case 'delete': |
82
|
|
|
$query = 'DELETE FROM'; |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case 'insert': |
86
|
|
|
$query = 'INSERT INTO'; |
87
|
|
|
if(isset($options['__multi'])) { |
88
|
|
|
// Here we generate mass-insert set |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
case 'update': |
92
|
|
|
if(!$query) { |
|
|
|
|
93
|
|
|
$query = 'UPDATE'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach($values as $field => &$value) { |
97
|
|
|
$value_type = gettype($value); |
98
|
|
|
if ($value_type == TYPE_STRING) { |
99
|
|
|
$value = "'" . db_escape($value) . "'"; |
100
|
|
|
} |
101
|
|
|
$value = "`{$field}` = {$value}"; |
102
|
|
|
} |
103
|
|
|
$field_set = 'SET ' . implode(', ', $values); |
104
|
|
|
break; |
105
|
|
|
|
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
$query .= " {$table} {$field_set}"; |
109
|
|
|
return classSupernova::$db->doExecute($query); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
function sn_db_field_set_is_safe(&$field_set) { |
115
|
|
|
return !empty($field_set['__IS_SAFE']); |
116
|
|
|
} |
117
|
|
|
function sn_db_field_set_safe_flag_clear(&$field_set) { |
118
|
|
|
unset($field_set['__IS_SAFE']); |
119
|
|
|
} |
120
|
|
|
function sn_db_field_set_safe_flag_set(&$field_set) { |
121
|
|
|
$field_set['__IS_SAFE'] = true; |
122
|
|
|
} |
123
|
|
|
function sn_db_field_set_make_safe($field_set, $serialize = false) { |
124
|
|
|
if(!is_array($field_set)) { |
125
|
|
|
die('$field_set is not an array!'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$result = array(); |
129
|
|
|
foreach($field_set as $field => $value) { |
130
|
|
|
$field = db_escape(trim($field)); |
131
|
|
|
switch (true) { |
132
|
|
|
case is_int($value): |
133
|
|
|
case is_double($value): |
134
|
|
|
break; |
135
|
|
|
|
136
|
|
|
case is_bool($value): |
137
|
|
|
$value = intval($value); |
138
|
|
|
break; |
139
|
|
|
|
140
|
|
|
case is_array($value): |
141
|
|
|
case is_object($value): |
142
|
|
|
$serialize ? $value = serialize($value) : die('$value is object or array with no $serialize'); |
143
|
|
|
|
144
|
|
|
case is_string($value): |
145
|
|
|
$value = '"' . db_escape($value) . '"'; |
146
|
|
|
break; |
147
|
|
|
|
148
|
|
|
case is_null($value): |
149
|
|
|
$value = 'NULL'; |
150
|
|
|
break; |
151
|
|
|
|
152
|
|
|
default: |
153
|
|
|
die('unsupported operand type'); |
154
|
|
|
} |
155
|
|
|
$result[$field] = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
sn_db_field_set_safe_flag_set($field_set); |
159
|
|
|
|
160
|
|
|
return $result; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
function sn_db_transaction_check($transaction_should_be_started = null) { |
167
|
|
|
return classSupernova::$gc->db->getTransaction()->check($transaction_should_be_started); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
function sn_db_transaction_start($level = '') { |
170
|
|
|
return classSupernova::$gc->db->getTransaction()->start($level); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
function sn_db_transaction_commit() { |
173
|
|
|
return classSupernova::$gc->db->getTransaction()->commit(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
function sn_db_transaction_rollback() { |
176
|
|
|
return classSupernova::$gc->db->getTransaction()->rollback(); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
function db_fetch(&$query) { |
182
|
|
|
return classSupernova::$gc->db->db_fetch($query); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
function db_escape($unescaped_string) { |
185
|
|
|
return classSupernova::$gc->db->db_escape($unescaped_string); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: