<?php
//require_once 'parseUtils.php';
require_once 'asyncWithComandLine.php';
require_once 'asyncWithPhp.php';

class asyncRequst {//change to upper case

   public static $req = null;
    
   public function __construct() {
        if (self::$req == null) {
            self::$req = self::create();
        }
    }

    public static function create() {
        global $asyncwithCmd;
        if ($asyncwithCmd) {
            return new asyncWithComandLine();//runs async req thru rungraph. bat
        } else {
            return new asyncWithPhp();//runs async req thrue php asqnc fopen - with server
        }
    }

    public static function getInstance() {
        if (self::$req == null) {
            self::$req = self::create();
        } 
        return self::$req;
    }

    public static function async_request($type, $url = null, $params = null) {
        $cookie = filter_input(INPUT_COOKIE, 'PHPSESSID');
        foreach ($params as $key => &$val) {
            if (is_array($val)) {
                $val = implode(',', $val);
            }
            $post_params[] = $key . '=' . urlencode($val);
        }
        $paramsString = implode('&', $post_params);
        $parts = parse_url($url);

        try {
            $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
            $out = self::request_body($type, $paramsString, $parts, $cookie);
            $res = fwrite($fp, $out); //This function returns the number of bytes written, or FALSE on failure.
            if (!$res) {
                write_to_log("ERROR", "async request faild:fwrite functin return:" . $res);
            }
            fclose($fp);
        } catch (Exception $e) {
            write_to_log("ERROR", "Exception:" . $e);
        }
    }

    public static function request_body($type, $paramsString, $parts, $cookie) {
        if ($type == "GET") {
            $out = "$type ";
            $out .= $parts['path'];
            if (isset($paramsString)) {
                $out .= "?" . $paramsString;
            }
            $out .= " HTTP/1.1\r\n";
            $out .= "Host: " . $parts['host'] . "\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Cookie: PHPSESSID=$cookie\r\n\r\n";
        } elseif ($type == "POST") {
            $out = "$type " . $parts['path'] . " HTTP/1.1\r\n";
            $out .= "Host: " . $parts['host'] . "\r\n";
            $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
            $out .= "Content-Length: " . strlen($paramsString) . "\r\n";
            $out .= "Connection: Close\r\n\r\n";
            if (isset($paramsString)) {
                $out .= $paramsString;
            }
        }
        return $out;
    }

  public static function getPath() {
        global $CURRENT_SOURCE_FOLDER;
        $pieces = explode("/", $CURRENT_SOURCE_FOLDER);
        $lastFolder = end($pieces);
        $beforeLastFolder = prev($pieces);
        $path = "$_SERVER[HTTP_HOST]/$beforeLastFolder/$lastFolder";
        return $path;
    }

}
