plugins/gallery/functions.php
author Dan
Wed, 05 Dec 2007 16:53:26 -0500
changeset 17 ecb96b051d08
parent 0 7caf561c50ee
child 18 c1c398349651
permissions -rw-r--r--
Fixed a number of critical bugs with ZIP uploads and creation of folders
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     1
<?php
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     2
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     3
/*
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     4
 * Snapr
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     5
 * Version 0.1 beta 1
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     6
 * Copyright (C) 2007 Dan Fuhry
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     7
 *
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    10
 *
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    13
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    14
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    15
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    16
 * Generates a random filename for Snapr images.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    17
 * @param int $length Optional - length of filename
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    18
 * @return string
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    19
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    20
 
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    21
function gallery_make_filename($length = 24)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    22
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    23
  $valid_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    24
  $valid_chars = enano_str_split($valid_chars);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    25
  $ret = '';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    26
  for ( $i = 0; $i < $length; $i++ )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    27
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    28
    $ret .= $valid_chars[mt_rand(0, count($valid_chars)-1)];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    29
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    30
  return $ret;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    31
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    32
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    33
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    34
 * Returns the extension of a file.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    35
 * @param string file
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    36
 * @return string
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    37
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    38
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    39
function get_file_extension($file)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    40
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    41
  return substr($file, ( strrpos($file, '.') + 1 ));
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    42
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    43
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    44
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    45
 * For a given image ID, return the folder hierarchy.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    46
 * @param int The image ID
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    47
 * @return array
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    48
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    49
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    50
function gallery_imgid_to_folder($img_id)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    51
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    52
  global $db, $session, $paths, $template, $plugins; // Common objects
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    53
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    54
  if ( !is_int($img_id) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    55
    return array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    56
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    57
  $img_id = strval($img_id);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    58
  $ret = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    59
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    60
  $sanity = 0;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    61
  $sanity_stack = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    62
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    63
  while(true)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    64
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    65
    $sanity++;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    66
    $q = $db->sql_query('SELECT img_title, img_id, folder_parent FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    67
    if ( !$q )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    68
      $db->_die();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    69
    $row = $db->fetchrow();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    70
    if ( !$row )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    71
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    72
      break;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    73
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    74
    if ( $sanity > 1 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    75
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    76
      $ret[] = $row['img_title'];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    77
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    78
    if ( !$row['folder_parent'] )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    79
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    80
      break;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    81
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    82
    if ( in_array($row['img_id'], $sanity_stack) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    83
      return array('Infinite loop');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    84
    $sanity_stack[] = $row['img_id'];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    85
    $img_id = $row['folder_parent'];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    86
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    87
  return $ret;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    88
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    89
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    90
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    91
 * Generates a hierarchy of Gallery folders.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    92
 * @return array
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    93
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    94
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    95
function gallery_folder_hierarchy()
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    96
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    97
  global $db, $session, $paths, $template, $plugins; // Common objects
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    98
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
    99
  $q = $db->sql_query('SELECT img_id, img_title, folder_parent FROM '.table_prefix.'gallery WHERE is_folder=1');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   100
  if ( !$q )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   101
    $db->_die();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   102
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   103
  if ( $db->numrows() < 1 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   104
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   105
    return array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   106
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   107
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   108
  $lookup_table = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   109
  $hier = array('_id' => 'NULL');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   110
  $orphans = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   111
  $persist_orphans = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   112
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   113
  while ( $row = $db->fetchrow() )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   114
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   115
    if ( !$row['folder_parent'] )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   116
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   117
      // root-level folder
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   118
      $hier[ $row['img_title'] ] = array('_id' => $row['img_id']);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   119
      $lookup_table[$row['img_id']] =& $hier[ $row['img_title'] ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   120
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   121
    else if ( $row['folder_parent'] && isset($lookup_table[$row['folder_parent']]) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   122
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   123
      // child folder, parent is resolved
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   124
      $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ] = array('_id' => $row['img_id']);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   125
      $lookup_table[ $row['img_id'] ] =& $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   126
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   127
    else if ( $row['folder_parent'] && !isset($lookup_table[$row['folder_parent']]) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   128
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   129
      // child folder, orphan as of yet
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   130
      $orphans[] = $row;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   131
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   132
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   133
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   134
  // Resolve orphans
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   135
  do
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   136
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   137
    $persist_orphans = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   138
    while ( count($orphans) > 0 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   139
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   140
      $orphan =& $orphans[ ( count($orphans) - 1 ) ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   141
      if ( isset($lookup_table[$orphan['folder_parent']]) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   142
      {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   143
        $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ] = array('_id' => $orphan['img_id']);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   144
        $lookup_table[ $orphan['img_id'] ] =& $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   145
      }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   146
      else
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   147
      {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   148
        $persist_orphans[] = $orphans[ ( count($orphans) - 1 ) ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   149
        //echo 'BUG: ' . htmlspecialchars($orphan['img_title']) . ' (' . $orphan['img_id'] . ') is an orphan folder (parent is ' . $orphan['folder_parent'] . '); placing in root<br />';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   150
        // $hier[ $orphan['img_title'] ] = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   151
        // $lookup_table[$orphan['img_id']] =& $hier[ $orphan['img_title'] ];
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   152
      }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   153
      unset($orphan, $orphans[ ( count($orphans) - 1 ) ]);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   154
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   155
    $orphans = $persist_orphans;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   156
    //die('insanity:<pre>'.print_r($hier,true).print_r($lookup_table,true).print_r($persist_orphans,true).'</pre>');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   157
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   158
  while ( count($persist_orphans) > 0 );
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   159
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   160
  return $hier;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   161
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   162
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   163
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   164
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   165
 * Generates HTML for a folder selector.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   166
 * @param string The form field name, defaults to folder_id.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   167
 * @param bool Whether to auto-select the root or not. Defaults to true.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   168
 * @return string
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   169
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   170
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   171
function gallery_hier_formfield($field_name = 'folder_id', $autosel = true)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   172
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   173
  $hier = gallery_folder_hierarchy();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   174
  $img_join      = scriptPath . '/images/icons/joinbottom.gif';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   175
  $img_join_term = scriptPath . '/images/icons/join.gif';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   176
  $img_line      = scriptPath . '/images/icons/line.gif';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   177
  $img_empty     = scriptPath . '/images/icons/empty.gif';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   178
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   179
  $html = _gallery_hier_form_inner($hier, '<Root>', $field_name, -1, array(), $img_join, $img_join_term, $img_line, $img_empty, $autosel);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   180
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   181
  return $html;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   182
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   183
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   184
// 
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   185
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   186
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   187
 * Inner loop for form field generator (needs to call itself recursively)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   188
 * @access private
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   189
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   190
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   191
function _gallery_hier_form_inner($el, $name, $fname, $depth, $depth_img, $img_join, $img_join_term, $img_line, $img_empty, $sel = false)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   192
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   193
  $html = '';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   194
  foreach ( $depth_img as $sw )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   195
    $html .= '<img alt="  " src="' . $sw . '" />';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   196
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   197
  $html .= '<label><input ' . ( $sel ? 'checked="checked"' : '' ) . ' type="radio" name="' . $fname . '" value="' . $el['_id'] . '" /> ' . htmlspecialchars($name) . '</label><br />';
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   198
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   199
  if ( count($el) > 1 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   200
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   201
    // Writing this image logic sucked.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   202
    $count = 0;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   203
    foreach ( $el as $key => $el_lower )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   204
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   205
      $count++;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   206
      if ( $key == '_id' )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   207
        continue;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   208
      $depth_mod = $depth_img;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   209
      $last = ( $count == count($el) );
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   210
      
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   211
      for ( $i = 0; $i < count($depth_mod); $i++ )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   212
      {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   213
        if ( $depth_mod[$i] == $img_join_term || $depth_mod[$i] == $img_empty )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   214
          $depth_mod[$i] = $img_empty;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   215
        else
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   216
          $depth_mod[$i] = $img_line;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   217
      }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   218
      
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   219
      if ( $last )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   220
        $depth_mod[] = $img_join_term;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   221
      else
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   222
        $depth_mod[] = $img_join;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   223
      
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   224
      $html .= _gallery_hier_form_inner($el_lower, $key, $fname, ( $depth + 1 ), $depth_mod, $img_join, $img_join_term, $img_line, $img_empty);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   225
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   226
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   227
  return $html;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   228
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   229
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   230
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   231
 * Returns an array containing the IDs of all of the given folder ID's children. Recursive function.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   232
 * @param int ID of folder
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   233
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   234
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   235
function gal_fetch_all_children($id)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   236
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   237
  global $db, $session, $paths, $template, $plugins; // Common objects
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   238
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   239
  if ( !is_int($id) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   240
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   241
    die('not int');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   242
    return false;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   243
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   244
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   245
  $children = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   246
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   247
  $q = $db->sql_query('SELECT img_id,is_folder FROM '.table_prefix.'gallery WHERE folder_parent=' . $id . ';');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   248
  if ( !$q )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   249
    $db->_die();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   250
  if ( $db->numrows() < 1 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   251
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   252
    return $children;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   253
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   254
  $folders = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   255
  while ( $row = $db->fetchrow() )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   256
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   257
    $children[] = intval($row['img_id']);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   258
    if ( $row['is_folder'] == 1 )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   259
      $folders[] = intval($row['img_id']);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   260
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   261
  foreach ( $folders as $folder )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   262
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   263
    $grandchildren = gal_fetch_all_children($folder);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   264
    if ( $grandchildren === false )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   265
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   266
      return false;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   267
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   268
    $children = array_merge($children, $grandchildren);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   269
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   270
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   271
  return $children;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   272
  
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   273
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   274
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   275
/**
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   276
 * Lists all normal files within a given directory. Recursive function. Can also return the list of directories in the second parameter by reference.
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   277
 * @param string Directory to search
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   278
 * @param array Variable in which to store 
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   279
 * @return array Not multi-depth
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   280
 */
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   281
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   282
function gal_dir_recurse($dir, &$dirlist)
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   283
{
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   284
  $dir_handle = opendir($dir);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   285
  if ( !$dir_handle )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   286
    return false;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   287
  $entries = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   288
  $dirlist = array();
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   289
  while ( true )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   290
  {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   291
    $file = readdir($dir_handle);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   292
    if ( !$file )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   293
      break;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   294
    if ( $file == '.' || $file == '..' )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   295
      continue;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   296
    $file = $dir . '/' . $file;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   297
    if ( is_dir($file) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   298
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   299
      $children = gal_dir_recurse($file, $dirtemp);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   300
      $dirlist[] = $file;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   301
      $dirlist = array_merge($dirlist, $dirtemp);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   302
      $entries = array_merge($entries, $children);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   303
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   304
    else if ( is_file($file) )
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   305
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   306
      $entries[] = $file;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   307
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   308
    else
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   309
    {
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   310
      die($file . ' is not a file or directory');
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   311
    }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   312
  }
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   313
  closedir($dir_handle);
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   314
  return $entries;
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   315
}
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   316
7caf561c50ee Initial population; browser, viewer, uploader, and security are working
Dan
parents:
diff changeset
   317
?>