display[]='download'; if(!$skip){ parent::__construct($config,$skip); $this->registerDefaultTask('download'); } } function download() { $app =& JFactory::getApplication(); $downloadid = JRequest::getString('downloadid'); if(!empty($downloadid)) { $database =& JFactory::getDBO(); $dbtable = hikashop_table('downloadids'); // Search the database for the given download id $query="SELECT * FROM $dbtable WHERE download_id='$downloadid'"; $database->setQuery($query); $database->query(); $row = $database->loadObject(); if (!empty($row)) { $fileClass = hikashop_get('class.file'); $fileid = $row->file_id; $orderid = $row->order_id; $file = $fileClass->get($fileid); $orderClass = hikashop_get('class.order'); $order = $orderClass->get($orderid); $file->order = $order; if(empty($order)) { $app->enqueueMessage(JText::_('ORDER_NOT_FOUND')); $this->error_type = 'no_order'; parent::show(); return false; } $config =& hikashop_config(); $order_status_for_download = $config->get('order_status_for_download','confirmed,shipped'); if(!in_array($order->order_status,explode(',',$order_status_for_download))){ $app->enqueueMessage(JText::_('BECAUSE_STATUS_NO_DOWNLOAD')); $this->error_type = 'status'; parent::show(); return false; } $download_time_limit = $config->get('download_time_limit',0); if(!empty($download_time_limit) && ($download_time_limit+$order->order_created)enqueueMessage(JText::_('TOO_LATE_NO_DOWNLOAD')); $this->error_type = 'date'; parent::show(); return false; } $query = 'SELECT a.* FROM '.hikashop_table('order_product').' AS a WHERE a.order_id = '.$orderid; $database->setQuery($query); $order->products = $database->loadObjectList(); $product_ids = array(); foreach($order->products as $product){ $products_ids[]=$product->product_id; } $query = 'SELECT * FROM '.hikashop_table('product').' WHERE product_id IN ('.implode(',',$products_ids).') AND product_type=\'variant\''; $database->setQuery($query); $products = $database->loadObjectList(); if(!empty($products)){ foreach($products as $product){ foreach($order->products as $item){ if($product->product_id == $item->product_id && !empty($product->product_parent_id)){ $item->product_parent_id = $product->product_parent_id; $products_ids[]=$product->product_parent_id; } } } } $filters = array('a.file_ref_id IN ('.implode(',',$products_ids).')','a.file_type=\'file\'','a.file_id='.$fileid); $query = 'SELECT a.*,b.* FROM '.hikashop_table('file').' AS a LEFT JOIN '.hikashop_table('download').' AS b ON b.order_id='.$order->order_id.' AND a.file_id = b.file_id WHERE '.implode(' AND ',$filters); $database->setQuery($query); $fileData = $database->loadObject(); if(!empty($fileData)){ if(!empty($file->file_limit) && (int)$file->file_limit != 0) $download_number_limit = (int)$file->file_limit; else $download_number_limit = $config->get('download_number_limit',0); if($download_number_limit < 0) $download_number_limit = 0; if(!empty($download_number_limit) && $download_number_limit<=$fileData->download_number){ $app->enqueueMessage(JText::_('MAX_REACHED_NO_DOWNLOAD')); $this->error_type = 'limit'; // Not sure if this is necessary // parent::show(); return false; } }else{ $app->enqueueMessage(JText::_('FILE_NOT_FOUND').' 1'); $this->error_type = 'no_file'; parent::show(); return false; } if(!empty($file)){ $path = $fileClass->getPath('file'); if(substr($file->file_path,0,7) == 'http://' || substr($file->file_path,0,1) == '@' || file_exists($path.$file->file_path) || file_exists($file->file_path) ){ if(!$app->isAdmin()){ if(!empty($file->file_free_download)){ $orderid='0'; } $query = 'SELECT * FROM '.hikashop_table('download').' WHERE file_id='.$file->file_id.' AND order_id='.$orderid; $database->setQuery($query); $download = $database->loadObject(); if(empty($download)){ $query = 'INSERT INTO '.hikashop_table('download').'(file_id,order_id,download_number) VALUES('.$file->file_id.','.$orderid.',1);'; }else{ $query = 'UPDATE '.hikashop_table('download').' SET download_number=download_number+1 WHERE file_id='.$file->file_id.' AND order_id='.$orderid; } $database->setQuery($query); $database->query(); } $fileClass->sendFile($file, true, $path); } } else { $app->enqueueMessage(JText::_('FILE_NOT_FOUND').' 2'); } } else { $app->enqueueMessage(JText::_('FILE_NOT_FOUND').' 3'); } } else { $app->enqueueMessage(JText::_('FILE_NOT_FOUND').' 4'); parent::show(); return false; } return true; } }