Page 1 of 1

CMSMS doesn't treat virtual() as expected

Posted: Fri Apr 04, 2008 10:28 pm
by chuck
Hello,
I should probably preface this by saying that I really like many of the things about CMSMS.
I'm hoping that I will be able to convert all of our web sites to CMSMS. But I'm really having
difficulties in a couple of areas.
I have one site that has many online tools. Many are written in perl. The simpler ones could
be rewritten in PHP. But as PHP has always handled them perfectly, I have no reason to think
that I need to. CMSMS on the other hand, is not dealing with them, as PHP does.
I'll use the simplest example; a text counter. With PHP, executing a perl script is simply a
matter of: . This has always worked
without fail in PHP. I can also use this same method with pages that contain perl scripts
with PHP: BUT, if I use this
same method with CMSMS ( {php} virtual('counter.pl'); {/php} ).
CMSMS complains about cannot modify headers, headers already sent.
Even though CMSMS complains, the counter still works. I have another very large, and
complex perl program that cannot be converted to PHP. So if CMSMS cannot deal
with perl correctly, using CMSMS will not be an option. :(
I should also note that I made a UDT tag to execute the counter. But CMSMS made the same
complaint. Has anyone successfully used perl scripts with CMSMS? Does anyone have
any possible solutions for the problem I'm having?

Thank you in advance, for your time and trouble.

Chuck

PHP 4-current
Apache-current
CMSMS-1.24

Re: CMSMS doesn't treat virtual() as expected

Posted: Fri Apr 04, 2008 10:36 pm
by calguy1000
it's probably a problem with your perl scripts outputting strings that shouldn't be output and are irrelevant to the task your trying to achieve.

try seing the results of the perl script without a php wrapper... and checking them in view source or in the web developers toolkit, etc.  there's probably something happening that you don't understand.

Re: CMSMS doesn't treat virtual() as expected

Posted: Fri Apr 04, 2008 11:46 pm
by chuck
Hello Calguy1000, and thank you for your fast response. :)

I'm not sure I understand your reply. Perhaps my explanation needs to be better. :)
OK here's the scoop; Again, I'll use the simple perl script counter as an example.
The counter has worked flawlessly for years. I use it like so in an .shtml file:

No errors are ever emited, counter works perfectly.
When I changed all of our pages to PHP, I used the following line to run the counter
in the PHP pages:

Again, everything worked as expected - no errors emited, counter worked perfectly.
Now, when I attempt to use the counter in CMSMS pages, I use the following
to run the counter:
{php}virtual('/cgi-bin/counter.pl');{/php}
But CMSMS vomits error messages to the PHP error log -

Code: Select all

Cannot modify header information - headers already sent by...
However, in spite of the error emited, the counter works.  ???
So. IMHO, it appears that CMSMS is not handling perl scripts in the same fashion
that PHP does. Is this correct?
For the sake of argument, here is the counter script:

Code: Select all

#!/usr/bin/perl -Tw
#
#####################################################
##############[ EDIT as REQUIRED ]###################

$number_of_digits = "8";
$end = ".gif";

$pathtocounter = "/usr/local/www/log/counters";
$pathtoimages = "/digits/aaa/";

$graphics = "yes";

##########[ NO EDITING REQUIRED BELOW THIS ]##########
######################################################

# HEAD Statement
# can also be print ("\n");
print ("Content-type: text/html\n\n");

# Find the counter file

open (COUNTER, "$pathtocounter");
$count = <COUNTER>;
chop ($count) if $count =~ /\n$/;
close (COUNTER);

# Add 1

$count += 1;

open (COUNTER, ">$pathtocounter");
print COUNTER ("$count");
close (COUNTER);

@digits = split(//, $count);

if ($number_of_digits eq "") {
        $howmany = @digits;
} else {
        $howmany = $number_of_digits;
}

# Validate statement

$spline = '%0' . $howmany . 'd';
$count = sprintf("$spline", $count);

@digitimages = split(//, $count);

# Display results

foreach $digitimage (@digitimages) {
	if ("$graphics eq yes") {
        $image = "<img src=$pathtoimages" . "$digitimage" . "$end>";
	print ("$image");
	} else {
        $plain = $digitimage;
 	print ("$plain");
	}
}

exit;
Hope this helps. Thank you again for your time and trouble.

Chuck


Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 12:23 am
by Nullig
Have you tried changing:

Code: Select all

# HEAD Statement
# can also be print ("\n");
print ("Content-type: text/html\n\n");
to:

Code: Select all

# HEAD Statement
# can also be print ("\n");
print ("\n");
Nullig

Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 12:37 am
by chuck
Hello Nullig, and thank you for your response. :)

Yes, of course. Perhaps you noticed that I mentioned that on the line right above the print statement:

Code: Select all

# can also be print ("\n");
print ("Content-type: text/html\n\n");
:)
In your defense, I didn't mention that I wrote this perl counter. :)

Anyway, to clarify; I tried both:
print ("Content-type: text/html\n\n");
and
print ("\n");
But CMSMS still emits the error to the PHP error log. :(
I should also probably mention that I made a UDT {simplecounter}:

Code: Select all

<?
//Simple perl counter script
virtual('/cgi-bin/counter.pl');
?>
Inserting the tag in the desired location of the CMSMS page also causes CMSMS to emit
the error message to the PHP error log. :(

Thanks again for your response.

Chuck


Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 1:25 am
by Nullig
Have you tried this workaround for your UDT:

Code: Select all

<?
//Simple perl counter script
echo " ";
virtual('/cgi-bin/counter.pl');
?>
Nullig

Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 4:47 am
by chuck
Nullig wrote: Have you tried this workaround for your UDT:

Code: Select all

<?
//Simple perl counter script
echo " ";
virtual('/cgi-bin/counter.pl');
?>
Nullig
LOL! Yes I did. But it didn't work either.
Sorry. I guess I should have mentioned that already.

Thanks for taking the time to respond Nullig.

Chuck

P.S. Love your icon. Looks great. :)


Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 12:47 pm
by Wiedmann
chuck wrote:With PHP, executing a perl script is simply a matter of: . This has always worked without fail in PHP. CMSMS complains about cannot modify headers, headers already sent.
virtual() does not work correctly with CMSMS, because Apache flush's the output buffer, output the content of the file, and then return to PHP (PHP can't controll the output buffer if you use virtual()). Every header() that CMSMS want set after virtual(), does not work.

You have two choices:

a) in your template use:

Code: Select all

{fetch file='http://example.com/cgi-bin/counter.pl'}
b) if you don't want use hardcoded servernames, or want use relative paths (like with virtual()), you can add this UDT
php_virtual

Code: Select all

if (!isset($params['uri'])) {
    return;
}

$config    =& $GLOBALS['gCms']->getConfig();
$cms_uri   =  parse_url($config['root_url']);
$param_uri =  parse_url($params['uri']);

$uri  = (isset($param_uri['scheme']) ? $param_uri['scheme'] : $cms_uri['scheme']).'://';
$uri .= isset($param_uri['user']) ? $param_uri['user'] : '';
$uri .= (isset($param_uri['user']) && isset($param_uri['pass'])) ? ':'.$param_uri['pass'] : '';
$uri .= isset($param_uri['user']) ? '@' : '';
$uri .= isset($param_uri['host']) ? $param_uri['host'] : $cms_uri['host'];
$uri .= (0 !== strpos($param_uri['path'], '/')) ? $cms_uri['path'].'/'.$param_uri['path'] : $param_uri['path'];
$uri .= isset($param_uri['query']) ? '?'.$param_uri['query'] : '';

$old_timeout = ini_set('default_socket_timeout', '10');
readfile($uri);
ini_set('default_socket_timeout', $old_timeout);
and in your template:

Code: Select all

{php_virtual uri='/cgi-bin/counter.pl'} 

Re: CMSMS doesn't treat virtual() as expected

Posted: Sat Apr 05, 2008 10:07 pm
by chuck
SWEET!!!
I really appreciate your taking the time to respond with this invaluable information Wiedmann - really.
I have a very large perl application that I built back in the late '90's that many orginations have come to depend on, on
a daily basis (nasa.gov, for one). I have spent the entire past three days trying to figure a way to make perl and CMSMS
"play nice" together. Only last night, I finally decided that until I was able to work this out, I'd need to resort to DHTML. I
wrote a layering system that wrote the output of my application to a DOM layer. A fair amount of JavaScript that I did
not want to depend on. But it worked, and didn't prevent me from offering my perl application until I could discover
the difference in perl handling between PHP, and CMSMS. I hadn't figured that Apache would be the culprit. As I
primarily run PHP as an Apache module (though not without exception). So I simply assumed CMSMS used/
hadnled PHP in much the same way I already did. How 'bout that! Really. You have no idea how grateful I am that
you were kind enough to take the time to share this information. Thank you Wiedmann.
Your contribution is very good news. :)

All the best to you.

Chuck


Re: CMSMS doesn't treat virtual() as expected

Posted: Sun Apr 06, 2008 12:49 am
by chuck
Hello,
Well, that eliminated the Header output error messages. The counter works as it always used to (correctly).
Now, all I need to do is figure out how to keep the output of my large perl application inside CMSMS.
{php_virtual} presents the page right where it belongs in CMSMS. But the output of the perl application
becomes the top page (is not presented in CMSMS). I'll have to take a closer look at how to
keep it in CMSMS.
Perhaps there is some information somewhere for CMSMS on this. Otherwise, I'll have to probably cobble
up some code to get the output to stay in CMSMS.

Thank you again for your input, Wiedmann.

Chuck