Guest

andrew

By: a guest on Nov 5th, 2008  |  syntax: PHP  |  size: 2.54 KB  |  hits: 218  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. define('PASTE_BIN', 'http://woot.pastebin.com/index.php');
  5.  
  6. $opt = getopt('e:f:u:l:h');
  7.  
  8. if (isset($opt['h']))
  9. {
  10.         echo "Usage: {$argv[0]} [-f file] [-e expire] [-u name] [-l language]\n\n";
  11.         echo "  -f file      Filename to paste; if not provided, reads from STDIN\n";
  12.         echo "  -e expire    Valid options: day, month, forever\n";
  13.         echo "  -u name      Username to paste as; if not provided uses whoami\n";
  14.         echo "  -l language  For syntax highlighting; default is text\n\n";
  15.         die();
  16. }
  17.  
  18. $name = (isset($opt['u']) ? $opt['u'] : trim(`whoami`));
  19. $expire = (isset($opt['e']) ? translateExpire($opt['e']) : 'd');
  20. $language = isset($opt['l']) ? $opt['l'] : 'text';
  21. $file = isset($opt['f']) ? $opt['f'] : NULL;
  22.  
  23. // read the file
  24. $contents = $file
  25.         ? file_get_contents($file)
  26.         : stream_get_contents(STDIN);
  27.  
  28. // try to auto-detect format
  29. if (!isset($opt['l'])
  30.         && ($f = detectFormat($contents, $file)) !== NULL)
  31. {
  32.         $language = $f;
  33. }
  34.  
  35. $post = array(
  36.         'format' => $language,
  37.         'poster' => $name,
  38.         'code2' => $contents,
  39.         'expiry' => $expire,
  40.         'paste' => 'Send',
  41. );
  42.  
  43. $ch = curl_init(PASTE_BIN);
  44. curl_setopt($ch, CURLOPT_POST, TRUE);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  46. curl_setopt($ch, CURLOPT_HEADER, 1);
  47. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  48.  
  49. $r = curl_exec($ch);
  50. if (!preg_match('/Location:\s+(\S+)/', $r, $m))
  51. {
  52.         die("Error while submitting to pastebin.\n");
  53. }
  54.  
  55. echo $m[1], "\n";
  56.  
  57. function translateExpire($value)
  58. {
  59.         switch ($value)
  60.         {
  61.                 case 'day':
  62.                 case 'd':
  63.                         return 'd';
  64.                 case 'month':
  65.                 case 'm':
  66.                         return 'm';
  67.                 case 'forever':
  68.                 case 'f':
  69.                         return 'f';
  70.         }
  71.         return 'd';
  72. }
  73.  
  74. function detectFormat($string, $filename = NULL)
  75. {
  76.         // first, attempt to detect the type by
  77.         // the file extension (if we're using a file)
  78.         if ($filename
  79.                 && ($pos = strrpos($filename, '.')) !== FALSE)
  80.         {
  81.                 $ext = substr($filename, $pos + 1);
  82.                 switch ($ext)
  83.                 {
  84.                         case 'php':
  85.                         case 'c':
  86.                         case 'diff':
  87.                         case 'html':
  88.                                 return $ext;
  89.                         case 'py':
  90.                                 return 'python';
  91.                         case 'patch':
  92.                                 return 'diff';
  93.                         case 'h':
  94.                                 return 'c';
  95.                 }
  96.         }
  97.  
  98.         // if it's a script (has the shebang), attempt
  99.         // to determine type by the binary
  100.         if (preg_match('/^#!(\S+)/', $string, $m))
  101.         {
  102.                 $bin = basename($m[1]);
  103.                 switch ($bin)
  104.                 {
  105.                         case 'php':
  106.                         case 'bash':
  107.                         case 'perl':
  108.                         case 'python':
  109.                                 return $bin;
  110.                 }
  111.         }
  112.  
  113.         // do some basic attempts at detecting the
  114.         // type by its contents
  115.         if (preg_match('/^\s*<\?php/', $string))
  116.         {
  117.                 return 'php';
  118.         }
  119.         return NULL;
  120. }
  121.  
  122. ?>