Advertisement
P22DX

PasteBin.php

Dec 16th, 2019
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /*
  5.  * Copyright (c) 2019 <cvar1984>
  6.  *
  7.  * Licensed unter the Apache License, Version 2.0 or the MIT license, at your
  8.  * option.
  9.  *
  10.  * ********************************************************************************
  11.  *
  12.  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13.  * this software and associated documentation files (the "Software"), to deal in
  14.  * the Software without restriction, including without limitation the rights to
  15.  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16.  * the Software, and to permit persons to whom the Software is furnished to do so,
  17.  * subject to the following conditions:
  18.  *
  19.  * The above copyright notice and this permission notice shall be included in all
  20.  * copies or substantial portions of the Software.
  21.  *
  22.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24.  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25.  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26.  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28.  *
  29.  * ********************************************************************************
  30.  *
  31.  * Licensed under the Apache License, Version 2.0 (the "License");
  32.  * you may not use this file except in compliance with the License.
  33.  * You may obtain a copy of the License at
  34.  *
  35.  *   http://www.apache.org/licenses/LICENSE-2.0
  36.  *
  37.  * Unless required by applicable law or agreed to in writing, software
  38.  * distributed under the License is distributed on an "AS IS" BASIS,
  39.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  40.  * See the License for the specific language governing permissions and
  41.  * limitations under the License.
  42.  */
  43.  
  44. class PasteBin
  45. {
  46.     protected static $api_dev_key = '';
  47.     protected static $api_user_key = '';
  48.     protected static $api_paste_private = '0';
  49.     protected static $api_paste_expire_date = 'N';
  50.  
  51.     private static function post(string $url, array $fields)
  52.     {
  53.         $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cookie.txt';
  54.         $ch = curl_init($url);
  55.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56.         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  57.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  58.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  59.         curl_setopt($ch, CURLOPT_ENCODING, 'br');
  60.         curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  61.         curl_setopt($ch, CURLOPT_VERBOSE, true);
  62.         curl_setopt($ch, CURLOPT_NOBODY, false);
  63.         curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp);
  64.         curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp);
  65.         return curl_exec($ch);
  66.         curl_close($ch);
  67.     }
  68.  
  69.     public static function paste(string $code, string $name, string $syntax)
  70.     {
  71.         $api_paste_name = urlencode($name);
  72.         $api_paste_format = urlencode($syntax);
  73.         $options = [
  74.             'api_option' => 'paste',
  75.             'api_user_key' => self::$api_user_key,
  76.             'api_paste_private' => self::$api_paste_private,
  77.             'api_paste_name' => $api_paste_name,
  78.             'api_paste_expire_date' => self::$api_paste_expire_date,
  79.             'api_paste_format' => $api_paste_format,
  80.             'api_dev_key' => self::$api_dev_key,
  81.             'api_paste_code' => $code
  82.         ];
  83.         return PasteBin::post('https://pastebin.com/api/api_post.php', $options);
  84.     }
  85. }
  86. try {
  87.     if (isset($argv[1])) {
  88.         $code = $argv[1];
  89.     } else {
  90.         $code = readline('Code : ');
  91.     }
  92.     $code = trim($code);
  93.     $code = file_get_contents(getcwd() . DIRECTORY_SEPARATOR . $code);
  94.     if (!$code) {
  95.         throw new Exception('Cannot get contents');
  96.     }
  97.  
  98.     $name = readline('Paste name : ');
  99.     $name = trim($name);
  100.  
  101.     $syntax = pathinfo($name, PATHINFO_EXTENSION);
  102.  
  103.     echo PasteBin::paste($code, $name, $syntax) . PHP_EOL;
  104. } catch (Exception $e) {
  105.     fprintf(STDERR, '%s%s', $e->getMessage(), PHP_EOL);
  106.     exit(1);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement