Skip to content

Instantly share code, notes, and snippets.

View nikic's full-sized avatar

Nikita Popov nikic

View GitHub Profile
@nikic
nikic / RouterGenerator.php
Created January 17, 2011 18:57
Generates Router classes
<?php
class RouteCompilationException extends LogicException
{
public function setContext($line, $route) {
$this->message = 'Line ' . $line . ': ' . $this->getMessage() . ' (route: "' . $route . '")';
}
}
class RouterCompiler
{
@nikic
nikic / benchmark.php
Created February 6, 2011 10:57
Compares multidimensional, flat and string access
<?php
echo '<pre>';
$w = 1000;
$h = 1000;
// generate multidimensional array
$multi = array();
for ($i = 0; $i < $w; ++$i) {
@nikic
nikic / A_WARNING.md
Last active September 25, 2015 06:28
PHP Sandbox

I do not recommend to use this code as it has known vulnerabilities. You may want to have a look at some more recent sandboxing project like https://github.com/fieryprophet/php-sandbox (though I can't say anything about its quality). In general though the only really safe way to sandbox PHP is to run it in a jail.

@nikic
nikic / hq9.php
Created June 13, 2011 12:49
HQ9 interpreter
<?php
define('SAFE_MODE', 42);
echo '<pre>';
function throw_exception($error) {
die('--- EXCEPTION: ' . $error);
}
if (!isset($_GET['source'])) {
@nikic
nikic / gist:1188670
Created September 2, 2011 14:06 — forked from ircmaxell/gist:1188649
PHP example on variable variables - without needing them
<?php
function bar($a, $b, $c) {
foreach (compact('a', 'b', 'c') as $key => $value) {
$form[$key] = array(
'#type' => 'hidden',
'#value' => $value,
);
}
}
@nikic
nikic / evil.php
Created September 11, 2011 11:59
Evil code
<?php
class PHPTokenizer_Tokenizer_PHP53To536 extends PHPTokenizer_Tokenizer_Base
{
public function tokenize($code) {
@$resetErrorMsg;
$tokens = @token_get_all($code);
$error = error_get_last();
try {
Index: ext/standard/tests/strings/md5_file.phpt
===================================================================
--- ext/standard/tests/strings/md5_file.phpt (revision 316568)
+++ ext/standard/tests/strings/md5_file.phpt (working copy)
@@ -56,7 +56,7 @@
/* Raw Binary Output for Empty file as input */
echo "\n*** Raw Binary Output for Empty file as Argument ***\n";
-var_dump( md5_file("EmptyFile.txt", true) );
+var_dump( bin2hex(md5_file("EmptyFile.txt", true)) );
@nikic
nikic / diff.patch
Created December 9, 2011 14:44
Additional token_get_all param
diff --git a/ext/tokenizer/tests/token_get_all_error.phpt b/ext/tokenizer/tests/token_get_all_error.phpt
index 29e97c3..5b455e9 100644
--- a/ext/tokenizer/tests/token_get_all_error.phpt
+++ b/ext/tokenizer/tests/token_get_all_error.phpt
@@ -19,7 +19,7 @@ var_dump( token_get_all());
echo "-- Testing token_get_all() function with more than expected no. of arguments --\n";
$source = '<?php ?>';
$extra_arg = 10;
-var_dump( token_get_all($source, $extra_arg));
+var_dump( token_get_all($source, false, $extra_arg));
[Wed Dec 21 17:06:58 2011] Script: 'test2.php'
/home/nikic/dev/php-src-git/Zend/zend_API.c(311) : Freeing 0xB78B836C (20 bytes), script=test2.php
Last leak repeated 1 time
[Wed Dec 21 17:06:58 2011] Script: 'test2.php'
/home/nikic/dev/php-src-git/Zend/zend_execute_API.c(876) : Freeing 0xB78B8490 (20 bytes), script=test2.php
=== Total 3 memory leaks detected ===
@nikic
nikic / php.pecl.log
Created December 22, 2011 15:32
PECL discussion on SPL exceptions
(3:22:33 PM) johannes: hey ho helly\G
(3:22:33 PM) helly\G: Moin moin
(3:24:21 PM) chregu_ [~chregu@77-58-253-248.dclient.hispeed.ch] entered the room.
(3:25:56 PM) jpauli1: helly\G: helly ! :) morning ^^
(3:26:26 PM) helly\G: Jo
(3:28:41 PM) jpauli1: helly\G: sorry that the guy (forgot his name) who wants to review ext/SPL code seems not here ATM
(3:29:19 PM) nikic: jpauli1: Levi?
(3:29:30 PM) jpauli1: nikic: +1 :)
(3:29:39 PM) helly\G: Spl use or extend?
(3:29:40 PM) jpauli1: nikic: I'm not good at names remembering :p