Last active
March 4, 2021 17:16
-
-
Save quant61/eb97d8dc98c476239210a566c275c571 to your computer and use it in GitHub Desktop.
Revisions
-
quant61 revised this gist
Mar 4, 2021 . 1 changed file with 13 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -137,9 +137,19 @@ function addOption($ch, $curlOption, $config, $key){ } } /** * possible answer to "php SimpleXMLElement get attributes as associative array" * * @param \SimpleXMLElement $element * @return array */ function getSimpleXmlElementAttributes(\SimpleXMLElement $element): array { $ans = []; foreach ($element->attributes() as $k => $v){ $ans[$k] = (string)$v; } return $ans; } -
quant61 revised this gist
Sep 11, 2018 . 1 changed file with 114 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,3 +37,117 @@ function get(&$var, $default=null) { } // version for php5 function makeRequest($url, $config = []){ $ch = curl_init(); if(isset($config['queryParams']) && is_array($config['queryParams'])){ if (strpos($url, '?') === false) { $url .= '?'; } $url .= http_build_query($config['queryParams']); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(isset($config['method'])){ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $config['method']); switch ($config['method']){ case 'POST': curl_setopt($ch, CURLOPT_POST, 1); break; case 'PUT': curl_setopt($ch, CURLOPT_PUT, 1); break; } } $headers = isset($config['headers'])? $config['headers'] : []; if(isset($config['body'])){ curl_setopt($ch, CURLOPT_POSTFIELDS, $config['body']); } else if(isset($config['json'])){ $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($config['json'])); } if($headers){ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } addOption($ch, CURLOPT_USERAGENT, $config, 'userAgent'); addOption($ch, CURLOPT_COOKIE, $config, 'cookies'); addOption($ch, CURLOPT_COOKIEFILE, $config, 'cookieFile'); addOption($ch, CURLOPT_COOKIEJAR, $config, 'cookieJar'); if(isset($config['proxy'])){ $proxy = $config['proxy']; if (isset($proxy['ip']) && isset($proxy['port'])) { curl_setopt($ch, CURLOPT_PROXY, $proxy['ip'] . ':' . $proxy['port']); } elseif (isset($proxy['ip'])) { curl_setopt($ch, CURLOPT_PROXY, $proxy['ip']); } if (isset($proxy['login']) && isset($proxy['password'])){ curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['login'] . ':' . $proxy['password']); } if (isset($proxy['type'])) { switch ($proxy['type']){ case 'http': curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); break; case 'socks4': curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4); break; case 'socks5': curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); break; } } } if(isset($config['curlOptions'])){ curl_setopt_array($ch, $config['curlOptions']); } curl_setopt($ch, CURLOPT_HEADER, 1); $rawResponse = curl_exec($ch); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($rawResponse, 0, $headerSize); $content = substr($rawResponse, $headerSize); $err = curl_error($ch); $info = curl_getinfo($ch); curl_close($ch); $report = compact('content', 'err', 'info', 'rawResponse', 'headers'); $parsedJson = json_decode($content, true); if($parsedJson){ $report['parsedJson'] = $parsedJson; } return $report; } function addOption($ch, $curlOption, $config, $key){ if(isset($config[$key])){ curl_setopt($ch, $curlOption, $config[$key]); } } -
quant61 revised this gist
Feb 24, 2018 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,4 +30,12 @@ func getSyscallArg(v interface{}) uintptr { } func getByte(addr uintptr) byte{ return (*struct{b byte})(unsafe.Pointer(addr)).b } func setByte(addr uintptr, value byte){ (*struct{b byte})(unsafe.Pointer(addr)).b = value } -
quant61 revised this gist
Feb 24, 2018 . 1 changed file with 28 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,4 +3,31 @@ // protects from directory traversal func safeJoin(baseDir, path string) string{ return filepath.Join(baseDir, filepath.Join("/", path)) } func getSyscallArg(v interface{}) uintptr { r := reflect.ValueOf(v) switch r.Kind(){ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return uintptr(r.Int()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return uintptr(r.Uint()) case reflect.Ptr, reflect.UnsafePointer, reflect.Slice: return r.Pointer() case reflect.String: b := []byte(r.String()) b = append(b, 0) return reflect.ValueOf(b).Pointer() // TODO: add&test support Array and Struct //case reflect.Array: // return r.Index(0).Addr() } // or panic? return 0 } -
quant61 revised this gist
Feb 2, 2018 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ // protects from directory traversal func safeJoin(baseDir, path string) string{ return filepath.Join(baseDir, filepath.Join("/", path)) } -
quant61 revised this gist
Jan 20, 2018 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,5 +32,8 @@ function copyKeys($source, &$target, $keys) } } function get(&$var, $default=null) { return isset($var) ? $var : $default; } -
quant61 revised this gist
Jan 20, 2018 . 1 changed file with 45 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ function arrayToUnique(array){ return [...new Set(array)] } // promise version of https://stackoverflow.com/a/14269536/6076531 function streamToBuffer(stream) { return new Promise(function(resolve, reject) { let bufs = []; stream.on('data', function (d) { // console.log('download', Buffer.concat(bufs).byteLength); bufs.push(d); }); stream.on('end', function() { console.log('stream loaded'); resolve(Buffer.concat(bufs)); }); stream.on('error', err => reject(err)); }); } function requestsStack(requests, results = []) { if(requests.length === 0){ return results; } let [current, ...rest] = requests; if(!Array.isArray(current)){ current = [current]; } console.log(`start request ${current}`); return connection.query(...current).then(function (data) { console.log(`request ${current} done`); results.push(data); return requestsStack(rest, results); }); } let hasDupes = array => new Set(array).size !== array.length let getDupsCount = array => array.length - new Set(array).size -
quant61 created this gist
Dec 3, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ <?php /** * [{id:13}, {id:42}] => {13: {id:13}, 42:{id:42}} * * @param array $items * @param callable $by * @return array */ function makeMapByFunc(array $items, $by){ $map = []; foreach ($items as $item){ $map[$by($item)] = $item; } return $map; } /** * TOGIST * @param $source * @param $target * @param $keys */ function copyKeys($source, &$target, $keys) { foreach ($keys as $key){ if(array_key_exists($key, $source)){ $target[$key] = $source[$key]; } } }