HEX
Server: Apache
System: Linux www3.pit.tblive.com 5.14.0-687.38.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 12 17:19:12 EDT 2026 x86_64
User: awaldron (1020)
PHP: 8.1.34
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/awaldron/_accomods/apidev.accomods.com/v1/_globals.php
<?php

//**********************************************************************************************************//
// DATABASE FUNCTIONS **************************************************************************************//

function dbconnect() {
/*
    if ( stristr($_SERVER['HTTP_HOST'], 'localhost') ) {
        $username = 'root';
        $password = 'root';
        $dbname = 'accomods_api';
    }
    else if ( stristr($_SERVER['HTTP_HOST'], 'apidev.accomods.com') ) {
        $username = 'awaldron_accomods_dev';
        $password = 'XgDpNjbMiSpK';
        $dbname = 'awaldron_accomods_dev';
    }
    else if ( stristr($_SERVER['HTTP_HOST'], 'api.accomods.com') ) {
        $username = 'awaldron_accomods';
        $password = '$7FuE1MuvBp';
        $dbname = 'awaldron_accomods';
    }
*/    
	$username = 'awaldron_accomods_dev';
	$password = 'XgDpNjbMiSpK';
	$dbname = 'awaldron_accomods_dev';

    $db = new mysqli('localhost', $username, $password, $dbname);
    if ($db->connect_errno) {
        response( 500, ['success' => false, 'errors' => ['Error connecting to the database.']]);
    }
    return $db;
}


//**********************************************************************************************************//
// CORS FUNCTIONS ******************************************************************************************//

function cors() {

    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day

    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, PUT, HEAD, DELETE, OPTIONS");

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }
}



//**********************************************************************************************************//
// BACKDOOR LOGIN TO CATALYST ******************************************************************************//

function backdoor_login() {
    $postData = 'grant_type=password&organization=accomods&isCustomer=true&username=waldo97@gmail.com&password=test123';

    // create curl resource
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_URL, "https://admin.inmotionapp.net/api/v1/token");
    curl_setopt($ch, CURLOPT_URL, "https://api.bimcatalyst.dev/v2/session/login");

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $data = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    return $data;
}



//**********************************************************************************************************//
// API FUNCTIONS *******************************************************************************************//

function validate_body( $args, $rules ) {

    $valid = true;
    $errors = [];

    if ($args !== false && $rules !== false) {
        foreach ($rules as $param => $rule) {
            // CHECK REQUIRED
            if (strstr($rule,'req')) {
                if (empty($args[$param])) {
                    $valid = false;
                    $errors[] = $param.' is required.';
                }
            }
            // CHECK EMAIL
            if (strstr($rule,'email')) {
                if (!filter_var($args[$param], FILTER_VALIDATE_EMAIL)) {
                    $valid = false;
                    $errors[] = $param.' is not a valid email address.';
                }
            }
        }
    }

    if (!$valid) {
        response( 400, ['success'=>false,'errors'=>$errors]);
    }
    return true;
}

function validate_token( $uid ) {
    // IF uid==0 THEN THROW ERROR INVALID TOKEN
    if ($uid !== false && $uid === 0) {
        response( 401, ['success'=>false,'errors'=>['Security token invalid.'.$_SERVER['HTTP_AUTHORIZATION']]]);
    }
}

function validate_admin( $is_admin ) {
    // IF is_admin==0 THEN THROW ERROR NOT AUTHORIZED
    if (!$is_admin) {
        response( 401, ['success'=>false,'errors'=>['Unauthorized. User must be an admin.']]);
    }
}

function response( $responseCode=200, $responseBody=[] ) {
    http_response_code($responseCode);
    header('Content-Type: application/json');
    echo json_encode($responseBody);
    exit;
}


//function new_token( $db, $uid ) {
//    $token = bin2hex(random_bytes(64));
//    $created = time();
//    $expires = time() + 60*60*12;
//    $ins = "INSERT INTO tokens
//            (user_id, token, created, expires) VALUES
//            (".$uid.",'".$token."',".$created.",".$expires.")";
//    if ($result = $db->query($ins)) {
//        return [
//            'token' => $token,
//            'created' => $created,
//            'expires' => $expires
//        ];
//    } else {
//        response( 400, ['success'=>false,'errors'=>['Error creating token.']]);
//    }
//}

function check_token( $db, $token ) {
    $sql = "SELECT token_id, user_id FROM tokens WHERE token='".$token."' AND expires > ".time();
    $res = qRow( $db, $sql );
    if (!empty($res['user_id'])) {
        // UPDATE TOKEN EXPIRATION
        $upd = "UPDATE tokens SET expires=".(time()+60*60*12)." WHERE token_id=".$res['token_id'];
        $db->query($upd);
        return $res['user_id'];
    } else {
        return 0;
    }
}


//**********************************************************************************************************//
// DB HELPER FUNCTIONS *************************************************************************************//


function qVal( $db, $sql ) {
    $res = $db->query($sql);
    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();
        foreach ($row as $k => $v) {
            return $v;
        }
    } else {
        return false;
    }
}

function qRow( $db, $sql ) {
    $res = $db->query($sql);
    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();
        return $row;
    } else {
        return false;
    }
}

function qSet( $db, $sql ) {
    $out = [];
    $res = $db->query($sql);
    while ($row = $res->fetch_assoc()) {
        $out[] = $row;
    }
    return $out;
}




function qsql( $q ) {
    if ($q->num_rows() == 0) return false;
    else {
        $r = $q->result_array();
        $r = $r[0];
        foreach ($r as $k => $v) return $v;
    }
}

function qin( $a ) {
    $in = '(';
    for ($i=0; $i < count($a); $i++) {
        if ($i > 0) $in .= ',';
        $in .= "'".$a[$i]."'";
    }
    $in .= ')';
    return $in;
}


function stripAttributes( $html, $emptyText='' ) {
    $find = ['&nbsp;', '’', '“', '”', 'o:p', '–'];
    $repl = ['', "'", '"', '"', 'p'. '-'];
    $html = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/si",'<$1$2>', $html);
    $html = str_ireplace($find, $repl, $html);
    $html = strip_tags($html, ['p','a','br','ul','li','ol','sub','sup','strike','i','b','strong']);
    return (empty($html) ? $emptyText : $html);
}

function pp( $arr ) {
    echo '<pre>', print_r($arr), '</pre>';
}

function send_tracking_email( $html, $subject ) {
	$mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP();
    $mail->Mailer = "smtp";
    $mail->Host = "mail.smtp2go.com";
    $mail->Port = "2525"; // 8025, 587 and 25 can also be used. Use Port 465 for SSL.
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Username = "accomods";
    $mail->Password = "dTZpM29jb3l3MjAw";

    $mail->From = 'admin@actiondriveneducation.com';
    $mail->FromName = 'Action Driven Education';
    $mail->AddAddress('awaldron@buildinmotion.com');
    $mail->AddReplyTo('admin@actiondriveneducation.com', 'Action Driven Education');

    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $html;
    return !!$mail->Send();
}