-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_class.php
167 lines (158 loc) · 4.38 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
155
156
157
158
159
160
161
162
163
164
165
166
167
<?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 *,concat(lastname,', ',firstname,' ',middlename) as name 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 3;
}
}
function logout(){
session_destroy();
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
header("location:login.php");
}
function save_user(){
extract($_POST);
$data = "";
foreach($_POST as $k => $v){
if(!in_array($k, array('id','cpass')) && !is_numeric($k)){
if($k =='password')
$v = md5($v);
if(empty($data)){
$data .= " $k='$v' ";
}else{
$data .= ", $k='$v' ";
}
}
}
$check = $this->db->query("SELECT * FROM users where email ='$email' ".(!empty($id) ? " and id != {$id} " : ''))->num_rows;
if($check > 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");
}else{
$save = $this->db->query("UPDATE users set $data where id = $id");
}
if($save){
return 1;
}
}
function update_user(){
extract($_POST);
$data = "";
foreach($_POST as $k => $v){
if(!in_array($k, array('id','cpass','table')) && !is_numeric($k)){
if($k =='password')
$v = md5($v);
if(empty($data)){
$data .= " $k='$v' ";
}else{
$data .= ", $k='$v' ";
}
}
}
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' ";
}
$check = $this->db->query("SELECT * FROM users where email ='$email' ".(!empty($id) ? " and id != {$id} " : ''))->num_rows;
if($check > 0){
return 2;
exit;
}
if(empty($id)){
$save = $this->db->query("INSERT INTO users set $data");
}else{
$save = $this->db->query("UPDATE users set $data where id = $id");
}
if($save){
foreach ($_POST as $key => $value) {
if($key != 'password' && !is_numeric($key))
$_SESSION['login_'.$key] = $value;
}
if($_FILES['img']['tmp_name'] != '')
$_SESSION['login_avatar'] = $fname;
return 1;
}
}
function delete_user(){
extract($_POST);
$delete = $this->db->query("DELETE FROM users where id = ".$id);
if($delete)
return 1;
}
function upload_file(){
extract($_FILES['file']);
// var_dump($_FILES);
if($tmp_name != ''){
$fname = strtotime(date('y-m-d H:i')).'_'.$name;
$move = move_uploaded_file($tmp_name,'assets/uploads/'. $fname);
}
if(isset($move) && $move){
return json_encode(array("status"=>1,"fname"=>$fname));
}
}
function remove_file(){
extract($_POST);
if(is_file('assets/uploads/'.$fname))
unlink('assets/uploads/'.$fname);
return 1;
}
function delete_file(){
extract($_POST);
$doc = $this->db->query("SELECT * FROM documents where id= $id")->fetch_array();
$delete = $this->db->query("DELETE FROM documents where id = ".$id);
if($delete){
foreach(json_decode($doc['file_json']) as $k => $v){
if(is_file('assets/uploads/'.$v))
unlink('assets/uploads/'.$v);
}
return 1;
}
}
function save_upload(){
extract($_POST);
// var_dump($_FILES);
$data = " title ='$title' ";
$data .= ", description ='".htmlentities(str_replace("'","’",$description))."' ";
$data .= ", user_id ='{$_SESSION['login_id']}' ";
$data .= ", file_json ='".json_encode($fname)."' ";
if(empty($id)){
$save = $this->db->query("INSERT INTO documents set $data ");
}else{
$save = $this->db->query("UPDATE documents set $data where id = $id");
}
if($save){
return 1;
}
}
}