source: extensions/instagram2piwigo/include/functions.inc.php @ 26199

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

update for 2.6

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