18

I'm trying to create a very simple PHP CLI application that can be run as a phar file from the command line:

# php myProject.phar

This is what I've tried so far:

My Project

My project is in a directory called MyProject and it has these two files in it:

 |-- createPhar.php
 `-- bootstrap.php

bootstrap.php

The bootstrap.php file contains this:

<?php
print phpversion() . PHP_EOL;
print 'i am some script' . PHP_EOL;

When I run this script from my Ubuntu command line:

# cd MyProject 
# php bootstrap.php

I get the following output:

5.3.2-1ubuntu4.9
i am some script

createPhar.php

The createPhar.php file is meant to turn the project into Phar archive. It looks like this:

<?php    
$phar = new Phar('MyProject.phar');
$phar->addFile('bootstrap.php');
$phar->setStub( $phar->createDefaultStub('bootstrap.php') );

When I run that script...

# php createPhar.php

... a new file called MyProject.phar is created in my project's directory.

|-- bootstrap.php
|-- createPhar.php
`-- MyProject.phar

Now here's the problem

When I run the phar file...

# php MyProject.phar 

...I expect to see the same the same output that I got when when I ran the bootstrap.php script.

Instead I see nothing. No output at all. This implies that my bootstrap.php script is not being included by the default stub that was created by $phar->createDefaultStub('bootstrap.php')

I think I am misunderstanding how Phars and their stubs are being created. Could you, please, explain where I have gone wrong.

JW.
  • 4,821
  • 5
  • 43
  • 60
  • Your example does work for me. But try the `->setDefaultStub` shortcut (over ::createDefault and ::setStub) for testing. – mario Jun 13 '11 at 21:01
  • Thanks mario. I tried using `->setDefaultStub` and it still did not work either. Its weird that you got it to work and I haven't. I suppose the problem is not to do with the code then. – JW. Jun 13 '11 at 21:15
  • My `PHP_VERSION` is `5.3.3-1ubuntu9.5`, but there was only one minor security bug fix related to the phar extension in between. It must be something else. Did you look at the resulting file with a hexeditor? Is the bootstrap content anywhere in it? (I got a plain php script with some admixed binary gibberish.) – mario Jun 13 '11 at 21:18
  • Yep. Had a look at `MyProject.phar` and there is bootstrap code plus some gibberish in it. Thanks for your suggestions. I suspect I am doing something really stupid somewhere along the line - so gonna sleep on it and re-try tomorrow. – JW. Jun 13 '11 at 21:35

2 Answers2

11

To answer my own question.

The method outlined in my question, above, is one correct way to create a phar / phar stub.

The reason why it did not work for me and did work for Mario (see his comment below the question), is because I had Suhosin installed and needed to tweak the settings.

Fixed using the technique outlined here:

To fix, put:

suhosin.executor.include.whitelist="phar"

in /etc/php5/cli/php.ini

JW.
  • 4,821
  • 5
  • 43
  • 60
0

you could also do it like this:

bootstrap.php;

<?php
function go() {
    print phpversion() . PHP_EOL;
    print 'i am some script' . PHP_EOL;
}

then:

php -r "require 'phar://Myproject.phar'; go();"

or don't have a function and it will execute whatever commands you have in there, but typically you would have some functions or class files in the phar.

ldg
  • 9,112
  • 2
  • 29
  • 44
  • Thanks for the suggestion. I tried with a function and without. Ran ` php -r "require 'phar://Myproject.phar';"`. and got no output. – JW. Jun 13 '11 at 21:28
  • Whats really weird is that, if i delete the phar file alltogether, and then run ` php -r "ini_set('display_errors', 1); error_reporting(E_ALL); require 'phar://Myproject.phar';"` I still get no output. – JW. Jun 13 '11 at 21:29
  • Is this Linux and is SELinux enabled? Also any system errors? – ldg Jun 13 '11 at 21:37
  • Ahaaa! Yes. The system log shows up some stuff. I piped the output of the `syslog` file into `grep` to pick up on any mentions of php: `cat /var/log/syslog | grep php`. Looks like suhosin is meddling: `Jun 13 21:43:41 ubuntu suhosin[3264]: ALERT - Include filename ('phar:///MyProject/MyProject.phar/bootstrap.php') is an URL that is not allowed (attacker 'REMOTE_ADDR not set', file '/MyProject/MyProject.phar', line 9)` – JW. Jun 13 '11 at 21:48
  • Yep. That was the problem. Fixed using [the technique outlined here](http://william.shallum.net/random-notes/suhosinpharurlnotallowed). Thanks for your help. – JW. Jun 13 '11 at 23:28