Your IP : 216.73.216.40


Current Path : /var/www/html/mmishra/irp/irp/ialumni/
Upload File :
Current File : /var/www/html/mmishra/irp/irp/ialumni/functions.inc

<?php
function dbConnect($db) {
        $link = mysql_connect("localhost", "root", "irp2015");
        mysql_select_db($db);
        return $link;
}

function uniq_sid($uid, $pwd) {
        //$epwd = @mcrypt_ecb(MCRYPT_DES, "gravity", $pwd, MCRYPT_ENCRYPT);
        $epwd = base64_encode($pwd);
        //clear entry for ppl who havent done anything for past 20 minutes
        //$result = mysql_query("delete from auth where last_activity < now()-1200");
        srand((double)microtime()*100000000);
        //generate a unique s_id
        do {
                $ch = "";
                for($i=0; $i<20; $i++) $ch = $ch . chr(rand(65, 90));
                //loop until a uniq string is generated
        } while (!mysql_query("insert into auth values('$ch', '$uid','$epwd', NULL)"));
        return $ch;
}

function automailer($email, $subject, $body) {
        //SMTP needs accurate times, and the PHP time zone MUST be set
        //This should be done in your php.ini, but this is how to do it if you don't have access to that

        //Create a new PHPMailer instance
        $mail = new PHPMailer();

        //Tell PHPMailer to use SMTP
        $mail->isSMTP();

        //Tell PHPMailer to enable HTML format
        $mail->isHTML(true);

        //Set the hostname of the mail server
        $mail->Host = 'smtp.gmail.com';

        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 465;

        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'ssl';

        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;

        //Username to use for SMTP authentication - use full email address for gmail
        $mail->Username = "ilight@iiita.ac.in";

        //Password to use for SMTP authentication
        $mail->Password = "irp@2015";

        //Set who the message is to be sent from
        $mail->setFrom('ilight@iiita.ac.in', 'iAlumni@IIITA');

        //Set an alternative reply-to address
        //$mail->addReplyTo('icure@iiita.ac.in', 'iCure@IIITA');

        //Set who the message is to be sent to
        $mail->addAddress($email, $email);
        //$mail->addBCC('mmishra@iiita.ac.in', 'Mmishra');

        //Set the subject line
        $mail->Subject = $subject;

        $mail->Body = $body . "<br><br>iAlumni@IIITA<br><br>
	Login at https://ialumni.iiita.ac.in using LDAP credentials for updated information on alumni.<br><br>
	(Automailer)";
        $mail->WordWrap = 75;

        //send the message, check for errors
        if (!$mail->send()) return 'XX';
        else return 'OK';
}

function localAuth($uid, $pwd) {
        $fullname = 'Alumni';
        if ($uid) {
                $rs = mysql_query("select * from registrations where rollno='$uid' and passwd=password('$pwd')");
                if (mysql_num_rows($rs)) {
                        $o = mysql_fetch_object($rs);
                        $fullname = $o->fullname;
                }
        }
        return $fullname;
}

function ldapAuth($uid, $pwd) {
        if ($uid && $pwd) {
                $ds = ldap_connect("172.31.1.42");
                ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                $a = ldap_search($ds, "dc=iiita,dc=ac,dc=in", "uid=$uid");
                $b = ldap_get_entries($ds, $a);
                $dn = $b[0]["dn"];
                //******************************************************
                $flag = (ldap_bind($ds, $dn, $pwd) ? TRUE : FALSE);
                ldap_close($ds);
        } else $flag = FALSE;

        return $flag;
}

function accessLog($user, $referer, $url, $remoteip) {
        $tbl = 'weblogs';

        if (!$user) $user = 'guest';
        if (!$referer) $referer = 'direct';
        if ($url == '/') $url = 'index.php';

        mysql_query("insert into $tbl(userid,referer,weburl,remoteip,dateaccess)
        values('$user', '$referer', '$url','$remoteip',now())");
}

function user_name($uid) {
        $ds = ldap_connect("172.31.1.42");
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        $z = ldap_bind($ds);
        $a = ldap_search($ds,"dc=iiita,dc=ac,dc=in","(&(uid=$uid)(objectclass=posixAccount))");
        $b = ldap_get_entries($ds,$a);
        $cn = $b[0]["gecos"][0];
        ldap_close($ds);
        return strtoupper($cn);
}

function get_enum_values($table, $field) {
        $rs = mysql_query("show columns from {$table} where field='{$field}'");
        $r = mysql_fetch_object($rs);
	$type = $r->Type;
	preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
	$enum = explode("','", $matches[1]);

	return $enum;
}

function photo_gallery() {
	$fp = fopen ("gallery.inc", "r");
	if (!$fp) {
    	        $images = array("indira-bhawan1.jpg");
	} else {
		$i = 0;
		$images = array();
		while (!feof ($fp)) {
    			$line = fgets ($fp, 1024);
    			//$images = explode(",", $line);
                	if ($line != "\n") $images[$i] = str_replace("\n", "", $line);
			$i++;
		}
	}
	fclose($fp);
	$i--;
	srand((double)microtime()*1000000);
        $no = rand(0, $i);
        $fname = $images[$no];
	echo "<img src='images/$fname' width='430' height='240'></img>";
}

function getIP() {
        //Just get the headers if we can or else use the SERVER global
        if ( function_exists( 'apache_request_headers' ) ) {
                $headers = apache_request_headers();
        } else {
                $headers = $_SERVER;
        }
        //Get the forwarded IP if it exists
        if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
                $the_ip = $headers['X-Forwarded-For'];
        } elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )) {
                $the_ip = $headers['HTTP_X_FORWARDED_FOR'];
        } else {
                $the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
        }
        return $the_ip;

}

function upload_file($basename) {

	if ($basename && move_uploaded_file($_FILES['userfile']['tmp_name'], "attachments/$basename")) {
                //shell_exec("chmod 755 "attachments/$basename");
	}
}

function generate_file($file, $str) {	
        if (!file_exists($file)) {
                touch ($file);
        }
        if (!$handle = fopen ($file, 'w+')) {
		echo "$file file permission error";
	} else if (is_writable($file)) {
        	if (fwrite ($handle, $str) === FALSE) {
			echo "Can't write to $file";
		}
	}
        fclose($handle);
        $out = shell_exec("chmod 757 $file");
}

function addto_gallery($fname){
        $str = shell_exec("cat gallery.inc") . "\n";
        //if not already in gallery
	if (!strpos($str, $fname)) {
		$str .= $fname;
		generate_file("gallery.inc", $str);
	}
}

function getFileExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
}

function htmlsafe($str) {
        return trim(htmlspecialchars(stripslashes($str), ENT_QUOTES));
}

function unhtmlentities ($string) {
    $trans_tbl = get_html_translation_table (HTML_ENTITIES);
    $trans_tbl = array_flip ($trans_tbl);
    return strtr ($string, $trans_tbl);
}


function previous_month() {
        $pmonth = (int)(date("n"));
        if ($pmonth == 0) {
                $pmonth = 12;
                $pyear = date("Y") - 1;
        } else
                $pyear = date("Y");
        return date("M-Y",mktime(0,0,0,$pmonth,0,$pyear));
}

function previous_month1($cmonth) {
        $pmonth = (int)(date("n"));
        if ($pmonth == 0) {
                $pmonth = 12;
                $pyear = date("Y",strtotime($cmonth)) - 1;
        } else
                $pyear = date("Y",strtotime($cmonth));
        return date("M-Y",mktime(0,0,0,$pmonth,0,$pyear));
}

function next_month() {
        $nmonth = (int)(date("n"));
        if ($nmonth == 12) {
                $nmonth = 0;
                $nyear = date("Y") + 1;
        } else
                $nyear = date("Y");

        $nmonth = $nmonth + 1;
        return date("Y-m-d",mktime(0,0,0,$nmonth,25,$nyear));
}

?>