plugins/gallery/browser.php
changeset 42 7c6e2e97aa08
parent 39 e9a7e37bdadd
equal deleted inserted replaced
41:0944c9354e9c 42:7c6e2e97aa08
    25  * @access private
    25  * @access private
    26  */
    26  */
    27  
    27  
    28 class SnaprFormatter
    28 class SnaprFormatter
    29 {
    29 {
    30   
    30 	
    31   /**
    31 	/**
    32    * Main render method, called from pagination function
    32  	* Main render method, called from pagination function
    33    * @access private
    33  	* @access private
    34    */
    34  	*/
    35   
    35 	
    36   function render($column_crap, $row, $row_crap = false)
    36 	function render($column_crap, $row, $row_crap = false)
    37   {
    37 	{
    38     global $db, $session, $paths, $template, $plugins; // Common objects
    38 		global $db, $session, $paths, $template, $plugins; // Common objects
    39     
    39 		
    40     $out = '<li class="snapr-icon">';
    40 		$out = '<li class="snapr-icon">';
    41     
    41 		
    42     $title_safe = $row['img_title'];
    42 		$title_safe = $row['img_title'];
    43     $title_safe = htmlspecialchars($title_safe);
    43 		$title_safe = htmlspecialchars($title_safe);
    44     
    44 		
    45     if ( $row['is_folder'] == 1 )
    45 		if ( $row['is_folder'] == 1 )
    46     {
    46 		{
    47       // It's a folder, show the icon
    47 			// It's a folder, show the icon
    48       $f_url_particle = sanitize_page_id($row['img_title']);
    48 			$f_url_particle = sanitize_page_id($row['img_title']);
    49       $f_url_particle = htmlspecialchars($f_url_particle);
    49 			$f_url_particle = htmlspecialchars($f_url_particle);
    50       $image_link = makeUrl( $paths->fullpage . '/' . $f_url_particle );
    50 			$image_link = makeUrl( $paths->fullpage . '/' . $f_url_particle );
    51       $image_url = scriptPath . '/plugins/gallery/folder.png';
    51 			$image_url = scriptPath . '/plugins/gallery/folder.png';
    52     }
    52 		}
    53     else
    53 		else
    54     {
    54 		{
    55       // It's an image, show a thumbnail
    55 			// It's an image, show a thumbnail
    56       $image_link = makeUrlNS('Gallery', $row['img_id']);
    56 			$image_link = makeUrlNS('Gallery', $row['img_id']);
    57       $image_url  = makeUrlNS('Special', 'GalleryFetcher/thumb/' . $row['img_id']);
    57 			$image_url  = makeUrlNS('Special', 'GalleryFetcher/thumb/' . $row['img_id']);
    58     }
    58 		}
    59     
    59 		
    60     if ( isset($row['score']) )
    60 		if ( isset($row['score']) )
    61     {
    61 		{
    62       $row['score'] = number_format($row['score'], 2);
    62 			$row['score'] = number_format($row['score'], 2);
    63     }
    63 		}
    64     
    64 		
    65     $image_url_js = addslashes($image_link);
    65 		$image_url_js = addslashes($image_link);
    66     $jsclick = ( $session->user_level < USER_LEVEL_ADMIN ) ? ' onclick="window.location=\'' . $image_url_js . '\'"' : '';
    66 		$jsclick = ( $session->user_level < USER_LEVEL_ADMIN ) ? ' onclick="window.location=\'' . $image_url_js . '\'"' : '';
    67     
    67 		
    68     $out .= '<div class="gallery_icon"' . $jsclick . '>';
    68 		$out .= '<div class="gallery_icon"' . $jsclick . '>';
    69     
    69 		
    70     $out .= '<a class="snapr-imagelink" href="' . $image_link . '"><img alt="&lt;Thumbnail&gt;" class="gallery_thumb" src="' . $image_url . '" /></a>';
    70 		$out .= '<a class="snapr-imagelink" href="' . $image_link . '"><img alt="&lt;Thumbnail&gt;" class="gallery_thumb" src="' . $image_url . '" /></a>';
    71     
    71 		
    72     if ( $session->user_level < USER_LEVEL_ADMIN )
    72 		if ( $session->user_level < USER_LEVEL_ADMIN )
    73     {
    73 		{
    74       $out .= '<span class="snapr-icon-label">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</span>';
    74 			$out .= '<span class="snapr-icon-label">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</span>';
    75     }
    75 		}
    76     else if ( $session->user_level >= USER_LEVEL_ADMIN )
    76 		else if ( $session->user_level >= USER_LEVEL_ADMIN )
    77     {
    77 		{
    78       $out .= '<div class="menu_nojs snapr-icon-label" style="text-align: center;"><a href="#" onclick="return false;" style="width: 74px;">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</a>';
    78 			$out .= '<div class="menu_nojs snapr-icon-label" style="text-align: center;"><a href="#" onclick="return false;" style="width: 74px;">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</a>';
    79       
    79 			
    80       $url_delete = makeUrlNS('Special', 'GalleryUpload', 'rm=' . $row['img_id'], true);
    80 			$url_delete = makeUrlNS('Special', 'GalleryUpload', 'rm=' . $row['img_id'], true);
    81       $url_edit   = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $row['img_id'], true);
    81 			$url_edit   = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $row['img_id'], true);
    82       
    82 			
    83       // Tools menu
    83 			// Tools menu
    84       $out .= '<ul style="text-align: left;">';
    84 			$out .= '<ul style="text-align: left;">';
    85       $out .= '<li><a href="' . $url_delete . '">Delete ' . ( $row['is_folder'] == 1 ? 'this folder and all contents' : 'this image' ) . '</a></li>';
    85 			$out .= '<li><a href="' . $url_delete . '">Delete ' . ( $row['is_folder'] == 1 ? 'this folder and all contents' : 'this image' ) . '</a></li>';
    86       $out .= '<li><a href="' . $url_edit   . '">Rename, move, or edit description</a></li>';
    86 			$out .= '<li><a href="' . $url_edit   . '">Rename, move, or edit description</a></li>';
    87       $out .= '</ul>';
    87 			$out .= '</ul>';
    88       $out .= '</div>';
    88 			$out .= '</div>';
    89       $out .= '<span class="menuclear"></span>';
    89 			$out .= '<span class="menuclear"></span>';
    90     }
    90 		}
    91     
    91 		
    92     $out .= '  </div>';
    92 		$out .= '  </div>';
    93     
    93 		
    94     $out .= '</li>';
    94 		$out .= '</li>';
    95     
    95 		
    96     return $out;
    96 		return $out;
    97   }
    97 	}
    98   
    98 	
    99 }
    99 }
   100 
   100 
   101 function page_Special_Gallery()
   101 function page_Special_Gallery()
   102 {
   102 {
   103   global $db, $session, $paths, $template, $plugins; // Common objects
   103 	global $db, $session, $paths, $template, $plugins; // Common objects
   104   
   104 	
   105   // die('<pre>' . print_r(gallery_folder_hierarchy(), true) . '</pre>');
   105 	// die('<pre>' . print_r(gallery_folder_hierarchy(), true) . '</pre>');
   106   
   106 	
   107   $sort_column = ( isset($_GET['sort'])  && in_array($_GET['sort'],  array('img_title', 'img_time_upload', 'img_time_mod')) ) ? $_GET['sort'] : 'img_title';
   107 	$sort_column = ( isset($_GET['sort'])  && in_array($_GET['sort'],  array('img_title', 'img_time_upload', 'img_time_mod')) ) ? $_GET['sort'] : 'img_title';
   108   $sort_order  = ( isset($_GET['order']) && in_array($_GET['order'], array('ASC', 'DESC')) ) ? $_GET['order'] : 'ASC';
   108 	$sort_order  = ( isset($_GET['order']) && in_array($_GET['order'], array('ASC', 'DESC')) ) ? $_GET['order'] : 'ASC';
   109   
   109 	
   110   // Determine number of pictures per page
   110 	// Determine number of pictures per page
   111   $template->load_theme();
   111 	$template->load_theme();
   112   
   112 	
   113   $where = 'WHERE folder_parent IS NULL ' . "\n  ORDER BY is_folder DESC, $sort_column $sort_order, img_title ASC";
   113 	$where = 'WHERE folder_parent IS NULL ' . "\n  ORDER BY is_folder DESC, $sort_column $sort_order, img_title ASC";
   114   $parms = $paths->getAllParams();
   114 	$parms = $paths->getAllParams();
   115   
   115 	
   116   $sql = "SELECT img_id, img_title, is_folder, 'NULL' AS folder_id FROM ".table_prefix."gallery $where;";
   116 	$sql = "SELECT img_id, img_title, is_folder, 'NULL' AS folder_id FROM ".table_prefix."gallery $where;";
   117   
   117 	
   118   // Breadcrumb browser
   118 	// Breadcrumb browser
   119   $breadcrumbs = array();
   119 	$breadcrumbs = array();
   120   $breadcrumbs[] = '<a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>';
   120 	$breadcrumbs[] = '<a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>';
   121   
   121 	
   122   $breadcrumb_urlcache = '';
   122 	$breadcrumb_urlcache = '';
   123   
   123 	
   124   // CSS for gallery browser
   124 	// CSS for gallery browser
   125   // Moved to search.php
   125 	// Moved to search.php
   126   //$template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />');
   126 	//$template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />');
   127   //$template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />');
   127 	//$template->add_header('<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />');
   128   
   128 	
   129   $header = $template->getHeader();
   129 	$header = $template->getHeader();
   130   
   130 	
   131   if ( !empty($parms) )
   131 	$folders = $f_errors = array();
   132   {
   132 	if ( !empty($parms) )
   133     $parms = dirtify_page_id($parms);
   133 	{
   134     if ( strstr($parms, '/') )
   134 		$parms = dirtify_page_id($parms);
   135     {
   135 		if ( strstr($parms, '/') )
   136       $folders = explode('/', $parms);
   136 		{
   137     }
   137 			$folders = explode('/', $parms);
   138     else
   138 		}
   139     {
   139 		else
   140       $folders = array(0 => $parms);
   140 		{
   141     }
   141 			$folders = array(0 => $parms);
   142     foreach ( $folders as $i => $_crap )
   142 		}
   143     {
   143 		foreach ( $folders as $i => $_crap )
   144       $folder =& $folders[$i];
   144 		{
   145       
   145 			$folder =& $folders[$i];
   146       $f_url = sanitize_page_id($folder);
   146 			
   147       $breadcrumb_urlcache .= '/' . $f_url;
   147 			$f_url = sanitize_page_id($folder);
   148       $breadcrumb_url = makeUrlNS('Special', 'Gallery' . $breadcrumb_urlcache);
   148 			$breadcrumb_urlcache .= '/' . $f_url;
   149       
   149 			$breadcrumb_url = makeUrlNS('Special', 'Gallery' . $breadcrumb_urlcache);
   150       $folder = str_replace('_', ' ', $folder);
   150 			
   151       
   151 			$folder = str_replace('_', ' ', $folder);
   152       if ( $i == ( count($folders) - 1 ) )
   152 			
   153       {
   153 			if ( $i == ( count($folders) - 1 ) )
   154         $breadcrumbs[] = htmlspecialchars($folder);
   154 			{
   155       }
   155 				$breadcrumbs[] = htmlspecialchars($folder);
   156       else
   156 			}
   157       {
   157 			else
   158         $breadcrumbs[] = '<a href="' . $breadcrumb_url . '">' . htmlspecialchars($folder) . '</a>';
   158 			{
   159       }
   159 				$breadcrumbs[] = '<a href="' . $breadcrumb_url . '">' . htmlspecialchars($folder) . '</a>';
   160     }
   160 			}
   161     unset($folder);
   161 		}
   162     $folders = array_reverse($folders);
   162 		unset($folder);
   163     // This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL
   163 		$folders = array_reverse($folders);
   164     $sql = 'SELECT gm.img_id, gm.img_title, gm.is_folder, g0.img_title AS folder_name, g0.img_id AS folder_id FROM '.table_prefix.'gallery AS gm' . "\n  " . 'LEFT JOIN '.table_prefix.'gallery AS g0' . "\n    " . 'ON ( gm.folder_parent = g0.img_id )';
   164 		// This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL
   165     $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
   165 		$sql = 'SELECT gm.img_id, gm.img_title, gm.is_folder, g0.img_title AS folder_name, g0.img_id AS folder_id FROM '.table_prefix.'gallery AS gm' . "\n  " . 'LEFT JOIN '.table_prefix.'gallery AS g0' . "\n    " . 'ON ( gm.folder_parent = g0.img_id )';
   166     foreach ( $folders as $i => $folder )
   166 		$where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
   167     {
   167 		foreach ( $folders as $i => $folder )
   168       if ( $i == 0 )
   168 		{
   169         continue;
   169 			if ( $i == 0 )
   170       $i_dec = $i - 1;
   170 				continue;
   171       $folder = $db->escape($folder);
   171 			$i_dec = $i - 1;
   172       $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
   172 			$folder = $db->escape($folder);
   173       $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
   173 			$sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
   174     }
   174 			$where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
   175     $where .= "\n    AND g{$i}.folder_parent IS NULL";
   175 		}
   176     $sql .= $where . "\n  ORDER BY is_folder DESC, gm.$sort_column $sort_order, gm.img_title ASC" . ';';
   176 		$where .= "\n    AND g{$i}.folder_parent IS NULL";
   177   }
   177 		$sql .= $where . "\n  ORDER BY is_folder DESC, gm.$sort_column $sort_order, gm.img_title ASC" . ';';
   178   
   178 	}
   179   $img_query = $db->sql_query($sql);
   179 	
   180   if ( !$img_query )
   180 	$img_query = $db->sql_query($sql);
   181     $db->_die('The folder ID could not be selected.');
   181 	if ( !$img_query )
   182   
   182 		$db->_die('The folder ID could not be selected.');
   183   if ( $db->numrows() < 1 )
   183 	
   184   {
   184 	if ( $db->numrows() < 1 )
   185     // Nothing in this folder, for one of two reasons:
   185 	{
   186     //   1) The folder doesn't exist
   186 		// Nothing in this folder, for one of two reasons:
   187     //   2) The folder exists but doesn't have any images in it
   187 		//   1) The folder doesn't exist
   188     
   188 		//   2) The folder exists but doesn't have any images in it
   189     if ( sizeof($folders) < 1 )
   189 		
   190     {
   190 		if ( count($folders) < 1 )
   191       // Nothing in the root folder
   191 		{
   192       
   192 			// Nothing in the root folder
   193       $first_row['folder_id'] = 'NULL';
   193 			
   194       if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) && isset($first_row['folder_id']) )
   194 			$first_row['folder_id'] = 'NULL';
   195       {
   195 			if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) && isset($first_row['folder_id']) )
   196         if ( empty($_POST['create_folder']) )
   196 			{
   197         {
   197 				if ( empty($_POST['create_folder']) )
   198           $f_errors[] = 'Please enter a folder name.';
   198 				{
   199         }
   199 					$f_errors[] = 'Please enter a folder name.';
   200         if ( $_POST['create_folder'] == '_id' )
   200 				}
   201         {
   201 				if ( $_POST['create_folder'] == '_id' )
   202           $f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.';
   202 				{
   203         }
   203 					$f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.';
   204         if ( count($f_errors) < 1 )
   204 				}
   205         {
   205 				if ( count($f_errors) < 1 )
   206           $q = $db->sql_query('INSERT INTO '.table_prefix.'gallery(img_title, is_folder, folder_parent) VALUES(\'' . $db->escape($_POST['create_folder']) . '\', 1, ' . $first_row['folder_id'] . ');');
   206 				{
   207           if ( !$q )
   207 					$q = $db->sql_query('INSERT INTO '.table_prefix.'gallery(img_title, is_folder, folder_parent, img_author) VALUES(\'' . $db->escape($_POST['create_folder']) . '\', 1, ' . $first_row['folder_id'] . ', ' . $session->user_id . ');');
   208             $db->_die();
   208 					if ( !$q )
   209           redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2);
   209 						$db->_die();
   210         }
   210 					redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2);
   211       }
   211 				}
   212       
   212 			}
   213       $html = '';
   213 			
   214       if ( $session->user_level >= USER_LEVEL_ADMIN )
   214 			$html = '';
   215       {
   215 			if ( $session->user_level >= USER_LEVEL_ADMIN )
   216         $html .= '<p><a href="' . makeUrlNS('Special', 'GalleryUpload') . '">Upload an image</a></p>';
   216 			{
   217         $html .= '<div class="select-outer">Create new folder';
   217 				$html .= '<p><a href="' . makeUrlNS('Special', 'GalleryUpload') . '">Upload an image</a></p>';
   218         $html .= '<div class="select-inner" style="padding-top: 4px;">';
   218 				$html .= '<div class="select-outer">Create new folder';
   219         $html .= '<form action="' . makeUrl($paths->fullpage) . '" method="post">';
   219 				$html .= '<div class="select-inner" style="padding-top: 4px;">';
   220         $html .= '<input type="text" name="create_folder" size="30" /> <input type="submit" value="Create" />';
   220 				$html .= '<form action="' . makeUrl($paths->fullpage) . '" method="post">';
   221         $html .= '</form></div>';
   221 				$html .= '<input type="text" name="create_folder" size="30" /> <input type="submit" value="Create" />';
   222         $html .= '</div><div class="select-pad">&nbsp;</div><br />';
   222 				$html .= '</form></div>';
   223       }
   223 				$html .= '</div><div class="select-pad">&nbsp;</div><br />';
   224       
   224 			}
   225       die_friendly('No images', '<p>No images have been uploaded to the gallery yet.</p>' . $html);
   225 			
   226     }
   226 			die_friendly('No images', '<p>No images have been uploaded to the gallery yet.</p>' . $html);
   227     
   227 		}
   228     /*
   228 		
   229     $folders_old = $folders;
   229 		/*
   230     $folders = array(
   230 		$folders_old = $folders;
   231       0 => $folders_old[0]
   231 		$folders = array(
   232       );
   232 			0 => $folders_old[0]
   233     $x = $folders_old;
   233 			);
   234     unset($x[0]);
   234 		$x = $folders_old;
   235     $folders = array_merge($folders, $x);
   235 		unset($x[0]);
   236     unset($x);
   236 		$folders = array_merge($folders, $x);
   237     */
   237 		unset($x);
   238     // die('<pre>' . print_r($folders, true) . '</pre>');
   238 		*/
   239     
   239 		// die('<pre>' . print_r($folders, true) . '</pre>');
   240     // This next query will try to determine if the folder itself exists
   240 		
   241     $sql = 'SELECT g0.img_id, g0.img_title FROM '.table_prefix.'gallery AS g0';
   241 		// This next query will try to determine if the folder itself exists
   242     $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
   242 		$sql = 'SELECT g0.img_id, g0.img_title FROM '.table_prefix.'gallery AS g0';
   243     foreach ( $folders as $i => $folder )
   243 		$where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
   244     {
   244 		foreach ( $folders as $i => $folder )
   245       if ( $i == 0 )
   245 		{
   246         continue;
   246 			if ( $i == 0 )
   247       $i_dec = $i - 1;
   247 				continue;
   248       $folder = $db->escape($folder);
   248 			$i_dec = $i - 1;
   249       $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
   249 			$folder = $db->escape($folder);
   250       $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
   250 			$sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
   251     }
   251 			$where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
   252     $where .= "\n    AND g{$i}.folder_parent IS NULL";
   252 		}
   253     $where .= "\n    AND g0.is_folder=1";
   253 		$where .= "\n    AND g{$i}.folder_parent IS NULL";
   254     $sql .= $where . ';';
   254 		$where .= "\n    AND g0.is_folder=1";
   255    
   255 		$sql .= $where . ';';
   256     $nameq = $db->sql_query($sql);
   256  	
   257     if ( !$nameq )
   257 		$nameq = $db->sql_query($sql);
   258       $db->_die();
   258 		if ( !$nameq )
   259     
   259 			$db->_die();
   260     if ( $db->numrows($nameq) < 1 )
   260 		
   261     {
   261 		if ( $db->numrows($nameq) < 1 )
   262       die_friendly('Folder not found', '<p>The folder you requested doesn\'t exist. Please check the URL and try again, or return to the <a href="' . makeUrlNS('Special', 'Gallery') . '">gallery index</a>.</p>');
   262 		{
   263     }
   263 			die_friendly('Folder not found', '<p>The folder you requested doesn\'t exist. Please check the URL and try again, or return to the <a href="' . makeUrlNS('Special', 'Gallery') . '">gallery index</a>.</p>');
   264     
   264 		}
   265     $row = $db->fetchrow($nameq);
   265 		
   266     
   266 		$row = $db->fetchrow($nameq);
   267     // Generate title
   267 		
   268     $title = dirtify_page_id($row['img_title']);
   268 		// Generate title
   269     $title = str_replace('_', ' ', $title);
   269 		$title = dirtify_page_id($row['img_title']);
   270     $title = htmlspecialchars($title);
   270 		$title = str_replace('_', ' ', $title);
   271     
   271 		$title = htmlspecialchars($title);
   272     $template->tpl_strings['PAGE_NAME'] = $title;
   272 		
   273     
   273 		$template->tpl_strings['PAGE_NAME'] = $title;
   274     $first_row = $row;
   274 		
   275     
   275 		$first_row = $row;
   276     if ( $db->numrows($img_query) > 0 )
   276 		
   277       $db->sql_data_seek(0, $img_query);
   277 		if ( $db->numrows($img_query) > 0 )
   278     
   278 			$db->sql_data_seek(0, $img_query);
   279     /* $folders = $folders_old; */
   279 		
   280   }
   280 		/* $folders = $folders_old; */
   281   else if ( !empty($parms) )
   281 	}
   282   {
   282 	else if ( !empty($parms) )
   283     $row = $db->fetchrow($img_query);
   283 	{
   284     $first_row = $row;
   284 		$row = $db->fetchrow($img_query);
   285     
   285 		$first_row = $row;
   286     // Generate title
   286 		
   287     $title = htmlspecialchars($row['folder_name']);
   287 		// Generate title
   288     
   288 		$title = htmlspecialchars($row['folder_name']);
   289     $template->tpl_strings['PAGE_NAME'] = $title;
   289 		
   290     
   290 		$template->tpl_strings['PAGE_NAME'] = $title;
   291     $db->sql_data_seek(0, $img_query);
   291 		
   292   }
   292 		$db->sql_data_seek(0, $img_query);
   293   else
   293 	}
   294   {
   294 	else
   295     $row = $db->fetchrow($img_query);
   295 	{
   296     $first_row = $row;
   296 		$row = $db->fetchrow($img_query);
   297     
   297 		$first_row = $row;
   298     $template->tpl_strings['PAGE_NAME'] = 'Image Gallery';
   298 		
   299     $breadcrumbs = array('<b>Gallery index</b>');
   299 		$template->tpl_strings['PAGE_NAME'] = 'Image Gallery';
   300     
   300 		$breadcrumbs = array('<b>Gallery index</b>');
   301     $db->sql_data_seek(0, $img_query);
   301 		
   302   }
   302 		$db->sql_data_seek(0, $img_query);
   303   
   303 	}
   304   $f_errors = array();
   304 	
   305   
   305 	$f_errors = array();
   306   if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) )
   306 	
   307   {
   307 	if ( $session->user_level >= USER_LEVEL_ADMIN && isset($_POST['create_folder']) )
   308     if ( !isset($first_row['folder_id']) )
   308 	{
   309     {
   309 		if ( !isset($first_row['folder_id']) )
   310       //die('FALLING<pre>' . print_r($first_row, true) . '</pre>');
   310 		{
   311       $first_row['folder_id'] =& $first_row['img_id'];
   311 			//die('FALLING<pre>' . print_r($first_row, true) . '</pre>');
   312     }
   312 			$first_row['folder_id'] =& $first_row['img_id'];
   313     if ( !isset($first_row['folder_id']) )
   313 		}
   314     {
   314 		if ( !isset($first_row['folder_id']) )
   315       $f_errors[] = 'Internal error getting parent folder ID';
   315 		{
   316     }
   316 			$f_errors[] = 'Internal error getting parent folder ID';
   317     if ( empty($_POST['create_folder']) )
   317 		}
   318     {
   318 		if ( empty($_POST['create_folder']) )
   319       $f_errors[] = 'Please enter a folder name.';
   319 		{
   320     }
   320 			$f_errors[] = 'Please enter a folder name.';
   321     if ( $_POST['create_folder'] == '_id' )
   321 		}
   322     {
   322 		if ( $_POST['create_folder'] == '_id' )
   323       $f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.';
   323 		{
   324     }
   324 			$f_errors[] = 'The name "_id" is reserved for internal functions and cannot be used on any image or folder.';
   325     if ( count($f_errors) < 1 )
   325 		}
   326     {
   326 		if ( count($f_errors) < 1 )
   327       $q = $db->sql_query('INSERT INTO '.table_prefix.'gallery(img_title, is_folder, folder_parent) VALUES(\'' . $db->escape($_POST['create_folder']) . '\', 1, ' . $first_row['folder_id'] . ');');
   327 		{
   328       if ( !$q )
   328 			$q = $db->sql_query('INSERT INTO '.table_prefix.'gallery(img_title, is_folder, folder_parent, img_author) VALUES(\'' . $db->escape($_POST['create_folder']) . '\', 1, ' . $first_row['folder_id'] . ', ' . $session->user_id . ');');
   329         $db->_die();
   329 			if ( !$q )
   330       redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2);
   330 				$db->_die();
   331     }
   331 			redirect(makeUrl($paths->fullpage), 'Folder created', 'The folder "' . htmlspecialchars($_POST['create_folder']) . '" has been created. Redirecting to last viewed folder...', 2);
   332   }
   332 		}
   333   
   333 	}
   334   echo $header;
   334 	
   335   
   335 	echo $header;
   336   if ( count($f_errors) > 0 )
   336 	
   337   {
   337 	if ( count($f_errors) > 0 )
   338     echo '<div class="error-box">Error creating folder:<ul><li>' . implode('</li><li>', $f_errors) . '</li></ul></div>';
   338 	{
   339   }
   339 		echo '<div class="error-box">Error creating folder:<ul><li>' . implode('</li><li>', $f_errors) . '</li></ul></div>';
   340   
   340 	}
   341   // From here, this breadcrumb stuff is a piece of... sourdough French bread :-) *smacks lips*
   341 	
   342   echo '<div class="breadcrumbs" style="padding: 4px; margin-bottom: 7px;">';
   342 	// From here, this breadcrumb stuff is a piece of... sourdough French bread :-) *smacks lips*
   343   // Upload image link for admins
   343 	echo '<div class="breadcrumbs" style="padding: 4px; margin-bottom: 7px;">';
   344   if ( $session->user_level >= USER_LEVEL_ADMIN )
   344 	// Upload image link for admins
   345   {
   345 	if ( $session->user_level >= USER_LEVEL_ADMIN )
   346     echo '<div style="float: right; font-size: smaller;">';
   346 	{
   347     echo '<a href="' . makeUrlNS('Special', 'GalleryUpload') . '">Upload new image(s)</a>';
   347 		echo '<div style="float: right; font-size: smaller;">';
   348     echo '</div>';
   348 		echo '<a href="' . makeUrlNS('Special', 'GalleryUpload') . '">Upload new image(s)</a>';
   349   }
   349 		echo '</div>';
   350   // The actual breadcrumbs
   350 	}
   351   echo '<small>' . implode(' &raquo; ', $breadcrumbs) . '</small>';
   351 	// The actual breadcrumbs
   352   echo '</div>';
   352 	echo '<small>' . implode(' &raquo; ', $breadcrumbs) . '</small>';
   353   
   353 	echo '</div>';
   354   // "Edit all" link
   354 	
   355   if ( $row = $db->fetchrow($img_query) && $session->user_level >= USER_LEVEL_ADMIN )
   355 	// "Edit all" link
   356   {
   356 	if ( $row = $db->fetchrow($img_query) && $session->user_level >= USER_LEVEL_ADMIN )
   357     $img_list = array();
   357 	{
   358     $fol_list = array();
   358 		$img_list = array();
   359     $all_list = array();
   359 		$fol_list = array();
   360     do
   360 		$all_list = array();
   361     {
   361 		do
   362       if ( $row === true && isset($first_row) )
   362 		{
   363       {
   363 			if ( $row === true && isset($first_row) )
   364         $row = $first_row;
   364 			{
   365       }
   365 				$row = $first_row;
   366         // die('<pre>' . var_dump($row) . $db->sql_backtrace() . '</pre>');
   366 			}
   367       if ( !$row['img_id'] )
   367 				// die('<pre>' . var_dump($row) . $db->sql_backtrace() . '</pre>');
   368         break;
   368 			if ( !$row['img_id'] )
   369       $all_list[] = $row['img_id'];
   369 				break;
   370       if ( $row['is_folder'] == 1 )
   370 			$all_list[] = $row['img_id'];
   371         $fol_list[] = $row['img_id'];
   371 			if ( $row['is_folder'] == 1 )
   372       else
   372 				$fol_list[] = $row['img_id'];
   373         $img_list[] = $row['img_id'];
   373 			else
   374     }
   374 				$img_list[] = $row['img_id'];
   375     while ( $row = $db->fetchrow($img_query) );
   375 		}
   376     
   376 		while ( $row = $db->fetchrow($img_query) );
   377     $all_list = implode(',', $all_list);
   377 		
   378     $fol_list = implode(',', $fol_list);
   378 		$all_list = implode(',', $all_list);
   379     $img_list = implode(',', $img_list);
   379 		$fol_list = implode(',', $fol_list);
   380     
   380 		$img_list = implode(',', $img_list);
   381     if ( !empty($all_list) )
   381 		
   382     {
   382 		if ( !empty($all_list) )
   383       echo '<div style="float: right;">
   383 		{
   384               Edit all in this folder: ';
   384 			echo '<div style="float: right;">
   385       if ( !empty($img_list) )
   385 							Edit all in this folder: ';
   386       {
   386 			if ( !empty($img_list) )
   387         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $img_list, true);
   387 			{
   388         echo "<a href=\"$edit_link\">images</a> ";
   388 				$edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $img_list, true);
   389       }
   389 				echo "<a href=\"$edit_link\">images</a> ";
   390       if ( !empty($fol_list) )
   390 			}
   391       {
   391 			if ( !empty($fol_list) )
   392         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $fol_list, true);
   392 			{
   393         echo "<a href=\"$edit_link\">folders</a> ";
   393 				$edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $fol_list, true);
   394       }
   394 				echo "<a href=\"$edit_link\">folders</a> ";
   395       if ( !empty($img_list) && !empty($fol_list) )
   395 			}
   396       {
   396 			if ( !empty($img_list) && !empty($fol_list) )
   397         $edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $all_list, true);
   397 			{
   398         echo "<a href=\"$edit_link\">both</a> ";
   398 				$edit_link = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $all_list, true);
   399       }
   399 				echo "<a href=\"$edit_link\">both</a> ";
   400       // " Bypass stupid jEdit bug
   400 			}
   401       echo '</div>';
   401 			// " Bypass stupid jEdit bug
   402     }
   402 			echo '</div>';
   403   }
   403 		}
   404   
   404 	}
   405   $url_sort_name_asc  = makeUrl($paths->fullpage, 'sort=img_title&order=ASC', true);
   405 	
   406   $url_sort_name_desc = makeUrl($paths->fullpage, 'sort=img_title&order=DESC', true);
   406 	$url_sort_name_asc  = makeUrl($paths->fullpage, 'sort=img_title&order=ASC', true);
   407   $url_sort_upl_asc   = makeUrl($paths->fullpage, 'sort=img_time_upload&order=ASC', true);
   407 	$url_sort_name_desc = makeUrl($paths->fullpage, 'sort=img_title&order=DESC', true);
   408   $url_sort_mod_asc   = makeUrl($paths->fullpage, 'sort=img_time_mod&order=ASC', true);
   408 	$url_sort_upl_asc   = makeUrl($paths->fullpage, 'sort=img_time_upload&order=ASC', true);
   409   $url_sort_upl_desc  = makeUrl($paths->fullpage, 'sort=img_time_upload&order=DESC', true);
   409 	$url_sort_mod_asc   = makeUrl($paths->fullpage, 'sort=img_time_mod&order=ASC', true);
   410   $url_sort_mod_desc  = makeUrl($paths->fullpage, 'sort=img_time_mod&order=DESC', true);
   410 	$url_sort_upl_desc  = makeUrl($paths->fullpage, 'sort=img_time_upload&order=DESC', true);
   411   
   411 	$url_sort_mod_desc  = makeUrl($paths->fullpage, 'sort=img_time_mod&order=DESC', true);
   412   // "Sort by" selector (pure CSS!)
   412 	
   413   echo '<div class="select-outer">
   413 	// "Sort by" selector (pure CSS!)
   414           <span>Sort by...</span>
   414 	echo '<div class="select-outer">
   415           <div class="select-inner">
   415 					<span>Sort by...</span>
   416             <a href="' . $url_sort_name_asc  . '">Image title (A-Z) <b>(default)</b></a>
   416 					<div class="select-inner">
   417             <a href="' . $url_sort_name_desc . '">Image title (Z-A)</a>
   417 						<a href="' . $url_sort_name_asc  . '">Image title (A-Z) <b>(default)</b></a>
   418             <a href="' . $url_sort_upl_desc  . '">Time first uploaded (newest first)</a>
   418 						<a href="' . $url_sort_name_desc . '">Image title (Z-A)</a>
   419             <a href="' . $url_sort_upl_asc   . '">Time first uploaded (oldest first)</a>
   419 						<a href="' . $url_sort_upl_desc  . '">Time first uploaded (newest first)</a>
   420             <a href="' . $url_sort_mod_desc  . '">Date of last modification (newest first)</a>
   420 						<a href="' . $url_sort_upl_asc   . '">Time first uploaded (oldest first)</a>
   421             <a href="' . $url_sort_mod_asc   . '">Date of last modification (oldest first)</a>
   421 						<a href="' . $url_sort_mod_desc  . '">Date of last modification (newest first)</a>
   422           </div>
   422 						<a href="' . $url_sort_mod_asc   . '">Date of last modification (oldest first)</a>
   423         </div>
   423 					</div>
   424         <div class="select-pad">&nbsp;</div>';
   424 				</div>
   425   
   425 				<div class="select-pad">&nbsp;</div>';
   426   if ( $db->numrows($img_query) > 0 )
   426 	
       
   427 	if ( $db->numrows($img_query) > 0 )
   427 	$db->sql_data_seek(0, $img_query);
   428 	$db->sql_data_seek(0, $img_query);
   428   
   429 	
   429   //
   430 	//
   430   // Main fetcher
   431 	// Main fetcher
   431   //
   432 	//
   432   
   433 	
   433   $renderer = new SnaprFormatter();
   434 	$renderer = new SnaprFormatter();
   434   $callers = array(
   435 	$callers = array(
   435     'img_id' => array($renderer, 'render')
   436 		'img_id' => array($renderer, 'render')
   436     );
   437 		);
   437   
   438 	
   438   $renderer->icons_per_row = 5;
   439 	$renderer->icons_per_row = 5;
   439   
   440 	
   440   $start = 0;
   441 	$start = 0;
   441   if ( isset($_GET['start']) && preg_match('/^[0-9]+$/', $_GET['start']) )
   442 	if ( isset($_GET['start']) && preg_match('/^[0-9]+$/', $_GET['start']) )
   442   {
   443 	{
   443     $start = intval($_GET['start']);
   444 		$start = intval($_GET['start']);
   444   }
   445 	}
   445   
   446 	
   446   $per_page = 25;
   447 	$per_page = 25;
   447   
   448 	
   448   $html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'sort=' . $sort_column . '&order=' . $sort_order . '&start=%s', false), $start, $per_page, $callers, '<ul class="snapr-gallery">', '</ul><span class="menuclear"></span>');
   449 	$html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'sort=' . $sort_column . '&order=' . $sort_order . '&start=%s', false), $start, $per_page, $callers, '<ul class="snapr-gallery">', '</ul><span class="menuclear"></span>');
   449   if ( empty($html) )
   450 	if ( empty($html) )
   450   {
   451 	{
   451   	  echo '<h2 class="emptymessage">No images</h2>';
   452 		  echo '<h2 class="emptymessage">No images</h2>';
   452   }
   453 	}
   453   else
   454 	else
   454   {
   455 	{
   455 	  echo $html;
   456 	  echo $html;
   456   }
   457 	}
   457   
   458 	
   458   if ( $session->user_level >= USER_LEVEL_ADMIN )
   459 	if ( $session->user_level >= USER_LEVEL_ADMIN )
   459   {
   460 	{
   460     echo '<div class="select-outer">Create new folder';
   461 		echo '<div class="select-outer">Create new folder';
   461     echo '<div class="select-inner" style="padding-top: 4px;">';
   462 		echo '<div class="select-inner" style="padding-top: 4px;">';
   462     echo '<form action="' . makeUrl($paths->fullpage) . '" method="post">';
   463 		echo '<form action="' . makeUrl($paths->fullpage) . '" method="post">';
   463     echo '<input type="text" name="create_folder" size="30" /> <input type="submit" value="Create" />';
   464 		echo '<input type="text" name="create_folder" size="30" /> <input type="submit" value="Create" />';
   464     echo '</form></div>';
   465 		echo '</form></div>';
   465     echo '</div><div class="select-pad">&nbsp;</div><br />';
   466 		echo '</div><div class="select-pad">&nbsp;</div><br />';
   466   }
   467 	}
   467   
   468 	
   468   $template->footer();
   469 	$template->footer();
   469   
   470 	
   470 }
   471 }
   471 
   472 
   472 ?>
   473 ?>