<?php if (!defined('PmWiki')) exit ();

/* 
    
	Original info:
	copyright 2010, Adam Overton
	based on code by Jon Haupt, copyright 2007-8
	which was built on code from swf.php copyright 2004 Patrick R. Michaud
	and from quicktime.php copyright 2006 Sebastian Siedentopf.
	
	This file is distributed under the terms of the GNU General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or (at your option) any later version.  

	. . .
	
	This module enables embedding of Scribd documents (pdfs, etc) into wiki pages.
	The simplest use of any of these includes:
	(:Scribd id=... key=...:)

	Additional arguments are also available...
	(:Scribd id=... key=... width=... height=... view=... page=...:)

	Scribd args:
		* id - required - code located after "document_id="
		* key - required - code located after "key-"
		* width - default: 600
		* height - default: 100%
		* page - default: 1
		* view - default: scroll - scroll, book, slideshow, or tiled
		* doc - not required
		* custom_logo_image_url - add your own logo to the top - requires a full url
		
	In addition, Replace-On-Edit Patterns ($ROEPatterns) have been created for this recipe, so that visitors can simply copy-and-paste the embed code from the Scribd website, click save or save-and-edit, and have the code automatically converted to the appropriate pmwiki markup. If this is undesired, an admin can disable this in config.php by setting $ScribdROEenabled to false.
	
	For more info on how to use this recipe, see http://www.pmwiki.org/wiki/Cookbook/Scribd
		
	. . .
		
	Versions:
	* 2010-10-27 - initial release
	
*/

# Version date
$RecipeInfo['SWFSites-Scribd']['Version'] = '2010-10-27';


### SCRIBD
Markup('Scribd', '<img', "/\\(:Scribd\\s+(.*):\\)/e", "ShowScribd('$1')");

# some parameter names have changed - drops the "show_" in some param names
SDVA($ScribdDefaultParams, array(
	'width' => '100%'
	,'height' => '600'
	,'page' => '1'
	,'view' => 'scroll'  # scroll will actually be assigned 'list';
));
SDV($ScribdROEenabled, true);


#### ROEPatterns - SCRIBD EMBED CONVERSION
if ($ScribdROEenabled) {

	# *** Scribd-EMBED-CODE-ROE: NEW UNIVERSAL VERSION - AUGUST 2010 ***
	# Converts pasted Scribd embed code into valid pmwiki (:Scribd:) code
	$ROEPatterns['!<object.*ScribdViewer.swf.*<\/object>!ie'] = "ScribdROE(PSS('$0'))";
	function ScribdROE($pattern) {
		global $ScribdDefaultParams;

		# get id & key, height & width
		preg_match("#height=\"([^\"]+)\" width=\"([^\"]+)\".*document_id=(\d+)&.*key-([^&\"]+)[&\"]#", $pattern, $matches);
		$height = $matches[1]; $width = $matches[2];
		$id = $matches[3]; $key = $matches[4];

		# page
		preg_match("#page=(\d+)[&\"]#", $pattern, $matches);
		$page = $matches[1];

		# viewMode
		preg_match("#viewMode=([^&\"]+)[&\"]#", $pattern, $matches);
		$viewMode = $matches[1];

		# output
		$out = "(:Scribd id=$id key=$key view=$viewMode page=$page height=$height width=$width:)";
		return $out;
	}
	
}
###########



########
# *** SCRIBD VIEWER FUNCTION ***
function ShowScribd($args) {
	global $ScribdDefaultParams, $ScribdOverrideUserParams;
	
	# gather up args
	$args = ParseArgs($args);

	# gather id and key (both necessary)
	if($args['id']) $id = $args['id'];
	else return "Scribd error: no 'id' provided";
	if($args['key']) $key = $args['key'];
	else return "Scribd error: no 'key' provided";

	if($args['doc']) $doc = $args['doc']; # doc doesn't seem to be necessary
	
	# add default parameters before parsing arguments
	$args = array_merge($ScribdDefaultParams, $args); # uses ScribdDefaultParams, unless supplied by user

	$width = $args['width'];
	$height = $args['height'];	
	
	$page = $args['page'];
	$custom_logo_image_url = $args['custom_logo_image_url'];  # load your own logo at the top
	$viewMode = $args['view'];  # view options: scroll, book, slideshow, tiled
	if($viewMode=="scroll") $viewMode = 'list';  # the 'scroll' option actually requires &viewMode=list
	elseif($viewMode=="tiled") $viewMode = 'tile';	# the 'tiled' option actually requires &viewMode=tile
	# the 2 other options (book & slideshow) are fine as-is
	
	# Output
	$out = '<object id="doc_'.$doc.'" name="doc_'.$doc.'" height="'.$height.'" width="'.$width.'" type="application/x-shockwave-flash" data="http://d1.scribdassets.com/ScribdViewer.swf" style="outline:none;" >
		<param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf">
		<param name="wmode" value="opaque">
		<param name="bgcolor" value="#ffffff">
		<param name="allowFullScreen" value="true">
		<param name="allowScriptAccess" value="always">
		<param name="FlashVars" value="document_id='.$id.'&access_key=key-'.$key.'&page='.$page.'&viewMode='.$viewMode;
		if($custom_logo_image_url) $out .= '&custom_logo_image_url='.$custom_logo_image_url;
		$out .= '">
		<embed id="doc_'.$doc.'" name="doc_'.$doc.'" src="http://d1.scribdassets.com/ScribdViewer.swf?document_id='.$id.'&access_key=key-'.$key.'&page='.$page.'&viewMode='.$viewMode;
		if($custom_logo_image_url) $out .= '&custom_logo_image_url='.$custom_logo_image_url;
		$out .= '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="'.$height.'" width="'.$width.'" wmode="opaque" bgcolor="#ffffff">
	</embed></object>';

	return Keep($out);
}