function parse_arguments(&$argv)
{
    // Default options
    $defaults = array('i' => 'localhost', 'p' => '8000', "mime-file" => "/etc/mime.types", "wp-root" => ".", "wp-version" => "latest", "show-errors" => 'E_ALL', "show-assets" => false, "show-hooks" => '', "show-everything" => false, "wordpresses" => $_SERVER['HOME'] . "/.cache/whippet/wordpresses", "cb-cache" => $_SERVER['HOME'] . "/.cache/whippet/callback-cache", "multisite" => false);
    // Are there some options in a config file? Check them in order.
    if (file_exists("/etc/whippetrc")) {
        $defaults = array_merge($defaults, parse_ini_file("/etc/whippetrc"));
    }
    if (!empty($_SERVER['HOME']) && file_exists($_SERVER['HOME'] . "/.whippetrc")) {
        $defaults = array_merge($defaults, parse_ini_file($_SERVER['HOME'] . "/.whippetrc"));
    }
    $optparser = new OptionParser();
    $optparser->addRule('h|help');
    $optparser->addRule('i::');
    $optparser->addRule('p::');
    $optparser->addRule('siteurl::');
    $optparser->addRule('q');
    $optparser->addRule('multisite');
    $optparser->addRule('mime-file::');
    $optparser->addRule('no-sql');
    $optparser->addRule('no-templates');
    $optparser->addRule('no-params');
    $optparser->addRule('no-scripts');
    $optparser->addRule('show-assets');
    $optparser->addRule('show-wp-errors');
    $optparser->addRule('show-wp-queries');
    $optparser->addRule('show-wp-hooks');
    $optparser->addRule('show-errors::');
    $optparser->addRule('show-everything');
    $optparser->addRule('wp-version::');
    $optparser->addRule('show-hooks::');
    $optparser->addRule('wordpresses::');
    try {
        $argv = $optparser->parse();
    } catch (Exception $e) {
        echo Colours::fg('red') . "Error: " . Colours::fg("white") . $e->getMessage() . "\n\n";
        usage();
        exit(0);
    }
    $arguments = $optparser->getAllOptions();
    if (!isset($arguments->siteurl)) {
        $i = isset($arguments['i']) ? $arguments['i'] : $defaults['i'];
        $p = isset($arguments['p']) ? $arguments['p'] : $defaults['p'];
        if ($p == 80) {
            $defaults['siteurl'] = "http://{$i}/";
        } else {
            $defaults['siteurl'] = "http://{$i}:{$p}/";
        }
    }
    return array_merge($defaults, $arguments);
}
Example #2
0
 public static function highlight_sql($sql)
 {
     $sql_keywords = array('ADD', 'ALL', 'ALTER', 'ANALYZE', 'AND', 'AS', 'ASC', 'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BIGINT', 'BINARY', 'BLOB', 'BOTH', 'BY', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR', 'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION', 'CONNECTION', 'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DEC', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV', 'DOUBLE', 'DROP', 'DUAL', 'DUPLICATE', 'EACH', 'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXIT', 'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FOR', 'FORCE', 'FOREIGN', 'FROM', 'FULLTEXT', 'GOTO', 'GRANT', 'GROUP', 'HAVING', 'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND', 'IF', 'IGNORE', 'IN', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INT1', 'INT2', 'INT3', 'INT4', 'INT8', 'INTEGER', 'INTERVAL', 'INTO', 'IS', 'ITERATE', 'JOIN', 'KEY', 'KEYS', 'KILL', 'LABEL', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT', 'LINES', 'LOAD', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCK', 'LONG', 'LONGBLOB', 'LONGTEXT', 'LOOP', 'LOW_PRIORITY', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MOD', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NULL', 'NUMERIC', 'ON', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', 'OR', 'ORDER', 'OUT', 'OUTER', 'OUTFILE', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'READ', 'READS', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REPLACE', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE', 'SCHEMA', 'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT', 'SENSITIVE', 'SEPARATOR', 'SET', 'SHOW', 'SMALLINT', 'SONAME', 'SPATIAL', 'SPECIFIC', 'SQL', 'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQLEXCEPTION', 'SQL_SMALL_RESULT', 'SQLSTATE', 'SQLWARNING', 'SSL', 'STARTING', 'STRAIGHT_JOIN', 'TABLE', 'TERMINATED', 'THEN', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TO', 'TRAILING', 'TRIGGER', 'TRUE', 'UNDO', 'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'UPDATE', 'UPGRADE', 'USAGE', 'USE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARCHARACTER', 'VARYING', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WRITE', 'XOR', 'YEAR_MONTH', 'ZEROFILL');
     $wp_tables = array('commentmeta', 'comments', 'links', 'options', 'postmeta', 'posts', 'terms', 'term_relationships', 'term_taxonomy', 'usermeta', 'users', 'blogs', 'blog_versions', 'registration_log', 'signups', 'site', 'sitecategories', 'sitemeta');
     //
     // Highlight keywords
     //
     foreach ($sql_keywords as $keyword) {
         $sql = preg_replace('/\\b' . $keyword . '\\b/', Colours::fg('bold_cyan') . $keyword . Colours::fg('white'), $sql);
     }
     //
     // Highlight WordPress table names
     //
     global $wpdb;
     foreach ($wp_tables as $table) {
         $sql = preg_replace('/\\b(' . $wpdb->prefix . '(\\d+_)?' . $table . ')\\b/', Colours::fg('purple') . '\\1' . Colours::fg('white'), $sql);
     }
     return $sql;
 }
Example #3
0
function signal_handler($signal)
{
    global $options;
    // Delete the settings file
    if (file_exists(sys_get_temp_dir() . "/.whippet-arguments")) {
        unlink(sys_get_temp_dir() . "/.whippet-arguments");
    }
    // Delete the output buffer
    if (file_exists(sys_get_temp_dir() . "/.whippet-output")) {
        unlink(sys_get_temp_dir() . "/.whippet-output");
    }
    // Delete the callback cache
    if (file_exists($options['cb-cache'])) {
        unlink($options['cb-cache']);
    }
    // Restore original wp-config
    if (WPS_LOCATION == 'root') {
        if (file_exists(dirname($options['wp-config']) . "/wp-config-original.whippet.bak")) {
            file_put_contents($options['wp-config'], file_get_contents(dirname($options['wp-config']) . "/wp-config-original.whippet.bak"));
            unlink(dirname($options['wp-config']) . "/wp-config-original.whippet.bak");
        } else {
            if (WPS_LOCATION == 'root') {
                Whippet::message(Colours::fg('red') . "Error: " . Colours::fg('white') . "Unable to find wp-config backup file; could not restore original configuration", "Your wp-config file should have been backed up at " . dirname($options['wp-config']) . "/wp-config-original.whippet.bak, but\n" . "it is missing or unreadable. You should edit your wp-config.php by hand to remove the\n" . "Whippet sections.\n");
            }
        }
    } else {
        if (WPS_LOCATION == 'wp-content') {
            unlink("{$options['wp-root']}/wp-config.php");
        }
    }
    echo "\nQuitting.\n";
    exit(0);
}
Example #4
0
<?php

// colours.php
require "Colours.class.php";
$colours = new Colours(["Whero", "Karaka", "Kowhai", "Kakariki", "Kikorangi", "Poropango", "Papura"]);
echo "<h3>while loop</h3>";
do {
    $colour = $colours->current();
    echo "{$colour} ";
} while ($colours->next()->valid());
echo "<hr><h3>foreach loop</h3>";
foreach ($colours as $colour) {
    echo "{$colour} ";
}
Example #5
0
 public function testResetCode()
 {
     $this->assertEquals("", Colours::getResetCode());
 }
Example #6
0
 /**
  * Output message
  *
  * @param string $message - message to output
  * @param mixed  $colour  - colour name
  * @param string $output  - where to send output, default is STDOUT
  * @static
  */
 public static function out($message, $colour = false, $output = 'STDOUT')
 {
     if (array_key_exists($output, self::$outputs)) {
         $output = self::$outputs[$output];
     }
     if ($colour) {
         file_put_contents($output, Colours::string($message, $colour) . self::$eol, FILE_APPEND);
     } else {
         file_put_contents($output, $message . self::$eol, FILE_APPEND);
     }
 }
Example #7
0
 /**
  * Emits the details of hooks and filters as they are called, if required
  */
 public function wps_filter_all($hook)
 {
     global $wp_filter;
     //
     // Check whether this hook should be displayed
     //
     $display = false;
     foreach ($this->options['show-hooks'] as $show) {
         if (preg_match("/^{$show}\$/", $hook)) {
             $display = true;
             break;
         }
     }
     if (!$display) {
         return;
     }
     //
     // If this hook has no callbacks, just bail
     //
     if (empty($wp_filter[$hook]) || !count($wp_filter[$hook])) {
         return;
     }
     //
     // Find the callbacks
     //
     $callback_messages = array();
     $all_wp_core = true;
     $hooks = $wp_filter[$hook];
     // TODO: this erroneously reorders hooks with the same priority
     ksort($hooks, SORT_NUMERIC);
     foreach ($hooks as $priority => $callbacks) {
         foreach ($callbacks as $callback) {
             if (is_array($callback['function'])) {
                 $function = $callback['function'][1];
             } else {
                 $function = $callback['function'];
             }
             // Skip Whippet callbacks
             if (preg_match('/^wps_/', $function)) {
                 continue;
             }
             $callback_message = "\t{$priority}: " . Colours::fg('cyan') . $function . Colours::off();
             $callback_data = $this->cb_cache->lookup($function);
             if (!$callback_data) {
                 // Find the function
                 $file = exec("grep -rn 'function {$function}' {$this->options['wp-root']}/*");
                 if (empty($file) && isset($this->options['wp-content'])) {
                     $file = exec("grep -rn 'function {$function}' {$this->options['wp-content']}/*");
                 }
                 // If we got it, add an entry to the cache
                 if (!empty($file) && preg_match('/^([^:]+):(\\d+):/', $file, $matches)) {
                     $this->cb_cache->add($function, $matches[1], $matches[2]);
                     $callback_data = $this->cb_cache->lookup($function);
                 }
             }
             if ($callback_data) {
                 // Is this a callback outside the WP core?
                 if (preg_match('/wp-content/', $callback_data['file'])) {
                     $all_wp_core = false;
                 }
                 // Make paths relative
                 $callback_data['file'] = str_replace($this->options['wp-root'], '', $callback_data['file']);
                 if (!empty($this->options['wp-content'])) {
                     $callback_data['file'] = str_replace($this->options['wp-content'], '', $callback_data['file']);
                 }
                 $callback_message .= " in " . Colours::fg("brown") . str_replace($this->options['wp-root'] . "/wp-content/", '', $callback_data['file']) . Colours::off() . " at line {$callback_data['line']}";
             } else {
                 $callback_message .= " (couldn't find this function's definition)";
             }
             $callback_messages[] = $callback_message;
         }
     }
     //
     // If we're not showing WP core hooks, and all these callbacks are from the core, bail
     // If there are no callbacks (probably because a whippet callback was skipped), bail
     //
     if (!isset($this->options['show-wp-hooks']) && $all_wp_core) {
         return;
     }
     if (!count($callback_messages)) {
         return;
     }
     //
     // Find the caller
     //
     $type = '';
     $caller = '';
     $backtrace = debug_backtrace();
     foreach ($backtrace as $i => $value) {
         if ($value['function'] == 'apply_filters' || $value['function'] == 'do_action' || $value['function'] == 'apply_filters_ref_array' || $value['function'] == 'do_action_ref_array') {
             $caller = $backtrace[$i + 1];
             if ($value['function'] == 'apply_filters') {
                 $type = "Filter";
             } else {
                 $type = "Action";
             }
             break;
         }
     }
     //
     // Put together the message
     //
     $message = Colours::fg('bold_cyan') . "Hook triggered: " . Colours::off() . "{$type} " . Colours::fg('cyan') . "{$hook}" . Colours::off() . " called from function " . Colours::fg('cyan') . "{$caller['function']}";
     if (!empty($caller['file'])) {
         $message .= Colours::off() . " in " . Colours::fg('brown') . str_replace($this->options['wp-root'], '', $caller['file']);
     }
     if (!empty($caller['line'])) {
         $message .= Colours::off() . " at line {$caller['line']}";
     }
     $this->message("{$message}" . Colours::off());
     foreach ($callback_messages as $callback_message) {
         $this->message($callback_message);
     }
 }