Documentation is available at admin.php
- <?php
- /**
- * @package net.siriux.photos
- * @author The Midgard Project, http://www.midgard-project.org
- * @version $Id: admin.php,v 1.20.2.4 2005/11/20 17:41:05 rambo Exp $
- * @copyright The Midgard Project, http://www.midgard-project.org
- * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
- */
- /**
- * Photo Gallery Admin Class
- *
- * @todo document
- *
- * @package net.siriux.photos
- */
- class net_siriux_photos_admin {
- var $_debug_prefix;
- var $_config;
- var $_config_dm;
- var $_topic;
- var $_photo;
- var $_upload_datamanager;
- var $_view;
- var $_view_status;
- var $_numpics;
- var $_startfrom;
- var $_scales;
- var $_enable_notes;
- var $_l10n;
- var $_l10n_midcom;
- var $errcode;
- var $errstr;
- var $_local_toolbar;
- var $_topic_toolbar;
- function net_siriux_photos_admin($topic, $config) {
- $this->_debug_prefix = "net.siriux.photos admin::";
- $this->_startfrom = 0;
- $this->_config = $config;
- $this->_config_dm = null;
- $this->_topic = $topic;
- $this->_photo = false;
- $this->_upload_datamanager = null;
- /* show 10 pics per page on index */
- $this->_numpics = 10;
- $this->_view = "";
- $this->_view_status = "";
- $this->_scales = Array (
- "view_x" => $config->get("view_x"),
- "view_y" => $config->get("view_x"),
- "thumb_x" => $config->get("thumb_x"),
- "thumb_y" => $config->get("thumb_y")
- );
- $this->_enable_notes = $config->get("enable_flash_notes");
- $i18n =& $GLOBALS["midcom"]->get_service("i18n");
- $this->_l10n = $i18n->get_l10n("net.siriux.photos");
- $this->_l10n_midcom = $i18n->get_l10n("midcom");
- $GLOBALS["view_l10n"] = $this->_l10n;
- $GLOBALS["view_l10n_midcom"] = $this->_l10n_midcom;
- $this->errcode = MIDCOM_ERROK;
- $this->errstr = "";
- $this->_local_toolbar =& $GLOBALS['midcom_admin_content_toolbar_component'];
- $this->_topic_toolbar =& $GLOBALS['midcom_admin_content_toolbar_main'];
- }
- /* functions called by the proxy class (contentadmin) */
- function can_handle($argc, $argv)
- {
- debug_push($this->_debug_prefix."can_handle");
- $GLOBALS["midcom"]->set_custom_context_data("configuration", $this->_config);
- $GLOBALS["midcom"]->set_custom_context_data("l10n", $this->_l10n);
- $GLOBALS["midcom"]->set_custom_context_data("l10n_midcom", $this->_l10n_midcom);
- $GLOBALS["midcom"]->set_custom_context_data("errstr", $this->errstr);
- debug_pop();
- if ($argc == 0)
- {
- return true;
- }
- switch ($argv[0])
- {
- case "upload":
- case "config":
- case "rereadexif":
- case "synccreated":
- case 'cleanupnames':
- case 'rebuildthumbs':
- case 'notes_locale':
- return ($argc == 1);
- case "edit":
- case "delete":
- case 'rotate_cw':
- case 'rotate_ccw':
- case 'notes_save':
- case 'notes':
- return ($argc == 2);
- default:
- return false;
- }
- }
- function handle($argc, $argv)
- {
- debug_push($this->_debug_prefix . "handle");
- /* Add the topic configuration item */
- $this->_topic_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => 'config.html',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
- MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- /* Add the new article link at the beginning*/
- $this->_topic_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => 'upload.html',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('upload photos'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/images.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ), 0);
- // pass startfrom value
- if (isset($_REQUEST) && array_key_exists("startfrom", $_REQUEST))
- {
- $this->_startfrom = $_REQUEST["startfrom"];
- }
- else
- {
- $this->_startfrom = 0;
- }
- if ($this->_startfrom % $this->_numpics > 0)
- {
- $this->_startfrom -= ($this->_startfrom % $this->_numpics);
- }
- if ($argc == 0)
- {
- debug_pop();
- return $this->_init_index();
- }
- switch ($argv[0]) {
- case "upload":
- $result = $this->_init_upload();
- break;
- case "config":
- $result = $this->_init_config();
- break;
- case "rereadexif":
- $result = $this->_init_reread_exif_timestamps();
- break;
- case "synccreated":
- $result = $this->_init_sync_article_created();
- break;
- case 'cleanupnames':
- $result = $this->_init_cleanup_names();
- break;
- case 'rebuildthumbs':
- $result = $this->_init_rebuild_thumbs();
- break;
- case "edit":
- $result = $this->_init_edit($argv[1]);
- break;
- case "delete":
- $result = $this->_init_delete($argv[1]);
- break;
- case 'rotate_cw':
- $result = $this->_init_rotate(90, $argv[1]);
- break;
- case 'rotate_ccw':
- $result = $this->_init_rotate(-90, $argv[1]);
- break;
- case 'notes_locale':
- $this->_show_notes_locale();
- break;
- case 'notes_save':
- $result = $this->_init_notes_save($argv[1]);
- break;
- case 'notes':
- $result = $this->_show_notes($argv[1]);
- break;
- default:
- $result = false;
- break;
- }
- debug_pop();
- return $result;
- }
- function show() {
- global $view_title;
- // get l10n libraries
- $i18n =& $GLOBALS["midcom"]->get_service("i18n");
- $GLOBALS["view_l10n"] = $i18n->get_l10n("net.siriux.photos");
- $GLOBALS["view_l10n_midcom"] = $i18n->get_l10n("midcom");
- $view_title = $this->_topic->extra;
- eval("\$result = \$this->_show_{$this->_view}();");
- return $result;
- }
- /* init functions, handle request and prepare output */
- /**
- * Rotate image init function, relocates to the index by default. Rotation
- * is controllable through the callee.
- *
- * @param int $degrees Number of degrees to rotate the image.
- * @param int $id The ID of the image to rotate.
- * @return bool false on failure, success relocates back to the AIS index.
- * @todo Make relocation target flexible
- */
- function _init_rotate ($degrees, $id)
- {
- debug_add("Rotating Photo {$id} {$degrees}° clockwise.", MIDCOM_LOG_DEBUG);
- $photo = new siriux_photos_Photo($id);
- if ($photo)
- {
- debug_add('Photo loaded, executing $photo->rotate');
- $photo->rotate($degrees);
- // Flush MidCOM cache
- debug_add("Invalidating MidCOM cache", MIDCOM_LOG_DEBUG);
- $GLOBALS['midcom']->cache->invalidate($photo->article->guid());
- $GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
- . "?startfrom={$this->_startfrom}");
- // This will exit
- }
- else
- {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = "Could not load Photo {$id}: " . mgd_errstr();
- return false;
- }
- }
- function _init_index() {
- debug_push($this->_debug_prefix . "_init_index");
- $this->_view = "index";
- // handle approval
- if (isset($_REQUEST))
- {
- if (array_key_exists("approve", $_REQUEST))
- {
- debug_add("Approving Photo " . $_REQUEST["approve"], MIDCOM_LOG_DEBUG);
- $article = mgd_get_article($_REQUEST['approve']);
- if ($article)
- {
- $meta = midcom_helper_metadata::retrieve($article);
- $meta->approve();
- }
- else
- {
- debug_add("Could not approve Photo " . $_REQUEST["approve"], MIDCOM_LOG_WARN);
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT, "Could not approve Photo {$_REQUEST['approve']}: " . mgd_errstr());
- }
- }
- else if (array_key_exists("unapprove", $_REQUEST))
- {
- debug_add("Unapproving Photo " . $_REQUEST["unapprove"], MIDCOM_LOG_DEBUG);
- $article = mgd_get_article($_REQUEST['unapprove']);
- if ($article)
- {
- $meta = midcom_helper_metadata::retrieve($article);
- $meta->unapprove();
- }
- else
- {
- debug_add("Could not unapprove Photo " . $_REQUEST["unapprove"], MIDCOM_LOG_WARN);
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT, "Could not unapprove Photo {$_REQUEST['unapprove']}: " . mgd_errstr());
- }
- }
- }
- debug_pop();
- return true;
- }
- function _show_index() {
- global $view_ids;
- global $view_prev;
- global $view_next;
- global $view_startfrom;
- $GLOBALS['view_thumbs_x'] = 1;
- $GLOBALS['view_thumbs_y'] = $this->_numpics;
- $view_ids = false;
- $view_prev = -1;
- $view_next = -1;
- $view_startfrom = $this->_startfrom;
- $articles = mgd_list_topic_articles($this->_topic->id, $this->_config->get("sort_order"));
- if ($articles) {
- $GLOBALS['view_total'] = $articles->N;
- $count = 0;
- while ($articles->fetch())
- {
- if ( $count >= $this->
- _startfrom
- && $count < ($this->_startfrom + $this->_numpics))
- {
- $view_ids[] = $articles->id;
- }
- $count++;
- }
- }
- midcom_show_style("admin_index");
- }
- function _init_upload() {
- debug_push($this->_debug_prefix . "_init_upload");
- $this->_view = "upload";
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => '',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- $this->_local_toolbar->disable_view_page();
- /* We create a datamanager who's sole purpose is to display an
- * uplaod form here. The specificied callback does not exist,
- * processing the resulting form is not an option here therefore,
- * the upload handler will do some hacks here to do the true
- * upload.
- */
- $GLOBALS["view_l10n"] = $this->_l10n;
- $this->_upload_datamanager = new midcom_helper_datamanager($this->_config->get("schemadb"));
- if (! $this->_upload_datamanager) {
- $this->errstr = "Failed to create the upload datamanager instance. Aborting.";
- $this->errcode = MIDCOM_ERRCRIT;
- return false;
- }
- if (! $this->_upload_datamanager->init_creation_mode($this->_config->get("schema_upload"), $this, "xxx")) {
- $this->errstr = "Failed to initialize the upload datamanager instance. Aborting.";
- $this->errcode = MIDCOM_ERRCRIT;
- return false;
- }
- if (array_key_exists('upload_complete', $_REQUEST))
- {
- $GLOBALS['view_contentmgr']->msg .= $_REQUEST['upload_complete'];
- }
- $this->_view_status = $this->_process_upload();
- debug_pop();
- return true;
- }
- function _process_upload() {
- debug_push($this->_debug_prefix . "_process_upload");
- /* This function handles the upload in a rather hacky way:
- *
- * The data, that has been submitted using the upload datamanager
- * will not be evaluated by that datamanager. Instead, the uploaded file
- * will be processed manually, and a new datamanager without the
- * upload field but with (at least) all other fields will be used instead.
- *
- * This is some averagly serious hack of the DM, and should be handled
- * with its due respect. If in doubt, ask me (torben@nehmer.net).
- *
- * If we have mulitple upload files, we display a progress log and redirect
- * to the (now empty) upload form. This is neccessary as Apache would kill
- * us after 2 mins of no-output-time.
- */
- /* 1st, we determine if we had a successfully posted request, if not, exit here */
- if (! array_key_exists("midcom_helper_datamanager_submit", $_REQUEST)) {
- debug_add("No upload form was submitted, exiting");
- debug_pop();
- return "";
- }
- /* 2nd, do a sanity check against the uploaded file */
- if (! array_key_exists("midcom_helper_datamanager_field_upload_file", $_FILES)) {
- debug_add("No file was uploaded.");
- debug_pop();
- return "No file was uploaded.<br />\n";
- }
- /* 3rd, check if we need to create a subtopic */
- $create_subgallery = FALSE;
- if ( array_key_exists("midcom_helper_datamanager_field_create_subgallery", $_REQUEST)
- && $_REQUEST["midcom_helper_datamanager_field_create_subgallery"] == "on")
- {
- $create_subgallery = TRUE;
- }
- $midgard = $GLOBALS["midcom"]->midgard;
- $result = "";
- $file = $_FILES["midcom_helper_datamanager_field_upload_file"];
- debug_print_r("Processing upload of this file:", $file);
- // Ignore client-side aborts for now, to avoid incomplete uploads due to the
- // Client Timeouts until the upload sequence is complete.
- debug_add('Disabling script abort through client.');
- ignore_user_abort(true);
- /* Scan for compressed files using the file name extension */
- $file_extensions = explode(".",strtolower($file["name"]));
- $file_extension = $file_extensions[((count($file_extensions)) - 1)];
- $compressed = false;
- $uploadtempdir = "/tmp/net.siriux.photos.upload." . time() . "." . getmypid();
- switch ($file_extension)
- {
- case "zip":
- $compressed = true;
- $unzipcmd =
- "{$GLOBALS['midcom_config']['utility_unzip']} "
- . escapeshellarg($file['tmp_name'])
- . " -d{$uploadtempdir}";
- break;
- case "gz":
- case "tgz":
- $compressed = true;
- $unzipcmd =
- "cd {$uploadtempdir} ; {$GLOBALS['midcom_config']['utility_tar']} -xzf "
- . escapeshellarg($file["tmp_name"]);
- break;
- default:
- $compressed = false;
- $uncompresscmd = "";
- break;
- }
- /* Depending on wether we have a compressed file, we do now
- * create a list of files to process. (So this could very well
- * be a single file.
- */
- $files = Array();
- if ($compressed)
- {
- $GLOBALS['midcom']->cache->content->enable_live_mode();
- $title = $this->_l10n->get('processing upload, please wait');
- ?>
- <html>
- <body>
- <h1><?php echo $title; ?>...</h1>
- <pre>
- Extracting archive...<?php
- flush();
- debug_add("executing upzip command: $unzipcmd");
- exec("mkdir {$uploadtempdir}");
- exec($unzipcmd);
- $find_result = Array();
- exec("{$GLOBALS['midcom_config']['utility_find']} {$uploadtempdir} -type f", $find_result);
- foreach ($find_result as $fullname)
- {
- if (! $this->check_uploaded_image($fullname))
- {
- // Not a valid image recognized by imagemagick.
- continue;
- }
- $lastslash = strrpos($fullname, '/');
- // We should always have at least two slashes, see the
- // definition of uploadtempdir above.
- $file = substr($fullname, $lastslash + 1);
- $files[$fullname] = $file;
- }
- echo " done\n";
- flush();
- }
- else
- {
- $files[$file["tmp_name"]] = $file["name"];
- }
- debug_print_r("We have to process these " . count($files) . " files:", $files);
- if ($create_subgallery)
- {
- // Create subgallery as user requested
- $subgallery = mgd_get_topic();
- $subgallery->up = $this->_topic->id;
- // Figure out a title for the topic
- if ($_REQUEST["midcom_helper_datamanager_field_title"] != "")
- {
- $subgallery->extra = $_REQUEST["midcom_helper_datamanager_field_title"];
- }
- else
- {
- // No user-supplied title for the gallery, use uploaded file's name
- // TODO: Remove file extension from the name
- $subgallery->extra = $_FILES["midcom_helper_datamanager_field_upload_file"]["name"];
- }
- $subgallery->name = midcom_generate_urlname_from_string($subgallery->extra);
- $subgallery_created = $subgallery->create();
- if (!$subgallery_created)
- {
- $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT,
- "Subgallery creation failed, reason " . mgd_errstr());
- // This will exit
- }
- $subgallery = mgd_get_topic($subgallery_created);
- $subgallery->parameter("midcom","component","net.siriux.photos");
- // Copy local gallery configuration
- $local_configuration = $this->_topic->listparameters("net.siriux.photos");
- while ( $local_configuration
- && $local_configuration->fetch())
- {
- $subgallery->parameter("net.siriux.photos", $local_configuration->name, $this->_topic->parameter("net.siriux.photos", $local_configuration->name));
- }
- }
- // Handle the upload, process each file in the array.
- $i = 0;
- $files_to_do = count($files) + 1;
- $file_at_work = 0;
- foreach ($files as $filename => $realname)
- {
- debug_add("Processing image {$realname} from {$filename}", MIDCOM_LOG_INFO);
- $file_at_work++;
- if ($compressed)
- {
- echo "Processing image {$realname} ({$file_at_work}/{$files_to_do})...";
- flush();
- }
- // Update script execution time
- set_time_limit(30);
- // Instantiate the photo object
- if ($create_subgallery)
- {
- // Create the photos into the newly created subgallery
- $photo = new siriux_photos_Photo(FALSE, null, $subgallery);
- }
- else
- {
- // Create the photos into this gallery
- $photo = new siriux_photos_Photo();
- }
- if ($photo->create_from_datamanager($filename, $realname))
- {
- $i++;
- if ($photo->article->title != "")
- {
- if (count($files) > 1)
- {
- $photo->article->title .= " {$i}";
- }
- }
- else
- {
- $photo->article->title = $photo->article->name;
- }
- $photo->article->update();
- // Recreate URL name from title only if mandated by config.
- if ($this->_config->get("create_urlname_from_title"))
- {
- $photo->article->name = midcom_generate_urlname_from_string($photo->article->title);
- $photo->article->update();
- }
- debug_add("Sucessfully uploaded {$filename} from {$realname}");
- if ($compressed)
- {
- // Report only failures during batch processing
- echo " done\n";
- flush();
- }
- else
- {
- $result .= "Successfully uploaded {$realname}<br\>\n";
- }
- }
- else
- {
- debug_add("Failed to upload {$filename} from {$realname}: {$this->errstr}", MIDCOM_LOG_WARN);
- $result .= "Failed to upload {$realname}: {$this->errstr}<br\>\n";
- if ($compressed)
- {
- echo " <span style='color:red;'>failed: {$this->errstr}</span>\n";
- flush();
- }
- }
- $photo->datamanager->destroy();
- }
- // Reset script execution time limit to some sane value for the rest
- // of the script.
- set_time_limit(20);
- /* Clean up all temporary files we are responsible for. */