| 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\Traits; |
| 32 |
|
| 33 |
use InvalidArgumentException; |
| 34 |
use Kigkonsult\Icalcreator\Util\ParameterFactory; |
| 35 |
use Kigkonsult\Icalcreator\Util\StringFactory; |
| 36 |
use Kigkonsult\Icalcreator\Util\Util; |
| 37 |
|
| 38 |
use function bin2hex; |
| 39 |
use function chr; |
| 40 |
use function openssl_random_pseudo_bytes; |
| 41 |
use function ord; |
| 42 |
use function str_split; |
| 43 |
use function vsprintf; |
| 44 |
|
| 45 |
/** |
| 46 |
* UID property functions |
| 47 |
* |
| 48 |
* @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se> |
| 49 |
* @since 2.29.14 2019-09-03 |
| 50 |
*/ |
| 51 |
trait UIDrfc7986trait |
| 52 |
{ |
| 53 |
/** |
| 54 |
* @var array component property UID value |
| 55 |
*/ |
| 56 |
protected $uid = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Return formatted output for calendar component property uid |
| 60 |
* |
| 61 |
* If uid is missing, uid is created |
| 62 |
* @return string |
| 63 |
* @since 2.29.5 2019-06-17 |
| 64 |
*/ |
| 65 |
public function createUid() |
| 66 |
{ |
| 67 |
if( self::isUidEmpty( $this->uid )) |
| 68 |
{ |
| 69 |
$this->uid = self::makeUid(); |
| 70 |
} |
| 71 |
return StringFactory::createElement( |
| 72 |
self::UID, |
| 73 |
ParameterFactory::createParams( $this->uid[Util::$LCparams] ), |
| 74 |
$this->uid[Util::$LCvalue] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Delete calendar component property uid |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
* @since 2.29.5 2019-06-17 |
| 83 |
*/ |
| 84 |
public function deleteUid() |
| 85 |
{ |
| 86 |
$this->uid = null; |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get calendar component property uid |
| 92 |
* |
| 93 |
* @param bool $inclParam |
| 94 |
* @return bool|array |
| 95 |
* @since 2.29.5 2019-06-17 |
| 96 |
*/ |
| 97 |
public function getUid( $inclParam = false ) |
| 98 |
{ |
| 99 |
if( self::isUidEmpty( $this->uid )) { |
| 100 |
$this->uid = self::makeUid(); |
| 101 |
} |
| 102 |
return ( $inclParam ) ? $this->uid : $this->uid[Util::$LCvalue]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Return bool true if uid is empty |
| 107 |
* |
| 108 |
* @param array $array |
| 109 |
* @return bool |
| 110 |
* @static |
| 111 |
* @since 2.29.5 2019-06-17 |
| 112 |
*/ |
| 113 |
private static function isUidEmpty( array $array = null ) |
| 114 |
{ |
| 115 |
if( empty( $array )) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
if( empty( $array[Util::$LCvalue] ) && |
| 119 |
( Util::$ZERO != $array[Util::$LCvalue] )) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Return an unique id for a calendar component object instance |
| 127 |
* |
| 128 |
* @return array |
| 129 |
* @static |
| 130 |
* @see https://www.php.net/manual/en/function.com-create-guid.php#117893 |
| 131 |
* @since 2.29.5 2019-06-17 |
| 132 |
*/ |
| 133 |
private static function makeUid() |
| 134 |
{ |
| 135 |
static $FMT = '%s%s-%s-%s-%s-%s%s%s'; |
| 136 |
static $MAX = 10; |
| 137 |
$cnt = 0; |
| 138 |
do { |
| 139 |
do { |
| 140 |
$bytes = openssl_random_pseudo_bytes( 16, $cStrong ); |
| 141 |
} while ( false === $bytes ); |
| 142 |
$cnt += 1; |
| 143 |
} while(( $MAX > $cnt ) && ( false === $cStrong )); |
| 144 |
$bytes[6] = chr(ord( $bytes[6] ) & 0x0f | 0x40 ); // set version to 0100 |
| 145 |
$bytes[8] = chr(ord( $bytes[8] ) & 0x3f | 0x80 ); // set bits 6-7 to 10 |
| 146 |
$uid = vsprintf( $FMT, str_split( bin2hex( $bytes ), 4 )); |
| 147 |
return [ |
| 148 |
Util::$LCvalue => $uid, |
| 149 |
Util::$LCparams => null, |
| 150 |
]; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Set calendar component property uid |
| 155 |
* |
| 156 |
* If empty input, male one |
| 157 |
* @param string $value |
| 158 |
* @param array $params |
| 159 |
* @return static |
| 160 |
* @throws InvalidArgumentException |
| 161 |
* @since 2.29.14 2019-09-03 |
| 162 |
*/ |
| 163 |
public function setUid( $value = null, $params = [] ) |
| 164 |
{ |
| 165 |
if( empty( $value ) && ( Util::$ZERO != $value )) { |
| 166 |
$this->uid = self::makeUid(); |
| 167 |
return $this; |
| 168 |
} // no allowEmpty check here !!!! |
| 169 |
Util::assertString( $value, self::UID ); |
| 170 |
$this->uid = [ |
| 171 |
Util::$LCvalue => StringFactory::trimTrailNL( $value ), |
| 172 |
Util::$LCparams => ParameterFactory::setParams( $params ), |
| 173 |
]; |
| 174 |
return $this; |
| 175 |
} |
| 176 |
} |
| 177 |
|