plugins/gallery/upload.php
changeset 0 7caf561c50ee
child 2 88c954d2846c
equal deleted inserted replaced
-1:000000000000 0:7caf561c50ee
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Snapr
       
     5  * Version 0.1 beta 1
       
     6  * Copyright (C) 2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 ##
       
    16 ## UPLOAD INTERFACE
       
    17 ##
       
    18 
       
    19 $plugins->attachHook('base_classes_initted', '
       
    20   global $paths;
       
    21     $paths->add_page(Array(
       
    22       \'name\'=>\'Image gallery upload\',
       
    23       \'urlname\'=>\'GalleryUpload\',
       
    24       \'namespace\'=>\'Special\',
       
    25       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
       
    26       ));
       
    27   ');
       
    28 
       
    29 function page_Special_GalleryUpload()
       
    30 {
       
    31   global $db, $session, $paths, $template, $plugins; // Common objects
       
    32   
       
    33   if ( $session->user_level < USER_LEVEL_ADMIN )
       
    34   {
       
    35     die_friendly('Access denied', '<p>You need to have administrative rights to use the gallery\'s upload features.</p>');
       
    36   }
       
    37   
       
    38   $zip_support = ( class_exists('ZipArchive') || ( file_exists('/usr/bin/unzip') && is_executable('/usr/bin/unzip') ) );
       
    39   
       
    40   $errors = array();
       
    41   $template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/gallery/dropdown.css" />');
       
    42   $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/gallery/gallery-bits.js"></script>');
       
    43   
       
    44   if ( isset($_GET['edit_img']) )
       
    45   {
       
    46     $edit_parms = $_GET['edit_img'];
       
    47     $regex = '/^((([0-9]+),)*)?([0-9]+?)$/';
       
    48     if ( !preg_match($regex, $edit_parms) )
       
    49     {
       
    50       die_friendly('Bad request', '<p>$_GET[\'edit_img\'] must be a comma-separated list of image IDs.</p>');
       
    51     }
       
    52     
       
    53     $idlist = explode(',', $edit_parms);
       
    54     $num_edit = count($idlist);
       
    55     $idlist = "SELECT img_id,img_title,img_desc,img_filename,is_folder FROM ".table_prefix."gallery WHERE img_id=" . implode(' OR img_id=', $idlist) . ';';
       
    56     
       
    57     if ( !$e = $db->sql_query($idlist) )
       
    58       $db->_die();
       
    59     
       
    60     $template->header();
       
    61     
       
    62     if ( isset($_POST['edit_do_save']) )
       
    63     {
       
    64       @set_time_limit(0);
       
    65       
       
    66       $arr_img_data = array();
       
    67       while ( $row = $db->fetchrow($e) )
       
    68         $arr_img_data[$row['img_id']] = $row;
       
    69       
       
    70       // Allow breaking out
       
    71       switch(true):case true:
       
    72         
       
    73         if ( !is_array($_POST['img']) )
       
    74         {
       
    75           $errors[] = 'No images passed to processor.';
       
    76           break;
       
    77         }
       
    78         
       
    79         // Main updater loop
       
    80         foreach ( $_POST['img'] as $img_id => $img_data )
       
    81         {
       
    82           
       
    83           if ( !preg_match('/^[0-9]+$/', $img_id) )
       
    84           {
       
    85             $errors[] = 'SQL injection attempted!';
       
    86             break 2;
       
    87           }
       
    88           
       
    89           // Array of columns to update
       
    90           $to_update = array();
       
    91           
       
    92           $key = 'reupload_' . $img_data['id'];
       
    93           if ( isset($_FILES[$key]) )
       
    94           {
       
    95             $file =& $_FILES[ $key ];
       
    96             if ( $file['tmp_name'] != '' )
       
    97             {
       
    98               // Reupload
       
    99               $filename = ENANO_ROOT . '/files/' . $arr_img_data[ $img_data['id'] ]['img_filename'];
       
   100               if ( !unlink($filename) )
       
   101               {
       
   102                 $errors[] = "Could not delete $filename";
       
   103                 break 2;
       
   104               }
       
   105               if ( !@move_uploaded_file($file['tmp_name'], $filename) )
       
   106               {
       
   107                 $errors[] = "Could not move uploaded file to $filename";
       
   108                 break 2;
       
   109               }
       
   110               
       
   111               //
       
   112               // Create scaled images
       
   113               //
       
   114               
       
   115               // Create thumbnail image
       
   116               $thumb_filename = ENANO_ROOT . '/cache/' . $arr_img_data[ $img_data['id'] ]['img_filename'] . '-thumb.jpg';
       
   117               if ( !unlink($thumb_filename) )
       
   118               {
       
   119                 $errors[] = "Could not delete $thumb_filename";
       
   120                 break 2;
       
   121               }
       
   122               $magick = getConfig('imagemagick_path');
       
   123               $command = "$magick '{$filename}' -resize ".'"'."80x80>".'"'." -quality 85 $thumb_filename";
       
   124               
       
   125               @system($command, $stat);
       
   126               
       
   127               if ( !file_exists($thumb_filename) )
       
   128               {
       
   129                 $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   130                 break 2;
       
   131               }
       
   132               
       
   133               // Create preview image
       
   134               $preview_filename = ENANO_ROOT . '/cache/' . $arr_img_data[ $img_data['id'] ]['img_filename'] . '-preview.jpg';
       
   135               if ( !unlink($preview_filename) )
       
   136               {
       
   137                 $errors[] = "Could not delete $preview_filename";
       
   138                 break 2;
       
   139               }
       
   140               $magick = getConfig('imagemagick_path');
       
   141               $command = "$magick '{$filename}' -resize ".'"'."640x640>".'"'." -quality 85 $preview_filename";
       
   142               
       
   143               @system($command, $stat);
       
   144               
       
   145               if ( !file_exists($preview_filename) )
       
   146               {
       
   147                 $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   148                 break 2;
       
   149               }
       
   150               $to_update['img_time_mod'] = strval(time());
       
   151             }
       
   152           }
       
   153           
       
   154           $vars = array(
       
   155             'year' => date('Y'),
       
   156             'month' => date('F'),
       
   157             'day' => date('d'),
       
   158             'time12' => date('g:i A'),
       
   159             'time24' => date('G:i')
       
   160           );
       
   161           
       
   162           // Image name/title
       
   163           
       
   164           $title = $template->makeParserText($img_data['title']);
       
   165           $title->assign_vars($vars);
       
   166           $executed = $title->run();
       
   167           if ( $executed == '_id' )
       
   168           {
       
   169             $errors[] = 'You cannot name an image or folder "_id", this name is reserved for internal functions.';
       
   170             break 2;
       
   171           }
       
   172           if ( $executed == '' )
       
   173           {
       
   174             $errors[] = 'Please enter a name for the item with unique ID ' . $img_data['id'] . '. <pre>' . print_r($_POST,true) . '</pre>';
       
   175             break 2;
       
   176           }
       
   177           $to_update['img_title'] = $executed;
       
   178           
       
   179           // Image description
       
   180           
       
   181           if ( isset($img_data['desc']) )
       
   182           {
       
   183             $desc = $template->makeParserText($img_data['desc']);
       
   184             $desc->assign_vars($vars);
       
   185             $executed = $desc->run();
       
   186             $executed = RenderMan::preprocess_text($executed, false, false);
       
   187             $to_update['img_desc'] = $executed;
       
   188           }
       
   189           
       
   190           // Folder
       
   191           $target_folder = false;
       
   192           
       
   193           if ( !empty($_POST['override_folder']) )
       
   194           {
       
   195             if ( $_POST['override_folder'] == 'NULL' || preg_match('/^[0-9]+$/', $_POST['override_folder']) )
       
   196             {
       
   197               $target_folder = $_POST['override_folder'];
       
   198             }
       
   199           }
       
   200           
       
   201           if ( !empty($img_data['folder']) )
       
   202           {
       
   203             if ( $img_data['folder'] == 'NULL' || preg_match('/^[0-9]+$/', $img_data['folder']) )
       
   204             {
       
   205               $target_folder = $img_data['folder'];
       
   206             }
       
   207           }
       
   208           
       
   209           if ( $target_folder )
       
   210           {
       
   211             $to_update['folder_parent'] = $target_folder;
       
   212           }
       
   213           
       
   214           if ( count($to_update) > 0 )
       
   215           {
       
   216             $up_keys = array_keys($to_update);
       
   217             $up_vals = array_values($to_update);
       
   218             
       
   219             $bin_cols = array('folder_parent');
       
   220             
       
   221             $sql = 'UPDATE ' . table_prefix.'gallery SET ';
       
   222             
       
   223             foreach ( $up_keys as $i => $key )
       
   224             {
       
   225               if ( in_array($key, $bin_cols) )
       
   226               {
       
   227                 $sql .= $key . '=' . $up_vals[$i] . ',';
       
   228               }
       
   229               else
       
   230               {
       
   231                 $sql .= $key . '=\'' . $db->escape($up_vals[$i]) . '\',';
       
   232               }
       
   233             }
       
   234             
       
   235             $sql = preg_replace('/,$/i', '', $sql) . ' WHERE img_id=' . $img_data['id'] . ';';
       
   236             
       
   237             if ( !$db->sql_query($sql) )
       
   238             {
       
   239               $db->_die();
       
   240             }
       
   241             
       
   242           }
       
   243           
       
   244         }
       
   245         
       
   246         echo '<div class="info-box" style="margin-left: 0;">Your changes have been saved.</div>';
       
   247         
       
   248       endswitch;
       
   249       
       
   250       // Rerun select query to make sure information in PHP memory is up-to-date
       
   251       if ( !$e = $db->sql_query($idlist) )
       
   252         $db->_die();
       
   253       
       
   254     }
       
   255     
       
   256     if ( count($errors) > 0 )
       
   257     {
       
   258       echo '<div class="error-box" style="margin-left: 0;">
       
   259               <b>The following errors were encountered while updating the image data:</b><br />
       
   260               <ul>
       
   261                 <li>' . implode("</li>\n        <li>", $errors) . '</li>
       
   262               </ul>
       
   263             </div>';
       
   264     }
       
   265     
       
   266     ?>
       
   267     <script type="text/javascript">
       
   268     
       
   269       function gal_unset_radios(name)
       
   270       {
       
   271         var radios = document.getElementsByTagName('input');
       
   272         for ( var i = 0; i < radios.length; i++ )
       
   273         {
       
   274           var radio = radios[i];
       
   275           if ( radio.name == name )
       
   276           {
       
   277             radio.checked = false;
       
   278           }
       
   279         }
       
   280       }
       
   281     
       
   282     </script>
       
   283     <?php
       
   284     
       
   285     echo '<form action="' . makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $edit_parms, true) . '" method="post" enctype="multipart/form-data">';
       
   286     
       
   287     if ( $row = $db->fetchrow($e) )
       
   288     {
       
   289       
       
   290       echo '<div class="tblholder">
       
   291               <table border="0" cellspacing="1" cellpadding="4">';
       
   292       echo '    <tr><th class="subhead">Information</th></tr>';
       
   293       echo '    <tr><td class="row3">
       
   294                   As with the upload form, the following variables can be used. <b>Note that when editing images, the {id} and {autotitle} variables will be ignored.</b>';
       
   295       ?>
       
   296           <ul>
       
   297             <li>{year}: The current year (<?php echo date('Y'); ?>)</li>
       
   298             <li>{month}: The current month (<?php echo date('F'); ?>)</li>
       
   299             <li>{day}: The day of the month (<?php echo date('d'); ?>)</li>
       
   300             <li>{time12}: 12-hour time (<?php echo date('g:i A'); ?>)</li>
       
   301             <li>{time24}: 24-hour time (<?php echo date('G:i'); ?>)</li>
       
   302           </ul>
       
   303       <?php
       
   304       echo '        </td></tr>';
       
   305       echo '  </table>
       
   306             </div>';
       
   307       
       
   308       $i = 0;
       
   309       do
       
   310       {
       
   311         $thumb_url = makeUrlNS('Special', 'GalleryFetcher/thumb/' . $row['img_id'], false, true);
       
   312         
       
   313         # Type: folder
       
   314         if ( $row['is_folder'] == 1 ):
       
   315         
       
   316         // Image ID tracker
       
   317         echo '<input type="hidden" name="img[' . $i . '][id]" value="' . $row['img_id'] . '" />';
       
   318         
       
   319         //
       
   320         // Editor table
       
   321         //
       
   322         
       
   323         $folders = gallery_imgid_to_folder(intval($row['img_id']));
       
   324         foreach ( $folders as $j => $xxx )
       
   325         {
       
   326           $folder =& $folders[$j];
       
   327           $folder = sanitize_page_id($folder);
       
   328         }
       
   329         $folders = array_reverse($folders);
       
   330         $gal_href = implode('/', $folders) . '/' . sanitize_page_id($row['img_title']);
       
   331         
       
   332         echo '<div class="tblholder">
       
   333                 <table border="0" cellspacing="1" cellpadding="4">';
       
   334         
       
   335         echo '<tr><th colspan="2">Folder: ' . htmlspecialchars($row['img_title']) . '</th></tr>';
       
   336         
       
   337         // Primary key
       
   338         echo '<tr>
       
   339                 <td class="row2">Unique ID:</td>
       
   340                 <td class="row1">' . $row['img_id'] . ' (<a href="' . makeUrlNS('Special', 'Gallery/' . $gal_href) . '">view folder contents</a>)</td>
       
   341               </tr>';
       
   342               
       
   343         // Path info
       
   344         echo '<tr>
       
   345                 <td class="row2">Parent folders:</td>
       
   346                 <td class="row1">' . /* Yeah it's dirty, but hey, it gets the job done ;-) */ ( ( $x = str_replace('&amp;raquo;', '&raquo;', htmlspecialchars(str_replace('_', ' ', implode(' &raquo; ', $folders)))) ) ? $x : '&lt;in root&gt;' ) . '</td>
       
   347               </tr>';
       
   348         
       
   349         // Image name
       
   350         
       
   351         echo '<tr>
       
   352                 <td class="row2">Folder name:</td>
       
   353                 <td class="row1"><input type="text" style="width: 98%;" name="img[' . $i . '][title]" value="' . htmlspecialchars($row['img_title']) . '" size="43" /></td>
       
   354               </tr>';
       
   355               
       
   356         // Mover widget
       
   357         ?>
       
   358         <tr>
       
   359           <td class="row2">Move to folder:</td>
       
   360           <td class="row1">
       
   361             <div class="toggle">
       
   362               <div class="head" onclick="gal_toggle(this.nextSibling.nextSibling, this.childNodes[1]);">
       
   363                 <img alt="&gt;&gt;" src="<?php echo scriptPath; ?>/plugins/gallery/toggle-closed.png" class="toggler" />
       
   364                 Select folder
       
   365               </div>
       
   366               <div class="body">
       
   367                 <?php
       
   368                   echo gallery_hier_formfield('img[' . $i . '][folder]', false);
       
   369                 ?>
       
   370                 <br />
       
   371                 <a href="#" onclick="gal_unset_radios('img[<?php echo $i; ?>][folder]'); return false;">Unselect field</a>
       
   372               </div>
       
   373             </div>
       
   374           </td>
       
   375         </tr>
       
   376         <?php
       
   377         
       
   378         // Finish table
       
   379         echo '</table>';
       
   380         echo '</div>';
       
   381         
       
   382         # Type: image
       
   383         else:
       
   384         
       
   385         // Image ID tracker
       
   386         echo '<input type="hidden" name="img[' . $i . '][id]" value="' . $row['img_id'] . '" />';
       
   387         
       
   388         //
       
   389         // Editor table
       
   390         //
       
   391         
       
   392         echo '<div class="tblholder">
       
   393                 <table border="0" cellspacing="1" cellpadding="4">';
       
   394         
       
   395         echo '<tr><th colspan="2">Image: ' . htmlspecialchars($row['img_title']) . '</th></tr>';
       
   396         
       
   397         // Primary key
       
   398         echo '<tr>
       
   399                 <td class="row2">Unique ID:</td>
       
   400                 <td class="row1">' . $row['img_id'] . ' (<a href="' . makeUrlNS('Gallery', $row['img_id']) . '">view image\'s page</a>)</td>
       
   401               </tr>';
       
   402               
       
   403         // Thumbnail
       
   404         
       
   405         echo '<tr>
       
   406                 <td class="row2">Thumbnail:</td>
       
   407                 <td class="row1"><img alt="Thumbnail image" src="' . $thumb_url . '" /></td>
       
   408               </tr>';
       
   409         
       
   410         // Image name
       
   411         
       
   412         echo '<tr>
       
   413                 <td class="row2">Image title:</td>
       
   414                 <td class="row1"><input type="text" style="width: 98%;" name="img[' . $i . '][title]" value="' . htmlspecialchars($row['img_title']) . '" size="43" /></td>
       
   415               </tr>';
       
   416               
       
   417         // Image description
       
   418         
       
   419         echo '<tr>
       
   420                 <td class="row2">Image description:</td>
       
   421                 <td class="row1"><textarea rows="10" cols="40" style="width: 98%;" name="img[' . $i . '][desc]">' . htmlspecialchars($row['img_desc']) . '</textarea></td>
       
   422               </tr>';
       
   423               
       
   424         // ACL editor trigger
       
   425         
       
   426         echo '<tr>
       
   427                 <td class="row2">Permissions:</td>
       
   428                 <td class="row1"><input type="button" onclick="ajaxOpenACLManager(\'' . $row['img_id'] . '\', \'Gallery\');" value="Edit permissions" /><br /><small>Only works in Firefox 1.5 or later, Safari 3.x or later, or Opera 9.0 or later.</small></td>
       
   429               </tr>';
       
   430               
       
   431         // Mover widget
       
   432         ?>
       
   433         <tr>
       
   434           <td class="row2">Move to folder:</td>
       
   435           <td class="row1">
       
   436             <div class="toggle">
       
   437               <div class="head" onclick="gal_toggle(this.nextSibling.nextSibling, this.childNodes[1]);">
       
   438                 <img alt="&gt;&gt;" src="<?php echo scriptPath; ?>/plugins/gallery/toggle-closed.png" class="toggler" />
       
   439                 Select folder
       
   440               </div>
       
   441               <div class="body">
       
   442                 <?php
       
   443                   echo gallery_hier_formfield('img[' . $i . '][folder]', false);
       
   444                 ?>
       
   445                 <br />
       
   446                 <a href="#" onclick="gal_unset_radios('img[<?php echo $i; ?>][folder]'); return false;">Unselect field</a>
       
   447               </div>
       
   448             </div>
       
   449           </td>
       
   450         </tr>
       
   451         <?php
       
   452               
       
   453         // File replacer
       
   454         
       
   455         echo '<tr>
       
   456                 <td class="row2">Upload new version:</td>
       
   457                 <td class="row1"><input type="file" name="reupload_' . $row['img_id'] . '" size="30" style="width: 98%;" /></td>
       
   458               </tr>';
       
   459               
       
   460         // Finish table
       
   461         echo '</table>';
       
   462         echo '</div>';
       
   463         
       
   464         endif;
       
   465         
       
   466         $i++;
       
   467       }
       
   468       while ( $row = $db->fetchrow($e) );
       
   469       $db->free_result();
       
   470       
       
   471       echo '<div class="tblholder">
       
   472               <table border="0" cellspacing="1" cellpadding="4">';
       
   473       // Mover widget
       
   474       if ( $num_edit > 1 ):
       
   475       ?>
       
   476       <tr>
       
   477         <td class="row2">Move all to folder:<br /><small>Other folder fields on this page can override this for individual images.</small></td>
       
   478         <td class="row1" style="width: 70%;">
       
   479           <div class="toggle">
       
   480             <div class="head" onclick="gal_toggle(this.nextSibling.nextSibling, this.childNodes[1]);">
       
   481               <img alt="&gt;&gt;" src="<?php echo scriptPath; ?>/plugins/gallery/toggle-closed.png" class="toggler" />
       
   482               Select folder
       
   483             </div>
       
   484             <div class="body">
       
   485               <?php
       
   486                 echo gallery_hier_formfield('override_folder', false);
       
   487               ?>
       
   488               <br />
       
   489               <a href="#" onclick="gal_unset_radios('override_folder'); return false;">Unselect folder override field</a>
       
   490             </div>
       
   491           </div>
       
   492         </td>
       
   493       </tr>
       
   494       <?php
       
   495       endif;
       
   496         
       
   497       echo '    <tr><th class="subhead" colspan="2"><input type="submit" name="edit_do_save" value="Save changes" /></th></tr>';
       
   498       echo '  </table>
       
   499             </div>';
       
   500       
       
   501     }
       
   502     else
       
   503     {
       
   504       echo '<p>No images that matched the ID list could be found.</p>';
       
   505     }
       
   506     
       
   507     echo '</form>';
       
   508     
       
   509     $template->footer();
       
   510     return;
       
   511   }
       
   512   
       
   513   if ( isset($_GET['rm']) )
       
   514   {
       
   515     $warnings = array();
       
   516     
       
   517     if ( !preg_match('/^[0-9]+$/', $_GET['rm']) )
       
   518       die_friendly('Bad Request', '<p>$_GET[rm] needs to be an integer.</p>');
       
   519     
       
   520     $rm_id = intval($_GET['rm']);
       
   521     
       
   522     if ( isset($_POST['confirmed']) )
       
   523     {
       
   524       // The user confirmed the request. Start plowing through data to decide what to delete.
       
   525       
       
   526       // Array of images and folder rows to delete
       
   527       $del_imgs = array($rm_id);
       
   528       // Array of files to delete
       
   529       $del_files = array();
       
   530       // Array of comment entries to delete
       
   531       $del_comments = array();
       
   532       
       
   533       $all_children = gal_fetch_all_children($rm_id);
       
   534       $del_imgs = array_merge($del_imgs, $all_children);
       
   535       
       
   536       $imglist = 'img_id=' . implode(' OR img_id=', $del_imgs);
       
   537       $sql = "SELECT img_id, img_filename FROM ".table_prefix."gallery WHERE ( $imglist ) AND is_folder!=1;";
       
   538       
       
   539       if ( !$db->sql_query($sql) )
       
   540       {
       
   541         $db->_die();
       
   542       }
       
   543       
       
   544       while ( $row = $db->fetchrow() )
       
   545       {
       
   546         $files = array(
       
   547             ENANO_ROOT . '/files/' . $row['img_filename'],
       
   548             ENANO_ROOT . '/cache/' . $row['img_filename'] . '-thumb.jpg',
       
   549             ENANO_ROOT . '/cache/' . $row['img_filename'] . '-preview.jpg'
       
   550           );
       
   551         $del_files = array_merge($del_files, $files);
       
   552         
       
   553         $del_comments[] = intval($row['img_id']);
       
   554       }
       
   555       
       
   556       $commentlist = 'page_id=\'' . implode('\' OR page_id=\'', $del_imgs) . '\'';
       
   557       
       
   558       // Main deletion cycle
       
   559       
       
   560       foreach ( $del_files as $file )
       
   561       {
       
   562         @unlink($file) or $warnings[] = 'Could not delete file ' . $file;
       
   563       }
       
   564       
       
   565       if ( !$db->sql_query('DELETE FROM '.table_prefix.'gallery WHERE ' . $imglist . ';') )
       
   566       {
       
   567         $warnings[] = 'Main delete query failed: ' . $db->get_error();
       
   568       }
       
   569       
       
   570       if ( !$db->sql_query('DELETE FROM '.table_prefix.'comments WHERE ( ' . $commentlist . ' ) AND namespace=\'Gallery\';') )
       
   571       {
       
   572         $warnings[] = 'Comment delete query failed: ' . $db->get_error();
       
   573       }
       
   574       
       
   575       if ( count($warnings) > 0 )
       
   576       {
       
   577         $template->header();
       
   578         
       
   579         echo '<h3>Error during deletion process</h3>';
       
   580         echo '<p>The deletion process generated some warnings which are shown below.</p>';
       
   581         echo '<ul><li>' . implode('</li><li>', $warnings) . '</li></ul>';
       
   582         
       
   583         $template->footer();
       
   584       }
       
   585       else
       
   586       {
       
   587         redirect(makeUrlNS('Special', 'Gallery'), 'Deletion successful', 'The selected item has been deleted from the gallery. You will now be transferred to the gallery index.', 2);
       
   588       }
       
   589       
       
   590     }
       
   591     else
       
   592     {
       
   593       // Removal form
       
   594       $template->header();
       
   595       
       
   596       echo '<form action="' . makeUrlNS('Special', 'GalleryUpload', 'rm=' . $rm_id, true) . '" method="post" enctype="multipart/form-data">';
       
   597       
       
   598       echo '<h3>Are you sure you want to delete this item?</h3>';
       
   599       echo '<p>If you continue, this item will be permanently deleted from the gallery &ndash; no rollbacks.</p>';
       
   600       echo '<p>If this is an image, the image files will be removed from the filesystem, and all comments associated with the image will be deleted, as well as the image\'s title, description, and location.</p>';
       
   601       echo '<p>If this is a folder, all of its contents will be removed. Any images will be removed from the filesystem and all comments and metadata associated with images in this folder or any folders in it will be permanently deleted.</p>';
       
   602       
       
   603       echo '<p><input type="submit" name="confirmed" value="Continue with delete" /></p>';
       
   604       
       
   605       echo '</form>';
       
   606       
       
   607       $template->footer();
       
   608     }
       
   609     return;
       
   610   }
       
   611   
       
   612   if ( isset($_POST['do_stage2']) )
       
   613   {
       
   614     // Allow breaking out of the validation in the case of an error
       
   615     switch(true):case true:
       
   616       
       
   617       if ( empty($_POST['img_name']) )
       
   618       {
       
   619         $errors[] = 'Please enter an image name.';
       
   620       }
       
   621       
       
   622       // Validate files
       
   623       $n_files = intval($_POST['img_count']);
       
   624       if ( $n_files < 1 )
       
   625       {
       
   626         $errors[] = 'Cannot get image count';
       
   627         break;
       
   628       }
       
   629       
       
   630       $files = array();
       
   631       
       
   632       for ( $i = 0; $i < $n_files; $i++ )
       
   633       {
       
   634         $key = "img_$i";
       
   635         if ( isset($_FILES[$key]) && !empty($_FILES[$key]['name']) )
       
   636         {
       
   637           $files[] =& $_FILES[$key];
       
   638         }
       
   639       }
       
   640       
       
   641       if ( count($files) < 1 )
       
   642       {
       
   643         $errors[] = 'No files specified.';
       
   644         break;
       
   645       }
       
   646       
       
   647       $allowed = array('png', 'jpg', 'jpeg', 'tiff', 'tif', 'bmp', 'gif');
       
   648       $is_zip = false;
       
   649       foreach ( $files as $i => $file )
       
   650       {
       
   651         $ext = substr($file['name'], ( strrpos($file['name'], '.') + 1 ));
       
   652         $ext = strtolower($ext);
       
   653         if ( !in_array($ext, $allowed) && ( !$zip_support || ( $ext != 'zip' || $i > 0 ) ) )
       
   654         {
       
   655           $errors[] = htmlspecialchars($file['name']) . ' is an invalid extension (' . htmlspecialchars($ext) . ').';
       
   656         }
       
   657         else if ( $ext == 'zip' && $i == 0 && $zip_support )
       
   658         {
       
   659           $is_zip = true;
       
   660         }
       
   661       }
       
   662       
       
   663       if ( count($errors) > 0 )
       
   664       {
       
   665         // Send error messages
       
   666         break;
       
   667       }
       
   668       
       
   669       // Parent folder
       
   670       $folder = $_POST['folder_id'];
       
   671       if ( $folder != 'NULL' && !preg_match('/^[0-9]+$/', $folder) )
       
   672       {
       
   673         $folder = 'NULL';
       
   674       }
       
   675       
       
   676       // Format title and description fields
       
   677       $title = $template->makeParserText($_POST['img_name']);
       
   678       $desc  = $template->makeParserText($_POST['img_desc']);
       
   679       
       
   680       $vars = array(
       
   681           'year' => date('Y'),
       
   682           'month' => date('F'),
       
   683           'day' => date('d'),
       
   684           'time12' => date('g:i A'),
       
   685           'time24' => date('G:i')
       
   686         );
       
   687       
       
   688       $title->assign_vars($vars);
       
   689       $desc->assign_vars($vars);
       
   690       
       
   691       $idlist = array();
       
   692       
       
   693       // Try to disable the time limit
       
   694       @set_time_limit(0);
       
   695       
       
   696       // Move uploaded files to the files/ directory
       
   697       foreach ( $files as $i => $__trash )
       
   698       {
       
   699         $file =& $files[$i];
       
   700         $ext = substr($file['name'], ( strrpos($file['name'], '.') + 1 ));
       
   701         $ext = strtolower($ext);
       
   702         if ( $ext == 'zip' && $is_zip && $zip_support )
       
   703         {
       
   704           //
       
   705           // Time for some unzipping fun.
       
   706           //
       
   707           
       
   708           // for debugging only
       
   709           system('rm -fr ' . ENANO_ROOT . '/cache/temp');
       
   710           
       
   711           error_reporting(E_ALL);
       
   712           
       
   713           mkdir(ENANO_ROOT . '/cache/temp') or $errors[] = 'Could not create temporary directory for extraction.';
       
   714           if ( count($errors) > 0 )
       
   715             break 2;
       
   716           $temp_dir = tempnam(ENANO_ROOT . '/cache/temp', 'galunz');
       
   717           if ( file_exists($temp_dir) )
       
   718             unlink($temp_dir);
       
   719           @mkdir($temp_dir);
       
   720           
       
   721           // Extract the zip file
       
   722           if ( class_exists('ZipArchive') )
       
   723           {
       
   724             $zip = new ZipArchive();
       
   725             $op = $zip->open($file['tmp_name']);
       
   726             if ( !$op )
       
   727             {
       
   728               $errors[] = 'Could not open the zip file.';
       
   729               break 2;
       
   730             }
       
   731             $op = $zip->extractTo($temp_dir);
       
   732             if ( !$op )
       
   733             {
       
   734               $errors[] = 'Could not extract the zip file.';
       
   735               break 2;
       
   736             }
       
   737           }
       
   738           else if ( file_exists('/usr/bin/unzip') )
       
   739           {
       
   740             $cmd = "/usr/bin/unzip -qq -d $temp_dir {$file['tmp_name']}";
       
   741             system($cmd);
       
   742           }
       
   743           
       
   744           // Any files?
       
   745           $file_list = gal_dir_recurse($temp_dir, $dirs);
       
   746           if ( !$file_list )
       
   747           {
       
   748             $errors[] = 'Could not get file list for temp directory.';
       
   749             break 2;
       
   750           }
       
   751           if ( count($file_list) < 1 )
       
   752           {
       
   753             $errors[] = 'There weren\'t any files in the uploaded zip file.';
       
   754           }
       
   755           
       
   756           $dirs = array_reverse($dirs);
       
   757           $img_files = array();
       
   758           
       
   759           // Loop through and add files
       
   760           foreach ( $file_list as $file )
       
   761           {
       
   762             $ext = get_file_extension($file);
       
   763             
       
   764             if ( in_array($ext, $allowed) )
       
   765             {
       
   766               $img_files[] = $file;
       
   767             }
       
   768             else
       
   769             {
       
   770               unlink($file);
       
   771             }
       
   772           }
       
   773           
       
   774           // Main storage loop
       
   775           $j = 0;
       
   776           foreach ( $img_files as $file )
       
   777           {
       
   778             $ext = get_file_extension($file);
       
   779             $stored_name = gallery_make_filename() . ".$ext";
       
   780             $store = ENANO_ROOT . '/files/' . $stored_name;
       
   781             if ( !rename($file, $store) )
       
   782             {
       
   783               $errors[] = 'Could not move file ' . $file . ' to permanent storage location ' . $store . '.';
       
   784               break 3;
       
   785             }
       
   786             
       
   787             $autotitle = capitalize_first_letter(basename($file));
       
   788             $autotitle = substr($autotitle, 0, ( strrpos($autotitle, '.') ));
       
   789             $autotitle = str_replace('_', ' ', $autotitle);
       
   790             
       
   791             $title->assign_vars(array('id' => ( $j + 1 ), 'autotitle' => $autotitle));
       
   792             $desc->assign_vars(array('id' => ( $j + 1 ), 'autotitle' => $autotitle));
       
   793             
       
   794             $local_t = $title->run();
       
   795             $local_t = RenderMan::preprocess_text($local_t, true, false);
       
   796             
       
   797             $local_d = $desc->run();
       
   798             $local_d = RenderMan::preprocess_text($local_d, true, false);
       
   799             
       
   800             $subq = '(\'' . $stored_name . '\', \'' . $db->escape($local_t) . '\', \'' . $db->escape($local_d) . '\',\'a:0:{}\', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ' . $folder . ')';
       
   801             $sql = "INSERT INTO ".table_prefix."gallery(img_filename,img_title,img_desc,print_sizes,img_time_upload,img_time_mod,folder_parent) VALUES{$subq};";
       
   802             
       
   803             if ( !$db->sql_query($sql) )
       
   804               $db->_die();
       
   805             
       
   806             $idlist[] = $db->insert_id();
       
   807             
       
   808             // Create thumbnail image
       
   809             $thumb_filename = ENANO_ROOT . '/cache/' . $stored_name . '-thumb.jpg';
       
   810             $magick = getConfig('imagemagick_path');
       
   811             $command = "$magick '{$store}' -resize ".'"'."80x80>".'"'." -quality 85 $thumb_filename";
       
   812             
       
   813             @system($command, $stat);
       
   814             
       
   815             if ( !file_exists($thumb_filename) )
       
   816             {
       
   817               $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   818               break 2;
       
   819             }
       
   820             
       
   821             // Create preview image
       
   822             $preview_filename = ENANO_ROOT . '/cache/' . $stored_name . '-preview.jpg';
       
   823             $magick = getConfig('imagemagick_path');
       
   824             $command = "$magick '{$store}' -resize ".'"'."640x640>".'"'." -quality 85 $preview_filename";
       
   825             
       
   826             @system($command, $stat);
       
   827             
       
   828             if ( !file_exists($preview_filename) )
       
   829             {
       
   830               $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   831               break 2;
       
   832             }
       
   833             
       
   834             $j++;
       
   835           }
       
   836           
       
   837           // clean up
       
   838           foreach ( $dirs as $dir )
       
   839           {
       
   840             rmdir($dir);
       
   841           }
       
   842           
       
   843           rmdir( $temp_dir ) or $errors[] = 'Couldn\'t delete the unzip directory.';
       
   844           rmdir( ENANO_ROOT . '/cache/temp' ) or $errors[] = 'Couldn\'t delete the temp directory.';
       
   845           if ( count($errors) > 0 )
       
   846             break 2;
       
   847           
       
   848           $idlist = implode(',', $idlist);
       
   849           $url = makeUrlNS('Special', 'GalleryUpload', "edit_img=$idlist");
       
   850           
       
   851           redirect($url, 'Upload successful', 'Your images have been uploaded successfully. Please wait while you are transferred...', 2);
       
   852           
       
   853           break 2;
       
   854         }
       
   855         $file['stored_name'] = gallery_make_filename() . '.' . $ext;
       
   856         $store = ENANO_ROOT . '/files/' . $file['stored_name'];
       
   857         if ( !@move_uploaded_file($file['tmp_name'], $store) )
       
   858         {
       
   859           $errors[] = "[Internal] Couldn't move temporary file {$file['tmp_name']} to permanently stored file $store";
       
   860           break 2;
       
   861         }
       
   862         
       
   863         $autotitle = capitalize_first_letter(basename($file['name']));
       
   864         $autotitle = substr($autotitle, 0, ( strrpos($autotitle, '.') ));
       
   865         $autotitle = str_replace('_', ' ', $autotitle);
       
   866         
       
   867         $title->assign_vars(array('id' => ( $i + 1 ), 'autotitle' => $autotitle));
       
   868         $desc->assign_vars (array('id' => ( $i + 1 ), 'autotitle' => $autotitle));
       
   869         
       
   870         $local_t = $title->run();
       
   871         $local_t = RenderMan::preprocess_text($local_t, true, false);
       
   872         
       
   873         $local_d = $desc->run();
       
   874         $local_d = RenderMan::preprocess_text($local_d, true, false);
       
   875         
       
   876         $subq = '(\'' . $file['stored_name'] . '\', \'' . $db->escape($local_t) . '\', \'' . $db->escape($local_d) . '\',\'a:0:{}\', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ' . $folder . ')';
       
   877         $sql = "INSERT INTO ".table_prefix."gallery(img_filename,img_title,img_desc,print_sizes,img_time_upload,img_time_mod,folder_parent) VALUES{$subq};";
       
   878         
       
   879         if ( !$db->sql_query($sql) )
       
   880           $db->_die();
       
   881         
       
   882         $idlist[] = $db->insert_id();
       
   883         
       
   884         // Create thumbnail image
       
   885         $thumb_filename = ENANO_ROOT . '/cache/' . $file['stored_name'] . '-thumb.jpg';
       
   886         $magick = getConfig('imagemagick_path');
       
   887         $command = "$magick '{$store}' -resize ".'"'."80x80>".'"'." -quality 85 $thumb_filename";
       
   888         
       
   889         @system($command, $stat);
       
   890         
       
   891         if ( !file_exists($thumb_filename) )
       
   892         {
       
   893           $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   894           break 2;
       
   895         }
       
   896         
       
   897         // Create preview image
       
   898         $preview_filename = ENANO_ROOT . '/cache/' . $file['stored_name'] . '-preview.jpg';
       
   899         $magick = getConfig('imagemagick_path');
       
   900         $command = "$magick '{$store}' -resize ".'"'."640x640>".'"'." -quality 85 $preview_filename";
       
   901         
       
   902         @system($command, $stat);
       
   903         
       
   904         if ( !file_exists($preview_filename) )
       
   905         {
       
   906           $errors[] = 'Couldn\'t scale image '.$i.': ImageMagick failed us';
       
   907           break 2;
       
   908         }
       
   909         
       
   910       }
       
   911       
       
   912       $idlist = implode(',', $idlist);
       
   913       $url = makeUrlNS('Special', 'GalleryUpload', "edit_img=$idlist");
       
   914       
       
   915       redirect($url, 'Upload successful', 'Your images have been uploaded successfully. Please wait while you are transferred...', 2);
       
   916       
       
   917       return;
       
   918       
       
   919     endswitch;
       
   920   }
       
   921   
       
   922   // Smart batch-upload interface
       
   923   $template->header();
       
   924   
       
   925   ?>
       
   926   <!-- Some Javascript magic :-) -->
       
   927   <script type="text/javascript">
       
   928     function gal_upload_addimg()
       
   929     {
       
   930       var id = 0;
       
   931       var td = document.getElementById('gal_upload_td');
       
   932       for ( var i = 0; i < td.childNodes.length; i++ )
       
   933       {
       
   934         var child = td.childNodes[i];
       
   935         if ( child.tagName == 'INPUT' && child.type == 'hidden' )
       
   936         {
       
   937           var file = document.createElement('input');
       
   938           file.type = 'file';
       
   939           file.size = '43';
       
   940           file.name = 'img_' + id;
       
   941           file.style.marginBottom = '3px';
       
   942           td.insertBefore(file, child);
       
   943           td.insertBefore(document.createElement('br'), child);
       
   944           child.value = String(id);
       
   945           return;
       
   946         }
       
   947         else if ( child.tagName == 'INPUT' && child.type == 'file' )
       
   948         {
       
   949           id++;
       
   950         }
       
   951       }
       
   952     }
       
   953   </script>
       
   954   <?php
       
   955   
       
   956   echo '<form action="' . makeUrlNS('Special', 'GalleryUpload') . '" enctype="multipart/form-data" method="post">';
       
   957   if ( count($errors) > 0 )
       
   958   {
       
   959     echo '<div class="error-box">
       
   960             <b>The following errors were encountered during the upload:</b><br />
       
   961             <ul>
       
   962               <li>' . implode("</li>\n        <li>", $errors) . '</li>
       
   963             </ul>
       
   964           </div>';
       
   965   }
       
   966   ?>
       
   967   <div class="tblholder">
       
   968     <table border="0" cellspacing="1" cellpadding="4">
       
   969       <tr>
       
   970         <th colspan="2">Upload images to gallery</th>
       
   971       </tr>
       
   972       <tr>
       
   973         <td class="row2">Image name template:</td>
       
   974         <td class="row1"><input type="text" name="img_name" size="43" style="width: 98%;" /></td>
       
   975       </tr>
       
   976       <tr>
       
   977         <td class="row2">Image description template:</td>
       
   978         <td class="row1"><textarea rows="10" cols="40" name="img_desc" style="width: 98%;"></textarea></td>
       
   979       </tr>
       
   980       <tr>
       
   981         <td colspan="2" class="row3" style="font-size: smaller;">
       
   982           <p>The name and description templates can contain the following variables:</p>
       
   983           <ul>
       
   984             <li>{id}: The number of the image (different for each image)</li>
       
   985             <li>{autotitle}: Let the uploader automatically generate a title, based on the filename (david_in_the_barn.jpg will become "David in the barn"). Sometimes this process can be very dumb (mtrooper2k5.jpg will become "Mtrooper2k5").</li>
       
   986             <li>{year}: The current year (<?php echo date('Y'); ?>)</li>
       
   987             <li>{month}: The current month (<?php echo date('F'); ?>)</li>
       
   988             <li>{day}: The day of the month (<?php echo date('d'); ?>)</li>
       
   989             <li>{time12}: 12-hour time (<?php echo date('g:i A'); ?>)</li>
       
   990             <li>{time24}: 24-hour time (<?php echo date('G:i'); ?>)</li>
       
   991           </ul>
       
   992           <p>Example: <input type="text" readonly="readonly" value="Photo #{id} - uploaded {month} {day}, {year} {time12}" size="50" /></p>
       
   993         </td>
       
   994       </tr>
       
   995       <tr>
       
   996         <td class="row2">
       
   997           Image files:
       
   998           <?php
       
   999           if ( $zip_support )
       
  1000           {
       
  1001             ?>
       
  1002             <br />
       
  1003             <small><b>Your server has support for zip files.</b>
       
  1004                    Instead of uploading many image files, you can upload a single zip file here. Note that if you send a zip file through,
       
  1005                    it must be the first and only file or it will be ignored. Any files in the zip archive that are not supported image
       
  1006                    files will be ignored.
       
  1007                    <?php
       
  1008                      if ( $sz = ini_get('upload_max_filesize') )
       
  1009                      {
       
  1010                        echo "<b>The maximum file size is <u>{$sz}B</u>.</b>";
       
  1011                      }
       
  1012                    ?>
       
  1013                    </small>
       
  1014             <?php
       
  1015           }
       
  1016           ?>
       
  1017         </td>
       
  1018         <td class="row1" id="gal_upload_td">
       
  1019           <input type="file" name="img_0" size="43" style="margin-bottom: 3px" /><br />
       
  1020           <input type="file" name="img_1" size="43" style="margin-bottom: 3px" /><br />
       
  1021           <input type="file" name="img_2" size="43" style="margin-bottom: 3px" /><br />
       
  1022           <input type="file" name="img_3" size="43" style="margin-bottom: 3px" /><br />
       
  1023           <input type="file" name="img_4" size="43" style="margin-bottom: 3px" /><br />
       
  1024           <input type="hidden" name="img_count" value="4" />
       
  1025           <input type="button" value="+  Add image" onclick="gal_upload_addimg();" title="Add another image field" />
       
  1026         </td>
       
  1027       </tr>
       
  1028       <tr>
       
  1029         <td class="row2">Upload to folder:</td>
       
  1030         <td class="row1">
       
  1031           <div class="toggle">
       
  1032             <div class="head" onclick="gal_toggle(this.nextSibling.nextSibling, this.childNodes[1]);">
       
  1033               <img alt="&gt;&gt;" src="<?php echo scriptPath; ?>/plugins/gallery/toggle-closed.png" class="toggler" />
       
  1034               Select folder
       
  1035             </div>
       
  1036             <div class="body">
       
  1037               <?php
       
  1038                 echo gallery_hier_formfield();
       
  1039               ?>
       
  1040             </div>
       
  1041           </div>
       
  1042         </td>
       
  1043       </tr>
       
  1044     </table>
       
  1045     <table border="0" cellspacing="1" cellpadding="4" style="padding-top: 0;">
       
  1046       <tr>
       
  1047         <th class="subhead" style="text-align: left;">
       
  1048           <small>Please press the Upload button only once! Depending on the size of your image files and the speed of your connection, the upload may take several minutes.</small>
       
  1049         </th>
       
  1050         <th class="subhead" style="text-align: right;">
       
  1051           <input type="submit" name="do_stage2" value="Upload images" /><br />
       
  1052         </th>
       
  1053       </tr>
       
  1054     </table>
       
  1055   </div>
       
  1056   <?php
       
  1057   echo '</form>';
       
  1058   
       
  1059   $template->footer();
       
  1060   
       
  1061 }
       
  1062 
       
  1063 ?>