aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/functions.php
diff options
context:
space:
mode:
authorpatdenice <patdenice@piwigo.org>2008-11-15 21:10:05 +0000
committerpatdenice <patdenice@piwigo.org>2008-11-15 21:10:05 +0000
commit59af44a8c2f4984e3d68321b5f4289833f8dddb1 (patch)
treed631241772ac50532f8329bbf1536110b857c7a7 /admin/include/functions.php
parent78edf1906c93af1bad409d4e7c59ab6921888f32 (diff)
- Add fetchRemote function which allow to retrieve datas over HTTP protocol using cURL method, file_get_contents function or fsockopen method. This allow to retrieve datas or files even if allow_url_fopen is deactivated.
- Use fetchRemote function in plugins manager and in latest version checking. - Add german translations for upgrade.lang.php. - Remove empty line at the end of pclzip.lib.php. - Change display of deactivated plugins after upgrade. git-svn-id: http://piwigo.org/svn/trunk@2880 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'admin/include/functions.php')
-rw-r--r--admin/include/functions.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/admin/include/functions.php b/admin/include/functions.php
index 658033e0c..e67925073 100644
--- a/admin/include/functions.php
+++ b/admin/include/functions.php
@@ -1922,4 +1922,112 @@ function cat_admin_access($category_id)
return true;
}
+/**
+ * Retrieve data from external URL
+ *
+ * @param string $src: URL
+ * @param global $dest: can be a file ressource or string
+ * @return bool
+ */
+function fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0)
+{
+ is_resource($dest) or $dest = '';
+
+ // Try curl to read remote file
+ if (function_exists('curl_init'))
+ {
+ $ch = @curl_init();
+ @curl_setopt($ch, CURLOPT_URL, $src);
+ @curl_setopt($ch, CURLOPT_HEADER, 0);
+ @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
+ is_resource($dest) ?
+ @curl_setopt($ch, CURLOPT_FILE, $dest):
+ @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $content = @curl_exec($ch);
+ @curl_close($ch);
+ if ($content !== false)
+ {
+ is_resource($dest) or $dest = $content;
+ return true;
+ }
+ }
+
+ // Try file_get_contents to read remote file
+ if (ini_get('allow_url_fopen'))
+ {
+ $content = @file_get_contents($src);
+ if ($content !== false)
+ {
+ is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
+ return true;
+ }
+ }
+
+ // Try fsockopen to read remote file
+ if ($step > 3)
+ {
+ return false;
+ }
+
+ $src = parse_url($src);
+ $host = $src['host'];
+ $path = isset($src['path']) ? $src['path'] : '/';
+ $path .= isset($src['query']) ? '?'.$src['query'] : '';
+
+ if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false)
+ {
+ return false;
+ }
+
+ fwrite($s,
+ "GET ".$path." HTTP/1.0\r\n"
+ ."Host: ".$host."\r\n"
+ ."User-Agent: ".$user_agent."\r\n"
+ ."Accept: */*\r\n"
+ ."\r\n"
+ );
+
+ $i = 0;
+ $in_content = false;
+ while (!feof($s))
+ {
+ $line = fgets($s);
+
+ if (rtrim($line,"\r\n") == '' && !$in_content)
+ {
+ $in_content = true;
+ $i++;
+ continue;
+ }
+ if ($i == 0)
+ {
+ if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m))
+ {
+ fclose($s);
+ return false;
+ }
+ $status = (integer) $m[2];
+ if ($status < 200 || $status >= 400)
+ {
+ fclose($s);
+ return false;
+ }
+ }
+ if (!$in_content)
+ {
+ if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m))
+ {
+ fclose($s);
+ return fetchRemote(trim($m[1]),$dest,$user_agent,$step+1);
+ }
+ $i++;
+ continue;
+ }
+ is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line;
+ $i++;
+ }
+ fclose($s);
+ return true;
+}
+
?> \ No newline at end of file