po/src/export.php

262 lines
11 KiB
PHP

<?php
// Copyright (C) 2002-2006 Balint Kis (balint@k-i-s.net)
// Copyright (C) 2005-2022 Solomon Peachy (pizza@shaftnet.org)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
include_once "include/config.php";
include_once "include/common.php";
include_once "include/site.php";
include_once "include/export.php";
include_once "include/orderby.php";
include_once "include/zipstream.php";
$compress_pages = false; /* Explicitly disable it here */
ini_set("max_execution_time", 0); /* Let it run until download is complete */
$database = site_prolog(PO_USER_TYPE_USER);
$export_type = $_REQUEST['export_type'];
$export_format = $po_options['export_format'];
/* Time to generate a tarball */
$photos_in_spooler = pg_query($database, "
select photo as identifier from album_content where album = $po_user[spool_album] group by photo order by photo");
$num_photos = pg_num_rows($photos_in_spooler);
$temporary_directory = tempnam($tmp_volume_path, "po.export.");
unlink($temporary_directory);
$export_id = basename($temporary_directory);
$export_id = substr($export_id, strlen("po.export."));
if (!mkdir($temporary_directory, 0700)) {
site_push_error(" - $temporary_directory - ". $strings['generic_failed']);
}
switch($export_type) {
case "photo":
case "photo_xml_iptc":
case "photo_iptc":
default:
break;
case "photo_xml":
if (($xml_file_desciptor = fopen($temporary_directory."/data.xml", "w+")) == FALSE) {
site_push_error($strings['generic_failed']);
}
fwrite($xml_file_desciptor, create_xml_header());
fwrite($xml_file_desciptor, create_xml_bulkupload_header());
break;
}
for ($photos = 0; $photos < $num_photos; $photos++) {
$photo_identifier = pg_fetch_assoc($photos_in_spooler, $photos);
/* optimize query, by excluding fields that are not used when not exporting shooting data */
switch($export_type) {
case "photo_xml_iptc":
case "photo_iptc":
case "photo_xml":
$photo_data = pg_fetch_assoc(pg_query($database, "
select folder, author, title, photo.caption, caption_writer, copyright_statement, web_statement, date_of_exposure, category, supplemental_category,
location, credit, source,
photo.access_rights, hide_original, headline, instructions,
camera, film, focal_length, aperture, shutter, transmission_reference,
exposure_comp, ev_difference, camera_metering, camera_program, flash_mode, flash_comp,
scan_resolution, scan_bitdepth, scan_multiscan, iso_override,
latitude, longitude, altitude, img_direction, photo.users, can_access_photo(photo.identifier, $po_user[id], '{".$passwords."}') as ok,
store_url, photo.comments as remark, folder.caption as folder_name
from photo, photo_version, photo_tech, folder
where photo.identifier = photo_version.photo
and photo.identifier = photo_tech.photo
and folder.identifier = photo.folder
and photo.identifier = $photo_identifier[identifier]"));
/* Get any keywords for the photo... */
$keywords = get_photo_keywords($database, $photo_identifier['identifier']);
$res = pg_query($database, "select photo, equipment, type from photo_equipment where photo = $photo_identifier[identifier] order by photo, type");
$equipment = FALSE;
for ($i=0; $i < pg_num_rows($res); $i++) {
$dat = pg_fetch_assoc($res);
$equipment[] = "$dat[equipment]:$dat[type]";
}
break;
case "photo":
default:
$photo_data = pg_fetch_assoc(pg_query($database, "select users, hide_original, title, can_access_photo(photo.identifier, $po_user[id], '{".$passwords."}') as ok from photo where identifier = $photo_identifier[identifier]"));
break;
}
/* Make sure we can access it! */
if (($photo_data['users'] != $po_user['id']) &&
!($po_user['type'] == PO_USER_TYPE_ADMIN)) {
if (($photo_data['ok'] != 't') || ($photo_data['hide_original'] != 't')) {
continue;
}
}
$photo_version_data = pg_query($database, "
select get_image_path(photo_version.identifier, 0) as original_path, original_image_name, comment, master, get_image_path(photo_version.identifier, -1) as sidecar_path
from photo_version
where photo_version.photo = $photo_identifier[identifier]");
$title = $photo_data['title'];
if ($title == "")
$title = $strings['photo_no_title'] ." - $photo_identifier[identifier]";
/* Generate filename list */
for ($versions = 0; $versions < pg_num_rows($photo_version_data); $versions++) {
$photo_version_single = pg_fetch_assoc($photo_version_data, $versions);
$file_name = $image_repository_path."/".$photo_version_single['original_path'];
$xmp_name = $image_repository_path."/".$photo_version_single['sidecar_path'];
$original_name = $temporary_directory."/".$photo_version_single['original_image_name'];
/* check for the source file's existence */
if (!is_file($file_name)) {
site_push_error($strings['errors_file_not_found'] ." : ". sprintf($strings['export_skipped'],emit_a(generate_link('photo', $photo_identifier['identifier']), $title)));
continue;
}
exec("ln -s \"$file_name\" \"$original_name\"", $who_cares, $retval_tar);
if (is_file($xmp_name)) {
exec("ln -s \"$xmp_name\" \"$original_name.xmp\"", $who_cares, $retval_tar);
}
/* we use "exec" instead of "system" to avoid unwanted output */
if ($retval_tar) {
site_push_error($strings['generic_failed']);
}
}
/* export shooting data */
switch($export_type) {
case "photo":
default:
break;
case "photo_xml":
$image_data = array ("file" => array(0 => array("name" => "", "remark" => "", "master" => "")),
"folder" => $photo_data['folder'],
"folder_name" => $photo_data['folder_name'],
"author" => $photo_data['author'],
"title" => $photo_data['title'],
"keywords" => $keywords,
"caption" => $photo_data['caption'],
"caption_writer" => $photo_data['caption_writer'],
"credit" => $photo_data['credit'],
"source" => $photo_data['source'],
"headline" => $photo_data['headline'],
"instructions" => $photo_data['instructions'],
"transmission_reference" => $photo_data['transmission_reference'],
"category" => $photo_data['category'],
"supplemental_category" => $photo_data['supplemental_category'],
"copyright" => $photo_data['copyright_statement'],
"web_statement" => $photo_data['web_statement'],
"date_of_exposure" => $photo_data['date_of_exposure'],
"location" => $photo_data['location'],
"access_rights" => $photo_data['access_rights'],
"hide_original" => $photo_data['hide_original'],
"camera" => $photo_data['camera'],
"film" => $photo_data['film'],
"focal_length" => $photo_data['focal_length'],
"aperture" => $photo_data['aperture'],
"shutter" => $photo_data['shutter'],
"exp_comp" => $photo_data['exposure_comp'],
"exp_diff" => $photo_data['ev_difference'],
"camera_metering" => $photo_data['camera_metering'],
"camera_program" => $photo_data['camera_program'],
"flash_mode" => $photo_data['flash_mode'],
"flash_comp" => $photo_data['flash_comp'],
"scan_resolution" => $photo_data['scan_resolution'],
"scan_bitdepth" => $photo_data['scan_bitdepth'],
"scan_multiscan" => $photo_data['scan_multiscan'],
"iso_override" => $photo_data['iso_override'],
"latitude" => $photo_data['latitude'],
"longitude" => $photo_data['longitude'],
"altitude" => $photo_data['altitude'],
"direction" => $photo_data['img_direction'],
"store_url" => $photo_data['store_url'],
"equipment" => $equipment,
"remark" => $photo_data['remark']);
for ($versions = 0; $versions < pg_num_rows($photo_version_data); $versions++) {
$photo_version_single = pg_fetch_assoc($photo_version_data, $versions);
$image_data['file'][$versions]['name'] = $photo_version_single['original_image_name'];
$image_data['file'][$versions]['remark'] = $photo_version_single['comment'];
$image_data['file'][$versions]['master'] = $photo_version_single['master'];
}
fwrite($xml_file_desciptor, create_xml_folder_header($image_data['folder'], $image_data['folder_name']));
fwrite($xml_file_desciptor, create_xml_photo($image_data));
fwrite($xml_file_desciptor, create_xml_folder_footer());
break;
case "photo_xml_iptc":
case "photo_iptc":
site_push_error($strings['errors_iptc_not_implemented']);
}
}
switch($export_type) {
case 'photo':
case 'photo_iptc':
case 'photo_xml_iptc':
default:
break;
case 'photo_xml':
fwrite($xml_file_desciptor, create_xml_bulkupload_footer());
fclose($xml_file_desciptor);
break;
}
/* Create the acual archive from symlink list */
switch ($export_format) {
case 'tar':
header("Content-Type: application/tar");
header("Content-Disposition: attachment; filename=po.export." . date(DATE_W3C) . ".tar");
$command = $sys_tar." -chC \"$temporary_directory\" . ";
$fh = popen($command, 'r');
while (!feof($fh)) {
print fread($fh, 8192);
}
pclose($fh);
break;
case 'zip':
$zip = new ZipStream('po.export.' . date(DATE_W3C) . '.zip');
$dh = opendir($temporary_directory);
while (false !== ($fn = readdir($dh))) {
if ($fn == "." || $fn == "..") continue;
$zip->add_file_from_path($fn ,"$temporary_directory/$fn");
}
$zip->finish();
break;
}
if (!deltree($temporary_directory)) {
site_push_error(" - $temporary_directory - " . $strings['generic_failed']);
}
?>