0

I'm currently using the Pygments for PHP plugin that is located here: http://derek.simkowiak.net/pygments-for-php/.

The line that actually calls Pygments from that code is an exec() passed:

pygmentize -f html $extra_opts -l $language $temp_name
as the command. This all works fine, and I get back the output and it is formatted by the plugin.

What I would like to happen at the same time is for Pygments to create an image of it, so I pass exec() a similar command:

pygmentize -f png $extra_opts -l $language -o $full_image_path/$output_file.png $temp_name
This is where I run into a problem. The image never shows up in the expected folder.

However, if I var_dump() that command string before I exec() it and take it and run it straight from the command line, it works fine.

I have tried echoing exec('whoami') which tells me that the PHP user is www-data. I've tried giving permissions to www-data and changing ownership to www-data on the folder where I store the images. I've also tried changing permissions to 777 just to see what would happen, and the answer is nothing.

Is there something I'm missing? I'm running out of ideas to try. Thank you!

Edit: Another thing that I've checked is the output from the exec command, and the return value. It outputs an empty array, and it returns 1 as the return value.

Edit 2: After seeing that that directory should be writeable/readable for the PHP user, is it possible that pygments doesn't have permission to write it as a specific user? I'm not sure this makes sense, as when I run it myself it works fine, and in fact, when PHP runs it with the HTML lexer, it is able to run. I'm not very experienced in Python, so I don't know if this is a potential issue.

Scott Douglass
  • 170
  • 2
  • 10
  • try using `print()` to look at the constructed command string before calling `exec()`. That ought to show you what the problem is. – SDC Mar 06 '13 at 15:48
  • Would that do the same general thing that `var_dump()` would do? I `var_dump($command)` where `$command` is the constructed command string prior to calling `exec()` and I get: `pygmentize -f png -O full,style=manni,cssclass=pygmentize_kbOKBd -l php -o /srv/www/path/to/images/uploads/2513732976ad4b7.02729290.png /tmp/pygmentize_kbOKBd`. I can then take that and run it myself in the command line and it works fine, which is why it's weird to me, and I thought it was a permissions problem, but I can't seem to fix it even with 777 on the folder I am putting it in. – Scott Douglass Mar 06 '13 at 15:55
  • hmm, yes, `var_dump` achieves the same thing here as `print`; the idea being to confirm that we're calling what we think we're calling. So since that's fine, I guess it rules out that idea. – SDC Mar 06 '13 at 16:00
  • To double-check how the permissions look inside PHP, use PHP to check the path using `file_exists()`, `is_readable()` and `is_writable()` etc on the path you want to write to. – SDC Mar 06 '13 at 16:02
  • if `file_exists()` gives false, then maybe your PHP is in `chroot` environment; ie it may be isolated from the rest of the system and unable to access paths in the same way as a normal user. – SDC Mar 06 '13 at 16:04
  • Thanks for the suggestions. I just got a chance to test all of them, however I get true back for all 3 of `is_readable()`, `is_writeable()`, and `file_exists()`. – Scott Douglass Mar 06 '13 at 23:03

2 Answers2

0

I guess you cannot do it like this.

$output_file.png

Try

$file = $output_file.".png"

and substitute in the exec

GBRocks
  • 698
  • 8
  • 30
  • After changing that, the issue is still there. I think the overall string is the same that was produced before and after. What is produced by the exec command that is passed to the command line is this: `pygmentize -f png -O full,style=manni,cssclass=pygmentize_kbOKBd -l php -o /srv/www/path/to/images/uploads/2513732976ad4b7.02729290.png /tmp/pygmentize_kbOKBd` – Scott Douglass Mar 06 '13 at 12:29
0

Ended up being an issue with the font that was installed for use by the www-root user. Apparently the one that is used by default for Pygments was installed only for the user that I was running as when I use the command line.

The way I was able to figure this out, was running exec("$command 2>&1", $out, $code);.

The extra 2>&1 redirects stderr into the output for me to see the issue.

The $out parameter showed the FontNotFound error that pygments was throwing.

I changed the font that Pygments used via the command line using: full,style=manni,cssclass=pygmentize_kbOKBd,font_name='DejaVu Sans Mono' -l php -o /srv/www/path/to/images/uploads/2513732976ad4b7.02729290.png /tmp/pygmentize_kbOKBd after finding which fonts I had available to me.

To see which fonts I had available to me as the user running the script, I just ran fc-list in an exec() command for Ubuntu, and checked the output of that for the list of available fonts.

Scott Douglass
  • 170
  • 2
  • 10