Last active
August 3, 2025 16:51
-
-
Save williamdes/9f1739cfb786be40d00812cdb8a5e772 to your computer and use it in GitHub Desktop.
PHP script for paste.debian.net
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 characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Minimal PHP paste client for paste.debian.net | |
* Only implements add paste functionality from stdin | |
* Inspired by https://github.com/gebi/debianpaste-clients/blob/master/paste.py | |
* | |
* @source https://paste.debian.net/1389513 | |
* @source https://gist.github.com/williamdes/9f1739cfb786be40d00812cdb8a5e772 | |
* @see https://paste.debian.net/rpc-interface.html | |
* @license UNLICENSE | |
*/ | |
define('DEFAULT_SERVER', 'https://paste.debian.net/server.pl'); | |
define('DEFAULT_EXPIRE', 72);// Hours, If not submitted or empty 172800 (72 hours) is choosen. Maximum is 604800 (7 days). Use -1 for post that shouldn't expire. | |
define('DEFAULT_LANG', 'Plain'); | |
define('DEFAULT_PRIVATE', 0); | |
// Check for XML-RPC extension | |
if (! extension_loaded('xmlrpc')) { | |
fwrite(STDERR, "Error: Required 'xmlrpc' PHP extension is not loaded.\n"); | |
fwrite(STDERR, "Install it with:\n"); | |
fwrite(STDERR, " Ubuntu/Debian: sudo apt-get install php-xmlrpc\n"); | |
exit(1); | |
} | |
// Check for paste name argument | |
if ($argc < 2) { | |
fwrite(STDERR, "Usage: {$argv[0]} <paste_name> < input.txt\n"); | |
fwrite(STDERR, " or: echo \"content\" | {$argv[0]} <paste_name>\n"); | |
exit(1); | |
} | |
$paste_name = $argv[1]; | |
// Read content from stdin | |
$content = ''; | |
while ($line = fgets(STDIN)) { | |
$content .= $line; | |
} | |
// Default options | |
$options = [ | |
'name' => $paste_name, | |
'expire' => DEFAULT_EXPIRE, | |
'lang' => DEFAULT_LANG, | |
'private' => DEFAULT_PRIVATE, | |
'server' => DEFAULT_SERVER | |
]; | |
// Prepare XML-RPC request | |
$request = xmlrpc_encode_request( | |
'paste.addPaste', [ | |
$content, | |
$options['name'], | |
$options['expire'] === -1 ? -1 : $options['expire'] * 3600, | |
$options['lang'], | |
$options['private'] | |
] | |
); | |
// Create stream context | |
$context = stream_context_create( | |
[ | |
'http' => [ | |
'method' => "POST", | |
'header' => "Content-Type: text/xml", | |
'content' => $request | |
] | |
] | |
); | |
// Send request | |
$response = file_get_contents($options['server'], false, $context); | |
$result = xmlrpc_decode($response); | |
// Handle response | |
if (is_array($result) && isset($result['rc'])) { | |
if ($result['rc'] == 0) { | |
echo $result['statusmessage'] . "\n"; | |
} else { | |
fwrite(STDERR, "Error: " . $result['statusmessage'] . "\n"); | |
exit(1); | |
} | |
} else { | |
fwrite(STDERR, "Invalid server response\n"); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment