PHP4 Example

Caution

For server security, please refer to the link below when handling this work.

[Software Security Weakness Diagnosis Guide]

1. Install the JSON Encoder

Download: JSON.phps

2. Image Upload (Video and File uploads follow the same pattern)

Caution

The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.

For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.

<?php
$uploadDir = 'uploads/images';
$fieldName = 'file';

$fileNameParts  = explode('.', $_FILES[$fieldName]['name']);
$extension      = end($fileNameParts);
$tmpName        = $_FILES[$fieldName]['tmp_name'];
$newFileName    = sha1(microtime());
$fileUploadPath = "${uploadDir}/${newFileName}.${extension}";
move_uploaded_file($tmpName, $fileUploadPath);

include("./JSON.phps");

header('Content-Type: application/json');
echo json_encode_new(['uploadPath' => $fileUploadPath]);

function json_encode_new($data) {
    $json = new Services_JSON();
    return $json->encode($data);
}
?>

3. HWP / MS Word / LibreOffice Document Import

Caution

The file-upload portion of the sample code below is intentionally minimal and lacks proper security handling.

For the file-upload portion, use what is already in place inside your project, and refer to the code below for the integration portion only.

<?php
$uploadDir = 'uploads/docs';
$fieldName = 'file';

$fileNameParts  = explode('.', $_FILES[$fieldName]['name']);
$extension      = end($fileNameParts);
$tmpName        = $_FILES[$fieldName]['tmp_name'];
$newFileName    = sha1(microtime());
$fileUploadPath = "${uploadDir}/${newFileName}.${extension}";
move_uploaded_file($tmpName, $fileUploadPath);

$wordDir    = 'works';
$importPath = "${wordDir}/${newFileName}";
executeConverter($fileUploadPath, $importPath);

// From v2.3.0 the filename is "document.pb" (was "document.word.pb")
$pbFilePath     = "${importPath}/document.pb";
$serializedData = readPBData($pbFilePath);

include("./JSON.phps");

header('Content-Type: application/json');
echo json_encode_new([
    'serializedData' => $serializedData,
    'importPath'     => $importPath,
]);

function executeConverter($inputFilePath, $outputFilePath) {
    $sedocConverterPath = 'c:/sedocConverter/sedocConverter.exe';
    $fontsDir           = 'c:/sedocConverter/fonts';
    $tempDir            = 'c:/sedocConverter/tmp';
    $cmd = "${sedocConverterPath} -f ${fontsDir} ${inputFilePath} ${outputFilePath} ${tempDir}";
    exec($cmd);
}

function readPBData($pbFilePath) {
    $filesize = filesize($pbFilePath);
    $zd       = gzopen($pbFilePath, 'r');
    $data     = substr(gzread($zd, $filesize), 16);
    gzclose($zd);

    $byteArray = unpack('C*', gzuncompress($data));
    return array_values($byteArray);
}

function json_encode_new($data) {
    $json = new Service_JSON();
    return $json->encode($data);
}
?>

Related Information