1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | FacebookPlug - a Piwigo Plugin | |
---|
4 | // | Copyright (C) 2010-2011 Ruben ARNAUD - rub@piwigo.org | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | This program is free software; you can redistribute it and/or modify | |
---|
7 | // | it under the terms of the GNU General Public License as published by | |
---|
8 | // | the Free Software Foundation | |
---|
9 | // | | |
---|
10 | // | This program is distributed in the hope that it will be useful, but | |
---|
11 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | // | General Public License for more details. | |
---|
14 | // | | |
---|
15 | // | You should have received a copy of the GNU General Public License | |
---|
16 | // | along with this program; if not, write to the Free Software | |
---|
17 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | // | USA. | |
---|
19 | // +-----------------------------------------------------------------------+ |
---|
20 | |
---|
21 | defined('FACEBOOKPLUG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR); |
---|
22 | |
---|
23 | function fbp_mkdir($dir) |
---|
24 | { |
---|
25 | if ( !is_dir($dir) ) |
---|
26 | { |
---|
27 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
28 | { |
---|
29 | $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); |
---|
30 | } |
---|
31 | $umask = umask(0); |
---|
32 | $mkd = @mkdir($dir, 0755, true); |
---|
33 | umask($umask); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | // Copy from Piwigo application |
---|
38 | // get_extension returns the part of the string after the last "." |
---|
39 | function fbp_get_extension( $filename ) |
---|
40 | { |
---|
41 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
42 | } |
---|
43 | |
---|
44 | // Copy from Piwigo application |
---|
45 | function fbp_url_is_remote($url) |
---|
46 | { |
---|
47 | if ( strncmp($url, 'http://', 7)==0 |
---|
48 | or strncmp($url, 'https://', 8)==0 ) |
---|
49 | { |
---|
50 | return true; |
---|
51 | } |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Retrieve data from external URL |
---|
57 | * Copy from Piwigo application |
---|
58 | * |
---|
59 | * @param string $src: URL |
---|
60 | * @param global $dest: can be a file ressource or string |
---|
61 | * @return bool |
---|
62 | */ |
---|
63 | function fbp_fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0) |
---|
64 | { |
---|
65 | // Try to retrieve data from local file? |
---|
66 | if (!fbp_url_is_remote($src)) |
---|
67 | { |
---|
68 | $content = @file_get_contents($src); |
---|
69 | if ($content !== false) |
---|
70 | { |
---|
71 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
72 | return true; |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | return false; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | // After 3 redirections, return false |
---|
81 | if ($step > 3) return false; |
---|
82 | |
---|
83 | // Initialize $dest |
---|
84 | is_resource($dest) or $dest = ''; |
---|
85 | |
---|
86 | // Try curl to read remote file |
---|
87 | if (function_exists('curl_init')) |
---|
88 | { |
---|
89 | $ch = @curl_init(); |
---|
90 | @curl_setopt($ch, CURLOPT_URL, $src); |
---|
91 | @curl_setopt($ch, CURLOPT_HEADER, 1); |
---|
92 | @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); |
---|
93 | @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
94 | $content = @curl_exec($ch); |
---|
95 | $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
---|
96 | $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
---|
97 | @curl_close($ch); |
---|
98 | if ($content !== false and $status >= 200 and $status < 400) |
---|
99 | { |
---|
100 | if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m)) |
---|
101 | { |
---|
102 | return fbp_fetchRemote($m[1], $dest, $user_agent, $step+1); |
---|
103 | } |
---|
104 | $content = substr($content, $header_length); |
---|
105 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
106 | return true; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | // Try file_get_contents to read remote file |
---|
111 | if (ini_get('allow_url_fopen')) |
---|
112 | { |
---|
113 | $content = @file_get_contents($src); |
---|
114 | if ($content !== false) |
---|
115 | { |
---|
116 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
117 | return true; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | // Try fsockopen to read remote file |
---|
122 | $src = parse_url($src); |
---|
123 | $host = $src['host']; |
---|
124 | $path = isset($src['path']) ? $src['path'] : '/'; |
---|
125 | $path .= isset($src['query']) ? '?'.$src['query'] : ''; |
---|
126 | |
---|
127 | if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false) |
---|
128 | { |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | fwrite($s, |
---|
133 | "GET ".$path." HTTP/1.0\r\n" |
---|
134 | ."Host: ".$host."\r\n" |
---|
135 | ."User-Agent: ".$user_agent."\r\n" |
---|
136 | ."Accept: */*\r\n" |
---|
137 | ."\r\n" |
---|
138 | ); |
---|
139 | |
---|
140 | $i = 0; |
---|
141 | $in_content = false; |
---|
142 | while (!feof($s)) |
---|
143 | { |
---|
144 | $line = fgets($s); |
---|
145 | |
---|
146 | if (rtrim($line,"\r\n") == '' && !$in_content) |
---|
147 | { |
---|
148 | $in_content = true; |
---|
149 | $i++; |
---|
150 | continue; |
---|
151 | } |
---|
152 | if ($i == 0) |
---|
153 | { |
---|
154 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m)) |
---|
155 | { |
---|
156 | fclose($s); |
---|
157 | return false; |
---|
158 | } |
---|
159 | $status = (integer) $m[2]; |
---|
160 | if ($status < 200 || $status >= 400) |
---|
161 | { |
---|
162 | fclose($s); |
---|
163 | return false; |
---|
164 | } |
---|
165 | } |
---|
166 | if (!$in_content) |
---|
167 | { |
---|
168 | if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m)) |
---|
169 | { |
---|
170 | fclose($s); |
---|
171 | return fbp_fetchRemote(trim($m[1]),$dest,$user_agent,$step+1); |
---|
172 | } |
---|
173 | $i++; |
---|
174 | continue; |
---|
175 | } |
---|
176 | is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line; |
---|
177 | $i++; |
---|
178 | } |
---|
179 | fclose($s); |
---|
180 | return true; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Return basename of the current script |
---|
185 | * Copy from Piwigo application |
---|
186 | * Lower case convertion is applied on return value |
---|
187 | * Return value is without file extention ".php" |
---|
188 | * |
---|
189 | * @param void |
---|
190 | * |
---|
191 | * @return script basename |
---|
192 | */ |
---|
193 | function fbp_script_basename() |
---|
194 | { |
---|
195 | global $conf; |
---|
196 | |
---|
197 | foreach (array('SCRIPT_NAME', 'SCRIPT_FILENAME', 'PHP_SELF') as $value) |
---|
198 | { |
---|
199 | if (!empty($_SERVER[$value])) |
---|
200 | { |
---|
201 | $filename = strtolower($_SERVER[$value]); |
---|
202 | $basename = basename($filename, '.php'); |
---|
203 | if (!empty($basename)) |
---|
204 | { |
---|
205 | return $basename; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | return ''; |
---|
210 | } |
---|
211 | |
---|
212 | function fbp_db_query($query) |
---|
213 | { |
---|
214 | $result = mysql_query($query); |
---|
215 | if (!$result) |
---|
216 | { |
---|
217 | die('Invalid query: [mysql error '.mysql_errno().'] '.mysql_error()."\n".$query); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | function fbp_db_log() |
---|
222 | { |
---|
223 | $link = @mysql_connect(FBP_HOST, FBP_USER, FBP_PASSWORD); |
---|
224 | if (!$link) |
---|
225 | { |
---|
226 | die("Can't connect to server"); |
---|
227 | } |
---|
228 | if (!mysql_select_db(FBP_BASE, $link)) |
---|
229 | { |
---|
230 | die('Connection to server succeed, but it was impossible to connect to database'); |
---|
231 | } |
---|
232 | |
---|
233 | fbp_db_query('set names "utf8"'); |
---|
234 | |
---|
235 | fbp_db_query(" |
---|
236 | insert into |
---|
237 | FBP_HISTORY |
---|
238 | ( |
---|
239 | VERSION, |
---|
240 | SCRIPT_NAME, |
---|
241 | IP, |
---|
242 | IMAGE_URL, |
---|
243 | PICTURE_URL, |
---|
244 | GALLERY_TITLE, |
---|
245 | PICTURE_TITLE |
---|
246 | ) |
---|
247 | values |
---|
248 | ( |
---|
249 | '".mysql_real_escape_string(FBP_VERSION)."', |
---|
250 | '".mysql_real_escape_string(fbp_script_basename())."', |
---|
251 | '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', |
---|
252 | '".mysql_real_escape_string(@$_GET['u'])."', |
---|
253 | '".mysql_real_escape_string(@$_GET['pu'])."', |
---|
254 | '".mysql_real_escape_string(@$_GET['gt'])."', |
---|
255 | '".mysql_real_escape_string(@$_GET['pt'])."' |
---|
256 | ); |
---|
257 | "); |
---|
258 | } |
---|
259 | |
---|
260 | ?> |
---|