Community & Support

Module Not Fixing URL so I can See Uploaded Content

I wrote this module based of the PrivateDownload module. It used to work but suddenly it says "Page not found" instead of displaying the content. If i'm not loged on It says 'Access Denied' so I know at least it's using the permisions but I don't know why it's not fixing the URL any more.

<?php
// $Id$

//-------------------------------------------------------------------
//Name: folder_protector_menu
//Abstract: Tells Drupal What Sites I'm Useing
//-------------------------------------------------------------------
function folder_protector_menu()
{

$items = array();
$VariableValue = "";

$items['admin/settings/folderprotector'] = array(
'title' => 'ProtectFolders',
'description' => 'Protect Folders',
'page callback' => 'drupal_get_form',
'page arguments' => array('folder_protector_admin'),
'access arguments' => array('Protect Folders'),
'type' => MENU_NORMAL_ITEM,
);

//Get A List Of Variables That Hold The Folder Names
$query_result = folder_protector_GetVariables();

//Loop threw Recordset
while ($Items = db_fetch_object($query_result))
{
//The Value is stored with other formating that needs to be striped out
$VariableValue = folder_protector_StripFormating($Items->value);

//$items['files/system/%'] = array(
//'access arguments' => array('access private download directory'),
//'page callback' => 'file_download',
//'page arguments' => array(variable_get('private_download_directory', 'private')),
//'type' => MENU_CALLBACK,

$items['files/system/private/'.$VariableValue.'/%' ] = array(
'access arguments' => array('Can Access The '.$VariableValue.' Folder'),
'page callback' => 'file_download',
'page arguments' => array($VariableValue),
'type' => MENU_CALLBACK
);
}

return $items;
}

//-------------------------------------------------------------------
//Name: folder_protector__file_download
//Abstract: Fix Mime Type
//-------------------------------------------------------------------
function folder_protector__file_download($file)
{
$header = array('Content-Type: '. file_get_mimetype($file));
// add additional file header attributes
return array_merge($header, explode("\n", variable_get('folder_protector_header', "Content-Transfer-Encoding: binary\nCache-Control: max-age=60, must-revalidate")));

}

Comments

In hook_menu(), you define

In hook_menu(), you define the callback function as being called 'file_download', but you named the function folder_protector__file_download(). The function file_download() doesn't exist.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

Thanks for that

Now It's Just displaying the Text Array under the content section and not opening the file

<?php
// $Id$

//-------------------------------------------------------------------
//Name: folder_protector_help
//Abstract: Hooks the help for the module and outputs the help information
//-------------------------------------------------------------------
function folder_protector_help($path, $arg)
{
$output = '';

switch ($path)
{
case "admin/help#folder_protector":
$output = '

'. t("Protect Folders") .'

';
break;
}

return $output;
}

//-------------------------------------------------------------------
//Name: folder_protector_perm
//Abstract: Set Up Permisions
//-------------------------------------------------------------------
function folder_protector_perm()
{
$permisons = array();
$Index = 0;
$Item = "";
$continue = TRUE;

//The First Permision Is The Abitly To Even Manage The Folder Permisions
$permisons[0] = 'Protect Folders';

//Get A List Of Variables That Hold The Folder Names
$query_result = folder_protector_GetVariables();

//Loop threw Recordset
while ($Items = db_fetch_object($query_result))
{
//The Value is stored with other formating that needs to be striped out
$VariableValue = folder_protector_StripFormating($Items->value);

//Adds the Permision leaving room for the first one
$permisons[$Index + 1] = 'Can Access The '.$VariableValue.' Folder';

$Index++;
}

return $permisons;
}

//-------------------------------------------------------------------
//Name: folder_protector_block
//Abstract: Output Content
//-------------------------------------------------------------------
function folder_protector_block($op='list', $delta=0)
{
//If Where Listing The Module
if ($op == "list")
{
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block[0]["info"] = t('Folder Protector');
}

return $block;
}

//-------------------------------------------------------------------
//Name: folder_protector_admin
//Abstract: Create Form To Administrate Upload Folders
//-------------------------------------------------------------------
function folder_protector_admin()
{
$form = array();
$Index = 0;
$Description = "";
$ItemName = "";
$Value = "";

//Create Descrption
$Description = 'Contents of htaccess file in the private download directory. The RewriteBase path must start with "/system/" followed by the !file_system directory name and ending with @@folder@@ which is where the folder name is added. Exp. /system/files/private/@@folder@@';

//Get Variables For htaccess and header values
$htaccess = variable_get('folder_protector_htaccess', "\n RewriteEngine on\n RewriteBase /system/files/private/@@folder@@\n RewriteRule ^(.*)$ $1 [L,R=301]\n");
$header = variable_get('folder_protector_header', "Content-Transfer-Encoding: binary\nCache-Control: max-age=60, must-revalidate");

//Create Text Areas For the htaccess with default values
$form['folder_protector_htaccess'] = folder_protector_CreateAdministrativeTextArea('htaccess content',$htaccess,TRUE,8,$Description);
$form['folder_protector_header'] = folder_protector_CreateAdministrativeTextArea('file headers',$header,TRUE,4,'Enter a list of header attributes, one entry per line.');

//Query Database to get Variables
$query_result = folder_protector_GetVariables();

//Look through Record Set
while ($Items = db_fetch_object($query_result))
{
//Get Values From Recordset
$VariableName = $Items->name;
$VariableValue = folder_protector_StripFormating($Items->value);

//Create Text Feild For The Folder Name
$form['folder_protector_array'.$Index] = folder_protector_CreateAdministrativeTextField('Folder Number '.($Index +1),$VariableValue,20,20,'',FALSE);

$Index++;
}

//Create three more Text Feild's for adding folders
$form['folder_protector_array'.$Index] = folder_protector_CreateAdministrativeTextField('Folder Number '.($Index +1),'',20,20,'',FALSE);
$form['folder_protector_array'.($Index +1)] = folder_protector_CreateAdministrativeTextField('Folder Number '.($Index +2),"",20,20,'',FALSE);
$form['folder_protector_array'.($Index +2)] = folder_protector_CreateAdministrativeTextField('Folder Number '.($Index +3),"",20,20,"To delete a folder's permisions clear all the text",FALSE);

//Return Form
return system_settings_form($form);
}

//-------------------------------------------------------------------
//Name: folder_protector_StripFormating
//Abstract: Removes Everything besides the Value
//-------------------------------------------------------------------
function folder_protector_StripFormating($Value)
{
$FirstQuotePosition = 0;
$SecondQuotePosition = 0;
$FinalValue = $Value;

//Get The Positon of ':' starting on the second charactor
$FirstQuotePosition = strpos($Value, ':', 2) + 2;

//If Didn't found it
if($FirstQuotePosition === False)
{
}
else
{
//Get Position for second ':'
$SecondQuotePosition = strrpos($Value, ';') - 1;

//If you didn't find it
if($SecondQuotePosition === False)
{
}
else
{
//Deterine Length
$Length = $SecondQuotePosition - $FirstQuotePosition;

//Get Final Value
$FinalValue = substr($Value,$FirstQuotePosition, $Length);
}
}

return $FinalValue;
}

//-------------------------------------------------------------------
//Name: folder_protector_CreateAdministrativeTextField
//Abstract: Creates A Control For Administration
//-------------------------------------------------------------------
function folder_protector_CreateAdministrativeTextField($Title,$DefaultValue,$Size,$Maxlength,$Description,$Required)
{
$Control = array
(
'#type' => 'textfield',
'#title' => t($Title),
'#default_value' => $DefaultValue,
'#size' => $Size,
'#maxlength' => $Maxlength,
'#description' => t($Description),
'#required' => $Required
);

return $Control;
}

//-------------------------------------------------------------------
//Name: folder_protector_CreateAdministrativeTextArea
//Abstract: Creates A Control For Administration
//-------------------------------------------------------------------
function folder_protector_CreateAdministrativeTextArea($Title,$DefaultValue,$Required,$Rows,$Description)
{
$Control = array
(
'#type' => 'textarea',
'#title' => t($Title),
'#default_value' => $DefaultValue,
'#required' => $Required,
'#rows' => $Rows,
'#description' => t($Description)
);

return $Control;
}

//-------------------------------------------------------------------
//Name: folder_protector_menu
//Abstract: Tells Drupal What Sites I'm Useing
//-------------------------------------------------------------------
function folder_protector_menu()
{

$items = array();
$VariableValue = "";

$items['admin/settings/folderprotector'] = array(
'title' => 'ProtectFolders',
'description' => 'Protect Folders',
'page callback' => 'drupal_get_form',
'page arguments' => array('folder_protector_admin'),
'access arguments' => array('Protect Folders'),
'type' => MENU_NORMAL_ITEM,
);

//Get A List Of Variables That Hold The Folder Names
$query_result = folder_protector_GetVariables();

//Loop threw Recordset
while ($Items = db_fetch_object($query_result))
{
//The Value is stored with other formating that needs to be striped out
$VariableValue = folder_protector_StripFormating($Items->value);

//$items['files/system/%'] = array(
//'access arguments' => array('access private download directory'),
//'page callback' => 'file_download',
//'page arguments' => array(variable_get('private_download_directory', 'private')),
//'type' => MENU_CALLBACK,

$items['files/system/private/'.$VariableValue.'/%' ] = array(
'access arguments' => array('Can Access The '.$VariableValue.' Folder'),
'type' => MENU_CALLBACK,
'page callback' => 'folder_protector__file_download',
'page arguments' => array($VariableValue)
);
}

return $items;
}

//-------------------------------------------------------------------
//Name: folder_protector__file_download
//Abstract: Fix Mime Type
//-------------------------------------------------------------------
function folder_protector__file_download($file)
{
$header = array('Content-Type: '. file_get_mimetype($file));
// add additional file header attributes
return array_merge($header, explode("\n", variable_get('folder_protector_header', "Content-Transfer-Encoding: binary\nCache-Control: max-age=60, must-revalidate")));
}

//-------------------------------------------------------------------
//Name: folder_protector_admin_validate
//Abstract: Validates User Input
//-------------------------------------------------------------------
function folder_protector_admin_validate($form, &$form_state)
{

}

//-------------------------------------------------------------------
//Name: folder_protector_form_alter
//Abstract: Adds Custom Event Handler
//-------------------------------------------------------------------
function folder_protector_form_alter(&$form, $form_state, $form_id)
{
//test for comment form
if ($form_id == 'folder_protector_admin')
{

// add an additional submit handler
$form['#submit'][0] = 'folder_protector_admin_submit_handler';
}
}

//-------------------------------------------------------------------
//Name: folder_protector_GetVariables
//Abstract: Gets the variables in the database
//-------------------------------------------------------------------
function folder_protector_GetVariables()
{
$query = "";
$Item = "";
$Index = 0;
$Result = array(array());

//Create query
$query = "SELECT name, variable.value FROM {variable} WHERE name like '%protector_array%' ORDER BY name";
//$query = "SELECT name, variable.value FROM {variable} WHERE name LIKE '%multiplefolderpermisionsandupload_array%' ORDER BY name";

//Run Query
$query_result = db_query($query);

//Return recordset
return $query_result;
}

//-------------------------------------------------------------------
//Name: folder_protector_ClearVariables
//Abstract: clears all the variables from the database
//-------------------------------------------------------------------
function folder_protector_ClearVariables()
{
db_query("DELETE FROM {variable} WHERE name LIKE '%protector_array%'");
cache_clear_all('variables','cache');
}

//-------------------------------------------------------------------
//Name: folder_protector_admin_submit_handler
//Abstract: Called When The Form Submits
//-------------------------------------------------------------------
function folder_protector_admin_submit_handler($form, &$form_state)
{
$Index = 0;
$Count = 0;
$FolderValueCount = 0;
$continue = FALSE;
$folderpath = '';
$Oldfolderpath = '';
$htaccessvalue = '';
$header = '';
$Newhtaccessvalue = '';
$OldValue = "";

//Get htaccessvalue From Form
$htaccessvalue = $form_state['values']['folder_protector_htaccess'];
variable_set('folder_protector_htaccess', $htaccessvalue);

$header = $form_state['values']['folder_protector_header'];
variable_set('folder_protector_header', $header);

$query_result = folder_protector_GetVariables();
$Result = folder_protector_FromResultToArray($query_result);

$Count = count($Result);

folder_protector_ClearVariables();

for($Index=0; $Index < $Count + 3; $Index++)
{
$Folder = $form_state['values']['folder_protector_array'.$Index];

folder_protector_DeleteOldAccessFile($Index,$Count,$Folder,$Result);
if($Folder != "")
{

$folderpath = file_directory_path().'/private/'.$Folder.'/.htaccess';

//Replace the placeholder '@@folder@@' with the folder Name
$Newhtaccessvalue = str_replace('@@folder@@',$Folder, $htaccessvalue );

//Try and Write the Access File
if (!folder_protector_write($folderpath, $Newhtaccessvalue))
{
// failed to write htaccess file; report to log and return
watchdog('private_download', t('Unable to write data to file: !folderpath', array('!folderpath' => $folderpath)), 'error');
return FALSE;
}

variable_set('folder_protector_array'.$FolderValueCount,$Folder);

$FolderValueCount++;

// clear menu cache to recognize private download directory
cache_clear_all('*', 'cache_menu', TRUE);
}

}

}

//-------------------------------------------------------------------
//Name: folder_protector_DeleteOldAccessFile
//Abstract: Deletes The Old Access File If Aplicable
//-------------------------------------------------------------------
function folder_protector_DeleteOldAccessFile($Index,$Count,$Folder,$Result)
{
$OldValue = "";

//If there is an old Value
if($Index < $Count)
{
//Get the Old Value
$OldValue = $Result[$Index][1];

if($OldValue != "")
{
//check the new value is "" (it's being deleted) or it's diffrent from
//the old value (it's being updated)
if($Folder == "" || $Folder != $OldValue)
{
//Get the old Access file path
$Oldfolderpath = file_directory_path().'/private/'.$OldValue.'/.htaccess';
//Delete It
unlink($Oldfolderpath);
}

//If Your Deleteing
if($Folder == "")
{
//Set Variable To "" so that folder_protector_ClearEmptyFolderNames()
// will delete It
variable_set('folder_protector_array'.$Index,"");
}
}
}
}

//-------------------------------------------------------------------
//Name: folder_protector_FromResultToArray
//Abstract: Convert The Query Result To An Array
//-------------------------------------------------------------------
function folder_protector_FromResultToArray($query_result)
{
$Result = array(array());
$Index = 0;

//Loop threw recordset
while ($Folders = db_fetch_object($query_result))
{
//Set Values
$Result[$Index][0] = $Folders->name;
$Result[$Index][1] = folder_protector_StripFormating($Folders->value);

//Increment Index
$Index++;
}

//Return Array
return $Result;
}

//-------------------------------------------------------------------
//Name: folder_protector_write
//Abstract: Writes out the HTAccess File
//-------------------------------------------------------------------
function folder_protector_write($folderpath, $content)
{
// write data to file; create file if not present
if ($handle = fopen($folderpath, 'w+b'))
{
if (fwrite($handle, $content))
{
fclose($handle);
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}

return TRUE;
}

Mate, that's way too much

Mate, that's way too much code to go through. Cut it down to the necessary parts.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

Ok

I only did that so that I don't not incude where the problem is.

/-------------------------------------------------------------------
//Name: folder_protector_menu
//Abstract: Tells Drupal What Sites I'm Useing
//-------------------------------------------------------------------
function folder_protector_menu()
{

$items = array();
$VariableValue = "";

$items['admin/settings/folderprotector'] = array(
'title' => 'ProtectFolders',
'description' => 'Protect Folders',
'page callback' => 'drupal_get_form',
'page arguments' => array('folder_protector_admin'),
'access arguments' => array('Protect Folders'),
'type' => MENU_NORMAL_ITEM,
);

//Get A List Of Variables That Hold The Folder Names
$query_result = folder_protector_GetVariables();

//Loop threw Recordset
while ($Items = db_fetch_object($query_result))
{
//The Value is stored with other formating that needs to be striped out
$VariableValue = folder_protector_StripFormating($Items->value);

//$items['files/system/%'] = array(
//'access arguments' => array('access private download directory'),
//'page callback' => 'file_download',
//'page arguments' => array(variable_get('private_download_directory', 'private')),
//'type' => MENU_CALLBACK,

$items['files/system/private/'.$VariableValue.'/%' ] = array(
'access arguments' => array('Can Access The '.$VariableValue.' Folder'),
'type' => MENU_CALLBACK,
'page callback' => 'folder_protector_file_download',
'page arguments' => array($VariableValue)
);
}

return $items;
}

//-------------------------------------------------------------------
//Name: folder_protector_file_download
//Abstract: Fix Mime Type
//-------------------------------------------------------------------
function folder_protector_file_download($file)
{
$header = array('Content-Type: '. file_get_mimetype($file));
// add additional file header attributes
return array_merge($header, explode("\n", variable_get('folder_protector_header', "Content-Transfer-Encoding: binary\nCache-Control: max-age=60, must-revalidate")));
}

So what are you trying to do,

So what are you trying to do, and what's the problem?

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

Problem

It used to show the file when you clicked on a file field link to something in the private directory. Not it just says "Page not found
The requested page could not be found." instead.

Can't be the problem

'file_download' is what you need to call on postback so it downloads the file, it's built into drupal. folder_protector__file_download() is a hook I don't need it there.

I don't know why it wouldn't but it dosen't.

nobody click here