1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | $current_dir = realpath(dirname(__FILE__)); |
---|
25 | require_once($current_dir . "/../module/DB.php"); |
---|
26 | |
---|
27 | $objDbConn = ""; |
---|
28 | |
---|
29 | class SC_DbConn{ |
---|
30 | |
---|
31 | var $conn; |
---|
32 | var $result; |
---|
33 | var $includePath; |
---|
34 | var $error_mail_to; |
---|
35 | var $error_mail_title; |
---|
36 | var $dsn; |
---|
37 | var $err_disp = true; |
---|
38 | var $dbFactory; |
---|
39 | |
---|
40 | |
---|
41 | // コンストラクタ |
---|
42 | function SC_DbConn($dsn = "", $err_disp = true, $new = false){ |
---|
43 | global $objDbConn; |
---|
44 | |
---|
45 | // Debugモード指定 |
---|
46 | $options['debug'] = PEAR_DB_DEBUG; |
---|
47 | // 持続的接続オプション |
---|
48 | $options['persistent'] = PEAR_DB_PERSISTENT; |
---|
49 | |
---|
50 | // 既に接続されていないか、新規接続要望の場合は接続する。 |
---|
51 | if(!isset($objDbConn->connection) || $new) { |
---|
52 | if($dsn != "") { |
---|
53 | $objDbConn = DB::connect($dsn, $options); |
---|
54 | $this->dsn = $dsn; |
---|
55 | } else { |
---|
56 | if(defined('DEFAULT_DSN')) { |
---|
57 | $objDbConn = DB::connect(DEFAULT_DSN, $options); |
---|
58 | $this->dsn = DEFAULT_DSN; |
---|
59 | } else { |
---|
60 | return; |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | if (DB_TYPE == 'mysql') { |
---|
66 | $objDbConn->query('SET NAMES utf8'); |
---|
67 | } |
---|
68 | |
---|
69 | $this->conn = $objDbConn; |
---|
70 | $this->error_mail_to = DB_ERROR_MAIL_TO; |
---|
71 | $this->error_mail_title = DB_ERROR_MAIL_SUBJECT; |
---|
72 | $this->err_disp = $err_disp; |
---|
73 | $this->dbFactory = SC_DB_DBFactory_Ex::getInstance(); |
---|
74 | } |
---|
75 | |
---|
76 | // クエリの実行 |
---|
77 | function query($n ,$arr = "", $ignore_err = false){ |
---|
78 | // mysqlの場合にはビュー表を変換する |
---|
79 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
80 | |
---|
81 | if ( $arr ) { |
---|
82 | $result = $this->conn->query($n, $arr); |
---|
83 | } else { |
---|
84 | $result = $this->conn->query($n); |
---|
85 | } |
---|
86 | |
---|
87 | if ($this->conn->isError($result) && !$ignore_err){ |
---|
88 | $this->send_err_mail ($result, $n); |
---|
89 | } |
---|
90 | |
---|
91 | $this->result = $result; |
---|
92 | return $this->result; |
---|
93 | } |
---|
94 | |
---|
95 | // 一件のみ取得 |
---|
96 | function getOne($n, $arr = ""){ |
---|
97 | |
---|
98 | // mysqlの場合にはビュー表を変換する |
---|
99 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
100 | |
---|
101 | if ( $arr ) { |
---|
102 | $result = $this->conn->getOne($n, $arr); |
---|
103 | } else { |
---|
104 | $result = $this->conn->getOne($n); |
---|
105 | } |
---|
106 | if ($this->conn->isError($result)){ |
---|
107 | $this->send_err_mail ($result ,$n); |
---|
108 | } |
---|
109 | $this->result = $result; |
---|
110 | |
---|
111 | return $this->result; |
---|
112 | } |
---|
113 | |
---|
114 | function getRow($n, $arr = ""){ |
---|
115 | |
---|
116 | // mysqlの場合にはビュー表を変換する |
---|
117 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
118 | |
---|
119 | if ( $arr ) { |
---|
120 | $result = $this->conn->getRow($n, $arr); |
---|
121 | } else { |
---|
122 | $result = $this->conn->getRow($n); |
---|
123 | } |
---|
124 | if ($this->conn->isError($result)){ |
---|
125 | $this->send_err_mail ($result ,$n); |
---|
126 | } |
---|
127 | $this->result = $result; |
---|
128 | return $this->result; |
---|
129 | } |
---|
130 | |
---|
131 | function getCol($n, $col, $arr = "") { |
---|
132 | |
---|
133 | // mysqlの場合にはビュー表を変換する |
---|
134 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
135 | |
---|
136 | if ($arr) { |
---|
137 | $result = $this->conn->getCol($n, $col, $arr); |
---|
138 | } else { |
---|
139 | $result = $this->conn->getCol($n, $col); |
---|
140 | } |
---|
141 | if ($this->conn->isError($result)) { |
---|
142 | $this->send_err_mail($result, $n); |
---|
143 | } |
---|
144 | $this->result = $result; |
---|
145 | return $this->result; |
---|
146 | } |
---|
147 | |
---|
148 | // SELECT文の実行結果を全て取得 |
---|
149 | function getAll($n, $arr = ""){ |
---|
150 | |
---|
151 | // mysqlの場合にはビュー表を変換する |
---|
152 | if (DB_TYPE == "mysql") $n = $this->dbFactory->sfChangeMySQL($n); |
---|
153 | |
---|
154 | if(PEAR::isError($this->conn)) { |
---|
155 | if(ADMIN_MODE){ |
---|
156 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:" . $this->dsn); |
---|
157 | }else{ |
---|
158 | SC_Utils_Ex::sfErrorHeader("DBへの接続に失敗しました。:"); |
---|
159 | } |
---|
160 | return 0; |
---|
161 | } |
---|
162 | |
---|
163 | if ( $arr ){ |
---|
164 | $result = $this->conn->getAll($n, $arr, DB_FETCHMODE_ASSOC); |
---|
165 | } else { |
---|
166 | $result = $this->conn->getAll($n, DB_FETCHMODE_ASSOC); |
---|
167 | } |
---|
168 | |
---|
169 | if ($this->conn->isError($result)){ |
---|
170 | $this->send_err_mail ($result, $n); |
---|
171 | } |
---|
172 | $this->result = $result; |
---|
173 | |
---|
174 | return $this->result; |
---|
175 | } |
---|
176 | |
---|
177 | function autoExecute($table_name, $fields_values, $sql_where = null){ |
---|
178 | |
---|
179 | if ( $sql_where ) { |
---|
180 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_UPDATE, $sql_where); |
---|
181 | } else { |
---|
182 | $result = $this->conn->autoExecute( $table_name, $fields_values, DB_AUTOQUERY_INSERT); |
---|
183 | } |
---|
184 | |
---|
185 | if ($this->conn->isError($result)){ |
---|
186 | $this->send_err_mail ($result, $n); |
---|
187 | } |
---|
188 | $this->result = $result; |
---|
189 | return $this->result; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | function prepare($n){ |
---|
194 | global $sql; |
---|
195 | $sql = $n; |
---|
196 | $result = $this->conn->prepare($n); |
---|
197 | $this->result = $result; |
---|
198 | return $this->result; |
---|
199 | } |
---|
200 | |
---|
201 | function execute($n, $obj){ |
---|
202 | global $sql; |
---|
203 | $sql = $n; |
---|
204 | $result = $this->conn->execute($n, $obj); |
---|
205 | $this->result = $result; |
---|
206 | return $this->result; |
---|
207 | } |
---|
208 | |
---|
209 | function reset(){ |
---|
210 | $this->conn->disconnect(); |
---|
211 | } |
---|
212 | |
---|
213 | function send_err_mail($result, $sql){ |
---|
214 | $url = ''; |
---|
215 | $errmsg = ''; |
---|
216 | |
---|
217 | if (SC_Utils_Ex::sfIsHTTPS()) { |
---|
218 | $url = "https://"; |
---|
219 | } else { |
---|
220 | $url = "http://"; |
---|
221 | } |
---|
222 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
---|
223 | |
---|
224 | $errmsg = $url."\n\n"; |
---|
225 | $errmsg.= "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . "\n"; |
---|
226 | $errmsg.= "REMOTE_ADDR:" . $_SERVER['REMOTE_ADDR'] . "\n"; |
---|
227 | $errmsg.= "USER_AGENT:" . $_SERVER['HTTP_USER_AGENT'] . "\n\n"; |
---|
228 | $errmsg.= $sql . "\n"; |
---|
229 | $errmsg.= $result->message . "\n\n"; |
---|
230 | $errmsg.= $result->userinfo . "\n\n"; |
---|
231 | |
---|
232 | $arrRbacktrace = array_reverse($result->backtrace); |
---|
233 | |
---|
234 | foreach($arrRbacktrace as $backtrace) { |
---|
235 | if($backtrace['class'] != "") { |
---|
236 | $func = $backtrace['class'] . "->" . $backtrace['function']; |
---|
237 | } else { |
---|
238 | $func = $backtrace['function']; |
---|
239 | } |
---|
240 | |
---|
241 | $errmsg.= $backtrace['file'] . " " . $backtrace['line'] . ":" . $func . "\n"; |
---|
242 | } |
---|
243 | |
---|
244 | require_once(CLASS_EX_PATH . "page_extends/error/LC_Page_Error_SystemError_Ex.php"); |
---|
245 | |
---|
246 | $objPage = new LC_Page_Error_SystemError_Ex(); |
---|
247 | register_shutdown_function(array($objPage, "destroy")); |
---|
248 | $objPage->init(); |
---|
249 | $objPage->process(); |
---|
250 | |
---|
251 | if (DEBUG_MODE == true) { |
---|
252 | print('<pre>'); |
---|
253 | print_r(htmlspecialchars($errmsg, ENT_QUOTES, CHAR_CODE)); |
---|
254 | print('</pre>'); |
---|
255 | } |
---|
256 | |
---|
257 | GC_Utils_Ex::gfPrintLog($errmsg); |
---|
258 | |
---|
259 | exit(); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * 直前に実行されたSQL文を取得する. |
---|
264 | * |
---|
265 | * @param boolean $disp trueの場合、画面出力を行う. |
---|
266 | * @return string SQL文 |
---|
267 | */ |
---|
268 | function getLastQuery($disp = true) { |
---|
269 | $sql = $this->conn->last_query; |
---|
270 | if($disp) { |
---|
271 | print($sql.";<br />\n"); |
---|
272 | } |
---|
273 | return $sql; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | ?> |
---|