Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ditesh committed Feb 10, 2014
0 parents commit 310ca80
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
17 changes: 17 additions & 0 deletions groups.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ceo]
users="ceo1,ceo2"

[users]
disallowed_domains="facebook.com"
users="user1,user2"

[receptionist]
disallowed_domains="all"
allowed_domains="jobstreet.com,gmail.com"
users="user3,user4"

[temp]
timer="enabled"
timeout=180
users="user5"
disallowed_domains="www.facebook.com"
130 changes: 130 additions & 0 deletions santa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

$path = "/home/ditesh/squid/users";
$ini = parse_ini_file("/home/ditesh/squid/groups.ini", TRUE);
$cached = array();
$cached["domains_files"] = array();
date_default_timezone_set("Asia/Kuala_Lumpur");

while(1) {

$loopflag = FALSE;
$timeout = 3*60;

$metadata = stream_get_meta_data(STDIN);
if ($metadata["eof"] === TRUE) exit;

$str = fgets(STDIN);
$str = explode(" ", $str);
$username = trim(rawurldecode($str[0]));
$website = trim(rawurldecode($str[1]));

if (strlen($username) === 0) continue;

file_put_contents("/tmp/santa", "'".json_encode(array($username, $website))."'\n", FILE_APPEND);

foreach($ini as $group=>$data) {

$users = explode(",", $data["users"]);
if (in_array($username, $users) === TRUE) {

if ($data["timer"] === "enabled") {

if (strlen($data["timeout"]) > 0) $timeout = intval($data["timeout"]);

if (file_exists("$path/$username")) {

$timestamp = filectime("$path/$username");

if (date("d-m-Y") === date("d-m-Y", $timestamp)) {

if (time() - $timestamp > $timeout) {

file_put_contents("/tmp/santa", "'$username errs because timeout $timeout'\n", FILE_APPEND);
$loopflag = TRUE;
echo "ERR\n";
break;

}

} else {

unlink("$path/$username");
touch("$path/$username");

}
} else {

touch("$path/$username");

}
}

if (strlen($data["disallowed_domains"]) > 0) {

if ($data["disallowed_domains"] === "all") {

if (strlen($data["allowed_domains"]) > 0) {

$domains = explode(",", $data["allowed_domains"]);

if (!in_array($website, $domains)) {

$loopflag = TRUE;
echo "ERR\n";
break;

}
} else {

$loopflag = TRUE;
echo "ERR\n";
break;

}
} else {

$domains = explode(",", $data["disallowed_domains"]);

if (in_array($website, $domains)) {

$loopflag = TRUE;
echo "ERR\n";
break;

}
}
}

if (strlen($data["disallowed_domains_files"]) > 0) {

if ($cached["domains_files"][$group] === NULL) {

$files = explode(",", $data["disallowed_domains_files"]);

foreach($files as $file) {

if (file_exists($file)) {

$cached["domains_files"][$group][] = file_get_contents($file);

}
}
}

if (in_array($website, $cached["domains_files"][$group])) {

$loopflag = TRUE;
echo "ERR\n";
break;

}
}
}
}

if ($loopflag === FALSE) echo "OK\n";

}

?>
47 changes: 47 additions & 0 deletions timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

date_default_timezone_set("Asia/Kuala_Lumpur");

while(1) {

$str = trim(fgets(STDIN));
if (strlen($str) === 0) continue;

file_put_contents("/tmp/debug", "'$str'\n", FILE_APPEND);
$str = explode(" ", $str);

$output = array();
$status = NULL;
$username = trim(rawurldecode($str[0]));
$password = trim(rawurldecode($str[1]));

$auth = "/usr/lib64/squid/ncsa_auth /etc/squid/users";

/*
$cmd="echo '$username $password' | $auth";
exec($cmd, $output, $status);
if ( $output[0] !== "OK") {
echo "ERR\n";
continue;
} else {
echo "OK\n";
continue;
}
*/

echo "OK\n";
continue;



}


?>

0 comments on commit 310ca80

Please sign in to comment.