Re: Re: Re: Re: Re: File downloads stopped working for orders by

Posts: 1311
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Well it's hook_order() that allows uesrs to access a file.

<?php
function uc_file_order($op, $order, $status) {
  global
$user;
 
  switch (
$op) {
    case
'update':
      if ((
$order->uid > 0) && ($order_user = user_load(array('uid' => $order->uid))) !== FALSE) {
        foreach (
$order->products as $product) {
         
$files = db_query("SELECT fp.fid, fp.pfid, model, f.filename FROM {uc_file_products} AS fp INNER JOIN {uc_product_features} AS pf ON pf.pfid = fp.pfid INNER JOIN {uc_files} as f ON f.fid = fp.fid WHERE nid = %d",$product->nid);
          while (
$file = db_fetch_object($files)) {
            if ((
$file->model == $product->model || empty($file->model)) && $status == variable_get('uc_file_default_order_status','completed')) {
             
$downloads = _user_table_action('allow',$file->fid,$order_user->uid,$file->pfid);
             
$user_downloads = (!empty($user_downloads)) ? array_merge($user_downloads,$downloads) : $downloads ;
             
$comment = (_get_dir_file_ids($file->fid)) ? t('User can now download files in the directory %dir', array('%dir' => $file->filename)) : t('User can now download the file %file', array('%file' => basename($file->filename)));
             
uc_order_comment_save($order->order_id, $user->uid, $comment);
            }
          }
        }
        if (!
is_null($user_downloads)) {
         
_email_file_download($order_user,$order,$user_downloads);
        }
      }
      break;
    default:
      break;
  }
}
?>

So you can see the line that checks the $order->uid element and then performs a user_load on the same uid, and if the result returns TRUE, it performs

<?php
_user_table_action
('allow',$file->fid,$order_user->uid,$file->pfid);
?>

Here's what that function looks like:

<?php
function _user_table_action($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL) {

  switch (
$op) {
    case
'allow': //arg1 = file id, arg2 = user id, $arg3 = pfid
      //@return file_user objects inserted into table
     
if (!is_null($arg1) && !is_null($arg2)) {
       
$output = array();
       
$granted = time();
       
$fids = (_get_dir_file_ids($arg1)) ? _get_dir_file_ids($arg1) : array($arg1);
        foreach (
$fids as $fid) {
         
$values = array($arg1,$arg2,$arg3,"",$granted,0,serialize(array()));
         
$hash = _generate_hash($values);
         
db_query("INSERT INTO {uc_file_users} (fid, uid, pfid, `key`, granted, accessed, addresses) VALUES (%d, %d, %d, '%s', %d, %d, '%s')",$fid,$arg2,$arg3,$hash,$granted,0,serialize(array()));
         
$output[] = db_fetch_object(db_query("SELECT * FROM {uc_file_users} WHERE uid = %d AND `key` = '%s'",$arg2,$hash));
        }
      }
      return (!
is_null($output)) ? $output : FALSE;
      break;
?>
(I cut the rest of the function which isn't needed in this case).

So I guess the question to the uberdudes is, when does the uid get created? My guess is that perhaps the uc_file.module functions are getting called too early? Or there is some other issue that is not passing the $order->uid element correctly for anonymous orders.

I have yet to test anything for you but you might want to add some debug code to track the uid, before and after uc_file's hook_order().

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

File downloads stopped working for orders by anonymous users By: stephthegeek@drupal.org (33 replies) Fri, 03/21/2008 - 18:32