<?php
/**
* Swftools class.
*/
class Swftools_Core
{
//default configuration
protected $config = array(
'pdf2swf' => 'pdf2swf', //default value for *nix system
'png2swf' => 'png2swf', //default value for *nix system
'gif2swf' => 'gif2swf', //default value for *nix system
'jpeg2swf' => 'jpeg2swf', //default value for *nix system
'path_to_players' => null,
'livepath_to_players' => null,
'player' => 'default',
'allowed_domain' => null
);
/**
* Handle pdf2swf, jpeg2swf, png2swf, gif2swf functions
* @param mixed
* @return bool
*/
public function __call($func, $vars = array())
{
switch ($func) {
case 'pdf2swf':
if (is_array($vars[0])) {
$vars[0] = $vars[0][0];
} else {
$vars[0] = array($vars[0]);
}
return $this->convert($func, $vars[0], $vars[1], isset($vars[2]) ? $vars[2] : null, isset($vars[3]) ? $vars[3] : null);
break;
case 'png2swf':
case 'jpeg2swf':
case 'gif2swf':
if (!is_array($vars[0])) {
$vars[0] = array($vars[0]);
}
return $this->convert($func, $vars[0], $vars[1]);
break;
}
}
/**
* Reload base configurations
* @param array Example: array('param_name' => 'param_value')
* @return bool
*/
public function init($c = array())
{
if (empty($c)) {
$this->log('Configuration is empty.');
return false;
};
foreach ($c as $k => $v) {
if (!empty($v)) $this->config[$k] = $v;
}
return true;
}
/**
* Convert array of source files to swf document
*
* @param string Converter(pdf2swf, jpeg2swf, png2swf, gif2swf)
* @param array Files for convertation
* @param int Start page, for pdf only
* @param int End page, for pdf only
* @return bool
*/
private function convert($converter = null, $source_files = array(), $swf_file = null, $start_page = null, $end_page = null)
{
if (count($source_files) < 1) {
$this->log('No source files.');
return false;
}
$str_source_files = '';
foreach ($source_files as $source_file) {
if (is_file($source_file)) {
$str_source_files .= ' "'.$source_file.'" ';
} else {
$this->log('Wrong source file(s).');
return false;
}
}
$str = $this->config[$converter] . ' ' // pdf2swf command
. ((!is_null($start_page)&&!is_null($end_page)) ? '-p ' . (int)$start_page . '-' . (int)$end_page . ' ' : '') // pages range
. ' -o "' . $swf_file .'" ' // output swf-file
. $str_source_files; // source files
exec($str);
if (!is_file($swf_file)) {
$this->log('Convertation errors.');
return false;
}
return true;
}
/**
* Retrieve a html code with swf player.
*
* @param string Correct URL of swf file
*/
public function render($swf = null)
{
if (is_null($swf)) {
$this->log('Wrong swf-file.');
return false;
}
$c = $this->config;
//read template from file
$template = file_get_contents($this->config['path_to_players'] . DIRECTORY_SEPARATOR . $this->config['player'] . '.html');
//init some template vars
$c['livepath_to_player'] = $c['livepath_to_players'] . '/' . $c['player'] . '.swf';
$c['flash_vars'] = 'allowed_domain=' . urlencode($c['allowed_domain'])
. '&swf=' . urlencode($swf);
// render template vars, replace {{var}} by $c['var'], where $c = $config + (some vars)
if (!empty($c))
foreach ($c as $k => $v) {
$template = str_replace('{{'.$k.'}}', $v, $template);
}
return $template;
}
/**
* Base log function
*
* @param string Message
*/
protected function log($msg)
{
ob_start();
debug_print_backtrace ();
$trace .= ob_get_contents();
ob_end_clean();
$msg = $msg . "\n" . $trace;
error_log($msg, 0);
return $msg;
}
}
?>