source: extensions/FacebookPlug/Server/include/functions.php @ 8200

Last change on this file since 8200 was 8200, checked in by rub, 13 years ago

First part of upload picture

  • Property svn:eol-style set to LF
File size: 5.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | FacebookPlug - a Piwigo Plugin                                        |
4// | Copyright (C) 2010 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
21function fbp_mkdir($dir)
22{
23  if ( !is_dir($dir) )
24  {
25    if (substr(PHP_OS, 0, 3) == 'WIN')
26    {
27      $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
28    }
29    $umask = umask(0);
30    $mkd = @mkdir($dir, 0755, true);
31    umask($umask);
32  }
33}
34
35// Copy from Piwigo application
36// get_extension returns the part of the string after the last "."
37function fbp_get_extension( $filename )
38{
39  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
40}
41
42// Copy from Piwigo application
43function fbp_url_is_remote($url)
44{
45  if ( strncmp($url, 'http://', 7)==0
46    or strncmp($url, 'https://', 8)==0 )
47  {
48    return true;
49  }
50  return false;
51}
52
53/**
54 * Retrieve data from external URL
55 * Copy from Piwigo application
56 *
57 * @param string $src: URL
58 * @param global $dest: can be a file ressource or string
59 * @return bool
60 */
61function fbp_fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0)
62{
63  // Try to retrieve data from local file?
64  if (!fbp_url_is_remote($src))
65  {
66    $content = @file_get_contents($src);
67    if ($content !== false)
68    {
69      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
70      return true;
71    }
72    else
73    {
74      return false;
75    }
76  }
77
78  // After 3 redirections, return false
79  if ($step > 3) return false;
80
81  // Initialize $dest
82  is_resource($dest) or $dest = '';
83
84  // Try curl to read remote file
85  if (function_exists('curl_init'))
86  {
87    $ch = @curl_init();
88    @curl_setopt($ch, CURLOPT_URL, $src);
89    @curl_setopt($ch, CURLOPT_HEADER, 1);
90    @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
91    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
92    $content = @curl_exec($ch);
93    $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
94    $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
95    @curl_close($ch);
96    if ($content !== false and $status >= 200 and $status < 400)
97    {
98      if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
99      {
100        return fbp_fetchRemote($m[1], $dest, $user_agent, $step+1);
101      }
102      $content = substr($content, $header_length);
103      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
104      return true;
105    }
106  }
107
108  // Try file_get_contents to read remote file
109  if (ini_get('allow_url_fopen'))
110  {
111    $content = @file_get_contents($src);
112    if ($content !== false)
113    {
114      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
115      return true;
116    }
117  }
118
119  // Try fsockopen to read remote file
120  $src = parse_url($src);
121  $host = $src['host'];
122  $path = isset($src['path']) ? $src['path'] : '/';
123  $path .= isset($src['query']) ? '?'.$src['query'] : '';
124
125  if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false)
126  {
127    return false;
128  }
129
130  fwrite($s,
131    "GET ".$path." HTTP/1.0\r\n"
132    ."Host: ".$host."\r\n"
133    ."User-Agent: ".$user_agent."\r\n"
134    ."Accept: */*\r\n"
135    ."\r\n"
136  );
137
138  $i = 0;
139  $in_content = false;
140  while (!feof($s))
141  {
142    $line = fgets($s);
143
144    if (rtrim($line,"\r\n") == '' && !$in_content)
145    {
146      $in_content = true;
147      $i++;
148      continue;
149    }
150    if ($i == 0)
151    {
152      if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m))
153      {
154        fclose($s);
155        return false;
156      }
157      $status = (integer) $m[2];
158      if ($status < 200 || $status >= 400)
159      {
160        fclose($s);
161        return false;
162      }
163    }
164    if (!$in_content)
165    {
166      if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m))
167      {
168        fclose($s);
169        return fbp_fetchRemote(trim($m[1]),$dest,$user_agent,$step+1);
170      }
171      $i++;
172      continue;
173    }
174    is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line;
175    $i++;
176  }
177  fclose($s);
178  return true;
179}
180
181?>
Note: See TracBrowser for help on using the repository browser.