<?php

include_once "function.php";
include "common/thumbnail.class.php";
/*image upload*/
$img_name = $_FILES['image']['name'];
$img_type = $_FILES['image']['type'];
//echo $img_type;
if ($img_type == 'image/png' || $img_type == 'image/jpeg' || $img_type == 'image/gif' || $img_type == 'image/bmp') {
    $image = rand(0, 999) . time() . $img_name;
    $path = "../uploads/question_images/" . $image;
    $tmp_name = $_FILES['image']['tmp_name'];
    if (move_uploaded_file($tmp_name, $path)) {
        $pic = new Thumbnail();
        $pic->filename = "../uploads/question_images/" . $image;
        $pic->filename2 = "../uploads/question_images/question_images_thumb/thumb_" . $image;
        $pic->maxW = 400;
        $pic->maxH = 350;
        $pic->SetNewWH();
        $pic->makeNew();
        $pic->FinirPImage();
        $image1 = "thumb_" . $image;
        $httphost = $_SERVER['HTTP_ORIGIN'];
        if ($_SERVER['HTTP_ORIGIN'] == 'http://localhost') {
            $httphost = $_SERVER['HTTP_ORIGIN'] . '/Learnquixweb/';
        }
        die(json_encode(array("status" => "1", "server" => $httphost, "img_path" => $httphost . "/uploads/question_images/question_images_thumb/" . $image1)));
    }
}
die(json_encode(array("status" => "0")));
 public function fbauth($me)
 {
     //echo "it makes it here";
     session_start();
     unset($authData);
     $_SESSION[SESSION_AUTHDATA] = $authData;
     //$id=$me['id'];
     $db = new Database();
     $db->connect();
     $where = "`id`='" . $me['id'] . "' ";
     $db->select('`user`', '`id`,`name`,`accountstatusid`, `password`, `schoolid`', $where);
     $result = $db->getresult();
     extract($result);
     //echo "its trying to auth with id". $id;
     if ($accountstatusid > 1 || $accountstatusid == -1) {
         $revports = FBData::$user_ports;
         $revports = array_flip($revports);
         if ($revports[$id] > 0) {
             $fbid = $revports[$id];
         } else {
             $fbid = $id;
         }
         $authData = array();
         $authData['name'] = $name;
         $authData['id'] = $id;
         $authData['fbid'] = $fbid;
         $authData['accountstatus'] = $accountstatusid;
         $authData['schoolid'] = $schoolid;
         $authData['fblogin'] = TRUE;
         $authData['logoutUrl'] = $me['fblogout'];
         $_SESSION['authData'] = $authData;
         session_write_close();
         $friends = $me['friends'];
         $importfriendquery = "INSERT IGNORE INTO user(`id`,`name`,`password`,`accountstatusid`,`schoolid`) VALUES ";
         foreach ($friends as $arrid => $frienddata) {
             $fname = str_replace("'", "&#39;", $frienddata['name']);
             $importfriendquery .= "('" . $frienddata['id'] . "','" . $fname . "','fblogin','1','" . $schoolid . "')";
             if (!next($friends) === FALSE) {
                 $importfriendquery .= ", ";
             }
         }
         $dbimport = new Database();
         $dbimport->connect();
         $dbimport->query($importfriendquery);
         if ($this->newuser == true) {
             return true;
         } else {
             return false;
         }
     } else {
         //register the new user automatically
         $affs = $me['affiliations'];
         $schoolid = -1;
         $affs2 = array_reverse($affs);
         if (is_array($affs2[0])) {
             foreach ($affs2 as $indaff) {
                 if ($indaff['type'] == "college") {
                     $schoolid = $indaff['nid'];
                     $schoolname = $indaff['name'];
                     break;
                 }
             }
         }
         if ($me['id'] == "100002408771848") {
             $schoolid = -1;
         }
         if ($schoolid == "16777217") {
             $schoolid = 1;
         }
         $checkschool = "select name from school where id='" . $schoolid . "'";
         $db = new Database();
         $db->query($checkschool);
         $result = $db->getresult();
         if (strlen($result['name'] < 1)) {
             $insertschool = "insert into school set id='" . $schoolid . "', name='" . $schoolname . "'";
             $db->query($insertschool);
         }
         if ($me['gender'] == 'male') {
             $sex = 2;
         } else {
             $sex = 1;
         }
         $defaultpicture = "http://graph.facebook.com/" . $me['id'] . "/picture?type=large";
         $contents = file_get_contents($defaultpicture);
         $target_path = "/home/newportb/public_html/photos/";
         $firstsubset = rand(100000, 9999999);
         $secondsubset = rand(100000, 9999999);
         $thirdsubset = rand(1000000, 99989999);
         $generated_path = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_n.jpg";
         while (file_exists($generated_path)) {
             $firstsubset = rand(100000, 9999999);
             $secondsubset = rand(100000, 9999999);
             $thirdsubset = rand(1000000, 99989999);
             $generated_path = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_n.jpg";
         }
         $full_target_path = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_f.jpg";
         file_put_contents($full_target_path, $contents);
         //NORMAL
         $pic3 = new Thumbnail();
         $pic3->filename = $full_target_path;
         $pic3->filename2 = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_n.jpg";
         $pic3->maxW = 170;
         $pic3->maxH = 400;
         $pic3->Text = "";
         $pic3->SetNewWH();
         $pic3->MakeNew();
         $pic3->FinirPImage();
         //SEARCH
         $pic2 = new Thumbnail();
         $pic2->filename = $full_target_path;
         $pic2->filename2 = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_s.jpg";
         $pic2->maxW = 100;
         $pic2->maxH = 200;
         $pic2->Text = "";
         $pic2->SetNewWH();
         $pic2->MakeNew();
         $pic2->FinirPImage();
         //THUMBNAIL
         $pic2 = new Thumbnail();
         $pic2->filename = $full_target_path;
         $pic2->filename2 = $target_path . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "_t.jpg";
         $pic2->maxW = 50;
         $pic2->maxH = 50;
         $pic2->Text = "";
         $pic2->SetNewWH();
         $pic2->MakeNew();
         $pic2->FinirPImage();
         unlink($full_target_path);
         $registerdate = date("Y-m-d");
         $dtime = date('Y-m-d H:i:s');
         $me['name'] = str_replace("'", "&#39;", $me['name']);
         if ($accountstatusid != 1) {
             $newfbuser = "******" . $me['id'] . "', '" . $me['email'] . "', 'fblogin', '2', '" . $registerdate . "', '" . $me['name'] . "', '" . $schoolid . "', '', '', '', '1', '" . $sex . "', '', '" . $me['birthday'] . "', '', '', '', '', '');";
         } else {
             $newfbuser = "******" . $me['email'] . "',\n\t\t\t\t\t`accountstatusid`='2', \n\t\t\t\t\t`registerdate`='" . $registerdate . "', \n\t\t\t\t\t`schoolid`='" . $schoolid . "', \n\t\t\t\t\t`schoolstatusid`='1', \n\t\t\t\t\t`sexid`='" . $sex . "', \n\t\t\t\t\t`birthday`='" . $me['birthday'] . "'\n\t\t\t\t\tWHERE `id`='" . $me['id'] . "' LIMIT 1\n\t\t\t\t\t";
         }
         $updateprofile = "insert into `profileupdates` (`id`, `userid`, `timestamp`) VALUES (NULL, '" . $me['id'] . "', '" . $dtime . "');";
         $setpicture = "insert into `picture` (`userid`, `albumid`, `link`) VALUES ('" . $me['id'] . "', '0', '" . $firstsubset . "_" . $secondsubset . "_" . $thirdsubset . "');";
         $site = new site();
         if ($site->get_setting('email_alerts') == 1) {
             $body = "Name: " . $me['name'] . ".<br>Email: " . $me['email'];
             email::send('*****@*****.**', 'New Registration', $body);
         }
         $db->query($newfbuser);
         $db->query($updateprofile);
         $db->query($setpicture);
         $this->newuser = true;
         $this->fbauth($me);
         return true;
     }
 }