@@ -91,17 +91,20 @@ function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_
91
91
*/
92
92
mysqli_query ($ db , "
93
93
CREATE TABLE IF NOT EXISTS ` $ table` (
94
- `request` CHAR( 35 ) NOT NULL ,
95
- `response` MEDIUMTEXT NOT NULL ,
96
- `expiration` DATETIME NOT NULL ,
97
- INDEX ( `request` )
94
+ `request` varchar(128 ) NOT NULL,
95
+ `response` mediumtext NOT NULL,
96
+ `expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
97
+ UNIQUE KEY `request` (`request` )
98
98
)
99
99
" );
100
100
101
- $ result = mysqli_query ($ db , "SELECT COUNT(*) FROM $ table " );
102
- $ result = mysqli_fetch_row ($ result );
103
- if ( $ result [0 ] > $ this ->max_cache_rows ) {
104
- mysqli_query ($ db , "DELETE FROM $ table WHERE expiration < DATE_SUB(NOW(), INTERVAL $ cache_expire second) " );
101
+ $ result = mysqli_query ($ db , "SELECT COUNT(*) 'count' FROM $ table " );
102
+ if ( $ result ) {
103
+ $ result = mysqli_fetch_assoc ($ result );
104
+ }
105
+
106
+ if ( $ result && $ result ['count ' ] > $ this ->max_cache_rows ) {
107
+ mysqli_query ($ db , "DELETE FROM $ table WHERE CURRENT_TIMESTAMP > expiration " );
105
108
mysqli_query ($ db , 'OPTIMIZE TABLE ' . $ this ->cache_table );
106
109
}
107
110
$ this ->cache = 'db ' ;
@@ -132,6 +135,7 @@ function getCached ($request)
132
135
//Checks the database or filesystem for a cached result to the request.
133
136
//If there is no cache result, it returns a value of false. If it finds one,
134
137
//it returns the unparsed XML.
138
+ unset($ request ['api_sig ' ]);
135
139
foreach ( $ request as $ key => $ value ) {
136
140
if ( empty ($ value ) ) unset($ request [$ key ]);
137
141
else $ request [$ key ] = (string ) $ request [$ key ];
@@ -141,10 +145,10 @@ function getCached ($request)
141
145
$ this ->cache_key = $ reqhash ;
142
146
$ this ->cache_request = $ request ;
143
147
if ($ this ->cache == 'db ' ) {
144
- $ result = mysqli_query ($ this ->cache_db , "SELECT response FROM " . $ this ->cache_table . " WHERE request = ' " . $ reqhash . "' AND DATE_SUB(NOW(), INTERVAL " . ( int ) $ this -> cache_expire . " SECOND) < expiration " );
145
- if ( mysqli_num_rows ($ result ) ) {
148
+ $ result = mysqli_query ($ this ->cache_db , "SELECT response FROM " . $ this ->cache_table . " WHERE request = ' " . $ reqhash . "' AND CURRENT_TIMESTAMP < expiration " );
149
+ if ( $ result && mysqli_num_rows ($ result ) ) {
146
150
$ result = mysqli_fetch_assoc ($ result );
147
- return $ result ['response ' ];
151
+ return urldecode ( $ result ['response ' ]) ;
148
152
} else {
149
153
return false ;
150
154
}
@@ -174,15 +178,18 @@ function cache ($request, $response)
174
178
$ reqhash = md5 (serialize ($ request ));
175
179
if ($ this ->cache == 'db ' ) {
176
180
//$this->cache_db->query("DELETE FROM $this->cache_table WHERE request = '$reqhash'");
177
- $ result = mysqli_query ($ this ->cache_db , "SELECT COUNT(*) FROM " . $ this ->cache_table . " WHERE request = ' " . $ reqhash . "' " );
178
- $ result = mysqli_fetch_row ($ result );
179
- if ( $ result [0 ] ) {
180
- $ sql = "UPDATE " . $ this ->cache_table . " SET response = ' " . str_replace ("' " , "'' " , $ response ) . "', expiration = ' " . strftime ("%Y-%m-%d %H:%M:%S " ) . "' WHERE request = ' " . $ reqhash . "' " ;
181
- mysqli_query ($ this ->cache_db , $ sql );
182
- } else {
183
- $ sql = "INSERT INTO " . $ this ->cache_table . " (request, response, expiration) VALUES (' $ reqhash', ' " . str_replace ("' " , "'' " , $ response ) . "', ' " . strftime ("%Y-%m-%d %H:%M:%S " ) . "') " ;
184
- mysqli_query ($ this ->cache_db , $ sql );
181
+ $ response = urlencode ($ response );
182
+ $ sql = 'INSERT INTO ' .$ this ->cache_table .' (request, response, expiration)
183
+ VALUES ( \'' .$ reqhash .'\', \'' .$ response .'\', TIMESTAMPADD(SECOND, ' .$ this ->cache_expire .',CURRENT_TIMESTAMP))
184
+ ON DUPLICATE KEY UPDATE response= \'' .$ response .'\',
185
+ expiration=TIMESTAMPADD(SECOND, ' .$ this ->cache_expire .',CURRENT_TIMESTAMP) ' ;
186
+
187
+ $ result = mysqli_query ($ this ->cache_db , $ sql );
188
+ if (!$ result ) {
189
+ echo mysqli_error ($ this ->cache_db );
185
190
}
191
+
192
+ return $ result ;
186
193
} elseif ($ this ->cache == "fs " ) {
187
194
$ file = $ this ->cache_dir . "/ " . $ reqhash . ".cache " ;
188
195
$ fstream = fopen ($ file , "w " );
@@ -270,7 +277,7 @@ function request ($command, $args = array(), $nocache = false)
270
277
}
271
278
272
279
//Process arguments, including method and login data.
273
- $ args = array_merge (array ("method " => $ command , "format " => "php_serial " , "api_key " => $ this ->api_key ), $ args );
280
+ $ args = array_merge (array ("method " => $ command , "format " => "json " , " nojsoncallback " => " 1 " , "api_key " => $ this ->api_key ), $ args );
274
281
if (!empty ($ this ->token )) {
275
282
$ args = array_merge ($ args , array ("auth_token " => $ this ->token ));
276
283
} elseif (!empty ($ _SESSION ['phpFlickr_auth_token ' ])) {
@@ -279,7 +286,8 @@ function request ($command, $args = array(), $nocache = false)
279
286
ksort ($ args );
280
287
$ auth_sig = "" ;
281
288
$ this ->last_request = $ args ;
282
- if (!($ this ->response = $ this ->getCached ($ args )) || $ nocache ) {
289
+ $ this ->response = $ this ->getCached ($ args );
290
+ if (!($ this ->response ) || $ nocache ) {
283
291
foreach ($ args as $ key => $ data ) {
284
292
if ( is_null ($ data ) ) {
285
293
unset($ args [$ key ]);
@@ -295,13 +303,14 @@ function request ($command, $args = array(), $nocache = false)
295
303
$ this ->cache ($ args , $ this ->response );
296
304
}
297
305
306
+
298
307
/*
299
308
* Uncomment this line (and comment out the next one) if you're doing large queries
300
309
* and you're concerned about time. This will, however, change the structure of
301
310
* the result, so be sure that you look at the results.
302
311
*/
303
- // $this->parsed_response = unserialize ($this->response);
304
- $ this ->parsed_response = $ this ->clean_text_nodes (unserialize ($ this ->response ));
312
+ $ this ->parsed_response = json_decode ($ this ->response , TRUE );
313
+ /* $this->parsed_response = $this->clean_text_nodes(json_decode ($this->response, TRUE )); */
305
314
if ($ this ->parsed_response ['stat ' ] == 'fail ' ) {
306
315
if ($ this ->die_on_error ) die ("The Flickr API returned the following error: # {$ this ->parsed_response ['code ' ]} - {$ this ->parsed_response ['message ' ]}" );
307
316
else {
@@ -1088,8 +1097,8 @@ function photos_search ($args = array()) {
1088
1097
*/
1089
1098
1090
1099
/* https://www.flickr.com/services/api/flickr.photos.search.html */
1091
- $ this ->request ("flickr.photos.search " , $ args );
1092
- return $ this ->parsed_response ? $ this ->parsed_response ['photos ' ] : false ;
1100
+ $ result = $ this ->request ("flickr.photos.search " , $ args );
1101
+ return ( $ this ->parsed_response ) ? $ this ->parsed_response ['photos ' ] : false ;
1093
1102
}
1094
1103
1095
1104
function photos_setContentType ($ photo_id , $ content_type ) {
0 commit comments