Page MenuHomePhabricator

D11443.id27497.diff
No OneTemporary

D11443.id27497.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -179,6 +179,7 @@
'PhutilHangForeverDaemon' => 'daemon/torture/PhutilHangForeverDaemon.php',
'PhutilHelpArgumentWorkflow' => 'parser/argument/workflow/PhutilHelpArgumentWorkflow.php',
'PhutilHgsprintfTestCase' => 'xsprintf/__tests__/PhutilHgsprintfTestCase.php',
+ 'PhutilINIParserException' => 'parser/exception/PhutilINIParserException.php',
'PhutilIPAddress' => 'ip/PhutilIPAddress.php',
'PhutilIPAddressTestCase' => 'ip/__tests__/PhutilIPAddressTestCase.php',
'PhutilInRequestKeyValueCache' => 'cache/PhutilInRequestKeyValueCache.php',
@@ -408,7 +409,9 @@
'phutil_get_library_root' => 'moduleutils/moduleutils.php',
'phutil_get_library_root_for_path' => 'moduleutils/moduleutils.php',
'phutil_get_signal_name' => 'future/exec/execx.php',
+ 'phutil_glob_to_regex' => 'utils/utils.php',
'phutil_implode_html' => 'markup/render.php',
+ 'phutil_ini_decode' => 'utils/utils.php',
'phutil_is_hiphop_runtime' => 'utils/utils.php',
'phutil_is_utf8' => 'utils/utf8.php',
'phutil_is_utf8_slowly' => 'utils/utf8.php',
@@ -604,6 +607,7 @@
'PhutilHangForeverDaemon' => 'PhutilTortureTestDaemon',
'PhutilHelpArgumentWorkflow' => 'PhutilArgumentWorkflow',
'PhutilHgsprintfTestCase' => 'PhutilTestCase',
+ 'PhutilINIParserException' => 'Exception',
'PhutilIPAddress' => 'Phobject',
'PhutilIPAddressTestCase' => 'PhutilTestCase',
'PhutilInRequestKeyValueCache' => 'PhutilKeyValueCache',
diff --git a/src/parser/exception/PhutilINIParserException.php b/src/parser/exception/PhutilINIParserException.php
new file mode 100644
--- /dev/null
+++ b/src/parser/exception/PhutilINIParserException.php
@@ -0,0 +1,3 @@
+<?php
+
+final class PhutilINIParserException extends Exception {}
diff --git a/src/utils/__tests__/PhutilUtilsTestCase.php b/src/utils/__tests__/PhutilUtilsTestCase.php
--- a/src/utils/__tests__/PhutilUtilsTestCase.php
+++ b/src/utils/__tests__/PhutilUtilsTestCase.php
@@ -535,6 +535,49 @@
}
}
+ public function testPhutilINIDecode() {
+ $valid_cases = array(
+ '' => array(),
+ 'foo=bar' => array('foo' => 'bar'),
+ 'foo = bar' => array('foo' => 'bar'),
+ "foo = bar\n" => array('foo' => 'bar'),
+ "[foo]\nbar = baz" => array('foo' => array('bar' => 'baz')),
+ "[foo]\n[bar]\nbaz = foo" => array(
+ 'foo' => array(),
+ 'bar' => array('baz' => 'foo'),
+ ),
+ "[foo]\nbar = baz\n\n[bar]\nbaz = foo" => array(
+ 'foo' => array('bar' => 'baz'),
+ 'bar' => array('baz' => 'foo'),
+ ),
+ "; Comment\n[foo]\nbar = baz" => array('foo' => array('bar' => 'baz')),
+ "# Comment\n[foo]\nbar = baz" => array('foo' => array('bar' => 'baz')),
+ );
+
+ foreach ($valid_cases as $input => $expect) {
+ $result = phutil_ini_decode($input);
+ $this->assertEqual($expect, $result, 'phutil_ini_decode('.$input.')');
+ }
+
+ $invalid_cases = array(
+ '[' =>
+ 'syntax error, unexpected $end, expecting \']\' in Unknown on line 1',
+ "[\nfoo\n]\nbar = baz\n" =>
+ 'syntax error, unexpected $end, expecting \']\' in Unknown on line 1',
+ );
+
+ foreach ($invalid_cases as $input => $expect) {
+ $caught = null;
+ try {
+ phutil_ini_decode($input);
+ } catch (Exception $ex) {
+ $caught = $ex;
+ }
+ $this->assertTrue($caught instanceof PhutilINIParserException);
+ $this->assertEqual($expect, $caught->getMessage());
+ }
+ }
+
public function testCensorCredentials() {
$cases = array(
'' => '',
@@ -616,4 +659,72 @@
phutil_var_export(new PhutilTestPhobject()));
}
+ public function testGlobToRegex() {
+ $cases = array(
+ '' => array(
+ array(''),
+ array('f', '/'),
+ ),
+ '*' => array(
+ array('foo'),
+ array('foo/', '/foo'),
+ ),
+ '**' => array(
+ array('foo', 'foo/', '/foo', 'foo/bar/baz'),
+ array(),
+ ),
+ 'foo.*' => array(
+ array('foo.php', 'foo.a', 'foo.'),
+ array('fooo.php', 'foo.php/foo'),
+ ),
+ 'fo?' => array(
+ array('foo', 'fot'),
+ array('fooo', 'ffoo', 'fo/'),
+ ),
+ 'fo{o,t}' => array(
+ array('foo', 'fot'),
+ array('fob', 'fo/'),
+ ),
+ 'foo(bar|foo)' => array(
+ array('foo(bar|foo)'),
+ array('foobar', 'foofoo'),
+ ),
+ 'foo,bar' => array(
+ array('foo,bar'),
+ array('foo', 'bar'),
+ ),
+ 'fo{o,\\,}' => array(
+ array('foo', 'fo,'),
+ array(),
+ ),
+ 'fo{o,\\\\}' => array(
+ array('foo', 'fo\\'),
+ array(),
+ ),
+ '/foo' => array(
+ array('/foo'),
+ array('foo'),
+ ),
+ );
+
+ foreach ($cases as $input => $expect) {
+ list($matches, $no_matches) = $expect;
+
+ $regex = phutil_glob_to_regex($input);
+ PhutilTypeSpec::newFromString('regex')->check($regex);
+
+ foreach ($matches as $match) {
+ $this->assertTrue(
+ (bool)preg_match($regex, $match),
+ pht('Expecting "%s" to match "%s".', $regex, $match));
+ }
+
+ foreach ($no_matches as $no_match) {
+ $this->assertFalse(
+ (bool)preg_match($regex, $no_match),
+ pht('Expecting "%s" not to match "%s".', $regex, $no_match));
+ }
+ }
+ }
+
}
diff --git a/src/utils/utils.php b/src/utils/utils.php
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -1058,6 +1058,34 @@
/**
+ * Decode an INI string.
+ *
+ * @param string
+ * @return mixed
+ */
+function phutil_ini_decode($string) {
+ $trap = new PhutilErrorTrap();
+
+ if (function_exists('parse_ini_string')) {
+ $result = @parse_ini_string($string, true);
+ } else {
+ $tmp = new TempFile();
+ Filesystem::writeFile($tmp, $string);
+ $full_path = (string)$tmp;
+
+ $result = @parse_ini_file($full_path, true);
+ }
+
+ if ($result === false) {
+ throw new PhutilINIParserException(trim($trap->getErrorsAsString()));
+ }
+
+ $trap->destroy();
+ return $result;
+}
+
+
+/**
* Attempt to censor any plaintext credentials from a string.
*
* The major use case here is to censor usernames and passwords from command
@@ -1121,3 +1149,65 @@
// Let PHP handle everything else.
return var_export($var, true);
}
+
+/**
+ * Returns a regular expression which is equivalent to the given glob pattern.
+ *
+ * This function was adapted from
+ * https://github.com/symfony/Finder/blob/master/Glob.php.
+ *
+ * @param string A glob pattern.
+ * @return regex
+ */
+function phutil_glob_to_regex($glob) {
+ $escaping = false;
+ $in_curlies = 0;
+ $regex = '';
+
+ for ($i = 0; $i < strlen($glob); $i++) {
+ $char = $glob[$i];
+ $next_char = ($i < strlen($glob) - 1) ? $glob[$i + 1] : null;
+
+ if (in_array($char, array('.', '(', ')', '|', '+', '^', '$'))) {
+ $regex .= "\\$char";
+ } else if ($char === '*') {
+ if ($escaping) {
+ $regex .= '\\*';
+ } else {
+ if ($next_char === '*') {
+ $regex .= '.*';
+ } else {
+ $regex .= '[^/]*';
+ }
+ }
+ } else if ($char === '?') {
+ $regex .= $escaping ? '\\?' : '[^/]';
+ } else if ($char === '{') {
+ $regex .= $escaping ? '\\{' : '(';
+ if (!$escaping) {
+ ++$in_curlies;
+ }
+ } else if ($char === '}' && $in_curlies) {
+ $regex .= $escaping ? '}' : ')';
+ if (!$escaping) {
+ --$in_curlies;
+ }
+ } else if ($char === ',' && $in_curlies) {
+ $regex .= $escaping ? ',' : '|';
+ } else if ($char === '\\') {
+ if ($escaping) {
+ $regex .= '\\\\';
+ $escaping = false;
+ } else {
+ $escaping = true;
+ }
+
+ continue;
+ } else {
+ $regex .= $char;
+ }
+ $escaping = false;
+ }
+
+ return '#^'.$regex.'$#';
+}

File Metadata

Mime Type
text/plain
Expires
Sun, May 12, 10:28 PM (3 w, 3 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6291854
Default Alt Text
D11443.id27497.diff (7 KB)

Event Timeline