thomascube
2012-01-18 7fe3811c65a7c63154f03610e289a6d196f3ae2e
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                            |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
bd911b 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Setup the command line environment and provide some utitlity        |
16  |   functions.                                                          |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20
21  $Id$
22
23 */
24
25 if (php_sapi_name() != 'cli') {
26   die('Not on the "shell" (php-cli).');
27 }
28
4351f7 29 require_once INSTALL_PATH . 'program/include/iniset.php';
bd911b 30
e87768 31 // Unset max. execution time limit, set to 120 seconds in iniset.php
A 32 @set_time_limit(0);
bd911b 33
T 34 /**
35  * Parse commandline arguments into a hash array
36  */
37 function get_opt($aliases=array())
38 {
39     $args = array();
40     for ($i=1; $i<count($_SERVER['argv']); $i++)
41     {
42         $arg = $_SERVER['argv'][$i];
43         if (substr($arg, 0, 2) == '--')
44         {
45             $sp = strpos($arg, '=');
46             $key = substr($arg, 2, $sp - 2);
47             $value = substr($arg, $sp+1);
48         }
49         else if ($arg{0} == '-')
50         {
51             $key = substr($arg, 1);
52             $value = $_SERVER['argv'][++$i];
53         }
54         else
55             continue;
56
57         $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
58         
59         if ($alias = $aliases[$key])
60             $args[$alias] = $args[$key];
61     }
62
63     return $args;
64 }
65
66
67 /**
68  * from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
69  */
70 function prompt_silent($prompt = "Password:")
71 {
72   if (preg_match('/^win/i', PHP_OS)) {
73     $vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
74     file_put_contents($vbscript, 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))');
75     $command = "cscript //nologo " . escapeshellarg($vbscript);
76     $password = rtrim(shell_exec($command));
77     unlink($vbscript);
78     return $password;
79   }
80   else {
81     $command = "/usr/bin/env bash -c 'echo OK'";
82     if (rtrim(shell_exec($command)) !== 'OK') {
83       echo $prompt;
84       $pass = trim(fgets(STDIN));
85       echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
86       return $pass;
87     }
88     $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
89     $password = rtrim(shell_exec($command));
90     echo "\n";
91     return $password;
92   }
93 }
94
7fe381 95 ?>