Example #1
0
function getDocEditorKey($fileUri)
{
    return GenerateRevisionId(getCurUserHostAddress() . "/" . basename($fileUri));
}
function save()
{
    $contentType = "text/plain";
    $downloadUri = $_GET["fileuri"];
    $fileName = $_GET["filename"];
    if (empty($downloadUri) || empty($fileName)) {
        $result["error"] = 'Error request';
        return $result;
    }
    $newType = trim(pathinfo($downloadUri, PATHINFO_EXTENSION), '.');
    $currentType = trim(!empty($_GET["filetype"]) ? $_GET["filetype"] : pathinfo($downloadUri, PATHINFO_EXTENSION), '.');
    if (strtolower($newType) != strtolower($currentType)) {
        $key = GenerateRevisionId($downloadUri);
        $newFileUri;
        try {
            $percent = GetConvertedUri($downloadUri, $newType, $currentType, $key, FALSE, $newFileUri);
            if ($percent != 100) {
                $result["error"] = "error: Can't convert file";
                return $result;
            }
        } catch (Exception $e) {
            $result["error"] = "error: " . $e->getMessage();
            return $result;
        }
        $downloadUri = $newFileUri;
        $newType = $currentType;
    }
    $path_parts = pathinfo($fileName);
    $ext = $path_parts['extension'];
    $name = $path_parts['basename'];
    $baseNameWithoutExt = substr($name, 0, strlen($name) - strlen($ext) - 1);
    $fileName = $baseNameWithoutExt . "." . $newType;
    if (($data = file_get_contents(str_replace(" ", "%20", $downloadUri))) === FALSE) {
        $result["error"] = 'Bad Request';
        return $result;
    } else {
        file_put_contents(getStoragePath($fileName), $data, LOCK_EX);
    }
    $result["success"] = 'success';
    return $result;
}
Example #3
0
/**
* Request for conversion to a service
*
* @param string $document_uri            Uri for the document to convert
* @param string $from_extension          Document extension
* @param string $to_extension            Extension to which to convert
* @param string $document_revision_id    Key for caching on service
* @param bool   $is_async                Perform conversions asynchronously
*
* @return Xml document request result of conversion
*/
function SendRequestToConvertService($document_uri, $from_extension, $to_extension, $document_revision_id, $is_async)
{
    if (empty($from_extension)) {
        $path_parts = pathinfo($document_uri);
        $from_extension = $path_parts['extension'];
    }
    $title = basename($document_uri);
    if (empty($title)) {
        $title = guid();
    }
    if (empty($document_revision_id)) {
        $document_revision_id = $document_uri;
    }
    $document_revision_id = GenerateRevisionId($document_revision_id);
    $validateKey = GenerateValidateKey($document_revision_id, false);
    $urlToConverter = generateUrlToConverter($document_uri, $from_extension, $to_extension, $title, $document_revision_id, $validateKey, $is_async);
    $response_xml_data;
    $countTry = 0;
    $opts = array('http' => array('method' => 'GET', 'timeout' => $GLOBALS['DOC_SERV_TIMEOUT']));
    if (substr($urlToConverter, 0, strlen("https")) === "https") {
        $opts['ssl'] = array('verify_peer' => FALSE);
    }
    $context = stream_context_create($opts);
    while ($countTry < ServiceConverterMaxTry) {
        $countTry = $countTry + 1;
        $response_xml_data = file_get_contents($urlToConverter, FALSE, $context);
        if ($response_xml_data !== false) {
            break;
        }
    }
    if ($countTry == ServiceConverterMaxTry) {
        throw new Exception("Bad Request or timeout error");
    }
    libxml_use_internal_errors(true);
    $data = simplexml_load_string($response_xml_data);
    if (!$data) {
        $exc = "Bad Response. Errors: ";
        foreach (libxml_get_errors() as $error) {
            $exc = $exc . "\t" . $error->message;
        }
        throw new Exception($exc);
    }
    return $data;
}