/**
	 * Fetches short title from an iso code
	 *
	 * @param	string		table name
	 * @param	string		iso code
	 * @param	string		language code - if not set current default language is used
	 * @param	boolean		local name only - if set local title is returned
	 * @return	string		short title
	 */
	function getTitleFromIsoCode ($table, $isoCode, $lang='', $local=FALSE) {
		global $TSFE, $TYPO3_DB;

		$title = '';
		$titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang, $local);
		if (count ($titleFields))	{
			$prefixedTitleFields = array();
			foreach ($titleFields as $titleField) {
				$prefixedTitleFields[] = $table.'.'.$titleField;
			}
			$fields = implode(',', $prefixedTitleFields);
			$whereClause = '1=1';
			if (!is_array($isoCode)) {
				$isoCode = array($isoCode);
			}
			$index = 0;
			foreach ($isoCode as $index => $code) {
				if ($code != '')	{
					$tmpField = tx_staticinfotables_div::getIsoCodeField($table, $code, TRUE, $index);
					$tmpValue = $TYPO3_DB->fullQuoteStr($code,$table);
					if ($tmpField && $tmpValue)	{
						$whereClause .= ' AND ' . $table . '.' . $tmpField . ' = ' . $tmpValue;
					}
				}
			}
			if (is_object($TSFE)) {
				$enableFields = $TSFE->sys_page->enableFields($table);
			} else {
				$enableFields = t3lib_BEfunc::deleteClause($table);
			}

			$res = $TYPO3_DB->exec_SELECTquery(
				$fields,
				$table,
				$whereClause.$enableFields
			);
			if ($row = $TYPO3_DB->sql_fetch_assoc($res))	{
				foreach ($titleFields as $titleField) {
					if ($row[$titleField]) {
						$title = $row[$titleField];
						break;
					}
				}
			}
			$TYPO3_DB->sql_free_result($res);
		}

		return $title;
	}
 /**
  * performs the service processing
  *
  * @param	string 	Content which should be processed.
  * @param	string 	Content type
  * @param	array 	Configuration array
  * @return	boolean
  */
 function process($content = '', $type = '', $conf = array())
 {
     $this->conf = $conf;
     $this->out = array();
     $this->out['fields'] = array();
     $this->exif = array();
     if ($content) {
         $this->setInput($content, $type);
     }
     if ($inputFile = $this->getInputFile()) {
         $cmd = t3lib_exec::getCommand($this->info['exec']) . ' -q -m -g -S ' . escapeshellarg($inputFile);
         $exif = $ret = NULL;
         exec($cmd, $exif, $ret);
         if (!$ret and is_array($exif)) {
             $section = 'EXIF';
             array_shift($exif);
             // ---- ExifTool ----
             array_shift($exif);
             // ExifToolVersion                 : 6.57
             foreach ($exif as $line) {
                 if (preg_match('#---- ([^ ]*) ----#', $line, $matches)) {
                     $section = $matches[1];
                 }
                 // Only request 2 elements because exiftool output contains colons in $value
                 list($name, $value) = explode(':', $line, 2);
                 $name = trim($name);
                 $value = trim($value);
                 if ($value) {
                     // ignore empty lines headers and empty entries
                     $name = str_replace('-', '', $name);
                     $name = str_replace(' ', '', $name);
                     // add to exif table
                     $this->exif[$section][$name] = $value;
                     // add to DAM table
                     switch ($name) {
                         case 'CameraModel':
                             $this->out['fields']['file_creator'] = $value;
                             break;
                         case 'XResolution':
                         case 'HorizontalResolution':
                             $this->out['fields']['hres'] = intval($value);
                             break;
                         case 'YResolution':
                         case 'VerticalResolution':
                             $this->out['fields']['vres'] = intval($value);
                             break;
                         case 'Title':
                         case 'Headline':
                         case 'XPTitle':
                             $this->out['fields']['title'] = $value;
                             break;
                         case 'Keywords':
                         case 'XPKeywords':
                             $this->out['fields']['keywords'] = $value;
                             break;
                         case 'Subject':
                         case 'ImageDescription':
                         case 'Description':
                             $this->out['fields']['description'] = $value;
                             break;
                         case 'CaptionAbstract':
                             $this->out['fields']['caption'] = $value;
                             break;
                         case 'ModifyDate':
                             $this->out['fields']['date_mod'] = tx_svmetaextract_lib::parseDate($value);
                             $this->out['fields']['file_mtime'] = tx_svmetaextract_lib::parseDate($value);
                             break;
                         case 'ImageCreated':
                         case 'CreateDate':
                         case 'DateTimeOriginal':
                             $this->out['fields']['date_cr'] = tx_svmetaextract_lib::parseDate($value);
                             $this->out['fields']['file_ctime'] = tx_svmetaextract_lib::parseDate($value);
                             break;
                         case 'CreatorTool':
                         case 'Software':
                             $this->out['fields']['file_creator'] = $value;
                             break;
                         case 'City':
                             $this->out['fields']['loc_city'] = $value;
                             break;
                         case 'Country':
                             // TODO format?
                             # $this->out['fields']['loc_country'] = $value;
                             break;
                         case 'CountryCode':
                             if (strlen($value) == 2) {
                                 $isoCodeField = tx_staticinfotables_div::getIsoCodeField('static_countries', $value);
                                 $enableFields = t3lib_BEfunc::deleteClause('static_countries');
                                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('cn_iso_3', 'static_countries', $isoCodeField . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($value), 'static_countries') . $enableFields);
                                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                                     $this->out['fields']['loc_country'] = $row['cn_iso_3'];
                                 }
                             }
                             break;
                         case 'Artist':
                         case 'Creator':
                             $this->out['fields']['creator'] = $value;
                             break;
                         case 'Copyright':
                         case 'CopyrightNotice':
                             $this->out['fields']['copyright'] = $value;
                             break;
                         case 'Rights':
                             $this->out['fields']['copyright'] = $value;
                             break;
                         case 'RightsUsageTerms':
                         case 'UsageTerms':
                             $this->out['fields']['instructions'] = $value;
                             break;
                         case 'Credit':
                             $this->out['fields']['copyright'] = $value;
                             break;
                         case 'Instructions':
                             $this->out['fields']['instructions'] = $value;
                             break;
                     }
                 }
             }
         }
         // TODO read XMP XML
         // $this->out['fields']['meta']['XMP_XML'] = $this->xmpRaw;
         unset($this->exif['File']);
         unset($this->exif['Composite']);
         $this->postProcess();
         $this->out['fields']['meta'] = $this->exif;
         $this->out['exif_done'] = true;
         $this->out['iptc_done'] = true;
         $this->out['xmp_done'] = true;
     } else {
         $this->errorPush(T3_ERR_SV_NO_INPUT, 'No or empty input.');
     }
     return $this->getLastError();
 }
 /**
  * performs the service processing
  *
  * @param	string		Content which should be processed.
  * @param	string		Content type
  * @param	array		Configuration array
  * @return	boolean
  */
 function process($content = '', $type = '', $conf = array())
 {
     $this->conf = $conf;
     $this->out = array();
     if ($content) {
         $this->setInput($content, $type);
     }
     if ($inputFile = $this->getInputFile() and $jpeg_header_data = get_jpeg_header_data($inputFile)) {
         $this->xmpRaw = get_XMP_text($jpeg_header_data);
         preg_match('#<x:xmpmeta[^>]*>.*</x:xmpmeta>#is', $this->xmpRaw, $match);
         $this->xmpRaw = $match[0];
         $this->xmp = parseXMP2simpleArray(read_XMP_array_from_text($this->xmpRaw));
         foreach ($this->xmp as $key => $value) {
             // ignore empty lines headers and emtpy entries
             if ($value) {
                 switch (strtolower($key)) {
                     case 'dc:creator':
                         $this->out['fields']['creator'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'dc:description':
                         $this->out['fields']['description'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'dc:rights':
                         $this->out['fields']['copyright'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'dc:subject':
                         $this->out['fields']['keywords'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'dc:title':
                         $this->out['fields']['title'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'iptc4xmpcore:countrycode':
                         if (strlen($value) == 2) {
                             $select = 'cn_iso_3';
                             $table = 'static_countries';
                             $where = tx_staticinfotables_div::getIsoCodeField($table, $value) . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr(strtoupper($value), 'static_countries');
                             $where .= t3lib_BEfunc::deleteClause($table);
                             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select, $table, $where);
                             if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                                 $this->out['fields']['loc_country'] = $row['cn_iso_3'];
                             }
                         }
                         break;
                     case 'iptc4xmpcore:location':
                         $value = trim($value);
                         if ($value[0] == '/' or $value[0] == '\\') {
                             $this->out['fields']['file_orig_location'] = tx_svmetaextract_lib::forceUtf8($value);
                         } else {
                             $this->out['fields']['loc_desc'] = tx_svmetaextract_lib::forceUtf8($value);
                         }
                         break;
                     case 'xmp:createdate':
                         $this->out['fields']['date_cr'] = tx_svmetaextract_lib::parseDate($value);
                         break;
                     case 'xmp:creatortool':
                         $this->out['fields']['file_creator'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'xmp:modifydate':
                         $this->out['fields']['date_mod'] = tx_svmetaextract_lib::parseDate($value);
                         break;
                     case 'xap:createdate':
                         $this->out['fields']['date_cr'] = tx_svmetaextract_lib::parseDate($value);
                         break;
                     case 'xap:creatortool':
                         $this->out['fields']['file_creator'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'xap:modifydate':
                         $this->out['fields']['date_mod'] = tx_svmetaextract_lib::parseDate($value);
                         break;
                     case 'xaprights:copyright':
                         $this->out['fields']['copyright'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'xaprights:usageterms':
                         $this->out['fields']['instructions'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'xmptpg:npages':
                         $this->out['fields']['pages'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'pdf:keywords':
                         $this->out['fields']['keywords'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'pdf:producer':
                         $this->out['fields']['file_creator'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'photoshop:captionwriter':
                         $this->out['fields']['photoshop:captionwriter'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'photoshop:city':
                         $this->out['fields']['loc_city'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'photoshop:credit':
                         $this->out['fields']['copyright'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'photoshop:headline':
                         $this->out['fields']['title'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                     case 'photoshop:instructions':
                         $this->out['fields']['instructions'] = tx_svmetaextract_lib::forceUtf8($value);
                         break;
                 }
             }
         }
         $this->out['fields']['meta']['xmp'] = $this->xmp;
         $this->out['fields']['meta']['xmp_xml'] = $this->xmpRaw;
     } else {
         $this->errorPush(T3_ERR_SV_NO_INPUT, 'No or empty input.');
     }
     return $this->getLastError();
 }