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

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

some code corrections

File size: 2.6 KB
Line 
1<?php
2if (!defined('FLICKR_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
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 */
24if (!function_exists('download_remote_file'))
25{
26  function download_remote_file($src, $dest)
27  {
28    if (empty($src))
29    {
30      return false;
31    }
32   
33    $return = ($dest === true) ? true : false;
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, array("Accept-language: en"));
47      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
48      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
49      curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
50      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
51      if (strpos($src, 'https://') !== false)
52      {
53        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
54        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
55      }
56      if (!$return)
57      {
58        curl_setopt($ch, CURLOPT_FILE, $newf);
59      }
60      else
61      {
62        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
63      }
64     
65      $out = curl_exec($ch);
66      curl_close($ch);
67     
68      if ($out === false)
69      {
70        return 'file_error';
71      }
72      else if (!$return)
73      {
74        fclose($newf);
75        return true;
76      }
77      else
78      {
79        return $out;
80      }
81    }
82    /* file get content */
83    else if (ini_get('allow_url_fopen'))
84    {
85      if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
86      {
87        return false;
88      }
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' => "Accept-language: en",
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  }
118}
119
120?>
Note: See TracBrowser for help on using the repository browser.