Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
image_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/files/file/file_edit_fns.inc';
19 
33 {
34 
35 
48  function paintFileUpload(&$asset, &$o, $prefix)
49  {
50  parent::paintFileUpload($asset, $o, $prefix);
51 
52  if ((!$asset->id) && file_exists(SQ_FUDGE_PATH.'/image_editor/imagej.jar')) {
53  // we are in the create screen, so offer the editor too unless we are on Safari Windows
54  echo '<p>';
55  normal_button($prefix.'_use_editor_button', translate('core_use_image_editor_button'), 'this.parentNode.parentNode.parentNode.style.display=\'none\'; document.getElementById(\''.$prefix.'_hide_editor_button\').parentNode.parentNode.style.display=\'\';', 'style=display:none;');
56  ?>
57  <script type="text/javascript">
58  if (!(navigator.userAgent.match(/Windows/i) && navigator.userAgent.match(/Safari/i))) {
59  document.getElementById('<?php echo $prefix; ?>_use_editor_button').style.display = 'inline';
60  }
61  </script>
62  <?php
63  echo '</p>';
64  }
65 
66  return TRUE;
67 
68  }//end paintFileUpload()
69 
70 
81  function paintEmbeddedData(&$asset, &$o, $prefix)
82  {
83  $embedded_data = $asset->attr('embedded_data');
84 
85  if (empty($embedded_data)) {
86  $o->note('Not found');
87  return;
88  }
89 
90  echo '<table class="sq-backend-table">';
91  echo '<tr><th>'.translate('name').'</th>';
92  echo '<th>'.translate('keyword').'</th>';
93  echo '<th>'.translate('value').'</th></tr>';
94  foreach ($embedded_data as $keyword => $value_set) {
95  $code = array_get_index($value_set, 'name');
96  echo '<tr>';
97  echo '<td>';
98  echo $code;
99  echo '</td>';
100  echo '<td>';
101  echo '%image_embedded_'.$keyword.'%';
102  echo '</td>';
103  echo '<td>';
104  foreach (array_get_index($value_set, 'value', Array()) as $data_item) {
105  echo $data_item;
106  echo '<br />';
107  }
108  echo '</td></tr>';
109  }
110  echo '</table>';
111 
112  }//end paintEmbeddedData()
113 
114 
136  function processFileUpload(&$asset, &$o, $prefix, $info=Array(),$redirect_in_limbo=TRUE, $asset_cloned=FALSE)
137  {
138  if (!parent::processFileUpload($asset, $o, $prefix, $info,$redirect_in_limbo, $asset_cloned)) {
139  return FALSE;
140  }
141 
142  // assetid will not be set if we are creating
143  if (!$asset->id) return TRUE;
144 
145  $info = $asset->getExistingFile();
146  if (!empty($info)) {
147  if (!$asset->setAttrValue('width', $info['width'])) {
148  return FALSE;
149  }
150  if (!$asset->setAttrValue('height', $info['height'])) {
151  return FALSE;
152  }
153  if (!$asset->setAttrValue('size', $info['size'])) {
154  return FALSE;
155  }
156  }//end if
157 
158  $this->_recreateVarietyImages($asset);
159 
160 
161  // extract Photoshop metadata
162  $meta = $this->_extractImageMetadata($info['path']);
163  if (!$asset->setAttrValue('embedded_data', $meta)) {
164  return FALSE;
165  }
166 
167  if (!$asset->saveAttributes()) {
168  trigger_localised_error('CORE0052', E_USER_WARNING);
169  return FALSE;
170  }
171 
172  return TRUE;
173 
174  }//end processFileUpload()
175 
176 
187  function paintCreateEditor(&$asset, &$o, $prefix)
188  {
189  if (!$asset->id) {
190  // we are in the create screen, so we'll print
191  normal_button($prefix.'_hide_editor_button', translate('core_use_simple_upload_button'), 'this.parentNode.parentNode.style.display=\'none\'; document.getElementById(\''.$prefix.'_use_editor_button\').parentNode.parentNode.parentNode.style.display=\'\';', 'style="margin-bottom: 5px"');
192  echo '<br />';
193  $this->_paintApplet($asset, $o, $prefix);
194  }
195  return TRUE;
196 
197  }//end paintCreateEditor()
198 
199 
210  function processCreateEditor(&$asset, &$o, $prefix)
211  {
212  if (isset($_POST[$prefix.'_applet_temp_filename']) && !empty($_POST[$prefix.'_applet_temp_filename'])) {
213  $_POST[$prefix.'_server_file'] = $_POST[$prefix.'_applet_temp_filename'];
214  // processFileChooser, called from File::create(), can now do the rest
215  }
216  return TRUE;
217 
218  }//end processCreateEditor()
219 
220 
232  function paintEditor(&$asset, &$o, $prefix)
233  {
234  if ($asset->writeAccess('attributes')) {
235  $this->_paintApplet($asset, $o, $prefix);
236  return TRUE;
237  } else {
238  $asset_url = $GLOBALS['SQ_SYSTEM']->am->getAssetURL($asset->id);
239  $root_url = $GLOBALS['SQ_SYSTEM']->am->getRootURL(strip_url($asset_url, TRUE));
240  ?>
241  <img src="<?php echo current_protocol().'://'.$root_url['url'].'?a='.$asset->id; ?>&t=<?php echo time(); ?>" />
242  <?php
243  return FALSE;
244  }
245 
246  }//end paintEditor()
247 
248 
260  function processEditor(&$asset, $o, $prefix)
261  {
262  if (isset($_POST[$prefix.'_applet_temp_filename'])) {
263  $_POST[$prefix.'_server_file'] = $_POST[$prefix.'_applet_temp_filename'];
264  // processServerFile can now do the rest
265  $this->processFileChooser($asset, $o, $prefix);
266  }
267  return TRUE;
268 
269  }//end processEditor()
270 
271 
282  function hasVarieties(&$asset, $prefix)
283  {
284  $varieties = $asset->attr('varieties');
285  return (!empty($varieties['data']));
286 
287  }//end hasVarieties()
288 
289 
301  function paintCurrentVarieties(&$asset, &$o, $prefix)
302  {
303  $wa = $asset->writeAccess('attributes');
304 
305  $varieties = $asset->attr('varieties');
306  if (!empty($varieties)) {
307  foreach ($varieties['data'] as $varid => $data) {
308  $variety = $asset->getAsset($asset->id.':'.$varid);
309  if (is_null($variety)) continue;
310  $edit_fns = $variety->getEditFns();
311 
312  $o->openSection($variety->name);
313  $edit_fns->paintInlineInterface($variety, $o, $prefix.'_'.$variety->id);
314  if ($wa) {
315  $o->openField(translate('delete_question'));
316  check_box($prefix.'_delete[]', $variety->id);
317  $o->closeField();
318  }
319  $o->closeSection();
320 
321  }// end foreach
322  }// endif
323 
324  return $wa;
325 
326  }//end paintCurrentVarieties()
327 
328 
340  function processCurrentVarieties(&$asset, $o, $prefix)
341  {
342  if (!$asset->writeAccess('attributes')) return FALSE;
343 
344  $changes_made = FALSE;
345 
346  if (!empty($_POST[$prefix.'_delete']) && is_array($_POST[$prefix.'_delete'])) {
347  foreach ($_POST[$prefix.'_delete'] as $assetid) {
348  list(, $varid) = explode(':', $assetid, 2);
349  if ($asset->deleteVariety($assetid)) {
350  $changes_made = TRUE;
351  }
352  }// end foreach
353  }// end if
354 
355  $varieties = $asset->attr('varieties');
356  if (!empty($varieties)) {
357  foreach ($varieties['data'] as $varid => $data) {
358  $variety = $asset->getAsset($asset->id.':'.$varid);
359  if (is_null($variety)) continue;
360  $edit_fns = $variety->getEditFns();
361  // If the interface has not been painted, and no file is uploaded for this variety, that means it is new,
362  // processNewVariety should already taken care of it
363  if (!isset($_FILES[$prefix.'_'.$variety->id]) && !isset($_POST[$prefix.'_'.$variety->id.'_variety_type_posted'])) continue;
364  if ($edit_fns->processInlineInterface($variety, $o, $prefix.'_'.$variety->id) && !($asset->status & SQ_SC_STATUS_SAFE_EDITING)) {
365  // update the public version with the new file if not in safe edit
366  if ($variety->checkFileState($asset->effectiveUnrestricted() && $asset->attr('allow_unrestricted'))) {
367  $changes_made = TRUE;
368  }
369  }
370  }// end foreach
371  }// end if
372 
373  return $changes_made;
374 
375  }//end processCurrentVarieties()
376 
377 
389  function paintNewVariety(&$asset, &$o, $prefix)
390  {
391  $wa = $asset->writeAccess('attributes');
392 
393  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image_variety');
394  $variety = new Image_Variety($asset->id.':v0');
395 
396  $edit_fns = $variety->getEditFns();
397 
398  $o->openSection(translate('new_variety'));
399  $edit_fns->paintInlineInterface($variety, $o, $prefix.'_new');
400  $o->closeSection();
401 
402  return $wa;
403 
404  }//end paintNewVariety()
405 
406 
418  function processNewVariety(&$asset, $o, $prefix)
419  {
420  if (!$asset->writeAccess('attributes')) return FALSE;
421  if (!isset($_POST[$prefix.'_new_name']) || trim($_POST[$prefix.'_new_name']) == '') {
422  return TRUE;
423  }
424  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image_variety');
425  $variety = new Image_Variety($asset->id.':v0');
426 
427  $edit_fns = $variety->getEditFns();
428  if (!$edit_fns->processInlineInterface($variety, $o, $prefix.'_new')) {
429  return FALSE;
430  }
431 
432  return (bool) $asset->saveVariety($variety);
433 
434  }//end processNewVariety()
435 
436 
437 //-- HELPERS --//
438 
439 
448  function printExistingFileInfo(&$asset)
449  {
450  parent::printExistingFileInfo($asset);
451  $existing = $asset->getExistingFile();
452  if (!empty($existing)) {
453  echo translate('core_dimensions', $existing['width'], $existing['height'])?><br/><?php
454  }
455 
456  }//end printExistingFileInfo()
457 
458 
470  function getChosenFileInfo($prefix)
471  {
472  $info = parent::getChosenFileInfo($prefix);
473  if (isset($_POST[$prefix.'_applet_filename']) && !empty($_POST[$prefix.'_applet_filename'])) {
474  $info['name'] = $_POST[$prefix.'_applet_filename'];
475  $info['tmp_name'] = SQ_TEMP_PATH.'/'.$_POST[$prefix.'_applet_temp_filename'];
476  }
477  return $info;
478 
479  }//end getChosenFileInfo()
480 
481 
493  function getPreUploadOptions(&$asset)
494  {
495  require_once(SQ_FUDGE_PATH.'/general/file_system.inc');
496  $options = Array();
497  foreach (list_files(SQ_TEMP_PATH) as $filename) {
498  if (in_array(get_file_type($filename), $asset->allowed_extensions)) {
499  $options[$filename] = $filename;
500  }
501  }
502  return $options;
503 
504  }//end getPreUploadOptions()
505 
506 
521  function _paintApplet(&$asset, &$o, $prefix)
522  {
523  if (!file_exists(SQ_FUDGE_PATH.'/image_editor/imagej.jar')) {
524  return FALSE;
525  }
526  $time = time();
527  $open_url = $asset->getURL();
528  if (empty($open_url)) {
529  $open_url = current_url(TRUE, TRUE).'?a='.$asset->id;
530  }
531  $open_url .= ((strpos($open_url, '?') === FALSE) ? '?' : '&').'SESSION_ID='.session_id().'&SESSION_KEY='.$GLOBALS['SQ_SYSTEM']->getUniqueSessionKey().'&t='.$time;
532  ?>
533 
534  <script type="text/javascript"><!--
535 
536  function getAppletName()
537  {
538  var applet = document.getElementById('<?php echo $prefix; ?>_image_editor');
539  if (applet.parentNode.parentNode.style.display != 'none') {
540  var tempNameFile = applet.getFilename();
541  if (tempNameFile.length < 5 ){
542  return false ;
543  }
544  return true;
545  } else {
546  return true;
547  }
548  }
549 
550  function getAppletResult()
551  {
552  var applet = document.getElementById('<?php echo $prefix; ?>_image_editor');
553  if (applet.parentNode.parentNode.style.display != 'none') {
554  var tempFilename = applet.doUpload();
555  if ((tempFilename !== null) && (tempFilename != '')) {
556  document.getElementById('<?php echo $prefix; ?>_applet_temp_filename').value = tempFilename;
557  document.getElementById('<?php echo $prefix; ?>_applet_filename').value = applet.getFilename();
558  return true;
559  } else {
560  return false;
561  }
562  }
563  return true;
564  }
565 
566  forms = document.getElementsByTagName('FORM');
567  if (forms[0].onsubmit) {
568  var otherOnSubmit = forms[0].onsubmit;
569  forms[0].onsubmit =
570  function()
571  {
572  if (getAppletName()) {
573  otherOnSubmit();
574  return getAppletResult();
575  } else {
576  window.alert("You must give a filename before committing");
577  }
578  }
579  } else {
580  forms[0].onsubmit = function() { return getAppletResult(); }
581  }
582  // -->
583  </script>
584 
585  &nbsp;
586  <input type="hidden" name="<?php echo $prefix; ?>_applet_temp_filename" id="<?php echo $prefix; ?>_applet_temp_filename" value="" />
587  <input type="hidden" name="<?php echo $prefix; ?>_applet_filename" id="<?php echo $prefix; ?>_applet_filename" value="" />
588 
589  <applet id="<?php echo $prefix; ?>_image_editor" name="<?php echo $prefix; ?>_image_editor" archive="<?php echo sq_web_path('fudge'); ?>/image_editor/imagej.jar?t=<?php echo $time; ?>" code="ij.ImageJ.class" width="90%" height="350">
590  <param name="SUBMIT_URL"
591  value="<?php echo sq_web_path('lib'); ?>/web/accept_file_upload.php?SESSION_ID=<?php echo session_id(); ?>&SESSION_KEY=<?php echo $GLOBALS['SQ_SYSTEM']->getUniqueSessionKey(); ?>" />
592  <param name="FILE_FIELD_NAME" value="<?php echo $prefix; ?>" />
593  <?php
594  if ($asset->id) {
595  ?>
596  <param name="OPEN_URL" value="<?php echo $open_url; ?>" />
597  <?php
598  }
599  ?>
600  </applet>
601 
602  <script type="text/javascript"><!--
603  // do not display applet for Safari Windows - problems exist currently with the JRE bundled with the browser, in particular the LiveConnect API
604  // ... And let Chrome through since it is OK and uses Web Kit
605  if (navigator.userAgent.match(/Windows/i) && navigator.userAgent.match(/Safari/i) && !navigator.userAgent.match(/Chrome/i)) {
606  var applet = document.getElementById('<?php echo $prefix; ?>_image_editor');
607  // cannot set display:none because Safari Windows will not respect this with an applet tag
608  applet.style.width = '0';
609  applet.style.height = '0';
610  var element = document.createElement('div');
611  element.innerHTML = js_translate('image_editor_disabled');
612  applet.parentNode.appendChild(element);
613  }
614  // -->
615  </script>
616  <?php
617  return TRUE;
618 
619  }//end _paintApplet()
620 
621 
631  function _recreateVarietyImages(&$asset)
632  {
633  $varieties = $asset->attr('varieties');
634  if (!empty($varieties)) {
635  foreach ($varieties['data'] as $varid => $data) {
636  $variety = $asset->getAsset($asset->id.':'.$varid);
637  if (is_null($variety)) continue;
638  $asset->saveVariety($variety, TRUE, FALSE);
639  }// end foreach
640  }// end if
641 
642  }//end _recreateVarietyImages()
643 
644 
653  function _extractImageMetadata($filename)
654  {
655  $meta = Array();
656 
657  if (!SQ_TOOL_JPEG_TOOLKIT_ENABLED) return $meta;
658 
659  if (!defined('META_TOOLKIT_DIR')) {
660  define ('META_TOOLKIT_DIR', SQ_TOOL_JPEG_TOOLKIT_PATH);
661  }
662 
663  if (!file_exists(META_TOOLKIT_DIR.'/JPEG.php')) {
664  trigger_error('JPEG Metadata Toolkit Path is invalid', E_USER_WARNING);
665  return $meta;
666  }
667 
668  include_once META_TOOLKIT_DIR.'/JPEG.php';
669  include_once META_TOOLKIT_DIR.'/JFIF.php';
670  include_once META_TOOLKIT_DIR.'/PictureInfo.php';
671  include_once META_TOOLKIT_DIR.'/XMP.php';
672  include_once META_TOOLKIT_DIR.'/Photoshop_IRB.php';
673  include_once META_TOOLKIT_DIR.'/EXIF.php';
674  include_once META_TOOLKIT_DIR.'/Photoshop_File_Info.php';
675 
676  // we start the buffer because the tool prints stuff to screen on error. nOObs
677  ob_start();
678  $jpeg_header_data = get_jpeg_header_data($filename);
679  $XMP_array = read_XMP_array_from_text( get_XMP_text( $jpeg_header_data ) );
680  ob_end_clean();
681 
682  if (empty($XMP_array)) return $meta;
683 
684  // the bit below is hardcoded to look for dc: prefix in tag name
685  foreach ($XMP_array as $xmp_item) {
686  if ($xmp_item['tag'] != 'x:xmpmeta') continue;
687  if (!isset($xmp_item['children'])) continue;
688 
689  foreach ($xmp_item['children'] as $xmp_meta_item) {
690  if ($xmp_meta_item['tag'] != 'rdf:RDF') continue;
691  if (!isset($xmp_meta_item['children'])) continue;
692 
693  foreach ($xmp_meta_item['children'] as $rdf_data) {
694  if ($rdf_data['tag'] == 'rdf:Description') {
695  if (!isset($rdf_data['children'])) continue;
696 
697  foreach ($rdf_data['children'] as $description_data) {
698  $tag_name = $description_data['tag'];
699  if (strpos($tag_name, 'dc:') !== 0) continue;
700 
701  if (!isset($description_data['children'])) continue;
702 
703  $ns = 'dc';
704  // 3 is the length of 'dc:'
705  $current_tag = substr($tag_name, 3);
706  $keyword = $ns.'--'.$current_tag;
707 
708  foreach ($description_data['children'] as $tag_value_list) {
709  if (!isset($tag_value_list['children'])) continue;
710 
711  $meta[$keyword]['name'] = $tag_name;
712  foreach ($tag_value_list['children'] as $tag_value) {
713  // sometimes value tag is missing in image header, checking first!
714  if (!isset($tag_value['value'])) {
715  $meta[$keyword]['value'][] = '';
716  } else {
717  $meta[$keyword]['value'][] = $tag_value['value'];
718  }
719  }
720  }
721  }
722 
723  }//end if
724 
725  }//end foreach
726 
727  }//end foreach
728 
729  }//end foreach
730 
731  return $meta;
732 
733  }//end _extractImageMetadata()
734 
735 
736 }//end class
737 
738 ?>