Scan Drupal installations for version number

In case you got multiple Drupal installations at your server and want to find out the current version of each:

for i in $(find -maxdepth 2 -name CHANGELOG.txt); do dirname $i; head -n 2 $i|grep Drupal ; done

Use "drush up" to update where needed.

ZIP direct download of current node filefield

Add a node menu local task that will create a temporary ZIP file on-the-fly containing files from a file field.
All files of a filefield identified by the field name (defaults to: field_download) will be added to it.
Force a download by the user's browser.


/**
* Implementation of hook_menu
*/
function onlinepdf_menu() {
$items['node/%node/zip'] = array(
'title' => 'Download as ZIP',
'page callback' => 'onlinepdf_zip',
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}


/**
* Page callback
* function: force a zip file to download generated on the fly with files from current node filefield
* arguments: filefield name (defaults to field_download)
*
*/
function onlinepdf_zip($filefield = 'field_download') {
//loads node object from current path
$node = menu_get_object("node", 1, current_path());
//check if the filefield exists and contains file records
if (isset($node->{$filefield}) && isset($node->{$filefield}[LANGUAGE_NONE]) > 0)
{
//create zip archive and add the files to it
$zip = new ZipArchive();
$uri = 'public://zip/'.mt_rand().'.zip';
$filepath = drupal_realpath($uri);
$filename = basename($filepath);
//could not create zip file, throw error and 404
if ($zip->open($filepath, ZipArchive::CREATE)!==TRUE) {
drupal_set_message(t('Could not open %filename', array('%filename' => $filename)), 'error');
watchdog("onlinepdf", 'Could not open %filename', array('%filename' => $filename), WATCHDOG_ERROR);
}
foreach ($node->field_download[LANGUAGE_NONE] as $file)
{
if (!$zip->addFile(drupal_realpath($file['uri']), $file['filename']))
{
//error in adding file to zip. throw error, discard zip and give 404
drupal_set_message(t('Could not add %filename to zip archive', array('%filename' => $file['filename'])), 'error');
watchdog("onlinepdf", 'Could not add %filename to zip archive', array('%filename' => $filename), WATCHDOG_ERROR);
$zip->close();
unlink($filepath);
return drupal_not_found();
}
}
$zip->close();
//create file object from zip file
$file = new stdClass;
$file->filename = $filename;
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
$file->filesize = filesize($filepath);
//set file as temporary. Will be deleted after 6 hours by cron
$file->status = 0;
$file = file_save($file);
file_transfer($file->uri, array('Content-disposition' => 'attachment; filename='.$filename));
return;
}
else {
//no filefield with file records found, throw 404
return drupal_not_found();
}
}

Forcibly download a file by FID

Add a record to Drupals menu system with hook_menu
The path download/% will pass % to page callback hook_force_download.
This page callback function loads the file object and returns the file to the users browser as an attachment.
When the fid arguments is not given or there is no corresponding file object, throw an error and show a 404 page with the error message.

The small amount of code proofs the outstanding possibilities of the Drupal framework once again.


/**
* Implementation of hook_menu
*/
function onlinepdf_menu() {
$items['download'] = array(
'title' => 'Download as ZIP',
'page callback' => 'onlinepdf_force_download',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}


/**
* Page callback
* Purpose: force a download of a file
* Arguments: fid file identifier
*/
function onlinepdf_force_download($fid = FALSE) {
//load file object from fid argument when fid is given and fid represents a file object
if (if ($fid) && file_load($fid))
{
$file = file_load($fid);
//extract filename from the file's stream wrapper uri
$filename = basename(drupal_realpath($file->uri));
dsm($file);
//send the file to the browser as an attachment
file_transfer($file->uri, array('Content-disposition' => 'attachment; filename='.$filename));
return;
}
//throw error and return a 404.
watchdog('onlinepdf', 'No file object found. fid argument: %fid', array('%fid' => $fid),
drupal_set_message (t("No file object found. fid argument: $fid"), 'error');
return drupal_not_found();
}

Preventing access to node/nid paths with .htaccess

With the Drupal module Extended path aliases it is possible to rewrite all node path's including edit & delete paths.

For those that want to prevent access to the regular node/ path, there is a simple Regex Rewriterule with .htaccess possible.

# Redirect all node nid paths
RewriteRule ^node/[0-9]+. http://accessdenied.com [R=301,L]

Take care, 301 is a permanent redirect, cached everywhere. One tiny mistake will take some time to recover from.
302 temporary redirect will make it maybe more fool proof. Just in case...

With the combination of Extended path aliases & this single rewrite line, all your nodes will stay secure and save.

Hide hyperlink from filefield node form (QUICKFIX)

Drupal will show a direct link to the file as soon as the upload is completed in node forms.
Sometimes this is unwanted, for example when a private filefield is desired.

It could be themed with some hooks, but with CSS it only takes 4 lines to hide the link.


.file a {
color: black;
pointer-events: none;
cursor: default; }

This will hide the link in the and it will behave as a normal line of text.
The link can still get discovered in the code, but it makes (private) filefields a little bit more private.