source: extensions/Google2Piwigo/include/functions.inc.php @ 26198

Last change on this file since 26198 was 26198, checked in by mistic100, 10 years ago

update for Piwigo 2.6

File size: 2.4 KB
Line 
1<?php
2defined('PICASA_WA_PATH') or die('Hacking attempt!');
3
4/**
5 * test if a download method is available
6 * @return: bool
7 */
8if (!function_exists('test_remote_download'))
9{
10  function test_remote_download()
11  {
12    return function_exists('curl_init') || ini_get('allow_url_fopen');
13  }
14}
15
16/**
17 * download a remote file (special version for Google2Piwigo)
18 *  - needs cURL or allow_url_fopen
19 *  - take care of SSL urls
20 *
21 * @param: string source url
22 * @param: mixed destination file (if true, file content is returned)
23 */
24function picasa_wa_download_remote_file($src, $dest, $headers=array())
25{
26  if (empty($src))
27  {
28    return false;
29  }
30 
31  $return = ($dest === true) ? true : false;
32 
33  array_push($headers, 'Accept-language: en');
34 
35  /* curl */
36  if (function_exists('curl_init'))
37  {
38    if (!$return)
39    {
40      $newf = fopen($dest, "wb");
41    }
42    $ch = curl_init();
43   
44    curl_setopt($ch, CURLOPT_URL, $src);
45    curl_setopt($ch, CURLOPT_HEADER, false);
46    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
47    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
48    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
49    if (!ini_get('safe_mode'))
50    {
51      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
52      curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
53    }
54    if (strpos($src, 'https://') !== false)
55    {
56      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
57      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
58    }
59    if (!$return)
60    {
61      curl_setopt($ch, CURLOPT_FILE, $newf);
62    }
63    else
64    {
65      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
66    }
67   
68    $out = curl_exec($ch);
69    curl_close($ch);
70   
71    if ($out === false)
72    {
73      return 'file_error';
74    }
75    else if (!$return)
76    {
77      fclose($newf);
78      return true;
79    }
80    else
81    {
82      return $out;
83    }
84  }
85  /* file get content */
86  else if (ini_get('allow_url_fopen'))
87  {
88    $src = preg_replace('#^https#', 'http', $src);
89   
90    $opts = array(
91      'http' => array(
92        'method' => "GET",
93        'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
94        'header' => implode("\r\n", $headers),
95      )
96    );
97
98    $context = stream_context_create($opts);
99   
100    if (($file = file_get_contents($src, false, $context)) === false)
101    {
102      return 'file_error';
103    }
104   
105    if (!$return)
106    {
107      file_put_contents($dest, $file);
108      return true;
109    }
110    else
111    {
112      return $file;
113    }
114  }
115 
116  return false;
117}
Note: See TracBrowser for help on using the repository browser.