A while ago, I installed joomla BibTeX (http://extensions.joomla.org/extensions/1766/details), a component to maintain a list of bibliography items. While tweaking the code to improve it, I found a serious race condition when downloading a bib file. I tried to reach the component's developer, but I've had no answer, and I need help to solve it myself.
The problematic code is the following. It gets executed when the user clicks a link to download a list of selected bibliography items.
Code: Select all
function showAllBib($database,$mainframe,$selected, $filter,$afilter,$limit,$limitstart,$catId){
/*SNIP - I took out some code here that sets some variables*/
$fp = fopen("components/com_jombib/download.bib", "w") or die("can't open file");
foreach($result as $contid){
$database->setQuery("SELECT content from #__bib_content where id='".$contid."';");
$bibstringall=$database->loadResult();
fwrite($fp, $bibstringall);
}
fclose($fp);
HTML_jombib::displayBibDownload();
}
The race condition appears because the file has always the same name in the server, and the user can take any time (seconds, minutes, days...) to download the bib file. If any other user (or even the same one, in another browser tab) selects another item list to download, the bib file is overwritten and both users get the same file. I believe this is a major usability issue.
I tried solving this replacing the function above with one that spits out the headers
Code: Select all
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$file\"\n");
Could you give me some pointers on how to solve this? The file should be generated on the fly (because the list of items to download is selected by the user) and, ideally, the download shouldn't take more than a single click (why clicking twice when a single click can do?). When this is solved, I'll post the changes here so all the users of this component can avoid the pitfall.
Oh, and one more thing. If the solution implies fiddling with the site's database, please do give me somewhat more detailed instructions. I am working on a production site so it's important to avoid erasing the whole database, or making it unreadable. I just began learning about Joomla and php... mySQL is somewhat of a mystery to me, right now.