Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * File containing the encryption services code.
4
 *
5
 * @package PayPal
6
 */
7
 
8
/**
9
 * Load files we depend on.
10
 */
11
require_once 'PayPal.php';
12
 
13
/**
14
 * API for doing PayPal encryption services.
15
 *
16
 * @package PayPal
17
 */
18
class EWPServices
19
{
20
    /**
21
     * The profile to use for encryption.
22
     *
23
     * @access protected
24
     *
25
     * @var EWPProfile $_profile
26
     */
27
    var $_profile;
28
 
29
    /**
30
     * Construct a new EWP services object.
31
     *
32
     * @param EWPProfile $profile  The profile with the username, password,
33
     *                             and any other information necessary to use
34
     *                             the SDK.
35
     */
36
    function EWPServices($profile)
37
    {
38
        $this->setEWPProfile($profile);
39
    }
40
 
41
    /**
42
     * Use a given profile.
43
     *
44
     * @param EWPProfile $profile  The profile with the username, password,
45
     *                             and any other information necessary to use
46
     *                             the SDK.
47
     */
48
    function setEWPProfile($profile)
49
    {
50
        $this->_profile = $profile;
51
    }
52
 
53
    /**
54
     * Get the current profile.
55
     *
56
     * @return EWPProfile  The current profile.
57
     */
58
    function getEWPProfile()
59
    {
60
        return $this->_profile;
61
    }
62
 
63
    /**
64
     * Creates a new encrypted button HTML block
65
     *
66
     * @param array The button parameters as key/value pairs
67
     * @return mixed A string of HTML or a Paypal error object on failure
68
     */
69
    function encryptButton($buttonParams)
70
    {
71
        if (!is_object($this->_profile)) {
72
            return PayPal::raiseError("No Profile is set, cannot encrypt");
73
        }
74
 
75
        $res = $this->_profile->validate();
76
        if (PayPal::isError($res)) {
77
            return $res;
78
        }
79
 
80
        $merchant_cert = 'file://' . $this->_profile->getCertificateFile();
81
        $merchant_key = 'file://' . $this->_profile->getPrivateKeyFile();
82
        $enc_cert = 'file://' . $this->getPayPalCertificateFile($this->_profile->getEnvironment());
83
 
84
        $tmpin_file  = tempnam('/tmp', 'paypal_');
85
        $tmpout_file = tempnam('/tmp', 'paypal_');
86
        $tmpfinal_file = tempnam('/tmp', 'paypal_');
87
 
88
        $rawdata = array();
89
        $buttonParams['cert_id'] = $this->_profile->getCertificateId();
90
        foreach ($buttonParams as $name => $value) {
91
            $rawdata[] = "$name=$value";
92
        }
93
        $rawdata = implode("\n", $rawdata);
94
 
95
        $fp = fopen($tmpin_file, 'w');
96
        if (!$fp) {
97
            return PayPal::raiseError("Could not open temporary file '$tmpin_file')");
98
        }
99
        fwrite($fp, $rawdata);
100
        fclose($fp);
101
 
102
        if (!@openssl_pkcs7_sign($tmpin_file, $tmpout_file, $merchant_cert,
103
                                 array($merchant_key, $this->_profile->getPrivateKeyPassword()),
104
                                 array(), PKCS7_BINARY)) {
105
            return PayPal::raiseError("Could not sign encrypted data: " . openssl_error_string());
106
        }
107
 
108
        $data = file_get_contents($tmpout_file);
109
        $data = explode("\n\n", $data);
110
        $data = $data[1];
111
        $data = base64_decode($data);
112
        $fp = fopen($tmpout_file, 'w');
113
        if (!$fp) {
114
            return PayPal::raiseError("Could not open temporary file '$tmpin_file')");
115
        }
116
        fwrite($fp, $data);
117
        fclose($fp);
118
 
119
        if (!@openssl_pkcs7_encrypt($tmpout_file, $tmpfinal_file, $enc_cert, array(), PKCS7_BINARY)) {
120
            return PayPal::raiseError("Could not encrypt data:" . openssl_error_string());
121
        }
122
 
123
        $encdata = @file_get_contents($tmpfinal_file, false);
124
        if (!$encdata) {
125
            return PayPal::raiseError("Encryption and signature of data failed.");
126
        }
127
 
128
        $encdata = explode("\n\n", $encdata);
129
        $encdata = trim(str_replace("\n", '', $encdata[1]));
130
        $encdata = "-----BEGIN PKCS7-----$encdata-----END PKCS7-----";
131
 
132
        @unlink($tmpfinal_file);
133
        @unlink($tmpin_file);
134
        @unlink($tmpout_file);
135
 
136
        $action = $this->_profile->getUrl();
137
        $buttonimgurl = $this->_profile->getButtonImage();
138
 
139
        $retval = <<< PPHTML
140
<FORM ACTION="$action" METHOD="post">
141
<INPUT TYPE="hidden" NAME="cmd" VALUE="_s-xclick">
142
<INPUT TYPE="hidden" NAME="encrypted" VALUE="$encdata">
143
<INPUT TYPE="image" SRC="$buttonimgurl" BORDER="0" NAME="submit" ALT="Make Payments with PayPal -- it's fast, free and secure!">
144
</FORM>
145
PPHTML;
146
        return $retval;
147
    }
148
 
149
    /**
150
     * Returns the PayPal public certificate filename.
151
     *
152
     * @param string The environment to get the certificate for.
153
     * @return mixed The path and file of the certificate file, or a PayPal error object on failure.
154
     */
155
    function getPayPalCertificateFile($environment)
156
    {
157
        $package_root = PayPal::getPackageRoot();
158
        $cert = $package_root . '/cert/' . strtolower($environment) . '.paypal.com.pem';
159
 
160
        if (@include "$package_root/conf/paypal-sdk.php") {
161
            if (isset($__PP_CONFIG['paypal_cert_file']) &&
162
                !empty($__PP_CONFIG['paypal_cert_file'])) {
163
                $cert =  $__PP_CONFIG['paypal_cert_file'][$environment];
164
            }
165
        }
166
 
167
        if (!file_exists($cert)) {
168
            return PayPal::raiseError("Could not file Paypal public Certificate file '$cert'");
169
        }
170
 
171
        return $cert;
172
    }
173
 
174
}