PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / ical / reader / http.php
vikappointments / site / helpers / libraries / ical / reader Last commit date
file.php 3 days ago http.php 3 days ago index.html 3 days ago optimizer.php 3 days ago
http.php
81 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Implementor used to extract an iCalendar from remote URL.
16 *
17 * @since 1.7.3
18 */
19 class VAPIcalReaderHttp implements VAPIcalReader
20 {
21 /**
22 * The remote URL.
23 *
24 * @var string
25 */
26 protected $url;
27
28 /**
29 * The request headers.
30 *
31 * @var array
32 */
33 protected $headers;
34
35 /**
36 * Class constructor.
37 *
38 * @param string $url
39 * @param array $headers
40 */
41 public function __construct($url, array $headers = [])
42 {
43 $this->url = $url;
44
45 /**
46 * Include a user agent to prevent a redirect with Outlook calendar.
47 *
48 * @since 1.7.8
49 */
50 $this->headers = array_merge([
51 'User-Agent' => 'E4J\\VikAppointments ' . VIKAPPOINTMENTS_SOFTWARE_VERSION,
52 ], $headers);
53 }
54
55 /**
56 * Extracts the iCalendar buffer from a remote host.
57 *
58 * @return string The iCalendar string.
59 */
60 public function load()
61 {
62 $http = new JHttp();
63
64 // replace WEBCAL scheme with HTTPS, because cURL may not support it
65 $url = preg_replace("/^webcal:\/\//", 'https://', $this->url);
66
67 // make GET request
68 $response = $http->get($url, $this->headers);
69
70 if ($response->code != 200)
71 {
72 // fetch error from body (ignore HTML tags)
73 $error = strip_tags($response->body);
74
75 throw new Exception($error ? $error : 'Error', $response->code);
76 }
77
78 return $response->body;
79 }
80 }
81