1/* 2 * Copyright 2011 Hans Leidekker for CodeWeavers 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 * 18 * Based on nsProxyAutoConfig.js from mozilla.org. 19 */ 20 21function myIpAddress() { 22 try { 23 return dns_resolve(''); 24 } catch (e) { 25 return '127.0.0.1'; 26 } 27} 28 29function dnsResolve(host) { 30 try { 31 return dns_resolve(host); 32 } catch (e) { 33 return null; 34 } 35} 36 37function dnsDomainIs(host, domain) { 38 return (host.length >= domain.length && 39 host.substring(host.length - domain.length) == domain); 40} 41 42function dnsDomainLevels(host) { 43 return host.split('.').length-1; 44} 45 46function convert_addr(ipchars) { 47 var bytes = ipchars.split('.'); 48 var result = ((bytes[0] & 0xff) << 24) | 49 ((bytes[1] & 0xff) << 16) | 50 ((bytes[2] & 0xff) << 8) | 51 (bytes[3] & 0xff); 52 return result; 53} 54 55function isInNet(ipaddr, pattern, maskstr) { 56 var test = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(ipaddr); 57 if (test == null) { 58 ipaddr = dnsResolve(ipaddr); 59 if (ipaddr == null) 60 return false; 61 } else if (test[1] > 255 || test[2] > 255 || 62 test[3] > 255 || test[4] > 255) { 63 return false; // not an IP address 64 } 65 var host = convert_addr(ipaddr); 66 var pat = convert_addr(pattern); 67 var mask = convert_addr(maskstr); 68 return ((host & mask) == (pat & mask)); 69} 70 71function isPlainHostName(host) { 72 return (host.search('\\.') == -1); 73} 74 75function isResolvable(host) { 76 var ip = dnsResolve(host); 77 return (ip != null); 78} 79 80function localHostOrDomainIs(host, hostdom) { 81 return (host == hostdom) || 82 (hostdom.lastIndexOf(host + '.', 0) == 0); 83} 84 85function shExpMatch(url, pattern) { 86 pattern = pattern.replace(/\./g, '\\.'); 87 pattern = pattern.replace(/\*/g, '.*'); 88 pattern = pattern.replace(/\?/g, '.'); 89 var newRe = new RegExp('^'+pattern+'$'); 90 return newRe.test(url); 91} 92 93var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6}; 94var months = {JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11}; 95 96function weekdayRange() { 97 function getDay(weekday) { 98 if (weekday in wdays) { 99 return wdays[weekday]; 100 } 101 return -1; 102 } 103 var date = new Date(); 104 var argc = arguments.length; 105 var wday; 106 if (argc < 1) 107 return false; 108 if (arguments[argc - 1] == 'GMT') { 109 argc--; 110 wday = date.getUTCDay(); 111 } else { 112 wday = date.getDay(); 113 } 114 var wd1 = getDay(arguments[0]); 115 var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1; 116 return (wd1 == -1 || wd2 == -1) ? false 117 : (wd1 <= wday && wday <= wd2); 118} 119 120function dateRange() { 121 function getMonth(name) { 122 if (name in months) { 123 return months[name]; 124 } 125 return -1; 126 } 127 var date = new Date(); 128 var argc = arguments.length; 129 if (argc < 1) { 130 return false; 131 } 132 var isGMT = (arguments[argc - 1] == 'GMT'); 133 134 if (isGMT) { 135 argc--; 136 } 137 // function will work even without explicit handling of this case 138 if (argc == 1) { 139 var tmp = parseInt(arguments[0]); 140 if (isNaN(tmp)) { 141 return ((isGMT ? date.getUTCMonth() : date.getMonth()) == getMonth(arguments[0])); 142 } else if (tmp < 32) { 143 return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp); 144 } else { 145 return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) == tmp); 146 } 147 } 148 var year = date.getFullYear(); 149 var date1, date2; 150 date1 = new Date(year, 0, 1, 0, 0, 0); 151 date2 = new Date(year, 11, 31, 23, 59, 59); 152 var adjustMonth = false; 153 for (var i = 0; i < (argc >> 1); i++) { 154 var tmp = parseInt(arguments[i]); 155 if (isNaN(tmp)) { 156 var mon = getMonth(arguments[i]); 157 date1.setMonth(mon); 158 } else if (tmp < 32) { 159 adjustMonth = (argc <= 2); 160 date1.setDate(tmp); 161 } else { 162 date1.setFullYear(tmp); 163 } 164 } 165 for (var i = (argc >> 1); i < argc; i++) { 166 var tmp = parseInt(arguments[i]); 167 if (isNaN(tmp)) { 168 var mon = getMonth(arguments[i]); 169 date2.setMonth(mon); 170 } else if (tmp < 32) { 171 date2.setDate(tmp); 172 } else { 173 date2.setFullYear(tmp); 174 } 175 } 176 if (adjustMonth) { 177 date1.setMonth(date.getMonth()); 178 date2.setMonth(date.getMonth()); 179 } 180 if (isGMT) { 181 var tmp = date; 182 tmp.setFullYear(date.getUTCFullYear()); 183 tmp.setMonth(date.getUTCMonth()); 184 tmp.setDate(date.getUTCDate()); 185 tmp.setHours(date.getUTCHours()); 186 tmp.setMinutes(date.getUTCMinutes()); 187 tmp.setSeconds(date.getUTCSeconds()); 188 date = tmp; 189 } 190 return ((date1 <= date) && (date <= date2)); 191} 192 193function timeRange() { 194 var argc = arguments.length; 195 var date = new Date(); 196 var isGMT= false; 197 198 if (argc < 1) { 199 return false; 200 } 201 if (arguments[argc - 1] == 'GMT') { 202 isGMT = true; 203 argc--; 204 } 205 206 var hour = isGMT ? date.getUTCHours() : date.getHours(); 207 var date1, date2; 208 date1 = new Date(); 209 date2 = new Date(); 210 211 if (argc == 1) { 212 return (hour == arguments[0]); 213 } else if (argc == 2) { 214 return ((arguments[0] <= hour) && (hour <= arguments[1])); 215 } else { 216 switch (argc) { 217 case 6: 218 date1.setSeconds(arguments[2]); 219 date2.setSeconds(arguments[5]); 220 case 4: 221 var middle = argc >> 1; 222 date1.setHours(arguments[0]); 223 date1.setMinutes(arguments[1]); 224 date2.setHours(arguments[middle]); 225 date2.setMinutes(arguments[middle + 1]); 226 if (middle == 2) { 227 date2.setSeconds(59); 228 } 229 break; 230 default: 231 throw 'timeRange: bad number of arguments' 232 } 233 } 234 235 if (isGMT) { 236 date.setFullYear(date.getUTCFullYear()); 237 date.setMonth(date.getUTCMonth()); 238 date.setDate(date.getUTCDate()); 239 date.setHours(date.getUTCHours()); 240 date.setMinutes(date.getUTCMinutes()); 241 date.setSeconds(date.getUTCSeconds()); 242 } 243 return ((date1 <= date) && (date <= date2)); 244} 245