-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_class.php
154 lines (145 loc) · 4.15 KB
/
admin_class.php
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
<?php
session_start();
ini_set('display_errors', 1);
Class Action {
private $db;
public function __construct() {
ob_start();
include 'db_connect.php';
$this->db = $conn;
}
function __destruct() {
$this->db->close();
ob_end_flush();
}
function login(){
extract($_POST);
$qry = $this->db->query("SELECT * FROM users where email = '".$email."' and password = '".md5($password)."' ");
if($qry->num_rows > 0){
foreach ($qry->fetch_array() as $key => $value) {
if($key != 'password' && !is_numeric($key))
$_SESSION['login_'.$key] = $value;
}
return 1;
}else{
return 2;
}
}
function logout(){
session_destroy();
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
header("location:index.php");
}
function save_user(){
extract($_POST);
$data = "";
foreach($_POST as $k => $v){
if(!in_array($k,array('id','cpass'))){
if(!empty($data))
$data .= ", ";
$data .= " $k='$v' ";
}
}
$chk = $this->db->query("Select * from users where email = '$email' and id !='$id' ")->num_rows;
if($chk > 0){
return 2;
exit;
}
if($_FILES['img']['tmp_name'] != ''){
$fname = strtotime(date('y-m-d H:i')).'_'.$_FILES['img']['name'];
$move = move_uploaded_file($_FILES['img']['tmp_name'],'assets/uploads/'. $fname);
$data .= ", avatar = '$fname' ";
}
if(empty($id)){
$save = $this->db->query("INSERT INTO users set ".$data);
$id=$this->db->insert_id;
}else{
$save = $this->db->query("UPDATE users set ".$data." where id = ".$id);
}
if($save){
$_SESSION['login_id'] = $id;
foreach ($_POST as $key => $value) {
if(!in_array($key,array('id','cpass')))
$_SESSION['login_'.$key] = $value;
}
return 1;
}
}
function delete_user(){
extract($_POST);
$delete = $this->db->query("DELETE FROM users where id = ".$id);
if($delete)
return 1;
}
function save_upload(){
extract($_POST);
$data = "";
foreach($_POST as $k => $v){
if(!in_array($k,array('id','img','vid'))){
if($k == 'description'){
}
if(!empty($data))
$data .= ", ";
$data .= " $k='$v' ";
}
}
if(empty($id)){
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$i = 1;
while($i == 1){
$_name =substr(str_shuffle($chars), 0, 16);
$chk = $this->db->query("SELECT * from uploads where code = '$_name' and id !='$id' ")->num_rows;
if($chk <= 0){
$i = 0;
}
}
$data .= ", code = '$_name' ";
}else{
$_name = $this->db->query("SELECT * from uploads where id = '$id' ")->fetch_array()['code'];
}
if($_FILES['img']['tmp_name'] != ''){
$ext = substr($_FILES['img']['name'], strrpos($_FILES['img']['name'], '.')+1);
$fname = $_name.'.'.$ext;
if(is_file('assets/uploads/thumbnail/'. $fname))
unlink('assets/uploads/thumbnail/'. $fname);
$move = move_uploaded_file($_FILES['img']['tmp_name'],'assets/uploads/thumbnail/'. $fname);
$data .= ", thumbnail_path = '$fname' ";
}
if($_FILES['vid']['tmp_name'] != ''){
$ext = substr($_FILES['vid']['name'], strrpos($_FILES['vid']['name'], '.')+1);
$fname = $_name.'.'.$ext;
if(is_file('assets/uploads/videos/'. $fname))
unlink('assets/uploads/videos/'. $fname);
$move = move_uploaded_file($_FILES['vid']['tmp_name'],'assets/uploads/videos/'. $fname);
$data .= ", video_path = '$fname' ";
}
if(empty($id)){
$data .= ", user_id = {$_SESSION['login_id']} ";
$save = $this->db->query("INSERT INTO uploads set ".$data);
$id=$this->db->insert_id;
}else{
$save = $this->db->query("UPDATE uploads set ".$data." where id = ".$id);
}
if($save){
return 1;
}
}
function delete_upload(){
extract($_POST);
$qry = $this->db->query("SELECT * from uploads where id = '$id' ")->fetch_array();
$delete = $this->db->query("DELETE FROM uploads where id = '$id'");
if($delete){
if(!empty($qry['thumbnail_path'])){
if(is_file('assets/uploads/thumbnail/'.$qry['thumbnail_path']))
unlink('assets/uploads/thumbnail/'.$qry['thumbnail_path']);
}
if(!empty($qry['video_path'])){
if(is_file('assets/uploads/videos/'.$qry['video_path']))
unlink('assets/uploads/videos/'.$qry['video_path']);
}
return 1;
}
}
}