| @@ -1,0 +1,145 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * +-----------------------------------------------------------------------+ | |
| 5 | + * | This file is part of the Roundcube Webmail client | | |
| 6 | + * | | | |
| 7 | + * | Copyright (C) The Roundcube Dev Team | | |
| 8 | + * | Copyright (C) Kolab Systems AG | | |
| 9 | + * | | | |
| 10 | + * | Licensed under the GNU General Public License version 3 or | | |
| 11 | + * | any later version with exceptions for skins & plugins. | | |
| 12 | + * | See the README file for a full license statement. | | |
| 13 | + * | | | |
| 14 | + * | PURPOSE: | | |
| 15 | + * | Utility class providing common functions | | |
| 16 | + * +-----------------------------------------------------------------------+ | |
| 17 | + * | Author: Thomas Bruederli <[email protected]> | | |
| 18 | + * | Author: Aleksander Machniak <[email protected]> | | |
| 19 | + * +-----------------------------------------------------------------------+ | |
| 20 | + */ | |
| 21 | + | |
| 22 | +namespace AcyMailing\Libraries\Imap2\Roundcube; | |
| 23 | + | |
| 24 | +use DateTimeZone; | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * Utility class providing common functions | |
| 28 | + * | |
| 29 | + * @package Framework | |
| 30 | + * @subpackage Utils | |
| 31 | + */ | |
| 32 | +class Utils | |
| 33 | +{ | |
| 34 | + /** | |
| 35 | + * Explode quoted string | |
| 36 | + * | |
| 37 | + * @param string $delimiter Delimiter expression string for preg_match() | |
| 38 | + * @param string $string Input string | |
| 39 | + * | |
| 40 | + * @return array String items | |
| 41 | + */ | |
| 42 | + public static function explode_quoted_string($delimiter, $string) | |
| 43 | + { | |
| 44 | + $result = []; | |
| 45 | + $strlen = strlen($string); | |
| 46 | + | |
| 47 | + for ($q = $p = $i = 0 ; $i < $strlen ; $i++) { | |
| 48 | + if ($string[$i] == "\"" && $string[$i - 1] != "\\") { | |
| 49 | + $q = $q ? false : true; | |
| 50 | + } elseif (!$q && preg_match("/$delimiter/", $string[$i])) { | |
| 51 | + $result[] = substr($string, $p, $i - $p); | |
| 52 | + $p = $i + 1; | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + $result[] = (string)substr($string, $p); | |
| 57 | + | |
| 58 | + return $result; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Improved equivalent to strtotime() | |
| 63 | + * | |
| 64 | + * @param string $date Date string | |
| 65 | + * @param DateTimeZone $timezone Timezone to use for DateTime object | |
| 66 | + * | |
| 67 | + * @return int Unix timestamp | |
| 68 | + */ | |
| 69 | + public static function strtotime($date, $timezone = null) | |
| 70 | + { | |
| 71 | + $date = self::clean_datestr($date); | |
| 72 | + $tzname = $timezone ? ' '.$timezone->getName() : ''; | |
| 73 | + | |
| 74 | + // unix timestamp | |
| 75 | + if (is_numeric($date)) { | |
| 76 | + return (int)$date; | |
| 77 | + } | |
| 78 | + | |
| 79 | + // It can be very slow when provided string is not a date and very long | |
| 80 | + if (strlen($date) > 128) { | |
| 81 | + $date = substr($date, 0, 128); | |
| 82 | + } | |
| 83 | + | |
| 84 | + // if date parsing fails, we have a date in non-rfc format. | |
| 85 | + // remove token from the end and try again | |
| 86 | + while (($ts = @strtotime($date.$tzname)) === false || $ts < 0) { | |
| 87 | + if (($pos = strrpos($date, ' ')) === false) { | |
| 88 | + break; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $date = rtrim(substr($date, 0, $pos)); | |
| 92 | + } | |
| 93 | + | |
| 94 | + return (int)$ts; | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Clean up date string for strtotime() input | |
| 99 | + * | |
| 100 | + * @param string $date Date string | |
| 101 | + * | |
| 102 | + * @return string Date string | |
| 103 | + */ | |
| 104 | + public static function clean_datestr($date) | |
| 105 | + { | |
| 106 | + $date = trim($date); | |
| 107 | + | |
| 108 | + // check for MS Outlook vCard date format YYYYMMDD | |
| 109 | + if (preg_match('/^([12][90]\d\d)([01]\d)([0123]\d)$/', $date, $m)) { | |
| 110 | + return sprintf('%04d-%02d-%02d 00:00:00', intval($m[1]), intval($m[2]), intval($m[3])); | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Clean malformed data | |
| 114 | + $date = preg_replace( | |
| 115 | + [ | |
| 116 | + '/\(.*\)/', // remove RFC comments | |
| 117 | + '/GMT\s*([+-][0-9]+)/', // support non-standard "GMTXXXX" literal | |
| 118 | + '/[^a-z0-9\x20\x09:\/\.+-]/i', // remove any invalid characters | |
| 119 | + '/\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*/i', // remove weekday names | |
| 120 | + ], | |
| 121 | + [ | |
| 122 | + '', | |
| 123 | + '\\1', | |
| 124 | + '', | |
| 125 | + '', | |
| 126 | + ], | |
| 127 | + $date | |
| 128 | + ); | |
| 129 | + | |
| 130 | + $date = trim($date); | |
| 131 | + | |
| 132 | + // try to fix dd/mm vs. mm/dd discrepancy, we can't do more here | |
| 133 | + if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(\s.*)?$/', $date, $m)) { | |
| 134 | + $mdy = $m[2] > 12 && $m[1] <= 12; | |
| 135 | + $day = $mdy ? $m[2] : $m[1]; | |
| 136 | + $month = $mdy ? $m[1] : $m[2]; | |
| 137 | + $date = sprintf('%04d-%02d-%02d%s', $m[3], $month, $day, $m[4] ? : ' 00:00:00'); | |
| 138 | + } // I've found that YYYY.MM.DD is recognized wrong, so here's a fix | |
| 139 | + elseif (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(\s.*)?$/', $date, $m)) { | |
| 140 | + $date = sprintf('%04d-%02d-%02d%s', $m[1], $m[2], $m[3], $m[4] ? : ' 00:00:00'); | |
| 141 | + } | |
| 142 | + | |
| 143 | + return $date; | |
| 144 | + } | |
| 145 | +} | |