Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard coded values, add guidance on permissions #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions BitcoinPayment.body.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

class BitcoinPayment {
public static function mtgox_check_post() {
// API settings
$key = 'your_key';
$secret = 'your_secret';
global $wgBitcoinPaymentAPIKey, $wgBitcoinPaymentAPISecret;

if ($_SERVER['HTTP_REST_KEY'] != $key) return false;
if ($_SERVER['HTTP_REST_KEY'] != $wgBitcoinPaymentAPIKey) return false;
$post_data = file_get_contents('php://input');
$hash = hash_hmac('sha512', $post_data, base64_decode($secret), true);
$hash = hash_hmac('sha512', $post_data, base64_decode($wgBitcoinPaymentAPISecret), true);
if (base64_decode($_SERVER['HTTP_REST_SIGN']) != $hash) return false;

return true;
}

public static function mtgox_query($path, array $req = array()) {
// API settings
$key = 'your_key';
$secret = 'your_secret';

global $wgBitcoinPaymentAPIKey, $wgBitcoinPaymentAPISecret;

// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
Expand All @@ -33,8 +29,8 @@ public static function mtgox_query($path, array $req = array()) {

// generate the extra headers
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix.$post_data, base64_decode($secret), true)),
'Rest-Key: '.$wgBitcoinPaymentAPIKey,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix.$post_data, base64_decode($wgBitcoinPaymentAPISecret), true)),
);

// our curl handle (initialize if required)
Expand All @@ -44,7 +40,7 @@ public static function mtgox_query($path, array $req = array()) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, 'https://mtgox.com/api/'.$path);
curl_setopt($ch, CURLOPT_URL, 'https://data.mtgox.com/api/'.$path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,25 @@ You will need a "BitcoinPayment" template. One example is available there:

https://en.bitcoin.it/w/index.php?title=Template:BitcoinPayment&action=edit

Installation requires that you paste the following into LocalSettings.php.

```
#MtGox API credentials (account required with API key with Deposit and Merchant privileges)
$wgBitcoinPaymentAPIKey = 'your_API_key';
$wgBitcoinPaymentAPISecret = 'your_API_secret';
$wgBitcoinPaymentSSL = true;

# Fee in satoshis to become "trusted", 0.001 BTC shown as default
$wgBitcoinPaymentFee = 100000;

# register bitcoin: uri syntax for wikitext parsing
$wgUrlProtocols[] = "bitcoin:";

# make pages editable only for trusted and administrative users.
$wgGroupPermissions['*']['edit'] = false;
$wgGroupPermissions['administrator']['edit'] = true;
$wgGroupPermissions['bureaucrat']['edit'] = true;
$wgGroupPermissions['trusted']['edit'] = true;
```


10 changes: 7 additions & 3 deletions SpecialBitcoinPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ public function __construct() {
}

public function execute($par) {
global $wgUser;
global $wgUser, $wgBitcoinPaymentFee, $wgBitcoinPaymentSSL;

if ($par == 'callback') {
if (!BitcoinPayment::mtgox_check_post()) {
die("error");
}
if (($_POST['status'] != 'confirmed') && ($_POST['status'] != 'published')) die("not_confirmed"); // don't care
if ($_POST['amount_int'] < 1000000) die("too_low"); // amount too low
if ($_POST['amount_int'] < $wgBitcoinPaymentFee ) die("too_low"); // amount too low
if ($_POST['item'] != 'BTC') die('not_btc');

$desc = $_POST['description'];
Expand Down Expand Up @@ -47,7 +47,11 @@ public function execute($par) {

if (is_null($btc_addr)) {
// $url = 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = 'https://'.$_SERVER['HTTP_HOST'].'/wiki/Special:BitcoinPayment/callback';
if( $wgBitcoinPaymentSSL )
$protocol = 'https';
else
$protocol = 'http';
$url = $protocol.'://'.$_SERVER['HTTP_HOST'].'/wiki/Special:BitcoinPayment/callback';
$btc_addr = BitcoinPayment::mtgox_query('2/money/bitcoin/address', array('ipn' => $url, 'description' => 'WP#'.$wgUser->getId()));
if ($btc_addr['result'] != 'success') {
$wikitext = 'An error occured, please retry later';
Expand Down