<?php
# HIDE ERRORS
error_reporting(0);
set_time_limit(0);

# HEADER
header("Content-Type: text/plain");

if ($argv[1] | $_GET["number"]) {
    $number = $argv[1] | $_GET["number"];
    $number = ltrim($number, '0');
    
    # EXTRACT STRINGS
    function GetStr($string, $start, $end)
    {
        $str = explode($start, $string);
        $str = explode($end, $str[1]);
        return $str[0];
    }
    
    # QUICK VIEW GET TOKEN
    $url  = "https://app1.smart.com.ph/api/v2/get-terms-and-conditions-token?number=63$number&token=QV";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $headers = array(
        "X-Application-Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhcHBfaWQiOiJpb3MiLCJpYXQiOjE1OTg1ODY1MjYsIm5iZiI6MTU5ODU4NjUyNiwiZXhwIjoxOTEzOTQ2NTI2fQ.xVM2s_Owt4zNWLOlllhPXcRQ4b23x6KQpqs_2NGu9zPlQ9hjOsSS6pr9Qams7jfsyMPXtik2MFvv8V_nT8oG5Q"
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $resp = curl_exec($curl);
    curl_close($curl);
    # EXTRACT TOKEN STRING
    $token = getstr($resp, 'token":"', '"');
    if (empty($token)) {
        exit();
    }
    
    # QUICK VIEW
    $url  = "https://app1.smart.com.ph/api/v2/quick-view?number=63$number";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $headers = array(
        "X-Application-Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhcHBfaWQiOiJpb3MiLCJpYXQiOjE1OTg1ODY1MjYsIm5iZiI6MTU5ODU4NjUyNiwiZXhwIjoxOTEzOTQ2NTI2fQ.xVM2s_Owt4zNWLOlllhPXcRQ4b23x6KQpqs_2NGu9zPlQ9hjOsSS6pr9Qams7jfsyMPXtik2MFvv8V_nT8oG5Q",
        "Authorization: Bearer $token"
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $resp = curl_exec($curl);
    curl_close($curl);
#   echo "$resp";
    
    # EXTRACT JSON STRING RESPONSE
    $number      = getstr($resp, '"accountMIN":"', '"');
    $balance     = getstr($resp, '{"load_balance":{"balance":"', '"');
    $expiration  = getstr($resp, '' . $balance . '","_expiration":"', '"');
    $pbalance    = getstr($resp, '"points":{"balance":"', '"');
    $pexpiration = getstr($resp, '"_expiration":"' . $pbalance . ' Points Exp ', '"');
    
# RESPONSE
echo 'Number: ' . $number . '
Load Balance: ' . $balance . '
Load Expiration: ' . $expiration . '
Points Balance: ' . $pbalance . '
Points Expiration: ' . $pexpiration . '
';
}
?>