bobbingwide / oik-batch

Batch interface to WordPress
https://www.oik-plugins.com/oik-plugins/oik-batch
GNU General Public License v2.0
0 stars 0 forks source link

Improve detection of PHP CLI #19

Open bobbingwide opened 7 years ago

bobbingwide commented 7 years ago

When trying to run some new code under oik-batch to set letter taxonomy terms I discovered that the value of the PHP constant PHP_SAPI was not constant!

Whereas php_sapi_name() was correctly returning "cli", PHP_SAPI was changing from "cli" to "cgi-fcgi".

My code, which tested the value of PHP_SAPI was not being invoked.

Is this truly unexpected behaviour?

Current situation

Using a workaround but not a happy bunny.

Could be a hosting problem?

bobbingwide commented 7 years ago

This is my code that checks if it's running under CLI.

function is_cli() { 
    echo PHP_EOL;
    echo "php_sapi_name: ";
    echo php_sapi_name();
    echo PHP_EOL;
    echo "PHP_SAPI: ";
    echo PHP_SAPI;
    echo PHP_EOL;
    echo "constant(): ";
    echo constant( 'PHP_SAPI' );
    echo PHP_EOL;

    $is_cli = false;
    switch ( PHP_SAPI ) {
        case 'cli':
            $is_cli = true;
          break;

        case 'cgi-fcgi':
            $is_cli = true;
            bw_trace2( PHP_SAPI, "PHP_SAPI unexpected?", false );
            break;

        default:
    }
    return( $is_cli );
}

This is the output I see

php_sapi_name: cli
PHP_SAPI: cgi-fcgi
constant(): cgi-fcgi

and this is the output from tracing.

/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4373) 4847 2017-03-11T14:11:20+00:00 7.862819 0.000207 cf=run_oik-shortcodes.php,save_post,query 52 0 20971520/20971520 2048M F=312 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4374) 4848 2017-03-11T14:11:20+00:00 7.864455 0.001636 cf=run_oik-shortcodes.php,wp_insert_post 53 0 20971520/20971520 2048M F=312 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-a2z/oik-a2z.php(186:0) is_cli(1) 4849 2017-03-11T14:11:20+00:00 7.864606 0.000151 cf=run_oik-shortcodes.php,wp_insert_post 53 0 20971520/20971520 2048M F=312 PHP_SAPI unexpected? cgi-fcgi
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4375) 4850 2017-03-11T14:11:20+00:00 7.864887 0.000281 cf=run_oik-shortcodes.php,wp_insert_post,query_post_type_letter_taxonomy_filters 53 0 20971520/20971520 2048M F=313 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4376) 4851 2017-03-11T14:11:20+00:00 7.865438 0.000551 cf=run_oik-shortcodes.php,wp_insert_post,oik_a2z_query_terms_post_letters 53 0 20971520/20971520 2048M F=313 anychange current value for: PHP_SAPI cli
bobbingwide commented 7 years ago

I'm not going to give up with this just yet.

I can't clone the code to different folders and reproduce the problem in those folders. But I have managed to reproduce it in a different environment from where I first noticed it. Which is a start.

The problem can be simplified to.

  1. Invoking routine ( called /oik-a2z/issue-19.php )
    <?php // (C) Copyright Bobbing Wide 2017
    echo "PHP_SAPI: " .  PHP_SAPI. PHP_EOL;   
    if ( !function_exists( "add_action" ) ) { function add_action() {} }
    require( "../oik-a2z/oik-a2z.php" );
    $cli = oik_a2z_get_php_sapi();
    echo "cli: " . $cli . PHP_EOL;
  2. New function in /oik-a2z/oik-a2z.php
    function oik_a2z_get_php_sapi() {
    return PHP_SAPI;
    }
  3. Execute php issue-19.php
    bworguk@c17064 [~/public_html/wp-content/plugins/oik-a2z]# php issue-19.php
    PHP_SAPI: cli
    cli: cgi-fcgi

The expected output is:

PHP_SAPI: cli
cli: cli
bobbingwide commented 7 years ago

This is the simplest demonstration of the problem.

Setup

  1. PHP Environment with OPcache enabled, Linux server.
  2. Two files in a folder called path/issue-19.

browsed.php

<?php
function get_php_sapi() {
    return PHP_SAPI;
}

issue-19.php

<?php
echo "PHP_SAPI: " .  PHP_SAPI. PHP_EOL;   
require( "browsed.php" );
$cli = get_php_sapi();
echo "cli: " . $cli . PHP_EOL;

Steps

Step Where Action
1 Browser Visit http://example.com/path/issue-19/issue-19.php
2 CLI cd path/issue-19
3 CLI touch issue-19.php
4 CLI php issue-19.php

For step 1. the result is expected to: PHP_SAPI: cgi-fcgi cli: cgi-fcgi For step 4. the result changes to: PHP_SAPI: cli cli: cgi-fcgi

To get the other combinations simply touch a file and try again. You should be able to get all 4 combinations in both the browser and on the Command Line. The value of PHP_SAPI depends on which routine loads the changed file into the OPcache.

So is it a constant or isn't it?

bobbingwide commented 7 years ago

Note: I can't reproduce the problem in my Windows server running Apache. In the browser the output is: PHP_SAPI: apache2handler cli: apache2handler From CLI it's: PHP_SAPI: cli cli: cli

bobbingwide commented 7 years ago

This problem could also explain the "That's odd" message from oikb_check_time_limit() which was being displayed every now and then for no obvious reason.

mattheu commented 7 years ago

Tested this on a chassis vagrant box (nginx, php7.1-fpm) and worked fine.

In the browser: PHP_SAPI: fpm-fcgi cli: fpm-fcgi In the command line; PHP_SAPI: cli cli: cli

bobbingwide commented 7 years ago

Thanks Matt.Alain Schlesser got the same result as you. SiteGround are going to try as well. I'm going to check which php executable gets called. Your result may be different because of Xdebug.

bobbingwide commented 7 years ago

In my .bashrc file php is set as an alias to php-cli

alias php='/usr/local/php70/bin/php-cli'

I get the same problem with both PHP 7.0.x and PHP 7.1.x

php -v gives:

PHP 7.0.16 (cli) (built: Feb 20 2017 11:16:52) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v6.0.8, Copyright (c) 2002-2016, by ionCube Ltd. with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies

bobbingwide commented 7 years ago

On different hosting I got the same results as Matt and Alain. Note: OPcache is not enabled on this hosting.

[oikcouk@host58 public_html]$ php issue-19.php PHP_SAPI: cli cli: cli [oikcouk@host58 public_html]$ php -v PHP 7.0.15 (cli) (built: Jan 19 2017 14:20:34) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies [oikcouk@host58 public_html]$

bobbingwide commented 7 years ago

This is a belated follow up to the issue that I reported at WordCamp London and which Anton from SiteGround has investigated.

I have today reproduced the problem on https://rowlandscastle.org.uk/path/issue-19.php

See below for more details.

Extract from Anton's response, dated 4th May 2017.

Dear Herb Miller, My name is Anton and we met at WordCamp London this year. First I would like to apologize for the late reply, I had a small incident with my arm which rendered me unable to type for a while. The reason for this email is our conversation about an issue you have described on this page: https://github.com/bobbingwide/oik-batch/issues/19

I tried to replicate the issue on my end to no avail. I was using my shared hosting account as well as a Cloud plan and I get consistent results both when executed through browser and shell.

Today's tests ( 23 Jun )

Replication of problem on rowlandscastle.org.uk

With PHP 7.0.16

Implemented the code referenced in https://github.com/bobbingwide/oik-batch/issues/19#issuecomment-285929511 under account rcorguk

./  ../  browsed.php  issue-19.php

set up a php alias for PHP 7

alias php='/usr/local/php70/bin/php-cli'

Run tests. The results demonstrated the same problem as before.

Note: php -v gives

PHP 7.0.16 (cli) (built: Feb 20 2017 11:16:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v6.0.8, Copyright (c) 2002-2016, by ionCube Ltd.
    with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies

With PHP 7.1.2

alias p71='/usr/local/php71/bin/php-cli'

Used cpanel to change PHP version of the path folder to 7.1.2

PHP 7.1.2 (cli) (built: Feb 20 2017 17:16:31) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.2, Copyright (c) 1999-2017, by Zend Technologies

I was able to reproduce the problem. Currently, when I visit https://rowlandscastle.org.uk/path/issue-19.php the result is

PHP_SAPI: cgi-fcgi cli: cli