|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* |
| 4 | +* @Name : NetPHP.php |
| 5 | +* @Version : 1.0 |
| 6 | +* @Programmer : Max |
| 7 | +* @Date : 2019-05-26 |
| 8 | +* @Released under : https://github.com/BaseMax/NetPHP/blob/master/LICENSE |
| 9 | +* @Repository : https://github.com/BaseMax/NetPHP |
| 10 | +* |
| 11 | +**/ |
| 12 | +$debug=false; |
| 13 | +// $debug=true; |
| 14 | +$debug_details=true; |
| 15 | +$debug_details=false; |
| 16 | +$token=null; |
| 17 | +function starts($haystack,$needle) |
| 18 | +{ |
| 19 | + $length = strlen($needle); |
| 20 | + return(substr($haystack,0,$length) === $needle); |
| 21 | +} |
| 22 | +function ends($haystack,$needle) |
| 23 | +{ |
| 24 | + $length = strlen($needle); |
| 25 | + if($length == 0) |
| 26 | + { |
| 27 | + return true; |
| 28 | + } |
| 29 | + return(substr($haystack,-$length) === $needle); |
| 30 | +} |
| 31 | +function contains($string,$contain) |
| 32 | +{ |
| 33 | + return strpos($string,$contain) !== false; |
| 34 | +} |
| 35 | +function useragent() |
| 36 | +{ |
| 37 | + return "Mozilla/5.0 (Windows NT 6.1; r…) Gecko/20100101 Firefox/60.0"; |
| 38 | + return "Mozilla/5.0(Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36(KHTML,like Gecko) curlrome/68.0.3440.106 Mobile Safari/537.36"; |
| 39 | +} |
| 40 | +function store_token($response) |
| 41 | +{ |
| 42 | + preg_match_all("/name=\"\_token\" value=\"(?<token>[^\"]+)\"/i",$response,$tokens); |
| 43 | + if(!isset($tokens["token"][0])) |
| 44 | + { |
| 45 | + exit("No Token value in login page!\n"); |
| 46 | + } |
| 47 | + return $tokens["token"][0]; |
| 48 | +} |
| 49 | +function get_headers_from_curl_response($headerContent) |
| 50 | +{ |
| 51 | + $headers = array(); |
| 52 | + $arrRequests = explode("\r\n\r\n",$headerContent); |
| 53 | + for($index = 0; $index < count($arrRequests) -1; $index++) |
| 54 | + { |
| 55 | + foreach(explode("\r\n",$arrRequests[$index]) as $i => $line) |
| 56 | + { |
| 57 | + if($i === 0) |
| 58 | + $headers[$index]['http_code'] = $line; |
| 59 | + else |
| 60 | + { |
| 61 | + list($key,$value) = explode(': ',$line); |
| 62 | + $headers[$index][$key] = $value; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return $headers; |
| 67 | +} |
| 68 | +function post($url,$values,$headers=[],$reffer="",$auto_redirect=true) |
| 69 | +{ |
| 70 | + global $debug,$debug_details; |
| 71 | + if($debug) |
| 72 | + { |
| 73 | + print "@Request[POST]----------------------------------------------\n"; |
| 74 | + print "----------@link ".$url."\n"; |
| 75 | + if($debug_details) |
| 76 | + { |
| 77 | + if($reffer!="") |
| 78 | + { |
| 79 | + print "----------@Reffer\n"; |
| 80 | + print $reffer; |
| 81 | + print "\n"; |
| 82 | + } |
| 83 | + if(count($values)!=0) |
| 84 | + { |
| 85 | + print "----------@Values\n"; |
| 86 | + print_r($values); |
| 87 | + } |
| 88 | + if(count($headers)!=0) |
| 89 | + { |
| 90 | + print "----------@Headers\n"; |
| 91 | + print_r($headers); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + $curl = curl_init($url); |
| 96 | + if(is_array($headers)) { |
| 97 | + curl_setopt($curl,CURLOPT_HTTPHEADER,$headers); |
| 98 | + } |
| 99 | + curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); |
| 100 | + curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,true); |
| 101 | + curl_setopt($curl,CURLOPT_FOLLOWLOCATION,$auto_redirect); |
| 102 | + curl_setopt($curl,CURLOPT_HEADER,true); |
| 103 | + curl_setopt($curl,CURLOPT_VERBOSE,false); |
| 104 | + curl_setopt($curl,CURLOPT_POST,true); |
| 105 | + curl_setopt($curl,CURLOPT_POSTFIELDS,$values); |
| 106 | + curl_setopt($curl,CURLOPT_USERAGENT,useragent()); |
| 107 | + curl_setopt($curl,CURLOPT_COOKIEJAR,"_cookies.txt"); |
| 108 | + curl_setopt($curl,CURLOPT_COOKIEFILE,"_cookies.txt"); |
| 109 | + if($reffer != "") |
| 110 | + curl_setopt($curl,CURLOPT_REFERER,$curl); |
| 111 | + $response = curl_exec($curl); |
| 112 | + $header_size = curl_getinfo($curl,CURLINFO_HEADER_SIZE); |
| 113 | + $header = substr($response,0,$header_size); |
| 114 | + $body = substr($response,$header_size); |
| 115 | + curl_close($curl); |
| 116 | + if($debug && $debug_details) |
| 117 | + { |
| 118 | + print "----------@Response Headers\n"; |
| 119 | + print_r($header); |
| 120 | + print "----------@Response Body\n"; |
| 121 | + print_r($body); |
| 122 | + print "\n"; |
| 123 | + } |
| 124 | + return [$body,$header]; |
| 125 | +} |
| 126 | +function get($url,$headers=[],$reffer="",$auto_redirect=true) |
| 127 | +{ |
| 128 | + global $debug,$debug_details; |
| 129 | + if($debug) |
| 130 | + { |
| 131 | + print "@Request[GET]----------------------------------------------\n"; |
| 132 | + print "----------@link ".$url."\n"; |
| 133 | + if($debug_details) |
| 134 | + { |
| 135 | + if($reffer!="") |
| 136 | + { |
| 137 | + print "----------@Reffer\n"; |
| 138 | + print $reffer; |
| 139 | + print "\n"; |
| 140 | + } |
| 141 | + if(count($headers)!=0) |
| 142 | + { |
| 143 | + print "----------@Headers\n"; |
| 144 | + print_r($headers); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + $curl = curl_init($url); |
| 149 | + if(is_array($headers)) { |
| 150 | + curl_setopt($curl,CURLOPT_HTTPHEADER,$headers); |
| 151 | + } |
| 152 | + curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); |
| 153 | + curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,true); |
| 154 | + curl_setopt($curl,CURLOPT_FOLLOWLOCATION,$auto_redirect); |
| 155 | + curl_setopt($curl,CURLOPT_HEADER,true); |
| 156 | + curl_setopt($curl,CURLOPT_VERBOSE,false); |
| 157 | + curl_setopt($curl,CURLOPT_POST,false); |
| 158 | + curl_setopt($curl,CURLOPT_USERAGENT,useragent()); |
| 159 | + curl_setopt($curl,CURLOPT_COOKIEJAR,"_cookies.txt"); |
| 160 | + curl_setopt($curl,CURLOPT_COOKIEFILE,"_cookies.txt"); |
| 161 | + if($reffer != "") |
| 162 | + curl_setopt($curl,CURLOPT_REFERER,$curl); |
| 163 | + $response = curl_exec($curl); |
| 164 | + $header_size = curl_getinfo($curl,CURLINFO_HEADER_SIZE); |
| 165 | + $header = substr($response,0,$header_size); |
| 166 | + $body = substr($response,$header_size); |
| 167 | + curl_close($curl); |
| 168 | + if($debug && $debug_details) |
| 169 | + { |
| 170 | + print "----------@Response Headers\n"; |
| 171 | + print_r($header); |
| 172 | + print "----------@Response Body\n"; |
| 173 | + print_r($body); |
| 174 | + print "\n"; |
| 175 | + } |
| 176 | + return [$body,$header]; |
| 177 | +} |
| 178 | +//////////////////////////////////////////////////////// |
| 179 | +function random()//like Math.random() in JS |
| 180 | +{ |
| 181 | + return(float)rand()/(float)getrandmax(); |
| 182 | +} |
| 183 | +function get_parse($content,$name) |
| 184 | +{ |
| 185 | + //name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="" |
| 186 | + preg_match('/name="'.$name.'" id="'.$name.'" value="(?<value>[^\"]+)"/i',$content,$values); |
| 187 | + // print_r($values); |
| 188 | + $value=$values["value"]; |
| 189 | + $value=str_replace("-","+",$value); |
| 190 | + $value=str_replace("_","/",$value); |
| 191 | + return $value; |
| 192 | +} |
0 commit comments