Permalink
Browse files

Added preview support, a getStatus method for the document class, opt…

…ion arrays are not converted to proper Vuzit parameters (such as Boolean).

Modified   lib/base.php
* parametersClean: Cleans parameters by switching all of the Boolean parameters to "1" and "0".
* parametersToUrl: Now calls parametersClean.
* postParameters: Creates a parameter array if one does not exist.  Added the "included_pages" parameter.  Calls the new signature function with the expanded parameters.

Modified   lib/document.php
* getStatus: Returns the document status.
* destroy, upload: The code is now simplified thanks to the new postParameters code.
* findById: Added a line to grab the status message.  Simplified because of postParameters method.

Modified   lib/service.php
* setServiceUrl: Throws an exception if there is a trailing slash on the end of the URL.
* setUserAgent: Fixed the code so that it works correctly.
* signature: Added the $pages and $label parameters and added them to the string to sign.

Modified   test/test.php
* Added page preview support and the accompanying Javascript to 2.9.
* printArray: Method that prints out the contents of a hash table into a list for testing purposes.
* Added printArray wherever there were options specified.
* Added getStatus to the page load and secure support to the upload command.
  • Loading branch information...
1 parent 932680b commit 82d0aca15e4bf0c1275ac5c2ac0455ab45d865b5 @bmatzelle bmatzelle committed Oct 23, 2009
Showing with 113 additions and 57 deletions.
  1. +47 −19 lib/base.php
  2. +17 −25 lib/document.php
  3. +10 −5 lib/service.php
  4. +39 −8 test/test.php
View
@@ -22,11 +22,44 @@ protected static function curlRequest()
}
/*
+ Cleans the parameters.
+ */
+ protected static function parametersClean($parameters)
+ {
+ $result = array();
+
+ foreach ($parameters as $key => &$val)
+ {
+ // Convert true/false to "1" and "0".
+ if(is_bool($val))
+ {
+ $val = $val ? "1" : "0";
+ }
+ else
+ {
+ // Remove empty values
+ if(!empty($val)) {
+ // Handle file uploads via a HTTP post operation
+ if($key != 'upload' && substr($val, 0, 1) == "@") {
+ $val = chr(32).$val;
+ }
+ }
+ }
+
+ $result[$key] = $val;
+ }
+
+ return $result;
+ }
+
+ /*
Changes an array (hash table) of parameters to a url.
*/
protected static function parametersToUrl($resource, $params, $id = null,
$extension = 'xml')
{
+ $params = self::parametersClean($params);
+
$result = Vuzit_Service::getServiceUrl() . "/" . $resource;
if($id != null) {
$result .= "/" . $id;
@@ -48,32 +81,27 @@ protected static function parametersToUrl($resource, $params, $id = null,
// memory and the other classes now obey this convention.
protected static function postParameters($method, $params, $id = '')
{
+ if($params == null) {
+ $params = array();
+ }
+
$params['method'] = $method;
$params['key'] = Vuzit_Service::getPublicKey();
+ // Signature variables
$timestamp = time();
- $sig = Vuzit_Service::signature($method, $id, $timestamp);
- $params['signature'] = $sig;
$params['timestamp'] = sprintf("%d", $timestamp);
-
- $result = array();
- foreach ($params as $key => &$val) {
- if(!empty($val)) {
- if (is_array($val)) {
- $val = implode(',', $val);
- }
-
- if($key != 'upload' && substr($val, 0, 1) == "@"){
- $val = chr(32).$val;
- }
- // TODO: If is_bool && $val == true then turn to "1"
- // If is_bool && $val == false then turn to "0"
-
- $result[$key] = $val;
- }
+ $pages = '';
+ if(array_key_exists("included_pages", $params)) {
+ $pages = $params["included_pages"];
}
+ $label = '';
- return $result;
+ // Create the signature
+ $sig = Vuzit_Service::signature($method, $id, $timestamp, $pages, $label);
+ $params['signature'] = $sig;
+
+ return $params;
}
}
?>
View
@@ -19,6 +19,7 @@ public function __construct() {
$this->pageWidth = -1;
$this->pageHeight = -1;
$this->fileSize = -1;
+ $this->status = -1;
}
/*
@@ -29,6 +30,13 @@ public function getId() {
}
/*
+ Returns the document status.
+ */
+ public function getStatus() {
+ return $this->status;
+ }
+
+ /*
Returns the document title.
*/
public function getTitle() {
@@ -44,9 +52,6 @@ public function getSubject() {
/*
Returns the document page count.
-
- Returns:
- int
*/
public function getPageCount() {
return $this->pageCount;
@@ -78,12 +83,9 @@ public function getFileSize() {
*/
public static function destroy($webId)
{
- $method = "destroy";
- $params = array();
+ $params = self::postParameters("destroy", null, $webId);
- $post_params = self::postParameters($method, $params, $webId);
-
- $url = self::parametersToUrl('documents', $post_params, $webId);
+ $url = self::parametersToUrl('documents', $params, $webId);
$ch = self::curlRequest();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // only if expecting response
@@ -118,16 +120,10 @@ public static function downloadUrl($webId, $fileExtension)
*/
public static function findById($webId, $options = null)
{
- $method = "show";
-
- if($options == null) {
- $options = array();
- }
-
- $post_params = self::postParameters($method, $options, $webId);
+ $params = self::postParameters("show", $options, $webId);
$ch = self::curlRequest();
- $url = self::parametersToUrl('documents', $post_params, $webId);
+ $url = self::parametersToUrl('documents', $params, $webId);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // only if expecting response
@@ -167,6 +163,7 @@ public static function findById($webId, $options = null)
$result->pageWidth = (int)$xml->width;
$result->pageHeight = (int)$xml->height;
$result->fileSize = (int)$xml->file_size;
+ $result->status = (int)$xml->status;
}
return $result;
@@ -177,24 +174,19 @@ public static function findById($webId, $options = null)
*/
public static function upload($file, $options = null)
{
- $method = "create";
- if($options == null) {
- $options = array();
- }
-
+ $params = self::postParameters("create", $options);
if(!file_exists($file)) {
throw new Vuzit_ClientException("Cannot find file at path: $file");
}
- $options['upload'] = "@".$file;
-
- $post_params = self::postParameters($method, $options);
+ $params['upload'] = "@".$file;
+ $params = self::parametersClean($params);
$ch = self::curlRequest();
$url = Vuzit_Service::getServiceUrl() . "/documents.xml";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // only if expecting response
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$xml_string = curl_exec($ch);
if(!$xml_string) {
View
@@ -7,8 +7,8 @@ class Vuzit_Service
private static $publicKey = '';
private static $privateKey = '';
private static $serviceUrl = 'http://vuzit.com';
- private static $productName = "VuzitPHP Library 1.2.0";
- private static $userAgent = "VuzitPHP Library 1.2.0";
+ private static $productName = "VuzitPHP Library 2.0.0";
+ private static $userAgent = "VuzitPHP Library 2.0.0";
// Public static getter and setter methods
@@ -51,6 +51,10 @@ public static function getServiceUrl() {
Sets service URL.
*/
public static function setServiceUrl($url) {
+ $url = trim($url);
+ if(substr($url, -1) == '/') {
+ throw new Exception("Trailing slashes (/) in service URLs are invalid");
+ }
self::$serviceUrl = $url;
}
@@ -65,7 +69,7 @@ public static function getUserAgent() {
Sets the user agent.
*/
public static function setUserAgent($agent) {
- self::$userAgent = (self::$agent + " (" + self::$productName + ")");
+ self::$userAgent = ($agent + " (" + self::$productName + ")");
}
// Public static methods
@@ -75,12 +79,13 @@ public static function setUserAgent($agent) {
with the Vuzit Javascript API then the value must be encoded with the
PHP rawurlencode function. See the Wiki example for more information:
*/
- public static function signature($service, $id = '', $time = null)
+ public static function signature($service, $id = '', $time = null,
+ $pages = '', $label = '')
{
$result = null;
$time = ($time == null) ? time() : $time;
- $msg = $service . $id . self::getPublicKey() . $time;
+ $msg = $service . $id . self::getPublicKey() . $time . $pages . $label;
$hmac = self::hmac_sha1(self::getPrivateKey(), $msg);
return base64_encode($hmac);
View
@@ -44,7 +44,7 @@ function header_load($doc = null)
{
$timestamp = time();
$id = $doc->getId();
- $sig = Vuzit_Service::signature("show", $doc->getId(), $timestamp);
+ $sig = Vuzit_Service::signature("show", $doc->getId(), $timestamp, get("p"));
$onload = "initialize()";
}
?>
@@ -54,9 +54,9 @@ function header_load($doc = null)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Vuzit <?php echo get("c") ?> Command Example</title>
- <link href="<?php echo Vuzit_Service::getServiceUrl(); ?>/stylesheets/Vuzit-2.8.css"
+ <link href="<?php echo Vuzit_Service::getServiceUrl(); ?>/stylesheets/Vuzit-2.9.css"
rel="Stylesheet" type="text/css" />
- <script src="<?php echo Vuzit_Service::getServiceUrl(); ?>/javascripts/Vuzit-2.8.js"
+ <script src="<?php echo Vuzit_Service::getServiceUrl(); ?>/javascripts/Vuzit-2.9.js"
type="text/javascript"></script>
<script type="text/javascript">
// Called when the page is loaded.
@@ -65,8 +65,11 @@ function initialize() {
vuzit.Base.webServerSet({ host: '<?php echo domain(); ?>', port: '80' });
vuzit.Base.imageServerSet({ host: '<?php echo domain(); ?>', port: '80' });
- var options = {signature: '<?php echo rawurlencode($sig); ?>',
- timestamp: '<?php echo $timestamp ?>'}
+ var options = { signature: '<?php echo rawurlencode($sig); ?>',
+ <?php if(get("p") != null) { ?>
+ includedPages: '<?php echo get("p"); ?>',
+ <?php } ?>
+ timestamp: '<?php echo $timestamp ?>'}
var viewer = vuzit.Viewer.fromId("<?php echo $id; ?>", options);
viewer.display(document.getElementById("vuzit_viewer"), { zoom: 1 });
@@ -89,6 +92,23 @@ function footer_load()
<?php
}
+// Prints out an array for things like options.
+function printArray($list)
+{
+ if(count($list) < 1) {
+ return '';
+ }
+
+ $result = 'Array:';
+ $result .= '<ul>';
+ foreach ($list as $key => &$value) {
+ $result .= '<li>' . $key . ' = ' . $value . '</li>';
+ }
+ $result .= '</ul>';
+
+ return $result;
+}
+
// COMMAND FUNCTIONS
// Runs the load command
@@ -109,13 +129,16 @@ function load_command()
<h3>
Document - <?php echo $doc->getID(); ?>
</h3>
+ <?php echo printArray($options); ?>
+ <p>Results:</p>
<ul>
<li>Title: <?php echo $doc->getTitle(); ?></li>
<li>Subject: <?php echo $doc->getSubject(); ?></li>
<li>Page count: <?php echo $doc->getPageCount(); ?></li>
<li>Width: <?php echo $doc->getPageWidth(); ?></li>
<li>Height: <?php echo $doc->getPageHeight(); ?></li>
<li>File size: <?php echo $doc->getFileSize(); ?></li>
+ <li>Status: <?php echo $doc->getStatus(); ?></li>
<li><a href="<?php echo $pdf_url; ?>">PDF Download</a></li>
</ul>
@@ -136,7 +159,8 @@ function delete_command()
}
catch(Vuzit_ClientException $ex)
{
- echo "Delete of " . $id . " failed with code [" . $ex->getCode() . "], message: " . $ex->getMessage();
+ echo "Delete of " . $id . " failed with code [" . $ex->getCode() .
+ "], message: " . $ex->getMessage();
}
footer_load();
}
@@ -151,13 +175,17 @@ function upload_command()
if(get("d") != null) {
$options["download_document"] = get("d");
}
+ if(get("s") != null) {
+ $options["secure"] = get("s");
+ }
$doc = Vuzit_Document::upload(get("path"), $options);
header_load($doc);
?>
<h3>
Document - <?php echo $doc->getID(); ?>
</h3>
+ <?php echo printArray($options); ?>
<div id="vuzit_viewer" style="width: 650px; height: 500px;"></div>
<?php
@@ -188,7 +216,7 @@ function event_command()
break;
default:
header_load();
- event_load_html($list);
+ event_load_html($list, $options);
footer_load();
break;
}
@@ -224,7 +252,7 @@ function event_load_csv($list, $fileName = "events.csv")
}
// Loads the output of the event list as HTML.
-function event_load_html($list)
+function event_load_html($list, $options)
{
?>
<h3>
@@ -233,6 +261,8 @@ function event_load_html($list)
<p>
Total events: <?php echo count($list); ?>
</p>
+ <?php echo printArray($options); ?>
+ <p>Results:</p>
<ol>
<?
$event = count($list);
@@ -284,6 +314,7 @@ function event_load_html($list)
Vuzit_Service::setPublicKey($public_key);
Vuzit_Service::setPrivateKey($private_key);
+Vuzit_Service::setUserAgent("Vuzit Test Suite");
// Grab the command and execute
switch(get("c"))

0 comments on commit 82d0aca

Please sign in to comment.