Beispiel #1
0
 /**
  * Returns a colorized string
  *
  * @param string $string Message to colorize for output
  * @return string
  */
 private function colorize($string)
 {
     $colorization_setting = $this->runner->getConfig('colorize');
     $colorize = $colorization_setting == 'auto' && !\cli\Shell::isPiped() || is_bool($colorization_setting) && $colorization_setting;
     $colorized_string = \cli\Colors::colorize($string, $colorize);
     return $colorized_string;
 }
 /**
  * Emit the message in specified format
  *
  * @params $format string optional - options are "raw","json"
  */
 public static function emit($format = 'raw')
 {
     $messenger = self::instance();
     switch ($format) {
         case 'pantheon':
         case 'json':
             $formatted = array();
             foreach ($messenger->messages as $message) {
                 $formatted[$message['name']] = $message;
             }
             \WP_CLI::print_value($formatted, array('format' => 'json'));
             break;
         case 'raw':
         case 'default':
             foreach ($messenger->messages as $message) {
                 // colorize
                 if ($message['score'] == 2) {
                     $color = "%G";
                 } elseif ($message['score'] == 0) {
                     $color = "%C";
                 } else {
                     $color = "%R";
                 }
                 // @todo might be a better way to do this
                 echo \cli\Colors::colorize(sprintf(str_repeat('-', 80) . PHP_EOL . "%s: (%s) \n%s\nResult:%s %s\nRecommendation: %s\n\n" . PHP_EOL, strtoupper($message['label']), $message['description'], str_repeat('-', 80), $color, $message['result'] . '%n', $message['action']));
             }
             break;
     }
 }
Beispiel #3
0
/**
 * Returns a colorized string
 *
 * @param string $string Message to colorize for output
 * @return string
 */
function colorize($string)
{
    $colorize = true;
    if (Terminus::getConfig('colorize') == 'auto') {
        $colorize = !\cli\Shell::isPiped();
    }
    $colorized_string = \cli\Colors::colorize($string, $colorize);
    return $colorized_string;
}
Beispiel #4
0
 function test_string_cache()
 {
     $string = 'x';
     $string_with_color = '%k' . $string;
     $colorized_string = "{$string}";
     // Ensure colorization works
     $this->assertEquals(\cli\Colors::colorize($string_with_color, true), $colorized_string);
     // Test that the value was cached appropriately
     $test_cache = array('passed' => $string_with_color, 'colorized' => $colorized_string, 'decolorized' => $string);
     $real_cache = \cli\Colors::getStringCache();
     // Test that the cache value exists
     $this->assertTrue(isset($real_cache[md5($string_with_color)]));
     // Test that the cache value is correctly set
     $this->assertEquals($test_cache, $real_cache[md5($string_with_color)]);
 }
    /**
     * Draw simple One column table with colored string
     * Output should look like:
     * +-------------+
     * | Test Header |
     * +-------------+
     * | x           |
     * +-------------+
     *
     * where `x` character has green color.
     * At the same time it checks that `green` defined in `cli\Colors` really looks as `green`.
     */
    public function testDrawOneColumnColoredTable()
    {
        $headers = array('Test Header');
        $rows = array(array(Colors::colorize('%Gx%n', true)));
        // green `x`
        $x = "x";
        $output = <<<OUT
+-------------+
| Test Header |
+-------------+
| {$x}           |
+-------------+

OUT;
        $this->assertInOutEquals(array($headers, $rows), $output);
    }
Beispiel #6
0
 /**
  * Similar to error( $message ), but outputs $message in a red box
  *
  * @param  array $message Message to write.
  */
 public function error_multi_line($message_lines)
 {
     // convert tabs to four spaces, as some shells will output the tabs as variable-length
     $message_lines = array_map(function ($line) {
         return str_replace("\t", '    ', $line);
     }, $message_lines);
     $longest = max(array_map('strlen', $message_lines));
     // write an empty line before the message
     $empty_line = \cli\Colors::colorize('%w%1 ' . str_repeat(' ', $longest) . ' %n');
     $this->write(STDERR, "\n\t{$empty_line}\n");
     foreach ($message_lines as $line) {
         $padding = str_repeat(' ', $longest - strlen($line));
         $line = \cli\Colors::colorize("%w%1 {$line} {$padding}%n");
         $this->write(STDERR, "\t{$line}\n");
     }
     // write an empty line after the message
     $this->write(STDERR, "\t{$empty_line}\n\n");
 }
Beispiel #7
0
 static function coloredOutput($message = "", $print = true)
 {
     $cli = new self('');
     // we're not using regex here because simple str_replace is faster. However,
     // we may need to go that route if this function gets too complex
     $message = str_replace('</M>', '%n', str_replace('<M>', '%M', $message));
     $message = str_replace('</m>', '%n', str_replace('<m>', '%m', $message));
     $message = str_replace('</G>', '%n', str_replace('<G>', '%G', $message));
     $message = str_replace('</y>', '%n', str_replace('<y>', '%y', $message));
     $message = str_replace('</Y>', '%n', str_replace('<Y>', '%Y', $message));
     $message = str_replace('</R>', '%n', str_replace('<R>', '%R', $message));
     $message = str_replace('</r>', '%n', str_replace('<r>', '%r', $message));
     $message = str_replace('</K>', '%n', str_replace('<K>', '%K', $message));
     $message = \cli\Colors::colorize("{$message}", $cli->in_color);
     if ($print) {
         $cli->write(STDOUT, "{$message}\n");
     }
     return $message;
 }
 /**
  * Migrate post content from <img> tags to image shortcodes.
  *
  * ## OPTIONS
  *
  * <id>...
  * : One or more IDs of posts to update.
  *
  * [--dry-run]
  * : Only show the content which is to be changed, don't update posts.
  *
  * ## EXAMPLES
  *
  *     ## Migrate all Posts to the Image Shortcake syntax
  *     wp image-shortcake-shortcode migrate `wp post list --post_type=post` --ids`
  *
  *     ## Converts images to shortcodes on one post, preserving a log to rollback in case of errors.
  *     wp image-shortcake migrate 123 > potential-oops.txt
  *
  *
  * @synopsis <id>... [--dry-run]
  */
 public function migrate($args, $assoc_args)
 {
     foreach (array_filter($args) as $post_ID) {
         $post = $this->fetcher->get_check($post_ID);
         $_content = $post->post_content;
         $caption_replacements = Img_Shortcode_Data_Migration::find_caption_shortcodes_for_replacement($_content);
         $_content = str_replace(array_keys($caption_replacements), array_values($caption_replacements), $_content);
         $img_tag_replacements = Img_Shortcode_Data_Migration::find_img_tags_for_replacement($_content);
         $_content = str_replace(array_keys($img_tag_replacements), array_values($img_tag_replacements), $_content);
         $replacements = array_merge((array) $caption_replacements, (array) $img_tag_replacements);
         WP_CLI::log('');
         if (0 === count($replacements)) {
             WP_CLI::log('Nothing to replace on post ' . $post->ID . '. Skipping.');
             WP_CLI::log('');
             continue;
         }
         $header = 'Image shortcode replacements for post ' . $post->ID;
         WP_CLI::log($header);
         WP_CLI::log(str_repeat('=', strlen($header)));
         WP_CLI::log('');
         foreach ($replacements as $del => $ins) {
             \WP_CLI::log(\cli\Colors::colorize('%C-%n') . $del, true);
             \WP_CLI::log(\cli\Colors::colorize('%G+%n') . $ins, true);
         }
         WP_CLI::log('');
         if (isset($assoc_args['dry-run'])) {
             WP_CLI::log('Post not updated: --dry-run specifed.');
             WP_CLI::log('');
             continue;
         }
         global $wpdb;
         // @codingStandardsIgnoreStart
         $updated = $wpdb->update($wpdb->posts, array('post_content' => $_content), array('ID' => $post_ID));
         // @codingStandardsIgnoreEnd
         if (1 === $updated) {
             clean_post_cache($post);
             WP_CLI::success('Updated post ' . $post->ID . '.');
         } else {
             WP_CLI::warning('There was an unexpected error updating post ' . $post->ID . '.');
         }
     }
 }
 public static function warning($message)
 {
     WP_CLI::log(Colors::colorize('%YWarning: %n' . $message));
 }
Beispiel #10
0
<?php

// Can be used by plugins/themes to check if Terminus is running or not
define('Terminus', true);
define('TERMINUS_VERSION', '0.4-beta');
$source = 'unknown';
if ('cli' === PHP_SAPI && isset($argv)) {
    $source = explode('/', $argv[0]);
    $source = end($source);
}
define('TERMINUS_SCRIPT', $source);
date_default_timezone_set('UTC');
include TERMINUS_ROOT . '/php/utils.php';
include TERMINUS_ROOT . '/php/login.php';
include TERMINUS_ROOT . '/php/FileCache.php';
include TERMINUS_ROOT . '/php/dispatcher.php';
include TERMINUS_ROOT . '/php/class-terminus.php';
include TERMINUS_ROOT . '/php/class-terminus-command.php';
\Terminus\Utils\load_dependencies();
# Set a custom exception handler
set_exception_handler('\\Terminus\\Utils\\handle_exception');
if (isset($_SERVER['TERMINUS_HOST']) && $_SERVER['TERMINUS_HOST'] != '') {
    define('TERMINUS_HOST', $_SERVER['TERMINUS_HOST']);
    \cli\line(\cli\Colors::colorize('%YNote: using custom target "' . $_SERVER['TERMINUS_HOST'] . '"%n'));
} else {
    define('TERMINUS_HOST', 'dashboard.getpantheon.com');
}
define('TERMINUS_PORT', '443');
Terminus::get_runner()->run();
Beispiel #11
0
 /**
  * Output one line of message to a resource.
  *
  * @param string $message Message to write.
  * @param string $label Prefix message with a label.
  * @param string $color Colorize label with a given color.
  * @param resource $handle Resource to write to. Defaults to STDOUT.
  */
 protected function _line($message, $label, $color, $handle = STDOUT)
 {
     $label = \cli\Colors::colorize("{$color}{$label}:%n", $this->in_color);
     $this->write($handle, "{$label} {$message}\n");
 }
Beispiel #12
0
    echo Colors::colorize('Translating : %b' . count($entries) . '%n entries from ' . $from . ' to ' . $to . PHP_EOL);
    $progress = new Bar('Translate status ', count($entries));
    foreach ($entries as $entry => $data) {
        $translate = "";
        $skipped = "";
        if (isset($previousEntries) && !empty($previousEntries[$entry]['msgstr'][0])) {
            $skipped = "(skipped)";
            $translate = $previousEntries[$entry]['msgstr'][0];
        } else {
            $translate = $translator->translate($entry, $from, $to);
        }
        $po->update_entry($entry, $translate);
        if ($verbose) {
            echo $verbose ? " {$entry} => {$translate} {$skipped}" . PHP_EOL : null;
        } else {
            $progress->tick();
        }
    }
    if (!$verbose) {
        $progress->finish();
    }
    echo 'Save output to: ' . $output . PHP_EOL;
    $po->write($output);
} catch (\DownloadException $e) {
    $response = @json_decode($e->getResponse());
    if (isset($response->error->errors[0]->reason) && $response->error->errors[0]->reason === 'keyInvalid') {
        die(Colors::colorize('%rInvalid Google Translate API key%n' . PHP_EOL));
    } else {
        die(Colors::colorize('%rError "' . $e->getMessage() . '"%n' . PHP_EOL));
    }
}
Beispiel #13
0
 /**
  * Installs the folders required for the bootstrap project from repo to project folder.
  *
  * @param string $targetDirectory The directory where to put the files/folders.
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return bool TRUE on success, otherwise FALSE
  * @access protected
  * @static
  */
 protected static function install($targetDirectory)
 {
     $notify = new \cli\notify\Spinner(\cli\Colors::colorize('%N%n%yInstalling ...%N%n'), 100);
     // Define source & destination
     $source = self::getSourcePath();
     $destination = $targetDirectory;
     // Iterate and copy ...
     foreach (self::getFolders() as $folder) {
         self::xcopy($source . $folder, $destination . $folder);
     }
     $notify->finish();
     return true;
 }
 /**
  * Output a line that's appropriately nested
  */
 private function nested_line($line, $change = false)
 {
     if ('add' == $change) {
         $color = '%G';
         $label = '+ ';
     } else {
         if ('remove' == $change) {
             $color = '%R';
             $label = '- ';
         } else {
             $color = false;
             $label = false;
         }
     }
     \cli\Colors::colorize("%n");
     $spaces = $this->output_nesting_level * 2 + 2;
     if ($color && $label) {
         $line = \cli\Colors::colorize("{$color}{$label}") . $line . \cli\Colors::colorize("%n");
         $spaces = $spaces - 2;
     }
     WP_CLI::line(str_pad(' ', $spaces) . $line);
 }
Beispiel #15
0
 /**
  * Returns a colorized string
  *
  * @param [string] $string Message to colorize for output
  * @return [string] $colorized_string
  */
 static function colorize($string)
 {
     $colorized_string = \cli\Colors::colorize($string, self::getRunner()->inColor());
     return $colorized_string;
 }
Beispiel #16
0
 /**
  * Installs the folders required for the bootstrap project from repo to project folder.
  *
  * @param string $targetDirectory The directory where to put the files/folders.
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return bool TRUE on success, otherwise FALSE
  * @static
  */
 protected static function install($targetDirectory)
 {
     $notify = new \cli\notify\Spinner(\cli\Colors::colorize('%N%n%yInstalling bootstrap project ...%N%n'), 100);
     // Define source & destination
     $source = self::getSourcePath();
     $destination = $targetDirectory;
     // Iterate and copy ...
     foreach (self::getFolders() as $folder) {
         self::xcopy($source . $folder, $destination . $folder);
     }
     $target = realpath($destination . 'vendor/maximebf/debugbar/src/DebugBar/Resources');
     $link = realpath($destination . 'web') !== false ? realpath($destination . 'web') . DIRECTORY_SEPARATOR . 'assets' : false;
     if ($target !== false && $link !== false) {
         // Create important symlinks to required assets like for DebugBar
         $symlinked = symlink($target, $link);
     } else {
         $symlinked = false;
     }
     if ($symlinked === false) {
         self::showError('Could not create symlink from "' . $target . '" to "' . $link . '"');
     }
     $notify->finish();
     return true;
 }
Beispiel #17
0
 static function colorize($string)
 {
     return \cli\Colors::colorize($string, self::get_runner()->in_color());
 }
Beispiel #18
0
 /**
  * Echoes a VHost skeleton with correct path inserted.
  *
  * @param string $installPath The install path to insert.
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return void
  * @access protected
  * @static
  */
 protected static function showVhostExample($installPath)
 {
     \cli\line();
     \cli\line('You could use this skeleton (example and not production ready!) for your vhost:');
     \cli\line();
     \cli\line(\cli\Colors::colorize('%y<VirtualHost *:80>'));
     \cli\line('    ServerName www.example.com:80');
     \cli\line('    ServerAlias example.com *.example.com');
     \cli\line('    ServerAdmin webmaster@example.com');
     \cli\line('    DocumentRoot "' . $installPath . DIRECTORY_SEPARATOR . 'web"');
     \cli\line('    <Directory "' . $installPath . DIRECTORY_SEPARATOR . 'web">');
     \cli\line('        Options Indexes FollowSymLinks Includes ExecCGI');
     \cli\line('        AllowOverride All');
     \cli\line('        Order allow,deny');
     \cli\line('        Allow from all');
     \cli\line('        DirectoryIndex app.php index.php index.html index.htm');
     \cli\line('    </Directory>');
     \cli\line('</VirtualHost>');
 }
Beispiel #19
0
 /**
  * Colorizes a string with a given color.
  *
  * @param string $string The string to colorize
  * @param string $color  The color
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return string The colorized string
  */
 protected function colorize($string, $color)
 {
     return \cli\Colors::colorize('%N%n' . $color . $string . '%N%n');
 }
<?php

// Samples. Lines marked with * should be colored in output
// php examples/colors.php
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// php examples/colors.php | cat
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
require_once 'common.php';
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');
echo \cli\Colors::colorize('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n', true) . "\n";
echo \cli\Colors::colorize('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n') . "\n";
\cli\Colors::enable();
// Forcefully enable
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');
\cli\Colors::disable();
// Disable forcefully!
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n', true);
\cli\Colors::enable(false);
// Enable, but not forcefully
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');
 /**
  * Show last  Exploit Scanner scan result.
  *
  * [--export-csv=<file-name>]
  * : It will export result to specified csv file
  * ## EXAMPLES
  *
  *     wp exploit-scanner result
  *
  * @synopsis
  */
 function result($args, $assoc_args)
 {
     $default = array('export-csv' => false);
     $assoc_args = wp_parse_args($assoc_args, $default);
     delete_transient('exploitscanner_results_trans');
     delete_transient('exploitscanner_files');
     $results = get_option('exploitscanner_results');
     if ($assoc_args['export-csv'] !== false) {
         $export_csv = array();
         $export_csv[] = array('Level', 'Location', 'Description', 'What was matched');
         $colorize_start = '';
         $colorize_end = '';
     } else {
         $colorize_start = '%R';
         $colorize_end = '%n';
     }
     foreach (array('severe', 'warning', 'note') as $l) {
         if (!empty($results[$l])) {
             //Level ' . ucwords($l) . ' (' . count($results[$l]) . ' matches)
             $lines = array();
             if (false === $assoc_args['export-csv']) {
                 WP_CLI::line('');
                 WP_CLI::line('');
                 WP_CLI::line('Level ' . ucwords($l) . ' (' . count($results[$l]) . ' matches)');
             }
             if (count($results[$l]) > 0) {
                 //$#$#
                 foreach ($results[$l] as &$r) {
                     $r['Level'] = ucwords($l);
                     $r['Location'] = '';
                     if (isset($r['loc'])) {
                         $r['Location'] .= $r['loc'];
                         if (isset($r['line_no'])) {
                             $r['Location'] .= ':' . $r['line_no'];
                         }
                     }
                     $r['Description'] = '';
                     if (isset($r['desc'])) {
                         $r['Description'] .= $r['desc'];
                     }
                     $r['What was matched'] = '';
                     if (isset($r['line'])) {
                         $text = $r['line'];
                         if (strlen($text) > 200) {
                             $start = strpos($text, '$#$#') - 50;
                             if ($start < 0) {
                                 $start = 0;
                             }
                             $end = strrpos($text, '#$#$') + 50;
                             $text = substr($text, $start, $end - $start + 1);
                         }
                         $start = strpos($text, '$#$#');
                         $temp_text = substr($text, $start + 4);
                         $end = strrpos($temp_text, '#$#$');
                         $highlight_text = substr($temp_text, 0, $end);
                         $replace_text = '$#$#' . $highlight_text . '#$#$';
                         if (false === $assoc_args['export-csv']) {
                             $r['What was matched'] = htmlspecialchars_decode(str_replace($replace_text, \cli\Colors::colorize($colorize_start . $highlight_text . $colorize_end), $text));
                         } else {
                             $r['What was matched'] = htmlspecialchars_decode(str_replace($replace_text, $colorize_start . $highlight_text . $colorize_end, $text));
                         }
                     }
                     if (false !== $assoc_args['export-csv']) {
                         $export_csv[] = array($r['Level'], $r['Location'], $r['Description'], $r['What was matched']);
                     }
                 }
                 if (false === $assoc_args['export-csv']) {
                     $fields = array('Location', 'Description', 'What was matched');
                     \WP_CLI\Utils\format_items('table', $results[$l], $fields);
                 }
             }
         }
     }
     if (false !== $assoc_args['export-csv']) {
         if (file_exists($assoc_args['export-csv'])) {
             WP_CLI::warning(sprintf("File already exists. The following file will be overwritten  %s", $assoc_args['export-csv']));
         }
         $fp = fopen($assoc_args['export-csv'], 'w+');
         foreach ($export_csv as $fields) {
             fputcsv($fp, $fields);
         }
         WP_CLI::success(sprintf("File created: %s", $assoc_args['export-csv']));
     }
 }