Home Forums OS X Server and Client Discussion Web running sips via Perl/PHP system function

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #371404
    jimlooney
    Participant

    Hello all,
    I am wondering if anyone knows what changed from Panther to Tiger that would cause failure when attempting to execute certain utilities via the Perl/PHP system() function?

    Specifically I am trying to run sips via a web-based application. Right now I am just trying to get it to take an uploaded image and generate a thumbnail for me.

    Now, on Panther server, it works great. No problem running it via my browser. However, on Tiger server, if I try to run via my browser, it fails. I CAN run it on command line successfully. So, I am trying to figure out what changed as far as interaction and permissions.

    Apache runs as “www” user, however I have permissions pretty well wide open while trying to get this thing to work.

    Any help, suggestions, criticism is warmly welcomed. 🙂

    #371405
    khiltd
    Participant

    Further information regarding the manner in which “it fails” would be necessary to make any attempt at a diagnosis. Your PHP configuration may simply be forbidding the use of the system function.

    Either way, using GD for whatever you’re trying to accomplish would probably be advisable, as nothing which relies on sips is going to be terribly portable.

    #371411
    jimlooney
    Participant

    Ya, thanks anywho, i guess I’ll just use GD. I was just perplexed as to why I can run other commands via system() but not sips. And also, why running sips via system() on Panther works, but not on Tiger. It annoys me Apple makes changes like that, which might take sysadmins by surprise.

    #371425
    khiltd
    Participant

    Again, nobody can really say why it isn’t working until we know what specifically isn’t working. Is it running and generating an error or is nothing happening at all? Are you supplying a full and accurate path to the binary? Is anything in your php.ini file preventing access to that binary? Is PHP being run as an Apache module or as a CGI application, etc etc etc

    #371426
    jimlooney
    Participant

    It returns a “6”, but I don’t see anywhere that lists what the heck the error codes mean, so it doesn’t help me much.

    Here’s what I’ve got going:

    Ok, so here is what I am trying to do. I have this old Perl script which I cannot get rid of because it is part of a large content management system. It has an image uploader which currently uploads just about anything. I want to take advantage of OSX’s “sips” command to resample the image and generate thumbnails. This is just a unix command, so I should be able to use one of the functions in the title above. However it doesn’t work.

    I tried calling directly in Perl, but it wasn’t working so then I switched to have perl call a php script so I could try and call sips or go another direction and use PHP’s image functions to do the resizing. So when I was calling directly from Perl, it looked like this ($path and $thumb_path are set based on the account running the script. I have verified that the values are correct):

    my $comm = “/usr/bin/sips –resampleWidth 60 $path –out $thumb_path”;
    system($comm) == 0 or die “$? $! / ” . ($? >> 8);

    In Perl, there are also system() and bactick operators. So, in the Perl code, right after the image is uploaded, I add a call to a php script :

    [code]
    `/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`;[/code]

    $path is the path to the image
    $thumb_path is where I want to write the thumbnail file to

    Now, the php script looks like this:

    [code][/code]

    All the extra stuff there is just me troubleshooting – I write to a text file, the command that I am passing to exec() so I can make sure it comes out right (which it does, everytime).

    So, the problem is that the exec() fails. I have checked permissions and they are wide open. I even modifed the command to be like this (to try and force the command to be executed as root user):

    [code]`sudo -u root -S /usr/bin/sips –resampleWidth 60 $path –out $thumb_path < /etc/root.secret`;[/code] Oh, and since I am printing out to that file (just so I can see the full command being sent to exec()), I can take the string from that file, put it in a php script and it works just fine. In other words, if you look at my code above, the command contains variables that need to be interpolated. However if I just run this script, it works fine: [code] [/code]

    So, apparently I am set to run exec w/o trouble. Note that I have also used the backtick operator and passthru on different occasions. Any other thoughts on ways to troubleshoot? For example, does anyone know the significance of the return error codes for exec()? When I run this it always returns a 6.

    #371427
    jimlooney
    Participant

    Here are the only differences between the php.ini files on the server where this all works, and on the server where it does not work:

    sips works:
    memory_limit = 8M

    sips does not work;
    memory_limit = 50M
    ———————————————
    sips works:
    extension_dir = “./extensions/”

    sips does not work:
    extension_dir = “./”
    ————————————————
    sips works:
    ;extension=php_gd2.dll

    sips does not work:
    extension=php_gd2.dll

    #371428
    jimlooney
    Participant

    lastly, I also changed over to use the PHP builtin image functions, but still have issues when perl calls the php script. Here are details.

    Ok, maybe you can still help me. If I call my php script directly from the browser, then using the PHP builtin functions works. However, if I call it from the Perl script, then the php script fails at:

    $src = imagecreatefromjpeg(“$path”);

    So, here is the code. This one works fine when called from the browser directly as so:

    http://domain.com/create_thumb.php?path=/Users/aw/www/images/uploads/1.jpg&path2=/Users/aw/www/images/uploads/thumbs/1.jpg

    [code]
    [/code]

    Now, it fails if from my perl script I call the php script like this:

    `/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`;

    [code][/code]

    And note – I also have the php script writing some stuff to a text file so I can troubleshoot. for instance I did implode(‘,’, $argv) and wrote that string to the text file and it shows that the 2 image paths are coming in correctly.

    #371430
    khiltd
    Participant

    And what PHP version is running on each? Different versions have different default settings which will be applied regardless of the fact that they are not explicitly defined in php.ini. In particular you’ll want to make sure safe_mode and open_base_dir are off. You should also be certain that /usr/bin/php is in sync with whatever Apache is using; they are frequently two completely different things.

    The return value of exec is also not particularly useful. What’s in the output?

    And you’ll also want to make sure you’ve read up on all the differences between the execution environments. mod_php runs differently than the command line version: http://www.php.net/manual/en/features.commandline.php

    Ultimately, if your application is written in Perl then I think you should probably focus on doing [i]this[/i] in Perl and cut out all this extraneous indirection.

    #371435
    jimlooney
    Participant

    I’ll check the different php configs. Although, yes, if I can get it to work in Perl then that would be great. I tried it like this, but it does not work ($? returns a 6):

    [code]my $comm = “/usr/bin/sips –resampleWidth 60 $path –out $thumb_path”;
    system($comm) == 0 or die “$? $! / ” . ($? >> 8);[/code]

    I also tried it with backticks:

    [code]
    `/usr/bin/sips –resampleWidth 60 $path –out $thumb_path`;
    [/code]

    Any Perl pros in the house?

    #371436
    khiltd
    Participant

    Again, the return value is largely irrelevant. What is being written to stdout and/or stderr?

    This works fine for me:

    [code]#! /usr/bin/php

    [/code]

    Output when invoked:

    [code]> ./sipstest.php /Users/nate/samples.jpg

    Running sips as uid=502(nate) gid=20(staff) groups=20(staff),98(_lpadmin),80(admin)

    [ (kCGColorSpaceDeviceRGB)] ( 0 0 0 1 )
    /Users/nate/samples.jpg
    /Users/nate/sipsthumb.jpg
    [/code]

    #371454
    jimlooney
    Participant

    Thank you so much for trying to help me with this.

    When I ran your script on command line, it works fine:

    [code][aw1:/users/php/acuweb_helpers] root# ./sipstest.php ./1.jpg

    Running sips as uid=0(root) gid=0(wheel) groups=0(wheel), 1(daemon), 2(kmem), 3(sys), 4(tty), 29(certusers), 8(procview), 5(operator), 9(procmod), 80(admin), 20(staff)

    /Users/php/acuweb_helpers/1.jpg
    /Users/php/acuweb_helpers/sipsthumb.jpg[/code]

    However, that is what I mentioned previously – the problem is when run through a browser. So I converted your script:

    [code]



    “;

    exec(“/usr/bin/sips –resampleWidth 60 $source –out $thumb”, $output, $retval);
    echo “Returns: “.$retval.”\n
    “;
    var_dump($output);
    foreach ( $output as $line )
    {
    echo “$line\n
    “;
    }

    ?>

    [/code]

    When I run that through the browser (like sipstest.php?image=/Users/php/acuweb_helpers/1.jpg), it outputs:

    [code]Running sips as uid=70(www) gid=70(www) groups=70(www), 80(admin)
    Returns: 6
    array(0) { }[/code]

    So the call to sips is failing and returning a 6. Can you test on your server? I am running 10.4.9, and php 4.4.4

    #371456
    khiltd
    Participant

    Although there’s a tremendous security hole there, it works perfectly for me on 5.2.4. I’d make sure the query string isn’t getting mangled by something else before finding its way to the command sent by exec.

    #371457
    jimlooney
    Participant

    hehe.ya, this would not be accessible to the public, and it’s just for proof of concept right now. 🙂

    So I output the sips command to screen now too:

    [code] echo “/usr/bin/sips –resampleWidth 60 $source –out $thumb
    “;
    exec(“/usr/bin/sips –resampleWidth 60 $source –out $thumb”, $output, $retval);
    [/code]

    and it looks fine:

    [code]Running sips as uid=70(www) gid=70(www) groups=70(www), 80(admin)
    /usr/bin/sips –resampleWidth 60 /Users/php/acuweb_helpers/1.jpg –out /Users/php/acuweb_helpers/sipsthumb.jpg
    Returns: 6
    array(0) { }[/code]

    Hehe, it’s driving me bonkers.

    #372645
    kilkonie
    Participant

    In reference to your sips utility not working via PHP. Are you using MAMP?
    First try running this script from PHP. You should add it to /usr/bin and change the permissions, etc.

    [code]#!/bin/sh

    /usr/bin/sips -s format png $1 –out $2.png 2> /tmp/error[/code]

    Pass in your input and output files accordingly. Look at the error log. It appears that I was suffering from a symbol collision with a MAMP-installed shared library. The PDFLib folks suffer from the same issue. Here’s their take on it:

    [code]
    PDFlib with XAMPP or MAMP on Mac OS X. If you add the PDFlib PHP extension to
    your php.ini on a Mac OS X Intel box with XAMPP Mac OS X 0.6.3 installed, the following
    error message appears:
    dyld: NSLinkModule() error
    dyld: Symbol not found: __cg_jpeg_resync_to_restart
    Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/
    Frameworks/ImageIO.framework/Versions/A/ImageIO
    Expected in: /Applications/xampp/xamppfiles/lib/libjpeg.62.dylib
    The PDFlib extension is linked against the ApplicationServices Framework, and XAMPP
    changes the DYLD_LIBRARY_PATH. This combination confuses the dynamic link editor.
    We detected that commenting out DYLD_LIBRARY_PATH in xamppfiles/bin/envvars cures
    this problem.
    A similar problem arises with MAMP. To cure the problem with MAMP comment out
    DYLD_LIBRARY_PATH in Library/bin/envvars.
    [/code]

    Of course, sips is also linked to the ApplicationServices Framework. So I pulled the DYLD_LIBRARY_PATH and restarted apache and it works. I’m not sure what the side effects are from doing this. (Like GD might have stopped working, for all I know.)

    And, yes, I do remember there were some changes to the jpeg library around 10.4. Apple shipped with an old version (and linked some things statically, other dynamically). I remember running across this issue a year or two ago when I was getting ImageMagick and GraphViz working from source.

    Hope this helps.

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.

Comments are closed