[4898] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /*********************************************** |
---|
| 4 | * File : utils.php |
---|
| 5 | * Project : Z-Push |
---|
| 6 | * Descr : |
---|
| 7 | * |
---|
| 8 | * Created : 03.04.2008 |
---|
| 9 | * |
---|
| 10 | * Zarafa Deutschland GmbH, www.zarafaserver.de |
---|
| 11 | * This file is distributed under GPL v2. |
---|
| 12 | * Consult LICENSE file for details |
---|
| 13 | ************************************************/ |
---|
| 14 | |
---|
| 15 | // saves information about folder data for a specific device |
---|
| 16 | function _saveFolderData($devid, $folders) { |
---|
| 17 | if (!is_array($folders) || empty ($folders)) |
---|
| 18 | return false; |
---|
| 19 | |
---|
| 20 | $unique_folders = array (); |
---|
| 21 | |
---|
| 22 | foreach ($folders as $folder) { |
---|
| 23 | if (!isset($folder->type)) |
---|
| 24 | continue; |
---|
| 25 | |
---|
| 26 | // don't save folder-ids for emails |
---|
| 27 | if ($folder->type == SYNC_FOLDER_TYPE_INBOX) |
---|
| 28 | continue; |
---|
| 29 | |
---|
| 30 | // no folder from that type or the default folder |
---|
| 31 | if (!array_key_exists($folder->type, $unique_folders) || $folder->parentid == 0) { |
---|
| 32 | $unique_folders[$folder->type] = $folder->serverid; |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // Treo does initial sync for calendar and contacts too, so we need to fake |
---|
| 37 | // these folders if they are not supported by the backend |
---|
| 38 | if (!array_key_exists(SYNC_FOLDER_TYPE_APPOINTMENT, $unique_folders)) |
---|
| 39 | $unique_folders[SYNC_FOLDER_TYPE_APPOINTMENT] = SYNC_FOLDER_TYPE_DUMMY; |
---|
| 40 | if (!array_key_exists(SYNC_FOLDER_TYPE_CONTACT, $unique_folders)) |
---|
| 41 | $unique_folders[SYNC_FOLDER_TYPE_CONTACT] = SYNC_FOLDER_TYPE_DUMMY; |
---|
| 42 | |
---|
| 43 | if (!file_put_contents(BASE_PATH.STATE_DIR."/compat-$devid", serialize($unique_folders))) { |
---|
| 44 | debugLog("_saveFolderData: Data could not be saved!"); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | // returns information about folder data for a specific device |
---|
| 49 | function _getFolderID($devid, $class) { |
---|
| 50 | $filename = BASE_PATH.STATE_DIR."/compat-$devid"; |
---|
| 51 | |
---|
| 52 | if (file_exists($filename)) { |
---|
| 53 | $arr = unserialize(file_get_contents($filename)); |
---|
| 54 | |
---|
| 55 | if ($class == "Calendar") |
---|
| 56 | return $arr[SYNC_FOLDER_TYPE_APPOINTMENT]; |
---|
| 57 | if ($class == "Contacts") |
---|
| 58 | return $arr[SYNC_FOLDER_TYPE_CONTACT]; |
---|
| 59 | |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Function which converts a hex entryid to a binary entryid. |
---|
| 67 | * @param string @data the hexadecimal string |
---|
| 68 | */ |
---|
| 69 | function hex2bin($data) { |
---|
| 70 | return pack("H*", $data); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | function utf8_to_windows1252($string, $option = "") |
---|
| 74 | { |
---|
| 75 | if (function_exists("iconv")){ |
---|
| 76 | return @iconv("UTF-8", "Windows-1252" . $option, $string); |
---|
| 77 | }else{ |
---|
| 78 | return utf8_decode($string); // no euro support here |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | function windows1252_to_utf8($string, $option = "") |
---|
| 83 | { |
---|
| 84 | if (function_exists("iconv")){ |
---|
| 85 | return @iconv("Windows-1252", "UTF-8" . $option, $string); |
---|
| 86 | }else{ |
---|
| 87 | return utf8_encode($string); // no euro support here |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | function w2u($string) { return windows1252_to_utf8($string); } |
---|
| 92 | function u2w($string) { return utf8_to_windows1252($string); } |
---|
| 93 | |
---|
| 94 | function w2ui($string) { return windows1252_to_utf8($string, "//TRANSLIT"); } |
---|
| 95 | function u2wi($string) { return utf8_to_windows1252($string, "//TRANSLIT"); } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Truncate an UTF-8 encoded sting correctly |
---|
| 99 | * |
---|
| 100 | * If it's not possible to truncate properly, an empty string is returned |
---|
| 101 | * |
---|
| 102 | * @param string $string - the string |
---|
| 103 | * @param string $length - position where string should be cut |
---|
| 104 | * @return string truncated string |
---|
| 105 | */ |
---|
| 106 | function utf8_truncate($string, $length) { |
---|
| 107 | if (strlen($string) <= $length) |
---|
| 108 | return $string; |
---|
| 109 | |
---|
| 110 | while($length >= 0) { |
---|
| 111 | if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) |
---|
| 112 | return substr($string, 0, $length); |
---|
| 113 | |
---|
| 114 | $length--; |
---|
| 115 | } |
---|
| 116 | return ""; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Build an address string from the components |
---|
| 122 | * |
---|
| 123 | * @param string $street - the street |
---|
| 124 | * @param string $zip - the zip code |
---|
| 125 | * @param string $city - the city |
---|
| 126 | * @param string $state - the state |
---|
| 127 | * @param string $country - the country |
---|
| 128 | * @return string the address string or null |
---|
| 129 | */ |
---|
| 130 | function buildAddressString($street, $zip, $city, $state, $country) { |
---|
| 131 | $out = ""; |
---|
| 132 | |
---|
| 133 | if (isset($country) && $street != "") $out = $country; |
---|
| 134 | |
---|
| 135 | $zcs = ""; |
---|
| 136 | if (isset($zip) && $zip != "") $zcs = $zip; |
---|
| 137 | if (isset($city) && $city != "") $zcs .= (($zcs)?" ":"") . $city; |
---|
| 138 | if (isset($state) && $state != "") $zcs .= (($zcs)?" ":"") . $state; |
---|
| 139 | if ($zcs) $out = $zcs . "\r\n" . $out; |
---|
| 140 | |
---|
| 141 | if (isset($street) && $street != "") $out = $street . (($out)?"\r\n\r\n". $out: "") ; |
---|
| 142 | |
---|
| 143 | return ($out)?$out:null; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Checks if the PHP-MAPI extension is available and in a requested version |
---|
| 148 | * |
---|
| 149 | * @param string $version - the version to be checked ("6.30.10-18495", parts or build number) |
---|
| 150 | * @return boolean installed version is superior to the checked strin |
---|
| 151 | */ |
---|
| 152 | function checkMapiExtVersion($version = "") { |
---|
| 153 | // compare build number if requested |
---|
| 154 | if (preg_match('/^\d+$/',$version) && strlen > 3) { |
---|
| 155 | $vs = preg_split('/-/', phpversion("mapi")); |
---|
| 156 | return ($version <= $vs[1]); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if (extension_loaded("mapi")){ |
---|
| 160 | if (version_compare(phpversion("mapi"), $version) == -1){ |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | return false; |
---|
| 166 | |
---|
| 167 | return true; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Parses and returns an ecoded vCal-Uid from an |
---|
| 172 | * OL compatible GlobalObjectID |
---|
| 173 | * |
---|
| 174 | * @param string $olUid - an OL compatible GlobalObjectID |
---|
| 175 | * @return string the vCal-Uid if available in the olUid, else the original olUid as HEX |
---|
| 176 | */ |
---|
| 177 | function getICalUidFromOLUid($olUid){ |
---|
| 178 | //check if "vCal-Uid" is somewhere in outlookid case-insensitive |
---|
| 179 | $icalUid = stristr($olUid, "vCal-Uid"); |
---|
| 180 | if ($icalUid !== false) { |
---|
| 181 | //get the length of the ical id - go back 4 position from where "vCal-Uid" was found |
---|
| 182 | $begin = unpack("V", substr($olUid, strlen($icalUid) * (-1) - 4, 4)); |
---|
| 183 | //remove "vCal-Uid" and packed "1" and use the ical id length |
---|
| 184 | return substr($icalUid, 12, ($begin[1] - 13)); |
---|
| 185 | } |
---|
| 186 | return strtoupper(bin2hex($olUid)); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * Checks the given UID if it is an OL compatible GlobalObjectID |
---|
| 191 | * If not, the given UID is encoded inside the GlobalObjectID |
---|
| 192 | * |
---|
| 193 | * @param string $icalUid - an appointment uid as HEX |
---|
| 194 | * @return string an OL compatible GlobalObjectID |
---|
| 195 | * |
---|
| 196 | */ |
---|
| 197 | function getOLUidFromICalUid($icalUid) { |
---|
| 198 | if (strlen($icalUid) <= 64) { |
---|
| 199 | $len = 13 + strlen($icalUid); |
---|
| 200 | $OLUid = pack("V", $len); |
---|
| 201 | $OLUid .= "vCal-Uid"; |
---|
| 202 | $OLUid .= pack("V", 1); |
---|
| 203 | $OLUid .= $icalUid; |
---|
| 204 | return hex2bin("040000008200E00074C5B7101A82E0080000000000000000000000000000000000000000". bin2hex($OLUid). "00"); |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | return hex2bin($icalUid); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * Extracts the basedate of the GlobalObjectID and the RecurStartTime |
---|
| 212 | * |
---|
| 213 | * @param string $goid - OL compatible GlobalObjectID |
---|
| 214 | * @param long $recurStartTime - RecurStartTime |
---|
| 215 | * @return long basedate |
---|
| 216 | * |
---|
| 217 | */ |
---|
| 218 | function extractBaseDate($goid, $recurStartTime) { |
---|
| 219 | $hexbase = substr(bin2hex($goid), 32, 8); |
---|
| 220 | $day = hexdec(substr($hexbase, 6, 2)); |
---|
| 221 | $month = hexdec(substr($hexbase, 4, 2)); |
---|
| 222 | $year = hexdec(substr($hexbase, 0, 4)); |
---|
| 223 | |
---|
| 224 | if ($day && $month && $year) { |
---|
| 225 | $h = $recurStartTime >> 12; |
---|
| 226 | $m = ($recurStartTime - $h * 4096) >> 6; |
---|
| 227 | $s = $recurStartTime - $h * 4096 - $m * 64; |
---|
| 228 | |
---|
| 229 | return gmmktime($h, $m, $s, $month, $day, $year); |
---|
| 230 | } |
---|
| 231 | else |
---|
| 232 | return false; |
---|
| 233 | } |
---|
| 234 | ?> |
---|