DoSql.php
7.15 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
<?php
define('EmpireCMSAdmin','1');
require("../../class/connect.php");
require("../../class/db_sql.php");
require("../../class/functions.php");
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
//驗證用戶
$lur=is_login();
$logininid=$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=$lur['groupid'];
$loginadminstyleid=$lur['adminstyleid'];
//ehash
$ecms_hashur=hReturnEcmsHashStrAll();
//驗證權限
CheckLevel($logininid,$loginin,$classid,"execsql");
//執行SQL語句
function DoExecSql($add,$userid,$username){
global $empire,$dbtbpre;
$dosave=(int)$add['dosave'];
$query=$add['query'];
if(!$query)
{
printerror("EmptyDoSqlQuery","history.go(-1)");
}
if($dosave==1&&!$add['sqlname'])
{
printerror("EmptySqltext","history.go(-1)");
}
$query=ClearAddsData($query);
//保存
if($dosave==1)
{
$add['sqlname']=hRepPostStr($add['sqlname'],1);
$isql=$empire->query("insert into {$dbtbpre}enewssql(sqlname,sqltext) values('".$add['sqlname']."','".addslashes($query)."');");
}
$query=RepSqlTbpre($query);
DoRunQuery($query);
//操作日誌
insert_dolog("query=".$query);
printerror("DoExecSqlSuccess","DoSql.php".hReturnEcmsHashStrHref2(1));
}
//運行SQL
function DoRunQuery($sql){
global $empire;
$sql=str_replace("\r","\n",$sql);
$ret=array();
$num=0;
foreach(explode(";\n",trim($sql)) as $query)
{
$queries=explode("\n",trim($query));
foreach($queries as $query)
{
$ret[$num].=$query[0]=='#'||$query[0].$query[1]=='--'?'':$query;
}
$num++;
}
unset($sql);
foreach($ret as $query)
{
$query=trim($query);
if($query)
{
$empire->query($query);
}
}
}
//增加SQL語句
function AddSql($add,$userid,$username){
global $empire,$dbtbpre;
if(!$add['sqlname']||!$add['sqltext'])
{
printerror("EmptySqltext","history.go(-1)");
}
$add['sqlname']=hRepPostStr($add['sqlname'],1);
$add[sqltext]=ClearAddsData($add[sqltext]);
$sql=$empire->query("insert into {$dbtbpre}enewssql(sqlname,sqltext) values('".$add['sqlname']."','".addslashes($add[sqltext])."');");
$lastid=$empire->lastid();
if($sql)
{
//操作日誌
insert_dolog("id=".$lastid."<br>sqlname=".$add[sqlname]);
printerror("AddSqlSuccess","AddSql.php?enews=AddSql".hReturnEcmsHashStrHref2(0));
}
else
{
printerror("DbError","history.go(-1)");
}
}
//修改SQL語句
function EditSql($add,$userid,$username){
global $empire,$dbtbpre;
$id=(int)$add[id];
if(!$add['sqlname']||!$add['sqltext']||!$id)
{
printerror("EmptySqltext","history.go(-1)");
}
$add['sqlname']=hRepPostStr($add['sqlname'],1);
$add[sqltext]=ClearAddsData($add[sqltext]);
$sql=$empire->query("update {$dbtbpre}enewssql set sqlname='".$add['sqlname']."',sqltext='".addslashes($add[sqltext])."' where id='$id'");
if($sql)
{
//操作日誌
insert_dolog("id=".$id."<br>sqlname=".$add[sqlname]);
printerror("EditSqlSuccess","ListSql.php".hReturnEcmsHashStrHref2(1));
}
else
{
printerror("DbError","history.go(-1)");
}
}
//刪除SQL語句
function DelSql($id,$userid,$username){
global $empire,$dbtbpre;
$id=(int)$id;
if(!$id)
{
printerror("EmptySqlid","history.go(-1)");
}
$r=$empire->fetch1("select sqlname from {$dbtbpre}enewssql where id='$id'");
$sql=$empire->query("delete from {$dbtbpre}enewssql where id='$id'");
if($sql)
{
//操作日誌
insert_dolog("id=".$id."<br>sqlname=".$r[sqlname]);
printerror("DelSqlSuccess","ListSql.php".hReturnEcmsHashStrHref2(1));
}
else
{
printerror("DbError","history.go(-1)");
}
}
//運行SQL語句
function ExecSql($id,$userid,$username){
global $empire,$dbtbpre;
$id=(int)$id;
if(empty($id))
{
printerror('EmptyExecSqlid','');
}
$r=$empire->fetch1("select sqltext from {$dbtbpre}enewssql where id='$id'");
if(!$r['sqltext'])
{
printerror('EmptyExecSqlid','');
}
$query=RepSqlTbpre($r['sqltext']);
DoRunQuery($query);
//操作日誌
insert_dolog("query=".$query);
printerror("DoExecSqlSuccess","ListSql.php".hReturnEcmsHashStrHref2(1));
}
$enews=$_POST['enews'];
if(empty($enews))
{$enews=$_GET['enews'];}
if($enews)
{
hCheckEcmsRHash();
}
//執行SQL語句
if($enews=='DoExecSql')
{
DoExecSql($_POST,$logininid,$loginin);
}
elseif($enews=='AddSql')//增加
{
AddSql($_POST,$logininid,$loginin);
}
elseif($enews=='EditSql')//修改
{
EditSql($_POST,$logininid,$loginin);
}
elseif($enews=='DelSql')//刪除
{
DelSql($_GET['id'],$logininid,$loginin);
}
elseif($enews=='ExecSql')//執行
{
ExecSql($_GET['id'],$logininid,$loginin);
}
$url="<a href=DoSql.php".$ecms_hashur['whehref'].">執行SQL語句</a>";
db_close();
$empire=null;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>執行SQL語句</title>
<link href="../adminstyle/<?=$loginadminstyleid?>/adminstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td height="25">位置:
<?=$url?>
</td>
<td width="50%"><div align="right" class="emenubutton">
<input type="button" name="Submit5" value="增加SQL語句" onclick="self.location.href='AddSql.php?enews=AddSql<?=$ecms_hashur['ehref']?>';">
<input type="button" name="Submit4" value="管理SQL語句" onclick="self.location.href='ListSql.php<?=$ecms_hashur['whehref']?>';">
</div></td>
</tr>
</table>
<form action="DoSql.php" method="POST" name="sqlform" onsubmit="return confirm('確認要執行?');">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<?=$ecms_hashur['form']?>
<tr class="header">
<td height="25"><div align="center">執行SQL語句</div></td>
</tr>
<tr>
<td height="25" bgcolor="#FFFFFF"><div align="center">(多條語句請用"回車"格開,每條語句以";"結束,數據表前綴可用:「[!db.pre!]"表示)</div></td>
</tr>
<tr>
<td height="25" bgcolor="#FFFFFF"><div align="center">
<textarea name="query" cols="90" rows="12" id="query"></textarea>
</div></td>
</tr>
<tr>
<td height="25" bgcolor="#FFFFFF"><div align="center">
<input type="submit" name="Submit" value=" 執行SQL">
<input type="reset" name="Submit2" value="重置">
<input name="enews" type="hidden" id="enews" value="DoExecSql" onclick="document.sqlform.dosave.value=0;">
<input name="dosave" type="hidden" id="dosave" value="0">
</div></td>
</tr>
<tr>
<td height="25" bgcolor="#FFFFFF"><div align="center">SQL名稱:
<input name="sqlname" type="text" id="sqlname">
<input type="submit" name="Submit3" value="執行SQL並保存" onclick="document.sqlform.dosave.value=1;">
</div></td>
</tr>
<tr>
<td height="25" bgcolor="#FFFFFF"><div align="center">此功能影響到整個系統的數據,請慎用.</div></td>
</tr>
</table>
</form>
</body>
</html>