public function execute(HTTPRequestCustom $request)
 {
     AppContext::get_session()->csrf_get_protect();
     $this->get_downloadfile($request);
     $this->check_authorizations();
     DownloadService::delete('WHERE id=:id', array('id' => $this->downloadfile->get_id()));
     DownloadService::get_keywords_manager()->delete_relations($this->downloadfile->get_id());
     PersistenceContext::get_querier()->delete(DB_TABLE_EVENTS, 'WHERE module=:module AND id_in_module=:id', array('module' => 'download', 'id' => $this->downloadfile->get_id()));
     CommentsService::delete_comments_topic_module('download', $this->downloadfile->get_id());
     NotationService::delete_notes_id_in_module('download', $this->downloadfile->get_id());
     Feed::clear_cache('download');
     DownloadCache::invalidate();
     AppContext::get_response()->redirect($request->get_url_referrer() ? $request->get_url_referrer() : DownloadUrlBuilder::home(), StringVars::replace_vars(LangLoader::get_message('download.message.success.delete', 'common', 'download'), array('name' => $this->downloadfile->get_name())));
 }
 public function execute(HTTPRequestCustom $request)
 {
     $id = $request->get_getint('id', 0);
     if (!empty($id)) {
         try {
             $this->downloadfile = DownloadService::get_downloadfile('WHERE download.id = :id', array('id' => $id));
         } catch (RowNotFoundException $e) {
             $error_controller = PHPBoostErrors::unexisting_page();
             DispatchManager::redirect($error_controller);
         }
     }
     if ($this->downloadfile !== null && !DownloadAuthorizationsService::check_authorizations($this->downloadfile->get_id_category())->read()) {
         $error_controller = PHPBoostErrors::user_not_authorized();
         DispatchManager::redirect($error_controller);
     } else {
         if ($this->downloadfile !== null && $this->downloadfile->is_visible()) {
             $this->downloadfile->set_number_downloads($this->downloadfile->get_number_downloads() + 1);
             DownloadService::update_number_downloads($this->downloadfile);
             DownloadCache::invalidate();
             $status = 200;
             $file_headers = get_headers($this->downloadfile->get_url()->absolute(), true);
             if (is_array($file_headers)) {
                 if (preg_match('/^HTTP\\/[12]\\.[01] (\\d\\d\\d)/', $file_headers[0], $matches)) {
                     $status = (int) $matches[1];
                 }
             }
             if ($status == 200) {
                 header('Content-Disposition: attachment; filename="' . urldecode(basename($this->downloadfile->get_url()->absolute())) . '"');
                 header('Content-Description: File Transfer');
                 header('Content-Transfer-Encoding: binary');
                 header('Accept-Ranges: bytes');
                 header('Content-Type: application/force-download');
                 set_time_limit(0);
                 readfile($this->downloadfile->get_url()->absolute());
             } else {
                 $error_controller = new UserErrorController(LangLoader::get_message('error', 'status-messages-common'), LangLoader::get_message('download.message.error.file_not_found', 'common', 'download'), UserErrorController::WARNING);
                 DispatchManager::redirect($error_controller);
             }
         } else {
             $error_controller = PHPBoostErrors::unexisting_page();
             DispatchManager::redirect($error_controller);
         }
     }
 }
 public function get_menu_content()
 {
     //Create file template
     $tpl = new FileTemplate('download/DownloadModuleMiniMenu.tpl');
     //Assign the lang file to the tpl
     $tpl->add_lang(LangLoader::get('common', 'download'));
     //Load module config
     $config = DownloadConfig::load();
     //Load module cache
     $download_cache = DownloadCache::load();
     //Load categories cache
     $categories_cache = DownloadService::get_categories_manager()->get_categories_cache();
     $downloadfiles = $download_cache->get_downloadfiles();
     $tpl->put_all(array('C_FILES' => !empty($downloadfiles), 'C_SORT_BY_DATE' => $config->is_sort_type_date(), 'C_SORT_BY_NOTATION' => $config->is_sort_type_notation(), 'C_SORT_BY_NUMBER_DOWNLOADS' => $config->is_sort_type_number_downloads()));
     $displayed_position = 1;
     foreach ($downloadfiles as $file) {
         $downloadfile = new DownloadFile();
         $downloadfile->set_properties($file);
         $tpl->assign_block_vars('downloadfiles', array_merge($downloadfile->get_array_tpl_vars(), array('DISPLAYED_POSITION' => $displayed_position)));
         $displayed_position++;
     }
     return $tpl->render();
 }
 private function save()
 {
     $downloadfile = $this->get_downloadfile();
     $downloadfile->set_name($this->form->get_value('name'));
     $downloadfile->set_rewrited_name(Url::encode_rewrite($downloadfile->get_name()));
     $downloadfile->set_id_category($this->form->get_value('id_category')->get_raw_value());
     $downloadfile->set_url(new Url($this->form->get_value('url')));
     $downloadfile->set_contents($this->form->get_value('contents'));
     $downloadfile->set_short_contents($this->form->get_value('short_contents_enabled') ? $this->form->get_value('short_contents') : '');
     $downloadfile->set_picture(new Url($this->form->get_value('picture')));
     if ($this->config->is_author_displayed()) {
         $downloadfile->set_author_display_name($this->form->get_value('author_display_name') && $this->form->get_value('author_display_name') !== $downloadfile->get_author_user()->get_display_name() ? $this->form->get_value('author_display_name') : '');
     }
     $file_size = $status = 0;
     $file = new File($downloadfile->get_url()->rel());
     if ($file->exists()) {
         $file_size = $file->get_file_size();
     }
     if (empty($file_size)) {
         $file_headers = get_headers($downloadfile->get_url()->absolute(), true);
         if (is_array($file_headers)) {
             if (isset($file_headers['Content-Length'])) {
                 $file_size = $file_headers['Content-Length'];
             }
             if (preg_match('/^HTTP\\/[12]\\.[01] (\\d\\d\\d)/', $file_headers[0], $matches)) {
                 $status = (int) $matches[1];
             }
         }
     }
     $file_size = $status == 200 && empty($file_size) && $downloadfile->get_size() ? $downloadfile->get_size() : $file_size;
     $downloadfile->set_size($file_size);
     if ($this->get_downloadfile()->get_id() !== null && $downloadfile->get_number_downloads() > 0 && $this->form->get_value('reset_number_downloads')) {
         $downloadfile->set_number_downloads(0);
     }
     if ($this->is_contributor_member()) {
         $downloadfile->set_approbation_type(DownloadFile::NOT_APPROVAL);
         $downloadfile->clean_start_and_end_date();
     } else {
         $downloadfile->set_creation_date($this->form->get_value('creation_date'));
         $downloadfile->set_approbation_type($this->form->get_value('approbation_type')->get_raw_value());
         if ($downloadfile->get_approbation_type() == DownloadFile::APPROVAL_DATE) {
             $downloadfile->set_start_date($this->form->get_value('start_date'));
             if ($this->form->get_value('end_date_enable')) {
                 $downloadfile->set_end_date($this->form->get_value('end_date'));
             } else {
                 $downloadfile->clean_end_date();
             }
         } else {
             $downloadfile->clean_start_and_end_date();
         }
     }
     if ($this->is_new_downloadfile) {
         $id = DownloadService::add($downloadfile);
     } else {
         $downloadfile->set_updated_date(new Date());
         $id = $downloadfile->get_id();
         DownloadService::update($downloadfile);
     }
     $this->contribution_actions($downloadfile, $id);
     DownloadService::get_keywords_manager()->put_relations($id, $this->form->get_value('keywords'));
     Feed::clear_cache('download');
     DownloadCache::invalidate();
 }
 private function save()
 {
     $this->config->set_items_number_per_page($this->form->get_value('items_number_per_page'));
     $this->config->set_categories_number_per_page($this->form->get_value('categories_number_per_page'));
     $this->config->set_columns_number_per_line($this->form->get_value('columns_number_per_line'));
     $this->config->set_category_display_type($this->form->get_value('category_display_type')->get_raw_value());
     if ($this->config->get_category_display_type() != DownloadConfig::DISPLAY_ALL_CONTENT) {
         if ($this->form->get_value('display_descriptions_to_guests')) {
             $this->config->display_descriptions_to_guests();
         } else {
             $this->config->hide_descriptions_to_guests();
         }
     }
     if ($this->form->get_value('author_displayed')) {
         $this->config->display_author();
     } else {
         $this->config->hide_author();
     }
     if ($this->form->get_value('comments_enabled')) {
         $this->config->enable_comments();
     } else {
         $this->config->disable_comments();
     }
     if ($this->form->get_value('notation_enabled')) {
         $this->config->enable_notation();
         $this->config->set_notation_scale($this->form->get_value('notation_scale'));
         if ($this->form->get_value('notation_scale') != $this->config->get_notation_scale()) {
             NotationService::update_notation_scale('download', $this->config->get_notation_scale(), $this->form->get_value('notation_scale'));
         }
     } else {
         $this->config->disable_notation();
     }
     $this->config->set_root_category_description($this->form->get_value('root_category_description'));
     $this->config->set_sort_type($this->form->get_value('sort_type')->get_raw_value());
     $this->config->set_files_number_in_menu($this->form->get_value('files_number_in_menu'));
     $this->config->set_authorizations($this->form->get_value('authorizations')->build_auth_array());
     DownloadConfig::save();
     DownloadService::get_categories_manager()->regenerate_cache();
     DownloadCache::invalidate();
 }