Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
flv_file.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/files/file/file.inc';
19 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
20 
21 
33 class FLV_File extends File
34 {
35 
36 
41  public $allowed_extensions = Array('flv');
42 
43 
50  function __construct($assetid=0)
51  {
52  $this->_ser_attrs = TRUE;
53  parent::__construct($assetid);
54 
55  //make asset_attribute_<metadata_name> keywords available
56  $metadata = $this->attr('attributes');
57  foreach ($metadata as $meta_name => $meta_val) {
58  $this->vars[$meta_name] = Array('value' => $meta_val, 'type' => 'text', 'attrid' => 0);
59  }
60 
61  }//end constructor
62 
63 
78  public function getAvailableKeywords()
79  {
80  $keywords = parent::getAvailableKeywords();
81 
82  $keywords['asset_attribute_duration_string'] = 'Duration of the FLV file (in Minutes:Seconds)';
83 
84  $keywords['asset_attribute_duration_int'] = 'Rounded duration of the FLV file in seconds';
85  $keywords['asset_attribute_width_int'] = 'The rounded width of the video in pixels';
86  $keywords['asset_attribute_height_int'] = 'The rounded height of the video in pixels';
87  $keywords['asset_attribute_framerate_int'] = 'Rounded number of frames per second';
88  $keywords['asset_attribute_videodatarate_int'] = 'Rounded video bit rate in kilobits per second';
89  $keywords['asset_attribute_videocodecid_int'] = 'The rounded video codec ID used in the file';
90  $keywords['asset_attribute_audiodatarate_int'] = 'Rounded audio bit rate in kilobits per second';
91  $keywords['asset_attribute_audiocodecid_int'] = 'The rounded audio codec ID used in the file';
92  $keywords['asset_attribute_audiosamplerate_int'] = 'The rounded frequency at which the audio stream is replayed';
93  $keywords['asset_attribute_audiosamplesize_int'] = 'The rounded resolution of a single audio sample';
94 
95  $keywords['asset_attribute_XXX'] = 'Generic FLV metadata keyword';
96  $keywords['asset_attribute_XXX_int'] = 'Generic rounded FLV metadata keyword';
97 
98  return $keywords;
99 
100  }// end getAvailableKeywords()
101 
102 
117  public function getKeywordReplacement($keyword)
118  {
119  $replacement = NULL;
120 
121  // Remove any modifiers from keyword
122  $full_keyword = $keyword;
123  $keyword = parse_keyword($keyword, $modifiers);
124  $contextid = extract_context_modifier($modifiers);
125 
126  if ($contextid !== NULL) {
127  // If we were able to extract a context ID to change to, and it's
128  // different to our current one, then change and then reload a copy
129  // of the asset in that context (as we would need to do anyway)
130 
131  if ((int)$contextid !== $GLOBALS['SQ_SYSTEM']->getContextId()) {
132  $GLOBALS['SQ_SYSTEM']->changeContext($contextid);
133  $contexted_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->id);
134 
135  // Get the keyword without any modifiers
136  $replacement = $contexted_asset->getKeywordReplacement($keyword);
137 
138  // Then apply the ones we had remaining, then return it
139  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $contexted_asset->id));
140 
141  unset($contexted_asset);
142  $GLOBALS['SQ_SYSTEM']->restoreContext();
143  return $replacement;
144 
145  }//end if contextid is not the currently active context
146 
147  }//end if contextid is not NULL
148 
149  //only process keywords that start with asset_attribute_
150  if ('asset_attribute_' == substr($keyword, 0, 16)) {
151  if ($keyword == 'asset_attribute_duration_string') {
152  //want a duration string Minutes:Seconds
153  $replacement = self::convertTimeToString($this->attr('duration'));
154 
155  } else {
156  $is_int = FALSE;
157  if ('_int' == substr($keyword, -4)) {
158  $keyword = substr($keyword, 0, -4);
159  $is_int = TRUE;
160  }
161  //get the replacement for keyword from parent asset
162  $replacement = parent::getKeywordReplacement($keyword);
163 
164  if (is_numeric($replacement)) { //the replacement is a number
165  //if the keyword ends with _int, round it
166  if ($is_int) {
167  $replacement = (int)(round($replacement));
168  }
169 
170  } else { //the replacement is a string
171  //replace HTML special characters so that malicious users can not use FLV metadata to make a XSS attack
172  $replacement = nl2br(htmlspecialchars($replacement));
173  }
174  }
175  }
176 
177  if ($replacement !== NULL) {
178  if (count($modifiers) > 0) {
179  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $this->id));
180  }
181  } else {
182  // use full keyword so the modifiers still get used
183  $replacement = parent::getKeywordReplacement($full_keyword);
184  }
185 
186  return $replacement;
187 
188  }//end getKeywordReplacement()
189 
190 
205  public function saveAttributes($dont_run_updated=FALSE)
206  {
207  $stored_metadata = Array(
208  'duration' => Array('attr_name' => 'duration', 'default' => 0),
209  'width' => Array('attr_name' => 'width', 'default' => 0),
210  'height' => Array('attr_name' => 'height', 'default' => 0),
211  'framerate' => Array('attr_name' => 'framerate', 'default' => 0),
212  'videodatarate' => Array('attr_name' => 'videodatarate', 'default' => 0),
213  'videocodecid' => Array('attr_name' => 'videocodecid', 'default' => 0),
214  'audiodatarate' => Array('attr_name' => 'audiodatarate', 'default' => 0),
215  'audiocodecid' => Array('attr_name' => 'audiocodecid', 'default' => 0),
216  'audiosamplerate' => Array('attr_name' => 'audiosamplerate', 'default' => 0),
217  'audiosamplesize' => Array('attr_name' => 'audiosamplesize', 'default' => 0),
218  );
219 
220  //if the extract metadata attribute is set, set all the metadata attributes to new values from the FLV metadata
221  if ($this->attr('extract_metadata')) {
222  $metadata = $this->extractMetadata();
223  if ($metadata !== FALSE) {
224  //put all the metadata specified in the _store_metadata array to the corresponding attributes
225  foreach ($stored_metadata as $meta_name => $attr) {
226  if (array_key_exists($meta_name, $metadata)) {
227  $this->setAttrValue($attr['attr_name'], $metadata[$meta_name]);
228  unset($metadata[$meta_name]);
229  } else {
230  $this->setAttrValue($attr['attr_name'], $attr['default']);
231  }
232  }
233  //save the rest of metadata to the attribute "attributes"
234  $this->setAttrValue('attributes', $metadata);
235  }
236 
237  //set the extract metadata attribute to FALSE again so that the default value of extract metadata checkbox is unchecked
238  $this->setAttrValue('extract_metadata', FALSE);
239  }
240 
241  return parent::saveAttributes($dont_run_updated);
242 
243  }//end saveAttributes()
244 
245 
256  public function extractMetadata()
257  {
258  //check if getID3() is enabled
259  if (!SQ_TOOL_GETID3_ENABLED || !is_file(SQ_TOOL_GETID3_PATH.'/getid3.php')) {
260  return FALSE;
261  }
262 
263  require_once SQ_TOOL_GETID3_PATH.'/getid3.php';
264 
265  //get file info
266  $file_info = $this->getExistingFile();
267  if (empty($file_info)) {
268  return FALSE;
269  }
270 
271  $getID3 = new getID3();
272  $file_info = $getID3->analyze($file_info['path']);
273 
274  $metadata = Array();
275 
276  if (isset($file_info['flv']['meta']['onMetaData'])) {
277  foreach ($file_info['flv']['meta']['onMetaData'] as $key => $value) {
278  $metadata[$key] = $value;
279  }
280  }
281 
282  //if duration metadata does not exist, use playtime_seconds instead
283  if (!array_key_exists('duration', $metadata) && isset($file_info['playtime_seconds'])) {
284  $metadata['duration'] = $file_info['playtime_seconds'];
285  }
286 
287  //if there are no width or height metadata, try to use video resolution instead
288  if (!array_key_exists('width', $metadata) && isset($file_info['video']['resolution_x'])) {
289  $metadata['width'] = $file_info['video']['resolution_x'];
290  }
291  if (!array_key_exists('height', $metadata) && isset($file_info['video']['resolution_y'])) {
292  $metadata['height'] = $file_info['video']['resolution_y'];
293  }
294 
295  //if framerate metadata does not exist, use video frame rate instead
296  if (!array_key_exists('framerate', $metadata) && isset($file_info['video']['frame_rate'])) {
297  $metadata['framerate'] = $file_info['video']['frame_rate'];
298  }
299 
300  return $metadata;
301 
302  }//end extractMetadata()
303 
304 
312  public static function convertTimeToString($time)
313  {
314  $minutes = floor($time / 60);
315  $seconds = floor($time % 60);
316 
317  return $minutes.':'.str_pad($seconds, 2, '0', STR_PAD_LEFT);
318 
319  }//end convertTimeToString
320 
321 
322 }//end class
323 
324 ?>