public function dispatchLoopShutdown()
 {
     //
     // Force output to be sent - we need the client to have the page before
     // we start flushing progress bar updates
     //
     $app = AppController::getInstance();
     $req = $app->getRequest();
     $resp = $app->getResponse();
     $resp->sendResponse();
     $resp->clearContent();
     //
     // Do export
     //
     if ($req->isLoggedIn()) {
         set_time_limit(3600 * 24);
         // if it takes more than 24 hours we're in trouble
         $vn_id = $req->getParameter('exporter_id', pInteger);
         $vs_search = $req->getParameter('search', pString);
         $t_exporter = new ca_data_exporters($vn_id);
         $vs_file = tempnam(caGetTempDirPath(), 'export');
         ca_data_exporters::exportRecordsFromSearchExpression($t_exporter->get('exporter_code'), $vs_search, $vs_file, array('request' => $req, 'progressCallback' => 'caIncrementBatchMetadataExportProgress'));
     }
     // export done, move file to application tmp dir and create download link (separate action in the export controller)
     if (filesize($vs_file)) {
         $vs_new_filename = $vn_id . "_" . md5($vs_file);
         rename($vs_file, __CA_APP_DIR__ . '/tmp/' . $vs_new_filename);
         caExportAddDownloadLink($req, $vs_new_filename);
     }
 }
Beispiel #2
0
 /**
  * Quickly loads list of row_ids in $pa_hits into a temporary database table uniquely identified by $ps_key
  * Only one temporary table can exist at a time for a given instance. If you call loadListIntoTemporaryResultTable() 
  * on an instance for which a temporary table has already been loaded the previously created table will be discarded
  * before the new one is created.
  *
  * @param array $pa_hits Array of row_id values
  * @param string $ps_key Unique alphanumeric identifier for the temporary table. Should only contain letters, numbers and underscores.
  * @return string The name of the temporary table created
  */
 public function loadListIntoTemporaryResultTable($pa_hits, $ps_key)
 {
     global $g_mysql_has_file_priv;
     $ps_key = preg_replace('![^A-Za-z0-9_]+!', '_', $ps_key);
     if ($this->ops_tmp_table_name == "caResultTmp{$ps_key}") {
         return $this->ops_tmp_table_name;
     }
     if ($this->ops_tmp_file_path) {
         $this->cleanupTemporaryResultTable();
     }
     $this->ops_tmp_file_path = tempnam(caGetTempDirPath(), 'caResultTmp');
     $this->ops_tmp_table_name = "caResultTmp{$ps_key}";
     $this->opo_db->query("\n\t\t\t\tCREATE TEMPORARY TABLE {$this->ops_tmp_table_name} (\n\t\t\t\t\trow_id int unsigned not null,\n\t\t\t\t\tkey (row_id)\n\t\t\t\t) engine=memory;\n\t\t\t");
     if (!sizeof($pa_hits)) {
         return $this->ops_tmp_table_name;
     }
     if (is_null($g_mysql_has_file_priv)) {
         // Figure out if user has FILE priv
         $qr_grants = $this->opo_db->query("\n\t\t\t\t\tSHOW GRANTS;\n\t\t\t\t");
         $g_mysql_has_file_priv = false;
         while ($qr_grants->nextRow()) {
             $va_grants = array_values($qr_grants->getRow());
             $vs_grant = array_shift($va_grants);
             if (preg_match('!^GRANT FILE!', $vs_grant)) {
                 $g_mysql_has_file_priv = true;
                 break;
             }
         }
     }
     if ($g_mysql_has_file_priv === true) {
         // Benchmarking has show that using "LOAD DATA INFILE" with an on-disk tmp file performs best
         // The downside is that it requires the MySQL global FILE priv, which often is not granted, especially in shared environments
         file_put_contents($this->ops_tmp_file_path, join("\n", $pa_hits));
         chmod($this->ops_tmp_file_path, 0755);
         $this->opo_db->query("LOAD DATA INFILE '{$this->ops_tmp_file_path}' INTO TABLE {$this->ops_tmp_table_name} (row_id)");
     } else {
         // Fallback when database login does not have FILE priv
         $vs_sql = "INSERT IGNORE INTO {$this->ops_tmp_table_name} (row_id) VALUES ";
         foreach ($pa_hits as $vn_row_id) {
             $vs_sql .= "(" . (int) $vn_row_id . "),";
         }
         $this->opo_db->query(substr($vs_sql, 0, strlen($vs_sql) - 1));
     }
     return $this->ops_tmp_table_name;
 }
Beispiel #3
0
 /**
  * @return Guzzle\Http\Client
  */
 public function getClient()
 {
     if (!isset($this->o_client)) {
         $this->o_client = new \Guzzle\Http\Client(self::NSL_SERVICES_URL, array('request.params' => array('cache.override_ttl' => 3600, 'params.cache.revalidate' => 'skip')));
         // can_cache needs to be callable
         $this->o_client->addSubscriber(new CachePlugin(array('storage' => new DefaultCacheStorage(new DoctrineCacheAdapter(new FilesystemCache(caGetTempDirPath()))), 'can_cache' => function () {
             // let's just cache for the above ttl
             return true;
         })));
         $o_conf = Configuration::load();
         if ($vs_proxy = $o_conf->get('web_services_proxy_url')) {
             /* proxy server is configured */
             $vo_config = $this->o_client->getConfig()->add('proxy', $vs_proxy);
             if (($vs_proxy_user = $o_conf->get('web_services_proxy_auth_user')) && ($vs_proxy_pass = $o_conf->get('web_services_proxy_auth_pw'))) {
                 $vo_config->add('curl.options', array(CURLOPT_PROXYUSERPWD => "{$vs_proxy_user}:{$vs_proxy_pass}"));
             }
         }
     }
     return $this->o_client;
 }
 /**
  * Handle ajax media uploads from editor
  */
 public function UploadFiles($pa_options = null)
 {
     if (!$this->request->isLoggedIn() || (int) $this->request->user->get('userclass') !== 0) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/2320?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     // use configured directory to dump media with fallback to standard tmp directory
     if (!is_writeable($vs_tmp_directory = $this->request->config->get('ajax_media_upload_tmp_directory'))) {
         $vs_tmp_directory = caGetTempDirPath();
     }
     $vn_user_id = $this->request->getUserID();
     $vs_user_dir = $vs_tmp_directory . "/userMedia{$vn_user_id}";
     if (!file_exists($vs_user_dir)) {
         mkdir($vs_user_dir);
     }
     if (!($vn_timeout = (int) $this->request->config->get('ajax_media_upload_tmp_directory_timeout'))) {
         $vn_timeout = 24 * 60 * 60;
     }
     // Cleanup any old files here
     $va_files_to_delete = caGetDirectoryContentsAsList($vs_user_dir, true, false, false, true, array('modifiedSince' => time() - $vn_timeout));
     foreach ($va_files_to_delete as $vs_file_to_delete) {
         @unlink($vs_file_to_delete);
     }
     $va_stored_files = array();
     foreach ($_FILES as $vn_i => $va_file) {
         $vs_dest_filename = pathinfo($va_file['tmp_name'], PATHINFO_FILENAME);
         copy($va_file['tmp_name'], $vs_dest_path = $vs_tmp_directory . "/userMedia{$vn_user_id}/{$vs_dest_filename}");
         // write file metadata
         file_put_contents("{$vs_dest_path}_metadata", json_encode(array('original_filename' => $va_file['name'], 'size' => filesize($vs_dest_path))));
         $va_stored_files[$vn_i] = "userMedia{$vn_user_id}/{$vs_dest_filename}";
         // only return the user directory and file name, not the entire path
     }
     print json_encode($va_stored_files);
 }
Beispiel #5
0
 private function _gmagickRead($ps_filepath)
 {
     try {
         $handle = new Gmagick($ps_filepath);
         $this->setResourceLimits($handle);
         $handle->setimageindex(0);
         // force use of first image in multi-page TIFF
         $this->handle = $handle;
         $this->filepath = $ps_filepath;
         $this->metadata = array();
         // handle metadata
         /* EXIF */
         if (function_exists('exif_read_data') && !$this->opo_config->get('dont_use_exif_read_data')) {
             if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) {
                 $va_metadata['EXIF'] = $va_exif;
             }
         }
         // if the builtin EXIF extraction is not used or failed for some reason, try ExifTool
         if (!isset($va_metadata['EXIF']) || !is_array($va_metadata['EXIF'])) {
             if (caExifToolInstalled()) {
                 $va_metadata['EXIF'] = caExtractMetadataWithExifTool($ps_filepath, true);
             }
         }
         // Rotate incoming image as needed
         if (isset($va_metadata['EXIF']['IFD0']['Orientation'])) {
             $vn_orientation = $va_metadata['EXIF']['IFD0']['Orientation'];
             $vs_tmp_basename = tempnam(caGetTempDirPath(), 'ca_image_tmp');
             $vb_is_rotated = false;
             switch ($vn_orientation) {
                 case 3:
                     $this->handle->rotateimage("#FFFFFF", 180);
                     unset($va_metadata['EXIF']['IFD0']['Orientation']);
                     $vb_is_rotated = true;
                     break;
                 case 6:
                     $this->handle->rotateimage("#FFFFFF", 90);
                     unset($va_metadata['EXIF']['IFD0']['Orientation']);
                     $vb_is_rotated = true;
                     break;
                 case 8:
                     $this->handle->rotateimage("#FFFFFF", -90);
                     unset($va_metadata['EXIF']['IFD0']['Orientation']);
                     $vb_is_rotated = true;
                     break;
             }
             if ($vb_is_rotated) {
                 if ($this->handle->writeimage($vs_tmp_basename)) {
                     $va_tmp = $this->handle->getimagegeometry();
                     $this->properties["faces"] = $this->opa_faces = caDetectFaces($vs_tmp_basename, $va_tmp['width'], $va_tmp['height']);
                 }
                 @unlink($vs_tmp_basename);
             }
         }
         // get XMP
         $o_xmp = new XMPParser();
         if ($o_xmp->parse($ps_filepath)) {
             if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) {
                 $va_metadata['XMP'] = array();
                 foreach ($va_xmp_metadata as $vs_xmp_tag => $va_xmp_values) {
                     $va_metadata['XMP'][$vs_xmp_tag] = join('; ', $va_xmp_values);
                 }
             }
         }
         // try to get IPTC and DPX with GraphicsMagick, if available
         if (caMediaPluginGraphicsMagickInstalled()) {
             /* IPTC metadata */
             $vs_iptc_file = tempnam(caGetTempDirPath(), 'gmiptc');
             @rename($vs_iptc_file, $vs_iptc_file . '.iptc');
             // GM uses the file extension to figure out what we want
             $vs_iptc_file .= '.iptc';
             exec($this->ops_graphicsmagick_path . " convert " . caEscapeShellArg($ps_filepath) . " " . caEscapeShellArg($vs_iptc_file) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return);
             $vs_iptc_data = file_get_contents($vs_iptc_file);
             @unlink($vs_iptc_file);
             $va_iptc_raw = iptcparse($vs_iptc_data);
             $va_iptc_tags = array('2#004' => 'Genre', '2#005' => 'DocumentTitle', '2#010' => 'Urgency', '2#015' => 'Category', '2#020' => 'Subcategories', '2#025' => 'Keywords', '2#040' => 'SpecialInstructions', '2#055' => 'CreationDate', '2#060' => 'TimeCreated', '2#080' => 'AuthorByline', '2#085' => 'AuthorTitle', '2#090' => 'City', '2#095' => 'State', '2#100' => 'CountryCode', '2#101' => 'Country', '2#103' => 'OTR', '2#105' => 'Headline', '2#110' => 'Credit', '2#115' => 'PhotoSource', '2#116' => 'Copyright', '2#120' => 'Caption', '2#122' => 'CaptionWriter');
             $va_iptc = array();
             if (is_array($va_iptc_raw)) {
                 foreach ($va_iptc_raw as $vs_iptc_tag => $va_iptc_tag_data) {
                     if (isset($va_iptc_tags[$vs_iptc_tag])) {
                         $va_iptc[$va_iptc_tags[$vs_iptc_tag]] = join('; ', $va_iptc_tag_data);
                     }
                 }
             }
             if (sizeof($va_iptc)) {
                 $va_metadata['IPTC'] = $va_iptc;
             }
             /* DPX metadata */
             exec($this->ops_graphicsmagick_path . " identify -format '%[DPX:*]' " . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return);
             if ($va_output[0]) {
                 $va_metadata['DPX'] = $va_output;
             }
         }
         $this->metadata = $va_metadata;
         return $handle;
     } catch (Exception $e) {
         $this->postError(1610, _t("Could not read image file"), "WLPlugGmagick->read()");
         return false;
         // gmagick couldn't read file, presumably
     }
 }
 private function _graphicsMagickGetMetadata($ps_filepath)
 {
     $va_metadata = array();
     /* EXIF metadata */
     if (function_exists('exif_read_data') && !$this->opo_config->get('dont_use_exif_read_data')) {
         if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) {
             $va_metadata['EXIF'] = $va_exif;
         }
     }
     // if the builtin EXIF extraction is not used or failed for some reason, try ExifTool
     if (!isset($va_metadata['EXIF']) || !is_array($va_metadata['EXIF'])) {
         if (caExifToolInstalled()) {
             $va_metadata['EXIF'] = caExtractMetadataWithExifTool($ps_filepath, true);
         }
     }
     // else try GraphicsMagick
     if (!isset($va_metadata['EXIF']) || !is_array($va_metadata['EXIF'])) {
         exec($this->ops_graphicsmagick_path . ' identify -format "%[EXIF:*]" ' . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return);
         if (is_array($va_output) && sizeof($va_output) > 1) {
             foreach ($va_output as $vs_output_line) {
                 $va_tmp = explode('=', $vs_output_line);
                 // format is "Make=NIKON CORPORATION"
                 if (isset($va_tmp[0]) && isset($va_tmp[1])) {
                     $va_metadata['EXIF'][$va_tmp[0]] = $va_tmp[1];
                 }
             }
         }
         $va_output = array();
     }
     $o_xmp = new XMPParser();
     if ($o_xmp->parse($ps_filepath)) {
         if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) {
             $va_metadata['XMP'] = array();
             foreach ($va_xmp_metadata as $vs_xmp_tag => $va_xmp_values) {
                 $va_metadata['XMP'][$vs_xmp_tag] = join('; ', $va_xmp_values);
             }
         }
     }
     /* IPTC metadata */
     $vs_iptc_file = tempnam(caGetTempDirPath(), 'gmiptc');
     @rename($vs_iptc_file, $vs_iptc_file . '.iptc');
     // GM uses the file extension to figure out what we want
     $vs_iptc_file .= '.iptc';
     exec($this->ops_graphicsmagick_path . " convert " . caEscapeShellArg($ps_filepath) . " " . caEscapeShellArg($vs_iptc_file) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return);
     $vs_iptc_data = file_get_contents($vs_iptc_file);
     @unlink($vs_iptc_file);
     $va_iptc_raw = iptcparse($vs_iptc_data);
     $va_iptc_tags = array('2#004' => 'Genre', '2#005' => 'DocumentTitle', '2#010' => 'Urgency', '2#015' => 'Category', '2#020' => 'Subcategories', '2#025' => 'Keywords', '2#040' => 'SpecialInstructions', '2#055' => 'CreationDate', '2#060' => 'TimeCreated', '2#080' => 'AuthorByline', '2#085' => 'AuthorTitle', '2#090' => 'City', '2#095' => 'State', '2#100' => 'CountryCode', '2#101' => 'Country', '2#103' => 'OTR', '2#105' => 'Headline', '2#110' => 'Credit', '2#115' => 'PhotoSource', '2#116' => 'Copyright', '2#120' => 'Caption', '2#122' => 'CaptionWriter');
     $va_iptc = array();
     if (is_array($va_iptc_raw)) {
         foreach ($va_iptc_raw as $vs_iptc_tag => $va_iptc_tag_data) {
             if (isset($va_iptc_tags[$vs_iptc_tag])) {
                 $va_iptc[$va_iptc_tags[$vs_iptc_tag]] = join('; ', $va_iptc_tag_data);
             }
         }
     }
     if (sizeof($va_iptc)) {
         $va_metadata['IPTC'] = $va_iptc;
     }
     /* DPX metadata */
     exec($this->ops_graphicsmagick_path . " identify -format '%[DPX:*]' " . caEscapeShellArg($ps_filepath) . (caIsPOSIX() ? " 2> /dev/null" : ""), $va_output, $vn_return);
     if ($va_output[0]) {
         $va_metadata['DPX'] = $va_output;
     }
     return $va_metadata;
 }
 protected function _genExportWithMapping($po_result, $pn_exporter_id)
 {
     // Can user batch export?
     if (!$this->request->user->canDoAction('can_batch_export_metadata')) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3440?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     // Can user export records of this type?
     if (!$this->request->user->canDoAction('can_export_' . $this->ops_tablename)) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3430?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     $t_exporter = new ca_data_exporters($pn_exporter_id);
     if (!($t_exporter->getPrimaryKey() > 0)) {
         $this->postError(3420, _t("Could not load export mapping"), "BaseFindController->_genExportWithMapping()");
         return;
     }
     if (substr(get_class($this), 0, 6) == 'Browse') {
         $vs_export_filename = Configuration::load()->get($this->ops_tablename . '_batch_export_filename');
     } else {
         $vs_export_filename = preg_replace("/[^\\p{L}\\p{N}\\-]/", '_', $this->opo_result_context->getSearchExpression());
         $vs_export_filename = preg_replace("/[\\_]+/", '_', $vs_export_filename);
     }
     $vs_tmp_file = tempnam(caGetTempDirPath(), 'export');
     ca_data_exporters::exportRecordsFromSearchResult($t_exporter->get('exporter_code'), $po_result, $vs_tmp_file);
     header('Content-Type: ' . $t_exporter->getContentType() . '; charset=UTF-8');
     header('Content-Disposition: attachment; filename="' . $vs_export_filename . '.' . $t_exporter->getFileExtension() . '"');
     header('Content-Transfer-Encoding: binary');
     readfile($vs_tmp_file);
     @unlink($vs_tmp_file);
     exit;
 }
Beispiel #8
0
 public function joinArchiveContents($pa_files, $pa_options = array())
 {
     if (!is_array($pa_files)) {
         return false;
     }
     $vs_archive_original = tempnam(caGetTempDirPath(), "caArchiveOriginal");
     @rename($vs_archive_original, $vs_archive_original . ".tif");
     $vs_archive_original = $vs_archive_original . ".tif";
     $vo_orig = new Imagick();
     foreach ($pa_files as $vs_file) {
         if (file_exists($vs_file)) {
             $vo_imagick = new Imagick();
             if ($vo_imagick->readImage($vs_file)) {
                 $vo_orig->addImage($vo_imagick);
             }
         }
     }
     if ($vo_orig->getNumberImages() > 0) {
         if ($vo_orig->writeImages($vs_archive_original, true)) {
             return $vs_archive_original;
         }
     }
     return false;
 }
 public static function lockRelease()
 {
     if (function_exists('sem_get') && caGetOSFamily() == OS_POSIX) {
         if (!self::$s_lock_resource) {
             self::$s_lock_resource = sem_get(ftok(__FILE__, 'CASearchIndexingQueue'));
         }
         sem_release(self::$s_lock_resource);
     } else {
         if (is_resource(self::$s_lock_resource)) {
             @fclose(self::$s_lock_resource);
         }
         @unlink(caGetTempDirPath() . DIRECTORY_SEPARATOR . 'search_indexing_queue.lock');
     }
 }
Beispiel #10
0
 /**
  * Quickly loads list of row_ids in $pa_hits into a temporary database table uniquely identified by $ps_key
  * Only one temporary table can exist at a time for a given instance. If you call loadListIntoTemporaryResultTable() 
  * on an instance for which a temporary table has already been loaded the previously created table will be discarded
  * before the new one is created.
  *
  * @param array $pa_hits Array of row_id values
  * @param string $ps_key Unique alphanumeric identifier for the temporary table. Should only contain letters, numbers and underscores.
  * @param array $pa_options
  * @return string The name of the temporary table created
  */
 public function loadListIntoTemporaryResultTable($pa_hits, $ps_key, $pa_options = null)
 {
     global $g_mysql_has_file_priv;
     $ps_key = preg_replace('![^A-Za-z0-9_]+!', '_', $ps_key);
     if ($this->ops_tmp_table_name == "caResultTmp{$ps_key}") {
         return $this->ops_tmp_table_name;
     }
     if ($this->ops_tmp_table_name) {
         $this->cleanupTemporaryResultTable();
     }
     $this->ops_tmp_file_path = tempnam(caGetTempDirPath(), 'caResultTmp');
     $this->ops_tmp_table_name = "caResultTmp{$ps_key}";
     if (is_array($va_sortable_values = caGetOption('sortableValues', $pa_options, false))) {
         $this->opo_db->query("\n\t\t\t\t\tCREATE TEMPORARY TABLE {$this->ops_tmp_table_name} (\n\t\t\t\t\t\trow_id int unsigned not null,\n\t\t\t\t\t\tsort_key1 varchar(255) not null default '',\n\t\t\t\t\t\tsort_key2 varchar(255) not null default '',\n\t\t\t\t\t\tsort_key3 varchar(255) not null default '',\n\t\t\t\t\t\tkey (row_id)\n\t\t\t\t\t) engine=memory;\n\t\t\t\t");
     } else {
         $this->opo_db->query("\n\t\t\t\t\tCREATE TEMPORARY TABLE {$this->ops_tmp_table_name} (\n\t\t\t\t\t\trow_id int unsigned not null,\n\t\t\t\t\t\tkey (row_id)\n\t\t\t\t\t) engine=memory;\n\t\t\t\t");
     }
     if (!sizeof($pa_hits)) {
         return $this->ops_tmp_table_name;
     }
     if (is_null($g_mysql_has_file_priv)) {
         // Figure out if user has FILE priv
         $qr_grants = $this->opo_db->query("\n\t\t\t\t\tSHOW GRANTS;\n\t\t\t\t");
         $g_mysql_has_file_priv = false;
         while ($qr_grants->nextRow()) {
             $va_grants = array_values($qr_grants->getRow());
             $vs_grant = array_shift($va_grants);
             if (preg_match('!^GRANT FILE!', $vs_grant)) {
                 $g_mysql_has_file_priv = true;
                 break;
             }
         }
     }
     if ($g_mysql_has_file_priv === true && sizeof($pa_hits) > 500) {
         // Benchmarking has show that using "LOAD DATA INFILE" with an on-disk tmp file performs best with large result sets
         // The downside is that it requires the MySQL global FILE priv, which often is not granted, especially in shared environments
         $vs_data = '';
         if (is_array($va_sortable_values)) {
             foreach ($pa_hits as $vn_hit) {
                 if (!($vs_sort_key_1 = $va_sortable_values[0][$vn_hit])) {
                     $vs_sort_key_1 = '';
                 }
                 if (!($vs_sort_key_2 = $va_sortable_values[1][$vn_hit])) {
                     $vs_sort_key_2 = '';
                 }
                 if (!($vs_sort_key_3 = $va_sortable_values[2][$vn_hit])) {
                     $vs_sort_key_3 = '';
                 }
                 if (is_numeric($vs_sort_key_1)) {
                     $vs_sort_key_1 = str_pad($vs_sort_key_1, 12, '0', STR_PAD_LEFT);
                 }
                 if (is_numeric($vs_sort_key_2)) {
                     $vs_sort_key_2 = str_pad($vs_sort_key_2, 12, '0', STR_PAD_LEFT);
                 }
                 if (is_numeric($vs_sort_key_3)) {
                     $vs_sort_key_3 = str_pad($vs_sort_key_3, 12, '0', STR_PAD_LEFT);
                 }
                 $vs_data .= $vn_hit . ',' . $vs_sort_key_1 . ',' . $vs_sort_key_2 . ',' . $vs_sort_key_3 . "\n";
             }
         } else {
             $vs_data = join("\n", $pa_hits);
         }
         file_put_contents($this->ops_tmp_file_path, $vs_data);
         chmod($this->ops_tmp_file_path, 0755);
         if (is_array($va_sortable_values)) {
             $vs_sql = "LOAD DATA INFILE '{$this->ops_tmp_file_path}' INTO TABLE {$this->ops_tmp_table_name} FIELDS TERMINATED BY ',' (row_id, sort_key1, sort_key2, sort_key3)";
             $this->opo_db->query($vs_sql);
         } else {
             $this->opo_db->query("LOAD DATA INFILE '{$this->ops_tmp_file_path}' INTO TABLE {$this->ops_tmp_table_name} (row_id)");
         }
     } else {
         // Fallback when database login does not have FILE priv
         if (is_array($va_sortable_values)) {
             $vs_sql = "INSERT IGNORE INTO {$this->ops_tmp_table_name} (row_id, sort_key1, sort_key2, sort_key3) VALUES ";
             foreach ($pa_hits as $vn_hit) {
                 if (!($vs_sort_key_1 = $va_sortable_values[0][$vn_hit])) {
                     $vs_sort_key_1 = '';
                 } else {
                     $vs_sort_key_1 = preg_replace("/[^[:alnum:][:space:]]/ui", '', $vs_sort_key_1);
                 }
                 if (!($vs_sort_key_2 = $va_sortable_values[1][$vn_hit])) {
                     $vs_sort_key_2 = '';
                 } else {
                     $vs_sort_key_2 = preg_replace("/[^[:alnum:][:space:]]/ui", '', $vs_sort_key_2);
                 }
                 if (!($vs_sort_key_3 = $va_sortable_values[2][$vn_hit])) {
                     $vs_sort_key_3 = '';
                 } else {
                     $vs_sort_key_3 = preg_replace("/[^[:alnum:][:space:]]/ui", '', $vs_sort_key_3);
                 }
                 if (is_numeric($vs_sort_key_1)) {
                     $vs_sort_key_1 = str_pad($vs_sort_key_1, 12, '0', STR_PAD_LEFT);
                 }
                 if (is_numeric($vs_sort_key_2)) {
                     $vs_sort_key_2 = str_pad($vs_sort_key_2, 12, '0', STR_PAD_LEFT);
                 }
                 if (is_numeric($vs_sort_key_3)) {
                     $vs_sort_key_3 = str_pad($vs_sort_key_3, 12, '0', STR_PAD_LEFT);
                 }
                 $vs_sql .= "(" . (int) $vn_hit . ",'" . $vs_sort_key_1 . "','" . $vs_sort_key_2 . "','" . $vs_sort_key_1 . "'),";
             }
         } else {
             $vs_sql = "INSERT IGNORE INTO {$this->ops_tmp_table_name} (row_id) VALUES ";
             foreach ($pa_hits as $vn_hit) {
                 $vs_sql .= "(" . (int) $vn_hit . "),";
             }
         }
         $this->opo_db->query(substr($vs_sql, 0, strlen($vs_sql) - 1));
     }
     return $this->ops_tmp_table_name;
 }
Beispiel #11
0
 public function read($ps_filepath, $mimetype = "")
 {
     if (!($this->handle && ${$ps_filepath} === $this->filepath)) {
         if ($mimetype == 'image/tilepic') {
             #
             # Read in Tilepic format image
             #
             $this->handle = new TilepicParser($ps_filepath);
             if (!$this->handle->error) {
                 $this->filepath = $ps_filepath;
                 foreach ($this->handle->properties as $k => $v) {
                     if (isset($this->properties[$k])) {
                         $this->properties[$k] = $v;
                     }
                 }
                 $this->properties["mimetype"] = "image/tilepic";
                 $this->properties["typename"] = "Tilepic";
                 return 1;
             } else {
                 $this->postError(1610, $this->handle->error, "WLPlugGmagick->read()");
                 return false;
             }
         } else {
             $this->handle = "";
             $this->filepath = "";
             if ($mimetype == 'image/x-dcraw') {
                 if ($this->filepath_conv) {
                     @unlink($this->filepath_conv);
                 }
                 if (!caMediaPluginDcrawInstalled($this->ops_dcraw_path)) {
                     $this->postError(1610, _t("Could not convert Camera RAW format file because conversion tool (dcraw) is not installed"), "WLPlugGmagick->read()");
                     return false;
                 }
                 $vs_tmp_name = tempnam(caGetTempDirPath(), "rawtmp");
                 if (!copy($ps_filepath, $vs_tmp_name)) {
                     $this->postError(1610, _t("Could not copy Camera RAW file to temporary directory"), "WLPlugGmagick->read()");
                     return false;
                 }
                 exec($this->ops_dcraw_path . " -T " . caEscapeShellArg($vs_tmp_name), $va_output, $vn_return);
                 if ($vn_return != 0) {
                     $this->postError(1610, _t("Camera RAW file conversion failed: %1", $vn_return), "WLPlugGmagick->read()");
                     return false;
                 }
                 if (!(file_exists($vs_tmp_name . '.tiff') && filesize($vs_tmp_name . '.tiff') > 0)) {
                     $this->postError(1610, _t("Translation from Camera RAW to TIFF failed"), "WLPlugGmagick->read()");
                     return false;
                 }
                 $ps_filepath = $this->filepath_conv = $vs_tmp_name . '.tiff';
                 @unlink($vs_tmp_name);
             }
             try {
                 $handle = new Gmagick($ps_filepath);
                 $handle->setimageindex(0);
                 // force use of first image in multi-page TIFF
                 $this->handle = $handle;
                 $this->filepath = $ps_filepath;
                 $this->metadata = array();
                 // exif
                 if (function_exists('exif_read_data')) {
                     if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) {
                         //
                         // Rotate incoming image as needed
                         //
                         if (isset($va_exif['IFD0']['Orientation'])) {
                             $vn_orientation = $va_exif['IFD0']['Orientation'];
                             $vs_tmp_basename = tempnam(caGetTempDirPath(), 'ca_image_tmp');
                             $vb_is_rotated = false;
                             switch ($vn_orientation) {
                                 case 3:
                                     $this->handle->rotateimage("#FFFFFF", 180);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                                 case 6:
                                     $this->handle->rotateimage("#FFFFFF", 90);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                                 case 8:
                                     $this->handle->rotateimage("#FFFFFF", -90);
                                     unset($va_exif['IFD0']['Orientation']);
                                     $vb_is_rotated = true;
                                     break;
                             }
                             if ($vb_is_rotated) {
                                 if ($this->handle->writeimage($vs_tmp_basename)) {
                                     $va_tmp = $this->handle->getimagegeometry();
                                     $this->properties["faces"] = $this->opa_faces = caDetectFaces($vs_tmp_basename, $va_tmp['width'], $va_tmp['height']);
                                 }
                                 @unlink($vs_tmp_basename);
                             }
                         }
                         $this->metadata['EXIF'] = $va_exif;
                     }
                 }
                 // XMP
                 $o_xmp = new XMPParser();
                 if ($o_xmp->parse($ps_filepath)) {
                     if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) {
                         $this->metadata['XMP'] = $va_xmp_metadata;
                     }
                 }
                 # load image properties
                 $va_tmp = $this->handle->getimagegeometry();
                 $this->properties["width"] = $va_tmp['width'];
                 $this->properties["height"] = $va_tmp['height'];
                 $this->properties["quality"] = "";
                 $this->properties["filesize"] = filesize($ps_filepath);
                 $this->properties["bitdepth"] = $this->handle->getimagedepth();
                 $this->properties["resolution"] = $this->handle->getimageresolution();
                 $this->properties["colorspace"] = $this->_getColorspaceAsString($this->handle->getimagecolorspace());
                 // force all images to true color (takes care of GIF transparency for one thing...)
                 $this->handle->setimagetype(Gmagick::IMGTYPE_TRUECOLOR);
                 if (!$this->handle->setimagecolorspace(Gmagick::COLORSPACE_RGB)) {
                     $this->postError(1610, _t("Error during RGB colorspace transformation operation"), "WLPlugGmagick->read()");
                     return false;
                 }
                 if (!$this->properties["faces"]) {
                     $this->properties["faces"] = $this->opa_faces = caDetectFaces($ps_filepath, $va_tmp['width'], $va_tmp['height']);
                 }
                 $this->properties["mimetype"] = $this->_getMagickImageMimeType($this->handle);
                 $this->properties["typename"] = $this->handle->getimageformat();
                 $this->ohandle = clone $this->handle;
                 return 1;
             } catch (Exception $e) {
                 $this->postError(1610, _t("Could not read image file"), "WLPlugGmagick->read()");
                 return false;
             }
         }
     } else {
         # image already loaded by previous call (probably divineFileFormat())
         return 1;
     }
 }
 /**
  * Perform periodic tasks
  *
  * @return boolean true because otherwise it disables subsequent plugins
  */
 public function hookPeriodicTask(&$pa_params)
 {
     global $AUTH_CURRENT_USER_ID;
     $t_log = new Eventlog();
     $o_db = new Db();
     //$t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not authenticate to remote system %1', $vs_base_url), 'SOURCE' => 'traveloguePlugin->hookPeriodicTask'));
     // Get new email
     $pn_locale_id = 1;
     // US
     $vs_server = $this->opo_config->get('imap_server');
     $vs_username = $this->opo_config->get('username');
     $vs_password = $this->opo_config->get('password');
     $vs_ssl = $this->opo_config->get('ssl');
     if (!$vs_server) {
         return true;
     }
     if (!$vs_username) {
         return true;
     }
     try {
         $o_mail = new Zend_Mail_Storage_Imap(array('host' => $vs_server, 'user' => $vs_username, 'password' => $vs_password, 'ssl' => $vs_ssl));
     } catch (Exception $e) {
         return true;
     }
     $va_mimetypes = $this->opo_config->getList('mimetypes');
     $va_mail_to_delete = array();
     foreach ($o_mail as $vn_message_num => $o_message) {
         $va_mail_to_delete[$vn_message_num] = true;
         // Extract title from subject line of email
         $vs_subject = $o_message->subject;
         $vs_from = $o_message->headerExists('from') ? $o_message->from : "";
         print "PROCESSING {$vs_subject} FROM {$vs_from}\n";
         // Extract media from email attachments
         // Extract caption from email body
         $va_images = array();
         $va_texts = array();
         foreach (new RecursiveIteratorIterator($o_message) as $o_part) {
             try {
                 if (in_array(strtok($o_part->contentType, ';'), $va_mimetypes)) {
                     $va_images[] = $o_part;
                 } else {
                     if (in_array(strtok($o_part->contentType, ';'), array("text/plain", "text/html"))) {
                         $va_texts[] = (string) $o_part;
                     }
                 }
             } catch (Zend_Mail_Exception $e) {
                 // ignore
             }
         }
         if (!sizeof($va_images)) {
             continue;
         }
         // Get user by email address
         if (preg_match('!<([^>]+)>!', $vs_from, $va_matches)) {
             // extract raw address from "from" header
             $vs_from = $va_matches[1];
         }
         $t_user = new ca_users();
         if ($t_user->load(array('email' => $vs_from))) {
             $AUTH_CURRENT_USER_ID = $vn_user_id = $t_user->getPrimaryKey();
             // force libs to consider matched user as logged in; change log will reflect this name
         } else {
             $vn_user_id = null;
         }
         // Create object
         $t_object = new ca_objects();
         $t_object->setMode(ACCESS_WRITE);
         $t_object->set('type_id', $this->opo_config->get('object_type'));
         // TODO: set idno to autogenerated # and/or allow for configurable policy
         $t_object->set('idno', '');
         $t_object->set('access', $this->opo_config->get('default_access'));
         $t_object->set('status', $this->opo_config->get('default_status'));
         // TODO: make this a configurable mapping ala how media metadata is done
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'generalNotes' => join("\n\n", $va_texts)), 'generalNotes');
         $t_object->insert();
         DataMigrationUtils::postError($t_object, "While adding object", "traveloguePlugin");
         // TODO: log this
         $t_object->addLabel(array('name' => $vs_subject), $pn_locale_id, null, true);
         DataMigrationUtils::postError($t_object, "While adding label", "traveloguePlugin");
         // TODO: log this
         // Add representation
         $vs_tmp_file_path = tempnam(caGetTempDirPath(), 'travelogue_');
         foreach ($va_images as $vn_i => $vs_file_content) {
             if (file_put_contents($vs_tmp_file_path, base64_decode((string) $vs_file_content))) {
                 $t_object->addRepresentation($vs_tmp_file_path, $this->opo_config->get('representation_type'), 1, $this->opo_config->get('default_status'), $this->opo_config->get('default_access'), true);
                 DataMigrationUtils::postError($t_object, "While adding media", "traveloguePlugin");
                 // TODO: log this
             }
         }
         // TODO: add option to link user-as-entity to image (probably as creator)
     }
     foreach (array_reverse(array_keys($va_mail_to_delete)) as $vn_message_num) {
         $o_mail->removeMessage($vn_message_num);
     }
     return true;
 }
 /**
  * @param RequestHTTP $po_request
  * @param null|array $pa_options
  *		progressCallback =
  *		reportCallback =
  *		sendMail =
  *		log = log directory path
  *		logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness:
  *			KLogger::EMERG = Emergency messages (system is unusable)
  *			KLogger::ALERT = Alert messages (action must be taken immediately)
  *			KLogger::CRIT = Critical conditions
  *			KLogger::ERR = Error conditions
  *			KLogger::WARN = Warnings
  *			KLogger::NOTICE = Notices (normal but significant conditions)
  *			KLogger::INFO = Informational messages
  *			KLogger::DEBUG = Debugging messages
  * @return array
  */
 public static function importMediaFromDirectory($po_request, $pa_options = null)
 {
     global $g_ui_locale_id;
     $vs_log_dir = caGetOption('log', $pa_options, __CA_APP_DIR__ . "/log");
     $vs_log_level = caGetOption('logLevel', $pa_options, "INFO");
     if (!is_writeable($vs_log_dir)) {
         $vs_log_dir = caGetTempDirPath();
     }
     $vn_log_level = BatchProcessor::_logLevelStringToNumber($vs_log_level);
     $o_log = new KLogger($vs_log_dir, $vn_log_level);
     $vs_import_target = caGetOption('importTarget', $pa_options, 'ca_objects');
     $t_instance = $po_request->getAppDatamodel()->getInstance($vs_import_target);
     $o_eventlog = new Eventlog();
     $t_set = new ca_sets();
     $va_notices = $va_errors = array();
     $vb_we_set_transaction = false;
     $o_trans = isset($pa_options['transaction']) && $pa_options['transaction'] ? $pa_options['transaction'] : null;
     if (!$o_trans) {
         $vb_we_set_transaction = true;
         $o_trans = new Transaction($t_set->getDb());
     }
     $o_batch_log = new Batchlog(array('user_id' => $po_request->getUserID(), 'batch_type' => 'MI', 'table_num' => (int) $t_instance->tableNum(), 'notes' => '', 'transaction' => $o_trans));
     if (!is_dir($pa_options['importFromDirectory'])) {
         $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => $vs_msg = _t("Specified import directory '%1' is invalid", $pa_options['importFromDirectory'])));
         $o_log->logError($vs_msg);
         return null;
     }
     $vs_batch_media_import_root_directory = $po_request->config->get('batch_media_import_root_directory');
     if (!preg_match("!^{$vs_batch_media_import_root_directory}!", $pa_options['importFromDirectory'])) {
         $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => $vs_msg = _t("Specified import directory '%1' is invalid", $pa_options['importFromDirectory'])));
         $o_log->logError($vs_msg);
         return null;
     }
     if (preg_match("!\\.\\./!", $pa_options['importFromDirectory'])) {
         $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => $vs_msg = _t("Specified import directory '%1' is invalid", $pa_options['importFromDirectory'])));
         $o_log->logError($vs_msg);
         return null;
     }
     $vb_include_subdirectories = (bool) $pa_options['includeSubDirectories'];
     $vb_delete_media_on_import = (bool) $pa_options['deleteMediaOnImport'];
     $vs_import_mode = $pa_options['importMode'];
     $vs_match_mode = $pa_options['matchMode'];
     $vn_type_id = $pa_options[$vs_import_target . '_type_id'];
     $vn_rep_type_id = $pa_options['ca_object_representations_type_id'];
     $va_limit_matching_to_type_ids = $pa_options[$vs_import_target . '_limit_matching_to_type_ids'];
     $vn_access = $pa_options[$vs_import_target . '_access'];
     $vn_object_representation_access = $pa_options['ca_object_representations_access'];
     $vn_status = $pa_options[$vs_import_target . '_status'];
     $vn_object_representation_status = $pa_options['ca_object_representations_status'];
     $vn_rel_type_id = isset($pa_options[$vs_import_target . '_representation_relationship_type']) ? $pa_options[$vs_import_target . '_representation_relationship_type'] : null;
     $vn_mapping_id = $pa_options[$vs_import_target . '_mapping_id'];
     $vn_object_representation_mapping_id = $pa_options['ca_object_representations_mapping_id'];
     $vs_idno_mode = $pa_options['idnoMode'];
     $vs_idno = $pa_options['idno'];
     $vs_representation_idno_mode = $pa_options['representationIdnoMode'];
     $vs_representation_idno = $pa_options['representation_idno'];
     $vs_set_mode = $pa_options['setMode'];
     $vs_set_create_name = $pa_options['setCreateName'];
     $vn_set_id = $pa_options['set_id'];
     $vn_locale_id = $pa_options['locale_id'];
     $vs_skip_file_list = $pa_options['skipFileList'];
     $vs_skip_file_list = $pa_options['skipFileList'];
     $vb_allow_duplicate_media = $pa_options['allowDuplicateMedia'];
     $va_relationship_type_id_for = array();
     if (is_array($va_create_relationship_for = $pa_options['create_relationship_for'])) {
         foreach ($va_create_relationship_for as $vs_rel_table) {
             $va_relationship_type_id_for[$vs_rel_table] = $pa_options['relationship_type_id_for_' . $vs_rel_table];
         }
     }
     if (!$vn_locale_id) {
         $vn_locale_id = $g_ui_locale_id;
     }
     $va_files_to_process = caGetDirectoryContentsAsList($pa_options['importFromDirectory'], $vb_include_subdirectories);
     $o_log->logInfo(_t('Found %1 files in directory \'%2\'', sizeof($va_files_to_process), $pa_options['importFromDirectory']));
     if ($vs_set_mode == 'add') {
         $t_set->load($vn_set_id);
     } else {
         if ($vs_set_mode == 'create' && $vs_set_create_name) {
             $va_set_ids = $t_set->getSets(array('user_id' => $po_request->getUserID(), 'table' => $t_instance->tableName(), 'access' => __CA_SET_EDIT_ACCESS__, 'setIDsOnly' => true, 'name' => $vs_set_create_name));
             $vn_set_id = null;
             if (is_array($va_set_ids) && sizeof($va_set_ids) > 0) {
                 $vn_possible_set_id = array_shift($va_set_ids);
                 if ($t_set->load($vn_possible_set_id)) {
                     $vn_set_id = $t_set->getPrimaryKey();
                 }
             } else {
                 $vs_set_code = mb_substr(preg_replace("![^A-Za-z0-9_\\-]+!", "_", $vs_set_create_name), 0, 100);
                 if ($t_set->load(array('set_code' => $vs_set_code))) {
                     $vn_set_id = $t_set->getPrimaryKey();
                 }
             }
             if (!$t_set->getPrimaryKey()) {
                 $t_set->setMode(ACCESS_WRITE);
                 $t_set->set('user_id', $po_request->getUserID());
                 $t_set->set('type_id', $po_request->config->get('ca_sets_default_type'));
                 $t_set->set('table_num', $t_instance->tableNum());
                 $t_set->set('set_code', $vs_set_code);
                 $t_set->insert();
                 if ($t_set->numErrors()) {
                     $va_notices['create_set'] = array('idno' => '', 'label' => _t('Create set %1', $vs_set_create_name), 'message' => $vs_msg = _t('Failed to create set %1: %2', $vs_set_create_name, join("; ", $t_set->getErrors())), 'status' => 'SET ERROR');
                     $o_log->logError($vs_msg);
                 } else {
                     $t_set->addLabel(array('name' => $vs_set_create_name), $vn_locale_id, null, true);
                     if ($t_set->numErrors()) {
                         $va_notices['add_set_label'] = array('idno' => '', 'label' => _t('Add label to set %1', $vs_set_create_name), 'message' => $vs_msg = _t('Failed to add label to set: %1', join("; ", $t_set->getErrors())), 'status' => 'SET ERROR');
                         $o_log->logError($vs_msg);
                     }
                     $vn_set_id = $t_set->getPrimaryKey();
                 }
             }
         } else {
             $vn_set_id = null;
             // no set
         }
     }
     if ($t_set->getPrimaryKey() && !$t_set->haveAccessToSet($po_request->getUserID(), __CA_SET_EDIT_ACCESS__)) {
         $va_notices['set_access'] = array('idno' => '', 'label' => _t('You do not have access to set %1', $vs_set_create_name), 'message' => $vs_msg = _t('Cannot add to set %1 because you do not have edit access', $vs_set_create_name), 'status' => 'SET ERROR');
         $o_log->logError($vs_msg);
         $vn_set_id = null;
         $t_set = new ca_sets();
     }
     $vn_num_items = sizeof($va_files_to_process);
     // Get list of regex packages that user can use to extract object idno's from filenames
     $va_regex_list = caBatchGetMediaFilenameToIdnoRegexList(array('log' => $o_log));
     // Get list of replacements that user can use to transform file names to match object idnos
     $va_replacements_list = caBatchGetMediaFilenameReplacementRegexList(array('log' => $o_log));
     // Get list of files (or file name patterns) to skip
     $va_skip_list = preg_split("![\r\n]+!", $vs_skip_file_list);
     foreach ($va_skip_list as $vn_i => $vs_skip) {
         if (!strlen($va_skip_list[$vn_i] = trim($vs_skip))) {
             unset($va_skip_list[$vn_i]);
         }
     }
     $vn_c = 0;
     $vn_start_time = time();
     $va_report = array();
     foreach ($va_files_to_process as $vs_file) {
         $va_tmp = explode("/", $vs_file);
         $f = array_pop($va_tmp);
         $d = array_pop($va_tmp);
         array_push($va_tmp, $d);
         $vs_directory = join("/", $va_tmp);
         // Skip file names using $vs_skip_file_list
         if (BatchProcessor::_skipFile($f, $va_skip_list)) {
             $o_log->logInfo(_t('Skipped file %1 because it was on the skipped files list', $f));
             continue;
         }
         $vs_relative_directory = preg_replace("!{$vs_batch_media_import_root_directory}[/]*!", "", $vs_directory);
         // does representation already exist?
         if (!$vb_allow_duplicate_media && ($t_dupe = ca_object_representations::mediaExists($vs_file))) {
             $va_notices[$vs_relative_directory . '/' . $f] = array('idno' => '', 'label' => $f, 'message' => $vs_msg = _t('Skipped %1 from %2 because it already exists %3', $f, $vs_relative_directory, caEditorLink($po_request, _t('(view)'), 'button', 'ca_object_representations', $t_dupe->getPrimaryKey())), 'status' => 'SKIPPED');
             $o_log->logInfo($vs_msg);
             continue;
         }
         $t_instance = $po_request->getAppDatamodel()->getInstance($vs_import_target, false);
         $t_instance->setTransaction($o_trans);
         $vs_modified_filename = $f;
         $va_extracted_idnos_from_filename = array();
         if (in_array($vs_import_mode, array('TRY_TO_MATCH', 'ALWAYS_MATCH')) || is_array($va_create_relationship_for) && sizeof($va_create_relationship_for)) {
             foreach ($va_regex_list as $vs_regex_name => $va_regex_info) {
                 $o_log->logDebug(_t("Processing mediaFilenameToObjectIdnoRegexes entry %1", $vs_regex_name));
                 foreach ($va_regex_info['regexes'] as $vs_regex) {
                     switch ($vs_match_mode) {
                         case 'DIRECTORY_NAME':
                             $va_names_to_match = array($d);
                             $o_log->logDebug(_t("Trying to match on directory '%1'", $d));
                             break;
                         case 'FILE_AND_DIRECTORY_NAMES':
                             $va_names_to_match = array($f, $d);
                             $o_log->logDebug(_t("Trying to match on directory '%1' and file name '%2'", $d, $f));
                             break;
                         default:
                         case 'FILE_NAME':
                             $va_names_to_match = array($f);
                             $o_log->logDebug(_t("Trying to match on file name '%1'", $f));
                             break;
                     }
                     // are there any replacements? if so, try to match each element in $va_names_to_match AND all results of the replacements
                     if (is_array($va_replacements_list) && sizeof($va_replacements_list) > 0) {
                         $va_names_to_match_copy = $va_names_to_match;
                         foreach ($va_names_to_match_copy as $vs_name) {
                             foreach ($va_replacements_list as $vs_replacement_code => $va_replacement) {
                                 if (isset($va_replacement['search']) && is_array($va_replacement['search'])) {
                                     $va_replace = caGetOption('replace', $va_replacement);
                                     $va_search = array();
                                     foreach ($va_replacement['search'] as $vs_search) {
                                         $va_search[] = '!' . $vs_search . '!';
                                     }
                                     $vs_replacement_result = @preg_replace($va_search, $va_replace, $vs_name);
                                     if (is_null($vs_replacement_result)) {
                                         $o_log->logError(_t("There was an error in preg_replace while processing replacement %1.", $vs_replacement_code));
                                     }
                                     if ($vs_replacement_result && strlen($vs_replacement_result) > 0) {
                                         $o_log->logDebug(_t("The result for replacement with code %1 applied to value '%2' is '%3' and was added to the list of file names used for matching.", $vs_replacement_code, $vs_name, $vs_replacement_result));
                                         $va_names_to_match[] = $vs_replacement_result;
                                     }
                                 } else {
                                     $o_log->logDebug(_t("Skipped replacement %1 because no search expression was defined.", $vs_replacement_code));
                                 }
                             }
                         }
                     }
                     $o_log->logDebug("Names to match: " . print_r($va_names_to_match, true));
                     foreach ($va_names_to_match as $vs_match_name) {
                         if (preg_match('!' . $vs_regex . '!', $vs_match_name, $va_matches)) {
                             $o_log->logDebug(_t("Matched name %1 on regex %2", $vs_match_name, $vs_regex));
                             if (!$vs_idno || strlen($va_matches[1]) < strlen($vs_idno)) {
                                 $vs_idno = $va_matches[1];
                             }
                             if (!$vs_modified_filename || strlen($vs_modified_filename) > strlen($va_matches[1])) {
                                 $vs_modified_filename = $va_matches[1];
                             }
                             $va_extracted_idnos_from_filename[] = $va_matches[1];
                             if (in_array($vs_import_mode, array('TRY_TO_MATCH', 'ALWAYS_MATCH'))) {
                                 if (!is_array($va_fields_to_match_on = $po_request->config->getList('batch_media_import_match_on')) || !sizeof($va_fields_to_match_on)) {
                                     $batch_media_import_match_on = array('idno');
                                 }
                                 $vs_bool = 'OR';
                                 $va_values = array();
                                 foreach ($va_fields_to_match_on as $vs_fld) {
                                     if (in_array($vs_fld, array('preferred_labels', 'nonpreferred_labels'))) {
                                         $va_values[$vs_fld] = array($vs_fld => array('name' => $va_matches[1]));
                                     } else {
                                         $va_values[$vs_fld] = $va_matches[1];
                                     }
                                 }
                                 if (is_array($va_limit_matching_to_type_ids) && sizeof($va_limit_matching_to_type_ids) > 0) {
                                     $va_values['type_id'] = $va_limit_matching_to_type_ids;
                                     $vs_bool = 'AND';
                                 }
                                 $o_log->logDebug("Trying to find records using boolean {$vs_bool} and values " . print_r($va_values, true));
                                 if (class_exists($vs_import_target) && ($vn_id = $vs_import_target::find($va_values, array('returnAs' => 'firstId', 'boolean' => $vs_bool)))) {
                                     if ($t_instance->load($vn_id)) {
                                         $va_notices[$vs_relative_directory . '/' . $vs_match_name . '_match'] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'message' => $vs_msg = _t('Matched media %1 from %2 to %3 using expression "%4"', $f, $vs_relative_directory, caGetTableDisplayName($vs_import_target, false), $va_regex_info['displayName']), 'status' => 'MATCHED');
                                         $o_log->logInfo($vs_msg);
                                     }
                                     break 3;
                                 }
                             }
                         } else {
                             $o_log->logDebug(_t("Couldn't match name %1 on regex %2", $vs_match_name, $vs_regex));
                         }
                     }
                 }
             }
         }
         if (!$t_instance->getPrimaryKey()) {
             // Use filename as idno if all else fails
             if ($t_instance->load(array('idno' => $f, 'deleted' => 0))) {
                 $va_notices[$vs_relative_directory . '/' . $f . '_match'] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'message' => $vs_msg = _t('Matched media %1 from %2 to %3 using filename', $f, $vs_relative_directory, caGetTableDisplayName($vs_import_target, false)), 'status' => 'MATCHED');
                 $o_log->logInfo($vs_msg);
             }
         }
         switch ($vs_representation_idno_mode) {
             case 'filename':
                 // use the filename as identifier
                 $vs_rep_idno = $f;
                 break;
             case 'filename_no_ext':
                 // use filename without extension as identifier
                 $vs_rep_idno = preg_replace('/\\.[^.\\s]{3,4}$/', '', $f);
                 break;
             case 'directory_and_filename':
                 // use the directory + filename as identifier
                 $vs_rep_idno = $d . '/' . $f;
                 break;
             default:
                 // use idno from form
                 $vs_rep_idno = $vs_representation_idno;
                 break;
         }
         $t_new_rep = null;
         if ($t_instance->getPrimaryKey() && $t_instance instanceof RepresentableBaseModel) {
             // found existing object
             $t_instance->setMode(ACCESS_WRITE);
             $t_new_rep = $t_instance->addRepresentation($vs_directory . '/' . $f, $vn_rep_type_id, $vn_locale_id, $vn_object_representation_status, $vn_object_representation_access, false, array('idno' => $vs_rep_idno), array('original_filename' => $f, 'returnRepresentation' => true, 'type_id' => $vn_rel_type_id));
             if ($t_instance->numErrors()) {
                 $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => _t("Error importing {$f} from {$vs_directory}: %1", join('; ', $t_instance->getErrors()))));
                 $va_errors[$vs_relative_directory . '/' . $f] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'errors' => $t_instance->errors(), 'message' => $vs_msg = _t("Error importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors())), 'status' => 'ERROR');
                 $o_log->logError($vs_msg);
                 $o_trans->rollback();
                 continue;
             } else {
                 if ($vb_delete_media_on_import) {
                     @unlink($vs_directory . '/' . $f);
                 }
             }
         } else {
             // should we create new record?
             if (in_array($vs_import_mode, array('TRY_TO_MATCH', 'DONT_MATCH'))) {
                 $t_instance->setMode(ACCESS_WRITE);
                 $t_instance->set('type_id', $vn_type_id);
                 $t_instance->set('locale_id', $vn_locale_id);
                 $t_instance->set('status', $vn_status);
                 $t_instance->set('access', $vn_access);
                 // for places, take first hierarchy we can find. in most setups there is but one. we might wanna make this configurable via setup screen at some point
                 if ($t_instance->hasField('hierarchy_id')) {
                     $va_hierarchies = $t_instance->getHierarchyList();
                     reset($va_hierarchies);
                     $vn_hierarchy_id = key($va_hierarchies);
                     $t_instance->set('hierarchy_id', $vn_hierarchy_id);
                 }
                 switch ($vs_idno_mode) {
                     case 'filename':
                         // use the filename as identifier
                         $t_instance->set('idno', $f);
                         break;
                     case 'filename_no_ext':
                         // use filename without extension as identifier
                         $f_no_ext = preg_replace('/\\.[^.\\s]{3,4}$/', '', $f);
                         $t_instance->set('idno', $f_no_ext);
                         break;
                     case 'directory_and_filename':
                         // use the directory + filename as identifier
                         $t_instance->set('idno', $d . '/' . $f);
                         break;
                     default:
                         // Calculate identifier using numbering plugin
                         $o_numbering_plugin = $t_instance->getIDNoPlugInInstance();
                         if (!($vs_sep = $o_numbering_plugin->getSeparator())) {
                             $vs_sep = '';
                         }
                         if (!is_array($va_idno_values = $o_numbering_plugin->htmlFormValuesAsArray('idno', null, false, false, true))) {
                             $va_idno_values = array();
                         }
                         $t_instance->set('idno', join($vs_sep, $va_idno_values));
                         // true=always set serial values, even if they already have a value; this let's us use the original pattern while replacing the serial value every time through
                         break;
                 }
                 $t_instance->insert();
                 if ($t_instance->numErrors()) {
                     $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => _t("Error creating new record while importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors()))));
                     $va_errors[$vs_relative_directory . '/' . $f] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'errors' => $t_instance->errors(), 'message' => $vs_msg = _t("Error creating new record while importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors())), 'status' => 'ERROR');
                     $o_log->logError($vs_msg);
                     $o_trans->rollback();
                     continue;
                 }
                 if ($t_instance->tableName() == 'ca_entities') {
                     // entity labels deserve special treatment
                     $t_instance->addLabel(array('surname' => $f), $vn_locale_id, null, true);
                 } else {
                     $t_instance->addLabel(array($t_instance->getLabelDisplayField() => $f), $vn_locale_id, null, true);
                 }
                 if ($t_instance->numErrors()) {
                     $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => _t("Error creating record label while importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors()))));
                     $va_errors[$vs_relative_directory . '/' . $f] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'errors' => $t_instance->errors(), 'message' => $vs_msg = _t("Error creating record label while importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors())), 'status' => 'ERROR');
                     $o_log->logError($vs_msg);
                     $o_trans->rollback();
                     continue;
                 }
                 $t_new_rep = $t_instance->addRepresentation($vs_directory . '/' . $f, $vn_rep_type_id, $vn_locale_id, $vn_object_representation_status, $vn_object_representation_access, true, array('idno' => $vs_rep_idno), array('original_filename' => $f, 'returnRepresentation' => true, 'type_id' => $vn_rel_type_id));
                 if ($t_instance->numErrors()) {
                     $o_eventlog->log(array("CODE" => 'ERR', "SOURCE" => "mediaImport", "MESSAGE" => _t("Error importing %1 from %2: ", $f, $vs_relative_directory, join('; ', $t_instance->getErrors()))));
                     $va_errors[$vs_relative_directory . '/' . $f] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'errors' => $t_instance->errors(), 'message' => $vs_msg = _t("Error importing %1 from %2: %3", $f, $vs_relative_directory, join('; ', $t_instance->getErrors())), 'status' => 'ERROR');
                     $o_log->logError($vs_msg);
                     $o_trans->rollback();
                     continue;
                 } else {
                     if ($vb_delete_media_on_import) {
                         @unlink($vs_directory . '/' . $f);
                     }
                 }
             }
         }
         if ($t_instance->getPrimaryKey()) {
             // Perform import of embedded metadata (if required)
             if ($vn_mapping_id) {
                 ca_data_importers::importDataFromSource($vs_directory . '/' . $f, $vn_mapping_id, array('logLevel' => $vs_log_level, 'format' => 'exif', 'forceImportForPrimaryKeys' => array($t_instance->getPrimaryKey(), 'transaction' => $o_trans)));
             }
             if ($vn_object_representation_mapping_id) {
                 ca_data_importers::importDataFromSource($vs_directory . '/' . $f, $vn_object_representation_mapping_id, array('logLevel' => $vs_log_level, 'format' => 'exif', 'forceImportForPrimaryKeys' => array($t_new_rep->getPrimaryKey()), 'transaction' => $o_trans));
             }
             $va_notices[$t_instance->getPrimaryKey()] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $t_instance->getLabelForDisplay(), 'message' => $vs_msg = _t('Imported %1 as %2', $f, $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD'))), 'status' => 'SUCCESS');
             $o_log->logInfo($vs_msg);
             if ($vn_set_id) {
                 $t_set->addItem($t_instance->getPrimaryKey(), null, $po_request->getUserID());
             }
             $o_batch_log->addItem($t_instance->getPrimaryKey(), $t_instance->errors());
             // Create relationships?
             if (is_array($va_create_relationship_for) && sizeof($va_create_relationship_for) && is_array($va_extracted_idnos_from_filename) && sizeof($va_extracted_idnos_from_filename)) {
                 foreach ($va_extracted_idnos_from_filename as $vs_idno) {
                     foreach ($va_create_relationship_for as $vs_rel_table) {
                         if (!isset($va_relationship_type_id_for[$vs_rel_table]) || !$va_relationship_type_id_for[$vs_rel_table]) {
                             continue;
                         }
                         $t_rel = $t_instance->getAppDatamodel()->getInstanceByTableName($vs_rel_table);
                         if ($t_rel->load(array($t_rel->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno))) {
                             $t_instance->addRelationship($vs_rel_table, $t_rel->getPrimaryKey(), $va_relationship_type_id_for[$vs_rel_table]);
                             if (!$t_instance->numErrors()) {
                                 $va_notices[$t_instance->getPrimaryKey() . '_rel'] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $vs_label = $t_instance->getLabelForDisplay(), 'message' => $vs_msg = _t('Added relationship between <em>%1</em> and %2 <em>%3</em>', $vs_label, $t_rel->getProperty('NAME_SINGULAR'), $t_rel->getLabelForDisplay()), 'status' => 'RELATED');
                                 $o_log->logInfo($vs_msg);
                             } else {
                                 $va_notices[$t_instance->getPrimaryKey()] = array('idno' => $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'label' => $vs_label = $t_instance->getLabelForDisplay(), 'message' => $vs_msg = _t('Could not add relationship between <em>%1</em> and %2 <em>%3</em>: %4', $vs_label, $t_rel->getProperty('NAME_SINGULAR'), $t_rel->getLabelForDisplay(), join("; ", $t_instance->getErrors())), 'status' => 'ERROR');
                                 $o_log->logError($vs_msg);
                             }
                         }
                     }
                 }
             }
         } else {
             $va_notices[$vs_relative_directory . '/' . $f] = array('idno' => '', 'label' => $f, 'message' => $vs_msg = $vs_import_mode == 'ALWAYS_MATCH' ? _t('Skipped %1 from %2 because it could not be matched', $f, $vs_relative_directory) : _t('Skipped %1 from %2', $f, $vs_relative_directory), 'status' => 'SKIPPED');
             $o_log->logInfo($vs_msg);
         }
         if (isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
             $ps_callback($po_request, $vn_c, $vn_num_items, _t("[%3/%4] Processing %1 (%3)", caTruncateStringWithEllipsis($vs_relative_directory, 20) . '/' . caTruncateStringWithEllipsis($f, 30), $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), $vn_c, $vn_num_items), $t_new_rep, time() - $vn_start_time, memory_get_usage(true), $vn_c, sizeof($va_errors));
         }
         $vn_c++;
     }
     if (isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $vn_num_items, $vn_num_items, _t("Processing completed"), null, time() - $vn_start_time, memory_get_usage(true), $vn_c, sizeof($va_errors));
     }
     $vn_elapsed_time = time() - $vn_start_time;
     if (isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback'])) {
         $va_general = array('elapsedTime' => $vn_elapsed_time, 'numErrors' => sizeof($va_errors), 'numProcessed' => $vn_c, 'batchSize' => $vn_num_items, 'table' => $t_instance->tableName(), 'set_id' => $t_set->getPrimaryKey(), 'setName' => $t_set->getLabelForDisplay());
         $ps_callback($po_request, $va_general, $va_notices, $va_errors);
     }
     $o_batch_log->close();
     if ($vb_we_set_transaction) {
         if (sizeof($va_errors) > 0) {
             $o_trans->rollback();
         } else {
             $o_trans->commit();
         }
     }
     $vs_set_name = $t_set->getLabelForDisplay();
     $vs_started_on = caGetLocalizedDate($vn_start_time);
     if (isset($pa_options['sendMail']) && $pa_options['sendMail']) {
         if ($vs_email = trim($po_request->user->get('email'))) {
             caSendMessageUsingView($po_request, array($vs_email => $po_request->user->get('fname') . ' ' . $po_request->user->get('lname')), __CA_ADMIN_EMAIL__, _t('[%1] Batch media import completed', $po_request->config->get('app_display_name')), 'batch_media_import_completed.tpl', array('notices' => $va_notices, 'errors' => $va_errors, 'directory' => $vs_relative_directory, 'numErrors' => sizeof($va_errors), 'numProcessed' => $vn_c, 'subjectNameSingular' => _t('file'), 'subjectNamePlural' => _t('files'), 'startedOn' => $vs_started_on, 'completedOn' => caGetLocalizedDate(time()), 'setName' => $vn_set_id ? $vs_set_name : null, 'elapsedTime' => caFormatInterval($vn_elapsed_time)));
         }
     }
     if (isset($pa_options['sendSMS']) && $pa_options['sendSMS']) {
         SMS::send($po_request->getUserID(), _t("[%1] Media import processing for directory %2 with %3 %4 begun at %5 is complete", $po_request->config->get('app_display_name'), $vs_relative_directory, $vn_num_items, $vn_num_items == 1 ? _t('file') : _t('files'), $vs_started_on));
     }
     $o_log->logInfo(_t("Media import processing for directory %1 with %2 %3 begun at %4 is complete", $vs_relative_directory, $vn_num_items, $vn_num_items == 1 ? _t('file') : _t('files')));
     return array('errors' => $va_errors, 'notices' => $va_notices, 'processing_time' => caFormatInterval($vn_elapsed_time));
 }
Beispiel #14
0
 /**
  * @param array $pa_options Options include:
  *		dontUseDefaultIcons = If set to true, write will fail rather than use default icons when preview can't be generated. Default is false – to use default icons.
  *
  */
 public function write($ps_filepath, $ps_mimetype, $pa_options = null)
 {
     if (!$this->handle) {
         return false;
     }
     $vb_dont_allow_default_icons = isset($pa_options['dontUseDefaultIcons']) && $pa_options['dontUseDefaultIcons'] ? true : false;
     # is mimetype valid?
     if (!($vs_ext = $this->info["EXPORT"][$ps_mimetype])) {
         $this->postError(1610, _t("Can't convert file to %1", $ps_mimetype), "WLPlugMediaOffice->write()");
         return false;
     }
     # write the file
     if ($ps_mimetype == "application/msword") {
         if (!copy($this->filepath, $ps_filepath . ".doc")) {
             $this->postError(1610, _t("Couldn't write file to '%1'", $ps_filepath), "WLPlugMediaOffice->write()");
             return false;
         }
     } else {
         if (!isset(WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath]) && $this->opb_libre_office_installed) {
             $vs_tmp_dir_path = caGetTempDirPath();
             $va_tmp = explode("/", $this->filepath);
             $vs_out_file = array_pop($va_tmp);
             putenv("HOME={$vs_tmp_dir_path}");
             // libreoffice will fail silently if you don't set this environment variable to a directory it can write to. Nice way to waste a day debugging. Yay!
             exec($this->ops_libreoffice_path . " --nologo --nofirststartwizard --headless -convert-to pdf " . caEscapeShellArg($this->filepath) . "  -outdir " . caEscapeShellArg($vs_tmp_dir_path) . " 2>&1", $va_output, $vn_return);
             exec($this->ops_libreoffice_path . " --nologo --nofirststartwizard --headless -convert-to html " . caEscapeShellArg($this->filepath) . "  -outdir " . caEscapeShellArg($vs_tmp_dir_path) . " 2>&1", $va_output, $vn_return);
             $va_out_file = explode(".", $vs_out_file);
             if (sizeof($va_out_file) > 1) {
                 array_pop($va_out_file);
             }
             $this->handle['content'] = strip_tags(file_get_contents("{$vs_tmp_dir_path}/" . join(".", $va_out_file) . ".html"));
             $va_out_file[] = 'pdf';
             WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath] = "{$vs_tmp_dir_path}/" . join(".", $va_out_file);
             $o_media = new Media();
             if ($o_media->read(WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath])) {
                 $this->set('width', $this->ohandle["width"] = $o_media->get('width'));
                 $this->set('height', $this->ohandle["height"] = $o_media->get('height'));
                 $this->set('resolution', $this->ohandle["resolution"] = $o_media->get('resolution'));
             }
         }
         if ($vs_media = WLPlugMediaOffice::$s_pdf_conv_cache[$this->filepath]) {
             switch ($ps_mimetype) {
                 case 'application/pdf':
                     $o_media = new Media();
                     $o_media->read($vs_media);
                     $o_media->set('version', $this->get('version'));
                     $o_media->write($ps_filepath, $ps_mimetype, array());
                     $vs_filepath_with_extension = $ps_filepath . ".pdf";
                     break;
                 case 'image/jpeg':
                     $o_media = new Media();
                     $o_media->read($vs_media);
                     $o_media->set('version', $this->get('version'));
                     foreach ($this->opa_transformations as $va_transform) {
                         $o_media->transform($va_transform['op'], $va_transform['params']);
                     }
                     $o_media->write($ps_filepath, $ps_mimetype, array());
                     $this->set('width', $o_media->get('width'));
                     $this->set('height', $o_media->get('height'));
                     $vs_filepath_with_extension = $ps_filepath . ".jpg";
                     break;
             }
         }
         # use default media icons
         if (!file_exists($vs_filepath_with_extension)) {
             // always jpegs
             return $vb_dont_allow_default_icons ? null : __CA_MEDIA_DOCUMENT_DEFAULT_ICON__;
         }
     }
     $this->properties["mimetype"] = $ps_mimetype;
     $this->properties["filesize"] = filesize($ps_filepath . "." . $vs_ext);
     //$this->properties["typename"] = $this->typenames[$ps_mimetype];
     return $ps_filepath . "." . $vs_ext;
 }
Beispiel #15
0
 public function parse($ps_filepath)
 {
     if (!function_exists("simplexml_load_file")) {
         return null;
     }
     $this->init();
     // Is file a KMZ file?
     $o_unzip = new UnZipFile($ps_filepath);
     $vs_tmp_dirname = tempnam(caGetTempDirPath(), 'kml');
     @unlink($vs_tmp_dirname);
     @mkdir($vs_tmp_dirname);
     if ($o_unzip->extract($vs_tmp_dirname, 'doc.kml')) {
         if (file_exists($vs_tmp_dirname . '/doc.kml')) {
             $ps_filepath = $vs_tmp_dirname . '/doc.kml';
         } else {
             return false;
         }
     }
     $o_kml = @simplexml_load_file($ps_filepath);
     if (!$o_kml) {
         return false;
     }
     caRemoveDirectory($vs_tmp_dirname, true);
     //
     // Placemarks
     //
     $va_namespaces = $o_kml->getNamespaces(true);
     foreach ($va_namespaces as $vs_prefix => $vs_schema_url) {
         $o_kml->registerXPathNamespace($vs_prefix ? $vs_prefix : 'g', $vs_schema_url);
     }
     $va_placemarks = $o_kml->xpath('//g:Placemark');
     $this->opa_filedata['placemarks'] = array();
     foreach ($va_placemarks as $va_placemark) {
         $vs_name = '' . $va_placemark->name[0];
         $vs_description = '' . $va_placemark->description[0];
         if (isset($va_placemark->Point)) {
             $vs_coord = $va_placemark->Point->coordinates;
             $va_tmp = explode(',', $vs_coord);
             $this->opa_filedata['placemarks'][] = array('name' => $vs_name, 'type' => 'POINT', 'description' => $vs_description, 'latitude' => $va_tmp[1], 'longitude' => $va_tmp[0]);
         } else {
             if (isset($va_placemark->LineString) && isset($va_placemark->LineString->coordinates)) {
                 $vs_coords = trim($va_placemark->LineString->coordinates);
                 $va_coord_lines = preg_split("/[ \n\r]+/", $vs_coords);
             } else {
                 if (isset($va_placemark->Polygon) && isset($va_placemark->Polygon->outerBoundaryIs) && isset($va_placemark->Polygon->outerBoundaryIs->LinearRing) && isset($va_placemark->Polygon->outerBoundaryIs->LinearRing->coordinates)) {
                     $vs_coords = trim($va_placemark->Polygon->outerBoundaryIs->LinearRing->coordinates);
                     $va_coord_lines = preg_split("/[ \n\r]+/", $vs_coords);
                 }
             }
             if (sizeof($va_coord_lines) > 0) {
                 $va_coord_list = array();
                 foreach ($va_coord_lines as $vs_coord_line) {
                     $va_tmp = explode(',', $vs_coord_line);
                     $va_coord_list[] = array('latitude' => $va_tmp[1], 'longitude' => $va_tmp[0]);
                 }
                 $this->opa_filedata['placemarks'][] = array('name' => $vs_name, 'type' => 'PATH', 'description' => $vs_description, 'coordinates' => $va_coord_list);
             }
         }
     }
     return true;
 }
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     $vb_is_file_path = false;
     $vb_is_user_media = false;
     if (is_array($ps_value) && $ps_value['_uploaded_file'] && file_exists($ps_value['tmp_name']) && filesize($ps_value['tmp_name']) > 0 || ($vb_is_file_path = file_exists($ps_value)) || ($vb_is_file_path = preg_match("!^userMedia[\\d]+/!", $ps_value)) || ($vb_is_user_media = preg_match("!^userMedia[\\d]+/!", $ps_value))) {
         // got file
         $vs_original_name = null;
         if ($vb_is_user_media) {
             $vb_is_file_path = true;
             $o_config = Configuration::load();
             if (!is_writeable($vs_tmp_directory = $o_config->get('ajax_media_upload_tmp_directory'))) {
                 $vs_tmp_directory = caGetTempDirPath();
             }
             $ps_value = "{$vs_tmp_directory}/{$ps_value}";
             // read metadata
             if (file_exists("{$ps_value}_metadata")) {
                 if (is_array($va_tmp_metadata = json_decode(file_get_contents("{$ps_value}_metadata"), true))) {
                     $vs_original_name = $va_tmp_metadata['original_filename'];
                 }
             }
         }
         if ($vb_is_file_path) {
             return array('value_blob' => $ps_value, 'value_longtext2' => $vs_original_name ? $vs_original_name : $ps_value, 'value_decimal1' => null, 'value_decimal2' => null, '_file' => true);
         } else {
             return array('value_blob' => $ps_value['tmp_name'], 'value_longtext2' => $ps_value['name'], 'value_decimal1' => null, 'value_decimal2' => null, '_file' => true);
         }
     } else {
         //$this->postError(1970, _t('No file uploaded'), 'FileAttributeValue->parseValue()');
         //return false;
     }
     return array('value_blob' => $this->ops_file_data, 'value_longtext2' => $this->ops_file_original_name, 'value_decimal1' => null, 'value_decimal2' => null, '_dont_save' => true);
 }
 /**
  * Update annotation. If time code of annotation has changed media preview will be regenerated. 
  * You can force the media preview to be regenerated whether the time code has changed or not
  * by passing the 'forcePreviewGeneration' option.
  *
  * @param array $pa_options An array of options:
  *		forcePreviewGeneration = if set preview media will be regenerated whether time code has changed or not. Default is false.
  * @return bool True on success, false on failure
  */
 public function update($pa_options = null)
 {
     $this->set('type_code', $vs_type = $this->getAnnotationType());
     if (!$this->opo_annotations_properties->validate()) {
         $this->errors = $this->opo_annotations_properties->errors;
         return false;
     }
     $this->set('props', $this->opo_annotations_properties->getPropertyValues());
     if (!$this->getAppConfig()->get('dont_generate_annotation_previews') && $this->getPrimaryKey() && ($this->changed('props') || isset($pa_options['forcePreviewGeneration']) && $pa_options['forcePreviewGeneration'])) {
         $vs_start = $this->getPropertyValue('startTimecode');
         $vs_end = $this->getPropertyValue('endTimecode');
         $va_data['start'] = $vs_start;
         $va_data['end'] = $vs_end;
         $t_rep = new ca_object_representations($this->get('representation_id'));
         if (($vs_file = $t_rep->getMediaPath('media', 'original')) && file_exists($vs_file)) {
             $o_media = new Media();
             if ($o_media->read($vs_file)) {
                 if ($o_media->writeClip($vs_file = tempnam(caGetTempDirPath(), 'annotationPreview'), $vs_start, $vs_end)) {
                     $this->set('preview', $vs_file);
                 }
             }
         }
     }
     $vn_rc = parent::update($pa_options);
     if (!$this->numErrors()) {
         $this->opo_annotations_properties = $this->loadProperties($vs_type);
     }
     if ($vs_file) {
         @unlink($vs_file);
     }
     return $vn_rc;
 }
/**
 * Embed media metadata into given file. Embedding is performed on a copy of the file and placed into the
 * system tmp directory. The given file is never modified.
 *
 * @param string $ps_file The file to embed metadata into
 * @param string $ps_table Table name of the subject record. This is used to figure out the appropriate mapping to use from media_metadata.conf
 * @param int $pn_pk Primary key of the subject record. This is used to run the export for the right record.
 * @param string $ps_type_code Optional type code for the subject record
 * @param int $pn_rep_pk Primary key of the subject representation.
 * 		If there are export mapping for object representations, we run them after the mapping for the subject table.
 * 		Fields that get exported here should overwrite fields from the subject table export.
 * @param string $ps_rep_type_code type code for object representation
 * @return string File name of a temporary file with the embedded metadata, false on failure
 */
function caEmbedMediaMetadataIntoFile($ps_file, $ps_table, $pn_pk, $ps_type_code, $pn_rep_pk, $ps_rep_type_code)
{
    require_once __CA_MODELS_DIR__ . '/ca_data_exporters.php';
    if (!caExifToolInstalled()) {
        return false;
    }
    // we need exiftool for embedding
    $vs_path_to_exif_tool = caGetExternalApplicationPath('exiftool');
    if (!file_exists($ps_file)) {
        return false;
    }
    if (!preg_match("/^image\\//", mime_content_type($ps_file))) {
        return false;
    }
    // Don't try to embed in files other than images
    // make a temporary copy (we won't touch the original)
    copy($ps_file, $vs_tmp_filepath = caGetTempDirPath() . "/" . time() . md5($ps_file));
    //
    // SUBJECT TABLE
    //
    if ($vs_subject_table_export = caExportMediaMetadataForRecord($ps_table, $ps_type_code, $pn_pk)) {
        $vs_export_filename = caGetTempFileName('mediaMetadataSubjExport', 'xml');
        if (@file_put_contents($vs_export_filename, $vs_subject_table_export) === false) {
            return false;
        }
        exec("{$vs_path_to_exif_tool} -tagsfromfile {$vs_export_filename} -all:all " . caEscapeShellArg($vs_tmp_filepath), $va_output, $vn_return);
        @unlink($vs_export_filename);
        @unlink("{$vs_tmp_filepath}_original");
    }
    //
    // REPRESENTATION
    //
    if ($vs_representation_Export = caExportMediaMetadataForRecord('ca_object_representations', $ps_rep_type_code, $pn_rep_pk)) {
        $vs_export_filename = caGetTempFileName('mediaMetadataRepExport', 'xml');
        if (@file_put_contents($vs_export_filename, $vs_representation_Export) === false) {
            return false;
        }
        exec("{$vs_path_to_exif_tool} -tagsfromfile {$vs_export_filename} -all:all " . caEscapeShellArg($vs_tmp_filepath), $va_output, $vn_return);
        @unlink($vs_export_filename);
        @unlink("{$vs_tmp_filepath}_original");
    }
    return $vs_tmp_filepath;
}
 /**
  * Export a single record using the mapping defined by this exporter and return as string
  * @param string $ps_exporter_code defines the exporter to use
  * @param int $pn_record_id Primary key of the record to export. Record type is determined by the table_num field for this exporter.
  * @param array $pa_options
  *        singleRecord = Gives a signal to the export format implementation that this is a single record export. For certain formats
  *        	this might trigger different behavior, for instance the XML export format prepends the item-level output with <?xml ... ?>
  *        	in those cases.
  *        rdfMode = Signals the implementation that this is an RDF mode export
  *        logDirectory = path to directory where logs should be written
  *		  logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness:
  *			KLogger::EMERG = Emergency messages (system is unusable)
  *			KLogger::ALERT = Alert messages (action must be taken immediately)
  *			KLogger::CRIT = Critical conditions
  *			KLogger::ERR = Error conditions
  *			KLogger::WARN = Warnings
  *			KLogger::NOTICE = Notices (normal but significant conditions)
  *			KLogger::INFO = Informational messages
  *			KLogger::DEBUG = Debugging messages
  *		  logger = Optional ready-to-use instance of KLogger to use for logging/debugging
  * @return string Exported record as string
  */
 public static function exportRecord($ps_exporter_code, $pn_record_id, $pa_options = array())
 {
     // The variable cache is valid for the whole record export.
     // It's being modified in ca_data_exporters::processExporterItem
     // and then reset here if we move on to the next record.
     ca_data_exporters::$s_variables = array();
     $o_log = caGetOption('logger', $pa_options);
     // only set up new logging facilities if no existing one has been passed down
     if (!$o_log || !$o_log instanceof KLogger) {
         $vs_log_dir = caGetOption('logDirectory', $pa_options);
         if (!file_exists($vs_log_dir) || !is_writable($vs_log_dir)) {
             $vs_log_dir = caGetTempDirPath();
         }
         if (!($vn_log_level = caGetOption('logLevel', $pa_options))) {
             $vn_log_level = KLogger::INFO;
         }
         $o_log = new KLogger($vs_log_dir, $vn_log_level);
     }
     // make sure we pass logger to item processor
     $pa_options['logger'] = $o_log;
     ca_data_exporters::$s_instance_cache = array();
     $t_exporter = ca_data_exporters::loadExporterByCode($ps_exporter_code);
     if (!$t_exporter) {
         $o_log->logError(_t("Failed to load exporter with code '%1' for item with ID %2", $ps_exporter_code, $pn_record_id));
         return false;
     }
     $o_log->logInfo(_t("Successfully loaded exporter with code '%1' for item with ID %2", $ps_exporter_code, $pn_record_id));
     $va_export = array();
     foreach ($t_exporter->getTopLevelItems() as $va_item) {
         $va_export = array_merge($va_export, $t_exporter->processExporterItem($va_item['item_id'], $t_exporter->get('table_num'), $pn_record_id, $pa_options));
     }
     $o_log->logInfo(_t("The export tree for exporter code '%1' and item with ID %2 is now ready to be processed by the export format (i.e. transformed to XML, for example).", $ps_exporter_code, $pn_record_id));
     $o_log->logDebug(print_r($va_export, true));
     // we may wanna auto-load this?
     switch ($t_exporter->getSetting('exporter_format')) {
         case 'XML':
             $o_export = new ExportXML();
             break;
         case 'MARC':
             $o_export = new ExportMARC();
             break;
         case 'CSV':
             $o_export = new ExportCSV();
             break;
         case 'ExifTool':
             $o_export = new ExportExifTool();
             break;
         default:
             return;
     }
     $o_export->setLogger($o_log);
     // if someone wants to mangle the whole tree ... well, go right ahead
     $o_manager = new ApplicationPluginManager();
     $o_manager->hookExportRecord(array('exporter_instance' => $t_exporter, 'record_id' => $pn_record_id, 'export' => &$va_export));
     $pa_options['settings'] = $t_exporter->getSettings();
     $vs_wrap_before = $t_exporter->getSetting('wrap_before_record');
     $vs_wrap_after = $t_exporter->getSetting('wrap_after_record');
     if ($vs_wrap_before || $vs_wrap_after) {
         $pa_options['singleRecord'] = false;
     }
     $vs_export = $o_export->processExport($va_export, $pa_options);
     if (strlen($vs_wrap_before) > 0) {
         $vs_export = $vs_wrap_before . "\n" . $vs_export;
     }
     if (strlen($vs_wrap_after) > 0) {
         $vs_export = $vs_export . "\n" . $vs_wrap_after;
     }
     return $vs_export;
 }
 /**
  * 
  *
  * @param string $ps_source
  * @param string $ps_mapping
  * @param array $pa_options
  *		user_id = user to execute import for
  *		description = Text describing purpose of import to be logged.
  *		showCLIProgressBar = Show command-line progress bar. Default is false.
  *		format = Format of data being imported. MANDATORY
  *		useNcurses = Use ncurses library to format output
  *		logDirectory = path to directory where logs should be written
  *		logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness:
  *			KLogger::EMERG = Emergency messages (system is unusable)
  *			KLogger::ALERT = Alert messages (action must be taken immediately)
  *			KLogger::CRIT = Critical conditions
  *			KLogger::ERR = Error conditions
  *			KLogger::WARN = Warnings
  *			KLogger::NOTICE = Notices (normal but significant conditions)
  *			KLogger::INFO = Informational messages
  *			KLogger::DEBUG = Debugging messages
  *		dryRun = do import but don't actually save data
  *		environment = an array of environment values to provide to the import process. The keys manifest themselves as mappable tags.
  *		forceImportForPrimaryKeys = list of primary key ids to force mapped source data into. The number of keys passed should equal or exceed the number of rows in the source data. [Default is empty] 
  *		transaction = transaction to perform import within. Will not be used if noTransaction option is set. [Default is to create a new transaction]
  *		noTransaction = don't wrap the import in a transaction. [Default is false]
  */
 public static function importDataFromSource($ps_source, $ps_mapping, $pa_options = null)
 {
     ca_data_importers::$s_num_import_errors = 0;
     ca_data_importers::$s_num_records_processed = 0;
     ca_data_importers::$s_num_records_skipped = 0;
     ca_data_importers::$s_import_error_list = array();
     $opa_app_plugin_manager = new ApplicationPluginManager();
     $va_notices = $va_errors = array();
     $pb_no_transaction = caGetOption('noTransaction', $pa_options, false, array('castTo' => 'bool'));
     $pa_force_import_for_primary_keys = caGetOption('forceImportForPrimaryKeys', $pa_options, null);
     if (!($t_mapping = ca_data_importers::mappingExists($ps_mapping))) {
         return null;
     }
     $o_event = ca_data_import_events::newEvent(isset($pa_options['user_id']) ? $pa_options['user_id'] : null, $pa_options['format'], $ps_source, isset($pa_options['description']) ? $pa_options['description'] : '');
     $o_trans = null;
     if (!$pb_no_transaction) {
         if (!($o_trans = caGetOption('transaction', $pa_options, null))) {
             $o_trans = new Transaction();
         }
         $t_mapping->setTransaction($o_trans);
     }
     $po_request = caGetOption('request', $pa_options, null);
     $pb_dry_run = caGetOption('dryRun', $pa_options, false);
     $pn_file_number = caGetOption('fileNumber', $pa_options, 0);
     $pn_number_of_files = caGetOption('numberOfFiles', $pa_options, 1);
     $o_config = Configuration::load();
     if (!is_array($pa_options) || !isset($pa_options['logLevel']) || !$pa_options['logLevel']) {
         $pa_options['logLevel'] = KLogger::INFO;
     }
     if (!is_array($pa_options) || !isset($pa_options['logDirectory']) || !$pa_options['logDirectory'] || !file_exists($pa_options['logDirectory'])) {
         if (!($pa_options['logDirectory'] = $o_config->get('batch_metadata_import_log_directory'))) {
             $pa_options['logDirectory'] = ".";
         }
     }
     if (!is_writeable($pa_options['logDirectory'])) {
         $pa_options['logDirectory'] = caGetTempDirPath();
     }
     $o_log = new KLogger($pa_options['logDirectory'], $pa_options['logLevel']);
     $vb_show_cli_progress_bar = isset($pa_options['showCLIProgressBar']) && $pa_options['showCLIProgressBar'] ? true : false;
     $o_progress = caGetOption('progressBar', $pa_options, new ProgressBar('WebUI'));
     if ($vb_show_cli_progress_bar) {
         $o_progress->setMode('CLI');
         $o_progress->set('outputToTerminal', true);
     }
     if ($vb_use_ncurses = isset($pa_options['useNcurses']) && $pa_options['useNcurses'] ? true : false) {
         $vb_use_ncurses = caCLIUseNcurses();
     }
     $vn_error_window_height = null;
     $vn_max_x = $vn_max_y = null;
     if ($vb_use_ncurses) {
         ncurses_init();
         $r_screen = ncurses_newwin(0, 0, 0, 0);
         ncurses_border(0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_getmaxyx($r_screen, $vn_max_y, $vn_max_x);
         $vn_error_window_height = $vn_max_y - 8;
         $r_errors = ncurses_newwin($vn_error_window_height, $vn_max_x - 4, 4, 2);
         ncurses_wborder($r_errors, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_errors, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_errors, 0, 1, _t(" Recent errors "));
         ncurses_wattroff($r_errors, NCURSES_A_REVERSE);
         $r_progress = ncurses_newwin(3, $vn_max_x - 4, $vn_max_y - 4, 2);
         ncurses_wborder($r_progress, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_progress, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_progress, 0, 1, _t(" Progress "));
         ncurses_wattroff($r_progress, NCURSES_A_REVERSE);
         $r_status = ncurses_newwin(3, $vn_max_x - 4, 1, 2);
         ncurses_wborder($r_status, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_status, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_status, 0, 1, _t(" Import status "));
         ncurses_wattroff($r_status, NCURSES_A_REVERSE);
         ncurses_refresh(0);
         ncurses_wrefresh($r_progress);
         ncurses_wrefresh($r_errors);
         ncurses_wrefresh($r_status);
     }
     $o_log->logInfo(_t('Started import of %1 using mapping %2', $ps_source, $t_mapping->get("importer_code")));
     $t = new Timer();
     $vn_start_time = time();
     $va_log_import_error_opts = array('startTime' => $vn_start_time, 'window' => $r_errors, 'log' => $o_log, 'request' => $po_request, 'progressCallback' => isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback']) ? $ps_callback : null, 'reportCallback' => isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback']) ? $ps_callback : null);
     global $g_ui_locale_id;
     // constant locale set by index.php for web requests
     $vn_locale_id = isset($pa_options['locale_id']) && (int) $pa_options['locale_id'] ? (int) $pa_options['locale_id'] : $g_ui_locale_id;
     $o_dm = $t_mapping->getAppDatamodel();
     $o_progress->start(_t('Reading %1', $ps_source), array('window' => $r_progress));
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, 100, _t('Reading %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors);
     }
     // Open file
     $ps_format = isset($pa_options['format']) && $pa_options['format'] ? $pa_options['format'] : null;
     if (!($o_reader = $t_mapping->getDataReader($ps_source, $ps_format))) {
         ca_data_importers::logImportError(_t("Could not open source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts);
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     if (!$o_reader->read($ps_source, array('basePath' => $t_mapping->getSetting('basePath')))) {
         ca_data_importers::logImportError(_t("Could not read source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts);
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     $o_log->logDebug(_t('Finished reading input source at %1 seconds', $t->getTime(4)));
     $vn_num_items = $o_reader->numRows();
     $o_progress->setTotal($vn_num_items);
     $o_progress->start(_t('Importing from %1', $ps_source), array('window' => $r_progress));
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, $vn_num_items, _t('Importing from %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors);
     }
     // What are we importing?
     $vn_table_num = $t_mapping->get('table_num');
     if (!($t_subject = $o_dm->getInstanceByTableNum($vn_table_num))) {
         // invalid table
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     $t_subject->setTransaction($o_trans);
     $vs_subject_table_name = $t_subject->tableName();
     $t_label = $t_subject->getLabelTableInstance();
     $t_label->setTransaction($o_trans);
     $vs_label_display_fld = $t_subject->getLabelDisplayField();
     $vs_subject_table = $t_subject->tableName();
     $vs_type_id_fld = $t_subject->getTypeFieldName();
     $vs_idno_fld = $t_subject->getProperty('ID_NUMBERING_ID_FIELD');
     // get mapping rules
     $va_mapping_rules = $t_mapping->getRules();
     // get mapping groups
     $va_mapping_groups = $t_mapping->getGroups();
     $va_mapping_items = $t_mapping->getItems();
     //
     // Mapping-level settings
     //
     $vs_type_mapping_setting = $t_mapping->getSetting('type');
     $vn_num_initial_rows_to_skip = $t_mapping->getSetting('numInitialRowsToSkip');
     if (!in_array($vs_import_error_policy = $t_mapping->getSetting('errorPolicy'), array('ignore', 'stop'))) {
         $vs_import_error_policy = 'ignore';
     }
     if (!in_array($vs_existing_record_policy = $t_mapping->getSetting('existingRecordPolicy'), array('none', 'skip_on_idno', 'skip_on_preferred_labels', 'merge_on_idno', 'merge_on_preferred_labels', 'merge_on_idno_and_preferred_labels', 'merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace', 'overwrite_on_idno', 'overwrite_on_preferred_labels', 'overwrite_on_idno_and_preferred_labels'))) {
         $vs_existing_record_policy = 'none';
     }
     // Analyze mapping for figure out where type, idno, preferred label and other mandatory fields are coming from
     $vn_type_id_mapping_item_id = $vn_idno_mapping_item_id = null;
     $va_preferred_label_mapping_ids = array();
     $va_mandatory_field_mapping_ids = array();
     $va_mandatory_fields = $t_subject->getMandatoryFields();
     foreach ($va_mapping_items as $vn_item_id => $va_item) {
         $vs_destination = $va_item['destination'];
         if (sizeof($va_dest_tmp = explode(".", $vs_destination)) >= 2) {
             if ($va_dest_tmp[0] == $vs_subject_table && $va_dest_tmp[1] == 'preferred_labels') {
                 if (isset($va_dest_tmp[2])) {
                     $va_preferred_label_mapping_ids[$vn_item_id] = $va_dest_tmp[2];
                 } else {
                     $va_preferred_label_mapping_ids[$vn_item_id] = $vs_label_display_fld;
                 }
                 continue;
             }
         }
         switch ($vs_destination) {
             case 'representation_id':
                 if ($vs_subject_table == 'ca_representation_annotations') {
                     $vn_type_id_mapping_item_id = $vn_item_id;
                 }
                 break;
             case "{$vs_subject_table}.{$vs_type_id_fld}":
                 $vn_type_id_mapping_item_id = $vn_item_id;
                 break;
             case "{$vs_subject_table}.{$vs_idno_fld}":
                 $vn_idno_mapping_item_id = $vn_item_id;
                 break;
         }
         foreach ($va_mandatory_fields as $vs_mandatory_field) {
             if ($vs_mandatory_field == $vs_type_id_fld) {
                 continue;
             }
             // type is handled separately
             if ($vs_destination == "{$vs_subject_table}.{$vs_mandatory_field}") {
                 $va_mandatory_field_mapping_ids[$vs_mandatory_field] = $vn_item_id;
             }
         }
     }
     $va_items_by_group = array();
     foreach ($va_mapping_items as $vn_item_id => $va_item) {
         $va_items_by_group[$va_item['group_id']][$va_item['item_id']] = $va_item;
     }
     $o_log->logDebug(_t('Finished analyzing mapping at %1 seconds', $t->getTime(4)));
     //
     // Set up environment
     //
     $va_environment = caGetOption('environment', $pa_options, array(), array('castTo' => 'array'));
     if (is_array($va_environment_config = $t_mapping->getEnvironment())) {
         foreach ($va_environment_config as $vn_i => $va_environment_item) {
             $va_env_tmp = explode("|", $va_environment_item['value']);
             if (!($o_env_reader = $t_mapping->getDataReader($ps_source, $ps_format))) {
                 break;
             }
             if (!$o_env_reader->read($ps_source, array('basePath' => ''))) {
                 break;
             }
             $o_env_reader->nextRow();
             switch (sizeof($va_env_tmp)) {
                 case 1:
                     $vs_env_value = $o_env_reader->get($va_environment_item['value'], array('returnAsArray' => false));
                     break;
                 case 2:
                     $vn_seek = (int) $va_env_tmp[0];
                     $o_reader->seek($vn_seek > 0 ? $vn_seek - 1 : $vn_seek);
                     $o_env_reader->nextRow();
                     $vs_env_value = $o_env_reader->get($va_env_tmp[1], array('returnAsArray' => false));
                     $o_reader->seek(0);
                     break;
                 default:
                     $vs_env_value = $va_environment_item['value'];
                     break;
             }
             $va_environment[$va_environment_item['name']] = $vs_env_value;
         }
     }
     //
     // Run through rows
     //
     $vn_row = 0;
     ca_data_importers::$s_num_records_processed = 0;
     while ($o_reader->nextRow()) {
         $va_mandatory_field_values = array();
         $vs_preferred_label_for_log = null;
         if ($vn_row < $vn_num_initial_rows_to_skip) {
             // skip over initial header rows
             $vn_row++;
             continue;
         }
         $vn_row++;
         $t->startTimer();
         $o_log->logDebug(_t('Started reading row %1 at %2 seconds', $vn_row, $t->getTime(4)));
         $t_subject = $o_dm->getInstanceByTableNum($vn_table_num);
         if ($o_trans) {
             $t_subject->setTransaction($o_trans);
         }
         $t_subject->setMode(ACCESS_WRITE);
         // Update status display
         if ($vb_use_ncurses && ca_data_importers::$s_num_records_processed) {
             ncurses_mvwaddstr($r_status, 1, 2, _t("Items processed/skipped: %1/%2", ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped) . str_repeat(" ", 5) . _t("Errors: %1 (%2)", ca_data_importers::$s_num_import_errors, sprintf("%3.1f", ca_data_importers::$s_num_import_errors / ca_data_importers::$s_num_records_processed * 100) . "%") . str_repeat(" ", 6) . _t("Mapping: %1", $ps_mapping) . str_repeat(" ", 5) . _t("Source: %1", $ps_source) . str_repeat(" ", 5) . date("Y-m-d H:i:s") . str_repeat(" ", 5));
             ncurses_refresh(0);
             ncurses_wrefresh($r_status);
         }
         //
         // Get data for current row
         //
         $va_row = array_merge($o_reader->getRow(), $va_environment);
         //
         // Apply rules
         //
         foreach ($va_mapping_rules as $va_rule) {
             if (!isset($va_rule['trigger']) || !$va_rule['trigger']) {
                 continue;
             }
             if (!isset($va_rule['actions']) || !is_array($va_rule['actions']) || !sizeof($va_rule['actions'])) {
                 continue;
             }
             $vm_ret = ExpressionParser::evaluate($va_rule['trigger'], $va_row);
             if (!ExpressionParser::hadError() && (bool) $vm_ret) {
                 foreach ($va_rule['actions'] as $va_action) {
                     if (!is_array($va_action) && strtolower($va_action) == 'skip') {
                         $va_action = array('action' => 'skip');
                     }
                     switch ($vs_action_code = strtolower($va_action['action'])) {
                         case 'set':
                             $va_row[$va_action['target']] = $va_action['value'];
                             // TODO: transform value using mapping rules?
                             break;
                         case 'skip':
                         default:
                             if ($vs_action_code != 'skip') {
                                 $o_log->logInfo(_t('Row was skipped using rule "%1" with default action because an invalid action ("%2") was specified', $va_rule['trigger'], $vs_action_code));
                             } else {
                                 $o_log->logDebug(_t('Row was skipped using rule "%1" with action "%2"', $va_rule['trigger'], $vs_action_code));
                             }
                             continue 4;
                             break;
                     }
                 }
             }
         }
         //
         // Perform mapping and insert
         //
         // Get minimal info for imported row (type_id, idno, label)
         // Get type
         if ($vn_type_id_mapping_item_id) {
             // Type is specified in row
             $vs_type = ca_data_importers::getValueFromSource($va_mapping_items[$vn_type_id_mapping_item_id], $o_reader, array('environment' => $va_environment));
         } else {
             // Type is constant for all rows
             $vs_type = $vs_type_mapping_setting;
         }
         // Get idno
         $vs_idno = null;
         if ($vn_idno_mapping_item_id) {
             // idno is specified in row
             $vs_idno = ca_data_importers::getValueFromSource($va_mapping_items[$vn_idno_mapping_item_id], $o_reader, array('environment' => $va_environment));
             if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && !strlen($vs_idno)) {
                 $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['default'];
             }
             if (!is_array($vs_idno) && $vs_idno[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vs_idno)) {
                 // Parse placeholder when it's at the beginning of the value
                 if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vs_idno, $va_row, $va_item, null, array('reader' => $o_reader, 'returnAsString' => true)))) {
                     $vs_idno = $vm_parsed_val;
                 }
             }
             // Apply prefix/suffix *AFTER* setting default
             if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'])) {
                 $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'] . $vs_idno;
             }
             if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix'])) {
                 $vs_idno .= $va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix'];
             }
             if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'])) {
                 $vs_idno = caProcessTemplate($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'], $va_row);
             }
         } else {
             $vs_idno = "%";
         }
         $vb_idno_is_template = (bool) preg_match('![%]+!', $vs_idno);
         // get preferred labels
         $va_pref_label_values = array();
         foreach ($va_preferred_label_mapping_ids as $vn_preferred_label_mapping_id => $vs_preferred_label_mapping_fld) {
             $vs_label_val = ca_data_importers::getValueFromSource($va_mapping_items[$vn_preferred_label_mapping_id], $o_reader, array('environment' => $va_environment));
             // If a template is specified format the label value with it so merge-on-preferred_label doesn't fail
             if (isset($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'])) {
                 $vs_label_val = caProcessTemplate($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'], $va_row);
             }
             $va_pref_label_values[$vs_preferred_label_mapping_fld] = $vs_label_val;
         }
         $vs_display_label = isset($va_pref_label_values[$vs_label_display_fld]) ? $va_pref_label_values[$vs_label_display_fld] : $vs_idno;
         //
         // Look for existing record?
         //
         if (is_array($pa_force_import_for_primary_keys) && sizeof($pa_force_import_for_primary_keys) > 0) {
             $vn_id = array_shift($pa_force_import_for_primary_keys);
             if (!$t_subject->load($vn_id)) {
                 $o_log->logInfo(_t('[%1] Skipped import because of forced primary key \'%1\' does not exist', $vn_id));
                 ca_data_importers::$s_num_records_skipped++;
                 continue;
                 // skip because primary key does not exist
             }
         } elseif ($vs_existing_record_policy != 'none') {
             switch ($vs_existing_record_policy) {
                 case 'skip_on_idno':
                     if (!$vb_idno_is_template) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                             ca_data_importers::$s_num_records_skipped++;
                             continue 2;
                             // skip because idno matched
                         }
                     }
                     break;
                 case 'skip_on_preferred_labels':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                         ca_data_importers::$s_num_records_skipped++;
                         continue 2;
                         // skip because label matched
                     }
                     break;
                 case 'merge_on_idno_and_preferred_labels':
                 case 'merge_on_idno':
                 case 'merge_on_idno_and_preferred_labels_with_replace':
                 case 'merge_on_idno_with_replace':
                     if (!$vb_idno_is_template) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $t_subject->load($va_ids[0]);
                             $o_log->logInfo(_t('[%1] Merged with existing record matched on identifer by policy %2', $vs_idno, $vs_existing_record_policy));
                             break;
                         }
                     }
                     if (in_array($vs_existing_record_policy, array('merge_on_idno', 'merge_on_idno_with_replace'))) {
                         break;
                     }
                     // fall through if merge_on_idno_and_preferred_labels
                 // fall through if merge_on_idno_and_preferred_labels
                 case 'merge_on_preferred_labels':
                 case 'merge_on_preferred_labels_with_replace':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $t_subject->load($va_ids[0]);
                         $o_log->logInfo(_t('[%1] Merged with existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                     }
                     break;
                 case 'overwrite_on_idno_and_preferred_labels':
                 case 'overwrite_on_idno':
                     if (!$vb_idno_is_template && $vs_idno) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $t_subject->load($va_ids[0]);
                             $t_subject->setMode(ACCESS_WRITE);
                             $t_subject->delete(true, array('hard' => true));
                             if ($t_subject->numErrors()) {
                                 ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                                 // Don't stop?
                             } else {
                                 $o_log->logInfo(_t('[%1] Overwrote existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                                 $t_subject->clear();
                                 break;
                             }
                         }
                     }
                     if ($vs_existing_record_policy == 'overwrite_on_idno') {
                         break;
                     }
                     // fall through if overwrite_on_idno_and_preferred_labels
                 // fall through if overwrite_on_idno_and_preferred_labels
                 case 'overwrite_on_preferred_labels':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $t_subject->load($va_ids[0]);
                         $t_subject->setMode(ACCESS_WRITE);
                         $t_subject->delete(true, array('hard' => true));
                         if ($t_subject->numErrors()) {
                             ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                             // Don't stop?
                         } else {
                             $o_log->logInfo(_t('[%1] Overwrote existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                             break;
                         }
                         $t_subject->clear();
                     }
                     break;
             }
         }
         $o_progress->next(_t("Importing %1", $vs_idno), array('window' => $r_progress));
         if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
             $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, ca_data_importers::$s_num_records_processed, $vn_num_items, _t("[%3/%4] Processing %1 (%2)", caTruncateStringWithEllipsis($vs_display_label, 50), $vs_idno, ca_data_importers::$s_num_records_processed, $vn_num_items), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors);
         }
         $vb_output_subject_preferred_label = false;
         $va_content_tree = array();
         foreach ($va_items_by_group as $vn_group_id => $va_items) {
             $va_group = $va_mapping_groups[$vn_group_id];
             $vs_group_destination = $va_group['destination'];
             $va_group_tmp = explode(".", $vs_group_destination);
             if (sizeof($va_items) < 2 && sizeof($va_group_tmp) > 2) {
                 array_pop($va_group_tmp);
             }
             $vs_target_table = $va_group_tmp[0];
             if (!($t_target = $o_dm->getInstanceByTableName($vs_target_table, true))) {
                 // Invalid target table
                 $o_log->logWarn(_t('[%1] Skipped group %2 because target %3 is invalid', $vs_idno, $vn_group_id, $vs_target_table));
                 continue;
             }
             if ($o_trans) {
                 $t_target->setTransaction($o_trans);
             }
             $va_group_buf = array();
             foreach ($va_items as $vn_item_id => $va_item) {
                 if ($vb_use_as_single_value = caGetOption('useAsSingleValue', $va_item['settings'], false)) {
                     // Force repeating values to be imported as a single value
                     $va_vals = array(ca_data_importers::getValueFromSource($va_item, $o_reader, array('delimiter' => caGetOption('delimiter', $va_item['settings'], ''), 'returnAsArray' => false)));
                 } else {
                     $va_vals = ca_data_importers::getValueFromSource($va_item, $o_reader, array('returnAsArray' => true, 'environment' => $va_environment));
                 }
                 if (!sizeof($va_vals)) {
                     $va_vals = array(0 => null);
                 }
                 // consider missing values equivalent to blanks
                 // Do value conversions
                 foreach ($va_vals as $vn_i => $vm_val) {
                     if (isset($va_item['settings']['default']) && strlen($va_item['settings']['default']) && !strlen($vm_val)) {
                         $vm_val = $va_item['settings']['default'];
                     }
                     // Apply prefix/suffix *AFTER* setting default
                     if ($vm_val && isset($va_item['settings']['prefix']) && strlen($va_item['settings']['prefix'])) {
                         $vm_val = $va_item['settings']['prefix'] . $vm_val;
                     }
                     if ($vm_val && isset($va_item['settings']['suffix']) && strlen($va_item['settings']['suffix'])) {
                         $vm_val .= $va_item['settings']['suffix'];
                     }
                     if (!is_array($vm_val) && $vm_val[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vm_val)) {
                         // Parse placeholder
                         if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vm_val, $va_row, $va_item, $vn_i, array('reader' => $o_reader, 'returnAsString' => true)))) {
                             $vm_val = $vm_parsed_val;
                         }
                     }
                     if (isset($va_item['settings']['formatWithTemplate']) && strlen($va_item['settings']['formatWithTemplate'])) {
                         $vm_val = caProcessTemplate($va_item['settings']['formatWithTemplate'], array_replace($va_row, array((string) $va_item['source'] => ca_data_importers::replaceValue($vm_val, $va_item))), array('getFrom' => $o_reader));
                     }
                     if (isset($va_item['settings']['applyRegularExpressions']) && is_array($va_item['settings']['applyRegularExpressions'])) {
                         if (is_array($va_item['settings']['applyRegularExpressions'])) {
                             foreach ($va_item['settings']['applyRegularExpressions'] as $vn_regex_index => $va_regex) {
                                 if (!strlen($va_regex['match'])) {
                                     continue;
                                 }
                                 $vm_val = preg_replace("!" . str_replace("!", "\\!", $va_regex['match']) . "!" . (isset($va_regex['caseSensitive']) && (bool) $va_regex['caseSensitive'] ? '' : 'i'), $va_regex['replaceWith'], $vm_val);
                             }
                         }
                     }
                     $va_vals[$vn_i] = $vm_val;
                     if ($o_reader->valuesCanRepeat()) {
                         $va_row[$va_item['source']][$vn_i] = $va_row[mb_strtolower($va_item['source'])][$vn_i] = $vm_val;
                     } else {
                         $va_row[$va_item['source']] = $va_row[mb_strtolower($va_item['source'])] = $vm_val;
                     }
                 }
                 // Process each value
                 $vn_c = -1;
                 foreach ($va_vals as $vn_i => $vm_val) {
                     $vn_c++;
                     if (isset($va_item['settings']['convertNewlinesToHTML']) && (bool) $va_item['settings']['convertNewlinesToHTML'] && is_string($vm_val)) {
                         $vm_val = nl2br($vm_val);
                     }
                     // Get location in content tree for addition of new content
                     $va_item_dest = explode(".", $va_item['destination']);
                     $vs_item_terminal = $va_item_dest[sizeof($va_item_dest) - 1];
                     if (isset($va_item['settings']['restrictToTypes']) && is_array($va_item['settings']['restrictToTypes']) && !in_array($vs_type, $va_item['settings']['restrictToTypes'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because of type restriction', $vs_idno, $vn_row));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfEmpty']) && (bool) $va_item['settings']['skipRowIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is empty', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfValue']) && is_array($va_item['settings']['skipRowIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipRowIfValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 matches value %5', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfNotValue']) && is_array($va_item['settings']['skipRowIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipRowIfNotValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is not in list of values', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id, $vm_val));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipGroupIfEmpty']) && (bool) $va_item['settings']['skipGroupIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 is empty', $vs_idno, $vn_group_id, $vs_item_terminal));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipGroupIfExpression']) && strlen(trim($va_item['settings']['skipGroupIfExpression']))) {
                         if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipGroupIfExpression'], $va_row)) {
                             $o_log->logInfo(_t('[%1] Skipped group %2 because expression %3 is true', $vs_idno, $vn_group_id, $va_item['settings']['skipGroupIfExpression']));
                             continue 3;
                         }
                     }
                     if (isset($va_item['settings']['skipGroupIfValue']) && is_array($va_item['settings']['skipGroupIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipGroupIfValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches value %4', $vs_idno, $vn_group_id, $vs_item_terminal, $vm_val));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipGroupIfNotValue']) && is_array($va_item['settings']['skipGroupIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipGroupIfNotValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches is not in list of values', $vs_idno, $vn_group_id, $vs_item_terminal));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipIfExpression']) && strlen(trim($va_item['settings']['skipIfExpression']))) {
                         if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipIfExpression'], $va_row)) {
                             $o_log->logInfo(_t('[%1] Skipped mapping because expression %2 is true', $vs_idno, $va_item['settings']['skipIfExpression']));
                             continue 2;
                         }
                     }
                     if (isset($va_item['settings']['skipIfEmpty']) && (bool) $va_item['settings']['skipIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped mapping because value for %2 is empty', $vs_idno, $vs_item_terminal));
                         continue 2;
                     }
                     if ($vn_type_id_mapping_item_id && $vn_item_id == $vn_type_id_mapping_item_id) {
                         continue;
                     }
                     if ($vn_idno_mapping_item_id && $vn_item_id == $vn_idno_mapping_item_id) {
                         continue;
                     }
                     if (is_null($vm_val)) {
                         continue;
                     }
                     // Get mapping error policy
                     $vb_item_error_policy_is_default = false;
                     if (!isset($va_item['settings']['errorPolicy']) || !in_array($vs_item_error_policy = $va_item['settings']['errorPolicy'], array('ignore', 'stop'))) {
                         $vs_item_error_policy = 'ignore';
                         $vb_item_error_policy_is_default = true;
                     }
                     //
                     if (isset($va_item['settings']['relationshipType']) && strlen($vs_rel_type = $va_item['settings']['relationshipType']) && $vs_target_table != $vs_subject_table) {
                         $va_group_buf[$vn_c]['_relationship_type'] = $vs_rel_type;
                     }
                     // Is it a constant value?
                     if (preg_match("!^_CONSTANT_:[\\d]+:(.*)!", $va_item['source'], $va_matches)) {
                         $va_group_buf[$vn_c][$vs_item_terminal] = $va_matches[1];
                         // Set it and go onto the next item
                         if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                             $va_mandatory_field_values[$vs_k] = $vm_val;
                         }
                         continue;
                     }
                     // Perform refinery call (if required) per value
                     if (isset($va_item['settings']['refineries']) && is_array($va_item['settings']['refineries'])) {
                         foreach ($va_item['settings']['refineries'] as $vs_refinery) {
                             if (!$vs_refinery) {
                                 continue;
                             }
                             if ($o_refinery = RefineryManager::getRefineryInstance($vs_refinery)) {
                                 $va_refined_values = $o_refinery->refine($va_content_tree, $va_group, $va_item, $va_row, array('mapping' => $t_mapping, 'source' => $ps_source, 'subject' => $t_subject, 'locale_id' => $vn_locale_id, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'reader' => $o_reader, 'valueIndex' => $vn_i));
                                 if (!$va_refined_values || is_array($va_refined_values) && !sizeof($va_refined_values)) {
                                     continue 2;
                                 }
                                 if ($o_refinery->returnsMultipleValues()) {
                                     foreach ($va_refined_values as $va_refined_value) {
                                         $va_refined_value['_errorPolicy'] = $vs_item_error_policy;
                                         if (!is_array($va_group_buf[$vn_c])) {
                                             $va_group_buf[$vn_c] = array();
                                         }
                                         $va_group_buf[$vn_c] = array_merge($va_group_buf[$vn_c], $va_refined_value);
                                         $vn_c++;
                                     }
                                 } else {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                     $va_group_buf[$vn_c][$vs_item_terminal] = $va_refined_values;
                                     $vn_c++;
                                 }
                                 if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                                     $va_mandatory_field_values[$vs_k] = $vm_val;
                                 }
                                 continue 2;
                             } else {
                                 ca_data_importers::logImportError(_t('[%1] Invalid refinery %2 specified', $vs_idno, $vs_refinery));
                             }
                         }
                     }
                     if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                         $va_mandatory_field_values[$vs_k] = $vm_val;
                     }
                     $vn_max_length = !is_array($vm_val) && isset($va_item['settings']['maxLength']) && (int) $va_item['settings']['maxLength'] ? (int) $va_item['settings']['maxLength'] : null;
                     if (isset($va_item['settings']['delimiter']) && $va_item['settings']['delimiter']) {
                         if (!is_array($va_item['settings']['delimiter'])) {
                             $va_item['settings']['delimiter'] = array($va_item['settings']['delimiter']);
                         }
                         if (sizeof($va_item['settings']['delimiter'])) {
                             foreach ($va_item['settings']['delimiter'] as $vn_index => $vs_delim) {
                                 $va_item['settings']['delimiter'][$vn_index] = preg_quote($vs_delim, "!");
                             }
                             $va_val_list = preg_split("!(" . join("|", $va_item['settings']['delimiter']) . ")!", $vm_val);
                             // Add delimited values
                             foreach ($va_val_list as $vs_list_val) {
                                 $vs_list_val = trim(ca_data_importers::replaceValue($vs_list_val, $va_item));
                                 if ($vn_max_length && mb_strlen($vs_list_val) > $vn_max_length) {
                                     $vs_list_val = mb_substr($vs_list_val, 0, $vn_max_length);
                                 }
                                 $va_group_buf[$vn_c] = array($vs_item_terminal => $vs_list_val, '_errorPolicy' => $vs_item_error_policy);
                                 $vn_c++;
                             }
                             $vn_row++;
                             continue;
                             // Don't add "regular" value below
                         }
                     }
                     if ($vn_max_length && mb_strlen($vm_val) > $vn_max_length) {
                         $vm_val = mb_substr($vm_val, 0, $vn_max_length);
                     }
                     switch ($vs_item_terminal) {
                         case 'preferred_labels':
                         case 'nonpreferred_labels':
                             if ($t_instance = $o_dm->getInstanceByTableName($vs_target_table, true)) {
                                 $va_group_buf[$vn_c][$t_instance->getLabelDisplayField()] = $vm_val;
                             }
                             if ($o_trans) {
                                 $t_instance->setTransaction($o_trans);
                             }
                             if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) {
                                 if (is_array($va_group_buf[$vn_c])) {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                 }
                             }
                             if ($vs_item_terminal == 'preferred_labels') {
                                 $vs_preferred_label_for_log = $vm_val;
                             }
                             break;
                         default:
                             $va_group_buf[$vn_c][$vs_item_terminal] = $vm_val;
                             if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) {
                                 if (is_array($va_group_buf[$vn_c])) {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                 }
                             }
                             break;
                     }
                 }
                 // end foreach($va_vals as $vm_val)
             }
             foreach ($va_group_buf as $vn_group_index => $va_group_data) {
                 $va_ptr =& $va_content_tree;
                 foreach ($va_group_tmp as $vs_tmp) {
                     if (!is_array($va_ptr[$vs_tmp])) {
                         $va_ptr[$vs_tmp] = array();
                     }
                     $va_ptr =& $va_ptr[$vs_tmp];
                     if ($vs_tmp == $vs_target_table) {
                         // add numeric index after table to ensure repeat values don't overwrite each other
                         $va_parent =& $va_ptr;
                         $va_ptr[] = array();
                         $va_ptr =& $va_ptr[sizeof($va_ptr) - 1];
                     }
                 }
                 $va_ptr = $va_group_data;
             }
         }
         //
         // Process out self-relationships
         //
         if (is_array($va_content_tree[$vs_subject_table])) {
             $va_self_related_content = array();
             foreach ($va_content_tree[$vs_subject_table] as $vn_i => $va_element_data) {
                 if (isset($va_element_data['_relationship_type'])) {
                     $va_self_related_content[] = $va_element_data;
                     unset($va_content_tree[$vs_subject_table][$vn_i]);
                 }
             }
             if (sizeof($va_self_related_content) > 0) {
                 $va_content_tree["related.{$vs_subject_table}"] = $va_self_related_content;
             }
         }
         $vn_row++;
         $o_log->logDebug(_t('Finished building content tree for %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         $o_log->logDebug(_t("Content tree is\n%1", print_R($va_content_tree, true)));
         //
         // Process data in subject record
         //
         //print_r($va_content_tree);
         //die("END\n\n");
         //continue;
         $opa_app_plugin_manager->hookDataImportContentTree(array('mapping' => $t_mapping, 'content_tree' => &$va_content_tree, 'idno' => &$vs_idno, 'transaction' => &$o_trans, 'log' => &$o_log, 'reader' => $o_reader, 'environment' => $va_environment, 'importEvent' => $o_event, 'importEventSource' => $vn_row));
         //print_r($va_content_tree);
         //die("done\n");
         if (!sizeof($va_content_tree) && !str_replace("%", "", $vs_idno)) {
             continue;
         }
         if (!$t_subject->getPrimaryKey()) {
             $o_event->beginItem($vn_row, $t_subject->tableNum(), 'I');
             $t_subject->setMode(ACCESS_WRITE);
             $t_subject->set($vs_type_id_fld, $vs_type);
             if ($vb_idno_is_template) {
                 $t_subject->setIdnoWithTemplate($vs_idno);
             } else {
                 $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoForRepresentationID' => true, 'assumeIdnoStubForLotID' => true));
                 // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_id
             }
             // Look for parent_id in the content tree
             $vs_parent_id_fld = $t_subject->getProperty('HIERARCHY_PARENT_ID_FLD');
             foreach ($va_content_tree as $vs_table_name => $va_content) {
                 if ($vs_table_name == $vs_subject_table) {
                     foreach ($va_content as $va_element_data) {
                         foreach ($va_element_data as $vs_element => $va_element_content) {
                             switch ($vs_element) {
                                 case $vs_parent_id_fld:
                                     if ($va_element_content[$vs_parent_id_fld]) {
                                         $t_subject->set($vs_parent_id_fld, $va_element_content[$vs_parent_id_fld], array('treatParentIDAsIdno' => true));
                                     }
                                     break;
                             }
                         }
                     }
                 }
             }
             foreach ($va_mandatory_field_mapping_ids as $vs_mandatory_field => $vn_mandatory_mapping_item_id) {
                 $t_subject->set($vs_mandatory_field, $va_mandatory_field_values[$vs_mandatory_field], array('assumeIdnoStubForLotID' => true));
             }
             $t_subject->insert();
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not insert new record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
                 continue;
             }
             $o_log->logDebug(_t('Created idno %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         } else {
             $o_event->beginItem($vn_row, $t_subject->tableNum(), 'U');
             // update
             $t_subject->setMode(ACCESS_WRITE);
             if ($vb_idno_is_template) {
                 $t_subject->setIdnoWithTemplate($vs_idno);
             } else {
                 $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoStubForLotID' => true));
                 // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_di
             }
             $t_subject->update();
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
                 continue;
             }
             $t_subject->clearErrors();
             if (sizeof($va_preferred_label_mapping_ids) && $t_subject->getPreferredLabelCount() > 0) {
                 $t_subject->removeAllLabels(__CA_LABEL_TYPE_PREFERRED__);
                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update remove preferred labels from matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                     if ($vs_import_error_policy == 'stop') {
                         $o_log->logAlert(_t('Import stopped due to import error policy'));
                         if ($vb_use_ncurses) {
                             ncurses_end();
                         }
                         if ($o_trans) {
                             $o_trans->rollback();
                         }
                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                         return false;
                     }
                 }
             }
             $o_log->logDebug(_t('Updated idno %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         }
         if ($vs_idno_fld && ($o_idno = $t_subject->getIDNoPlugInInstance())) {
             $va_values = $o_idno->htmlFormValuesAsArray($vs_idno_fld, $t_subject->get($vs_idno_fld));
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (($vs_proc_idno = join($o_idno->getSeparator(), $va_values)) && $vs_proc_idno != $vs_idno) {
                 $t_subject->set($vs_idno_fld, $vs_proc_idno);
                 $t_subject->update();
                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could update idno"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                     if ($vs_import_error_policy == 'stop') {
                         $o_log->logAlert(_t('Import stopped due to import error policy'));
                         if ($vb_use_ncurses) {
                             ncurses_end();
                         }
                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                         if ($o_trans) {
                             $o_trans->rollback();
                         }
                         return false;
                     }
                     continue;
                 }
             }
         }
         $va_elements_set_for_this_record = array();
         foreach ($va_content_tree as $vs_table_name => $va_content) {
             if ($vs_table_name == $vs_subject_table) {
                 foreach ($va_content as $vn_i => $va_element_data) {
                     foreach ($va_element_data as $vs_element => $va_element_content) {
                         if (is_array($va_element_content)) {
                             $vs_item_error_policy = $va_element_content['_errorPolicy'];
                             unset($va_element_content['_errorPolicy']);
                         } else {
                             $vs_item_error_policy = null;
                         }
                         $t_subject->clearErrors();
                         $t_subject->setMode(ACCESS_WRITE);
                         switch ($vs_element) {
                             case 'preferred_labels':
                                 $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, true);
                                 if ($t_subject->numErrors() == 0) {
                                     $vb_output_subject_preferred_label = true;
                                 }
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add preferred label to %2. Record was deleted because no preferred label could be applied: ", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     $t_subject->delete(true, array('hard' => false));
                                     if ($vs_import_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to import error policy %1', $vs_import_error_policy));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                     continue 5;
                                 }
                                 break;
                             case 'nonpreferred_labels':
                                 $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, false);
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add non-preferred label to %2:", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 break;
                             default:
                                 if ($t_subject->hasField($vs_element)) {
                                     $t_subject->set($vs_element, $va_element_content[$vs_element], array('assumeIdnoStubForLotID' => true));
                                     $t_subject->update();
                                     break;
                                 }
                                 if ($vs_subject_table == 'ca_representation_annotations' && $vs_element == 'properties') {
                                     foreach ($va_element_content as $vs_prop => $vs_prop_val) {
                                         $t_subject->setPropertyValue($vs_prop, $vs_prop_val);
                                     }
                                     break;
                                 }
                                 if (is_array($va_element_content)) {
                                     $va_element_content['locale_id'] = $vn_locale_id;
                                 }
                                 if (!isset($va_elements_set_for_this_record[$vs_element]) && !$va_elements_set_for_this_record[$vs_element] && in_array($vs_existing_record_policy, array('merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace'))) {
                                     $t_subject->removeAttributes($vs_element, array('force' => true));
                                 }
                                 $va_elements_set_for_this_record[$vs_element] = true;
                                 $t_subject->addAttribute($va_element_content, $vs_element, null, array('showRepeatCountErrors' => true, 'alwaysTreatValueAsIdno' => true));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Failed to add value for %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 $t_subject->update();
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 break;
                         }
                     }
                 }
             } else {
                 // related
                 $vs_table_name = preg_replace('!^related\\.!', '', $vs_table_name);
                 foreach ($va_content as $vn_i => $va_element_data) {
                     $va_match_on = caGetOption('_matchOn', $va_element_data, null);
                     $vb_dont_create = caGetOption('_dontCreate', $va_element_data, null);
                     $va_data_for_rel_table = $va_element_data;
                     $va_nonpreferred_labels = isset($va_data_for_rel_table['nonpreferred_labels']) ? $va_data_for_rel_table['nonpreferred_labels'] : null;
                     unset($va_data_for_rel_table['preferred_labels']);
                     unset($va_data_for_rel_table['_relationship_type']);
                     unset($va_data_for_rel_table['_type']);
                     unset($va_data_for_rel_table['_parent_id']);
                     unset($va_data_for_rel_table['_errorPolicy']);
                     unset($va_data_for_rel_table['_matchOn']);
                     $va_data_for_rel_table = array_merge($va_data_for_rel_table, ca_data_importers::_extractIntrinsicValues($va_data_for_rel_table, $vs_table_name));
                     $t_subject->clearErrors();
                     switch ($vs_table_name) {
                         case 'ca_objects':
                             if ($vn_rel_id = DataMigrationUtils::getObjectID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_object_lots':
                             $vs_idno_stub = null;
                             if (is_array($va_element_data['idno_stub'])) {
                                 $vs_idno_stub = isset($va_element_data['idno_stub']['idno_stub']) ? $va_element_data['idno_stub']['idno_stub'] : '';
                             } else {
                                 $vs_idno_stub = isset($va_element_data['idno_stub']) ? $va_element_data['idno_stub'] : '';
                             }
                             if ($vn_rel_id = DataMigrationUtils::getObjectLotID($vs_idno_stub, $va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno_stub']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object lot with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_entities':
                             if ($vn_rel_id = DataMigrationUtils::getEntityID($va_element_data['preferred_labels'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related entity with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_places':
                             if ($vn_rel_id = DataMigrationUtils::getPlaceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related place with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_collections':
                             if ($vn_rel_id = DataMigrationUtils::getCollectionID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related collection with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_occurrences':
                             if ($vn_rel_id = DataMigrationUtils::getOccurrenceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related occurrence with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_storage_locations':
                             if ($vn_rel_id = DataMigrationUtils::getStorageLocationID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related storage location with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_list_items':
                             $va_data_for_rel_table['is_enabled'] = 1;
                             $va_data_for_rel_table['preferred_labels'] = $va_element_data['preferred_labels'];
                             if ($vn_rel_id = DataMigrationUtils::getListItemID($va_element_data['_list'], $va_element_data['idno'] ? $va_element_data['idno'] : null, $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related list item with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_object_representations':
                             if ($vn_rel_id = DataMigrationUtils::getObjectRepresentationID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels, 'matchMediaFilesWithoutExtension' => true))) {
                                 $t_subject->linkRepresentation($vn_rel_id, null, null, null, null, array('type_id' => trim($va_element_data['_relationship_type']), 'is_primary' => true));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object representation with:", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             //
                             // 									if (($vs_subject_table_name == 'ca_objects') && $va_element_data['media']['media']) {
                             // 										unset($va_data_for_rel_table['media']);
                             //
                             // 										foreach($va_data_for_rel_table as $vs_key => $vm_val) {
                             // 											// Attributes, including intrinsics are in two-level format, eg. idno is $va_attributes['idno']['idno']
                             // 											// but addRepresentations() expects intrinsics to be single level (eg. $va_attributes['idno']) so
                             // 											// we do some rewriting here
                             // 											if (is_array($vm_val) && isset($vm_val[$vs_key])) {
                             // 												$va_data_for_rel_table[$vs_key] = $vm_val[$vs_key];
                             // 											}
                             // 										}
                             //
                             // 										if (!($t_subject->addRepresentation($va_element_data['media']['media'], isset($va_element_data['_type']) ? $va_element_data['_type'] : caGetDefaultItemID('object_representation_types'), $vn_locale_id, 0, 0, true, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on)))) {
                             // 											$vs_error = join("; ", $t_subject->getErrors());
                             // 											ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                             // 											if ($vs_item_error_policy == 'stop') {
                             // 												$o_log->logAlert(_t('Import stopped due to mapping error policy'));
                             // 												if($vb_use_ncurses) { ncurses_end(); }
                             // 												if ($o_trans) { $o_trans->rollback(); }
                             // 												return false;
                             // 											}
                             // 										}
                             // 									}
                             break;
                         case 'ca_loans':
                             if ($vn_rel_id = DataMigrationUtils::getLoanID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related loan with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_movements':
                             if ($vn_rel_id = DataMigrationUtils::getMovementID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related movement with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                     }
                     if (is_array($va_element_data['_related_related']) && sizeof($va_element_data['_related_related'])) {
                         foreach ($va_element_data['_related_related'] as $vs_rel_rel_table => $va_rel_rels) {
                             foreach ($va_rel_rels as $vn_i => $va_rel_rel) {
                                 if (!($t_rel_instance = $o_dm->getInstanceByTableName($vs_table_name))) {
                                     $o_log->logWarn(_t("[%1] Could not instantiate related table %2", $vs_idno, $vs_table_name));
                                     continue;
                                 }
                                 if ($o_trans) {
                                     $t_rel_instance->setTransaction($o_trans);
                                 }
                                 if ($t_rel_instance->load($vn_rel_id)) {
                                     if ($t_rel_rel = $t_rel_instance->addRelationship($vs_rel_rel_table, $va_rel_rel['id'], $va_rel_rel['_relationship_type'])) {
                                         $o_log->logInfo(_t('[%1] Related %2 (%3) to related %4 with relationship %5', $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type'])));
                                     } else {
                                         if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related %2 (%3) to related %4 with relationship %5:", $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                             ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // $t_subject->update();
         //
         // 			if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, 'attributes', ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
         // 				ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
         // 				if ($vs_item_error_policy == 'stop') {
         // 					$o_log->logAlert(_t('Import stopped due to mapping error policy'));
         // 					if($vb_use_ncurses) { ncurses_end(); }
         //
         // 					$o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
         //
         // 					if ($o_trans) { $o_trans->rollback(); }
         // 					return false;
         // 				}
         // 			}
         //
         $o_log->logDebug(_t('Finished inserting content tree for %1 at %2 seconds into database', $vs_idno, $t->getTime(4)));
         if (!$vb_output_subject_preferred_label && $t_subject->getPreferredLabelCount() == 0) {
             $t_subject->addLabel(array($vs_label_display_fld => '???'), $vn_locale_id, null, true);
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add default label", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
             }
         }
         $o_log->logInfo(_t('[%1] Imported %2 as %3 ', $vs_idno, $vs_preferred_label_for_log, $vs_subject_table_name));
         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_SUCCESS__, _t('Imported %1', $vs_idno));
         ca_data_importers::$s_num_records_processed++;
     }
     $o_log->logInfo(_t('Import of %1 completed using mapping %2: %3 imported/%4 skipped/%5 errors', $ps_source, $t_mapping->get('importer_code'), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped, ca_data_importers::$s_num_import_errors));
     //if ($vb_show_cli_progress_bar) {
     $o_progress->finish();
     //}
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, $vn_num_items, $vn_num_items, _t('Import completed'), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors);
     }
     if (isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback'])) {
         $va_general = array('elapsedTime' => time() - $vn_start_time, 'numErrors' => ca_data_importers::$s_num_import_errors, 'numProcessed' => ca_data_importers::$s_num_records_processed);
         $ps_callback($po_request, $va_general, ca_data_importers::$s_import_error_list, true);
     }
     if ($vb_use_ncurses) {
         ncurses_end();
     }
     if ($pb_dry_run) {
         if ($o_trans) {
             $o_trans->rollback();
         }
         $o_log->logInfo(_t('Rollback successful import run in "dry run" mode'));
     } else {
         if ($o_trans) {
             $o_trans->commit();
         }
     }
     return true;
 }
/**
 *
 */
function caGenerateBarcode($ps_value, $pa_options = null)
{
    $ps_barcode_type = caGetOption('type', $pa_options, 'code128', array('forceLowercase' => true));
    $pn_barcode_height = caConvertMeasurementToPoints(caGetOption('height', $pa_options, '9px'));
    $vs_tmp = null;
    switch ($ps_barcode_type) {
        case 'qr':
        case 'qrcode':
            $vs_tmp = tempnam(caGetTempDirPath(), 'caQRCode');
            $vs_tmp2 = tempnam(caGetTempDirPath(), 'caQRCodeResize');
            if (!defined('QR_LOG_DIR')) {
                define('QR_LOG_DIR', false);
            }
            if ($pn_barcode_height < 1 || $pn_barcode_height > 8) {
                $pn_barcode_height = 1;
            }
            QRcode::png($ps_value, "{$vs_tmp}.png", QR_ECLEVEL_H, $pn_barcode_height);
            return $vs_tmp;
            break;
        case 'code128':
        case 'code39':
        case 'ean13':
        case 'int25':
        case 'postnet':
        case 'upca':
            $o_barcode = new Barcode();
            $vs_tmp = tempnam(caGetTempDirPath(), 'caBarCode');
            if (!($va_dimensions = $o_barcode->draw($ps_value, "{$vs_tmp}.png", $ps_barcode_type, 'png', $pn_barcode_height))) {
                return null;
            }
            return $vs_tmp;
            break;
        default:
            // invalid barcode
            break;
    }
    return null;
}
Beispiel #22
0
 /**
  * Creates new user record. You must set all required user fields before calling this method. If errors occur you can use the standard Table class error handling methods to figure out what went wrong.
  *
  * Required fields are user_name, password, fname and lname.
  *
  * @access public 
  * @return bool Returns true if no error, false if error occurred
  */
 public function insert($pa_options = null)
 {
     # Confirmation key is an md5 hash than can be used as a confirmation token. The idea
     # is that you create a new user record with the 'active' field set to false. You then
     # send the confirmation key to the new user (usually via e-mail) and ask them to respond
     # with the key. If they do, you know that the e-mail address is valid.
     $vs_confirmation_key = md5(tempnam(caGetTempDirPath(), "meow") . time() . rand(1000, 999999999));
     $this->set("confirmation_key", $vs_confirmation_key);
     # set user vars (the set() method automatically serializes the vars array)
     $this->set("vars", $this->opa_user_vars);
     $this->set("volatile_vars", $this->opa_volatile_user_vars);
     return parent::insert($pa_options);
 }
Beispiel #23
0
 function encode_gd($ps_filepath, $ps_output_path, $pa_options)
 {
     if (!($vs_tilepic_tmpdir = $this->opo_config->get('tilepic_tmpdir'))) {
         $vs_tilepic_tmpdir = caGetTempDirPath();
     }
     if (!($magick = $this->mimetype2magick[$pa_options["output_mimetype"]])) {
         $this->error = "Invalid output format";
         return false;
     }
     #
     # Open image
     #
     if ($va_info = getimagesize($ps_filepath)) {
         switch ($va_info[2]) {
             case IMAGETYPE_GIF:
                 $r_image = imagecreatefromgif($ps_filepath);
                 $vs_mimetype = "image/gif";
                 $vs_typename = "GIF";
                 break;
             case IMAGETYPE_JPEG:
                 $r_image = imagecreatefromjpeg($ps_filepath);
                 if (function_exists('exif_read_data')) {
                     if (is_array($va_exif = @exif_read_data($ps_filepath, 'EXIF', true, false))) {
                         if (isset($va_exif['IFD0']['Orientation'])) {
                             $vn_orientation = $va_exif['IFD0']['Orientation'];
                             $h = new WLPlugMediaGD();
                             switch ($vn_orientation) {
                                 case 3:
                                     $r_image = $h->rotateImage($r_image, 180);
                                     break;
                                 case 6:
                                     $r_image = $h->rotateImage($r_image, -90);
                                     $vn_width = $va_info[0];
                                     $va_info[0] = $va_info[1];
                                     $va_info[1] = $vn_width;
                                     break;
                                 case 8:
                                     $r_image = $h->rotateImage($r_image, 90);
                                     $vn_width = $va_info[0];
                                     $va_info[0] = $va_info[1];
                                     $va_info[1] = $vn_width;
                                     break;
                             }
                         }
                     }
                 }
                 $vs_mimetype = "image/jpeg";
                 $vs_typename = "JPEG";
                 break;
             case IMAGETYPE_PNG:
                 $r_image = imagecreatefrompng($ps_filepath);
                 $vs_mimetype = "image/png";
                 $vs_typename = "PNG";
                 break;
         }
         if (!$r_image) {
             $this->error = "Couldn't open image {$ps_filepath}: open for {$vs_typename} failed";
             return false;
         }
     } else {
         $this->error = "Couldn't open image {$ps_filepath}: unsupported file type";
         return false;
     }
     $image_width = $va_info[0];
     $image_height = $va_info[1];
     if ($image_width < 10 || $image_height < 10) {
         $this->error = "Image is too small to be output as Tilepic; minimum dimensions are 10x10 pixels";
         return false;
     }
     if ($pa_options["scale_factor"] != 1) {
         $image_width *= $pa_options["scale_factor"];
         $image_height *= $pa_options["scale_factor"];
         $r_new_image = imagecreatetruecolor($image_width, $image_height);
         $r_color = ImageColorAllocate($r_new_image, 255, 255, 255);
         imagefilledrectangle($r_new_image, 0, 0, $image_width - 1, $image_height - 1, $r_color);
         if (imagecopyresampled($r_new_image, $r_image, 0, 0, 0, 0, $image_width, $image_height, $va_info[0], $va_info[1])) {
             $this->error = "Couldn't scale image for new layer";
             return false;
         }
         imagedestroy($r_image);
         $r_image = $r_new_image;
     }
     #
     # How many layers to make?
     #
     if (!$pa_options["layers"]) {
         $sw = $image_width * $pa_options["layer_ratio"];
         $sh = $image_height * $pa_options["layer_ratio"];
         $pa_options["layers"] = 1;
         while ($sw >= $pa_options["tile_width"] || $sh >= $pa_options["tile_height"]) {
             $sw = ceil($sw / $pa_options["layer_ratio"]);
             $sh = ceil($sh / $pa_options["layer_ratio"]);
             $pa_options["layers"]++;
         }
     }
     #
     # Cut image into tiles
     #
     $tiles = 0;
     $layer_list = array();
     $base_width = $image_width;
     $base_height = $image_height;
     for ($l = $pa_options["layers"]; $l >= 1; $l--) {
         $x = $y = 0;
         if ($l < $pa_options["layers"]) {
             $old_image_width = $image_width;
             $old_image_height = $image_height;
             $image_width = ceil($image_width / $pa_options["layer_ratio"]);
             $image_height = ceil($image_height / $pa_options["layer_ratio"]);
             $r_new_image = imagecreatetruecolor($image_width, $image_height);
             $r_color = ImageColorAllocate($r_new_image, 255, 255, 255);
             imagefilledrectangle($r_new_image, 0, 0, $image_width - 1, $image_height - 1, $r_color);
             if (!imagecopyresampled($r_new_image, $r_image, 0, 0, 0, 0, $image_width, $image_height, $old_image_width, $old_image_height)) {
                 $this->error = "Couldn't scale image for layer {$l}";
                 return false;
             }
             imagedestroy($r_image);
             $r_image = $r_new_image;
         }
         $i = 0;
         //$slices = array();
         $layer_list[] = array();
         while ($y < $image_height) {
             $wx = $pa_options["tile_width"];
             $wy = $pa_options["tile_height"];
             if ($image_width - $x < $wx) {
                 $wx = $image_width - $x;
             }
             if ($image_height - $y < $wy) {
                 $wy = $image_height - $y;
             }
             $r_slice = imagecreatetruecolor($wx, $wy);
             $r_color = ImageColorAllocate($r_slice, 255, 255, 255);
             imagefilledrectangle($r_slice, 0, 0, $wx - 1, $wy - 1, $r_color);
             if (!imagecopy($r_slice, $r_image, 0, 0, $x, $y, $wx, $wy)) {
                 $this->error = "Couldn't create tile in level {$l}";
                 return false;
             }
             $vs_gd_tmp = tempnam($vs_tilepic_tmpdir, 'tpc_gd_tmp');
             switch ($pa_options["output_mimetype"]) {
                 case 'image/gif':
                     imagegif($r_slice, $vs_gd_tmp);
                     break;
                 case 'image/jpeg':
                     if ($pa_options["quality"] > 0) {
                         imagejpeg($r_slice, $vs_gd_tmp, $pa_options["quality"]);
                     } else {
                         imagejpeg($r_slice, $vs_gd_tmp);
                     }
                     break;
                 case 'image/png':
                     imagepng($r_slice, $vs_gd_tmp);
                     break;
                 default:
                     die("Invalid output format " . $pa_options["output_mimetype"]);
             }
             $vs_image = file_get_contents($vs_gd_tmp);
             @unlink($vs_gd_tmp);
             $layer_list[sizeof($layer_list) - 1][] = $vs_image;
             imagedestroy($r_slice);
             $x += $pa_options["tile_width"];
             if ($x >= $image_width) {
                 $y += $pa_options["tile_height"];
                 $x = 0;
             }
             $i++;
             $tiles++;
         }
     }
     imagedestroy($r_image);
     #
     # Write Tilepic format file
     #
     if ($fh = fopen($ps_output_path . ".tpc", "w")) {
         # --- attribute list
         $attribute_list = "";
         $attributes = 0;
         if (isset($pa_options["attributes"]) && is_array($pa_options["attributes"])) {
             $pa_options["attributes"]["mimeType"] = $pa_options["output_mimetype"];
         } else {
             $pa_options["attributes"] = array("mimeType" => $pa_options["output_mimetype"]);
         }
         foreach ($pa_options["attributes"] as $k => $v) {
             $attribute_list .= "{$k}={$v}";
             $attributes++;
         }
         # --- header
         if (!fwrite($fh, "TPC\n")) {
             $this->error = "Could not write Tilepic signature";
             return false;
         }
         if (!fwrite($fh, pack("NNNNNNnnNN", 40, $base_width, $base_height, $pa_options["tile_width"], $pa_options["tile_height"], $tiles, $pa_options["layers"], $pa_options["layer_ratio"], strlen($attribute_list), $attributes))) {
             $this->error = "Could not write Tilepic header";
             return false;
         }
         # --- offset table
         $offset = 44 + $tiles * 4;
         for ($i = sizeof($layer_list) - 1; $i >= 0; $i--) {
             for ($j = 0; $j < sizeof($layer_list[$i]); $j++) {
                 if (!fwrite($fh, pack("N", $offset))) {
                     $this->error = "Could not write Tilepic offset table";
                     return false;
                 }
                 $offset += strlen($layer_list[$i][$j]);
             }
         }
         if (!fwrite($fh, pack("N", $offset))) {
             $this->error = "Could not finish writing Tilepic offset table";
             return false;
         }
         # --- tiles
         for ($i = sizeof($layer_list) - 1; $i >= 0; $i--) {
             for ($j = 0; $j < sizeof($layer_list[$i]); $j++) {
                 if (!fwrite($fh, $layer_list[$i][$j])) {
                     $this->error = "Could not write Tilepic tile data";
                     return false;
                 }
             }
         }
         unset($layer_list);
         # --- attributes
         if (!fwrite($fh, $attribute_list)) {
             $this->error = "Could not write Tilepic attributes";
             return false;
         }
         fclose($fh);
         return $pa_options;
     } else {
         $this->error = "Couldn't open output file {$ps_output_path}\n";
         return false;
     }
 }
Beispiel #24
0
 private function _graphicsMagickGetMetadata($ps_filepath)
 {
     $va_metadata = array();
     /* EXIF metadata */
     if (function_exists('exif_read_data')) {
         if (is_array($va_exif = caSanitizeArray(@exif_read_data($ps_filepath, 'EXIF', true, false)))) {
             $va_metadata['EXIF'] = $va_exif;
         }
     }
     $o_xmp = new XMPParser();
     if ($o_xmp->parse($ps_filepath)) {
         if (is_array($va_xmp_metadata = $o_xmp->getMetadata()) && sizeof($va_xmp_metadata)) {
             $va_metadata['XMP'] = array();
             foreach ($va_xmp_metadata as $vs_xmp_tag => $va_xmp_values) {
                 $va_metadata['XMP'][$vs_xmp_tag] = join('; ', $va_xmp_values);
             }
         }
     }
     /* IPTC metadata */
     $vs_iptc_file = tempnam(caGetTempDirPath(), 'gmiptc');
     @rename($vs_iptc_file, $vs_iptc_file . '.iptc');
     // GM uses the file extension to figure out what we want
     $vs_iptc_file .= '.iptc';
     exec($this->ops_graphicsmagick_path . " convert " . caEscapeShellArg($ps_filepath) . " " . caEscapeShellArg($vs_iptc_file), $va_output, $vn_return);
     $vs_iptc_data = file_get_contents($vs_iptc_file);
     @unlink($vs_iptc_file);
     $va_iptc_raw = iptcparse($vs_iptc_data);
     $va_iptc_tags = array('2#004' => 'Genre', '2#005' => 'DocumentTitle', '2#010' => 'Urgency', '2#015' => 'Category', '2#020' => 'Subcategories', '2#025' => 'Keywords', '2#040' => 'SpecialInstructions', '2#055' => 'CreationDate', '2#060' => 'TimeCreated', '2#080' => 'AuthorByline', '2#085' => 'AuthorTitle', '2#090' => 'City', '2#095' => 'State', '2#100' => 'CountryCode', '2#101' => 'Country', '2#103' => 'OTR', '2#105' => 'Headline', '2#110' => 'Credit', '2#115' => 'PhotoSource', '2#116' => 'Copyright', '2#120' => 'Caption', '2#122' => 'CaptionWriter');
     $va_iptc = array();
     if (is_array($va_iptc_raw)) {
         foreach ($va_iptc_raw as $vs_iptc_tag => $va_iptc_tag_data) {
             if (isset($va_iptc_tags[$vs_iptc_tag])) {
                 $va_iptc[$va_iptc_tags[$vs_iptc_tag]] = join('; ', $va_iptc_tag_data);
             }
         }
     }
     if (sizeof($va_iptc)) {
         $va_metadata['IPTC'] = $va_iptc;
     }
     /* DPX metadata */
     exec($this->ops_graphicsmagick_path . " identify -format '%[DPX:*]' " . caEscapeShellArg($ps_filepath), $va_output, $vn_return);
     if ($va_output[0]) {
         $va_metadata['DPX'] = $va_output;
     }
     return $va_metadata;
 }
Beispiel #25
0
/**
 *
 */
function caMakeGetFilePath($ps_prefix = null, $ps_extension = null)
{
    $vs_path = caGetTempDirPath();
    do {
        $vs_file_path = $vs_path . DIRECTORY_SEPARATOR . $ps_prefix . mt_rand() . getmypid() . ($ps_extension ? ".{$ps_extension}" : "");
    } while (file_exists($vs_file_path));
    return $vs_file_path;
}
/**
 * Embed XMP metadata into representation media. Embedding is performed on a copy of the representation media and placed
 * into the system tmp directory. The original media is never modified.
 *
 * @param BaseModel $po_object ca_objects instance to pull metadata from for embedding
 * @param BaseModel $po_representation ca_object_representations instance to pull metadata from for embedding
 * @param string $ps_version Version of media to embed into. If omitted "original" version is used.
 * @return string Path to copy of media with embedded metadata. False is returned in the embedding failed.
 */
function caEmbedMetadataIntoRepresentation($po_object, $po_representation, $ps_version = "original")
{
    if (!($vs_media_metadata_config = $po_representation->getAppConfig()->get('media_metadata'))) {
        return false;
    }
    $o_metadata_config = Configuration::load($vs_media_metadata_config);
    $vs_mimetype = $po_representation->getMediaInfo('media', $ps_version, 'MIMETYPE');
    if (!in_array($vs_mimetype, array('image/jpeg'))) {
        return false;
    }
    // Don't try to embed in files other than JPEGs
    $vs_filepath = $po_representation->getMediaPath('media', $ps_version);
    if (!file_exists($vs_filepath)) {
        return false;
    }
    $va_mappings = $o_metadata_config->getAssoc('export_mappings');
    $o_xmp = new XMPParser();
    copy($vs_filepath, $vs_tmp_filepath = caGetTempDirPath() . "/" . time() . md5($vs_filepath));
    $o_xmp->parse($vs_tmp_filepath);
    $o_xmp->initMetadata();
    if (is_object($po_object) && isset($va_mappings['ca_objects']) && is_array($va_mappings['ca_objects'])) {
        $va_mapping = $va_mappings['ca_objects'];
        $vs_type = $po_object->getTypeCode();
        if (isset($va_mapping[$vs_type]) && is_array($va_mapping[$vs_type])) {
            $va_mapping = $va_mapping[$vs_type];
        } else {
            if (isset($va_mapping['__default__']) && is_array($va_mapping['__default__'])) {
                $va_mapping = $va_mapping['__default__'];
            } else {
                return null;
            }
        }
        if (is_array($va_mapping)) {
            foreach ($va_mapping as $vs_xmp => $va_ca) {
                $va_tmp = explode(':', $vs_xmp);
                if (sizeof($va_tmp) > 1) {
                    $vs_xmp = $va_tmp[1];
                }
                foreach ($va_ca as $vs_ca => $va_opts) {
                    if (preg_match('!^static:!', $vs_ca)) {
                        $vs_val = preg_replace('!^static:!', '', $vs_ca);
                    } else {
                        $vs_val = $po_object->get($vs_ca, $va_opts);
                    }
                    if ($vs_val) {
                        $o_xmp->set($vs_xmp, $vs_val);
                    }
                }
            }
        }
    }
    if (is_object($po_representation) && isset($va_mappings['ca_object_representations']) && is_array($va_mappings['ca_object_representations'])) {
        $va_mapping = $va_mappings['ca_object_representations'];
        $vs_type = $po_representation->getTypeCode();
        if (isset($va_mapping[$vs_type]) && is_array($va_mapping[$vs_type])) {
            $va_mapping = $va_mapping[$vs_type];
        } else {
            if (isset($va_mapping['__default__']) && is_array($va_mapping['__default__'])) {
                $va_mapping = $va_mapping['__default__'];
            } else {
                return null;
            }
        }
        if (is_array($va_mapping)) {
            foreach ($va_mapping as $vs_xmp => $va_ca) {
                $va_tmp = explode(':', $vs_xmp);
                if (sizeof($va_tmp) > 1) {
                    $vs_xmp = $va_tmp[1];
                }
                foreach ($va_ca as $vs_ca => $va_opts) {
                    if (preg_match('!^static:!', $vs_ca)) {
                        $vs_val = preg_replace('!^static:!', '', $vs_ca);
                    } else {
                        $vs_val = $po_representation->get($vs_ca, $va_opts);
                    }
                    if ($vs_val) {
                        $o_xmp->set($vs_xmp, $vs_val);
                    }
                }
            }
        }
    }
    $o_xmp->write();
    return $vs_tmp_filepath;
}
Beispiel #27
0
 /**
  * Constructor
  */
 public function __construct()
 {
     $this->_tmp_data_path = tempnam(caGetTempDirPath(), "ZIP");
     $this->_tmp_data = fopen($this->_tmp_data_path, "w+");
 }
Beispiel #28
0
 public function write($filepath, $mimetype)
 {
     if (!$this->handle) {
         return false;
     }
     if (!($ext = $this->info["EXPORT"][$mimetype])) {
         # this plugin can't write this mimetype
         $this->postError(1610, _t("Can't convert '%1' to '%2': unsupported format", $this->handle["mime_type"], $mimetype), "WLPlugAudio->write()");
         return false;
     }
     $o_config = Configuration::load();
     $va_tags = $this->get("getID3_tags");
     $vs_intro_filepath = $this->get("intro_filepath");
     $vs_outro_filepath = $this->get("outro_filepath");
     if (($vn_output_bitrate = $this->get("bitrate")) < 32) {
         $vn_output_bitrate = 64;
     }
     if (($vn_sample_frequency = $this->get("sample_frequency")) < 4096) {
         $vn_sample_frequency = 44100;
     }
     if (($vn_channels = $this->get("channels")) < 1) {
         $vn_channels = 1;
     }
     if ($this->properties["mimetype"] == $mimetype && !($this->properties["mimetype"] == "audio/mpeg" && ($vs_intro_filepath || $vs_outro_filepath)) && ($vn_output_bitrate == $this->input_bitrate && $vn_sample_frequency == $this->input_sample_frequency && $vn_channels == $this->input_channels)) {
         # write the file
         if (!copy($this->filepath, $filepath . "." . $ext)) {
             $this->postError(1610, _t("Couldn't write file to '%1'", $filepath), "WLPlugAudio->write()");
             return false;
         }
     } else {
         if ($mimetype != "image/png" && $mimetype != "image/jpeg" && $this->opb_ffmpeg_available) {
             #
             # Do conversion
             #
             if ($mimetype == 'audio/ogg') {
                 exec($this->ops_path_to_ffmpeg . " -f " . $this->info["IMPORT"][$this->properties["mimetype"]] . " -i " . caEscapeShellArg($this->filepath) . " -acodec libvorbis -ab " . $vn_output_bitrate . " -ar " . $vn_sample_frequency . " -ac " . $vn_channels . "  -y " . caEscapeShellArg($filepath . "." . $ext) . " 2>&1", $va_output, $vn_return);
             } else {
                 exec($this->ops_path_to_ffmpeg . " -f " . $this->info["IMPORT"][$this->properties["mimetype"]] . " -i " . caEscapeShellArg($this->filepath) . " -f " . $this->info["EXPORT"][$mimetype] . " -ab " . $vn_output_bitrate . " -ar " . $vn_sample_frequency . " -ac " . $vn_channels . "  -y " . caEscapeShellArg($filepath . "." . $ext) . " 2>&1", $va_output, $vn_return);
             }
             if ($vn_return != 0) {
                 @unlink($filepath . "." . $ext);
                 $this->postError(1610, _t("Error converting file to %1 [%2]: %3", $this->typenames[$mimetype], $mimetype, join("; ", $va_output)), "WLPlugAudio->write()");
                 return false;
             }
             if ($mimetype == "audio/mpeg") {
                 if ($vs_intro_filepath || $vs_outro_filepath) {
                     // add intro
                     $vs_tmp_filename = tempnam(caGetTempDirPath(), "audio");
                     if ($vs_intro_filepath) {
                         exec($this->ops_path_to_ffmpeg . " -i " . caEscapeShellArg($vs_intro_filepath) . " -f mp3 -ab " . $vn_output_bitrate . " -ar " . $vn_sample_frequency . " -ac " . $vn_channels . " -y " . caEscapeShellArg($vs_tmp_filename), $va_output, $vn_return);
                         if ($vn_return != 0) {
                             @unlink($filepath . "." . $ext);
                             $this->postError(1610, _t("Error converting intro to %1 [%2]: %3", $this->typenames[$mimetype], $mimetype, join("; ", $va_output)), "WLPlugAudio->write()");
                             return false;
                         }
                     }
                     $r_fp = fopen($vs_tmp_filename, "a");
                     $r_mp3fp = fopen($filepath . "." . $ext, "r");
                     while (!feof($r_mp3fp)) {
                         fwrite($r_fp, fread($r_mp3fp, 8192));
                     }
                     fclose($r_mp3fp);
                     if ($vs_outro_filepath) {
                         $vs_tmp_outro_filename = tempnam(caGetTempDirPath(), "audio");
                         exec($this->ops_path_to_ffmpeg . " -i " . caEscapeShellArg($vs_outro_filepath) . " -f mp3 -ab " . $vn_output_bitrate . " -ar " . $vn_sample_frequency . " -ac " . $vn_channels . " -y " . caEscapeShellArg($vs_tmp_outro_filename), $va_output, $vn_return);
                         if ($vn_return != 0) {
                             @unlink($filepath . "." . $ext);
                             $this->postError(1610, _t("Error converting outro to %1 [%2]: %3", $this->typenames[$mimetype], $mimetype, join("; ", $va_output)), "WLPlugAudio->write()");
                             return false;
                         }
                         $r_mp3fp = fopen($vs_tmp_outro_filename, "r");
                         while (!feof($r_mp3fp)) {
                             fwrite($r_fp, fread($r_mp3fp, 8192));
                         }
                         unlink($vs_tmp_outro_filename);
                     }
                     fclose($r_fp);
                     copy($vs_tmp_filename, $filepath . "." . $ext);
                     unlink($vs_tmp_filename);
                 }
                 $o_getid3 = new getid3();
                 $va_mp3_output_info = $o_getid3->analyze($filepath . "." . $ext);
                 $this->properties = array();
                 if (is_array($va_mp3_output_info["tags"]["id3v1"]["title"])) {
                     $this->properties["title"] = join("; ", $va_mp3_output_info["tags"]["id3v1"]["title"]);
                 }
                 if (is_array($va_mp3_output_info["tags"]["id3v1"]["artist"])) {
                     $this->properties["author"] = join("; ", $va_mp3_output_info["tags"]["id3v1"]["artist"]);
                 }
                 if (is_array($va_mp3_output_info["tags"]["id3v1"]["comment"])) {
                     $this->properties["copyright"] = join("; ", $va_mp3_output_info["tags"]["id3v1"]["comment"]);
                 }
                 if (is_array($va_mp3_output_info["tags"]["id3v1"]["album"]) && is_array($va_mp3_output_info["tags"]["id3v1"]["year"]) && is_array($va_mp3_output_info["tags"]["id3v1"]["genre"])) {
                     $this->properties["description"] = join("; ", $va_mp3_output_info["tags"]["id3v1"]["album"]) . " " . join("; ", $va_mp3_output_info["tags"]["id3v1"]["year"]) . " " . join("; ", $va_mp3_output_info["tags"]["id3v1"]["genre"]);
                 }
                 $this->properties["type_specific"] = array("audio" => $va_mp3_output_info["audio"], "tags" => $va_mp3_output_info["tags"]);
                 $this->properties["bandwidth"] = array("min" => $va_mp3_output_info["bitrate"], "max" => $va_mp3_output_info["bitrate"]);
                 $this->properties["bitrate"] = $va_mp3_output_info["bitrate"];
                 $this->properties["channels"] = $va_mp3_output_info["audio"]["channels"];
                 $this->properties["sample_frequency"] = $va_mp3_output_info["audio"]["sample_rate"];
                 $this->properties["duration"] = $va_mp3_output_info["playtime_seconds"];
             }
         } else {
             # use default media icons if ffmpeg is not present or the current version is an image
             if (!$this->get("width") && !$this->get("height")) {
                 $this->set("width", 580);
                 $this->set("height", 200);
             }
             return __CA_MEDIA_AUDIO_DEFAULT_ICON__;
         }
     }
     if ($mimetype == "audio/mpeg") {
         // try to write getID3 tags (if set)
         if (is_array($pa_options) && is_array($pa_options) && sizeof($pa_options) > 0) {
             require_once 'parsers/getid3/getid3.php';
             require_once 'parsers/getid3/write.php';
             $o_getID3 = new getID3();
             $o_tagwriter = new getid3_writetags();
             $o_tagwriter->filename = $filepath . "." . $ext;
             $o_tagwriter->tagformats = array('id3v2.3');
             $o_tagwriter->tag_data = $pa_options;
             // write them tags
             if (!$o_tagwriter->WriteTags()) {
                 // failed to write tags
             }
         }
     }
     $this->properties["mimetype"] = $mimetype;
     $this->properties["typename"] = $this->typenames[$mimetype];
     return $filepath . "." . $ext;
 }
Beispiel #29
0
 /**
  * Apply media transformation to media in specified field of current loaded row. When a transformation is applied
  * it is applied to all versions, including the "original." A copy of the original is stashed in a "virtual" version named "_undo_"
  * to make it possible to recover the original media, if desired, by calling removeMediaTransformations().
  *
  * @param string $ps_field The name of the media field
  * @param string $ps_op A valid media transformation op code, as defined by the media plugin handling the media being transformed.
  * @param array $pa_params The parameters for the op code, as defined by the media plugin handling the media being transformed.
  * @param array $pa_options An array of options. No options are currently implemented.
  *
  * @return bool True on success, false if an error occurred.
  */
 public function applyMediaTransformation($ps_field, $ps_op, $pa_params, $pa_options = null)
 {
     $va_media_info = $this->getMediaInfo($ps_field);
     if (!is_array($va_media_info)) {
         return null;
     }
     if (isset($pa_options['revert']) && $pa_options['revert'] && isset($va_media_info['_undo_'])) {
         $vs_path = $vs_undo_path = $this->getMediaPath($ps_field, '_undo_');
         $va_transformation_history = array();
     } else {
         $vs_path = $this->getMediaPath($ps_field, 'original');
         // Copy original into "undo" slot (if undo slot is empty)
         $vs_undo_path = !isset($va_media_info['_undo_']) ? $vs_path : $this->getMediaPath($ps_field, '_undo_');
         if (!is_array($va_transformation_history = $va_media_info['TRANSFORMATION_HISTORY'])) {
             $va_transformation_history = array();
         }
     }
     // TODO: Check if transformation valid for this media
     // Apply transformation to original
     $o_media = new Media();
     $o_media->read($vs_path);
     $o_media->transform($ps_op, $pa_params);
     $va_transformation_history[$ps_op][] = $pa_params;
     $vs_tmp_basename = tempnam(caGetTempDirPath(), 'ca_media_rotation_tmp');
     $o_media->write($vs_tmp_basename, $o_media->get('mimetype'), array());
     // Regenerate derivatives
     $this->setMode(ACCESS_WRITE);
     $this->set($ps_field, $vs_tmp_basename . "." . $va_media_info['original']['EXTENSION'], $vs_undo_path ? array('undo' => $vs_undo_path, 'TRANSFORMATION_HISTORY' => $va_transformation_history) : array('TRANSFORMATION_HISTORY' => $va_transformation_history));
     $this->setAsChanged($ps_field);
     $this->update();
     return $this->numErrors() ? false : true;
 }
Beispiel #30
0
 public function joinArchiveContents($pa_files, $pa_options = array())
 {
     if (!is_array($pa_files)) {
         return false;
     }
     if (!caMediaPluginImageMagickInstalled($this->ops_imagemagick_path)) {
         return false;
     }
     $vs_archive_original = tempnam(caGetTempDirPath(), "caArchiveOriginal");
     @rename($vs_archive_original, $vs_archive_original . ".tif");
     $vs_archive_original = $vs_archive_original . ".tif";
     $va_acceptable_files = array();
     foreach ($pa_files as $vs_file) {
         if (file_exists($vs_file)) {
             if ($this->_imageMagickIdentify($vs_file)) {
                 $va_acceptable_files[] = $vs_file;
             }
         }
     }
     if (sizeof($va_acceptable_files)) {
         exec($this->ops_imagemagick_path . "/convert " . join(" ", $va_acceptable_files) . " " . $vs_archive_original, $va_output, $vn_return);
         if ($vn_return === 0) {
             return $vs_archive_original;
         }
     }
     return false;
 }