php Swftools class class Swftools_Core default configuration protected

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?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;
}
}
?>