Example #1
0
/**
 * Format text for terminal output. This function behaves like `sprintf`,
 * except that all the normal conversions (like "%s") will be properly escaped,
 * and additional conversions are supported:
 *
 *   %B (Block)
 *     Escapes text, but preserves tabs and newlines.
 *
 *   %R (Raw String)
 *     Inserts raw, unescaped text. DANGEROUS!
 *
 * Particularly, this will escape terminal control characters.
 */
function tsprintf($pattern)
{
    $args = func_get_args();
    $args[0] = PhutilConsoleFormatter::interpretFormat($args[0]);
    $string = xsprintf('xsprintf_terminal', null, $args);
    return new PhutilTerminalString($string);
}
 public function process(XHPASTNode $root)
 {
     static $functions = array('fprintf' => 1, 'printf' => 0, 'sprintf' => 0, 'vfprintf' => 1, 'csprintf' => 0, 'execx' => 0, 'exec_manual' => 0, 'hgsprintf' => 0, 'hsprintf' => 0, 'jsprintf' => 0, 'pht' => 0, 'phutil_passthru' => 0, 'qsprintf' => 1, 'queryfx' => 1, 'queryfx_all' => 1, 'queryfx_one' => 1, 'vcsprintf' => 0, 'vqsprintf' => 1);
     $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
     foreach ($function_calls as $call) {
         $name = $call->getChildByIndex(0)->getConcreteString();
         $name = strtolower($name);
         $start = idx($functions + $this->printfFunctions, $name);
         if ($start === null) {
             continue;
         }
         $parameters = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
         $argc = count($parameters->getChildren()) - $start;
         if ($argc < 1) {
             $this->raiseLintAtNode($call, pht('This function is expected to have a format string.'));
             continue;
         }
         $format = $parameters->getChildByIndex($start);
         if ($format->getTypeName() != 'n_STRING_SCALAR') {
             continue;
         }
         $argv = array($format->evalStatic()) + array_fill(0, $argc, null);
         try {
             xsprintf(null, null, $argv);
         } catch (BadFunctionCallException $ex) {
             $this->raiseLintAtNode($call, str_replace('xsprintf', $name, $ex->getMessage()));
         } catch (InvalidArgumentException $ex) {
             // Ignore.
         }
     }
 }
Example #3
0
 public function testCreate()
 {
     $cli = $this->getMock('Webcreate\\Util\\Cli', array('execute', 'getOutput', 'getErrorOutput'));
     $cli->expects($this->once())->method('execute')->with(xsprintf('/usr/local/bin/svnadmin create %xs', $this->svndir . '/test_test'))->will($this->returnValue(0));
     $svnadmin = new Svnadmin($this->svndir, '/usr/local/bin/svnadmin', $cli);
     $svnadmin->create('test_test');
 }
Example #4
0
/**
 * Format a regular expression. Supports the following conversions:
 *
 *  %s String
 *    Escapes a string using `preg_quote`.
 *
 *  %R Raw
 *    Inserts a raw regular expression.
 *
 * @param  string  sprintf()-style format string.
 * @param  string  Flags to use with the regular expression.
 * @param  ...     Zero or more arguments.
 * @return string  Formatted string.
 */
function pregsprintf($pattern)
{
    $args = func_get_args();
    $flags = head(array_splice($args, 1, 1));
    $delim = chr(7);
    $userdata = array('delimiter' => $delim);
    $pattern = xsprintf('xsprintf_regex', $userdata, $args);
    return $delim . $pattern . $delim . $flags;
}
 private function renderString($unmasked)
 {
     return xsprintf('xsprintf_command', array('unmasked' => $unmasked, 'mode' => $this->escapingMode), $this->argv);
 }
Example #6
0
/**
 * Format a URI. This function behaves like sprintf(), except that all the
 * normal conversions (like %s) will be properly escaped, and additional
 * conversions are supported:
 *
 *   %s (String)
 *     Escapes text for use in a URI.
 *
 *   %p (Path Component)
 *     Escapes text for use in a URI path component.
 *
 *   %R (Raw String)
 *     Inserts raw, unescaped text. DANGEROUS!
 */
function urisprintf($pattern)
{
    $args = func_get_args();
    return xsprintf('xsprintf_uri', null, $args);
}
Example #7
0
function vqsprintf(PhutilQsprintfInterface $escaper, $pattern, array $argv)
{
    array_unshift($argv, $pattern);
    return xsprintf('xsprintf_query', $escaper, $argv);
}
Example #8
0
/**
 * @group storage
 */
function vqsprintf($conn, $pattern, array $argv)
{
    array_unshift($argv, $pattern);
    return xsprintf('xsprintf_query', $conn, $argv);
}
Example #9
0
/**
 * Format a shell command string. This function behaves like sprintf(), except
 * that all the normal conversions (like %s) will be properly escaped, and
 * additional conversions are supported:
 *
 *   %Ls
 *     List of strings that will be escaped. They will be space separated.
 *
 *   %C (Raw Command)
 *     Passes the argument through without escaping. Dangerous!
 *
 * Generally, you should invoke shell commands via execx() rather than by
 * calling csprintf() directly.
 *
 * @param  string  sprintf()-style format string.
 * @param  ...     Zero or more arguments.
 * @return string  Formatted string, escaped appropriately for shell contexts.
 * @group exec
 */
function csprintf($pattern)
{
    $args = func_get_args();
    return xsprintf('xsprintf_command', null, $args);
}
Example #10
0
function vjsprintf($pattern, array $argv)
{
    array_unshift($argv, $pattern);
    return xsprintf('xsprintf_javascript', null, $argv);
}
Example #11
0
/**
 * Format an LDAP string. This function behaves like sprintf(), except that all
 * the normal conversions (like %s) will be properly escaped, and additional
 * conversions are supported:
 *
 *   %S (Search Filter)
 *     Escapes text for use in a search filter.
 *
 *   %Q (Raw Query)
 *     Inserts raw, unescaped text. DANGEROUS!
 *
 */
function ldap_sprintf($pattern)
{
    $args = func_get_args();
    return xsprintf('xsprintf_ldap', null, $args);
}
Example #12
0
/**
 * Format a Mercurial revset expression. Supports the following conversions:
 *
 *  %s Symbol
 *    Escapes a Mercurial symbol, like a branch or bookmark name.
 *
 *  %R Rrraw Rreferrrence / Rrrrevset
 *    Passes text through unescaped (e.g., an already-escaped revset).
 *
 * @group mercurial
 */
function hgsprintf($pattern)
{
    $args = func_get_args();
    return xsprintf('xsprintf_mercurial', null, $args);
}
Example #13
0
/**
 * @group storage
 */
function vqsprintf(AphrontDatabaseConnection $conn, $pattern, array $argv)
{
    array_unshift($argv, $pattern);
    return xsprintf('xsprintf_query', $conn, $argv);
}
Example #14
0
 public function testDiffSummaryCommandline()
 {
     $svn = $this->svn->setMethods(array('parseDiffXmlResult'))->getMock();
     $svn->setCredentials($this->username, $this->password);
     $expected = xsprintf("%s diff %xs %xs --summarize --xml --non-interactive --username %xs --password %xs", $this->bin, $this->url . '/trunk@2', $this->url . '/trunk@100', $this->username, $this->password);
     $this->cli->expects($this->once())->method('execute')->with($expected)->will($this->returnValue(0));
     // disable the parsing
     $this->adapter->setParser($this->getMock('Webcreate\\Vcs\\Svn\\Parser\\CliParser', array('parse')));
     $result = $svn->diff('/', '', 2, 100, true);
 }
Example #15
0
/**
 * Format text for terminal output. This function behaves like `sprintf`,
 * except that all the normal conversions (like "%s") will be properly escaped,
 * and additional conversions are supported:
 *
 *   %B (Block)
 *     Escapes text, but preserves tabs and newlines.
 *
 *   %R (Raw String)
 *     Inserts raw, unescaped text. DANGEROUS!
 *
 * Particularly, this will escape terminal control characters.
 */
function tsprintf($pattern)
{
    $args = func_get_args();
    $string = xsprintf('xsprintf_terminal', null, $args);
    return new PhutilTerminalString($string);
}
Example #16
0
 /**
  * Internal read handler, which is used by all read functions
  * It also provides a pagination abstraction to the user
  *
  * @todo Find a better pagination abstraction
  *
  * @param string $query Query format string
  * @param mixed ... Query parameters
  * @return ressource MySQLi result of the query
  */
 public static final function _read($query)
 {
     $query = xsprintf($query, 'DB::_escape', '#');
     foreach (DB::$work as $q => $d) {
         if (isset(DB::$con[$q])) {
             DB::$cur =& DB::$con[$q];
             break;
         }
     }
     DB::$work = DB::$args = array();
     DB::$a = 0;
     if (empty(DB::$con[$q])) {
         $r = apc_fetch('database-' . $q);
         if ($q > 0) {
             $r = $r['slaves'];
             DB::_connect($r[array_rand($r)], $q);
         } else {
             DB::_connect($r, $q);
         }
         DB::$cur =& DB::$con[$q];
     }
     mysqli_select_db(DB::$cur, DB_NAME . $q);
     if (null !== DB::$pre_query) {
         mysqli_query(DB::$cur, DB::$pre_query);
         DB::$pre_query = null;
     }
     $s = microtime(true);
     if (array() === DB::$options) {
         if (false === ($res = mysqli_query(DB::$cur, $query, DB::$result))) {
             DB::_log($query);
         }
         DB::$result = 0;
     } else {
         if (empty(DB::$options['table'])) {
             $table = '_rndtbl' . rand(1000, 9999);
         } else {
             $table = DB::$options['table'];
         }
         $param = DB::$options['sort'];
         if (isset($opt['dir']) && 0 === strcasecmp($opt['dir'], 'desc')) {
             $param .= ' DESC';
         }
         if (false === ($res = mysqli_query(DB::$cur, 'CREATE TEMPORARY TABLE ' . $table . ' (KEY SORT(' . $param . ')) ' . $query))) {
             DB::_log($query);
         } else {
             $res->free();
             if (isset($opt['offset'])) {
                 $offset = (int) $opt['offset'];
             } else {
                 $offset = '0';
             }
             $res = mysqli_query(DB::$cur, 'ALTER TABLE ' . $table . ' ADD OFFSET INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, DROP INDEX SORT, ORDER BY ' . $param);
             $res->free();
             if (isset($opt['limit'])) {
                 $param .= ' LIMIT ';
                 $param .= (int) $opt['limit'];
             }
             $res = mysqli_query(DB::$cur, 'SELECT * FROM ' . $table . ' WHERE OFFSET >=' . $offset . ' ORDER BY OFFSET' . $limit, DB::$result);
         }
         DB::$options = array();
     }
     if (class_exists('FB')) {
         FB::log(microtime(true) - $s . " - " . $query);
     }
     return $res;
 }