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