Projet

Général

Profil

Paste
Télécharger (5,53 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / iCalcreator-2.22.1 / lib / iCal.vCard.inc.php @ bad93dab

1
<?php
2
/*********************************************************************************/
3
/**
4
 *
5
 * iCalcreator, a PHP rfc2445/rfc5545 solution.
6
 *
7
 * @copyright Copyright (c) 2007-2015 Kjell-Inge Gustafsson, kigkonsult, All rights reserved
8
 * @link      http://kigkonsult.se/iCalcreator/index.php
9
 * @license   http://kigkonsult.se/downloads/dl.php?f=LGPL
10
 * @package   iCalcreator
11
 * @version   2.22
12
 */
13
/**
14
 * This library is free software; you can redistribute it and/or
15
 * modify it under the terms of the GNU Lesser General Public
16
 * License as published by the Free Software Foundation; either
17
 * version 2.1 of the License, or (at your option) any later version.
18
 *
19
 * This library is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
 * Lesser General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public
25
 * License along with this library; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28
/*********************************************************************************/
29
/*          iCalcreator vCard helper functions                                   */
30
/*********************************************************************************/
31
/**
32
 * convert single ATTENDEE, CONTACT or ORGANIZER (in email format) to vCard
33
 * returns vCard/TRUE or if directory (if set) or file write is unvalid, FALSE
34
 *
35
 * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
36
 * @since 2.12.2 - 2012-07-11
37
 * @param string $email
38
 * @param string $version    vCard version (default 2.1)
39
 * @param string $directory  where to save vCards (default FALSE)
40
 * @param string $ext        vCard file extension (default 'vcf')
41
 * @uses ICALCREATOR_VERSION
42
 * @return mixed
43
 */
44
function iCal2vCard( $email, $version='2.1', $directory=FALSE, $ext='vcf' ) {
45
  if( FALSE === ( $pos = strpos( $email, '@' )))
46
    return FALSE;
47
  if( $directory ) {
48
    if( DIRECTORY_SEPARATOR != substr( $directory, ( 0 - strlen( DIRECTORY_SEPARATOR ))))
49
      $directory .= DIRECTORY_SEPARATOR;
50
    if( !is_dir( $directory ) || !is_writable( $directory ))
51
      return FALSE;
52
  }
53
            /* prepare vCard */
54
  $email  = str_replace( 'MAILTO:', '', $email );
55
  $name   = $person = substr( $email, 0, $pos );
56
  if( ctype_upper( $name ) || ctype_lower( $name ))
57
    $name = array( $name );
58
  else {
59
    if( FALSE !== ( $pos = strpos( $name, '.' ))) {
60
      $name = explode( '.', $name );
61
      foreach( $name as $k => $part )
62
        $name[$k] = ucfirst( $part );
63
    }
64
    else { // split camelCase
65
      $chars = $name;
66
      $name  = array( $chars[0] );
67
      $k     = 0;
68
      $x     = 1;
69
      while( FALSE !== ( $char = substr( $chars, $x, 1 ))) {
70
        if( ctype_upper( $char )) {
71
          $k += 1;
72
          $name[$k] = '';
73
        }
74
        $name[$k]  .= $char;
75
        $x++;
76
      }
77
    }
78
  }
79
  $nl     = "\r\n";
80
  $FN     = 'FN:'.implode( ' ', $name ).$nl;
81
  $name   = array_reverse( $name );
82
  $N      = 'N:'.array_shift( $name );
83
  $scCnt  = 0;
84
  while( NULL != ( $part = array_shift( $name ))) {
85
    if(( '4.0' != $version ) || ( 4 > $scCnt ))
86
      $scCnt += 1;
87
    $N   .= ';'.$part;
88
  }
89
  while(( '4.0' == $version ) && ( 4 > $scCnt )) {
90
    $N   .= ';';
91
    $scCnt += 1;
92
  }
93
  $N     .= $nl;
94
  $EMAIL  = 'EMAIL:'.$email.$nl;
95
           /* create vCard */
96
  $vCard  = 'BEGIN:VCARD'.$nl;
97
  $vCard .= "VERSION:$version$nl";
98
  $vCard .= 'PRODID:-//kigkonsult.se '.ICALCREATOR_VERSION."//$nl";
99
  $vCard .= $N;
100
  $vCard .= $FN;
101
  $vCard .= $EMAIL;
102
  $vCard .= 'REV:'.gmdate( 'Ymd\THis\Z' ).$nl;
103
  $vCard .= 'END:VCARD'.$nl;
104
            /* save each vCard as (unique) single file */
105
  if( $directory ) {
106
    $fname = $directory.preg_replace( '/[^a-z0-9.]/i', '', $email );
107
    $cnt   = 1;
108
    $dbl   = '';
109
    while( is_file ( $fname.$dbl.'.'.$ext )) {
110
      $cnt += 1;
111
      $dbl = "_$cnt";
112
    }
113
    if( FALSE === file_put_contents( $fname, $fname.$dbl.'.'.$ext ))
114
      return FALSE;
115
    return TRUE;
116
  }
117
            /* return vCard */
118
  else
119
    return $vCard;
120
}
121
/**
122
 * convert ATTENDEEs, CONTACTs and ORGANIZERs (in email format) to vCards
123
 *
124
 * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
125
 * @since 2.12.2 - 2012-05-07
126
 * @param object $calendar   iCalcreator vcalendar instance reference
127
 * @param string $version    vCard version (default 2.1)
128
 * @param string $directory  where to save vCards (default FALSE)
129
 * @param string $ext        vCard file extension (default 'vcf')
130
 * @uses vcalendar::getProperty()
131
 * @return mixed
132
 */
133
function iCal2vCards( & $calendar, $version='2.1', $directory=FALSE, $ext='vcf' ) {
134
  $hits   = array();
135
  $vCardP = array( 'ATTENDEE', 'CONTACT', 'ORGANIZER' );
136
  foreach( $vCardP as $prop ) {
137
    $hits2 = $calendar->getProperty( $prop );
138
    foreach( $hits2 as $propValue => $occCnt ) {
139
      if( FALSE === ( $pos = strpos( $propValue, '@' )))
140
        continue;
141
      $propValue = str_replace( 'MAILTO:', '', $propValue );
142
      if( isset( $hits[$propValue] ))
143
        $hits[$propValue] += $occCnt;
144
      else
145
        $hits[$propValue]  = $occCnt;
146
    }
147
  }
148
  if( empty( $hits ))
149
    return FALSE;
150
  ksort( $hits );
151
  $output   = '';
152
  foreach( $hits as $email => $skip ) {
153
    $res = iCal2vCard( $email, $version, $directory, $ext );
154
    if( $directory && !$res )
155
      return FALSE;
156
    elseif( !$res )
157
      return $res;
158
    else
159
      $output .= $res;
160
  }
161
  if( $directory )
162
    return TRUE;
163
  if( !empty( $output ))
164
    return $output;
165
  return FALSE;
166
}