##################################################
# npa_upload.pl v2.5 (BETA)for NewsPro			 # 
# By Russ Mckendrick							 # 
# With help and inspiration from Jim Logan		 # 
# Email: russ@rock-city.co.uk					 # 
# Web:   www.rock-city.co.uk					 #
##################################################
# NewsPro Addon Hooks

push(@Addons_Loaded, 'Upload v2.5');
push(@Addons_PageHandler, 'npa_upload_PageHandler');
push(@Addons_MainPage, 'npa_upload_MainPage');
#push(@Addons_MainPage, 'npa_upload_delete_MainPage');
push(@Addons_NPHTMLFoot, 'npa_upload_HTMLFoot');
#push(@Addons_NPHTMLFoot, 'npa_upload_delete_HTMLFoot');
push(@Addons_LoadHelp, 'npa_upload_LoadHelp');
push(@Addons_AdvancedSettingsLoad, 'Addnpa_Upload_Settings');
push(@Addons_DisplaySubForm_Post4, 'npa_upload_body');
#push(@Addons_DisplaySubForm_Post3, 'npa_UploadList_Link');
push(@Addons_NPHTMLHead_Head, 'npa_UploadList_Head');
$Addons_List{'Upload v2.5'} = ['npa_upload.pl', '<b>BETA</b>Upload and view files from with NewsPro.'];

##################################################
# What are we doing?

sub npa_upload_PageHandler {
if (query_string() eq "npa_upload_mainpage") {
&npa_upload_main;
exit;
}
if (query_string() eq "npa_UploadList_remote") {
&npa_UploadList_Remote;
exit;
}
if (query_string() eq "npa_upload_delete") {
&npa_upload_remove;
exit;
}
if (query_string() eq "npa_remove_success") {
&npa_remove_succ;
exit;
}
elsif (query_string() eq "npa_upload_success") {
&npa_upload_succ;
exit;
}

# 
##################################################


##################################################
# Add the open new window javascript in the header
sub npa_UploadList_Head {
print qq~
<script language="JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>
~;	
}

##################################################
# Add the link in the bottom menu
sub npa_upload_HTMLFoot {
print qq~ <a href="$scripturl?npa_upload_mainpage"  class="navlink">Upload/View Files</a> |~;
} 

##################################################
# Add the delete link in the main menu for webmasters only
sub npa_upload_delete_HTMLFoot {
if ($up == 3) {
	print qq~ <a href="$scripturl?npa_upload_delete" class="navlink">Delete File</a>|~
}
}

##################################################
# Add the delete link in the bottom menu for webmasters only
sub npa_upload_delete_MainPage {
if ($up == 3) {
	print qq~	<b><a href="$scripturl?npa_upload_delete">Delete File</a>:</b> Delete a file.<br><br>~;
}
}

##################################################
# Add the link in the main menu
sub npa_upload_MainPage {
print qq~
	<b><a href="$scripturl?npa_upload_mainpage">Upload/View Files</a>:</b> Upload a file or view a list of files.<br><br>
~;
} 

##################################################
# Add some online help
sub npa_upload_LoadHelp {
$Help{'npa_upload_mainpage'} = q~Select a file you want to upload by using the "Choose" button, you can select serveral at a time. Once you have made your selection click "Upload".~;
$Help{'npa_upload_success'} = q~Your selected files have been uploaded.~;
# $Help{'npa_upload_delete'} = q~Select a file you wish to delete, the click on "DELETE SELECTED FILE" <b>PLEASE NOTE</b> You can not undo this fucntion once "DELETE SELECTED FILE" has been clicked.~;
$Help{'npa_remove_success'} = q~Your selected file have been deleted.~;
}
##################################################

# Setup all of the setting needed in the advance setting part of news pro
sub Addnpa_Upload_Settings {
$AdvDescr{'UploadDir'} = "Full serverpath to where your files are, no trailing slash ( eg. /home/site/path/to/image/dir ).";
$AdvDescr{'UploadURL'} = "The URL to the directory specified above, no trailing slash ( eg. http://www.yourname.com/image/dir ).";
$AdvDescr{'UploadDir2'} = "Full serverpath to a second upload directory ( please leave blank if you are only using one directory ).";
$AdvDescr{'UploadURL2'} = "The URL to a second upload directory ( please leave blank if you are only using one directory ).";
$AdvDescr{'UploadDir3'} = "Full serverpath to a third upload directory ( please leave blank if you are only using one directory ).";
$AdvDescr{'UploadURL3'} = "The URL to a third upload directory ( please leave blank if you are only using one directory ).";
$AdvDescr{'AllowAll'} = "Do you wish to allow all file types?  yes or no (no capital letters).";
$AdvDescr{'TheExt'} = "If the above = no then which is the only extention to allow. Remember to have the LAST 4 characters eg .gif";
$AdvDescr{'RemoteWidth'} = "The width (in pixels) of the remote window";
$AdvDescr{'RemoteHeight'} = "The height (in pixels) of the remote window";
push(@advancedsettings, 'draw_line','UploadDir','UploadURL','AllowAll','TheExt','RemoteWidth','RemoteHeight','UploadDir2','UploadURL2','UploadDir3','UploadURL3',);
}

##################################################
# Add the remote window link to the Submit Form

sub npa_UploadList_Link {
unless ($NPConfig{'UploadDir'} && $NPConfig{'UploadURL'} && $NPConfig{'AllowAll'} && $NPConfig{'RemoteWidth'} && $NPConfig{'RemoteHeight'}) {
print "You have not set-up npa_upload.pl, please goto <a href=\"$scripturl?advset\">\"Advanced Settings\"</a> and enter the UploadDir, UploadURL, AllowAll, RemoteWidth and RemoteHeight.";
}

print qq~<table width="95%" border="0" cellspacing="0" cellpadding="3" align="center"><tr>
<td><div align="center"><a href="#" onClick="MM_openBrWindow('$scripturl?npa_UploadList_remote','','scrollbars=yes,resizable=yes,width=200,height=400')">Click here to open a list of files you can use.</a></div>
</td></tr></table>~;
}


##################################################
# Upload Main Page (Thanks to Jim Logan for the mod)

sub npa_upload_main {
&NPHTMLHead("Upload a file");
unless ($NPConfig{'UploadDir'} && $NPConfig{'UploadURL'} && 
$NPConfig{'AllowAll'} && $NPConfig{'RemoteWidth'} && 
$NPConfig{'RemoteHeight'}) {
print "You have not set-up npa_upload.pl, please goto <a 
href=\"$scripturl?advset\">\"Advanced Settings\"</a> and enter the 
UploadDir, UploadURL, AllowAll, RemoteWidth and RemoteHeight.";
&NPHTMLFoot;
exit;
}
&npa_upload_body;
&NPHTMLFoot;
}

##################################################
# Main Page Content (Thanks to Jim Logan for the mod)

sub npa_upload_body {
print qq~
<form method="POST" action="upload.pl" enctype="multipart/form-data">
<div align="center">
<center>
<h4><font face="Arial">File Uploader 2.5</font></h4>
<table border="0" cellspacing="0" cellpadding="3" width="95%">
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 1: </font></b> 
<input type="file" name="FILE1">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 2: </font></b> 
<input type="file" name="FILE2">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 3: </font></b> 
<input type="file" name="FILE3">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 4: </font></b> 
<input type="file" name="FILE4">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 5: </font></b> 
<input type="file" name="FILE5">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 6: </font></b> 
<input type="file" name="FILE6">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 7: </font></b> 
<input type="file" name="FILE7">
</div>
</td>
</tr>
<tr> 
<td> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">File 8: </font></b> 
<input type="file" name="FILE8">
</div>
</td>
</tr>
<tr> 
<td align="center"> 
<div align="center"><b><font face="Arial, Verdana, Helvetica" size="2">Please select a directory from the list below (please leave as the default if you are not sure)</font></b></div>
</td>
</tr>
<tr> 
<td align="center"> 
<div align="center"> 
<select name="THEDIR">
<option value="$NPConfig{'UploadDir'}" selected>$NPConfig{'UploadURL'}</option>~;
if ($NPConfig{'UploadDir2'}) {
print qq~<option value="$NPConfig{'UploadDir2'}">$NPConfig{'UploadURL2'}</option>~;
}
if ($NPConfig{'UploadDir2'}) {
print qq~<option value="$NPConfig{'UploadDir3'}">$NPConfig{'UploadURL3'}</option>~;
}
print qq~</select>
</div>
</td>
</tr>
<tr> 
<td align="center"> 
<div align="center"> 
<input type="submit" value="Upload Now" name="submit">
</div>
</td>
</tr>
</table>
</center>
</div>
</form>
~;
&npa_UploadList_Files;
}
}

##################################################
# Delete Main Page

sub npa_upload_remove {
&NPHTMLHead("Delete a file");
unless ($NPConfig{'UploadDir'} && $NPConfig{'UploadURL'} && $NPConfig{'AllowAll'} && $NPConfig{'RemoteWidth'} && $NPConfig{'RemoteHeight'}) {
print "You have not set-up npa_upload.pl, please goto <a href=\"$scripturl?advset\">\"Advanced Settings\"</a> and enter the UploadDir, UploadURL, AllowAll, RemoteWidth and RemoteHeight.";
&NPHTMLFoot;
exit;
}
print qq~
<form method="POST" action="upremove.pl" enctype="multipart/form-data">
~;

my $filelist = 0; 

print qq~<b>Listing For $NPConfig{'UploadURL'}</b><TABLE>~; 

opendir (DIR, $NPConfig{'UploadDir'}); 

print qq~<TR>~; 
while ($filelist == 0) {

for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 

if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~
<TD WIDTH="10%" valign="middle" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><IMG SRC="$NPConfig{'UploadURL'}/$file" ALT="$file" WIDTH="80"></a></td>
</tr>
<tr>
<td><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-3">$file</font></a></td>
</tr>
<tr>
<td><font face="Geneva, Verdana, Helvetica" size="-3"> 
<input type="radio" name="FILE1" value="$file">Delete</font> </td>
</tr>
</table>
</TD>
~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;

} 
closedir(DIR);

print qq~</TABLE>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr> 
<td colspan="2"> 
<div align="center"> 
<input type="submit" value="DELETE SELECTED FILE" name="submit">
</div>
</td>
</tr>
</table>
</form>
~;
&NPHTMLFoot;
}


##################################################
# 
# Display the remote page
#  

sub npa_UploadList_Remote {

my $filelist = 0; 

&NPHTMLHead("Picture List"); 

print qq~ <TABLE> ~; 

opendir (DIR, $NPConfig{'UploadDir'}); 


while ($filelist == 0) {
#print qq~ <TR> ~; 

for ($i = 0; $i < 1; $i++) {
if (defined($file = readdir(DIR))) { 

if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TR><TD WIDTH="12,5%"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')">$file"</A></TD></TR>~;
}
}
else {
$filelist = 1;
}
}
#print qq~ </TR> ~;

} 
closedir(DIR);
print qq~ </TABLE>
~;
} 

##################################################
# 
# Display The Files

sub  npa_UploadList_Files{

if ($NPConfig{'UploadDir'}) {
my $filelist = 0; 
print qq~Listing For - <b>"$NPConfig{'UploadURL'}"</b><TABLE>~; 
opendir (DIR, $NPConfig{'UploadDir'}); 
print qq~<TR>~; 
while ($filelist == 0) {
for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 
if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TD WIDTH="10%" valign="middle" align="center"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-2">$file</font></a></TD>~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;
} 
closedir(DIR);
print qq~ </TABLE>~;
} 

if ($NPConfig{'UploadDir2'}) {
my $filelist = 0; 
print qq~Listing For - <b>"$NPConfig{'UploadURL2'}"</b><TABLE>~; 
opendir (DIR, $NPConfig{'UploadDir2'}); 
print qq~<TR>~; 
while ($filelist == 0) {

for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 

if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TD WIDTH="10%" valign="middle" align="center"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL2'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-3">$file</font></a></TD>~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;
} 
closedir(DIR);
print qq~ </TABLE>~;
}

if ($NPConfig{'UploadDir3'}) {
my $filelist = 0; 
print qq~Listing For - <b>"$NPConfig{'UploadURL3'}"</b><TABLE>~; 
opendir (DIR, $NPConfig{'UploadDir3'}); 
print qq~<TR>~; 
while ($filelist == 0) {

for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 

if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TD WIDTH="10%" valign="middle" align="center"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL3'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-3">$file</font></a></TD>~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;
} 
closedir(DIR);
print qq~ </TABLE>~;
}

}
##################################################
# 
# Display The File Names

sub  npa_UploadList_FilesNames{

if ($NPConfig{'UploadDir'}) {
my $filelist = 0; 
print qq~<TABLE>~; 
opendir (DIR, $NPConfig{'UploadDir'}); 
print qq~<TR>~; 
while ($filelist == 0) {
for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 
if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TD WIDTH="10%" valign="middle" align="center"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-3">$file</font></a></TD>~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;
} 
closedir(DIR);
print qq~ </TABLE>~;
} 

if ($NPConfig{'UploadDir2'}) {
my $filelist = 0; 
print qq~<TABLE>~; 
opendir (DIR, $NPConfig{'UploadDir2'}); 
print qq~<TR>~; 
while ($filelist == 0) {

for ($i = 0; $i < 6; $i++) {
if (defined($file = readdir(DIR))) { 

if ( $file !~ /^\.\.?$/) { # Only if not a File handle "." and ".."
print qq~<TD WIDTH="10%" valign="middle" align="center"><a href="#" onClick="MM_openBrWindow('$NPConfig{'UploadURL2'}/$file','','scrollbars=yes,resizable=yes,width=$NPConfig{'RemoteWidth'},height=$NPConfig{'RemoteHeight'}')"><font face="Geneva, Verdana, Helvetica" size="-3">$file</font></a></TD>~;
}
}
else {
$filelist = 1;
}
}
print qq~</TR>~;
} 
closedir(DIR);
print qq~ </TABLE>~;
}

}

##################################################
# Uploaded File

sub npa_upload_succ {
&NPHTMLHead("Files Uploaded");
print qq~ Your file(s) have now been uploaded, you should now be able to see them in the list below		
~;
&npa_UploadList_FilesNames;
&NPHTMLFoot;
}

##################################################
# Deleted File

sub npa_remove_succ {
&NPHTMLHead("Files Deleted");
print qq~ The file have now been deleted.		
~;
&npa_UploadList_FilesNames;
&NPHTMLFoot;
}

##################################################
1;
