如何解决奇怪的PHP解析错误?

时间:2019-01-21 14:43:15

标签: php cloudkit-web-services

使用PHP 7.0.33

我有通过CloudKit Web服务执行查找的PHP代码。我使用该代码已有一年或更长时间,并且效果很好。

但是,当在一个文件(usersavedworks.html)中需要时,该代码将引发解析错误

  

解析错误:语法错误,意外的'['in   /home1/arbourme/public_html/artsinpa/user/listsavedworks.php在线   60

第60行是:

$signature = $time . ":" . base64_encode(hash("sha256", $worksQuery, true)) . ":" . explode('cloudkit.com', $url)[1];

此行不会在其中包含的任何其他文件中引发解析错误。仅当usersavedworks.html中要求时,才会发生此错误。

usersavedworks.html包含另一个php文件。

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'profileFunctions.php';
require 'listsavedworks.php';

<html>
<head>
<meta charset="UTF-8">
<title>Profile Page</title>
<link href="../c/main.css" rel="stylesheet" type="text/css">
<link href="../c/profile.css" rel="stylesheet" type="text/css">
</head> . . . file continues

'profileFunctions.php'可以在任何使用的地方工作,除非'listsavedworks.php'要求

我假设有一些不明显的代码冲突导致该错误。这可能吗?我将如何查找/调试冲突?

这是profileFunctions.php:

<?php

// Show only the post allowed by role
function getAllowedPosts(){
    if (in_array("Artist", $_SESSION["roles"])) {
    echo '<li><a href= OMITTED.php?r='.$_SESSION["userRecordName"].'">Update Artist Info</a></li>'; 
    }

    if (in_array("Musician", $_SESSION["roles"])) {
    echo '<li><a href= OMITTED?r='.$_SESSION["userRecordName"].'">Update Musician Info</a></li>';   
    }

    if (in_array("Author", $_SESSION["roles"])) {
    echo '<li><a href="OMITTED?r='.$_SESSION["userRecordName"].'">Update Author Info</a></li>'; 
    }

    if (in_array("Art Professional", $_SESSION["roles"])) {
    echo '<li><a href="OMITTED?r='.$_SESSION["userRecordName"].'">Update Professional Info</a></li>';   
    }

    if (in_array("Arts Organization Representative", $_SESSION["roles"])) {
    echo '<li><a href="OMITTED?r='.$_SESSION["userRecordName"].'">Update Arts Org Info</a></li>';   
    }

    if (in_array("Ensemble Representative", $_SESSION["roles"])) {
    echo '<li><a href="OMITTED?r='.$_SESSION["userRecordName"].'">Update Ensemble Info</a></li>';   
    }

    if (in_array("Artist", $_SESSION["roles"]) || in_array("Author", $_SESSION["roles"]) || in_array("Musician", $_SESSION["roles"])) {
    echo '<li><a href="OMITTED">Post a New Work</a></li>';  
    echo '<li><a href="OMITTED php?">Edit a Work</a></li>';
    }

    if (in_array("Artist", $_SESSION["roles"]) || in_array("Arts Organization Representative", $_SESSION["roles"]) ) {
    echo '<li><a href= OMITTED >Post a New Art Show</a></li>';
    echo '<li><a href="OMITTED.php">Edit an Art Show</a></li>';
    }

    if (in_array("Musician", $_SESSION["roles"]) || in_array("Ensemble Representative", $_SESSION["roles"]) ) {
    echo '<li><a href=OMITTED >Post a New Performance</a></li>';
    echo '<li><a href= OMITTED >Edit a Performance</a></li>';
    }
}

function setRoles(){
    if (sizeof($_SESSION['roles']) == 1) {

        echo '<h1>Your Current Role</h1>';
    } else {
        echo '<h1>Your Current Roles</h1>';
    }

    foreach ($_SESSION['roles'] as $role){
        echo '<p>'.$role.'</p>';
    }
}

function setRoleApplicationLinks(){

    if(!in_array("Artist", $_SESSION["roles"])){
        echo '<li><a href= OMITTED">Artist</a></li>';
    }

    if(!in_array("Musician", $_SESSION["roles"])){
        echo '<li><a href="OMITTED">Musician</a></li>';
    }

    if(!in_array("Author", $_SESSION["roles"])){
        echo '<li><a href= OMITTED >Author</a></li>';
    }

    if(!in_array("Art Professional", $_SESSION["roles"])){
        echo '<li><a href= OMITTED >Art Professional</a></li>';
    }

    if(!in_array("Arts Organization Representative", $_SESSION["roles"])){
        echo '<li><a href= OMITTED >Arts Organization</a></li>';

    }

    if(!in_array("Ensemble Representative", $_SESSION["roles"])){
        echo '<li><a href= OMITTED >Performing Ensemble</a></li>';
    }
}

// the userRole needs to be set for a perfomance or art show so we can later
// delete the performance or art show records associated with users that may have
// more than one role which allows them to post art shows or performances.
function setUserRole($recordType){
    if ( $recordType == "Art Show" && in_array("Artist", $_SESSION['roles']) && in_array("Arts Organization Representative", $_SESSION['roles'])) {
    echo '<li><label>With which of your roles should this art show information be associated with?</label></li>';
    echo '<li><input type="radio" name="userRole" value="Artist"> Artist: '.$_SESSION['fullName'].'</li>';
    echo '<li><input type="radio" name="userRole" value="Arts Organization Representative"> Representative of '.$_SESSION['artsOrgRepFor'].'</li>';
    } else {

        if (in_array("Artist", $_SESSION['roles'])) {
            $prsenterName = $_SESSION['fullName'];
            echo '<input type="hidden" name="userRole" value="Artist"/>';
        }

        if (in_array("Arts Organization Representative", $_SESSION['roles'])) {
            $prsenterName = $_SESSION['artsOrgRepFor'];
            echo '<input type="hidden" name="userRole" value="Arts Organization Representative"/>';
        }

    }

    if ( $recordType == "Performance" && in_array("Musician", $_SESSION['roles']) && in_array("Ensemble Representative", $_SESSION['roles'])) {
    echo '<li><label>With which of your roles should this performance information be associated with?</label></li>';
    echo '<li><input type="radio" name="userRole" value="Musician"> Musician</li>';
    echo '<li><input type="radio" name="userRole" value="Ensemble Representative"> Ensemble Representative</li>';
    } else {

        if (in_array("Musician", $_SESSION['roles'])) {
            echo '<input type="hidden" name="userRole" value="Musician"/>';
        }

        if (in_array("Ensemble Representative", $_SESSION['roles'])) {
            echo '<input type="hidden" name="userRole" value="Ensemble Representative"/>';
        }
    }
}

listsavedworks.php是:

<?php
function listWorks(){
// (1) get saved works works records
$savedWorks = $_SESSION['savedworks'];
$worksQuery =worksQ2($savedWorks);
$results = getWorks($worksQuery);


// (2) Check for CK Errors, if any
$error = checkCKErrors($results);
    if ($error[0] == "BAD_REQUEST") {
        echo "CK Error on record query: ".$error[0].": ".$error[1];
     } else { 

// (3) list the works in a table

$showRecords = $results['records'];
    echo '<table class="show-savedworks">';
    foreach ($showRecords as $record){

        list($width, $height, $type, $attr) = getimagesize($record['fields']['imageAURL']['value']);
        $width = $width/8;
        $height = $height/8;
        echo '<tr>';
        if ($width == 0 || $height == 0){
            echo '<td valign="top"><a href= OMITTED?iid='.$record['recordName'].'"><img src= OMITTED/i/no_image_fs.png" width="105" height="157"></a></td>';
        } else {
            echo '<td valign="top"><a href= OMITTED.php?iid='.$record['recordName'].'"><img src="'.$record['fields']['imageAURL']['value'].'" width="'.$width.'" height="'.$height.'"></a></td>';
        }
        echo '<td valign="top"><a href= OMITTED'.$record['recordName'].'.html">'.$record['fields']['title']['value'].'</a></td>';
        echo '</tr>';
    }
    echo '</table>';
}
    if(sizeof($showRecords) == 0 ){
        echo '<p>You have no saved works at the moment.</p>';
    }
}

function getWorks($worksQuery){

// Set Cloud Kit DB info
$KEY_ID                 =  OMITTED
$CONTAINER              =  OMITTED
$PRIVATE_PEM_LOCATION   =  OMITTED

// Set CK web service URL
$url = 'OMITTED/1/' . $CONTAINER . '/development/public/records/lookup';

// Set cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Create signature
date_default_timezone_set('UTC');
$explode_date = explode('+', date("c", time()));
$time = $explode_date[0] . 'Z';
$signature = $time . ":" . base64_encode(hash("sha256", $worksQuery, true)) . ":" . explode('cloudkit.com', $url)[1];

// Get private key
$pkeyid = openssl_pkey_get_private("file://" . $PRIVATE_PEM_LOCATION); 

// Sign signature with private key
if(openssl_sign($signature, $signed_signature, $pkeyid, "sha256WithRSAEncryption")) {
    openssl_free_key($pkeyid);
    // Set headers
    curl_setopt($ch, CURLOPT_HTTPHEADER,
        [
            "Content-Type: text/plain",
            "X-Apple-CloudKit-Request-KeyID: " . $KEY_ID,
            "X-Apple-CloudKit-Request-ISO8601Date: " . $time,
            "X-Apple-CloudKit-Request-SignatureV1: " . base64_encode($signed_signature),
        ]
    );

    // Set body
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $worksQuery);

    // Send the request & save response to $resp
    $resp = curl_exec($ch);
    if(!$resp) {
        die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
    } else {
        $json_result = json_decode($resp, true);
        return $json_result;
    }
    curl_close($ch);
} else {
    while ($msg = openssl_error_string()) {
        echo $msg . "<br />\n";
    }
}}

// CloudKit returns errors in two different formats,
// so handle these
function checkCKErrors($result){
if ($result['records'][0]['serverErrorCode'] == "BAD_REQUEST") {
            $error = $result['records'][0]['serverErrorCode'];
            $reason = $result['records'][0]['reason'];
            return [$error,$reason];
        } else {
            $error = $result['serverErrorCode'];
            $reason = $result['reason'];
            return [$error,$reason];
        }
}


// set lookup for lookup
function worksQ2($savedWorks){
    $recordDict = "";
    foreach ($savedWorks as $item) {
        $recordDict = $recordDict."{\"recordName\":\"$item\"},";
    }
    $recordDict = substr($recordDict,0,-1);  // remove that last comma
    $request = '{"records":['.$recordDict.']}';
    return $request;
}
?>

0 个答案:

没有答案