thomascube
2011-03-01 715c7961ba8ff72fe40720bb4feaa7865e57e8b9
commit | author | age
bd911b 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/clisetup.php                                          |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
e224b0 8  | Copyright (C) 2010, The Roundcube Dev Team                            |
bd911b 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Setup the command line environment and provide some utitlity        |
13  |   functions.                                                          |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22 if (php_sapi_name() != 'cli') {
23   die('Not on the "shell" (php-cli).');
24 }
25
26 require_once 'iniset.php';
27
28
29 /**
30  * Parse commandline arguments into a hash array
31  */
32 function get_opt($aliases=array())
33 {
34     $args = array();
35     for ($i=1; $i<count($_SERVER['argv']); $i++)
36     {
37         $arg = $_SERVER['argv'][$i];
38         if (substr($arg, 0, 2) == '--')
39         {
40             $sp = strpos($arg, '=');
41             $key = substr($arg, 2, $sp - 2);
42             $value = substr($arg, $sp+1);
43         }
44         else if ($arg{0} == '-')
45         {
46             $key = substr($arg, 1);
47             $value = $_SERVER['argv'][++$i];
48         }
49         else
50             continue;
51
52         $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
53         
54         if ($alias = $aliases[$key])
55             $args[$alias] = $args[$key];
56     }
57
58     return $args;
59 }
60
61
62 /**
63  * from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
64  */
65 function prompt_silent($prompt = "Password:")
66 {
67   if (preg_match('/^win/i', PHP_OS)) {
68     $vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
69     file_put_contents($vbscript, 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))');
70     $command = "cscript //nologo " . escapeshellarg($vbscript);
71     $password = rtrim(shell_exec($command));
72     unlink($vbscript);
73     return $password;
74   }
75   else {
76     $command = "/usr/bin/env bash -c 'echo OK'";
77     if (rtrim(shell_exec($command)) !== 'OK') {
78       echo $prompt;
79       $pass = trim(fgets(STDIN));
80       echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
81       return $pass;
82     }
83     $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
84     $password = rtrim(shell_exec($command));
85     echo "\n";
86     return $password;
87   }
88 }
89
90 ?>