├── application ├── views │ ├── error │ │ └── error.phtml │ └── index │ │ └── index.phtml ├── library │ ├── Common │ │ ├── Util.php │ │ ├── Logger │ │ │ └── Helper.php │ │ ├── Template.php │ │ └── Request │ │ │ └── Helper.php │ └── Db │ │ ├── Factory.php │ │ ├── DbInterface.php │ │ └── Mysql.php ├── models │ ├── BaseModel.php │ └── IndexModel.php ├── controllers │ ├── Index.php │ ├── Error.php │ └── Base.php └── Bootstrap.php ├── log ├── yaf.log └── Yaf_Log ├── conf ├── application.ini └── auto.php ├── README.md └── index.php /application/views/error/error.phtml: -------------------------------------------------------------------------------- 1 | 2 | error occured 3 | -------------------------------------------------------------------------------- /log/yaf.log: -------------------------------------------------------------------------------- 1 | [2012-05-24 01:12:41]516--Could not find controller script /var/www/ata/application/controllers/Sdfsdf.php 2 | -------------------------------------------------------------------------------- /application/views/index/index.phtml: -------------------------------------------------------------------------------- 1 | 4 | 5 |
6 |';
27 | echo 'Connection failed: ' . $e->getMessage();
28 | die();
29 | }
30 | }
31 |
32 | static public function getInstance($db_config = '') {
33 |
34 | $_db_host = $db_config->host;
35 | $_db_name = $db_config->dbname;
36 | $_db_charset = $db_config->charset;
37 | $_db_usr = $db_config->usr;
38 | $_db_pwd = $db_config->pwd;
39 |
40 | $idx = md5($_db_host . $_db_name);
41 |
42 | if (!isset(self::$_instances[$idx])) {
43 | self::$_instances[$idx] = new Mysql($_db_host, $_db_usr, $_db_pwd, $_db_name, $_db_charset);
44 | }
45 | return self::$_instances[$idx];
46 | }
47 |
48 | function halt($msg = '', $sql = '') {
49 |
50 | $error_info = $this->_sth->errorInfo();
51 | $s = '';
52 | $s .= 'Error:' . $error_info[2] . '
';
53 | $s .= 'Errno:' . $error_info[1] . '
';
54 | $s .= 'Sql:' . $this->_sql;
55 | exit($s);
56 | }
57 |
58 | function execute($sql, $values = array()) {
59 | $this->_sql = $sql;
60 | $this->_sth = $this->_dbh->prepare($sql);
61 | $bool = $this->_sth->execute($values);
62 |
63 | if ('00000' !== $this->_sth->errorCode()) {
64 | $this->halt();
65 | }
66 |
67 | return $bool;
68 | }
69 |
70 | function getAll($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) {
71 | $this->execute($sql, $values);
72 | return $this->_sth->fetchAll($fetch_style);
73 | }
74 |
75 | function getCol($sql, $params = array(), $column_number = 0) {
76 | $columns = array();
77 | $results = array();
78 | $this->execute($sql, $params);
79 | $results = $this->_sth->fetchAll(\PDO::FETCH_NUM);
80 | foreach ($results as $result) {
81 | $columns[] = $result[$column_number];
82 | }
83 | return $columns;
84 | }
85 |
86 | function getRow($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) {
87 | $this->execute($sql, $values);
88 | return $this->_sth->fetch($fetch_style);
89 | }
90 |
91 | function getOne($sql, $values = array(), $column_number = 0) {
92 | $this->execute($sql, $values);
93 | return $this->_sth->fetchColumn($column_number);
94 | }
95 |
96 | function insert($table = null, $data = null, $returnStr = false) {
97 |
98 | $fields = array_keys($data);
99 | $marks = array_fill(0, count($fields), '?');
100 |
101 | $sql = "INSERT INTO $table (`" . implode('`,`', $fields) . "`) VALUES (" . implode(", ", $marks) . " )";
102 | if ($returnStr) {
103 | $fields = array_keys($data);
104 | $marks = array_values($data);
105 |
106 | foreach ($marks as $k => $v) {
107 | if (!is_numeric($v))
108 | $marks[$k] = '\'' . $v . '\'';
109 | }
110 | $sql = "INSERT INTO $table (`" . implode('`,`', $fields) . "`) VALUES (" . implode(", ", $marks) . " )";
111 | return $sql;
112 | }
113 | $this->execute($sql, array_values($data));
114 | $last_insert_id = $this->_dbh->lastInsertId();
115 | if ($last_insert_id)
116 | return $last_insert_id;
117 | else
118 | return true;
119 | }
120 |
121 | /*
122 | * 处理事务
123 | */
124 |
125 | function transaction($sql) {
126 | try {
127 | $this->_dbh->beginTransaction();
128 | $this->_dbh->exec($sql);
129 | $this->_dbh->commit();
130 | } catch (PDOException $ex) {
131 | $this->_dbh->rollBack();
132 | }
133 | }
134 |
135 | function update($table, $data, $where) {
136 |
137 | $values = $bits = $wheres = array();
138 | foreach ($data as $k => $v) {
139 | $bits[] = "`$k` = ?";
140 | $values[] = $v;
141 | }
142 |
143 | foreach ($where as $c => $v) {
144 | $wheres[] = "$c = ?";
145 | $values[] = $v;
146 | }
147 |
148 | $sql = "UPDATE $table SET " . implode(', ', $bits) . ' WHERE ' . implode(' AND ', $wheres);
149 | return $this->execute($sql, $values);
150 | }
151 |
152 | function delete($table, $where) {
153 | $values = $wheres = array();
154 | foreach ($where as $key => $val) {
155 | $wheres[] = "$key = ?";
156 | $values[] = $val;
157 | }
158 |
159 | $sql = "DELETE FROM $table WHERE " . implode(' AND ', $wheres);
160 | return $this->execute($sql, $values);
161 | }
162 |
163 | function close() {
164 | unset($this->_instances);
165 | unset($this->_dbh);
166 | }
167 |
168 | }
--------------------------------------------------------------------------------
/application/library/Common/Request/Helper.php:
--------------------------------------------------------------------------------
1 | cookies array ready for the next request
30 | // Note: This currently ignores the cookie path (and time) completely. Time is not important,
31 | // but path could possibly lead to security problems.
32 | var $persist_referers = true; // For each request, sends path of last request as referer
33 | var $debug = false;
34 | var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found
35 | var $max_redirects = 5;
36 | var $headers_only = false; // If true, stops receiving once headers have been read.
37 | // Basic authorization variables
38 | var $username;
39 | var $password;
40 | // Response vars
41 | var $status;
42 | var $headers = array();
43 | var $content = '';
44 | var $errormsg;
45 | // Tracker variables
46 | var $redirect_count = 0;
47 | var $cookie_host = '';
48 |
49 | function __construct($host, $port = 80) {
50 | $this->host = $host;
51 | $this->port = $port;
52 | }
53 |
54 | function get($path, $data = false) {
55 | $this->path = $path;
56 | $this->method = 'GET';
57 | if ($data) {
58 | $this->path .= '?' . $this->buildQueryString($data);
59 | }
60 | return $this->doRequest();
61 | }
62 |
63 | function post($path, $data) {
64 | $this->path = $path;
65 | $this->method = 'POST';
66 | $this->postdata = $this->buildQueryString($data);
67 | return $this->doRequest();
68 | }
69 |
70 | function buildQueryString($data) {
71 | $querystring = '';
72 | if (is_array($data)) {
73 | // Change data in to postable data
74 | foreach ($data as $key => $val) {
75 | if (is_array($val)) {
76 | foreach ($val as $val2) {
77 | $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';
78 | }
79 | } else {
80 | $querystring .= urlencode($key) . '=' . urlencode($val) . '&';
81 | }
82 | }
83 | $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
84 | } else {
85 | $querystring = $data;
86 | }
87 | return $querystring;
88 | }
89 |
90 | function doRequest() {
91 | // Performs the actual HTTP request, returning true or false depending on outcome
92 | if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
93 | // Set error message
94 | switch ($errno) {
95 | case -3:
96 | $this->errormsg = 'Socket creation failed (-3)';
97 | case -4:
98 | $this->errormsg = 'DNS lookup failure (-4)';
99 | case -5:
100 | $this->errormsg = 'Connection refused or timed out (-5)';
101 | default:
102 | $this->errormsg = 'Connection failed (' . $errno . ')';
103 | $this->errormsg .= ' ' . $errstr;
104 | $this->debug($this->errormsg);
105 | }
106 | return false;
107 | }
108 | socket_set_timeout($fp, $this->timeout);
109 | $request = $this->buildRequest();
110 | $this->debug('Request', $request);
111 | fwrite($fp, $request);
112 | // Reset all the variables that should not persist between requests
113 | $this->headers = array();
114 | $this->content = '';
115 | $this->errormsg = '';
116 | // Set a couple of flags
117 | $inHeaders = true;
118 | $atStart = true;
119 | // Now start reading back the response
120 | while (!feof($fp)) {
121 | $line = fgets($fp, 4096);
122 | if ($atStart) {
123 | // Deal with first line of returned data
124 | $atStart = false;
125 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
126 | $this->errormsg = "Status code line invalid: " . htmlentities($line);
127 | $this->debug($this->errormsg);
128 | return false;
129 | }
130 | $http_version = $m[1]; // not used
131 | $this->status = $m[2];
132 | $status_string = $m[3]; // not used
133 | $this->debug(trim($line));
134 | continue;
135 | }
136 | if ($inHeaders) {
137 | if (trim($line) == '') {
138 | $inHeaders = false;
139 | $this->debug('Received Headers', $this->headers);
140 | if ($this->headers_only) {
141 | break; // Skip the rest of the input
142 | }
143 | continue;
144 | }
145 | if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
146 | // Skip to the next header
147 | continue;
148 | }
149 | $key = strtolower(trim($m[1]));
150 | $val = trim($m[2]);
151 | // Deal with the possibility of multiple headers of same name
152 | if (isset($this->headers[$key])) {
153 | if (is_array($this->headers[$key])) {
154 | $this->headers[$key][] = $val;
155 | } else {
156 | $this->headers[$key] = array($this->headers[$key], $val);
157 | }
158 | } else {
159 | $this->headers[$key] = $val;
160 | }
161 | continue;
162 | }
163 | // We're not in the headers, so append the line to the contents
164 | $this->content .= $line;
165 | }
166 | fclose($fp);
167 | // If data is compressed, uncompress it
168 | if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
169 | $this->debug('Content is gzip encoded, unzipping it');
170 | $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php
171 | $this->content = gzinflate($this->content);
172 | }
173 | // If $persist_cookies, deal with any cookies
174 | if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
175 | $cookies = $this->headers['set-cookie'];
176 | if (!is_array($cookies)) {
177 | $cookies = array($cookies);
178 | }
179 | foreach ($cookies as $cookie) {
180 | if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
181 | $this->cookies[$m[1]] = $m[2];
182 | }
183 | }
184 | // Record domain of cookies for security reasons
185 | $this->cookie_host = $this->host;
186 | }
187 | // If $persist_referers, set the referer ready for the next request
188 | if ($this->persist_referers) {
189 | $this->debug('Persisting referer: ' . $this->getRequestURL());
190 | $this->referer = $this->getRequestURL();
191 | }
192 | // Finally, if handle_redirects and a redirect is sent, do that
193 | if ($this->handle_redirects) {
194 | if (++$this->redirect_count >= $this->max_redirects) {
195 | $this->errormsg = 'Number of redirects exceeded maximum (' . $this->max_redirects . ')';
196 | $this->debug($this->errormsg);
197 | $this->redirect_count = 0;
198 | return false;
199 | }
200 | $location = isset($this->headers['location']) ? $this->headers['location'] : '';
201 | $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
202 | if ($location || $uri) {
203 | $url = parse_url($location . $uri);
204 | // This will FAIL if redirect is to a different site
205 | return $this->get($url['path']);
206 | }
207 | }
208 | return true;
209 | }
210 |
211 | function buildRequest() {
212 | $headers = array();
213 | $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding
214 | $headers[] = "Host: {$this->host}";
215 | $headers[] = "User-Agent: {$this->user_agent}";
216 | $headers[] = "Accept: {$this->accept}";
217 | if ($this->use_gzip) {
218 | $headers[] = "Accept-encoding: {$this->accept_encoding}";
219 | }
220 | $headers[] = "Accept-language: {$this->accept_language}";
221 | if ($this->referer) {
222 | $headers[] = "Referer: {$this->referer}";
223 | }
224 | // Cookies
225 | if ($this->cookies) {
226 | $cookie = 'Cookie: ';
227 | foreach ($this->cookies as $key => $value) {
228 | $cookie .= "$key=$value; ";
229 | }
230 | $headers[] = $cookie;
231 | }
232 | // Basic authentication
233 | if ($this->username && $this->password) {
234 | $headers[] = 'Authorization: BASIC ' . base64_encode($this->username . ':' . $this->password);
235 | }
236 | // If this is a POST, set the content type and length
237 | if ($this->postdata) {
238 | $headers[] = 'Content-Type: application/x-www-form-urlencoded';
239 | $headers[] = 'Content-Length: ' . strlen($this->postdata);
240 | }
241 | $request = implode("\r\n", $headers) . "\r\n\r\n" . $this->postdata;
242 | return $request;
243 | }
244 |
245 | function getStatus() {
246 | return $this->status;
247 | }
248 |
249 | function getContent() {
250 | return $this->content;
251 | }
252 |
253 | function getHeaders() {
254 | return $this->headers;
255 | }
256 |
257 | function getHeader($header) {
258 | $header = strtolower($header);
259 | if (isset($this->headers[$header])) {
260 | return $this->headers[$header];
261 | } else {
262 | return false;
263 | }
264 | }
265 |
266 | function getError() {
267 | return $this->errormsg;
268 | }
269 |
270 | function getCookies() {
271 | return $this->cookies;
272 | }
273 |
274 | function getRequestURL() {
275 | $url = 'http://' . $this->host;
276 | if ($this->port != 80) {
277 | $url .= ':' . $this->port;
278 | }
279 | $url .= $this->path;
280 | return $url;
281 | }
282 |
283 | // Setter methods
284 | function setUserAgent($string) {
285 | $this->user_agent = $string;
286 | }
287 |
288 | function setAuthorization($username, $password) {
289 | $this->username = $username;
290 | $this->password = $password;
291 | }
292 |
293 | function setCookies($array) {
294 | $this->cookies = $array;
295 | }
296 |
297 | // Option setting methods
298 | function useGzip($boolean) {
299 | $this->use_gzip = $boolean;
300 | }
301 |
302 | function setPersistCookies($boolean) {
303 | $this->persist_cookies = $boolean;
304 | }
305 |
306 | function setPersistReferers($boolean) {
307 | $this->persist_referers = $boolean;
308 | }
309 |
310 | function setHandleRedirects($boolean) {
311 | $this->handle_redirects = $boolean;
312 | }
313 |
314 | function setMaxRedirects($num) {
315 | $this->max_redirects = $num;
316 | }
317 |
318 | function setHeadersOnly($boolean) {
319 | $this->headers_only = $boolean;
320 | }
321 |
322 | function setDebug($boolean) {
323 | $this->debug = $boolean;
324 | }
325 |
326 | // "Quick" static methods
327 | function quickGet($url) {
328 | $bits = parse_url($url);
329 | $host = $bits['host'];
330 | $port = isset($bits['port']) ? $bits['port'] : 80;
331 | $path = isset($bits['path']) ? $bits['path'] : '/';
332 | if (isset($bits['query'])) {
333 | $path .= '?' . $bits['query'];
334 | }
335 | $client = new HttpClient($host, $port);
336 | if (!$client->get($path)) {
337 | return false;
338 | } else {
339 | return $client->getContent();
340 | }
341 | }
342 |
343 | static function quickPost($url, $data) {
344 | $bits = parse_url($url);
345 | $host = $bits['host'];
346 | $port = isset($bits['port']) ? $bits['port'] : 80;
347 | $path = isset($bits['path']) ? $bits['path'] : '/';
348 | $client = new self($host, $port);
349 | if (!$client->post($path, $data)) {
350 | return false;
351 | } else {
352 | return $client->getContent();
353 | }
354 | }
355 |
356 | function debug($msg, $object = false) {
357 | if ($this->debug) {
358 | print 'HttpClient Debug: ' . $msg;
359 | if ($object) {
360 | ob_start();
361 | print_r($object);
362 | $content = htmlentities(ob_get_contents());
363 | ob_end_clean();
364 | print '' . $content . '
';
365 | }
366 | print '';
367 | }
368 | }
369 |
370 | }
371 |
372 | ?>
--------------------------------------------------------------------------------