| 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 |
| 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 Kigkonsult\Icalcreator\Vcalendar; |
| 34 |
|
| 35 |
use function array_change_key_case; |
| 36 |
use function array_filter; |
| 37 |
use function array_merge; |
| 38 |
use function ctype_digit; |
| 39 |
use function in_array; |
| 40 |
use function is_array; |
| 41 |
use function ksort; |
| 42 |
use function sprintf; |
| 43 |
use function strcasecmp; |
| 44 |
use function strpos; |
| 45 |
use function strtoupper; |
| 46 |
use function trim; |
| 47 |
|
| 48 |
/** |
| 49 |
* iCalcreator iCal parameters support class |
| 50 |
* |
| 51 |
* @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se> |
| 52 |
* @since 2.30.2 - 2021-02-04 |
| 53 |
*/ |
| 54 |
class ParameterFactory |
| 55 |
{ |
| 56 |
/** |
| 57 |
* Return formatted output for calendar component property parameters |
| 58 |
* |
| 59 |
* @param array $inputParams |
| 60 |
* @param array $ctrKeys |
| 61 |
* @param string $lang |
| 62 |
* @return string |
| 63 |
* @static |
| 64 |
* @since 2.29.25 - 2020-09-02 |
| 65 |
*/ |
| 66 |
public static function createParams( $inputParams = null, $ctrKeys = null, $lang = null ) |
| 67 |
{ |
| 68 |
static $FMTFMTTYPE = ';FMTTYPE=%s%s'; |
| 69 |
static $FMTKEQV = '%s=%s'; |
| 70 |
static $FMTQ = '"%s"'; |
| 71 |
static $FMTQTD = ';%s=%s%s%s'; |
| 72 |
static $FMTCMN = ';%s=%s'; |
| 73 |
static $DFKEYS = [ Vcalendar::DISPLAY, Vcalendar::FEATURE ]; |
| 74 |
static $KEYGRP1 = [ |
| 75 |
Vcalendar::VALUE, |
| 76 |
Vcalendar::TZID, |
| 77 |
Vcalendar::RANGE, |
| 78 |
Vcalendar::RELTYPE |
| 79 |
]; |
| 80 |
static $KEYGRP2 = [ Vcalendar::DIR, Vcalendar::ALTREP ]; |
| 81 |
static $KEYGRP3 = [ |
| 82 |
Vcalendar::SENT_BY, |
| 83 |
Vcalendar::DISPLAY, |
| 84 |
Vcalendar::FEATURE, |
| 85 |
Vcalendar::LABEL |
| 86 |
]; |
| 87 |
|
| 88 |
if( isset( $inputParams[Util::$ISLOCALTIME ] )) { |
| 89 |
unset( $inputParams[Util::$ISLOCALTIME ] ); |
| 90 |
} |
| 91 |
if( empty( $inputParams ) && empty( $ctrKeys ) && empty( $lang )) { |
| 92 |
return Util::$SP0; |
| 93 |
} |
| 94 |
if( ! is_array( $inputParams )) { |
| 95 |
$inputParams = []; |
| 96 |
} |
| 97 |
if( empty( $ctrKeys )) { |
| 98 |
$ctrKeys = []; |
| 99 |
} |
| 100 |
elseif( ! is_array( $ctrKeys )) { |
| 101 |
$ctrKeys = [ $ctrKeys ]; |
| 102 |
} |
| 103 |
$attrLANG = $attr1 = $attr2 = null; |
| 104 |
$hasCNattrKey = in_array( Vcalendar::CN, $ctrKeys ); |
| 105 |
$hasLANGattrKey = in_array( Vcalendar::LANGUAGE, $ctrKeys ); |
| 106 |
$CNattrExist = false; |
| 107 |
$params = $xparams = []; |
| 108 |
foreach( |
| 109 |
array_change_key_case( $inputParams, CASE_UPPER ) |
| 110 |
as $paramKey => $paramValue |
| 111 |
) { |
| 112 |
if(( false !== strpos( $paramValue, Util::$COLON )) || |
| 113 |
( false !== strpos( $paramValue, Util::$SEMIC )) || |
| 114 |
( false !== strpos( $paramValue, Util::$COMMA ))) { |
| 115 |
if( ! in_array( $paramKey, $DFKEYS )) { // DISPLAY, FEATURE |
| 116 |
$paramValue = sprintf( $FMTQ, $paramValue ); |
| 117 |
} |
| 118 |
} |
| 119 |
switch( true ) { |
| 120 |
case ctype_digit((string) $paramKey ) : // ?? |
| 121 |
$xparams[] = $paramValue; |
| 122 |
break; |
| 123 |
case StringFactory::isXprefixed( $paramKey ) : |
| 124 |
$xparams[$paramKey] = $paramValue; |
| 125 |
break; |
| 126 |
default : |
| 127 |
$params[$paramKey] = $paramValue; |
| 128 |
break; |
| 129 |
} // end switch |
| 130 |
} // end foreach |
| 131 |
ksort( $xparams, SORT_STRING ); |
| 132 |
foreach( $xparams as $paramKey => $paramValue ) { |
| 133 |
$attr2 .= Util::$SEMIC; |
| 134 |
$attr2 .= ( ctype_digit((string) $paramKey )) |
| 135 |
? $paramValue |
| 136 |
: sprintf( $FMTKEQV, $paramKey, $paramValue ); |
| 137 |
} |
| 138 |
if( isset( $params[Vcalendar::FMTTYPE] ) && |
| 139 |
! in_array( Vcalendar::FMTTYPE, $ctrKeys )) { |
| 140 |
$attr1 .= sprintf( $FMTFMTTYPE, $params[Vcalendar::FMTTYPE], $attr2 ); |
| 141 |
$attr2 = null; |
| 142 |
unset( $params[Vcalendar::FMTTYPE] ); |
| 143 |
} |
| 144 |
if( isset( $params[Vcalendar::ENCODING] ) && |
| 145 |
! in_array( Vcalendar::ENCODING, $ctrKeys )) { |
| 146 |
if( ! empty( $attr2 )) { |
| 147 |
$attr1 .= $attr2; |
| 148 |
$attr2 = null; |
| 149 |
} |
| 150 |
$attr1 .= sprintf( |
| 151 |
$FMTCMN, |
| 152 |
Vcalendar::ENCODING, |
| 153 |
$params[Vcalendar::ENCODING] |
| 154 |
); |
| 155 |
unset( $params[Vcalendar::ENCODING] ); |
| 156 |
} |
| 157 |
foreach( $KEYGRP1 as $key ) { // VALUE, TZID, RANGE, RELTYPE |
| 158 |
if( isset( $params[$key] ) && ! in_array( $key, $ctrKeys )) { |
| 159 |
$attr1 .= sprintf( $FMTCMN, $key, $params[$key] ); |
| 160 |
unset( $params[$key] ); |
| 161 |
} |
| 162 |
} // end foreach |
| 163 |
if( isset( $params[Vcalendar::CN] ) && $hasCNattrKey ) { |
| 164 |
$attr1 .= sprintf( $FMTCMN, Vcalendar::CN, $params[Vcalendar::CN] ); |
| 165 |
$CNattrExist = true; |
| 166 |
unset( $params[Vcalendar::CN] ); |
| 167 |
} |
| 168 |
foreach( $KEYGRP2 as $key ) { // DIR, ALTREP |
| 169 |
if( isset( $params[$key] ) && in_array( $key, $ctrKeys )) { |
| 170 |
$delim = ( false !== strpos( $params[$key], StringFactory::$QQ )) |
| 171 |
? null |
| 172 |
: StringFactory::$QQ; |
| 173 |
$attr1 .= sprintf( $FMTQTD, $key, $delim, $params[$key], $delim ); |
| 174 |
unset( $params[$key] ); |
| 175 |
} |
| 176 |
} // end foreach |
| 177 |
foreach( $KEYGRP3 as $key ) { // SENT_BY, DISPLAY, FEATURE, LABEL |
| 178 |
if( isset( $params[$key] ) && in_array( $key, $ctrKeys )) { |
| 179 |
$attr1 .= sprintf( $FMTCMN, $key, $params[$key] ); |
| 180 |
unset( $params[$key] ); |
| 181 |
} |
| 182 |
} // end foreach |
| 183 |
if( isset( $params[Vcalendar::LANGUAGE] ) && $hasLANGattrKey ) { |
| 184 |
$attrLANG .= sprintf( |
| 185 |
$FMTCMN, |
| 186 |
Vcalendar::LANGUAGE, |
| 187 |
$params[Vcalendar::LANGUAGE] |
| 188 |
); |
| 189 |
unset( $params[Vcalendar::LANGUAGE] ); |
| 190 |
} |
| 191 |
elseif(( $CNattrExist || $hasLANGattrKey ) && ! empty( $lang )) { |
| 192 |
$attrLANG .= sprintf( $FMTCMN, Vcalendar::LANGUAGE, $lang ); |
| 193 |
} |
| 194 |
if( ! empty( $params )) { // accept other or iana-token (Other IANA-registered) parameter types |
| 195 |
foreach( $params as $paramKey => $paramValue ) { |
| 196 |
$attr1 .= sprintf( $FMTCMN, $paramKey, $paramValue ); |
| 197 |
} |
| 198 |
} |
| 199 |
return $attr1 . $attrLANG . $attr2; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Remove key/value from array (if found) |
| 204 |
* |
| 205 |
* @param array $array iCal property parameters |
| 206 |
* @param string $expectedKey expected key |
| 207 |
* @param string $expectedValue expected value |
| 208 |
* @static |
| 209 |
* @since 2.30.2 - 2021-02-04 |
| 210 |
*/ |
| 211 |
public static function ifExistRemove( |
| 212 |
array & $array, |
| 213 |
$expectedKey, |
| 214 |
$expectedValue = null |
| 215 |
) { |
| 216 |
if( empty( $array )) { |
| 217 |
return; |
| 218 |
} |
| 219 |
foreach( $array as $key => $value ) { |
| 220 |
if(( 0 == strcasecmp( $expectedKey, $key )) && |
| 221 |
( empty( $expectedValue ) || |
| 222 |
( 0 == strcasecmp( $expectedValue, $value )))) { |
| 223 |
unset( $array[$key] ); |
| 224 |
$array = array_filter( $array ); |
| 225 |
break; |
| 226 |
} // end if |
| 227 |
} // end foreach |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Return true if property parameter VALUE is set to argument, otherwise false |
| 232 |
* |
| 233 |
* @param array $parameterArr |
| 234 |
* @param string $arg |
| 235 |
* @return bool |
| 236 |
* @static |
| 237 |
* @since 2.27.14 - 2019-03-01 |
| 238 |
*/ |
| 239 |
public static function isParamsValueSet( $parameterArr, $arg ) |
| 240 |
{ |
| 241 |
if( empty( $parameterArr ) || ! isset( $parameterArr[Util::$LCparams] )) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
return Util::issetKeyAndEquals( |
| 245 |
$parameterArr[Util::$LCparams], |
| 246 |
Vcalendar::VALUE, |
| 247 |
strtoupper( $arg ) |
| 248 |
); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Return param[TZID] or null |
| 253 |
* |
| 254 |
* @param array $parameterArr |
| 255 |
* @return string|null |
| 256 |
* @static |
| 257 |
* @since 2.27.14 - 2019-02-10 |
| 258 |
*/ |
| 259 |
public static function getParamTzid( $parameterArr ) |
| 260 |
{ |
| 261 |
return ( isset( $parameterArr[Util::$LCparams][Vcalendar::TZID] )) |
| 262 |
? $parameterArr[Util::$LCparams][Vcalendar::TZID] |
| 263 |
: null; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Return (conformed) iCal component property parameters |
| 268 |
* |
| 269 |
* Trim quoted values, default parameters may be set, if missing |
| 270 |
* |
| 271 |
* @param array $params |
| 272 |
* @param array $defaults |
| 273 |
* @return array |
| 274 |
* @static |
| 275 |
* @since 2.29.25 - 2020-08-25 |
| 276 |
*/ |
| 277 |
public static function setParams( $params, $defaults = null ) |
| 278 |
{ |
| 279 |
$output = []; |
| 280 |
if( empty( $params ) && empty( $defaults )) { |
| 281 |
return $output; |
| 282 |
} |
| 283 |
if( ! is_array( $params )) { |
| 284 |
$params = []; |
| 285 |
} |
| 286 |
foreach( |
| 287 |
array_change_key_case( $params, CASE_UPPER ) |
| 288 |
as $paramKey => $paramValue ) { |
| 289 |
if( Vcalendar::FEATURE === $paramKey ) { |
| 290 |
$output[Vcalendar::FEATURE] = |
| 291 |
trim( $paramValue, StringFactory::$QQ ); // accept comma in value |
| 292 |
continue; |
| 293 |
} |
| 294 |
if( is_array( $paramValue )) { |
| 295 |
foreach( $paramValue as $pkey => $pValue ) { |
| 296 |
$paramValue[$pkey] = trim( $pValue, StringFactory::$QQ ); |
| 297 |
} |
| 298 |
} |
| 299 |
else { |
| 300 |
$paramValue = trim( $paramValue, StringFactory::$QQ ); |
| 301 |
} |
| 302 |
if( Vcalendar::VALUE === $paramKey ) { |
| 303 |
$output[Vcalendar::VALUE] = strtoupper( $paramValue ); |
| 304 |
} |
| 305 |
else { |
| 306 |
$output[$paramKey] = $paramValue; |
| 307 |
} |
| 308 |
} // end foreach |
| 309 |
if( is_array( $defaults )) { |
| 310 |
$output = array_merge( |
| 311 |
array_change_key_case( |
| 312 |
$defaults, CASE_UPPER ), |
| 313 |
$output |
| 314 |
); |
| 315 |
} |
| 316 |
return $output; |
| 317 |
} |
| 318 |
} |
| 319 |
|