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

Last change on this file since 17475 was 17475, checked in by mistic100, 12 years ago

new extension: Google2Piwigo

File size: 2.4 KB
Line 
1<?php
2if (!defined('PICASA_WA_PATH')) 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)
25{
26  if (empty($src))
27  {
28    return false;
29  }
30 
31  $return = ($dest === true) ? true : false;
32 
33  /* curl */
34  if (function_exists('curl_init'))
35  {
36    if (!$return)
37    {
38      $newf = fopen($dest, "wb");
39    }
40    $ch = curl_init();
41   
42    curl_setopt($ch, CURLOPT_URL, $src);
43    curl_setopt($ch, CURLOPT_HEADER, false);
44    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en"));
45    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
46    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
47    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
48    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
49    if (strpos($src, 'https://') !== false)
50    {
51      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
52      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
53    }
54    if (!$return)
55    {
56      curl_setopt($ch, CURLOPT_FILE, $newf);
57    }
58    else
59    {
60      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
61    }
62   
63    $out = curl_exec($ch);
64    curl_close($ch);
65   
66    if ($out === false)
67    {
68      return 'file_error';
69    }
70    else if (!$return)
71    {
72      fclose($newf);
73      return true;
74    }
75    else
76    {
77      return $out;
78    }
79  }
80  /* file get content */
81  else if (ini_get('allow_url_fopen'))
82  {
83    $src = preg_replace('#^https#', 'http', $src);
84   
85    $opts = array(
86      'http' => array(
87        'method' => "GET",
88        'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
89        'header' => "Accept-language: en",
90      )
91    );
92
93    $context = stream_context_create($opts);
94   
95    if (($file = file_get_contents($src, false, $context)) === false)
96    {
97      return 'file_error';
98    }
99   
100    if (!$return)
101    {
102      file_put_contents($dest, $file);
103      return true;
104    }
105    else
106    {
107      return $file;
108    }
109  }
110 
111  return false;
112}
113
114?>
Note: See TracBrowser for help on using the repository browser.