Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
foooo
* Readme
calendaring thing
form thing for testing stuff
Web-viewer for curl
4
found
auth
Name:
Category: foooo
Body:
<?php ini_set('display_errors', '1'); error_reporting(-1); // disable this when it goes live if (isset($_GET['ajax'])) { ob_start(); // init stuff $success = false; $message = ''; $data = array(); $debug = ''; foreach(explode(' ', 'url method data') as $field) $$field = is_array(@$_REQUEST[$field]) ? @$_REQUEST[$field] : trim(@$_REQUEST[$field]); do { if (substr(strtolower($url), 0, 4)!='http') { $message = "Invalid URL."; break; } if (! in_array(strtolower($method), array('get', 'post'))) { $message = "Invalid Method (\"GET\" or \"POST\". It's not hard.)"; break; } /* curl_get() returns an assoc array: 'status' => array( 'line' => $status_line, 'http' => $status_http, 'code' => $status_code, 'text' => $status_text ), 'headers' => $headers, 'body' => $body */ // get data $data = curl_get($url, $method, $data); if (is_array($data)) { foreach ($data['headers'] as $key => $val) { $headers[strtolower($key)] = $val; } if (substr($headers['content-type'], 0, 5) == 'image') { $data['body'] = '<img src="data:'.$headers['content-type'].';base64,'. base64_encode($data['body']) .'">'; }else{ $data['body'] = htmlents($data['body']); } $data['headers'] = array_dump($data['headers'], 'r'); $hex = array(); $len = strlen($data['raw']); // these habits die hard // There is a shorter way to do all this, I know it - but apparently not at 3 AM. $j = 0; $hexString = $charString = ''; $table = "<table>\n"; for($i=0;$i<$len;$i++) { $char = $data['raw'][$i]; $hex = dechex(ord($char)); if (strlen($hex) == 1) $hex = '0'.$hex; if ($hex == '00' or in_array($char, array("\n", "\r", "\t"))) $char = '.'; $hexString .= $hex.' '; // $charString .= htmlents($char); $charString .= iconv("ISO-8859-1", "UTF-8", htmlentities($char)); // $charString .= $char; $j++; if ($j == 8) { $hexString .= ' '; // $charString .= ' '; }elseif ($j == 16) { $table .= "<tr><td>$hexString | </td><td>$charString</td></tr>\n"; $hexString = $charString = ''; $j = 0; } } if ($j) $table .= "<tr><td>$hexString</td><td>$charString</td></tr>\n"; // trailing bits $table .= "</table>"; $data['hex'] = $table; // and replace. $success = true; $message = 'Yo.'; } } while (false); $debug = ob_get_clean(); echo json_encode(compact('success', 'message', 'data', 'debug')); die(); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Curl</title> <link rel="stylesheet" href="/css/style.css" type="text/css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(document).ready(function(){ }); </script> <style type="text/css" media="screen"> body { font-family: consolas, monaco, courier, monospace; } .button { border: 2px outset gray; font-weight: bold; padding: 0px 3px; color: #666; background: #888; } a.button:hover { cursor: pointer; } .button.active { border: 1px inset gray; background: #eee; color: red; } .function { color: blue; background: #D9B0FF; } .min, .plus { } .resultDisplay { border: 1px solid red; white-space: pre-wrap; display: none; } .resultDisplay.hex { white-space: pre; } .resultDisplay.html img { border: 1px solid black; } .resultDisplay.active { display: block; } .tab { border: 1px solid gray; padding: 0px 4px; margin-right: 5px; background: #eee; } .tab.active { color: red; background: white; } </style> </head> <body> Yo.<br> <?php echo create_form(); ?> <script type="text/javascript"> // OBEY $('.submit').click(function() { var $button = $(this); var $form = $button.parents('form'); var data = {}; $form.find('.values').each(function() { data[$(this).attr('name')] = $(this).val(); }); // alert(data.toSource()); $.post('curl.php?ajax', data, function(result) { if (result.success) { var resultDivs = $button.closest('form').next('div.resultDivs'); resultDivs.find('div.html').html(result.data.status.line + '<br>\n' + result.data.headers + '<br>\n' + result.data.body); resultDivs.find('div.hex').html(result.data.hex); // alert(result.toSource()); }else{ alert(result.message); } }, 'json'); }); // to get or to post, that's the method $('.method').click(function() { var button = $(this); var method = button.data('method'); // get value button.nextAll('input[name="method"]').val(method); // set value (the thrill!) button.addClass('active'); button.siblings('.method').removeClass('active'); }); // min & plus params $('.min').live('click', function() { $(this).closest('div').remove(); if ($(this).closest('.params').children('div').length == 1) { return; } }); $('.plus').click(function() { var paramsDiv = $(this).parents('form').find('.params'); paramsDiv.append('<div><input type="text" class="param" name="name" value=""> : <input type="text" class="param" name="value" value="">\n<input class="min" type="button" value="-">\n</div>\n'); paramsDiv.children('div').last().children('input[name="name"]').focus(); }); // tab clicks $('.tab').live('click', function() { var tabButton = $(this); tabButton.addClass('active').siblings('.tab').removeClass('active'); tabButton.closest('.resultDivs').find('.resultDisplay.'+tabButton.data('type')).addClass('active').siblings('.resultDisplay').removeClass('active'); }); </script> </body> </html> <?php /** -- Functions ------------------------- * * * create_form($params=array()) * htmlents($string) * curl_get($url, $method='get', $post=array()) * * **/ //_______________________________ // create_form($params=array()) / function create_form($params=array()) { $defaults = array( 'url' => 'http://mech.cx/', 'method' => 'get', 'params' => array( // ''=>'', ), ); extract(array_merge($defaults, $params)); $out = ' <form action="curl.php" method="post" class="form"> URL: <input type="text" class="values" name="url" value="'. htmlents($url) .'" size="80"><br> Method: <a class="button method '.($method == 'get' ? 'active':'').'" data-method="get">GET</a> <a class="button method '.($method == 'post' ? 'active':'').'" data-method="post">POST</a> <input type="hidden" name="method" class="values" value="'.htmlents($method).'"><br> <div style="float: left;">Params: <input class="plus" type="button" value="+"></div> <div style="float: left;" class="params"> Name : Value'; foreach ($params as $key => $val) { $out .= '<div><input type="text" class="param" name="name" value="'.htmlents($key).'"> : <input type="text" class="param" name="value" value="'.htmlents($val).'"> <input class="min" type="button" value="-"> </div>'; } $out .= '</div><div style="clear: both;"></div>'; $out .= '<input type="button" class="submit" name="" value="Go fetch!" id=""> </form>'."\n"; $out .= '<div class="resultDivs"> <span class="tab active" data-type="html">HTML</span> <span class="tab" data-type="hex">Hex</span> <div class="resultDisplay html active"></div> <div class="resultDisplay hex"></div> </div>'."\n"; return $out; } //____________________ // htmlents($string) / function htmlents($string) { return htmlspecialchars($string, ENT_QUOTES); } //_______________________________________________ // curl_get($url, $method='get', $post=array()) / function curl_get($url, $method='get', $post=array()) { if (! $url) return false; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); // include headers to output curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // have curl_exec return output as string // if (strtolower($method) == 'post') { // curl_setopt($ch, CURLOPT_POST, true); // curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // } $string = curl_exec($ch); curl_close($ch); // process more, split headers from body $gap_pos = strpos($string, "\r\n\r\n"); $header = substr($string, 0, $gap_pos); $body = substr($string, $gap_pos+4); // status line $status_line = substr($header, 0, strpos($header, "\r\n")); list($status_http, $status_code, $status_text) = explode(' ', $status_line, 3); // headers $keys = array(); foreach(explode("\r\n", substr($header, strpos($header, "\r\n")+2)) as $line) { if (strpos($line, ':') > 0) { $col_pos = strpos($line, ':'); list($key, $value) = array(trim(substr($line, 0, $col_pos)), trim(substr($line, $col_pos+1))); if (in_array($key, $keys)) { $headers[$key] .= ', '.$value; // as per RFC 2616 sec 4.2, 2.1 }else{ $headers[$key] = $value; } $keys[] = $key; }else{ $malformed_headers[] = $line; } } $out = array( 'status' => array( 'line' => $status_line, 'http' => $status_http, 'code' => $status_code, 'text' => $status_text ), 'headers' => $headers, 'body' => $body, 'raw' => $string ); if (isset($malformed_headers)) $out['errors']['malformed_headers'] = $malformed_headers; return $out; } //__________________________________ draws a table around a multidimensional array (such as a SELECT result) // array_dump($array, $options='') / function array_dump($array, $options='') { $options = strtolower(" $options"); $pre = strpos($options, 'p'); // duplicate as needed $return = strpos($options, 'r'); // echo or return? $out = ''; $out .= "<table border='1' cellspacing='0'>\n"; if (is_array(current($array))) { // nested array if (! is_numeric(key(current($array)))) { // of which inner arrays are assoc $out .= "<tr><th>•</th><th>"; $out .= join('</th><th>', array_map('htmlents', array_keys(current($array)))); $out .= "</th></tr>\n"; } } foreach($array as $key => $value) { $out .= "<tr>"; if (is_string($value)) { $out .= "<td valign='top'>$key</td><td>".(($pre && strstr($value, "\n")) ? '<pre>':'').htmlspecialchars($value)."</td>"; }else if(is_array($value)) { $out .= "<td>$key</td>"; foreach($value as $col) { $out .= "<td>".(($pre && strstr($value, "\n")) ? '<pre>':'').htmlspecialchars($col)."</td>"; } } $out .= "</tr>\r"; } $out .= "</table>"; if ($return) return $out; echo $out; } /* -- Log -------------------------------- [2011-03-01 04:21:32] Got it to a somewhat functioning state: it displays HTTP Status, Headers HTML or image, and everything all over again but in hex (coz we're cool kids), badly, if it's funky characters (coz we're not /too/ cool, gotta keep it real, right). curl_get() was copied from online reposithinger but hacked around a bunch. Todo: - Maybe using the PHP class to simulate a browser (http://www.lastcraft.com/browser_documentation.php) could help, here. If not, then at least the method list could serve as "inspiration" to re-create something like it closer to what we can use, here. The curl_get() function could be replaced by a class. - Prevent hitting enter in the URL input to reload the friggin' page, make it run the JS ajax code instead. - The hex display should filter out more characters that a browser can't display anyway. While there, figure out what the hell is up with that character set converting. Right now it won't display unless the characters were first htmlentity'd, THEN iconv'd, char by bloody char. How does that make any sense? - More hex display: try and see if JavaScript get-selection tricks can be used to colorize the data in one column, based on the selection in the other. The current table-row-by-row display will have to change of course. */ ?>
Footer