class_cURLTools.inc.php 11 KB
<?php
/**
 * @file	cURLTools.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
class cURLTools {

    private $simple_curl;
    private $simple_cookie;
    private $searchTextOutput;
    private $cookiepath;
    private $headers;
    private $user_agent;
    private $obj_FileTools;
    private $var_filedir;

    public function __construct($param_cookiepath=null) {
        if (!$param_cookiepath) {
            $this->cookiepath = STMANAGEData::getTestPathIn('Cookie');
        } else {
            $this->cookiepath = $param_cookiepath;
        }
        $var_cfgSTStd = STMANAGEData::getGenCfgSTStd();
//	print_r($var_cfgSTStd); 
	$var_cfgSTStd = $var_cfgSTStd['0'];
        $this->var_filedir = $var_cfgSTStd['Temp'];
        $this->obj_FileTools = new FileTools($this->var_filedir);

        $this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
        $this->headers[] = 'Connection: Keep-Alive';
        $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
        $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
    }

    public function curlSendData($param_testInput,$param_user=null) {
  if($param_user !=null){
            $var_cfgSTStd = STMANAGEData::getGenCfgSTStd();
            $var_cfgSTStd = $var_cfgSTStd['0'];


        }
        $num = count($param_testInput) - 1;
        $urlcomplete = SendReceiveTools::makeLinkFromGET($_GET, 'all');
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlcomplete);
        curl_setopt($this->simple_curl, CURLOPT_POST, 1);
        if (isset($_POST)) {
            $_POST = $this->dataEncode($_POST);
            curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $_POST);
        }
        curl_setopt($this->simple_curl, CURLOPT_HEADER, 0);
        curl_setopt($this->simple_curl, CURLOPT_COOKIESESSION, 1);
        $curloutput = curl_exec($this->simple_curl);
        $var_search_array = array('Fatal' => 'Fatal', 'mysql_error' => 'mysql_error', 'Versuche zur Verfügung' => 'Versuche zur Verfügung');
        $var_curl_fail = RegExpTools::newsearchTextOutput($curloutput, $var_search_array);
        if ($param_testInput['cur']['MCase'] == 'edit') {
            //			RegExpTools::special_checkHTML("<div\sclass\=\"formbox\"><form",$curloutput);
        }
        if (isset($this->ary_testPar['0']) && key_exists('ShowCurl', $this->ary_testPar['0']) && $this->ary_testPar['0']['ShowCurl'] == 'TRUE') {
            echo $curloutput;
        } elseif ($var_curl_fail != 'false') {
            $string = "<?php echo '### cURLFail: <pre>'; print_r(" . var_export($_POST, true) . "); echo '</pre>';?>";
            $string .= "<?php echo '<br>' ?>";
            $string .= "<?php echo ' Methode: '.__METHOD__.' Zeile: '. __Line__ ?>";
            $string .= "<?php echo " . "'<br><h2>Fehler in Der Seite:</h2> => " . $var_curl_fail . "'" . ";?>";
            $string .= "<?php echo ' <br><h2>html output:</h2> <pre>'; print_r(" . var_export($curloutput, true) . "); echo '</pre>';?>"; //"  ". $curloutput ." ";
            $var_filename = STTree::$var_numCurWalkAry . '_' . $param_testInput['cur']['MCase'] . '_Fail.php';
            $this->obj_FileTools->fileputs($var_filename, $string, 2);
        }
        return $curloutput;
    }

    public function curlSend($param_get, $param_post=null, $param_service=null) {
        /** Abfrage ob statt der index.php die service.php verwendet wird  */
        if ($param_service == null) {
            $urlcomplete = SendReceiveTools::makeLinkFromGET($param_get, 'all');
        } else {
            $urlcomplete = SendReceiveTools::makeLinkFromGET($param_get, 'all', TRUE, null, TRUE);
        }
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlcomplete);
        curl_setopt($this->simple_curl, CURLOPT_POST, 1);
        if ($param_post !== null) {
            $param_post = $this->dataEncode($param_post);
            curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $param_post);
        } else {
            $_POST = $this->dataEncode($_POST);
            curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $_POST);
        }
        curl_setopt($this->simple_curl, CURLOPT_HEADER, 0);
        curl_setopt($this->simple_curl, CURLOPT_COOKIESESSION, 1);
        $curloutput = curl_exec($this->simple_curl);
        $var_search_array = array('Fatal' => 'Fatal', 'mysql_error' => 'mysql_error', 'Versuche zur Verfügung' => 'Versuche zur Verfügung');
        return $curloutput;
    }

    /**
     * curlReceive
     * @return
     * @param string || array $param_GET
     * @param array $param_POST
     * @param string $param_externWebsite
     */
    public function curlReceive($param_GET=null, $param_POST=null, $param_externWebsite=null) {
//        print_r($param_externWebsite);
 //       print_r($param_GET);
//	echo ' POST: ';
  //      print_r($param_POST);
	if ($param_externWebsite) {
            $var_getpart = 'extern';
        } else {
            $var_getpart = 'all';
        }
        if (is_array($param_GET)) {
            $urlcomplete = SendReceiveTools::makeLinkFromGET($_GET, $var_getpart, $param_externWebsite);
        } else if (is_string($param_GET)) {
            $urlcomplete = $param_externWebsite . '?' . $param_GET;
        } else if (!$param_GET && $param_externWebsite) {
            $urlcomplete = $param_externWebsite;
        }
        $this->simple_curl = curl_init();
        curl_setopt($this->simple_curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlcomplete);
        if ($param_POST) {
            curl_setopt($this->simple_curl, CURLOPT_POST, 1);
            $_POST = $this->dataEncode($_POST);
            curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $_POST);
        }
        curl_setopt($this->simple_curl, CURLOPT_COOKIEFILE, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_COOKIEJAR, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->simple_curl, CURLOPT_USERAGENT, $this->user_agent);
        curl_setopt($this->simple_curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->simple_curl, CURLOPT_HEADER, 0);
        curl_setopt($this->simple_curl, CURLOPT_COOKIESESSION, 1);
        curl_setopt($this->simple_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        $curloutput = curl_exec($this->simple_curl);
//        echo 'TEST:'. $curloutput.' END';
        return $curloutput;
    }

    public function curlLogin($param_UserID, $param_Password, $param_ProfileID=null, $param_POST =null, $param_Website =null, $param_rtn=null) {
        if ($param_POST) {
            $_POST = $param_POST;
        } else {
            unset($_POST);
            $_POST['ID'] = $param_UserID;
            $_POST['password'] = $param_Password;
            $_POST['Submit'] = 'OK';
            $_POST['function'] = '';
        }
        if ($param_Website) {
            $urlfirst = $param_Website;
        } else {
            $urlfirst = SendReceiveTools::makeLinkFromGET($_GET, 'first', null, $param_rtn);
            if ($this->varProfileID != null) {
                $urlfirst = $urlfirst . '?HostProfileID=' . $param_ProfileID;
            }
        }
        $this->simple_curl = curl_init();
        curl_setopt($this->simple_curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlfirst);
        $_POST = $this->dataEncode($_POST);
        curl_setopt($this->simple_curl, CURLOPT_POST, 1);
        curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $_POST);
        curl_setopt($this->simple_curl, CURLOPT_COOKIEFILE, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_COOKIEJAR, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->simple_curl, CURLOPT_USERAGENT, $this->user_agent);
        curl_setopt($this->simple_curl, CURLOPT_FOLLOWLOCATION, true);
        $curlshow = curl_exec($this->simple_curl);
        $this->simple_cookie = $_COOKIE;
        if (is_array($param_rtn)) {
            $param_rtn['urlfirstLogin'] = $urlfirst;
        }
        if (is_array($param_rtn)) {
            $param_rtn['cURLLogin'] = $curlshow;
        }

        return $param_rtn;
    }

    public function curlLogout() {
        $pseudoGet = array('p' => array('0' => 'logout'));
        $urlcomplete = SendReceiveTools::makeLinkFromGET($pseudoGet, 'all');
        $this->simple_curl = curl_init();
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlcomplete);
        curl_setopt($this->simple_curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($this->simple_curl, CURLOPT_POST, 1);
        curl_setopt($this->simple_curl, CURLOPT_POSTFIELDS, $_POST);
        curl_setopt($this->simple_curl, CURLOPT_COOKIEFILE, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_COOKIEJAR, $this->cookiepath);
        curl_setopt($this->simple_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->simple_curl, CURLOPT_USERAGENT, $this->user_agent);
        curl_setopt($this->simple_curl, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($this->simple_curl);
        unset($this->simple_curl);
    }

    public function curlChangeProfile($param_ID) {
        $pseudoGet = array('HostProfileID' => $param_ID);
        $urlcomplete = SendReceiveTools::makeLinkFromGET($pseudoGet, 'all');
        curl_setopt($this->simple_curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($this->simple_curl, CURLOPT_URL, $urlcomplete);
        curl_setopt($this->simple_curl, CURLOPT_HEADER, 0);
        curl_setopt($this->simple_curl, CURLOPT_COOKIESESSION, 1);
        $curloutput = curl_exec($this->simple_curl);
        curl_close($this->simple_curl);
    }

    private function dataEncode($param_data, $param_keyprefix = "", $param_keypostfix = "") {
        if (!is_array($param_data)) {
            return;
        }
        $vars = null;
        foreach ($param_data as $key => $value) {
            if (is_array($value)) {
                $vars .= $this->dataEncode($value, $param_keyprefix . $key . $param_keypostfix . urlencode("["), urlencode("]"));
            } else {
                $vars .= $param_keyprefix . $key . $param_keypostfix . "=" . urlencode($value) . "&";
            }
        }
        return $vars;
    }

}
?>