cvs: pear /MDB2_Schema/MDB2/Schema Tool.php
| From: | Christian Weiske | Date: | Fri, 28 Sep 2007 22:37:17 +0000 |
| Subject: | cvs: pear /MDB2_Schema/MDB2/Schema Tool.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48697@lists.php.net to get a copy of this message | ||
cweiske Fri Sep 28 22:37:17 2007 UTC
Modified files:
/pear/MDB2_Schema/MDB2/Schema Tool.php
Log:
Add password reading from stdin
http://cvs.php.net/viewvc.cgi/pear/MDB2_Schema/MDB2/Schema/Tool.php?r1=1.1&r2=1.2&diff_format=u
Index: pear/MDB2_Schema/MDB2/Schema/Tool.php
diff -u pear/MDB2_Schema/MDB2/Schema/Tool.php:1.1 pear/MDB2_Schema/MDB2/Schema/Tool.php:1.2
--- pear/MDB2_Schema/MDB2/Schema/Tool.php:1.1 Fri Sep 28 21:59:33 2007
+++ pear/MDB2_Schema/MDB2/Schema/Tool.php Fri Sep 28 22:37:17 2007
@@ -263,8 +263,9 @@
//read password if necessary
if ($bAskPassword) {
- $strPassword = $this->readPasswordFromStdin();
- //FIXME: Integrate password into DSN
+ $password = $this->readPasswordFromStdin($arg);
+ $arg = self::setPasswordIntoDsn($arg, $password);
+ self::toStdErr($arg);
}
return array('dsn', $arg);
}//protected function getFileOrDsn(&$args)
@@ -272,6 +273,60 @@
/**
+ * Takes a DSN data source name and integrates the given
+ * password into it.
+ *
+ * @param string $dsn Data source name
+ * @param string $password Password
+ *
+ * @return string DSN with password
+ */
+ protected function setPasswordIntoDsn($dsn, $password)
+ {
+ //simple try to integrate password
+ if (strpos($dsn, '@') === false) {
+ //no @ -> no user and no password
+ return str_replace('://', '://:' . $password . '@',
$dsn);
+ } else if (preg_match('|://[^:]+@|', $dsn)) {
+ //user only, no password
+ return str_replace('@', ':' . $password . '@', $dsn);
+ } else if (strpos($dsn, ':@') !== false) {
+ //abstract version
+ return str_replace(':@', ':' . $password . '@', $dsn);
+ }
+
+ return $dsn;
+ }//protected function setPasswordIntoDsn($dsn, $password)
+
+
+
+ /**
+ * Reads a password from stdin
+ *
+ * @param string $dsn DSN name to put into the message
+ *
+ * @return string Password
+ */
+ protected function readPasswordFromStdin($dsn)
+ {
+ $stdin = fopen('php://stdin', 'r');
+ self::toStdErr('Please insert password for ' . $dsn . "\n");
+ $password = '';
+ $breakme = false;
+ while (false !== ($char = fgetc($stdin))) {
+ if (ord($char) == 10 || $char == "\n" || $char == "\r") {
+ break;
+ }
+ $password .= $char;
+ }
+ fclose($stdin);
+
+ return trim($password);
+ }//protected function readPasswordFromStdin()
+
+
+
+ /**
* Creates a database schema dump and sends it to stdout
*
* @param array $args Command line arguments