/var/www/hkosl.com/imusiccircle/paypal/lib/paypal-permissions-sdk-php-eef386c/samples/install.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php

/**
 *  Downloads PayPal PHP SDK dependencies based on your composer.json file
 */

define('DS'DIRECTORY_SEPARATOR);

define('COMPOSER_FILE''composer.json');

// name of the bootstrap file in custom installation
define('BOOTSTRAP_FILE''PPBootStrap.php');

// name of the SDK configuration file
define('CONFIGURATION_FILE''sdk_config.ini');

// URL from where the composer.json is downloaded if not present
define('COMPOSER_URL''https://raw.github.com/paypal/merchant-sdk-php/stable-php5.3/samples/composer.json');

// Flag to control whether composer should be used for installation
$useComposer false;

init($useComposer);
createAutoload();
createConfig(CONFIGURATION_FILE);
createBootStrap(BOOTSTRAP_FILE);
echo 
"Installation successful";
exit(
0);


function 
init($useComposer) {

    
// download if composer.json is not present
    
if(!file_exists(COMPOSER_FILE)) {
        
$fp fopen(COMPOSER_FILE"w");
        
curlExec(COMPOSER_URL$fp);
        
fclose($fp);
    }

    
// check if composer is installed
    
if($useComposer) {
        @
exec('composer'$output$status);
        if( 
$status == 0) {
            @
exec('composer update'$output$status);
            
var_dump($output);
            exit(
$status);
        } else {
            @
exec('composer.phar'$output$status);
            if( 
$status == 0)
            {
                @
exec('composer.phar update'$output$status);
                
var_dump($output);
                exit(
$status);
            }
        }
    } else {
        echo 
"composer not installed or 'useComposer' is set to false in install.php." PHP_EOL;
        echo 
"Running custom installation ... " PHP_EOL;
        
$extensions = array('zip''curl''openssl');
        foreach(
$extensions as $e) {
            if (!
extension_loaded($e)) {
                echo 
PHP_EOL "Please enable the $e extension. This script requires the " implode(", "$extensions) . " extensions to work correctly.";
                exit(
1);
            }
        }

        
$json = @file_get_contents(COMPOSER_FILE);
        
$json_a json_decode($jsontrue);
        
//array $processsed contains list of dependencies that have already been downloaded
        
$processed = array();
        foreach (
getDependency($json_a) as $dependency ) {
            
customInstall($dependency$dependency['group'], $processed);
        }
    }
}

/*
 * get the correct tag based on the tag in composer.json
* ex: get v3.1.4 if the entry in composer.json is 3.1.*
* @param $inputTag the tag in composer.json
* @param array $array array of all the tags fetched from github
*/
function getTag($inputTag$tagArray$branchArray)
{
    
natsort($tagArray);
    if(
strpos($inputTag'*') === )
    {
        return 
end($tagArray);
    }
    else if(((
strpos($inputTag'>')) !== false) && (strpos($inputTag'>') >=0))
    {
        if(!
in_array($inputTag$tagArray))
        {
            echo 
"error: invalid version tag in composer.json";
            exit();
        }
        return 
end($tagArray);
    }
    else
    {
        if(
strpos($inputTag'<=') === 0)
        {
            
$strippedTag 'v'.str_replace('<='''$inputTag);
            foreach (
$tagArray as $version)
            {
                if(
version_compare($strippedTagstrtolower($version)) == || version_compare($strippedTagstrtolower($version)) == 0)
                {
                    
$tag $version;
                }
            }
            if(!
in_array($tag$tagArray))
            {
                echo 
"error: invalid version tag in composer.json";
                exit();
            }
            return 
$tag;
        }
        else if(
strpos($inputTag'<') === 0)
        {
            
$strippedTag 'v'.str_replace('<'''$inputTag);
            foreach (
$tagArray as $version)
            {
                if(
version_compare($strippedTagstrtolower($version)) == 1)
                {
                    
$tag $version;
                }
            }
            if(!
in_array($tag$tagArray))
            {
                echo 
"error: invalid version tag in composer.json";
                exit();
            }
            return 
$tag;
        }
        else if(
strpos($inputTag'*'))
        {
            
$exp explode('*'$inputTag);
            
$tag 'v'.str_replace('*''0'$inputTag);

            foreach (
$tagArray as $version)
            {
                if(
strpos($version$exp['0']) == && version_compare($tag$version) == -1)
                {
                    
$tag $version;
                }
            }
            if(!
in_array($tag$tagArray))
            {
                echo 
"error: invalid version tag in composer.json";
                exit();
            }
            return 
$tag;
        }
        else
        {
            
$inputTag str_replace('dev-'''$inputTag);
            if(!
in_array($inputTag$tagArray) && !in_array($inputTag$branchArray))
            {
                echo 
"error: invalid version tag or branch in composer.json";
                exit();
            }
            return 
$inputTag;
        }
    }
}

/*
 * extract the tags/branches from github reference API response
 */
function extractRef($url)
{
    
$reference json_decode(curlExec($url));
    if(
strpos($reference['0']->ref'refs/tags/') === 0)
    {
        foreach (
$reference as $ref)
        {
            
$array[] = str_replace('refs/tags/'''$ref->ref);
        }
    }
    else 
    {
        foreach (
$reference as $ref)
        {
            
$array[] = str_replace('refs/heads/'''$ref->ref);
        }
    }
    
    return 
$array;
}
/**
 * @param array $dependency
 * @param array $installDir    directory where the dependency must be copied to
 * @param array $processed contains list of directories already scanned for dependency
 */
function customInstall($dependency$installDir, &$processed) {
    
$tagUrl sprintf('https://api.github.com/repos/%s/%s/git/refs/tags/',
            
$dependency['group'], $dependency['artifact']);
    
$branchUrl sprintf('https://api.github.com/repos/%s/%s/git/refs/heads/',
            
$dependency['group'], $dependency['artifact']);
    
$branchArray extractRef($branchUrl);
    
$tagsArray extractRef($tagUrl);
    
$dependency['branch'] = getTag($dependency['branch'], $tagsArray$branchArray);
    
// download zip from github
    
$downloadUrl sprintf('https://api.github.com/repos/%s/%s/zipball/%s',
            
$dependency['group'], $dependency['artifact'], $dependency['branch']);
    if(!
in_array($downloadUrl$processed)) {
        echo 
"Downloading " $dependency['artifact'] . ' - ' $dependency['branch'] . PHP_EOL;
        
$dest 'vendor/' $installDir '/';
        
$fileZip tempnam(sys_get_temp_dir(), 'ppzip');
        
$fp fopen($fileZip"w");

        
curlExec($downloadUrl$fp);
        
$processed[] = $downloadUrl;

        
// extract the downloaded zip
        
$zip = new ZipArchive;
        if(
$zip->open($fileZip) != "true") {
            echo 
PHP_EOL "Could not open $fileZip";
            exit;
        }
        
$zip->extractTo($dest);
        
$zip->close();
        
fclose($fp);
        
unlink($fileZip);

        
// scan extracted directory for nested dependency
        
foreach (glob("$dest/**/composer.json") as $composer) {
            
$json file_get_contents($composer);
            
$json_a json_decode($jsontrue);
            
$dependencies =  getDependency($json_a);
            foreach (
$dependencies as $dependency ) {
                
customInstall($dependency$dependency['group'], $processed);
            }
        }
    }
}

/*
 * @param array $json_a composer.json converted to array
*/
function getDependency($json_a) {
    if( !
array_key_exists('require'$json_a)) {
        return array();
    }

    
$res = array();
    foreach (
$json_a['require'] as $key => $val) {
        if(
strpos($key'/')) {
            
$parts explode('/'$key);
            
// Convert versions such as "dev-xyz" to "xyz"
            
$pos strpos($val'-');
            if(
$pos == null || empty($pos)) {
                
$branch $val;
            } else {
                
$branch substr($val$pos+1);
            }
            
$batch['group'] = $parts[0] ;
            
$batch['artifact'] = $parts[1];
            
$batch['branch'] = $branch;
            
$res[] = $batch;
        }

    }
    return 
$res;
}

/*
 * curl execute
* @param $targetUrl url to hit
* @param $writeToFile file to which the received data to be written
*/
function curlExec($targetUrl$writeToFile null) {
    
$ch curl_init();
    
curl_setopt($chCURLOPT_URL$targetUrl);
    
curl_setopt($chCURLOPT_FAILONERRORtrue);
    
curl_setopt($chCURLOPT_HEADER0);
    
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
    
curl_setopt($chCURLOPT_AUTOREFERERtrue);
    
curl_setopt($chCURLOPT_USERAGENT'curl/installScript');
    
curl_setopt($chCURLOPT_BINARYTRANSFER,true);
    
curl_setopt($chCURLOPT_TIMEOUT60);
    
curl_setopt ($chCURLOPT_SSL_VERIFYHOST0);
    
curl_setopt ($chCURLOPT_SSL_VERIFYPEER0);
    
curl_setopt ($chCURLOPT_RETURNTRANSFERtrue);
    if(
$writeToFile != null)
    {
        
curl_setopt($chCURLOPT_FILE$writeToFile);
    }
    
$res curl_exec($ch);
    if (!
$res) {
        echo 
PHP_EOL "cURL error number:" .curl_errno($ch) . " for $targetUrl";
        echo 
PHP_EOL "cURL error:" curl_error($ch);
        exit;
    }
    
curl_close($ch);
    return 
$res;
}

/*
 * get the namespace and file location map
* @param array $json_a composer.json converted to array
* @param $composerPath path to composer.json
*/
function getNamespaceMapping($json_a$composerPath)
{
    
$composerPath str_replace("composer.json"""$composerPath);
    if(!isset(
$json_a['autoload']['psr-0']))
    {
        echo 
"error: This install script only suppoorts namespace based SDK";
        exit();
    }
    foreach (
$json_a['autoload']['psr-0'] as $namespace => $path)
    {
        
$pathArr[$namespace] = 'self::$vendorDir .' $composerPath.$path;
    }
    return 
$pathArr;
}
/**
 * creates namespace map to load the classes
 */
function createAutoload() {
    
$dest 'vendor/paypal/';
    
$classes = array();
    foreach (
glob("$dest/**/composer.json") as $composer) {
        
$json file_get_contents($composer);
        
$json_a json_decode($jsontrue);

        foreach (
getNamespaceMapping($json_a$composer) as $key => $value) {
            
$classes[$key][] = $value;
        }

    }

    
$libraryPath dirname(__FILE__) . '/';
    
$loaderClass 'PPAutoloader';
    
$loaderFile  $loaderClass '.php';
    echo 
"Generating autoload file ".PHP_EOL;

    
ksort($classesSORT_STRING);
    
$classList var_export($classestrue);
    
$classList str_replace('\'self::$vendorDir .''self::$vendorDir .\'/' $classList);
    
$script = <<< SCRIPT
<?php
     /**
      * Basic class-map auto loader generated by install.php.
      * Do not modify.
      */
     class 
{$loaderClass} {
        private static \$vendorDir = '';
        private static \$map = array();

        public static function loadClass(\$class) {
            if ('\\\\' == \$class[0]) {
                \$class = substr(\$class, 1);
            }
            
            if (false !== \$pos = strrpos(\$class, '\\\\')) {
                // namespaced class name
                \$classPath = str_replace('\\\\', DIRECTORY_SEPARATOR, substr(\$class, 0, \$pos)) . DIRECTORY_SEPARATOR;
                \$className = substr(\$class, \$pos + 1);
            } else {
                // PEAR-like class name
                \$classPath = null;
                \$className = \$class;
            }
            
            \$classPath .= str_replace('_', DIRECTORY_SEPARATOR, \$className) . '.php';
            foreach (self::\$map as \$prefix => \$dirs) {
                if (0 === strpos(\$class, \$prefix)) {
                    foreach (\$dirs as \$dir) {
                        if (file_exists(\$dir . DIRECTORY_SEPARATOR . \$classPath)) {
                            include \$dir . DIRECTORY_SEPARATOR . \$classPath;
                            return;
                        }
                    }
                }
            }
        }

        public static function register() {
            self::\$vendorDir = dirname(__FILE__);
             self::\$map = 
{$classList};
            spl_autoload_register(array(__CLASS__, 'loadClass'), true);
        }
}
SCRIPT;

    
file_put_contents($loaderFile$script);

}

/**
 * Creates a config file if one is not present
 * @param string $configFile name of the configuration file
 */
function createConfig($configFile) {
    if(!
file_exists($configFile)) {
        echo 
"Generating $configFile. You must update it with your account details." PHP_EOL;
        
$script = <<< SCRIPT

; Integration mode - Must be one of sandbox/live
mode = sandbox

;Account credentials
[Account]
; Update your account credentials from developer portal here
acct1.UserName =
acct1.Password =
acct1.Signature =

;Connection Information
[Http]
http.ConnectionTimeOut = 30
http.Retry = 1

;Logging Information
[Log]
log.FileName=PayPal.log
log.LogLevel=INFO
log.LogEnabled=true

SCRIPT;
        
file_put_contents($configFile$script);
    }
}

/**
 *  initiates and installs the SDK
 */
function createBootStrap($bootstrapFile) {

    if(!
file_exists($bootstrapFile)) {
        
$script = <<< SCRIPT
<?php
/**
 * Include this file in your application. This file sets up the required classloader based on
 * whether you used composer or the custom installer.
 */

// Let the SDK know where the config file resides.
define('PP_CONFIG_PATH', dirname(__FILE__));

if(file_exists( dirname(__FILE__). '/vendor/autoload.php')) {
    require 'vendor/autoload.php';
} else {
    require 'PPAutoloader.php';
    PPAutoloader::register();
}
SCRIPT;
        
file_put_contents($bootstrapFile$script);
    }
}