This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
util.php
152 lines (127 loc) · 4.51 KB
/
util.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
<?php
require __DIR__ . '/../sendgrid-php/sendgrid-php.php';
$key = '';
function sendEmail($emails, $title, $message, $key) {
/* $attachment = new SendGrid\Attachment();
$file_encoded;
$filename = array_filter($_FILES['file']['name']);
if(!empty($filename)) {
$attachment->setType("application/text");
$attachment->setFilename($filename);
$file_encoded = base64_encode(file_get_contents($_FILES['file']['tmp_name']));
$attachment->setContent($file_encoded);
$attachment->setDisposition("attachment");
}*/
foreach($emails as $email) {
$mail = new \SendGrid\Mail\Mail();
$mail->setFrom("donotreply@YOUR_SENDGRID_DOMAIN", "COMPANY NAME");
$mail->setSubject($title);
// $mail->addAttachment($attachment);
$mail->addTo($email, $email);
$mail->addContent("text/plain", $message);
$sendgrid = new \SendGrid($key);
try {
$sendgrid->send($mail);
} catch (Exception $e) {
}
}
}
function exportDB($host, $name, $user, $password) {
$db = new mysqli($host, $user, $password, $name);
$tables = array();
$result = $db->query("SHOW TABLES");
while($row = $result->fetch_row()) {
$tables[] = $row[0];
}
$return = '';
foreach($tables as $table){
$result = $db->query("SELECT * FROM $table");
$numColumns = $result->field_count;
$result2 = $db->query("SHOW CREATE TABLE $table");
$row2 = $result2->fetch_row();
$return .= "\n\n".$row2[1].";\n\n";
for($i = 0; $i < $numColumns; $i++) {
while($row = $result->fetch_row()) {
$return .= "INSERT INTO $table VALUES(";
for($j=0; $j < $numColumns; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = $row[$j];
if (isset($row[$j])) {
$return .= '"'.$row[$j].'"' ;
} else {
$return .= '""';
}
if ($j < ($numColumns-1)) {
$return.= ',';
}
}
$return .= ");\n";
}
}
$return .= "\n\n\n";
}
$filename = time() . '.sql';
$handle = fopen($filename, 'w');
fwrite($handle, $return);
fclose($handle);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '. filesize($filename));
header('Content-Type: application/sql');
ob_clean();
flush();
readfile($filename);
}
function importDB($pdo) {
$sql = file_get_contents($_FILES['file']['tmp_name']);
$pdo->query($sql);
}
function uploadImages() {
// File upload configuration
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$paths = array();
$fileNames = array_filter($_FILES['files']['name']);
if(!empty($fileNames)){
foreach($_FILES['files']['name'] as $key=>$val){
// File upload path
$file = explode(".", $_FILES["files"]["name"][$key]);
$fileName = md5(microtime(true)) . '.' . end($file);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)) {
$imageTemp = $_FILES["files"]["tmp_name"][$key];
$imageUploadPath = $targetDir . $fileName;
$paths[] = compressImage($imageTemp, $imageUploadPath, 50);
}
}
}
return $paths;
}
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}