~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to libphutil/src/utils/utils.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-01-29 00:15:58 UTC
  • mfrom: (0.16.1) (0.15.1) (0.12.2) (2.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20150129001558-na84707j70qqla7z
Tags: 0~git20150129-1
* New snapshot release
* restricted access to local config file (closes: #775479)
* moved local config file to /var/lib/phabricator (closes: #775478)
* switched mysql-server dependency to recommends (closes: #773536)
* use /run instead of /var/run (closes: #775803)
* prevent package reinstall from overwritting local changes (closes: #776288)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1058
1058
 
1059
1059
 
1060
1060
/**
 
1061
 * Decode an INI string.
 
1062
 *
 
1063
 * @param  string
 
1064
 * @return mixed
 
1065
 */
 
1066
function phutil_ini_decode($string) {
 
1067
  $results = null;
 
1068
  $trap = new PhutilErrorTrap();
 
1069
 
 
1070
  try {
 
1071
    if (!function_exists('parse_ini_string')) {
 
1072
      throw new PhutilMethodNotImplementedException(
 
1073
        pht(
 
1074
          '%s is not compatible with your version of PHP (%s). This function '.
 
1075
          'is only supported on PHP versions newer than 5.3.0.',
 
1076
          __FUNCTION__,
 
1077
          phpversion()));
 
1078
    }
 
1079
 
 
1080
    $results = @parse_ini_string($string, true, INI_SCANNER_RAW);
 
1081
 
 
1082
    if ($results === false) {
 
1083
      throw new PhutilINIParserException(trim($trap->getErrorsAsString()));
 
1084
    }
 
1085
 
 
1086
    foreach ($results as $section => $result) {
 
1087
      if (!is_array($result)) {
 
1088
        // We JSON decode the value in ordering to perform the following
 
1089
        // conversions:
 
1090
        //
 
1091
        //   - `'true'` => `true`
 
1092
        //   - `'false'` => `false`
 
1093
        //   - `'123'` => `123`
 
1094
        //   - `'1.234'` => `1.234`
 
1095
        //
 
1096
        $result = json_decode($result, true);
 
1097
 
 
1098
        if ($result !== null && !is_array($result)) {
 
1099
          $results[$section] = $result;
 
1100
        }
 
1101
 
 
1102
        continue;
 
1103
      }
 
1104
 
 
1105
      foreach ($result as $key => $value) {
 
1106
        $value = json_decode($value, true);
 
1107
 
 
1108
        if ($value !== null && !is_array($value)) {
 
1109
          $results[$section][$key] = $value;
 
1110
        }
 
1111
      }
 
1112
    }
 
1113
  } catch (Exception $ex) {
 
1114
    $trap->destroy();
 
1115
    throw $ex;
 
1116
  }
 
1117
 
 
1118
  $trap->destroy();
 
1119
  return $results;
 
1120
}
 
1121
 
 
1122
 
 
1123
/**
1061
1124
 * Attempt to censor any plaintext credentials from a string.
1062
1125
 *
1063
1126
 * The major use case here is to censor usernames and passwords from command
1121
1184
  // Let PHP handle everything else.
1122
1185
  return var_export($var, true);
1123
1186
}
 
1187
 
 
1188
 
 
1189
/**
 
1190
 * An improved version of `fnmatch`.
 
1191
 *
 
1192
 * @param  string  A glob pattern.
 
1193
 * @param  string  A path.
 
1194
 * @return bool
 
1195
 */
 
1196
function phutil_fnmatch($glob, $path) {
 
1197
  // Modify the glob to allow `**/` to match files in the root directory.
 
1198
  $glob = preg_replace('@(?:(?<!\\\\)\\*){2}/@', '{,*/,**/}', $glob);
 
1199
 
 
1200
  $escaping = false;
 
1201
  $in_curlies = 0;
 
1202
  $regex = '';
 
1203
 
 
1204
  for ($i = 0; $i < strlen($glob); $i++) {
 
1205
    $char = $glob[$i];
 
1206
    $next_char = ($i < strlen($glob) - 1) ? $glob[$i + 1] : null;
 
1207
 
 
1208
    $escape = array('$', '(', ')', '+', '.', '^', '|');
 
1209
    $mapping = array();
 
1210
 
 
1211
    if ($escaping) {
 
1212
      $escape[] = '*';
 
1213
      $escape[] = '?';
 
1214
      $escape[] = '{';
 
1215
    } else {
 
1216
      $mapping['*'] = $next_char === '*' ? '.*' : '[^/]*';
 
1217
      $mapping['?'] = '[^/]';
 
1218
      $mapping['{'] = '(';
 
1219
 
 
1220
      if ($in_curlies) {
 
1221
        $mapping[','] = '|';
 
1222
        $mapping['}'] = ')';
 
1223
      }
 
1224
    }
 
1225
 
 
1226
    if (in_array($char, $escape)) {
 
1227
      $regex .= "\\{$char}";
 
1228
    } else if ($replacement = idx($mapping, $char)) {
 
1229
      $regex .= $replacement;
 
1230
    } else if ($char === '\\') {
 
1231
      if ($escaping) {
 
1232
        $regex .= '\\\\';
 
1233
      }
 
1234
      $escaping = !$escaping;
 
1235
      continue;
 
1236
    } else {
 
1237
      $regex .= $char;
 
1238
    }
 
1239
 
 
1240
    if ($char === '{' && !$escaping) {
 
1241
      $in_curlies++;
 
1242
    } else if ($char === '}' && $in_curlies && !$escaping) {
 
1243
      $in_curlies--;
 
1244
    }
 
1245
 
 
1246
    $escaping = false;
 
1247
  }
 
1248
 
 
1249
  if ($in_curlies || $escaping) {
 
1250
    throw new InvalidArgumentException(pht('Invalid glob pattern.'));
 
1251
  }
 
1252
 
 
1253
  $regex = '(\A'.$regex.'\z)';
 
1254
  return (bool)preg_match($regex, $path);
 
1255
}