Removed bullshit

master
Ondřej Hruška 9 years ago
parent 7131e2accd
commit 3809682801
  1. 7
      LICENSE
  2. 2
      Makefile
  3. 0
      README.md
  4. 24
      composer.json
  5. 88
      index.php
  6. 65
      lib/MelodyPlugin.php

@ -1,4 +1,5 @@
Copyright (c) 2010, Jordi Boggiano <j.boggiano@seld.be> Copyright (c) 2010, Jordi Boggiano <j.boggiano@seld.be>
Copyright (c) 2015, Ondřej Hruška <ondra@ondrovo.com>
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -8,9 +9,9 @@ modification, are permitted provided that the following conditions are met:
* Redistributions in binary form must reproduce the above copyright * Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
* Neither the name of Slippy nor the * Neither the name of this project nor the names of its contributors may
names of its contributors may be used to endorse or promote products be used to endorse or promote products derived from this software
derived from this software without specific prior written permission. without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

@ -1,4 +1,4 @@
web: .PHONY web: .PHONY
php -S localhost:8088 -t . index.php php -S localhost:1337
.PHONY: .PHONY:

@ -1,24 +0,0 @@
{
"name": "seld/php-console",
"description": "A console to quickly try and run PHP code in your browser",
"keywords": ["console", "php"],
"homepage": "http://github.com/Seldaek/php-console",
"type": "project",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"require": {
"php": ">=5.2.0",
"sensiolabs/melody": "@dev"
},
"extra": {
"branch-alias": {
"dev-master": "1.5.x-dev"
}
}
}

@ -1,10 +1,15 @@
<?php <?php
ini_set('log_errors', 0);
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
// --- Handle config ---
$defaults = array( $defaults = array(
// how many spaces to use for indention, 0 will make it use real tabs // how many spaces to use for indention, 0 will make it use real tabs
'tabsize' => 4, 'tabsize' => 4,
// whitelist of IPs which don't need to be authenticated // whitelist of IPs
// use '*' to allow any IP // use '*' to allow any IP
'ip_whitelist' => array('127.0.0.1', '::1'), 'ip_whitelist' => array('127.0.0.1', '::1'),
@ -21,93 +26,54 @@ if (file_exists(__DIR__.'/config.php')) {
$options = $defaults; $options = $defaults;
} }
/**
* PHP Console // --- IP Whitelist ---
*
* A web-based php debug console
*
* Copyright (C) 2010, Jordi Boggiano
* http://seld.be/ - j.boggiano@seld.be
*
* Licensed under the new BSD License
* See the LICENSE file for details
*
* Source on Github http://github.com/Seldaek/php-console
*/
if (!in_array('*', $options['ip_whitelist'], true) && if (!in_array('*', $options['ip_whitelist'], true) &&
!in_array($_SERVER['REMOTE_ADDR'], $options['ip_whitelist'], true) !in_array($_SERVER['REMOTE_ADDR'], $options['ip_whitelist'], true)
) { ) {
header('HTTP/1.1 401 Access unauthorized'); header("HTTP/1.0 401 Unauthorized");
die('ERR/401 Go Away'); die('Access Denied');
} }
define('PHP_CONSOLE_VERSION', '1.4.0');
require 'lib/MelodyPlugin.php';
require 'vendor/autoload.php';
ini_set('log_errors', 0);
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
$debugOutput = ''; $debugOutput = '';
function runCode($__source_code, $__bootstrap_file)
{
if ($__bootstrap_file) {
require $__bootstrap_file;
}
eval($__source_code);
}
if (isset($_POST['code'])) { if (isset($_POST['code'])) {
ini_set('html_errors', false); ini_set('html_errors', false);
if (get_magic_quotes_gpc()) {
$code = stripslashes($code);
}
$code = $_POST['code']; $code = $_POST['code'];
// // if there's only one line wrap it into a krumo() call // --- Evaluate the code ---
// if (preg_match('#^(?!var_dump|echo|print|< )([^\r\n]+?);?\s*$#is', $code, $m) && trim($m[1])) {
// $code = 'krumo('.$m[1].');';
// }
// replace '< foo' by krumo(foo)
// $code = preg_replace('#^<\s+(.+?);?[\r\n]?$#m', 'krumo($1);', $code);
// replace newlines in the entire code block by the new specified one
// i.e. put #\r\n on the first line to emulate a file with windows line
// endings if you're on a unix box
if (preg_match('{#((?:\\\\[rn]){1,2})}', $code, $m)) {
$newLineBreak = str_replace(array('\\n', '\\r'), array("\n", "\r"), $m[1]);
$code = preg_replace('#(\r?\n|\r\n?)#', $newLineBreak, $code);
}
ob_start(); ob_start();
$memBefore = memory_get_usage(true); $memBefore = memory_get_usage(true);
$start = microtime(true); $start = microtime(true);
$melodyPlugin = new MelodyPlugin(); // Remove the < ?php mark
if ($melodyPlugin->isMelodyScript($code)) { // TODO remove also ? > if present
if ($melodyPlugin->isScriptingSupported()) {
$melodyPlugin->runScript($code, $options['bootstrap']);
} else {
throw new Exception('php-console misses required dependencies to run melody scripts.');
}
} else {
// Important: replace only line by line, so the generated source lines will map 1:1 to the initial user input!
$code = preg_replace('{^\s*<\?(php)?\s*}i', '', $code); $code = preg_replace('{^\s*<\?(php)?\s*}i', '', $code);
runCode($code, $options['bootstrap']); /** Run code with bootstrap in separate scope */
function runCode($__source_code, $__bootstrap_file) {
if ($__bootstrap_file) {
require $__bootstrap_file;
} }
eval($__source_code);
}
runCode($code, $options['bootstrap']);
// compare with peak, because regular memory could be free'd already
$end = microtime(true); $end = microtime(true);
$memAfter = memory_get_peak_usage(true); $memAfter = memory_get_peak_usage(true);
$debugOutput .= ob_get_clean(); $debugOutput .= ob_get_clean();
// ---------------------------
if (isset($_GET['js'])) { if (isset($_GET['js'])) {
// --- Send response with metadata in headers ---
header('Content-Type: text/plain'); header('Content-Type: text/plain');
$memory = sprintf('%.3f', ($memAfter - $memBefore) / 1024.0 / 1024.0); // in MB $memory = sprintf('%.3f', ($memAfter - $memBefore) / 1024.0 / 1024.0); // in MB
@ -117,6 +83,7 @@ if (isset($_POST['code'])) {
header('X-Rendertime: '. $rendertime); header('X-Rendertime: '. $rendertime);
echo $debugOutput; echo $debugOutput;
die('#end-php-console-output#'); die('#end-php-console-output#');
} }
} }
@ -154,7 +121,6 @@ if (isset($_POST['code'])) {
</div> </div>
<div class="statusbar"> <div class="statusbar">
<span class="position">Line: 1, Column: 1</span> <span class="position">Line: 1, Column: 1</span>
<!-- <a href="" class="reset">Reset</a> -->
<span class="runtime-info"></span> <span class="runtime-info"></span>
<input type="submit" name="subm" value="Run!"/> <input type="submit" name="subm" value="Run!"/>
</div> </div>

@ -1,65 +0,0 @@
<?php
use SensioLabs\Melody\Melody;
use SensioLabs\Melody\Configuration\RunConfiguration;
use Symfony\Component\Process\Process;
use SensioLabs\Melody\Resource\ResourceParser;
use Symfony\Component\Process\ExecutableFinder;
/**
* Class which integrates melody scripts into the php-console.
*
* @author mstaab
* @see https://github.com/sensiolabs/melody
*/
class MelodyPlugin {
public function isMelodyScript($source) {
return preg_match(ResourceParser::MELODY_PATTERN, $source);
}
public function isScriptingSupported() {
$executableFinder = new ExecutableFinder();
foreach (['composer', 'composer.phar'] as $candidateName) {
if ($composerPath = $executableFinder->find($candidateName, null, array(getcwd()))) {
return true;
}
}
return false;
}
public function runScript($__source_code, $__bootstrap_file)
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'melody-composer';
// make sure the melody subprocess has a composer home,
// which is not the case when running from webcontext
$_ENV['COMPOSER_HOME'] = $tmpDir;
$melody = new Melody();
$configuration = new RunConfiguration(/*true, true*/);
$executor = function (Process $process, $verbose)
{
$callback = function ($type, $text)
{
// we only have one output channel to the browser, just echo "all the things"
echo $text;
};
$process->run($callback);
};
//TODO missing $__bootstrap_file support
/*
if ($__bootstrap_file) {
require $__bootstrap_file;
}
*/
$tmpFile = tempnam($tmpDir, '_script');
register_shutdown_function(function() use ($tmpFile) {
@unlink($tmpFile);
});
file_put_contents($tmpFile, $__source_code);
$melody->run($tmpFile, array(), $configuration, $executor);
}
}
Loading…
Cancel
Save