运行“php -f file.php”会打印文件内容,而不是执行它

运行“php -f file.php”会打印文件内容,而不是执行它

我刚刚安装了 Ubuntu Unity (23.10) 操作系统,并在其中安装了 PHP。

现在我想从终端运行一个 PHP 文件,但它不执行它,而只是打印其内容。(该 PHP 文件来自GitHub 仓库,不是我的。)

打印“Hello world”的简单文件就可以正常工作。

没有错误。

marc@workstation:~/projects/playground/SmartCrypto2/SmartCrypto$ php smart.php
<?
require('tv_geturl.php');

$UserId = "654321";
$AppId = "12345";
$dest_hash = "";
$SKPrime = "";
$ctx = "";
//$Guid =  "7e509404-9d7c-46b4-8f6a-e2a9668ad184";
$deviceId =  "784c5390-73a2-441e-91d6-1980b114e866";
$hash = "";
$AES_key= "";
$lastRequestId = "";
$sessionId = 0;
$tvIP = "192.168.2.100";
$tvPort = "8080";

function getFullUrl($urlPath)
{
    global $tvIP, $tvPort;
    return "http://".$tvIP.":".$tvPort.$urlPath;
}
function GetFullRequestUri($step, $appId, $deviceId)
{
    return getFullUrl("/ws/pairing?step=".$step."&app_id=".$appId."&device_id=".$deviceId);
}
function CheckPinPageOnTv()
{
    $full_url = getFullUrl("/ws/apps/CloudPINPage");
    $page = geturl($full_url,$tvPort);
    if(preg_match("@<state>([^<>]*)</state>@si",$page,$out))
    {
        echo "Current state: ".$out[1]."\n";
        if($out[1] == "stopped")
        {
            return true;
        }
    }
    return false;
}
function ShowPinPageOnTv()
{
    geturl2(getFullUrl("/ws/apps/CloudPINPage"), "pin4");
}
function StartPairing()
{
    global $lastRequestId;
    $lastRequestId=0;
    if(CheckPinPageOnTv())
    {
        echo "Pin NOT on TV"."\n";
        ShowPinPageOnTv();
    }
    else
        echo "Pin ON TV"."\n";
}
function FirstStepOfPairing()
{
    global $AppId, $deviceId;
    $firstStepURL = GetFullRequestUri(0,$AppId, $deviceId)."&type=1";
    //echo("firstStepURL: '".$firstStepURL."'\n");
    $firstStepResponse = geturl($firstStepURL);
    //echo ("firstStepResponse: ".$firstStepResponse."");
}
function GenerateServerHello($pin)
{
    global $UserId ,$hash,$AES_key;
    $res = shell_exec("./smartcrypto generateServerHello $UserId $pin");
    if(preg_match("@AES key: ([^ \n]*).*hash: ([^ \n]*).*ServerHello: ([^ \n]*)@si",$res,$out))
    {
        $AES_key = $out[1];
        $hash = $out[2];
        return $out[3];
    }
    return false;
}
function parseClientHello($clientHello)
{
    global $UserId, $hash, $AES_key,$dest_hash, $SKPrime, $ctx;
    $res = shell_exec("./smartcrypto parseClientHello $clientHello $hash $AES_key $UserId");
    if(preg_match("@dest_hash: ([^ \n]*).*SKPrime: ([^ \n]*).*ctx: ([^ \n]*)@si",$res,$out))
    {
        $dest_hash = $out[1];
        $SKPrime = $out[2];
        $ctx = $out[3];
        echo "dest_hash: $dest_hash\n";
        echo "SKPrime: $SKPrime\n";
        echo "ctx: $ctx\n";
        return true;
    }
    return false;
}
function HelloExchange($pin)
{
    global $AppId, $deviceId, $lastRequestId, $hash, $AES_key;
    $serverHello = GenerateServerHello($pin);
    if(!$serverHello)
        return false;
    $content = "{\"auth_Data\":{\"auth_type\":\"SPC\",\"GeneratorServerHello\":\"".$serverHello."\"}}";
    $secondStepURL = GetFullRequestUri(1,$AppId, $deviceId);
    //echo "secondStepURL: '".$secondStepURL."'\n";
    //echo "secondStep content: '".$content."'\n";
    $secondStepResponse = geturl2($secondStepURL,$content);
    //echo "secondStepResponse: ".$secondStepResponse."";
    if(!preg_match("@request_id.*?(\d).*?GeneratorClientHello.*?:.*?(\d[0-9a-zA-Z]*)@si",$secondStepResponse,$out))
        return false;
    #var_dump($out);
    $requestId = $out[1];
    $clientHello = $out[2];
    //echo "clientHello: '".$clientHello."'\n";
    //echo "requestId: '".$requestId."'\n";
    //echo "hash: '".$hash."'\n";
    //echo "AES key: '".$AES_key."'\n";
    $lastRequestId = $requestId; 
    return parseClientHello($clientHello);
}
function generateServerAcknowledge()
{
    global $SKPrime;
    $SKPrimeHash = sha1(hex2bin($SKPrime."01"));
    return "0103000000000000000014".strtoupper($SKPrimeHash)."0000000000";
}
function ParseClientAcknowledge($clientAck)
{
    global $SKPrime;
    $SKPrimeHash = sha1(hex2bin($SKPrime."02"));
    $tmpClientAck = "0104000000000000000014".strtoupper($SKPrimeHash)."0000000000";
    return $clientAck === $tmpClientAck;
}
function AcknowledgeExchange()
{
    global $lastRequestId,$AppId, $deviceId, $sessionId;
    $serverAckMessage = generateServerAcknowledge();
    $content="{\"auth_Data\":{\"auth_type\":\"SPC\",\"request_id\":\"".$lastRequestId."\",\"ServerAckMsg\":\"".$serverAckMessage."\"}}";
    $thirdStepURL = GetFullRequestUri(2,$AppId, $deviceId);
    //echo "thirdStepURL: '".$thirdStepURL."'\n";
    //echo "thirdStep content: $content\n";
    $thirdStepResponse = geturl2($thirdStepURL,$content);
    //echo "thirdStepResponse: '$thirdStepResponse'";
    if(strstr($thirdStepResponse,"secure-mode"))
    {
        echo "TODO: Implement handling of encryption flag!!!!\n";
        die(-1);
    }
    if(!preg_match("@ClientAckMsg.*?:.*?(\d[0-9a-zA-Z]*).*?session_id.*?(\d)@si",$thirdStepResponse,$out))
    {
        echo "Unable to get session_id and/or ClientAckMsg!!!\n";
        die(-1);
    }
    $clientAck = $out[1];
    //echo "clientAck: '$clientAck'\n";
    if(!ParseClientAcknowledge($clientAck))
    {
        echo "Parse client ac message failed.\n";
        die(-1);
    }
    $sessionId=$out[2];
    echo "sessionId: $sessionId\n";
    return $sessionId;
}
function ClosePinPageOnTv()
{
    geturl(getFullUrl("/ws/apps/CloudPINPage/run"),"DELETE");
   return false;
}
StartPairing();
$pinAccepted = false;
do 
{
    echo "Please enter pin from tv:\n";
    $tvPIN = readline();
    echo "Got pin: '".$tvPIN."'\n";
    FirstStepOfPairing();
    $pinAccepted=HelloExchange($tvPIN);
    if($pinAccepted)
        echo "Pin accepted :)\n\n";
    else
        echo "Pin incorrect. Please try again...\n\n";
}while(!$pinAccepted);
$currentSessionId = AcknowledgeExchange();
ClosePinPageOnTv();
echo "Authorization successfull :)\n\n";
?>
PHP 8.2.10-2ubuntu1 (cli) (built: Sep  5 2023 14:37:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.10-2ubuntu1, Copyright (c), by Zend Technologies

退出代码也是 0。

有什么想法/提示吗?

答案1

该文件用于<?启动 PHP 代码。它应该使用<?php

使用 bare<?需要short_open_tags启用 PHP 选项(通过 php.ini 或类似文件)。我不建议这样做 - 它可能导致与 XML 文件(用于<?xml其标题)发生冲突,而且实际上并不能节省太多的输入。

即使该项目不是您的,它似乎也是一个足够小的项目,将其拥有的两个 PHP 文件<?的打开标签更改为 会更简单<?php(嗯,一个,因为另一个已经有完整的打开标签)。

答案2

代码使用short_open_tag仅适用于 php.ini 设置short_open_tag=1- 使用以下命令调用它

php -d short_open_tag=1 smart.php

可以解决这个特殊问题。

但是,在我看来,您的代码是为 PHP5 或更早版本(甚至是 PHP4?)编写的,而且我怀疑即使使用 ,它也无法在 PHP8.2 上正确执行short_open_tag

仅供参考,short_open_tag不推荐使用,并且已经多次尝试弃用和删除,但都没有成功short_open_tag。(12

相关内容