PluginProbe
Import Social Events / 1.8.5
Import Social Events v1.8.5
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 / IcalvCardFactory.php

IcalvCardFactory.php in Import Social Events 1.8.5, at includes/lib/icalcreator/src/Util/IcalvCardFactory.php

224 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 InvalidArgumentException;
34 use Kigkonsult\Icalcreator\Vcalendar;
35
36 use function array_reverse;
37 use function array_shift;
38 use function ctype_lower;
39 use function ctype_upper;
40 use function explode;
41 use function gmdate;
42 use function implode;
43 use function in_array;
44 use function sprintf;
45 use function strlen;
46 use function strpos;
47 use function substr;
48 use function ucfirst;
49
50 /**
51 * iCalcreator vCard support class
52 *
53 * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
54 * @since 2.27.8 - 2019-03-18
55 */
56 class IcalvCardFactory
57 {
58 /*
59 * $var array
60 */
61 private static $VCARDVERSIONS = [ 2 => '2.1', 3 => '3.0', 4 => '4.0' ];
62 /*
63 $V2_1 = '2.1';
64 private static $V3_0 = '3.0';
65 private static $V4_0 = '4.0';
66 */
67
68 /**
69 * Convert single ATTENDEE, CONTACT or ORGANIZER (in email format) to vCard 2.1, 3,0 or 4.0
70 *
71 * Returns vCard/true or if directory (if set) or file write is invalid, false
72 *
73 * @param string $email
74 * @param string $version vCard version (default 2.1)
75 * @return string
76 * @throws InvalidArgumentException
77 * @since 2.27.8 - 2019-03-18
78 */
79 public static function iCal2vCard( $email, $version = null )
80 {
81 static $FMTFN = "FN:%s\r\n";
82 static $FMTEMAIL = "EMAIL:%s\r\n";
83 static $BEGINVCARD = "BEGIN:VCARD\r\n";
84 static $FMTVERSION = "VERSION:%s\r\n";
85 static $FMTPRODID = "PRODID:-//kigkonsult.se %s\r\n";
86 static $FMTREV = "REV:%s\r\n";
87 static $YMDTHISZ = 'Ymd\THis\Z';
88 static $ENDVCARD = "END:VCARD\r\n";
89 if( empty( $version ) ) {
90 $version = self::$VCARDVERSIONS[2];
91 }
92 else {
93 self::assertVcardVersion( $version );
94 }
95 CalAddressFactory::assertCalAddress( $email );
96 /* prepare vCard name */
97 $names = self::splitNameInNameparts(
98 CalAddressFactory::extractNamepartFromEmail( $email )
99 );
100 /* create vCard */
101 $vCard = $BEGINVCARD;
102 $vCard .= sprintf( $FMTVERSION, $version );
103 $vCard .= sprintf( $FMTPRODID, ICALCREATOR_VERSION );
104 $vCard .= self::getVcardN( $names, $version );
105 $vCard .= sprintf( $FMTFN, implode( utiL::$SP1, $names ));
106 $vCard .= sprintf( $FMTEMAIL, CalAddressFactory::removeMailtoPrefix( $email ));
107 $vCard .= sprintf( $FMTREV, gmdate( $YMDTHISZ ));
108 $vCard .= $ENDVCARD;
109 return $vCard;
110 }
111
112 /**
113 * Convert ATTENDEEs, CONTACTs and ORGANIZERs (in email format) to vCard 2.1 or 4.0
114 *
115 * Skips ATTENDEEs, CONTACTs and ORGANIZERs not in email format
116 * @param Vcalendar $calendar iCalcreator Vcalendar instance
117 * @param string $version vCard version (default 2.1)
118 * @param bool $inclParams fetch from values or include from parameters
119 * @param int $count on return, count of hits
120 * @return string vCards
121 * @since 2.27.8 - 2019-03-17
122 */
123 public static function iCal2vCards(
124 Vcalendar $calendar,
125 $version = null,
126 $inclParams = true,
127 & $count = null
128 ) {
129 $hits = CalAddressFactory::getCalAddresses( $calendar, null, $inclParams );
130 $output = null;
131 $count = 0;
132 foreach( $hits as $email ) {
133 try {
134 $res = self::iCal2vCard( $email, $version );
135 }
136 catch( InvalidArgumentException $e ) {
137 continue;
138 }
139 $count += 1;
140 $output .= $res;
141 }
142 return $output;
143 }
144
145 /**
146 * Assert vCard version
147 *
148 * @param string $version
149 * @throws InvalidArgumentException
150 * @since 2.27.8 - 2019-03-18
151 */
152 private static function assertVcardVersion( $version )
153 {
154 static $ERRMSG1 = 'Invalid version %s';
155 if( ! in_array( $version, self::$VCARDVERSIONS )) {
156 throw new InvalidArgumentException( sprintf( $ERRMSG1, $version ));
157 }
158 }
159
160 /**
161 * Split string name into array nameParts
162 *
163 * @param string $name
164 * @return array
165 * @since 2.27.8 - 2019-03-17
166 */
167 private static function splitNameInNameparts( $name )
168 {
169 switch( true ) {
170 case ( ctype_upper( $name ) || ctype_lower( $name )) :
171 $nameParts = [ $name ];
172 break;
173 case ( false !== strpos( $name, Util::$DOT )) :
174 $nameParts = explode( Util::$DOT, $name );
175 foreach( $nameParts as $k => $part ) {
176 $nameParts[$k] = ucfirst( $part );
177 }
178 break;
179 default : // split camelCase
180 $nameParts = [ substr( $name, 0, 1 ) ];
181 $k = 0;
182 $x = 1;
183 $len = strlen( $name );
184 while( $x < $len ) {
185 if( ctype_upper( $name[$x] )) {
186 $k += 1;
187 $nameParts[$k] = null;
188 }
189 $nameParts[$k] .= $name[$x];
190 $x++;
191 } // end while
192 break;
193 } // end switch
194 return $nameParts;
195 }
196
197 /**
198 * Return formatted vCard name
199 *
200 * @param array $names
201 * @param string $version
202 * @return string
203 * @since 2.27.8 - 2019-03-18
204 */
205 private static function getVcardN( array $names, $version )
206 {
207 static $FMTN = 'N:%s';
208 $name = array_reverse( $names );
209 $vCardN = sprintf( $FMTN, array_shift( $name ));
210 $scCnt = 0;
211 while( null != ( $part = array_shift( $name ))) {
212 if(( self::$VCARDVERSIONS[4] != $version ) || ( 4 > $scCnt )) {
213 $scCnt += 1;
214 }
215 $vCardN .= Util::$SEMIC . $part;
216 } // end while
217 while(( self::$VCARDVERSIONS[4] == $version ) && ( 4 > $scCnt )) {
218 $vCardN .= Util::$SEMIC;
219 $scCnt += 1;
220 } // end while
221 return $vCardN . Util::$CRLF;
222 }
223 }
224