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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
<?php
require_once 'PEAR/Start.php';
class PEAR_Start_CLI extends PEAR_Start
{
var $descLength;
var $descFormat;
var $first;
var $last;
var $origpwd;
var $tty;
function PEAR_Start_CLI()
{
parent::PEAR_Start();
ini_set('html_errors', 0);
define('WIN32GUI', OS_WINDOWS && php_sapi_name() == 'cli' && System::which('cscript'));
$this->tty = OS_WINDOWS ? @fopen('\con', 'r') : @fopen('/dev/tty', 'r');
if (!$this->tty) {
$this->tty = fopen('php://stdin', 'r');
}
$this->origpwd = getcwd();
$this->config = array_keys($this->configPrompt);
// make indices run from 1...
array_unshift($this->config, "");
unset($this->config[0]);
reset($this->config);
$this->descLength = max(array_map('strlen', $this->configPrompt));
$this->descFormat = "%-{$this->descLength}s";
$this->first = key($this->config);
end($this->config);
$this->last = key($this->config);
PEAR_Command::setFrontendType('CLI');
}
function _PEAR_Start_CLI()
{
if ($this->tty) {
@fclose($this->tty);
}
}
function run()
{
if (PEAR::isError($err = $this->locatePackagesToInstall())) {
return $err;
}
$this->startupQuestion();
$this->setupTempStuff();
$this->getInstallLocations();
$this->displayPreamble();
if (PEAR::isError($err = $this->postProcessConfigVars())) {
return $err;
}
$this->doInstall();
$this->finishInstall();
}
function startupQuestion()
{
if (OS_WINDOWS) {
print "
Are you installing a system-wide PEAR or a local copy?
(system|local) [system] : ";
$tmp = trim(fgets($this->tty, 1024));
if (!empty($tmp) && strtolower($tmp) !== 'system') {
print "Please confirm local copy by typing 'yes' : ";
$tmp = trim(fgets($this->tty, 1024));
if (strtolower($tmp) == 'yes') {
$slash = "\\";
if (strrpos($this->prefix, '\\') === (strlen($this->prefix) - 1)) {
$slash = '';
}
$this->localInstall = true;
$this->pear_conf = '$prefix' . $slash . 'pear.ini';
}
}
} else {
if (get_current_user() == 'root') {
return;
}
$this->pear_conf = $this->safeGetenv('HOME') . '/.pearrc';
}
}
function getInstallLocations()
{
while (true) {
print "
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.
";
foreach ($this->config as $n => $var) {
$fullvar = $this->$var;
foreach ($this->config as $blah => $unused) {
foreach ($this->config as $m => $var2) {
$fullvar = str_replace('$'.$var2, $this->$var2, $fullvar);
}
}
printf("%2d. $this->descFormat : %s\n", $n, $this->configPrompt[$var], $fullvar);
}
print "\n$this->first-$this->last, 'all' or Enter to continue: ";
$tmp = trim(fgets($this->tty, 1024));
if (empty($tmp)) {
if (OS_WINDOWS && !$this->validPHPBin) {
echo "**ERROR**
Please, enter the php.exe path.
";
} else {
break;
}
}
if (isset($this->config[(int)$tmp])) {
$var = $this->config[(int)$tmp];
$desc = $this->configPrompt[$var];
$current = $this->$var;
if (WIN32GUI && $var != 'pear_conf'){
$tmp = $this->win32BrowseForFolder("Choose a Folder for $desc [$current] :");
$tmp.= '\\';
} else {
print "(Use \$prefix as a shortcut for '$this->prefix', etc.)
$desc [$current] : ";
$tmp = trim(fgets($this->tty, 1024));
}
$old = $this->$var;
$this->$var = $$var = $tmp;
if (OS_WINDOWS && $var=='php_bin') {
if ($this->validatePhpExecutable($tmp)) {
$this->php_bin = $tmp;
} else {
$this->php_bin = $old;
}
}
} elseif ($tmp == 'all') {
foreach ($this->config as $n => $var) {
$desc = $this->configPrompt[$var];
$current = $this->$var;
print "$desc [$current] : ";
$tmp = trim(fgets($this->tty, 1024));
if (!empty($tmp)) {
$this->$var = $tmp;
}
}
}
}
}
function validatePhpExecutable($tmp)
{
if (OS_WINDOWS) {
if (strpos($tmp, 'php.exe')) {
$tmp = str_replace('php.exe', '', $tmp);
}
if (file_exists($tmp . DIRECTORY_SEPARATOR . 'php.exe')) {
$tmp = $tmp . DIRECTORY_SEPARATOR . 'php.exe';
$this->php_bin_sapi = $this->win32DetectPHPSAPI();
if ($this->php_bin_sapi=='cgi'){
print "
******************************************************************************
NOTICE! We found php.exe under $this->php_bin, it uses a $this->php_bin_sapi SAPI.
PEAR commandline tool works well with it.
If you have a CLI php.exe available, we recommend using it.
Press Enter to continue...";
$tmp = trim(fgets($this->tty, 1024));
} elseif ($this->php_bin_sapi=='unknown') {
print "
******************************************************************************
WARNING! We found php.exe under $this->php_bin, it uses an $this->php_bin_sapi SAPI.
PEAR commandline tool has NOT been tested with it.
If you have a CLI (or CGI) php.exe available, we strongly recommend using it.
Press Enter to continue...";
$tmp = trim(fgets($this->tty, 1024));
}
echo "php.exe (sapi: $this->php_bin_sapi) found.\n\n";
return $this->validPHPBin = true;
} else {
echo "**ERROR**: not a folder, or no php.exe found in this folder.
Press Enter to continue...";
$tmp = trim(fgets($this->tty, 1024));
return $this->validPHPBin = false;
}
}
}
/**
* Create a vbs script to browse the getfolder dialog, called
* by cscript, if it's available.
* $label is the label text in the header of the dialog box
*
* TODO:
* - Do not show Control panel
* - Replace WSH with calls to w32 as soon as callbacks work
* @author Pierrre-Alain Joye
*/
function win32BrowseForFolder($label)
{
static $wshSaved=false;
static $cscript='';
$wsh_browserfolder = 'Option Explicit
Dim ArgObj, var1, var2, sa, sFld
Set ArgObj = WScript.Arguments
Const BIF_EDITBOX = &H10
Const BIF_NEWDIALOGSTYLE = &H40
Const BIF_RETURNONLYFSDIRS = &H0001
Const BIF_DONTGOBELOWDOMAIN = &H0002
Const BIF_STATUSTEXT = &H0004
Const BIF_RETURNFSANCESTORS = &H0008
Const BIF_VALIDATE = &H0020
Const BIF_BROWSEFORCOMPUTER = &H1000
Const BIF_BROWSEFORPRINTER = &H2000
Const BIF_BROWSEINCLUDEFILES = &H4000
Const OFN_LONGNAMES = &H200000
Const OFN_NOLONGNAMES = &H40000
Const ssfDRIVES = &H11
Const ssfNETWORK = &H12
Set sa = CreateObject("Shell.Application")
var1=ArgObj(0)
Set sFld = sa.BrowseForFolder(0, var1, BIF_EDITBOX + BIF_VALIDATE + BIF_BROWSEINCLUDEFILES + BIF_RETURNFSANCESTORS+BIF_NEWDIALOGSTYLE , ssfDRIVES )
if not sFld is nothing Then
if not left(sFld.items.item.path,1)=":" Then
WScript.Echo sFld.items.item.path
Else
WScript.Echo "invalid"
End If
Else
WScript.Echo "cancel"
End If
';
if( !$wshSaved){
$cscript = $this->ptmp . DIRECTORY_SEPARATOR . "bf.vbs";
$fh = fopen($cscript, "wb+");
fwrite($fh, $wsh_browserfolder, strlen($wsh_browserfolder));
fclose($fh);
$wshSaved = true;
}
exec('cscript ' . escapeshellarg($cscript) . ' "' . escapeshellarg($label) . '" //noLogo', $arPath);
if (!count($arPath) || $arPath[0]=='' || $arPath[0]=='cancel') {
return '';
} elseif ($arPath[0]=='invalid') {
echo "Invalid Path.\n";
return '';
}
@unlink($cscript);
return $arPath[0];
}
function displayPreamble()
{
if (OS_WINDOWS) {
/*
* Checks PHP SAPI version under windows/CLI
*/
if ($this->php_bin == '') {
print "
We do not find any php.exe, please select the php.exe folder (CLI is
recommended, usually in c:\php\cli\php.exe)
";
$this->validPHPBin = false;
} elseif (strlen($this->php_bin)) {
$this->php_bin_sapi = $this->win32DetectPHPSAPI();
$this->validPHPBin = true;
switch ($this->php_bin_sapi) {
case 'cli':
break;
case 'cgi':
case 'cgi-fcgi':
print "
*NOTICE*
We found php.exe under $this->php_bin, it uses a $this->php_bin_sapi SAPI. PEAR commandline
tool works well with it, if you have a CLI php.exe available, we
recommend using it.
";
break;
default:
print "
*WARNING*
We found php.exe under $this->php_bin, it uses an unknown SAPI. PEAR commandline
tool has not been tested with it, if you have a CLI (or CGI) php.exe available,
we strongly recommend using it.
";
break;
}
}
}
}
function finishInstall()
{
$sep = OS_WINDOWS ? ';' : ':';
$include_path = explode($sep, ini_get('include_path'));
if (OS_WINDOWS) {
$found = false;
$t = strtolower($this->php_dir);
foreach ($include_path as $path) {
if ($t == strtolower($path)) {
$found = true;
break;
}
}
} else {
$found = in_array($this->php_dir, $include_path);
}
if (!$found) {
print "
******************************************************************************
WARNING! The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
<$this->php_dir>
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.
";
if ($php_ini = $this->getPhpiniPath()) {
print "\n\nWould you like to alter php.ini <$php_ini>? [Y/n] : ";
$alter_phpini = !stristr(fgets($this->tty, 1024), "n");
if ($alter_phpini) {
$this->alterPhpIni($php_ini);
} else {
if (OS_WINDOWS) {
print "
Please look over your php.ini file to make sure
$this->php_dir is in your include_path.";
} else {
print "
I will add a workaround for this in the 'pear' command to make sure
the installer works, but please look over your php.ini or Apache
configuration to make sure $this->php_dir is in your include_path.
";
}
}
}
print "
Current include path : ".ini_get('include_path')."
Configured directory : $this->php_dir
Currently used php.ini (guess) : $php_ini
";
print "Press Enter to continue: ";
fgets($this->tty, 1024);
}
$pear_cmd = $this->bin_dir . DIRECTORY_SEPARATOR . 'pear';
$pear_cmd = OS_WINDOWS ? strtolower($pear_cmd).'.bat' : $pear_cmd;
// check that the installed pear and the one in the path are the same (if any)
$pear_old = System::which(OS_WINDOWS ? 'pear.bat' : 'pear', $this->bin_dir);
if ($pear_old && ($pear_old != $pear_cmd)) {
// check if it is a link or symlink
$islink = OS_WINDOWS ? false : is_link($pear_old) ;
if ($islink && readlink($pear_old) != $pear_cmd) {
print "\n** WARNING! The link $pear_old does not point to the " .
"installed $pear_cmd\n";
} elseif (!$this->localInstall && is_writable($pear_old) && !is_dir($pear_old)) {
rename($pear_old, "{$pear_old}_old");
print "\n** WARNING! Backed up old pear to {$pear_old}_old\n";
} else {
print "\n** WARNING! Old version found at $pear_old, please remove it or ".
"be sure to use the new $pear_cmd command\n";
}
}
print "\nThe 'pear' command is now at your service at $pear_cmd\n";
// Alert the user if the pear cmd is not in PATH
$old_dir = $pear_old ? dirname($pear_old) : false;
if (!$this->which('pear', $old_dir)) {
print "
** The 'pear' command is not currently in your PATH, so you need to
** use '$pear_cmd' until you have added
** '$this->bin_dir' to your PATH environment variable.
";
print "Run it without parameters to see the available actions, try 'pear list'
to see what packages are installed, or 'pear help' for help.
For more information about PEAR, see:
http://pear.php.net/faq.php
http://pear.php.net/manual/
Thanks for using go-pear!
";
}
if (OS_WINDOWS && !$this->localInstall) {
$this->win32CreateRegEnv();
}
}
/**
* System::which() does not allow path exclusion
*/
function which($program, $dont_search_in = false)
{
if (OS_WINDOWS) {
if ($_path = $this->safeGetEnv('Path')) {
$dirs = explode(';', $_path);
} else {
$dirs = explode(';', $this->safeGetEnv('PATH'));
}
foreach ($dirs as $i => $dir) {
$dirs[$i] = strtolower(realpath($dir));
}
if ($dont_search_in) {
$dont_search_in = strtolower(realpath($dont_search_in));
}
if ($dont_search_in &&
($key = array_search($dont_search_in, $dirs)) !== false)
{
unset($dirs[$key]);
}
foreach ($dirs as $dir) {
$dir = str_replace('\\\\', '\\', $dir);
if (!strlen($dir)) {
continue;
}
if ($dir{strlen($dir) - 1} != '\\') {
$dir .= '\\';
}
$tmp = $dir . $program;
$info = pathinfo($tmp);
if (isset($info['extension']) && in_array(strtolower($info['extension']),
array('exe', 'com', 'bat', 'cmd'))) {
if (file_exists($tmp)) {
return strtolower($tmp);
}
} elseif (file_exists($ret = $tmp . '.exe') ||
file_exists($ret = $tmp . '.com') ||
file_exists($ret = $tmp . '.bat') ||
file_exists($ret = $tmp . '.cmd')) {
return strtolower($ret);
}
}
} else {
$dirs = explode(':', $this->safeGetEnv('PATH'));
if ($dont_search_in &&
($key = array_search($dont_search_in, $dirs)) !== false)
{
unset($dirs[$key]);
}
foreach ($dirs as $dir) {
if (is_executable("$dir/$program")) {
return "$dir/$program";
}
}
}
return false;
}
/**
* Not optimized, but seems to work, if some nice
* peardev will test it? :)
*
* @author Pierre-Alain Joye <paj@pearfr.org>
*/
function alterPhpIni($pathIni='')
{
$foundAt = array();
$iniSep = OS_WINDOWS ? ';' : ':';
if ($pathIni=='') {
$pathIni = $this->getPhpiniPath();
}
$arrayIni = file($pathIni);
$i=0;
$found=0;
// Looks for each active include_path directives
foreach ($arrayIni as $iniLine) {
$iniLine = trim($iniLine);
$iniLine = str_replace(array("\n", "\r"), array('', ''), $iniLine);
if (preg_match("/^\s*include_path/", $iniLine)) {
$foundAt[] = $i;
$found++;
}
$i++;
}
if ($found) {
$includeLine = $arrayIni[$foundAt[0]];
list(, $currentPath) = explode('=', $includeLine);
$currentPath = trim($currentPath);
if (substr($currentPath,0,1) == '"') {
$currentPath = substr($currentPath, 1, strlen($currentPath) - 2);
}
$arrayPath = explode($iniSep, $currentPath);
$newPath = array();
if ($arrayPath[0]=='.') {
$newPath[0] = '.';
$newPath[1] = $this->php_dir;
array_shift($arrayPath);
} else {
$newPath[0] = $this->php_dir;
}
foreach ($arrayPath as $path) {
$newPath[]= $path;
}
} else {
$newPath = array();
$newPath[0] = '.';
$newPath[1] = $this->php_dir;
$foundAt[] = count($arrayIni); // add a new line if none is present
}
$nl = OS_WINDOWS ? "\r\n" : "\n";
$includepath = 'include_path="' . implode($iniSep,$newPath) . '"';
$newInclude = "$nl$nl;***** Added by go-pear$nl" .
$includepath .
$nl . ";*****" .
$nl . $nl;
$arrayIni[$foundAt[0]] = $newInclude;
for ($i=1; $i<$found; $i++) {
$arrayIni[$foundAt[$i]]=';' . trim($arrayIni[$foundAt[$i]]);
}
$newIni = implode("", $arrayIni);
if (!($fh = @fopen($pathIni, "wb+"))) {
$prefixIni = $this->prefix . DIRECTORY_SEPARATOR . "php.ini-gopear";
$fh = fopen($prefixIni, "wb+");
if (!$fh) {
echo "
******************************************************************************
WARNING: Cannot write to $pathIni nor in $this->prefix/php.ini-gopear. Please
modify manually your php.ini by adding:
$includepath
";
return false;
} else {
fwrite($fh, $newIni, strlen($newIni));
fclose($fh);
echo "
******************************************************************************
WARNING: Cannot write to $pathIni, but php.ini was successfully created
at <$this->prefix/php.ini-gopear>. Please replace the file <$pathIni> with
<$prefixIni> or modify your php.ini by adding:
$includepath
";
}
} else {
fwrite($fh, $newIni, strlen($newIni));
fclose($fh);
echo "
php.ini <$pathIni> include_path updated.
";
}
return true;
}
/**
* Generates a registry addOn for Win32 platform
* This addon set PEAR environment variables
* @author Pierrre-Alain Joye
*/
function win32CreateRegEnv()
{
$nl = "\r\n";
$reg ='REGEDIT4'.$nl.
'[HKEY_CURRENT_USER\Environment]'. $nl .
'"PHP_PEAR_SYSCONF_DIR"="' . addslashes($this->prefix) . '"' . $nl .
'"PHP_PEAR_INSTALL_DIR"="' . addslashes($this->php_dir) . '"' . $nl .
'"PHP_PEAR_DOC_DIR"="' . addslashes($this->doc_dir) . '"' . $nl .
'"PHP_PEAR_BIN_DIR"="' . addslashes($this->bin_dir) . '"' . $nl .
'"PHP_PEAR_DATA_DIR"="' . addslashes($this->data_dir) . '"' . $nl .
'"PHP_PEAR_PHP_BIN"="' . addslashes($this->php_bin) . '"' . $nl .
'"PHP_PEAR_TEST_DIR"="' . addslashes($this->test_dir) . '"' . $nl;
$fh = fopen($this->prefix . DIRECTORY_SEPARATOR . 'PEAR_ENV.reg', 'wb');
if($fh){
fwrite($fh, $reg, strlen($reg));
fclose($fh);
echo "
* WINDOWS ENVIRONMENT VARIABLES *
For convenience, a REG file is available under {$this->prefix}PEAR_ENV.reg .
This file creates ENV variables for the current user.
Double-click this file to add it to the current user registry.
";
}
}
function displayHTMLProgress()
{
}
}
?>
|