trouble creating .htaccess files

hi there

I’ve been writing a script to automate the process of creating a web folder online (for clients to see stuff) and then have it be password protected. Our server is using apache and as such i decided to utilize a htpasswd and htaccess file.

I’ve got the script mostly working - i can successfully create the online folder, create a preference file with the name of this folder locally (for use in other scripts) ask for a user name and password and have the script generate a passwd and htaccess file and upload them to the appropriate places on our server online.

however, i get the following error “The server encountered an internal error or misconfiguration and was unable to complete your request.” when going to a supposedly password protected url on our server. If i delete the .htaccess file just created in said folder, i can get access to it again - leading me to believe that at least the server is seeing the htaccess file and is trying to do something with it, but failing.

Interestingly enough if i manually type an .htaccess file in textedit, the above works fine - even if the contents of my manually typed file matches exactly the script generated one which fails.

My guess is that it must have something to do with the way the script is encoding the text in the file.

here’s the script:



--properties left blank for security
--i've left the .htaccess file as htaccess and have been manually adding the "." until testing is done :)

property webScripts : ""
property ftpInfo : "" -- in format ftp://user:pass@ftp.server.com:21
property passwdPath : ""
property clientPath : ""
property authUserPath : "/home/user/.htpasswds/clients/"


tell application "Finder"
	
	activate
	set localPath to container of (path to me) as string
	
	repeat
		display dialog "Web folder name: don't use '/' unless you mean to create a subfolder (eg parent/subfolder)" default answer "" buttons {"Cancel", "Create"} default button 2
		set folderName to (text returned of result)
		if result is not "" then
			--changes any characters not found in okCharacters to make it websafe
			set okCharacters to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_/"
			set allCharacters to characters of (folderName as text)
			set characterList to allCharacters
			repeat with k from 1 to (count allCharacters)
				if (item k of characterList is in okCharacters) then
				else
					set item k of characterList to "_"
				end if
			end repeat
			set okName to characterList as text
			set folderName to okName
			
			try
				--once folderName is made websafe, create the folder online
				do shell script "curl 'CWD " & clientPath & folderName & "' " & ftpInfo --checks to see if folder already exists online. If it does, start repeat over again
				do shell script "curl -Q 'MKD " & clientPath & folderName & "' " & ftpInfo --creates the folder online 
				duplicate every file of folder webScripts to localPath
				my createPrefFile(localPath, folderName)
				set link to "http://www.headgearanimation.com/clients/" & folderName as string
				display dialog "Client Folder created succesfully at " & link buttons {"Ok"} default button 1
				set the clipboard to link
				exit repeat
			on error
				display dialog "There's a problem with the name you entered.  Does that folder already exist online?" buttons "Ok" default button 1 with icon caution
			end try
		else if result is "" then
			display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
		end if
	end repeat
	
	display dialog "Would you like to password protect this new folder?" buttons {"Cancel", "Yes"} default button "Yes"
	
	if (button returned of result) is "Yes" then
		my createPasswdFile(localPath, folderName)
	end if
	
end tell

on createPrefFile(localPath, folderName)
	--creates the text file
	set prefFile to (((localPath) as string) & "_webdata.txt") as file specification
	
	try
		open for access prefFile with write permission
		set eof of prefFile to 0
		set folderName to folderName as text
		write (folderName & return) to prefFile starting at eof
		close access prefFile
	on error
		try
			close access prefFile
		end try
	end try
end createPrefFile

on createPasswdFile(localPath, folderName)
	tell application "Finder"
		activate
		
		repeat
			display dialog "Please Enter Username" default answer "" buttons {"Cancel", "Continue."} default button 2
			set theUsername to (text returned of result)
			if theUsername is not "" then exit repeat
			display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
		end repeat
		
		repeat
			display dialog "Please Enter Password" default answer "" buttons {"Cancel", "Ok"} default button 2
			set thePassword to (text returned of result)
			if thePassword is not "" then exit repeat
			display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
		end repeat
		
		set theResult to (do shell script "htpasswd -bmnd " & theUsername & " " & thePassword)
		set passwdFile to (((localPath) as string) & "passwd") as file specification
		try
			open for access passwdFile with write permission
			set eof of passwdFile to 0
			write (theResult & return) to passwdFile starting at eof
			close access passwdFile
		on error
			try
				close access passwdFile
			end try
		end try
		
		set htaccessFile to (((localPath) as string) & "htaccess") as file specification
		try
			open for access htaccessFile with write permission
			set eof of htaccessFile to 0
			write (return & "AuthType Basic" & return & return) to htaccessFile starting at eof
			write ("AuthName \"Restricted Area\"" & return & return) to htaccessFile starting at eof
			write ("AuthUserFile \"" & authUserPath & folderName & "/passwd\"" & return & return & return) to htaccessFile starting at eof
			write ("require valid-user" & return) to htaccessFile starting at eof
			close access htaccessFile
		on error
			try
				close access htaccessFile
			end try
		end try

		set passwdFile to POSIX path of passwdFile
		do shell script "curl -Q 'MKD " & passwdPath & folderName & "' " & ftpInfo --creates the password folder online	
		do shell script "curl -T " & passwdFile & "  " & ftpInfo & passwdPath & folderName & "/" --uploads the passwd file
		
		set htaccessFile to POSIX path of htaccessFile
		do shell script "curl -T " & htaccessFile & "  " & ftpInfo & clientPath & folderName & "/"  --uploads the .htaccess file
	
	end tell
	
end createPasswdFile

and here is an example htaccess file from the script:
[b]AuthType Basic

AuthName “Restricted Area”

AuthUserFile “/home/user/.htpasswds/clients/job/passwd”

require valid-user[/b]

and here is one generated from the online tool from our host company:
[b]AuthType Basic

AuthName “Restricted Area”

AuthUserFile “/home/user/.htpasswds/clients/job/passwd”

require valid-user[/b]

Anyone have any tips at all as to what might be going on? Should i just be using some sort of shell script to write the htaccess file so its already in proper unix format and save it somewhere specific for me? same for the password file - could i just echo the results of the htpasswd command to a file at a set location?

any help would be greatly appreciated

Hi Kyle,

I’m not able to reply directly to your script itself, however I’m sure others will certainly pipe in here. I think this is a really nifty script/idea! :smiley:

I assume you’re running OSX Server?

The reason I ask this, is it sounds like your script is working fine, but once the file itself is physically created on the server, it doesn’t have the right permissions.

I’m only speculating here, but it sounds like your’re creating the .htaccess file ok, only the htpasswd is the problem.

As a suggestion, try to set permissions on the file after it’s been created on the server, if possible, before the user has a chance to write any info to the htpasswd file.

By all means, please consider posting this as a solution to Code Exchange, or even as a complete package to ScriptBuilders! :slight_smile:

Let me second Ray Barber’s invitation, Kyle. This is a really neat concept. I’ve copied it to my desktop so I can figure out what properties you’ve omitted (you left out the names). ScriptBuilders would love to have it.

Adam

I’ll totally offer it up as a complete script once i get it working - but for now i still seems to create .htaccess files that are somehow incorrect. There’s some other stuff in this script which don’t really have anything to do with password protecting a site - like the’ property webScripts’ bit. Maybe i’ll strip all that stuff out after i get this all sorted.

There’s nothing really missing from that script i posted - just some paths to things i probably didn’t need to leave out, but they are on my companies servers… anyway, it was totally over cautious so here you go


property webScripts : ""  -- just a local path to where I have some other scripts to copy into the folder this script resides in (localPath) when a user creates an online client folder using this script
property ftpInfo : "" -- in format ftp://user:pass@ftp.server.com:21
property passwdPath : "/.htpasswds/clients/" --just the  base url path where the passwd files are stored online - in this case, dictated by my hosting company.  
property clientPath : "/public_html/clients" -- url path to where the new password protected folder is to be created 
property authUserPath : "/home/user/.htpasswds/clients/" --base "path" used in the line AuthUserPath in the .htaccess file


Again, if i can get this working i’ll gladly clean it up as best i can and comment each part more carefully, etc. But i still can’t seem to get it to work…

Thanks for the permissions tip - i checked that and it ends up the applescript created files are the same as what the hosting companies cpanel tool creates, so i don’t think that’s the problem - could be wrong though. I still think its an encoding problem, the way applescript writes to a file so i’m thinking of trying to write to a file using a “do shell script” or something…

I’m not certain of this, but it could be that AppleScript is using a return character and shell scripts require a newline character. I’ll look into it tomorrow.

that helped in creating the htaccess file - thanks!

here’s what i changed - first declared a variable “newline” to equal ASCII character 10, which i got from a googled site somewhere.

property newline : ASCII character 10

then in the code where i write to the file, i replaced the "& return"s and replaced them with “newline”




set htaccessFile to (((localPath) as string) & ".htaccess") as file specification
		try
			open for access htaccessFile with write permission
			set eof of htaccessFile to 0
			write (return & "AuthType Basic" & newline & newline) to htaccessFile starting at eof
			write ("AuthName \"Restricted Area\"" & newline & newline) to htaccessFile starting at eof
			write ("AuthUserFile \"" & authUserPath & folderName & "/passwd\"" & newline & newline & newline) to htaccessFile starting at eof
			write ("require valid-user" & newline) to htaccessFile starting at eof
			close access htaccessFile
		on error
			try
				close access htaccessFile
			end try
		end try

So now i’m finally getting a prompt to enter a login and password online!

But, i think something is up with my password file. (oh, i changed the return in that part to newline as well) I’m trying to write to the password file using a do shell script command - but no luck so far…

Kyle

Have you checked that it’s even getting there? Is the curl ftp part doing its thing or is the pw file gefritzed?

actually i finally just got this working! I’m in the process of cleaning it up a bit now and i’ll see about posting it soon. Thanks for all the help!

cool! :cool:

In the process of cleaning up the code, and putting in error checking, etc, i seem to have broken the script again!!! ARGH!

I don’t think the error checking is working right -

can anyone try this out?



property webScripts : "Geezer:Templates:Web_Scripts:" --path to other scripts for copying
property ftpInfo : "xxx" -- in format ftp://user:pass@ftp.server.com:21
property passwdPath : "/.htpasswds/clients/" --base path for the passwd file online.  "folderName" gets appended to this in the script
property clientPath : "/public_html/clients/" --base path for the created folder "folderName"
property authUserPath : "/home/headgear/.htpasswds/clients/" --base path used in .htaccess file.  full path gets appended in script
property newline : ASCII character 10 -- newline character to be used so unix understands the created files


tell application "Finder"
	
	activate
	set localPath to container of (path to me) as string
	
	repeat
		display dialog "Please enter the folder name." & return & "Don't use '/' unless you mean to create a subfolder (eg parent/subfolder)" default answer "" buttons {"Cancel", "Create"} default button 2
		set folderName to (text returned of result)
		
		if result is not "" then
			
			--changes any characters not found in okCharacters to make it websafe
			set okCharacters to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_/" --list of ok characters
			set allCharacters to characters of (folderName as text) --get the number of characters in folderName
			set characterList to allCharacters
			repeat with k from 1 to (count allCharacters) -- do the loop for the number of times found in folderName
				if (item k of characterList is in okCharacters) then
				else
					set item k of characterList to "_"
				end if
			end repeat
			set okName to characterList as text
			set folderName to okName
			
			

			
			try
				
				do shell script "curl 'CWD " & clientPath & folderName & "' " & ftpInfo --checks to see if folder already exists online. If it does, give the following options
				display dialog "the folder \"" & folderName & "\" already exists." & return & "Do you want to quit, re-enter a new folder name, or password protect this existing one?" buttons {"Quit", "Re-Enter", "Enter password"} default button 3 with icon caution
				if (button returned of result) is "Quit" then
					exit repeat
				else if (button returned of result) is "Enter password" then
					my createPasswdFile(localPath, folderName)
					my createPrefFile(localPath, folderName)
					exit repeat
				end if
			on error
				do shell script "curl -s 'MKD " & clientPath & folderName & "' " & ftpInfo --creates the folder online 
				duplicate every file of folder webScripts to localPath
				my createPrefFile(localPath, folderName)
				set link to "http://www.headgearanimation.com/clients/" & folderName as string
				set the clipboard to link
				display dialog "Client Folder created succesfully at: " & return & link & return & "Would you like to password protect this folder?" buttons {"Cancel", "Yes"} default button 2
				if (button returned of result) is "Yes" then
					my createPasswdFile(localPath, folderName)
					my createPrefFile(localPath, folderName)
				end if
				exit repeat
			end try
			
		else if result is "" then
			display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
		end if
		
	end repeat
	
end tell



on createPrefFile(localPath, folderName)
	--creates the preference file containing folderName for later retrieval by other scripts
	set prefFile to (((localPath) as string) & "_webdata.txt") as file specification
	
	try
		open for access prefFile with write permission
		set eof of prefFile to 0
		set folderName to folderName as text
		write (folderName & return) to prefFile starting at eof
		close access prefFile
	on error
		try
			close access prefFile
		end try
	end try
end createPrefFile



on createPasswdFile(localPath, folderName)
	tell application "Finder"
		activate
		
		repeat
			
			repeat
				display dialog "Please enter the username" default answer "" buttons {"Cancel", "Continue."} default button 2
				set theUsername to (text returned of result)
				if theUsername is not "" then exit repeat
				display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
			end repeat
			
			repeat
				display dialog "Please enter the password" default answer "" buttons {"Cancel", "Ok"} default button 2
				set thePassword to (text returned of result)
				if thePassword is not "" then exit repeat
				display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
			end repeat
			
			display dialog "Folder \"" & folderName & "\" will be password protected with user name \"" & theUsername & "\" and password \"" & thePassword & "\"" buttons {"Re-enter", "Ok"} default button 2
			if (button returned of result) is "Ok" then exit repeat
		end repeat
		
		--creates the htpasswd encrypted file called passwd at the path localPath/passwd
		set localPosixPath to POSIX path of localPath --shell scripts need the unix compatible path format /something/something/
		do shell script "htpasswd -bc  " & localPosixPath & "passwd " & theUsername & " " & thePassword
		
		--creates the htaccess file
		set htaccessFile to (((localPath) as string) & ".htaccess") as file specification
		try
			open for access htaccessFile with write permission
			set eof of htaccessFile to 0
			write (return & "AuthType Basic" & newline & newline) to htaccessFile starting at eof
			write ("AuthName \"Restricted Area\"" & newline & newline) to htaccessFile starting at eof
			write ("AuthUserFile \"" & authUserPath & folderName & "/passwd\"" & newline & newline & newline) to htaccessFile starting at eof
			write ("require valid-user" & newline) to htaccessFile starting at eof
			close access htaccessFile
		on error
			try
				close access htaccessFile
			end try
		end try
		
		
		--Upload the password file to the correct location online
		set passwdFile to POSIX path of localPath & "passwd"
		do shell script "curl -s 'MKD " & passwdPath & folderName & "' " & ftpInfo --creates the password folder online	
		do shell script "curl -T " & passwdFile & "  " & ftpInfo & passwdPath & folderName & "/" --uploads the passwd file
		
		--Upload the .htaccess file to the new folder online	
		set htaccessFile to POSIX path of htaccessFile
		do shell script "curl -T " & htaccessFile & "  " & ftpInfo & clientPath & folderName & "/" --uploads the .htaccess file
		
		
		display dialog "Folder \"" & folderName & "\" was successfully  password protected." buttons "Ok" default button 1
	end tell
	
end createPasswdFile









Some of your tries close access as their only action - why not have them warn you somehow. Reading through it, though, I don’t see anything obvious, and I don’t have a server set up to try it (because it hasn’t arrived yet).

I’ve been working away on the script and its working fine now. I’m now trying to add the option to delete a folder and its contents but can’t seem to get that part to work. I’m trying different variations on curl RMD, DELE, etc., with no luck. I’m not finding much documentation online about it either - any tips?

Here’s some of the (incomplete) code i’m using:


on deleteTheFolder(localPath)
	tell application "Finder"
		
		set delDir to do shell script "curl -l " & ftpInfo & clientPath & "/" --gets list of existing folders at clientPath and puts it in delDir
		repeat
			display dialog "Delete which of these folders?" & return & delDir default answer "" buttons {"Cancel", "Delete"} default button 2
			set folderName to (text returned of result)
			
			if delDir contains folderName then
				display dialog "Are you sure you want to delete the folder \"" & folderName & "\" and all its contents?" buttons {"Yes", "Cancel"} default button 2 with icon caution
				
				if (button returned of result) is "Yes" then
					--do shell script "curl RMD " & clientPath & folderName & ftpInfo --creates the password folder online
					do shell script "curl 'DELE " & clientPath & folderName & "/*' " & ftpInfo --creates the password folder online
					do shell script "curl 'RMD " & clientPath & folderName & ftpInfo & "' " --creates the password folder online
					set delPassDir to do shell script "curl -l " & ftpInfo & passwdPath & "/" --gets list of existing folders at clientPath and puts it in delPassDir
					if delPassDir contains folderName then do shell script "curl 'DELE " & passwdPath & folderName & "' " & ftpInfo
					exit repeat
				else
					exit repeat
				end if
				
			else
				display dialog "Sorry, \"" & folderName & "\" was not in the list.  Please try again." buttons {"Ok"} default button 1 with icon caution
			end if
			
		end repeat
		
	end tell
end deleteTheFolder


You are going to post it, I hope.

Some suggestions: "display dialog “Delete which of these folders?” & return & delDir default answer “” buttons {“Cancel”, “Delete”} default button " could also be done with:

set delDir to {"alpha", "beta", "gamma"}
choose from list delDir with prompt "Please choose the folder to delete from the list below" OK button name "Delete" without multiple selections allowed and empty selection allowed
-- returns choice as string in a list
set folderName to result as string

With that structure you don’t need to check whether the entry is accurate, it must be from the list.

I can’t help you with the rest because I’m not sure how one would go about ftp’ing a shell command to delete a file. I think most folks would have gone for a secure session approach, and confess I haven’t done that from a script.

wow thanks for that tip!

here’s what became of that:


on deleteTheFolder(localPath)
	tell application "Finder"
		
		set delDir to do shell script "curl -l " & ftpInfo & clientPath & "/" --gets list of existing folders at clientPath and puts it in delDir
		set delDir to delDir's paragraphs --changes the unix curl -l list into a list format suitable for applescript
		--repeat
		choose from list (delDir) with prompt "Please choose the folder to delete from the list below" OK button name "Delete" without multiple selections allowed and empty selection allowed
		set folderName to result as string
		display dialog "Are you sure you want to delete the folder \"" & folderName & "\" and all its contents?" buttons {"Yes", "Cancel"} default button 2 with icon caution
		
		--do shell script "curl RMD " & clientPath & folderName & ftpInfo --creates the password folder online
		do shell script "curl 'DELE " & clientPath & folderName & "/*' " & ftpInfo --creates the password folder online
		do shell script "curl 'RMD " & clientPath & folderName & ftpInfo & "' " --creates the password folder online
		set delPassDir to do shell script "curl -l " & ftpInfo & passwdPath & "/" --gets list of existing folders at clientPath and puts it in delPassDir
		if delPassDir contains folderName then do shell script "curl 'DELE " & passwdPath & folderName & "' " & ftpInfo
		
	end tell
end deleteTheFolder


now if i could just figure out how to actually delete the folder i’ld be in business… :slight_smile:

Have you conquered the beast yet?

here’s were i’m at:


--change the following to suit your server settings, etc.
--on our server, the password files and htaccess files are stored it 2 different locations/  the passwd  file is stored in a sub folder at /.htpasswds/clients/subfolder/  and the the .htaccess file is stored at the path /public_html/clients/subfolder/

property webScripts : "" --path to other scripts for copying
property ftpInfo : "" -- in format ftp://user:pass@ftp.server.com:21
property passwdPath : "/.htpasswds/clients/" --base path for the passwd files online.  "folderName" gets appended to this in the script
property clientPath : "/public_html/clients/" --base path for the created folder "folderName"
property authUserPath : "/home/headgear/.htpasswds/clients/" --base path used in .htaccess file.  full path gets appended in script

--at various times during the script, "returns" are needed, and at other times unix "newlines" are needed.  
property charNewline : ASCII character 10
property charReturn : ASCII character 13

tell application "Finder"
	activate
	set localPath to container of (path to me) as string --gets path to this script, in path:to:me format
	display dialog "What would you like to do? Change an exisiting password, or create a new client folder, (with or without a password?)" buttons {"Cancel", "Change Password", "Create Folder"} default button 3
	if (button returned of result) is "Change Password" then
		my changeThePassword(localPath)
	else if (button returned of result) is "Create Folder" then
		my createTheFolder(localPath)
	end if
end tell


on changeThePassword(localPath)
	--gets existing folders at the specified location and returns them to user in a list format to choose from, then username/password are collected and passed to createPasswdFile
	
	tell application "Finder"
		activate
		set delDir to do shell script "curl -l " & ftpInfo & clientPath & "/" --gets list of existing folders at clientPath and puts it in delDir
		set delDir to delDir's paragraphs --changes the unix curl -l list into a list format suitable for applescript
		choose from list (delDir) with prompt "Please choose the folder you would like to change the password for from the list below" OK button name "Choose" without multiple selections allowed and empty selection allowed
		set folderName to result as string
		display dialog "Are you sure you want to change the password for the folder \"" & folderName & "\"?" buttons {"Cancel", "Yes"} default button 2 with icon caution
		if (button returned of result) is "Yes" then
			my createPasswdFile(localPath, folderName)
		end if
	end tell
end changeThePassword


on createTheFolder(localPath)
	--gets a new client folder name, changes any letters in the name not found in "okCharacters" to a "_" to ensure that the name is safe for the web,
	--then creates the folder, if it doesn't already exist online.  If it does, options to add or change the password for it	 are given.
	
	tell application "Finder"
		repeat
			display dialog "Please enter the folder name." & return & "Don't use '/' unless you mean to create a subfolder (eg parent/subfolder)" default answer "" buttons {"Cancel", "Create"} default button 2
			set folderName to (text returned of result)
			
			--changes any characters not found in okCharacters to make it websafe:
			if result is not "" then
				set okCharacters to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_/" --list of ok characters
				set allCharacters to characters of (folderName as text) --get the number of characters in folderName
				set characterList to allCharacters
				repeat with k from 1 to (count allCharacters) -- do the loop for the number of times found in in all characters, aka the number of characters in folderName
					if (item k of characterList is in okCharacters) then --if letter k folderName is in okCharacters, do nothing
					else --change the letter to a "_"
						set item k of characterList to "_"
					end if
				end repeat
				set okName to characterList as text
				set folderName to okName
				
				--test if folderName already exists online, and if it does ask user if they want to try a different name, or change the existing folders password:
				set testDir to do shell script "curl -l " & ftpInfo & clientPath & "/" --gets list of existing folders at clientPath via a unix shell command, and puts it in testDir
				if testDir contains folderName then
					display dialog "The folder \"" & folderName & "\" already exists. Do you want to quit, re-enter a different folder name, or password protect this existing one? (will overide any existing passwords)" buttons {"Quit", "Re-Enter", "Enter password"} default button 3 with icon caution
					if (button returned of result) is "Quit" then
						exit repeat
					else if (button returned of result) is "Enter password" then
						my createPasswdFile(localPath, folderName)
						my createPrefFile(localPath, folderName)
						exit repeat
					end if
				else
					do shell script "curl -Q 'MKD " & clientPath & folderName & "' " & ftpInfo --creates the folder online 
					duplicate every file of folder webScripts to localPath replacing yes
					my createPrefFile(localPath, folderName)
					set link to "http://www.headgearanimation.com/clients/" & folderName & "/" as string
					set the clipboard to link
					display dialog "Client Folder created succesfully at: " & return & link & return & "Would you like to password protect this folder?" buttons {"Cancel", "Yes"} default button 2
					if (button returned of result) is "Yes" then
						my createPasswdFile(localPath, folderName)
						my createPrefFile(localPath, folderName)
					end if
					exit repeat
				end if
			else if result is "" then
				display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
			end if
			
		end repeat
	end tell
end createTheFolder


on createPrefFile(localPath, folderName)
	--creates the preference file "_webdata" containing folderName for later retrieval by other scripts
	
	set prefFile to (((localPath) as string) & "_webdata.txt") as file specification
	
	try
		open for access prefFile with write permission
		set eof of prefFile to 0
		set folderName to folderName as text
		write (folderName & return) to prefFile starting at eof
		close access prefFile
	on error
		try
			close access prefFile
		end try
	end try
end createPrefFile


on createPasswdFile(localPath, folderName)
	--gets a user name and password, and passes them to a shell script which uses os x's apache's htpasswd command for the creation of a password file.  Different servers use different naming conventions for this file - ours uses passwd so i kept it the same here.  change it as needed
	
	tell application "Finder"
		activate
		
		repeat
			repeat
				display dialog "Please enter a username" default answer "" buttons {"Cancel", "Continue."} default button 2
				set theUsername to (text returned of result)
				if theUsername is not "" then exit repeat
				display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
			end repeat
			
			repeat
				display dialog "Please enter a password" default answer "" buttons {"Cancel", "Ok"} default button 2
				set thePassword to (text returned of result)
				if thePassword is not "" then exit repeat
				display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
			end repeat
			
			display dialog "Folder \"" & folderName & "\" will be password protected with user name \"" & theUsername & "\" and password \"" & thePassword & "\"." buttons {"Re-enter", "Ok"} default button 2
			if (button returned of result) is "Ok" then exit repeat
		end repeat
		
		--creates the htpasswd encrypted file called passwd at the path localPath/passwd
		set localPosixPath to POSIX path of localPath --shell scripts need the unix compatible path format /something/something/
		do shell script "htpasswd -bc " & localPosixPath & "passwd " & theUsername & " " & thePassword
		
		--creates the htaccess file
		--i think you only need to ensure each command is on a seperate line, so all the charNewLines here are probably extraneous,
		--but are here for consistency to  match what our server creates
		set htaccessFile to (((localPath) as string) & ".htaccess") as file specification
		try
			open for access htaccessFile with write permission
			set eof of htaccessFile to 0
			write (return & "AuthType Basic" & charNewline & charNewline) to htaccessFile starting at eof
			write ("AuthName \"Restricted Area\"" & charNewline & charNewline) to htaccessFile starting at eof
			write ("AuthUserFile \"" & authUserPath & folderName & "/passwd\"" & charNewline & charNewline & charNewline) to htaccessFile starting at eof
			write ("require valid-user" & charNewline) to htaccessFile starting at eof
			close access htaccessFile
		on error
			try
				close access htaccessFile
			end try
		end try
		
		
		--Upload the password file to the correct location online:
		set passwdFile to localPosixPath & "passwd" --sets passwdFile to a posix path of the passwf file -ie /path/to/passwd
		set testPassDir to do shell script "curl -l " & ftpInfo & passwdPath & "/" --gets list of existing folders at passwdPath and puts it in testPassDir.  Used to ensure script doesn't try to create over an exisiting folder (which makes it fail)
		if testPassDir contains folderName then
			do shell script "curl -T " & passwdFile & "  " & ftpInfo & passwdPath & folderName & "/" --uploads the passwd file
		else
			do shell script "curl -Q 'MKD " & passwdPath & folderName & "' " & ftpInfo --creates the password folder online			
			do shell script "curl -T " & passwdFile & "  " & ftpInfo & passwdPath & folderName & "/" --uploads the passwd file
		end if
		
		--Upload the .htaccess file to the new folder online	
		set htaccessFile to POSIX path of htaccessFile
		do shell script "curl -T " & htaccessFile & "  " & ftpInfo & clientPath & folderName & "/" --uploads the .htaccess file
		
		delete localPath & "passwd" --cleans up
		delete localPath & ".htaccess"
		
		display dialog "Folder \"" & folderName & "\" was successfully password protected with user name \"" & theUsername & "\" and password \"" & thePassword & "\"." buttons "Ok" default button 1
		
	end tell
end createPasswdFile


Its all working properly unless the folder containing the script has spaces in it, which causes the htpasswd shell script to fail. I’ld love some tips on that one, and as i’m a relative noob at this if anyone notices anything that could be cleaned up i’ld love to hear it.

Thanks!

I think this is the way around that:

	do shell script "rm " & quoted form of theFile

Hey Adam - thanks for the tip but i actually gave up on trying to delete the folders online for now. Otherwise the script is working but i’m still just stuck on:



		set localPosixPath to POSIX path of localPath --shell scripts need the unix compatible path format /something/something/
		do shell script "htpasswd -bc " & localPosixPath & "passwd " & theUsername & " " & thePassword



Seems i’m not understanding something properly - cause if the variable localPath contains any spaces the script fails at this point. Is there a way to deal with the spaces? I was under the impression that a posix path converted spaces ok - guess not?

thanks!
Kyle

Argh!

i feel like i’ve tried everything but i just can’t seem to get this last troublesome part of the script to work. Ive tried some versions using “quoted form of”, but still no luck.

Then i tried manually turning the spaces in the filename into "\ " using the following, but it doesn’t help:



	set localPosixPath to POSIX path of localPath & "passwd" --shell scripts need the unix compatible path format /something/something/
		set chars to every character of localPosixPath
		repeat with i from 2 to length of chars
			if item i of chars as text is equal to " " then
				set item i of chars to "\\ "
			end if
		end repeat
		set localPosixPath to chars as Unicode text
		--display dialog localPosixPath
		do shell script "htpasswd -bc " & localPosixPath & " " & theUsername & " " & thePassword


the curl command still gets tripped up if there is a space in localPath. Anyone have a solution? It would be VERY much appreciated!

-Kyle

If you actually go to a terminal session on the client machine, what does it take?

The only alternative that occurs to me is to write in script that prevents their ever being a space in the folder referred to earlier in the game.