]> git.donarmstrong.com Git - roundcube.git/blob - program/include/clisetup.php
Imported Upstream version 0.7
[roundcube.git] / program / include / clisetup.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/clisetup.php                                          |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2010, The Roundcube Dev Team                            |
9  | Licensed under the GNU GPL                                            |
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: clisetup.php 5299 2011-10-03 09:25:33Z alec $
19
20 */
21
22 if (php_sapi_name() != 'cli') {
23   die('Not on the "shell" (php-cli).');
24 }
25
26 require_once INSTALL_PATH . 'program/include/iniset.php';
27
28 // Unset max. execution time limit, set to 120 seconds in iniset.php
29 @set_time_limit(0);
30
31 /**
32  * Parse commandline arguments into a hash array
33  */
34 function get_opt($aliases=array())
35 {
36         $args = array();
37         for ($i=1; $i<count($_SERVER['argv']); $i++)
38         {
39                 $arg = $_SERVER['argv'][$i];
40                 if (substr($arg, 0, 2) == '--')
41                 {
42                         $sp = strpos($arg, '=');
43                         $key = substr($arg, 2, $sp - 2);
44                         $value = substr($arg, $sp+1);
45                 }
46                 else if ($arg{0} == '-')
47                 {
48                         $key = substr($arg, 1);
49                         $value = $_SERVER['argv'][++$i];
50                 }
51                 else
52                         continue;
53
54                 $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
55                 
56                 if ($alias = $aliases[$key])
57                         $args[$alias] = $args[$key];
58         }
59
60         return $args;
61 }
62
63
64 /**
65  * from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
66  */
67 function prompt_silent($prompt = "Password:")
68 {
69   if (preg_match('/^win/i', PHP_OS)) {
70     $vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
71     file_put_contents($vbscript, 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))');
72     $command = "cscript //nologo " . escapeshellarg($vbscript);
73     $password = rtrim(shell_exec($command));
74     unlink($vbscript);
75     return $password;
76   }
77   else {
78     $command = "/usr/bin/env bash -c 'echo OK'";
79     if (rtrim(shell_exec($command)) !== 'OK') {
80       echo $prompt;
81       $pass = trim(fgets(STDIN));
82       echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
83       return $pass;
84     }
85     $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
86     $password = rtrim(shell_exec($command));
87     echo "\n";
88     return $password;
89   }
90 }
91
92 ?>