source: extensions/flickr2piwigo/include/functions.inc.php @ 17312

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

I use the same function in three plugins

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