getConnection(); $server = $_SERVER['SERVER_NAME']; $IS_PROD = 0; if ( $server == 'www.gavilan.edu') { $IS_PROD = 1; } date_default_timezone_set('America/Los_Angeles'); // 1. lookups, like a username function single_row_select($qry, $j) { global $c; $r = mysqli_query($c, $qry); d_err($qry); $e = mysqli_error($c); if($e) { d_err("sql error: " . $e ); } if (!$r) { return $r; } //echo("\n\n\n"); $a = mysqli_fetch_assoc($r); $e = mysqli_error($c); if($e) { d_err("sql error: " . $e ); } if (! $j) { return $a; } return json_encode($a); } // 1a. inserts function single_row_insert($qry, $j) { global $c; error_log($qry, 0); $r = mysqli_query($c, $qry); //d_err($qry); //$e = mysqli_error($c); if($e) { d_err("sql error: " . $e ); } $new_id = mysqli_insert_id($c); return $new_id; } /*$a = mysqli_fetch_assoc($c); $e = mysqli_error($c); if($e) { d_err("sql error: " . $e ); } if (! $j) { return $a; } return json_encode($a); } */ // 1b. updates function single_row_update($qry, $j) { global $c; $r = mysqli_query($c, $qry); return 1; } // 2. grid or fancier joins, like get all sessions, rosters, todos, etc function multi_row_select($qry, $j) { global $c; $rows = array(); $result = mysqli_query($c, $qry); while($r = mysqli_fetch_assoc($result)) { $rows[] = $r; } if (! $j) { return $rows; } return json_encode( $rows); } // 4. Plain query function query($qry) { global $c; return mysqli_query($c, $qry); } function d_err($s) { global $DEBUG; if ($DEBUG) { p($s); } } // 3. Check if an entry exists function does_exist($qry, $full_record) { global $c; $r = mysqli_query($c, $qry); $a = mysqli_num_rows($r); if (! $a ) { return 0; } $row = mysqli_fetch_array($r, MYSQLI_NUM); $id = $row[0]; // getting the id of that which exists... assuming first column has it. $e = mysqli_error($c); if($e) { d_err("sql error: " . $e); } if ($a && $full_record) { return $row; } if ($a) { return $id; } return 0; } function dumpit($var) { ob_start(); var_dump($var); $a=ob_get_contents(); ob_end_clean(); $a = preg_replace('~[\r\n]+~', '', $a); return $a; } function insert_or_get_browser($b) { global $c; $BROWSER = $b; // ok($b); $existing = does_exist( "SELECT id FROM www_browsers WHERE string='$BROWSER'", 0); if ($existing) { return $existing; } else { $q = "INSERT INTO www_browsers (string) VALUES ('$BROWSER')"; single_row_insert($q,0,$c); return does_exist( "SELECT id FROM www_browsers WHERE string='$BROWSER'",0,$c); } } // // // // Log everything! function log_it($action, $user=860) { global $USER, $USER_NAME, $USER_GOO, $USER_EMAIL, $c; if (! $USER) { $USER = array( 'user_id'=>'unknown', 'id'=>-1 ); } $user_browser = $_SERVER['HTTP_USER_AGENT']; $user_ip = $_SERVER['REMOTE_ADDR']; $BROWSER = insert_or_get_browser($user_browser); //$ACTION = ok($action) . " / " . dumpit($USER) . " / " . $USER_GOO . " / " . $USER_EMAIL; $ACTION = $action . " / " . dumpit($USER) . " / " . $USER_GOO . " / " . $USER_EMAIL; //$persid = $USER->id; //if (! $persid) { $persid = -1; } $qupdate = "INSERT INTO gavi_logs SET personnel_id='{$user}', action='{$ACTION}',name='{$USER_NAME}',browser=$BROWSER, ip='$user_ip'"; //print_r($qupdate); single_row_insert($qupdate,0,$c); return array("result"=>"success","action"=>"logged","query"=>$qupdate,"err"=>mysqli_error($c)); } /*session_destroy(); echo "ending session"; exit(); */ $auth_script = "mAuth.php"; log_it($IS_PROD); //if (file_exists($auth_script)) { if ($IS_PROD) { require_once($auth_script); $USER_TYPE = $attributes['Roles'][0]; $USER_GOO = $attributes['uid'][0]; $USER_EMAIL = $attributes['EmailGC'][0]; log_it("mauth returned user_type=$USER_TYPE, user_goo=$USER_GOO, user_email=$USER_EMAIL."); session_start(); $SESSION_ID = session_id(); log_it("new session: " . $SESSION_ID); //if ( $SESSION_ID == '' ) { // session_status() == PHP_SESSION_ACTIVE // newer php uses this if ( session_status() == PHP_SESSION_ACTIVE ) { // // newer php uses this $_SESSION['USER_TYPE'] = $USER_TYPE; $_SESSION['USER_GOO'] = $USER_GOO; $_SESSION['USER_EMAIL'] = $USER_EMAIL; log_it("Logged in Session id: {$SESSION_ID} / {$USER_NAME} / {$USER_EMAIL} / {$USER_GOO}"); } else { // already logged in.... $USER_TYPE = $_SESSION['USER_TYPE']; $USER_GOO = $_SESSION['USER_GOO']; $USER_EMAIL = $_SESSION['USER_EMAIL']; log_it("Already logged in: Session id: {$SESSION_ID} / {$USER_NAME} / {$USER_EMAIL} / {$USER_GOO}"); } } else { // Handle the error if the file doesn't exist $USER_TYPE = 'FACULTY'; $USER_GOO = 'G00102586'; $USER_EMAIL = 'phowell@my.gavilan.edu'; session_start(); $_SESSION['USER_TYPE'] = $USER_TYPE; $_SESSION['USER_GOO'] = $USER_GOO; $_SESSION['USER_EMAIL'] = $USER_EMAIL; log_it("mauth failed."); } // The SSO check should have happened on the actual page. If it gets // stuck on an api call the app will break. // // SSO Given email is inconsistent. Lookup by GOO // $goo = substr($USER_GOO, 3); $q0 = "SELECT email,name,id FROM conf_users WHERE goo='{$goo}'"; //echo $q0; $temp_usr = single_row_select($q0,0); $USER_NAME = ''; // Is this a "MISSING USER?" Create an account for them. if (is_null($temp_usr)) { $LC_EMAIL = strtolower($USER_EMAIL); $result = single_row_insert("INSERT INTO conf_users (goo, email, name) VALUES ('{$goo}','{$LC_EMAIL}','{$USER_NAME}');"); //echo "INSERT INTO conf_users (goo, email, name) VALUES ('{$goo}','{$LC_EMAIL}','{$USER_NAME}');"; //print_r($result); $USER_NAME = $temp_usr['name']; $USER_EMAIL = strtolower($temp_usr['email']); // Set the $USER global $usr_qry = "SELECT id AS conf_id, goo AS conf_goo, email AS conf_email, name AS conf_name, active AS conf_active FROM conf_users WHERE goo='{$goo}'"; $USER = single_row_select($usr_qry, 0); $logaction = log_it("Made a new conf_users row for {$USER_NAME} / {$LC_EMAIL} / {$USER_GOO}", $result); } else { $USER_NAME = $temp_usr['name']; $USER_EMAIL = strtolower($temp_usr['email']); // Set the $USER global $usr_qry = "SELECT id AS conf_id, goo AS conf_goo, email AS conf_email, name AS conf_name, active AS conf_active FROM conf_users WHERE goo='{$goo}'"; $USER = single_row_select($usr_qry, 0); $logaction = log_it("Successful conf_users lookup for {$temp_usr['name']}", $temp_usr['id']); } /* // Check again for missing user entry. Create an account for them. if (is_null($USER)) { $LC_EMAIL = strtolower($USER_EMAIL); $goo = substr($USER_GOO, 3); $USER_NAME = ''; $result = single_row_insert("INSERT INTO conf_users (goo, email, name) VALUES ('{$goo}','{$LC_EMAIL}','{$USER_NAME}');"); //echo "INSERT INTO conf_users (goo, email, name) VALUES ('{$goo}','{$LC_EMAIL}','{$USER_NAME}');"; //print_r($result); //$logaction = log_it("Made a new conf_users row for {$USER_NAME} / {$LC_EMAIL} / {$USER_GOO}"); $usr_qry = "SELECT id AS conf_id, goo AS conf_goo, email AS conf_email, name AS conf_name, active AS conf_active FROM conf_users WHERE LOWER(email)='{$USER_EMAIL}'"; $USER = single_row_select($usr_qry, 0); } */