You are on page 1of 8

/*

File:

PACSuppport.js

Contains:

PAC Support functions

Copyright: 2003-2004 by Apple Computer, Inc., all rights reserved


Bugs?:

For bug reports, consult the following page on


the World Wide Web:
http://developer.apple.com/bugreporter/

Note from Adobe Systems, Inc:


This particular copy of the source was slightly modified (with the help
of Apple DTS, incident 19413954)
to support Auto Proxy in the Adobe ALM client. The original source can b
e found in the Mac OSX system at:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/
CFNetwork.framework/Versions/A/Resources/PACSupport.js
*/
var __Apple_dsn_cache = new Array;
function isPlainHostName(host) {
return (host.indexOf('.') == -1 ? true : false);
}
function dnsDomainIs(host, domain) {
var h = host.toLowerCase();
var d = domain.toLowerCase();
var sub = h.substring(h.length - d.length, h.length);
if (sub == d)
return true;
return false;
}
function localHostOrDomainIs(host, hostdom) {
var h1 = host.toLowerCase();
var h2 = hostdom.toLowerCase();
return ((h1 == h2) || (isPlainHostName(h1) & !isPlainHostName(h2))) ? true :
false;
}
function isResolvable(host) {
var ip = dnsResolve(host);
return ((typeof ip == "string") && ip.length) ? true : false;

}
function isInNet(host, pattern, mask) {
var ip = dnsResolve(host);
if (ip) {
var p = pattern.split('.');
var m = mask.split('.');
var a = ip.split('.');
if ((p.length == m.length) && (m.length == a.length)) {
for (i = 0; i < p.length; i++) {
if ((p[i]& m[i]) != (m[i]& a[i]))
return false;
}
return true;
}
}
return false;
}
function dnsResolve(host) {
var ips, i;
for (i = 0; i < __Apple_dsn_cache.length; i += 2) {
if (__Apple_dsn_cache[i] == host) {
ips = __Apple_dsn_cache[i + 1];
break;
}
}
if (i >= __Apple_dsn_cache.length) {
ips = TestObject.TestCommand( host );
if (__Apple_dsn_cache.length == 20) {
__Apple_dsn_cache.pop();
__Apple_dsn_cache.pop();
}
__Apple_dsn_cache[__Apple_dsn_cache.length] = host;
__Apple_dsn_cache[__Apple_dsn_cache.length] = ips;
}
if (ips && ips.length) {
for (var i = 0; i < ips.length; i++) {
var bytes = ips[i].split('.');
if (bytes.length == 4)
return ips[i];
}

}
return null;
}
function myIpAddress() {
var primary = TestObject.TestCommand( "myIpAddress" );
var ips = null;
if (primary && primary.length) {
ips = primary.Addresses;
}
if (ips && ips.length) {
for (var i = 0; i < ips.length; i++) {
var bytes = ips[i].split('.');
if (bytes.length == 4)
return ips[i];
}
}
return null;
}
function dnsDomainLevels(host) {
var parts = host.split('.');
return parts.length - 1;
}
function shExpMatch(str, shexp) {
if (typeof str != "string" || typeof shexp != "string")
return false;
if (shexp == "*")
return true;
if (str == "" && shexp == "")
return true;
str = str.toLowerCase();
shexp = shexp.toLowerCase();
var
var
var
for

len = str.length;
pieces = shexp.split('*');
start = 0;
(i = 0; i < pieces.length; i++) {
if (pieces[i] == "")
continue;
if (start > len)
return false;

start = str.indexOf(pieces[i]);
if (start == -1)
return false;
start += pieces[i].length;
str = str.substring(start, len);
len = str.length;
}
i--;
if ((pieces[i] == "") || (str == ""))
return true;
return false;
}
function weekdayRange(wd1, wd2, gmt) {
var today = new Date();
var days = "SUNMONTUEWEDTHUFRISAT";
wd1 = wd1.toUpperCase();
if (wd2 == undefined)
wd2 = wd1;
else
wd2 = wd2.toUpperCase();
var d1 = days.indexOf(wd1);
var d2 = days.indexOf(wd2);
if ((d2 == -1) && (wd2 == "GMT")) {
gmt = wd2;
d2 = d1;
}
if ((d1 == -1) || (d2 == -1))
return false;
d1 = d1 / 3;
d2 = d2 / 3;
if (gmt == "GMT")
today = today.getUTCDay();
else
today = today.getDay();
if ((d1 <= d2) && (today >= d1) && (today <= d2))
return true;
if ((d2 < d1) && ((today <= d2) || (today >= d1)))
return true;
return false;
}
function dateRange() {

var today = new Date();


var num = arguments.length;
var gmt = arguments[num - 1];
if (typeof gmt != "string")
gmt = false;
else {
gmt = gmt.toUpperCase();
if (gmt != "GMT")
gmt = false;
else {
gmt = true;
num--;
}
}
if (!num || (num > 6))
return false;
var
var
var
var
var
var

d1
d2
m1
m2
y1
y2

=
=
=
=
=
=

0;
0;
0;
0;
0;
0;

for (i = 0; i < num; i++) {


var arg = arguments[i];
if (typeof arg == "number") {
if (arg > 31) {
if (!y1)
y1 = arg;
else if (!y2)
y2 = arg;
else
return false;
}
else if (!arg)
return false;
else if (!d1)
d1 = arg;
else if (!d2)
d2 = arg;
else
return false;
}
else if (typeof arg == "string") {
var months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
arg = arg.toUpperCase();
arg = months.indexOf(arg);

if (arg == -1)
return false;
arg /= 3;
arg += 1;
if (!m1)
m1 = arg;
else if (!m2)
m2 = arg;
else
return false;
}
else
return false;
}
if
if
if
if
if
if

(!y1)
(!y2)
(!m1)
(!m2)
(!d1)
(!d2)

y1
y2
m1
m2
d1
d2

=
=
=
=
=
=

gmt ? today.getUTCFullYear() : today.getFullYear();


y1;
(gmt ? today.getUTCMonth() : today.getMonth()) + 1;
m1;
gmt ? today.getUTCDate() : today.getDate();
d1;

var date1;
var date2;
if (gmt) {
date1 = Date.UTC(y1, m1 - 1, d1, 0, 0, 0, 0);
date2 = Date.UTC(y2, m2 - 1, d2, 23, 59, 59, 999);
}
else {
date1 = (Date(y1, m1 - 1, d1, 0, 0, 0, 0)).valueOf();
date2 = (Date(y2, m2 - 1, d2, 23, 59, 59, 999)).valueOf();
}
today = today.valueOf();
return ((date1 <= today) && (today <= date2));
}
function timeRange() {
var date1 = new Date();
var today = new Date();
var date2 = new Date();
var num = arguments.length;
var gmt = arguments[num - 1];
if (typeof gmt != "string")
gmt = false;
else {
gmt = gmt.toUpperCase();
if (gmt != "GMT")

gmt = false;
else {
gmt = true;
num--;
}
}
if ( !num || (num > 6) || ( (num % 2 != 0) && (num != 1) ) )
return false;
date1.setMinutes(0);
date1.setSeconds(0);
date1.setMilliseconds(0);
date2.setMinutes(59);
date2.setSeconds(59);
date2.setMilliseconds(999);
for (i = 0; i < (num / 2); i++) {
var arg = arguments[i];
if (gmt) {
switch (i) {
case 0:
date1.setUTCHours(arg);
date2.setUTCHours(arg);
break;
case 1:
date1.setUTCMinutes(arg);
date2.setUTCMinutes(arg);
break;
case 2:
date1.setUTCSeconds(arg);
date2.setUTCSeconds(arg);
break;
}
}
else {
switch (i) {
case 0:
date1.setHours(arg);
date2.setHours(arg);
break;
case 1:
date1.setMinutes(arg);
date2.setMinutes(arg);
break;
case 2:
date1.setSeconds(arg);
date2.setSeconds(arg);
break;
}
}
}
if (num != 1) {
date2.setMinutes(0);
date2.setSeconds(0);
date2.setMilliseconds(0);

for (i = 0; i < (num / 2); i++) {


var arg = arguments[(num / 2) + i];
if (gmt) {
switch (i) {
case 0:
date2.setUTCHours(arg);
break;
case 1:
date2.setUTCMinutes(arg);
break;
case 2:
date2.setUTCSeconds(arg);
break;
}
}
else {
switch (i) {
case 0:
date2.setHours(arg);
break;
case 1:
date2.setMinutes(arg);
break;
case 2:
date2.setSeconds(arg);
break;
}
}
}
}
today = today.valueOf();
date1 = date1.valueOf();
date2 = date2.valueOf();
if (date2 < date1)
date2 += 86400000;
return ((date1 <= today) && (today <= date2));
}
REPLACEME_FINDPROXYFORURL
function __Apple_FindProxyForURL(host, url) {
__Apple_dsn_cache = new Array;
var result = FindProxyForURL(host, url);
__Apple_dsn_cache = new Array;
return result;
}
__Apple_FindProxyForURL("%@", "%@");