-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.php
More file actions
258 lines (236 loc) · 7.65 KB
/
Copy pathDataBase.php
File metadata and controls
258 lines (236 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Created by PhpStorm.
* User: maha
* Date: 19.08.17
* Time: 12:36
*/
class DataBase
{
private $conn='';
/*
* connect to bd
*/
function __construct($hn, $un, $pw, $db){
$this->conn = new mysqli($hn, $un, $pw, $db);
if ($this->conn->connect_error) die($this->conn->connect_error);
}
/*
* adding new user to bd
*/
function user_add($data){
$id = null;
$name = null;
$email = null;
$phone = null;
if (isset($data['uid'])){//vk
$id = $data['uid'].'_vk';
$name = $data['first_name'].' '.$data['last_name'];
$email = $data['email'];
$phone = $data['mobile_phone'].' '.$data['home_phone'];
} elseif (isset($data['id'])){//fb
$id = $data['id'].'_fb';
$name = $data['name'];
$email = $data['email'];
}
//add id and name of user to sesstion
$this->add_session($id, $name);
if (!$this->user_is($id)) {
$query ="INSERT INTO users VALUES(NULL, '$id', '$name', '$email', '$phone', NULL, NULL);";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
}
/*
* update date of current user
*/
function update($name, $email, $phone, $vk, $fb){
$name = $this->sanitizeMySQL($this->conn, $name);
$email = $this->sanitizeMySQL($this->conn, $email);
$phone = $this->sanitizeMySQL($this->conn, $phone);
$vk = $this->sanitizeMySQL($this->conn, $vk);
$fb = $this->sanitizeMySQL($this->conn, $fb);
$this->start_session();
$id = $_SESSION['id'];
if ($name!=NULL) {
$query = "UPDATE users SET name='$name' WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
if ($email!=NULL) {
$query = "UPDATE users SET email='$email' WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
if ($phone!=NULL) {
$query = "UPDATE users SET phone='$phone' WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
if ($vk!=NULL) {
$query = "UPDATE users SET vk='$vk' WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
if ($fb!=NULL) {
$query = "UPDATE users SET fb='$fb' WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
}
/*
* update order of current use
* change status not paid on paid
*/
function paid_order($num_order){
$query = "UPDATE orders SET status='paid' WHERE number='$num_order'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
}
/*
* add date to table 'orders' of current user
* column url_download is 'в работе'
* column status is 'not paid'
*/
function add_order($url_landing, $vk_pixel, $fb_pixel, $metka_pixel, $date, $total_price, $text, $modules){
$this->start_session();
$id = $_SESSION['id'];
$url_landing = $this->sanitizeMySQL($this->conn, $url_landing);
$text = $this->sanitizeMySQL($this->conn, $text);
$query ="INSERT INTO orders VALUES(NULL, '$id', '$url_landing', '$vk_pixel',
'$fb_pixel', '$metka_pixel', '$date', '$total_price', '$text', '$modules', 'в работе', 'not paid');";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
$result->close();
}
/*
* return all orders of current user
*/
function out_orders(){
$this->start_session();
$id = $_SESSION['id'];
$query = "SELECT * FROM orders WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
return $result;
}
/*
* return an order of special number
*/
function get_order($number){
$query = "SELECT * FROM orders WHERE number='$number'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
return $result;
}
/*
* checking is current user in table users
* return true if is else false
*/
function user_is($id){
$query ="SELECT EXISTS(SELECT id FROM users WHERE id = '$id')";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
$result->data_seek(0);
if ($result->fetch_array(MYSQLI_NUM)[0]) {
$result->close();
return true;
} else {
$result->close();
return false;
}
}
/*
* if user is in system
*/
function is_authorisation(){
$this->start_session();
if (isset($_SESSION['id'])){
echo "Вы авторизированны";
}
}
/*
* add session of current user
* name and id are in session
*/
function add_session($id, $name){
//$this->destroy_session_and_data();
$this->start_session();
$_SESSION['id'] = $id;
$_SESSION['name'] = $name;
}
/*
* return data of current user from the table 'users'
*/
function take_inf(){
$this->start_session();
$id = $_SESSION['id'];
$query = "SELECT * FROM users WHERE id='$id'";
$result = $this->conn->query($query);
if (!$result) die ("Database access failed: " . $this->conn->error);
return $result;
}
/*
* hash safety
*/
private function myhash($password){
$salt1 ="maha";
$salt2 ="stas";
return hash('ripemd128', "$salt1$password$salt2");
}
function destroy_session_and_data()
{
$_SESSION = array();
setcookie(session_name(), '', time() - 2592000, '/');
session_destroy();
}
/*
* start sesseion with more safety
*/
function start_session(){
session_start();
if (!isset($_SESSION['initiated']))
{
session_regenerate_id();
$_SESSION['initiated'] = 1;
}
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
}
/*
* if someone use id of our users
* now dont use this function
*/
function different_user(){
$this->destroy_session_and_data();
echo "Ввойдите занова";
}
/*
* safety out
*/
function mysql_entities_fix_string($conn, $string){
return htmlentities(mysql_fix_string($conn, $string));
}
function mysql_fix_string($conn, $string){
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return $conn->real_escape_string($string);
}
/*
* safety in
*/
function sanitizeString($var){
//$var =stripslashes($var);
$var = strip_tags($var);
$var = htmlentities($var);
return $var;
}
function sanitizeMySQL($connection, $var){
$var = $connection->real_escape_string($var);
$var = $this->sanitizeString($var);
return $var;
}
function __destruct(){
$this->conn->close();
}
}
?>