/**
  * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
  * @Request({"id": "int"}, csrf=true)
  */
 public function deleteAction($id)
 {
     if ($project = File::find($id)) {
         if (!App::user()->hasAccess('download: manage downloads')) {
             return ['error' => __('Access denied.')];
         }
         $project->delete();
     }
     return ['message' => 'success'];
 }
 /**
  * {@inheritdoc}
  */
 public function generate(array $parameters = [])
 {
     $id = $parameters['id'];
     if (isset($parameters['category_id'])) {
         unset($parameters['category_id']);
     }
     if (!isset($this->cacheEntries[$id])) {
         if (!($file = File::where(compact('id'))->first())) {
             throw new RouteNotFoundException('File not found.');
         }
         $this->addCache($file);
     }
     $meta = $this->cacheEntries[$id];
     $parameters['slug'] = $meta['slug'];
     unset($parameters['id']);
     return $parameters;
 }
 /**
  * @Route("/{id}", name="id")
  * @Request({"id": "integer", "key": "string", "pkey": "string"})
  * @param integer $id File id
  * @param string $key session key
  * @param string $purchaseKey optional purchase key
  * @return BinaryFileResponse
  */
 public function downloadAction($id, $key, $purchaseKey)
 {
     //todo return proper errors
     if (!($file = File::where(['id = ?', 'status = ?'], [$id, 1])->first())) {
         App::abort(404, __('File not found.'));
     }
     if (!$file->hasAccess(App::user())) {
         App::abort(403, __('Insufficient User Rights.'));
     }
     if (!$this->download->checkDownloadKey($file, $key, $purchaseKey)) {
         App::abort(400, __('Key not valid.'));
     }
     $file->updateDownloadCount();
     // Generate response
     $response = new BinaryFileResponse($file->path);
     $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($file->path), mb_convert_encoding(basename($file->path), 'ASCII')));
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function generate(array $parameters = [])
 {
     $id = $parameters['id'];
     $category_id = isset($parameters['category_id']) ? $parameters['category_id'] : 0;
     if (!isset($this->cacheEntries[$id . '-' . $category_id])) {
         if (!($file = File::find($id))) {
             throw new RouteNotFoundException('File not found.');
         }
         if ($category_id && !($category = Category::find($category_id))) {
             throw new RouteNotFoundException('Category not found.');
         }
         $this->addCache($file, $category_id, isset($category) ? $category->slug : App::config('bixie/download')->get('root_category', 'root'));
     }
     $meta = $this->cacheEntries[$id . '-' . $category_id];
     $parameters['slug'] = $meta['slug'];
     unset($parameters['id']);
     unset($parameters['category_id']);
     return $parameters;
 }
 /**
  * @Route("/{id}", name="id")
  * @Request({"id":"int", "category_id":"int"})
  */
 public function fileAction($id = 0, $category_id = 0)
 {
     /** @var File $file */
     if (!($file = File::where(['id = ?', 'status = ?'], [$id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->first())) {
         App::abort(404, __('File not found.'));
     }
     $file->setActiveCategory($category_id);
     App::trigger('bixie.prepare.file', [$file, App::view()]);
     $file->content = App::content()->applyPlugins($file->content, ['file' => $file, 'markdown' => $file->get('markdown')]);
     $previous = File::getPrevious($file);
     $next = File::getNext($file);
     /** @var Category $category */
     if ($category_id && !($category = Category::where(['id = ?', 'status = ?'], [$category_id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->related('files')->first())) {
         App::abort(404, __('Category not found.'));
     }
     if ($breadcrumbs = App::module('bixie/breadcrumbs')) {
         if ($category_id) {
             $cat = $category;
             $crumbs = [['title' => $category->title, 'url' => $category->getUrl()]];
             while ($parent_id = $cat->parent_id) {
                 if ($cat = $cat->find($parent_id, true)) {
                     $crumbs[] = ['title' => $cat->title, 'url' => $cat->getUrl()];
                 }
             }
             foreach (array_reverse($crumbs) as $data) {
                 $breadcrumbs->addUrl($data);
             }
         }
         //add file
         $breadcrumbs->addUrl(['title' => $file->title, 'url' => $file->getUrl()]);
     }
     return ['$view' => ['title' => __($file->title), 'name' => 'bixie/download/file.php'], 'download' => $this->download, 'config' => $this->download->config(), 'previous' => $previous, 'next' => $next, 'file' => $file, 'node' => App::node()];
 }
 public function onRoleDelete($event, $role)
 {
     File::removeRole($role);
 }
 /**
  * @Access("system: access settings")
  */
 public function settingsAction()
 {
     return ['$view' => ['title' => __('Download Settings'), 'name' => 'bixie/download/admin/settings.php'], '$data' => ['config' => App::module('bixie/download')->config(), 'tags' => File::allTags()]];
 }
 /**
  * @param Order    $order
  * @param CartItem $cartItem
  * @param File     $file
  * @return bool
  */
 protected function validateOrder(Order $order, CartItem $cartItem, $file)
 {
     if ($order->isValid() and $file->status == File::STATUS_PUBLISHED and $file->get('cart_active')) {
         if (!$cartItem->get('validity_period')) {
             return true;
         }
         try {
             $now = new \DateTime();
             $expiry_date = new \DateTime($cartItem->get('valid_until'));
             if ($now < $expiry_date) {
                 return true;
             }
         } catch (\Exception $ignore) {
         }
     }
     return false;
 }
Exemple #9
0
<?php

use Bixie\Download\Model\File;
return ['name' => 'bixie/downloads', 'label' => 'Downloads', 'events' => ['view.scripts' => function ($event, $scripts) use($app) {
    $scripts->register('widget-downloads', 'bixie/download:app/bundle/widget-downloads.js', ['~widgets']);
}], 'render' => function ($widget) use($app) {
    $files = File::where(['status = :status'], ['status' => File::STATUS_PUBLISHED])->get();
    $view = $widget->get('view', 'list');
    return $app['view']('bixie/download/widgets/downloads-' . $view . '.php', compact('widget', 'files'));
}];