This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_common.php
208 lines (184 loc) · 6.4 KB
/
function_common.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
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
<?php
//获取到表名
function tname($name) {
global $_SC;
return $_SC['tablepre'].$name;
}
//添加数据
function inserttable($tablename, $insertsqlarr, $returnid=0, $replace = false, $silent=0) {
global $_SGLOBAL;
$insertkeysql = $insertvaluesql = $comma = '';
foreach ($insertsqlarr as $insert_key => $insert_value) {
$insertkeysql .= $comma.'`'.$insert_key.'`';
$insertvaluesql .= $comma.'\''.$insert_value.'\'';
$comma = ', ';
}
$method = $replace?'REPLACE':'INSERT';
$_SGLOBAL['db']->query($method.' INTO '.tname($tablename).' ('.$insertkeysql.') VALUES ('.$insertvaluesql.')', $silent?'SILENT':'');
if($returnid && !$replace) {
return $_SGLOBAL['db']->insert_id();
}
}
//更新数据
function updatetable($tablename, $setsqlarr, $wheresqlarr, $silent=0) {
global $_SGLOBAL;
$setsql = $comma = '';
foreach ($setsqlarr as $set_key => $set_value) {//fix
$setsql .= $comma.'`'.$set_key.'`'.'=\''.$set_value.'\'';
$comma = ', ';
}
$where = $comma = '';
if(empty($wheresqlarr)) {
$where = '1';
} elseif(is_array($wheresqlarr)) {
foreach ($wheresqlarr as $key => $value) {
$where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
$comma = ' AND ';
}
} else {
$where = $wheresqlarr;
}
$_SGLOBAL['db']->query('UPDATE '.tname($tablename).' SET '.$setsql.' WHERE '.$where, $silent?'SILENT':'');
}
//写运行日志
function runlog($log, $file='wechat', $halt=0) {
global $_SGLOBAL, $_SERVER;
$nowurl = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
$log = gmdate('Y-m-d H:i:s', $_SGLOBAL['timestamp'])."\t$type\t"."\t{$nowurl}\t".str_replace(array("\r", "\n"), array(' ', ' '), trim($log))."\n";
$yearmonth = gmdate('Ym', $_SGLOBAL['timestamp']);
$logdir = './data/log/';
if(!is_dir($logdir)) mkdir($logdir, 0777);
$logfile = $logdir.$yearmonth.'_'.$file.'.php';
if(@filesize($logfile) > 2048000) {
$dir = opendir($logdir);
$length = strlen($file);
$maxid = $id = 0;
while($entry = readdir($dir)) {
if(strexists($entry, $yearmonth.'_'.$file)) {
$id = intval(substr($entry, $length + 8, -4));
$id > $maxid && $maxid = $id;
}
}
closedir($dir);
$logfilebak = $logdir.$yearmonth.'_'.$file.'_'.($maxid + 1).'.php';
@rename($logfile, $logfilebak);
}
if($fp = @fopen($logfile, 'a')) {
@flock($fp, 2);
fwrite($fp, "<?PHP exit;?>\t".str_replace(array('<?', '?>', "\r", "\n"), '', $log)."\n");
fclose($fp);
}
if($halt) exit();
}
function getbxinfo($revdata){
$revdataarr = explode(' ', $revdata);
if(count($revdataarr)<=1) return '';
for($i=count($revdataarr)-1;$i>=0;$i--){
if(trim($revdataarr[$i])==''){
unset($revdataarr[$i]);
}
}
$revdataarr=array_values($revdataarr);
var_dump($revdataarr);
$bx=array();
if(count($revdataarr)>=8){
$bx['stunum']=strlen($revdataarr[1])==11?(ctype_digit($revdataarr[1])?$revdataarr[1]:''):'';
if(empty($bx['stunum'])) return '学号输入有误。';
$bx['stuname']=$revdataarr[2];
$bx['sushe']=mb_substr($revdataarr[3],0,2,'UTF-8')=='东莞'?$revdataarr[3]:'';
if(empty($bx['sushe'])) return '宿舍输入有误。';
$bx['fangjian']=ctype_digit($revdataarr[4])?(strlen($revdataarr[4])==3?$revdataarr[4]:''):'';
if(empty($bx['fangjian'])) return '房间号输入有误。';
$bx['port']=substr(strtoupper($revdataarr[5]),0,1);
$bx['port']=in_array($bx['port'],array('A','B','C','D'))?$bx['port']:'';
if(empty($bx['port'])) return '端口号输入有误。';
$phonelen=strlen($bx['phone']=$revdataarr[6]);
if($phonelen>11&&$phonele<=18){
$phonearr = explode('/', $bx['phone']);
$bx['phone']=strlen($phonearr[0])==11?(ctype_digit($phonearr[0])?$phonearr[0]:''):'';
$bx['phone2']=strlen($phonearr[1])<=6?(ctype_digit($phonearr[1])?$phonearr[1]:''):'';
if(empty($bx['phone2'])) return '联系电话输入有误。';
}
elseif($phonelen==11){
$bx['phone']=ctype_digit($bx['phone'])?$bx['phone']:'';
}
if(empty($bx['phone'])) return '联系电话输入有误。';
$bx['info']=$revdataarr[7];
return $bx;
}
return '报修信息不完整或输入信息没有足够的空格分割。';
}
function sendemail($mailbody,$subject){
global $_SC;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.163.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = $_SC['username'];
//Password to use for SMTP authentication
$mail->Password = $_SC['password'];
//Set who the message is to be sent from
$mail->setFrom($_SC['fromaddr'], $_SC['fromname']);
//Set an alternative reply-to address
$mail->addReplyTo($_SC['replyaddr'], $_SC['replyname']);
//Set who the message is to be sent to
$mail->addAddress($_SC['sendaddr'], $_SC['sendname']);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->Body = $mailbody;
$mail->AltBody =$mailbody;
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
//runlog("Mailer Error: " . $mail->ErrorInfo,'mail');
return false;
} else {
//echo "Message sent!";
return true;
}
}
function curl($url, $data=array(), $timeout = 30)
{
$ssl = substr($url, 0, 5) == "https" ? TRUE : FALSE;
$ch = curl_init();
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => $timeout,
);
if(!empty($data)){
$opt[CURLOPT_POST ] = 1;
$opt[CURLOPT_POSTFIELDS ] = $data;
}
if ($ssl)
{
//$opt[CURLOPT_SSL_VERIFYHOST] = 1;
$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
curl_setopt_array($ch, $opt);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>