1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage dispatcher
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * A download adapter using URL fopen() wrappers
13 */
14 class FOFDownloadAdapterFopen extends FOFDownloadAdapterAbstract implements FOFDownloadInterface
15 {
16 public function __construct()
17 {
18 $this->priority = 100;
19 $this->supportsFileSize = false;
20 $this->supportsChunkDownload = true;
21 $this->name = 'fopen';
22
23 // If we are not allowed to use ini_get, we assume that URL fopen is
24 // disabled.
25 if (!function_exists('ini_get'))
26 {
27 $this->isSupported = false;
28 }
29 else
30 {
31 $this->isSupported = ini_get('allow_url_fopen');
32 }
33 }
34
35 /**
36 * Download a part (or the whole) of a remote URL and return the downloaded
37 * data. You are supposed to check the size of the returned data. If it's
38 * smaller than what you expected you've reached end of file. If it's empty
39 * you have tried reading past EOF. If it's larger than what you expected
40 * the server doesn't support chunk downloads.
41 *
42 * If this class' supportsChunkDownload returns false you should assume
43 * that the $from and $to parameters will be ignored.
44 *
45 * @param string $url The remote file's URL
46 * @param integer $from Byte range to start downloading from. Use null for start of file.
47 * @param integer $to Byte range to stop downloading. Use null to download the entire file ($from is ignored)
48 * @param array $params Additional params that will be added before performing the download
49 *
50 * @return string The raw file data retrieved from the remote URL.
51 *
52 * @throws Exception A generic exception is thrown on error
53 */
54 public function downloadAndReturn($url, $from = null, $to = null, array $params = array())
55 {
56 if (empty($from))
57 {
58 $from = 0;
59 }
60
61 if (empty($to))
62 {
63 $to = 0;
64 }
65
66 if ($to < $from)
67 {
68 $temp = $to;
69 $to = $from;
70 $from = $temp;
71
72 unset($temp);
73 }
74
75 if (!(empty($from) && empty($to)))
76 {
77 $options = array(
78 'http' => array(
79 'method' => 'GET',
80 'header' => "Range: bytes=$from-$to\r\n"
81 ),
82 'ssl' => array(
83 'verify_peer' => true,
84 'cafile' => __DIR__ . '/cacert.pem',
85 'verify_depth' => 5,
86 )
87 );
88
89 $options = array_merge($options, $params);
90
91 $context = stream_context_create($options);
92 $result = @file_get_contents($url, false, $context, $from - $to + 1);
93 }
94 else
95 {
96 $options = array(
97 'http' => array(
98 'method' => 'GET',
99 ),
100 'ssl' => array(
101 'verify_peer' => true,
102 'cafile' => __DIR__ . '/cacert.pem',
103 'verify_depth' => 5,
104 )
105 );
106
107 $options = array_merge($options, $params);
108
109 $context = stream_context_create($options);
110 $result = @file_get_contents($url, false, $context);
111 }
112
113 if ($result === false)
114 {
115 $error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_HTTPERROR');
116 throw new Exception($error, 1);
117 }
118 else
119 {
120 return $result;
121 }
122 }
123 }