PluginProbe
Import Social Events / 1.9.0
Import Social Events v1.9.0
1.9.1 1.9.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 All 61 releases
import-facebook-events / includes / lib / icalcreator / src / Util / HttpFactory.php

HttpFactory.php in Import Social Events 1.9.0, at includes/lib/icalcreator/src/Util/HttpFactory.php

214 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile
3 /**
4 * iCalcreator, the PHP class package managing iCal (rfc2445/rfc5445) calendar information.
5 *
6 * copyright (c) 2007-2021 Kjell-Inge Gustafsson, kigkonsult, All rights reserved
7 * Link https://kigkonsult.se
8 * Package iCalcreator
9 * Version 2.30.3
10 * License Subject matter of licence is the software iCalcreator.
11 * The above copyright, link, package and version notices,
12 * this licence notice and the invariant [rfc5545] PRODID result use
13 * as implemented and invoked in iCalcreator shall be included in
14 * all copies or substantial portions of the iCalcreator.
15 *
16 * iCalcreator is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published
18 * by the Free Software Foundation, either version 3 of the License,
19 * or (at your option) any later version.
20 *
21 * iCalcreator is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with iCalcreator. If not, see <https://www.gnu.org/licenses/>.
28 *
29 * This file is a part of iCalcreator.
30 */
31
32 namespace Kigkonsult\Icalcreator\Util;
33
34 use Exception;
35 use InvalidArgumentException;
36 use Kigkonsult\Icalcreator\Vcalendar;
37
38 use function clearstatcache;
39 use function file_put_contents;
40 use function filesize;
41 use function filter_var;
42 use function gzencode;
43 use function header;
44 use function sprintf;
45 use function strcasecmp;
46 use function strlen;
47 use function strpos;
48 use function substr;
49 use function sys_get_temp_dir;
50 use function tempnam;
51 use function unlink;
52 use function utf8_encode;
53
54 /**
55 * iCalcreator http support class
56 *
57 * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
58 * @since 2.30.3 - 2021-02-14
59 */
60 class HttpFactory
61 {
62 /**
63 * HTTP headers
64 *
65 * @var array $headers
66 * @access private
67 * @static
68 */
69 private static $headers = [
70 'Content-Encoding: gzip',
71 'Vary: *',
72 'Content-Length: %s',
73 'Content-Type: text/calendar; charset=utf-8',
74 'Content-Disposition: attachment; filename="%s"',
75 'Content-Disposition: inline; filename="%s"',
76 'Cache-Control: max-age=10',
77 ];
78
79 /**
80 * Return created, updated and/or parsed calendar, sending a HTTP redirect header.
81 *
82 * @param Vcalendar $calendar
83 * @param bool $utf8Encode
84 * @param bool $gzip
85 * @param bool $cdType true : Content-Disposition: attachment... (default), false : ...inline...
86 * @param string $fileName
87 * @return bool true on success, false on error
88 * @throws Exception
89 * @static
90 * @since 2.29.15 - 2020-01-19
91 */
92 public static function returnCalendar(
93 Vcalendar $calendar,
94 $utf8Encode = false,
95 $gzip = false,
96 $cdType = true,
97 $fileName = null
98 ) {
99 static $ICR = 'iCr';
100 $utf8Encode ?: false;
101 $gzip ?: false;
102 $cdType ?: false;
103 if( empty( $fileName ) ) {
104 $fileName = self::getFakedFilename();
105 }
106 $output = $calendar->createCalendar();
107 if( $utf8Encode ) {
108 $output = utf8_encode( $output ); // phpcs:ignore wp_function_not_compatible_with_requires_wp
109 }
110 $fsize = null;
111 if( $gzip ) {
112 $output = gzencode( $output, 9 );
113 $fsize = strlen( $output );
114 header( self::$headers[0] );
115 header( self::$headers[1] );
116 }
117 else {
118 if( false !== ( $temp = tempnam( sys_get_temp_dir(), $ICR ))) {
119 if( false !== file_put_contents( $temp, $output )) {
120 $fsize = @filesize( $temp );
121 }
122 unlink( $temp );
123 clearstatcache();
124 }
125 } // end else
126 if( ! empty( $fsize )) {
127 header( sprintf( self::$headers[2], $fsize ));
128 }
129 header( self::$headers[3] );
130 $cdType = ( $cdType ) ? 4 : 5;
131 header( sprintf( self::$headers[$cdType], $fileName ));
132 header( self::$headers[6] );
133 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 return true;
135 }
136
137 /**
138 * Return faked filename
139 *
140 * @return string $propName
141 * @access private
142 * @static
143 * @since 2.29.4 - 2019-07-02
144 */
145 private static function getFakedFilename()
146 {
147 static $DOTICS = '.ics';
148 return gmdate(
149 DateTimeFactory::$YmdHis,
150 intval( microtime( true ))
151 ) . $DOTICS;
152 }
153
154 /**
155 * Assert URL
156 *
157 * @param string $url
158 * @throws InvalidArgumentException
159 * @static
160 * @since 2.27.3 - 2018-12-28
161 */
162 public static function assertUrl( $url )
163 {
164 static $UC = '_';
165 static $URN = 'urn';
166 static $HTTP = 'http://';
167 static $MSG = 'URL validity error #%d, \'%s\'';
168 $url2 = ( false !== strpos( $url, $UC ))
169 ? str_replace( $UC, Util::$MINUS, $url )
170 : $url;
171 $no = 0;
172 do {
173 if( false !== filter_var( $url2, FILTER_VALIDATE_URL )) {
174 break;
175 }
176 if( empty( parse_url( $url2, PHP_URL_SCHEME)) &&
177 ( false !== filter_var( $HTTP . $url2, FILTER_VALIDATE_URL ))) {
178 break;
179 }
180 $no = 1;
181 if( 0 != strcasecmp( $URN, substr( $url, 0, 3 ))) {
182 $no = 2;
183 }
184 break;
185 } while( true );
186 // if( ! empty( $no )) {
187 // throw new InvalidArgumentException( sprintf( $MSG, $no, $url ));
188 // }
189 }
190
191 /**
192 * Set calendar component property uri; URL, TZURL, SOURCE
193 *
194 * @param array $valueProp
195 * @param string $value
196 * @param array $params
197 * @return void
198 * @throws InvalidArgumentException
199 * @since 2.30.3 - 2021-02-14
200 */
201 public static function urlSet( & $valueProp, $value = null, $params = [] )
202 {
203 if( ! empty( $value )) {
204 StringFactory::checkFixUriValue( $value );
205 self::assertUrl( $value );
206 }
207 ParameterFactory::ifExistRemove( $params, Vcalendar::VALUE, Vcalendar::URI );
208 $valueProp = [
209 Util::$LCvalue => $value,
210 Util::$LCparams => ParameterFactory::setParams( $params ),
211 ];
212 }
213 }
214