<?
/////////////////////////////////////////////////////////////////////
function getmicrotime()
{
$res = explode(' ',microtime());
return $res[1];
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function is_user_loggedin()
{
$username = $_COOKIE["username"];
$password = $_COOKIE["password"];
$result = mysql_query("SELECT * FROM `reporters` WHERE `username`='$username' and `password`='$password'");
$num_rows = mysql_num_rows($result);
if($num_rows==1)
{
$res = "true";
}
else
{
$res = "false";
}
return $res;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function is_admin_loggedin($config_username, $config_password)
{
$username = $_COOKIE["admin_username"];
$password = $_COOKIE["admin_password"];
if($config_username==$username and $config_password==$password)
{
$res = "true";
}
else
{
$res = "false";
}
return $res;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_user_info()
{
$username = $_COOKIE["username"];
$password = $_COOKIE["password"];
$result = mysql_query("SELECT * FROM `reporters` WHERE `username`='$username' and `password`='$password'");
$row = mysql_fetch_array($result);
$info['email'] = $row['email'];
$info['id'] = $row['id'];
$info['username'] = $row['username'];
return $info;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function generate_random_number($start, $end)
{
$random_number = rand($start, $end);
return $random_number;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function sign_in($username,$password)
{
if(preg_match("/^[a-zA-Z0-9]+$/", $username) != 1) {
// string only contain the a to z , A to Z, 0 to 9
$error[]="ERROR: user name can only contain numbers and letters.";
}
if(preg_match("/^[a-zA-Z0-9]+$/", $password) != 1) {
// string only contain the a to z , A to Z, 0 to 9
$error[]="ERROR: password can only contain numbers and letters.";
}
if(count($error)==0) {
$result = mysql_query("SELECT * FROM `reporters` WHERE `username`='$username' and `password`='$password'");
$num_rows = mysql_num_rows($result);
if($num_rows==1){
$expire=time()+60*60*24*30;
setcookie("username", $username, $expire);
setcookie("password", $password, $expire);
}
else
{
$error[]="invalid user name or password.";
}
}
if(count($error)>0)
{
return $error;
}
else
{
return "yes";
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function admin_login($username,$password,$admin_username,$admin_password)
{
if($username==$admin_username and $password==$admin_password){
$expire=time()+60*60*24*30;
setcookie("admin_username", $username, $expire);
setcookie("admin_password", $password, $expire);
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function register($username,$password,$email)
{
if(preg_match("/^[a-zA-Z0-9]+$/", $username) != 1) {
// string only contain the a to z , A to Z, 0 to 9
$error[]="ERROR: user name can only contain numbers and letters";
}
if(preg_match("/^[a-zA-Z0-9]+$/", $password) != 1) {
// string only contain the a to z , A to Z, 0 to 9
$error[]="ERROR: password can only contain numbers and letters";
}
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
//echo "Valid email address.";
}
else {
//echo "Invalid email address.";
$error[]="ERROR: invalid email address";
}
if(count($error)>0){
return $error;
}
if(count($error)==0){
mysql_query("INSERT INTO `reporters` (`username` ,`email` ,`password`)VALUES ('$username', '$email', '$password');");
return "yes";
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function sign_out()
{
setcookie('username','',time()-3600);
setcookie('password','',time()-3600);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function admin_sign_out()
{
setcookie('admin_username','',time()-3600);
setcookie('admin_password','',time()-3600);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function submit_message($user_id,$message,$time,$name,$key)
{
mysql_query("INSERT INTO `messages` (`time` ,`message` ,`reporter_id`,`name`,`key`)VALUES ('$time', '$message', '$user_id', '$name', '$key');");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function include_only()
{
if(basename(__FILE__) == basename($_SERVER['PHP_SELF']))
{
echo "restricted access.";
exit();
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_latest_reports($limit)
{
$res = mysql_query("SELECT * FROM `messages` ORDER BY `time` DESC limit $limit");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['time'] = $row['time'];
$result[$count]['message'] = $row['message'];
$result[$count]['reporter_id'] = $row['reporter_id'];
$result[$count]['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_reports_by_ids($start, $limit)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `id` <= '$start' ORDER BY time DESC limit $limit");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['time'] = $row['time'];
$result[$count]['message'] = $row['message'];
$result[$count]['reporter_id'] = $row['reporter_id'];
$result[$count]['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_by_id($id)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `id` = '$id' limit 1");
while ($row = mysql_fetch_array($res)) {
$result['id'] = $row['id'];
$result['time'] = $row['time'];
$result['message'] = $row['message'];
$result['reporter_id'] = $row['reporter_id'];
$result['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_older_reports_by_time($start, $limit)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `time` <= '$start' ORDER BY time DESC limit $limit");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['time'] = $row['time'];
$result[$count]['message'] = $row['message'];
$result[$count]['reporter_id'] = $row['reporter_id'];
$result[$count]['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_newer_reports_by_time($start, $limit)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `time` >= '$start' ORDER BY time DESC limit $limit");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['time'] = $row['time'];
$result[$count]['message'] = $row['message'];
$result[$count]['reporter_id'] = $row['reporter_id'];
$result[$count]['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_max_id()
{
$result = mysql_query("select MAX(id) from messages");
$data = mysql_fetch_array($result);
return $data[0];
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_max_time()
{
$result = mysql_query("select MAX(time) from messages");
$data = mysql_fetch_array($result);
return $data[0];
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_min_time()
{
$result = mysql_query("select MIN(time) from messages");
$data = mysql_fetch_array($result);
return $data[0];
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_info($id)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `id` = '$id' limit 1");
while ($row = mysql_fetch_array($res)) {
$result['id'] = $row['id'];
$result['time'] = $row['time'];
$result['message'] = $row['message'];
$result['reporter_id'] = $row['reporter_id'];
$result['name'] = $row['name'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function url(){
$protocol = $_SERVER['HTTPS'] ? "https" : "http";
return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function read_option($option_name)
{
$res = mysql_query("SELECT * FROM `options` WHERE `option` = '$option_name'");
while ($row = mysql_fetch_array($res)) {
//$result['id'] = $row['id'];
//$result['option'] = $row['option'];
$result = $row['value'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function write_option($option_name, $option_value)
{
mysql_query("UPDATE `options` SET `value` = '$option_value' WHERE `option` = '$option_name' LIMIT 1");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function add_network($name,$url,$key,$read,$write,$enable)
{
mysql_query("INSERT INTO `networks` (`name` ,`url` ,`secret_key` ,`read` ,`write`, `enable`) VALUES ('$name','$url', '$key', '$read', '$write', '$enable');");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function generate_random_string($length)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($p = 0; $p < $length; $p++)
{
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_all_networks()
{
$res = mysql_query("SELECT * FROM `networks`");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['url'] = $row['url'];
$result[$count]['secret_key'] = $row['secret_key'];
$result[$count]['read'] = $row['read'];
$result[$count]['write'] = $row['write'];
$result[$count]['enable'] = $row['enable'];
$result[$count]['name'] = $row['name'];
$result[$count]['state'] = $row['state'];
$result[$count]['last_key_change'] = $row['last_key_change'];
$result[$count]['my_write_key'] = $row['my_write_key'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_networks_count()
{
$res = mysql_query("SELECT 'id' FROM `networks`");
$networks_count = mysql_num_rows($res);
return $networks_count;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_users_count()
{
$res = mysql_query("SELECT 'id' FROM `reporters`");
$count = mysql_num_rows($res);
return $count;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_messages_count()
{
$res = mysql_query("SELECT 'id' FROM `messages`");
$count = mysql_num_rows($res);
return $count;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function redirect_to_page($page_name)
{
echo "
<script type='text/javascript'>
<!--
window.location = 'index.php?page=".$page_name."'
//-->
</script>
";
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function delete_network($network_id)
{
mysql_query("DELETE FROM `networks` WHERE `id` = $network_id LIMIT 1;");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_network_info($network_id)
{
$res = mysql_query("SELECT * FROM `networks` WHERE `id` = '$network_id' limit 1");
while ($row = mysql_fetch_array($res)) {
$result['id'] = $row['id'];
$result['url'] = $row['url'];
$result['secret_key'] = $row['secret_key'];
$result['read'] = $row['read'];
$result['write'] = $row['write'];
$result['enable'] = $row['enable'];
$result['name'] = $row['name'];
$result['state'] = $row['state'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function edit_network_info($id,$name,$url,$secret_key,$read,$write,$enable)
{
mysql_query("UPDATE `networks` SET `url` = '$url',`secret_key` = '$secret_key',`read` = '$read',`write` = '$write',`name` = '$name',`enable` = '$enable' WHERE `id` =$id LIMIT 1 ;");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_write_networks()
{
$res = mysql_query("SELECT * FROM `networks` WHERE `write` = 1");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['url'] = $row['url'];
$result[$count]['secret_key'] = $row['secret_key'];
$result[$count]['read'] = $row['read'];
$result[$count]['write'] = $row['write'];
$result[$count]['enable'] = $row['enable'];
$result[$count]['name'] = $row['name'];
$result[$count]['state'] = $row['state'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_read_networks()
{
$res = mysql_query("SELECT * FROM `networks` WHERE `read` = 1");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['url'] = $row['url'];
$result[$count]['secret_key'] = $row['secret_key'];
$result[$count]['read'] = $row['read'];
$result[$count]['write'] = $row['write'];
$result[$count]['enable'] = $row['enable'];
$result[$count]['name'] = $row['name'];
$result[$count]['state'] = $row['state'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_report_by_key($key)
{
$res = mysql_query("SELECT * FROM `messages` WHERE `key` = '$key' limit 1");
while ($row = mysql_fetch_array($res)) {
$result['id'] = $row['id'];
$result['time'] = $row['time'];
$result['message'] = $row['message'];
$result['reporter_id'] = $row['reporter_id'];
$result['name'] = $row['name'];
$result['key'] = $row['key'];
$result['network_id'] = $row['network_id'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function add_to_send_queue($network_id,$report_id,$time,$state)
{
mysql_query("INSERT INTO `send_queue` (`network_id` ,`report_id` ,`time` ,`state`)VALUES ('$network_id', '$report_id', '$time', '$state');");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function microtime_to_string($microtime)
{
$current_micotime = getmicrotime();
$res = $current_micotime - $microtime;
switch ($res) {
case $res<60 :
$result = $res." seconds";
break;
case $res>60 and $res<3600 :
$result = floor($res / 60) ." minutes";
break;
case $res>3600 and $res<86400:
$result = floor($res / 3600) ." hours";
break;
case $res>86400 :
$result = date('F jS, Y, H:i:s', $microtime);
break;
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_send_queue($limit)
{
$res = mysql_query("SELECT * FROM `send_queue` limit $limit");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
$result[$count]['id'] = $row['id'];
$result[$count]['network_id'] = $row['network_id'];
$result[$count]['report_id'] = $row['report_id'];
$result[$count]['time'] = $row['time'];
$result[$count]['state'] = $row['state'];
}
return $result;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_client_ip()
{
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
$ip=$_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
$ip=$_SERVER["HTTP_CLIENT_IP"] . ' ';
}
return $ip;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function get_web_page($url)
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
function generate_network_key($network_id)
{
$new_key = generate_random_string('50');
$last_change = getmicrotime();
mysql_query("UPDATE `networks` SET `secret_key` = '$new_key',`last_key_change` = '$last_change' WHERE `id` = '$network_id' LIMIT 1 ;");
}
/////////////////////////////////////////////////////////////////////
?>