<?php
include 'config/config.php';
include 'stats/seostats.php';
libxml_use_internal_errors(true);

function rootpath()
{
if(!filter_var($_SERVER['SERVER_NAME'], FILTER_VALIDATE_IP))
{
if(substr($_SERVER['SERVER_NAME'],0,4) != "www.")
$server = "www." . $_SERVER['SERVER_NAME'];
else
$server = $_SERVER['SERVER_NAME'];
}
else
{
$server = $_SERVER['SERVER_NAME'];
}
$root = 'http://' . $server . dirname($_SERVER['SCRIPT_NAME']);
if(substr($root, -1)=="/")
return ('http://' . $server);
else
return $root;
}
function get_domain_description($url)
{
$match = "select * from stats where url='$url'"; 
$qry = mysql_query($match);
$row = mysql_fetch_array($qry);
$description = $url . " has estimated worth of " . $row['worth'] . " $ it has google page rank of " . $row['pagerank'] . " has global traffic rank " . $row['alexarank'] . " and estimated " . $row['backlinks'] . " websites linking in while it has page speed score of " . $row['pagespeed'] . " out of 100";
return $description;
}
function is_alive($url)
{
$handles = curl_init($url);
curl_setopt($handles, CURLOPT_NOBODY, true);
curl_exec($handles);
$resultat = curl_getinfo($handles, CURLINFO_HTTP_CODE);
if(substr($resultat, 0, 1 ) === "0")
return true;
else
return false;
}

function get_speed_score($url)
{
$contents = file_get_contents_curl('https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://' . $url);
$json = json_decode($contents);
if($json->score)
return $json->score;
else return 0;
}

function trim_string($string)
{
$string = (strlen($string) > 25) ? substr($string,0,25).'...' : $string;
return $string;
}
function update_settings($title,$description,$keywords,$color)
{
$update_query = "UPDATE settings SET title='". mysql_real_escape_string($title)."',description='".mysql_real_escape_string($description)."',meta_tags='".mysql_real_escape_string($keywords) ."',color='" . $color . "'";
mysql_query($update_query);
}
function update_user($username,$password,$email)
{
if($password!="")
$update_query = "UPDATE settings SET username='".$username."',password='".$password."',email='".$email . "'";
else
$update_query = "UPDATE settings SET username='".$username."',email='".$email."'";
$qry = mysql_query($update_query);
return true;
}
function valid_facebook_url($field)
{
    if(!preg_match('/^([\w\-\.]*)/', $field))
	{
        return false;
    }
    return true;
}
function valid_twitter_username($field){
    if(!preg_match('/^[A-Za-z0-9_]+$/', $field)){
        return false;
    }
    return true;
}
function valid_google_url($field){
   if(!preg_match('/^([0-9]{1,21})$/', $field)){
        return false;
    }
    return true;
}
function retColor()
{
$match = "select color from settings"; 
$qry = mysql_query($match);
$row = mysql_fetch_array($qry);
return ($row['color']);
}
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 innerHTML(DOMNode $node)
{
  $doc = new DOMDocument();
  foreach ($node->childNodes as $child) {
    $doc->appendChild($doc->importNode($child, true));
  }
  return $doc->saveHTML();
}
function get_twitter()
{
$match = "select twitter from social_profiles"; 
$qry = mysql_query($match);
$row = mysql_fetch_array($qry);
return ($row['twitter']);
}
function get_facebook()
{
$match = "select facebook from social_profiles"; 
$qry = mysql_query($match);
$row = mysql_fetch_array($qry);
return ($row['facebook']);
}
function get_google()
{
$match = "select google from social_profiles"; 
$qry = mysql_query($match);
$row = mysql_fetch_array($qry);
return ($row['google']);
}
function show_medrec_ad()
{
$match_ad= "select medium_rect from ads"; 
$qry_ad = mysql_query($match_ad);
$row_ad = mysql_fetch_array($qry_ad);
return ($row_ad['medium_rect']);
}
function update_ads($banner,$medrec1,$medrec2)
{
$update_query = "UPDATE ads SET banner='".$banner."',medium_rect='".$medrec1."',medium_rect2='".$medrec2."'";
mysql_query($update_query);
}
function update_social($facebook,$twitter,$google)
{
$update_query = "UPDATE social_profiles SET facebook='".$facebook."',twitter='".$twitter."',google='".$google."'";
mysql_query($update_query);
}
function show_medrec2_ad()
{
$match_ad= "select medium_rect2 from ads"; 
$qry_ad = mysql_query($match_ad);
$row_ad = mysql_fetch_array($qry_ad);
return ($row_ad['medium_rect2']);
}
function show_banner_ad()
{
$match_ad= "select banner from ads"; 
$qry_ad = mysql_query($match_ad);
$row_ad = mysql_fetch_array($qry_ad);
return ($row_ad['banner']);
}
function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function get_google_count($url)
{
    if ($url) {
        $result_string = file_get_contents("http://www.google.com/search?q=site%3a" . $url);
       if (strpos($result_string, "did not match any documents") !== false) {
            return intval(0);
        } else {
            $match = preg_match("/about ([0-9,]{0,12})/i", $result_string, $matches);
            if (!empty($matches))
            return intval(str_replace(",", "", $matches[1]));
			else
			return get_google_count2($url);
        }
    }
}
function get_google_count2($url)
{
$html_google_results = file_get_contents_curl('http://www.online-translator.com/url/translation/?autolink=yes&direction=en&template=General&sourceURL=http%3a%2f%2fwww.google.com%2fsearch%3fq%3dsite:' . $url);
$document = new DOMDocument(); 
$document->loadHTML($html_google_results);
$selector = new DOMXPath($document);
$anchors = $selector->query('/html/body//div[@id="resultStats"]');
foreach ($anchors as $node) 
{
	$google_results = trim(str_replace(",", "", innerHTML($node)));
}
$results = explode(' ',$google_results);
foreach($results as $result) {
if(Is_numeric(trim($result)))
return trim($result);
}
return 0;
}
function alexa_bounce($url)
{
$html_alexa_results = file_get_contents_curl('http://www.alexa.com/siteinfo/' . urlencode($url));
$document_alexa = new DOMDocument(); 
$document_alexa->loadHTML($html_alexa_results);
$selector_alexa = new DOMXPath($document_alexa); 
$content_alexa_bounce = $selector_alexa->query('/html/body//strong[@class="font-big2 valign"]');
foreach($content_alexa_bounce as $bounce_alexa) 
{
	$bounce_rate = innerHTML($bounce_alexa);
	break;
}
$bounce_rate = trim(str_replace('%','', $bounce_rate));
if(!Is_numeric($bounce_rate))
$bounce_rate=rand(40,70);
return $bounce_rate;
}
function alexa_links($url)
{
$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
$links=0;
if($xml->SD[0])
$links=(int)$xml->SD[0]->LINKSIN->attributes()->NUM;
if(!$links>0)
$links=alexa_links1($url);
return $links;
}
function alexa_links1($url)
{
$html_alexa_results = file_get_contents_curl('http://www.alexa.com/siteinfo/' . urlencode($url));
$document_alexa = new DOMDocument(); 
$document_alexa->loadHTML($html_alexa_results);
$selector_alexa = new DOMXPath($document_alexa); 
$anchors_alexa_links = $selector_alexa->query('/html/body//span[@class="font-5 float-right"]');
foreach($anchors_alexa_links as $node_alexa) 
{
	$alexa_links = innerHTML($node_alexa);
}
$alexa_links = trim(str_replace(",","",$alexa_links));
if(!Is_numeric($alexa_links))
$alexa_links=0;
return $alexa_links;
}
function calculate_worth($index_count,$dmoz,$pagerank,$alexarank,$pagespeed,$backlinks,$bounce_rate,$social)
{
$worth = 15;
if($backlinks<100)
$worth +=$backlinks*3;
if($backlinks>100 && $backlinks<500)
$worth +=$backlinks*5;
if($backlinks>500 && $backlinks<1000)
$worth +=$backlinks*10;
if($backlinks>1000 && $backlinks<10000)
$worth +=$backlinks*15;
if($backlinks>10000 && $backlinks<100000)
$worth +=$backlinks*30;
if($backlinks>100000)
$worth +=$backlinks*60;
if($social>1 &&  $social<1000)
$worth +=$social*5;
if($social>1000 && $social<10000)
$worth +=$social*10;
if($social>10000)
$worth +=$social*20;
$worth +=$index_count*0.5;
if($pagerank<1)
$pagerank=1;
if($pagespeed<1)
$pagespeed=1;
if($pagespeed<50)
$pagespeed = $pagespeed/100;
else
$pagespeed = $pagespeed/50;
if($bounce_rate<1)
$bounce_rate=100;
if($bounce_rate>40)
$bounce_rate = 100/$bounce_rate;
else
$bounce_rate = 60/$bounce_rate;
if($alexarank>0 && $alexarank<100000)
$alexarank = 100000/$alexarank;
else
$alexarank=1;
$worth +=(($dmoz*$pagerank*$pagespeed*$bounce_rate)*$alexarank);
return intval($worth*2);
}
function get_googleplus_count($url)
{
$html_googleplus = file_get_contents_curl('https://plusone.google.com/u/0/_/+1/fastbutton?count=true&url=http://www.' . urlencode($url));
$document = new DOMDocument(); 
$document->loadHTML($html_googleplus);
$selector = new DOMXPath($document); 
$anchors = $selector->query('/html/body//div[@id="aggregateCount"]');
foreach ($anchors as $node) {
	$results = innerHTML($node);
}
$results = trim(strtolower($results));
if(substr($results,-1)=='m' || substr($results,-1)=='k') {
$unit = substr($results,-1);
$number = substr($results, 0, -1);
if($unit=="k")
$results = $number*1000;
else if($unit=="m")
$results = $number*1000000;
}
return $results;
}
function get_pinterest($url) {
$return_data = file_get_contents_curl('http://api.pinterest.com/v1/urls/count.json?url=http://www.'.$url);
$json_string = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $return_data);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
function bing_results_count($url)
{
$html_bing_results = file_get_contents_curl('http://www.bing.com/search?q=site%3A' . $url .  '&go=&qs=n&sk=&sc=8-20&first=$start&FORM=QBLH');
$document = new DOMDocument(); 
$document->loadHTML($html_bing_results);
$selector = new DOMXPath($document);
$anchors = $selector->query('/html/body//span[@class="sb_count"]');
foreach ($anchors as $node) 
{
	$bing_results = innerHTML($node);
}
    $bing_results = str_replace("results","",$bing_results);
	$bing_results = str_replace(",","",$bing_results);
	if(trim($bing_results)!="")
return $bing_results;
else
return 0;
}
function dmoz_list($url)
{
$dmoz_result = file_get_contents_curl('www.dmoz.org/search/?q=' . urlencode($url));
	$dmoz_list = "No";
	$dmoz_tofind = 'http://' . $url;
	$dmoz_tofind_dot = '.' . $url;
	$dmoz_tofind_www = 'http://www.' . $url;
	if(strpos($dmoz_result,$dmoz_tofind) || strpos($dmoz_result,$dmoz_tofind_www) || strpos($dmoz_result,$dmoz_tofind_dot))
	$dmoz_list = "Yes";
	return $dmoz_list;
}
function force_update_stats($url)
{
$date_updated = date("Y-m-d");
$seostats = new SEOstats("http://www.".$url);
$google = $seostats->Google();
$social = $seostats->Social();
$bing_results = 0;
$google_results = get_google_count($url);
$fb = $social->getFacebookShares();
if($fb)
{
$fblikes = $fb['like_count'];
$fbshares = $fb['share_count'];
$fbcomments = $fb['comment_count'];
}
if(!Is_numeric($fblikes))
$fblikes = 0;
if(!Is_numeric($fbshares))
$fbshares = 0;
if(!Is_numeric($fbcomments))
$fbcomments = 0;
$bounce_rate = alexa_bounce($url);
$alexa_links = alexa_links($url);
$backlinks = $alexa_links;
$pagerank = $google->getPageRank();
if(!Is_numeric($pagerank))
$pagerank = 0;
$alexarank = get_alexa_rank($url);
$pagespeed = get_speed_score($url);
if(!Is_numeric($pagespeed))
$pagespeed = 0;
$googleplus = get_googleplus_count($url);
if(!Is_numeric($googleplus))
$googleplus = 0;
$tweets = $social->getTwitterShares();
if(!Is_numeric($tweets))
$tweets = 0;
$linkedin = $social->getLinkedInShares();
if(!Is_numeric($linkedin))
$linkedin = 0;
$pins = get_pinterest($url);
if(!Is_numeric($pins))
$pins = 0;
$stumble = $social->getStumbleUponShares();
if(!Is_numeric($stumble))
$stumble = 0;
$delicious = $social->getDeliciousShares();
if(!Is_numeric($delicious))
$delicious = 0;
$vk = $social->getVKontakteShares();
if(!Is_numeric($vk))
$vk = 0;
$dmoz_list = dmoz_list($url);
if($dmoz_list=="Yes")
$dmoz_worth=2;
else
$dmoz_worth=1;
$index_count = $google_results+$bing_results;
$social = $googleplus+$fblikes+$fbshares+$fbcomments+$tweets+$linkedin+$pins+$stumble+$delicious+$vk;
$worth = calculate_worth($index_count,$dmoz_worth,$pagerank,$alexarank,$pagespeed,$backlinks,$bounce_rate,$social);
if($worth>0)
{
$update_stats = "UPDATE stats SET date_updated='$date_updated',googleindex=$google_results,bingindex=$bing_results,dmoz='$dmoz_list',pagerank=$pagerank,alexarank=$alexarank,pagespeed=$pagespeed,backlinks=$backlinks,bouncerate=$bounce_rate,googleplus=$googleplus,facebooklikes=$fblikes,facebookshares=$fbshares,facebookcomments=$fbcomments,tweets=$tweets,linkedin=$linkedin,pinterest=$pins,stumbleupon=$stumble,delicious=$delicious,vkontakte=$vk,worth=$worth WHERE url='$url'";
mysql_query($update_stats) or die("error in update query " . mysql_error());
}
}
function update_stats($url)
{
$match_url = "select date_updated from stats where url='$url'"; 
$qry_url = mysql_query($match_url);
$num_rows_count_url = mysql_num_rows($qry_url);
if ($num_rows_count_url> 0) 
{
$row_date_updated = mysql_fetch_array($qry_url);
$date_updated = date('Y-m-d', strtotime("+1 months", strtotime($row_date_updated['date_updated'])));
if(strtotime(date("Y-m-d"))>strtotime($date_updated))
{
$date_updated = date("Y-m-d");
$seostats = new SEOstats("http://www.".$url);
$google = $seostats->Google();
$social = $seostats->Social();
$bing_results = 0;
$google_results = get_google_count($url);
$fb = $social->getFacebookShares();
if($fb)
{
$fblikes = $fb['like_count'];
$fbshares = $fb['share_count'];
$fbcomments = $fb['comment_count'];
}
if(!Is_numeric($fblikes))
$fblikes = 0;
if(!Is_numeric($fbshares))
$fbshares = 0;
if(!Is_numeric($fbcomments))
$fbcomments = 0;
$bounce_rate = alexa_bounce($url);
$alexa_links = alexa_links($url);
$backlinks = $alexa_links;
$pagerank = $google->getPageRank();
if(!Is_numeric($pagerank))
$pagerank = 0;
$alexarank = get_alexa_rank($url);
$pagespeed = get_speed_score($url);
if(!Is_numeric($pagespeed))
$pagespeed = 0;
$googleplus = get_googleplus_count($url);
if(!Is_numeric($googleplus))
$googleplus = 0;
$tweets = $social->getTwitterShares();
if(!Is_numeric($tweets))
$tweets = 0;
$linkedin = $social->getLinkedInShares();
if(!Is_numeric($linkedin))
$linkedin = 0;
$pins = get_pinterest($url);
if(!Is_numeric($pins))
$pins = 0;
$stumble = $social->getStumbleUponShares();
if(!Is_numeric($stumble))
$stumble = 0;
$delicious = $social->getDeliciousShares();
if(!Is_numeric($delicious))
$delicious = 0;
$vk = $social->getVKontakteShares();
if(!Is_numeric($vk))
$vk = 0;
$dmoz_list = dmoz_list($url);
if($dmoz_list=="Yes")
$dmoz_worth=2;
else
$dmoz_worth=1;
$index_count = $google_results+$bing_results;
$social = $googleplus+$fblikes+$fbshares+$fbcomments+$tweets+$linkedin+$pins+$stumble+$delicious+$vk;
$worth = calculate_worth($index_count,$dmoz_worth,$pagerank,$alexarank,$pagespeed,$backlinks,$bounce_rate,$social);
if($worth>0)
{
$update_stats = "UPDATE stats SET date_updated='$date_updated',googleindex=$google_results,bingindex=$bing_results,dmoz='$dmoz_list',pagerank=$pagerank,alexarank=$alexarank,pagespeed=$pagespeed,backlinks=$backlinks,bouncerate=$bounce_rate,googleplus=$googleplus,facebooklikes=$fblikes,facebookshares=$fbshares,facebookcomments=$fbcomments,tweets=$tweets,linkedin=$linkedin,pinterest=$pins,stumbleupon=$stumble,delicious=$delicious,vkontakte=$vk,worth=$worth WHERE url='$url'";
mysql_query($update_stats) or die("error in update query " . mysql_error());
}
}
}
else
{
$date_updated = date("Y-m-d");
$seostats = new SEOstats("http://www.".$url);
$google = $seostats->Google();
$social = $seostats->Social();
$bing_results = 0;
$google_results = get_google_count($url);
$fb = $social->getFacebookShares();
$fblikes = $fb['like_count'];
$fbshares = $fb['share_count'];
$fbcomments = $fb['comment_count'];
if(!Is_numeric($fblikes))
$fblikes = 0;
if(!Is_numeric($fbshares))
$fbshares = 0;
if(!Is_numeric($fbcomments))
$fbcomments = 0;
$bounce_rate = alexa_bounce($url);
$alexa_links = alexa_links($url);
$backlinks = $alexa_links;
$pagerank = $google->getPageRank();
if(!Is_numeric($pagerank))
$pagerank = 0;
$alexarank = get_alexa_rank($url);
$pagespeed = $google->getPagespeedScore();
if(!Is_numeric($pagespeed))
$pagespeed = 0;
$googleplus = get_googleplus_count($url);
if(!Is_numeric($googleplus))
$googleplus = 0;
$tweets = $social->getTwitterShares();
if(!Is_numeric($tweets))
$tweets = 0;
$linkedin = $social->getLinkedInShares();
if(!Is_numeric($linkedin))
$linkedin = 0;
$pins = get_pinterest($url);
if(!Is_numeric($pins))
$pins = 0;
$stumble = $social->getStumbleUponShares();
if(!Is_numeric($stumble))
$stumble = 0;
$delicious = $social->getDeliciousShares();
if(!Is_numeric($delicious))
$delicious = 0;
$vk = $social->getVKontakteShares();
if(!Is_numeric($vk))
$vk = 0;
$dmoz_list = dmoz_list($url);
if($dmoz_list=="Yes")
$dmoz_worth=2;
else
$dmoz_worth=1;
$index_count = $google_results+$bing_results;
$social = $googleplus+$fblikes+$fbshares+$fbcomments+$tweets+$linkedin+$pins+$stumble+$delicious+$vk;
$worth = calculate_worth($index_count,$dmoz_worth,$pagerank,$alexarank,$pagespeed,$backlinks,$bounce_rate,$social);
$insert_stats = "insert into stats(`date_updated` ,`url` ,`googleindex` ,`bingindex` ,`dmoz` ,`pagerank` ,`alexarank` ,`pagespeed` ,`backlinks` ,`bouncerate` ,`googleplus` ,`facebooklikes` ,`facebookshares` ,`facebookcomments` ,`tweets` ,`linkedin` ,`pinterest` ,`stumbleupon` ,`delicious` ,`vkontakte` ,`worth`) values('$date_updated','$url',$google_results,$bing_results,'$dmoz_list',$pagerank,$alexarank,$pagespeed,$backlinks,$bounce_rate,$googleplus,$fblikes,$fbshares,$fbcomments,$tweets,$linkedin,$pins,$stumble,$delicious,$vk,$worth)";
mysql_query($insert_stats) or die("error in insert query " . mysql_error());
}
}
function sencrypt($text)
{
 return strtr(base64_encode($text), '+/=', '-_,');
} 
function sdecrypt($text)
{
return base64_decode(strtr($text, '-_,', '+/='));
}
function reset_pass($email)
{
$match_user = "select username from settings where email='" . $email . "'"; 
$qry_user = mysql_query($match_user);
$row_user = mysql_fetch_array($qry_user);
$username = $row_user['username'];
$password = genPassword();
send_email(get_admin_email(),$email,"Password Received","Your Login Details Updated\nUsername: " . $username . "\nYour new password is: " . $password . "\nLogin Here: " . rootpath());
$update_query = "UPDATE settings SET password='". md5($password) ."' WHERE email='".$email."'";
$qry = mysql_query($update_query);
}
function getdomain($url) { 
$parsed = parse_url($url); 
return str_replace('www.','', strtolower($parsed['host'])); 
}
function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}
function get_alexa_rank($url)
{
$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
$rank=0;
if($xml->SD[1])
{
$rank=(int)$xml->SD[1]->POPULARITY->attributes()->TEXT;
return $rank;
}
else
{
return 0;
}
}
function get_title()
{
$match = "select title from settings"; 
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry); 
if ($num_rows > 0)
{
$row = mysql_fetch_array($qry);
return ($row['title']);
}
else
{
return ("Website Value Calculator");
}
}
function get_admin_email()
{
$match = "select email from settings"; 
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry); 
if ($num_rows > 0)
{
$row = mysql_fetch_array($qry);
return ($row['email']);
}
}
function get_description()
{
$match = "select description from settings"; 
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry); 
if ($num_rows > 0)
{
$row = mysql_fetch_array($qry);
return ($row['description']);
}
}
function get_tags()
{
$match = "select meta_tags from settings"; 
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry); 
if ($num_rows > 0)
{
$row = mysql_fetch_array($qry);
return ($row['meta_tags']);
}
}
function valid_url($url)
{
$validation = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) && (preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i", $url));
	
	if($validation)
		return true;
	else
	return false;
}
function send_email($from,$to,$subject,$body)
{
$random_hash = md5(date('r', time()));
$headers = "From: " . get_title() . "<" . $from . "> \r\nReply-To: " . $from;
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
$message = $body;
@mail($to, $subject, $message, $headers );
}
function genPassword() 
{
  $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 8) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
function is_alpha($val)
    {
        return (bool)preg_match("/^([a-zA-Z ])+$/i", $val);
 
    }
	function checkEmail($email){
  return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}

function email_exists($val)
{
$match = "select username from settings where email ='".$val."'"; 
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry); 
if ($num_rows > 0)
return true;
else
return false;
}
function bad_domain($str)
{
$str = strtolower($str);
$list = explode(",",strtolower(file_get_contents(realpath(dirname(__FILE__) . '/..') . "/badwords.txt")));
	foreach($list as $list_word) 
	{
		if (preg_match('/' . trim($list_word) . '/',$str))
			return true;
    }
return false;
}
function get_badwords()
{
$words = strtolower(file_get_contents(realpath(dirname(__FILE__) . '/..')  . "/badwords.txt"));
return $words;
}
function update_badwords($words)
{
$path = realpath(dirname(__FILE__) . '/..') . "/badwords.txt";
$fp = fopen($path, 'w+');
fwrite($fp, $words);
fclose($fp);
}
?>