Caion Posted April 22, 2005 Posted April 22, 2005 (edited) Hi, I have been working on a way to protect my video files from hotlinkers. Using .htaccess turned out to be a less than viable solution to my problem because the videos I have on my website are streamed from the page itself. If I protect the files with .htaccess I must allow for blank referers to access them, as referer information is not passed through Windows Media Player. Allowing blank referers, however, allows anyone using an <embed> tag to hotlink my videos and steal my bandwith. The best solution I could come up with was to randomize the urls leading to my media files. Here is how I presently accomplish this. Say the link to one of my videos would look like this. http://www.my-site.com/23234/cars.wmv I would write the actual link like this <a href="http://www.my-site.com/<? include "/randomlink.inc";?>/cars.wmv"> In the file, randomlink.inc, would be the name of the directory in which the video file is stored. Currently I alter the links manually. First I rename the actual folder the video files are stored in, copy the name, then paste it into my randomlink.inc file. What I'm looking for is a script to automate these tasks so I don't have to do them manually. Ideally it would be a php script that would perform two tasks: 1. Change the name of the video directory to a randomized name at a specifically set interval (hours, days, etc). 2. Write the changed name into the randomlink.inc file. I'd like to know if this is possible, and if someone could point out how that could be accomplished. Edited April 22, 2005 by Caion Quote
TCH-Thomas Posted April 23, 2005 Posted April 23, 2005 Hi and welcome to the forum. Unfortunately I dont know how to do this, but hold on, there is always someone that knows the answer. Quote
Pendragon Posted April 23, 2005 Posted April 23, 2005 Is this a single directory that needs renaming, or multiples? Don't know a thing about php, but it'd be fairly easy to do in perl. Quote
Pendragon Posted April 23, 2005 Posted April 23, 2005 Going on the assumption that randomlink.inc is a text file with a single entry that is the name of the directory... http://www.thehomeforums.com/members/pendragon/rendir.zip Quote
TweezerMan Posted April 23, 2005 Posted April 23, 2005 What I'm looking for is a script to automate these tasks so I don't have to do them manually. Ideally it would be a php script that would perform two tasks: 1. Change the name of the video directory to a randomized name at a specifically set interval (hours, days, etc). 2. Write the changed name into the randomlink.inc file. I'd like to know if this is possible, and if someone could point out how that could be accomplished. <{POST_SNAPBACK}> As Pendragon said, a script to perform the task of renaming the directory and writing that name into the randomlink.inc file is certainly possible - in Perl or PHP. The script by itself won't be automated though - for that, you need to run the script from a cron job. I have two suggestions for changes to Pendragon's code: 1) Since the script needs to run from a cron job, it should output *nothing* unless an error occurs. Printing "Reading $my_file", "Renaming directory", and "Updating $my_file" will cause cron to send an e-mail every time it runs the script. 2) If the directory is successfully renamed, but then the write to randomlink.inc fails, the directory should be renamed back to its old name. Otherwise, the directory name and the name contained in randomlink.inc will no longer match. I've attached a PHP script (rand.cgi) that pretty much does the same thing as Pendragon's script. To use the script, you'll need to do the following: 1) Edit the three variables at the beginning of the script, particularly $wwwDir - it should be the path of whatever directory contains randomlink.inc. 2) The script can be uploaded into any directory you wish - it does not have be placed in your public_html directory. If you want to be able to run it manually, it should be in the public_html directory or a subdirectory under the public_html directory (so it is accessible to your browser). 3) After uploading the script to your server, the script needs to be executable - change its permission to 0755. 4) The "random" directory (such as public_html/23234) needs to already exist, and the randomlinks.inc file (or whatever file name you use in the $randIncFileName variable) also needs to already exist, and contain the name of the "random" directory ("23234"). 5) If you want to test the script, you can do so by going directly to the rand.cgi script in your browser. If it works, you will see only a blank page displayed. You'll need to use CPanel's File Manager or your FTP program to see the directory name and that name in randomlink.inc changed. 6) To automate the script, you'll need to set up a cron job in your CPanel to run it at the interval you want. For the "Command to run", you just need to enter the full path to rand.cgi: >/home/cpanelName/public_html/rand.cgi (You should not need to prefix the file name with 'php -q'.) I think that covers about everything! rand.zip Quote
Pendragon Posted April 23, 2005 Posted April 23, 2005 > Printing .... will cause cron to send an e-mail every time it runs the script. I did not know that (never used 'cron jobs'). Those lines are of course, easily deleted. > but then the write to randomlink.inc fails, the directory should be renamed back to its old name. That's a good idea, didn't think about that. > The script can be uploaded into any directory you wish I thought cgi's could only be run from the cgi-bin? Quote
TCH-Bruce Posted April 23, 2005 Posted April 23, 2005 I thought cgi's could only be run from the cgi-bin? Depends on how a server is configured. The TCH servers are configured to allow scripts to be run from any directory. Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 Wow, you people are amazing! Thank you so much for your help. I'm going to try the script you've written right away. Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 Is this a single directory that needs renaming, or multiples? Don't know a thing about php, but it'd be fairly easy to do in perl. <{POST_SNAPBACK}> It's just one directory that needs renaming, and your assumption that the randomlink.inc file is a text document with the directory name in it is absolutely correct. Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 I've attached a PHP script (rand.cgi) that pretty much does the same thing as Pendragon's script. To use the script, you'll need to do the following: I'm getting an error with your script when I try to test it. The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, (email removed) and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. I've renamed the variables as specified and installed the script, but when I try to run it from my browser I get an internal server error. I double checked my file and directory names and they are all correct. I also tried renaming the cgi file into a php file, and when I tried to run it like that I got a different error message which said this. #!/usr/bin/php Error: Renaming directory /home/CpanelName/public_html/videos to /home/CpanelName/public_html/72359 failed (note, I've changed my actual cpanel name to "CpanelName" for the sake of this post). Quote
TweezerMan Posted April 24, 2005 Posted April 24, 2005 The actual error should be noted in the Error Log in CPanel, but my first guess would be that you did not change the permissions on rand.cgi to 0755 before trying to access it in your browser. I also tried renaming the cgi file into a php file, and when I tried to run it like that I got a different error message which said this. This is not a good idea. As a .cgi file, the script runs as you (your user ID, your user permissions), so it should be able to easily rename directories and write files in your account. As a .php script, the script runs as the user 'nobody', and will not be able to rename any directories unless they have 0777 permissions, nor modify any files in your account unless they at least 0666 permissions. Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 (edited) I checked everything again, and I found the problem. It was a silly mistake on my part. When I uploaded the file in my FTP program I forgot to switch it to ascii mode, so it uploaded the script in binary mode. I just tested the script and it worked. However there does appear to be a couple of bugs in the script. 1. In addition to altering the randomlink.inc file it creates a file named "/home/CpanelName/public_html/randomlink.inc" in the directory. 2. When the script alters the randomlink.inc file it includes a line break at the end, so a link that would look like this. http://www.my-site.com/23234/cars.wmv Looks like this. http://www.my-site.com/23234/cars.wmv Some of my links are in Javascript popups so this prevents them from working. Thanks. Edited April 24, 2005 by Caion Quote
TweezerMan Posted April 24, 2005 Posted April 24, 2005 1. In addition to altering the randomlink.inc file it creates a file named "/home/CpanelName/public_html/randomlink.inc" in the directory. <{POST_SNAPBACK}> I don't understand what you mean. Is the randomlink.inc that is being modified not in your public_html directory? Some more details about what you're seeing would be helpful. 2. When the script alters the randomlink.inc file it includes a line break at the end, so a link that would look like this. <{POST_SNAPBACK}> Something I thought was a good idea, but is clearly not. You can fix this by editing line 48 of the script from this: > if (!fwrite($fh, $randNum . "\n")) { ...to this, which should remove the extra line break: > if (!fwrite($fh, $randNum)) { Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 (edited) I don't understand what you mean. Is the randomlink.inc that is being modified not in your public_html directory? Some more details about what you're seeing would be helpful. Here is an example of how my directory listing looks. [Folder one][Folder two][Folder three] index.php about.php \home\CpanelName\public_html\randomlink.inc randomlink.inc media.php It appears as an actual file in my directory. just like that. Edited April 24, 2005 by Caion Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 I'm not sure what caused the error I mentioned above, but it seems it's not doing that anymore. I can only assume that the strange file in my directory was the result of something I did earlier. I modified the script to get rid of the line break, and now it works perfectly. I'm setting up a cron job for the script now. Thanks a bunch! Quote
TweezerMan Posted April 24, 2005 Posted April 24, 2005 I'm not sure what caused the error I mentioned above, but it seems it's not doing that anymore. I can only assume that the strange file in my directory was the result of something I did earlier. <{POST_SNAPBACK}> I was thinking that maybe it got created when you first tried to run it (the script wasn't uploaded in ASCII mode, and you received the 500 Internal Server Error). I was going to suggest that you delete the file and see if it comes back. Glad to hear that it's working now! Quote
Caion Posted April 24, 2005 Author Posted April 24, 2005 I'm not sure what caused the error I mentioned above, but it seems it's not doing that anymore. I can only assume that the strange file in my directory was the result of something I did earlier. <{POST_SNAPBACK}> I was thinking that maybe it got created when you first tried to run it (the script wasn't uploaded in ASCII mode, and you received the 500 Internal Server Error). I was going to suggest that you delete the file and see if it comes back. Glad to hear that it's working now! <{POST_SNAPBACK}> You have been a big help. I thank you and Pendragon for taking the time to assist me. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.