/** * Creates an instance of this class. It obtains a path to the OpenSSL * binary. * * @return void */ public function __construct() { $this->opensslPath = t3lib_exec::getCommand('openssl'); $this->temporaryDirectory = PATH_site . 'typo3temp'; // Get temporary directory from the configuration $extconf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['rsaauth']); if ($extconf['temporaryDirectory'] != '' && $extconf['temporaryDirectory'][0] == '/' && @is_dir($extconf['temporaryDirectory']) && is_writable($extconf['temporaryDirectory'])) { $this->temporaryDirectory = $extconf['temporaryDirectory']; } }
/** * 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']) . ' ' . escapeshellarg($inputFile); $exif = $ret = NULL; exec($cmd, $exif, $ret); if (!$ret and is_array($exif)) { foreach ($exif as $line) { list($name, $value) = t3lib_div::trimExplode(':', $line); if ($value) { // ignore empty lines headers and emtpy entries $name = str_replace('-', '', $name); $name = str_replace(' ', '', $name); // add to exif table $this->exif[$name] = $value; // add to DAM table switch ($name) { case 'CameraModel': $this->out['fields']['file_creator'] = $value; break; case 'ImageCreated': $this->out['fields']['date_cr'] = tx_svmetaextract_lib::parseDate($value); break; case 'HorizontalResolution': $this->out['fields']['hres'] = intval($value); break; case 'VerticalResolution': $this->out['fields']['vres'] = intval($value); break; case 'ColorSpaceInformation': $this->out['fields']['color_space'] = $value; break; case 'Title': $this->out['fields']['title'] = $value; break; } } } } $this->postProcess(); $this->out['fields']['meta']['EXIF'] = $this->exif; } 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(); $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(); }
/** * Calculates a hash value from a file. * The hash is used to identify file changes or a file itself. * Remember that a file can occur multiple times in the file system, therefore you can detect only that it is the same file. But you have to take the location (path) into account to identify the right file. * * @param mixed $fileInfo Is a file path or an array containing a file info from tx_dam::file_compileInfo(). * @return string hash value */ function file_calcHash($fileInfo) { $hash = false; $filename = tx_dam::file_absolutePath($fileInfo); if (function_exists('md5_file')) { $hash = @md5_file($filename); } else { if (filesize($filename) > 0xfffff) { // 1MB $cmd = t3lib_exec::getCommand('md5sum'); $output = array(); $retval = 0; exec($cmd . ' -b ' . escapeshellcmd($filename), $output, $retval); $output = explode(' ', $output[0]); $match = array(); if (preg_match('#[0-9a-f]{32}#', $output[0], $match)) { $hash = $match[0]; } } else { $file_string = t3lib_div::getUrl($filename); $hash = md5($file_string); } } return $hash; }
/** * tx_dam::file_calcHash() */ public function test_file_calcHash() { $filename = $this->getFixtureFilename(); $hash = array(); $hash['file_calcHash'] = tx_dam::file_calcHash($filename); $compareHash = '4e231415019b6593f0266b99b7704bc2'; if (function_exists('md5_file')) { $hash['md5_file'] = @md5_file($filename); } $cmd = t3lib_exec::getCommand('md5sum'); $output = array(); $retval = ''; exec($cmd . ' -b ' . escapeshellcmd($filename), $output, $retval); $output = explode(' ', $output[0]); $match = array(); if (preg_match('#[0-9a-f]{32}#', $output[0], $match)) { $hash['md5sum'] = $match[0]; } $file_string = t3lib_div::getUrl($filename); $hash['md5'] = md5($file_string); foreach ($hash as $key => $value) { self::assertEquals($compareHash, $value, 'Wrong hash: ' . $value . ' (' . $key . ')'); } }
/** * get the mime type of a file with full path * * @param string $pathname absolute path to file * @return array file information */ function getFileMimeType($pathname) { // this will be called from tx_dam therefore $pathname can be a fileInfo array $pathname = tx_dam::file_absolutePath($pathname); $TX_DAM = $GLOBALS['T3_VAR']['ext']['dam']; $mimeType = array(); $mimeType['fulltype'] = ''; $mimeType['file_mime_type'] = ''; $mimeType['file_mime_subtype'] = ''; $mimeType['file_type'] = ''; $path_parts = t3lib_div::split_fileref($pathname); $mimeType['file_type'] = strtolower($path_parts['realFileext']); // cleanup bakup files extension $mimeType['file_type'] = preg_replace('#\\~$#', '', $mimeType['file_type']); $this->setup['useInternalMimeList'] = tx_dam::config_checkValueEnabled('setup.indexing.useInternalMimeList', true); $this->setup['useMimeContentType'] = tx_dam::config_checkValueEnabled('setup.indexing.useMimeContentType', true); $this->setup['useFileCommand'] = tx_dam::config_checkValueEnabled('setup.indexing.useFileCommand', true); // Get the mimetype info from the DB $file_type = tx_dam_db::getMediaExtension($mimeType['file_type']); // try first to get the mime type by extension with own array // I made the experience that it is a bit safer than with 'file' if ($this->setup['useInternalMimeList'] and $mimeType['file_type'] and isset($file_type['mime'])) { $mt = $file_type['mime']; if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used builtin conversion table', 'tx_dam_indexing'); } // next try } elseif ($this->setup['useMimeContentType'] and function_exists('mime_content_type')) { // available in PHP 4.3.0 $mt = mime_content_type($pathname); if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used mime_content_type()', 'tx_dam_indexing'); } } // last chance if ($this->setup['useFileCommand'] and (!$mt or $mt === 'application/octet-stream')) { $osType = TYPO3_OS; if (!($osType === 'WIN')) { if ($cmd = t3lib_exec::getCommand('file')) { $dummy = array(); $ret = false; $mimeTypeTxt = trim(exec($cmd . ' --mime ' . escapeshellarg($pathname), $dummy, $ret)); if (!$ret and strstr($mimeTypeTxt, tx_dam::file_basename($pathname) . ':')) { $a = explode(':', $mimeTypeTxt); $a = explode(';', trim($a[1])); //a[1]: text/plain, English; charset=iso-8859-1 $a = explode(',', trim($a[0])); $a = explode(' ', trim($a[0])); $mt = trim($a[0]); } } } if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType(): used t3lib_exec::getCommand(\'file\')', 'tx_dam_indexing'); } } $mtarr = explode('/', $mt); if (is_array($mtarr) && count($mtarr) == 2) { $mimeType['fulltype'] = $mt; $mimeType['file_mime_type'] = $mtarr[0]; $mimeType['file_mime_subtype'] = $mtarr[1]; } if ($mimeType['file_type'] == '') { $file_type = tx_dam_db::getMediaExtension('', $mimeType['fulltype']); $mimeType['file_type'] = $file_type['mime']; } if ($this->writeDevLog) { t3lib_div::devLog('getFileMimeType()', 'tx_dam_indexing', 0, $mimeType); } unset($mimeType['fulltype']); return $mimeType; }
/** * Extracts PDF metadata using 'pdfinfo' * 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->out = array(); if ($content) { $this->setInput($content, $type); } if ($inputFile = $this->getInputFile()) { $cmd = t3lib_exec::getCommand($this->info['exec']) . ' "' . $inputFile . '"'; $pdfmeta = ''; $ret = ''; exec($cmd, $pdfmeta, $ret); if (!$ret and is_array($pdfmeta)) { foreach ($pdfmeta as $line) { // Only request 2 elements because pdfinfo output might contain colons in $value list($name, $value) = explode(':', $line, 2); $name = trim($name); $value = trim($value); if ($value) { // ignore empty lines headers and empty entries switch ($name) { case 'Author': $this->out['fields']['creator'] = tx_svmetaextract_lib::forceUtf8($value); break; case 'Producer': $this->out['fields']['file_creator'] = tx_svmetaextract_lib::forceUtf8($value); break; case 'Title': $this->out['fields']['title'] = tx_svmetaextract_lib::forceUtf8($value); break; case 'Subject': $this->out['fields']['description'] = tx_svmetaextract_lib::forceUtf8($value); break; case 'Keywords': $this->out['fields']['keywords'] = tx_svmetaextract_lib::forceUtf8($value); break; case 'CreationDate': $this->out['fields']['date_cr'] = tx_svmetaextract_lib::parseDate($value); $this->out['fields']['file_ctime'] = tx_svmetaextract_lib::parseDate($value); break; case 'ModDate': $this->out['fields']['date_mod'] = tx_svmetaextract_lib::parseDate($value); $this->out['fields']['file_mtime'] = tx_svmetaextract_lib::parseDate($value); break; case 'Pages': case 'PageCount': $this->out['fields']['pages'] = intval($value); break; case 'Page Size': case 'Page size': // 595 x 842 pts (A4) $v = explode(' ', $value); $unitFrom = $v[3]; // TODO: create TCA to let user choose imperial/metric unit $unitTo = 'cm'; $this->out['fields']['width'] = (double) tx_svmetaextract_lib::convertUnit($v[0], $unitFrom, $unitTo); $this->out['fields']['height'] = (double) tx_svmetaextract_lib::convertUnit($v[2], $unitFrom, $unitTo); $this->out['fields']['width_unit'] = $unitTo; $this->out['fields']['height_unit'] = $unitTo; break; case 'PDF version': $this->out['fields']['file_type_version'] = $value; break; } } } } } else { $this->errorPush(T3_ERR_SV_NO_INPUT, 'No or empty input.'); } return $this->getLastError(); }