Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Tracking downloads with secure files module


Go to End


5 Posts   1835 Views

Avatar
MarijnKampf

Community Member, 176 Posts

23 December 2010 at 4:52am

I'm trying to track which files are downloaded by particular users. In my code I extended the function onAccessGranted() from the Secure Files Module. However, with some files I'm getting a File not found error. Live HTTP Headers shows:

HTTP/1.1 500 Notice: "ob_flush() [<a href='ref.outcontrol'>ref.outcontrol</a>]: failed to flush buffer. No buffer to flush." at line 170 of G:\localhost\4ddynamics.eu\securefiles\code\SecureFileController.php

Below the code I'm using. Is it an error in the SecureFileController.php or is it my code?

TrackDownload.php

<?php
class TrackDownload extends DataObjectDecorator {
  function extraStatics() {
		return array(
			'many_many' => array(
				'Members' => 'Member' // Downloaders
			)
		);
    // Return an array containing keys 'db', 'has_one', 'many_many', 'belongs_many_many',
  }

	public function onAccessGranted() {
		$member = Member::currentUser();
		$this->owner->Members()->add($member);
		$this->owner->Members()->write();
		//Debug::Show("Access Granted");
	}

	public function onAccessDenied() {
//		Debug::Show("Access Denied");
//		exit();
	}

	public function summaryFields() {
		return array(
			'Members' => 'User'
		);
	}

	public function getCMSFields() {
		$fields = parent::getCMSFields();
   	$this->extend('updateCMSFields', $fields);
   	return $fields;
	}

	public function updateCMSFields(FieldSet $fields) {
	// DataObject::get("Product", "", "Title ASC")->toDropdownMap()
//		$fields->addFieldToTab("Root.Downloaders", new CheckboxSetField('Members','Downloaders', $this->Members));
	}
}

User.php

<?php
class User extends DataObjectDecorator {
  function extraStatics() {
		return array(
			'db' => array(
				'Company' => 'Varchar(255)',
				'Country' => 'Varchar(255)',
				'Marketing' => 'Boolean',
			),
			'many_many' => array(
				'Products' => 'Product',
			),
			'belongs_many_many' => array(
				'Downloaded' => 'File'
			)
		);
    // Return an array containing keys 'db', 'has_one', 'many_many', 'belongs_many_many',
  }

  function somethingElse() {
    // You can add any other methods you like, which you can call directly on the member object.
  }

	public function getCMSFields() {
		$fields = parent::getCMSFields();
   	$this->extend('updateCMSFields', $fields);
   	return $fields;
	}

	public function updateCMSFields(FieldSet $fields) {
    $fields->addFieldToTab("Root.Main", new TextField('Company', 'Company'), "MemberUserDetailsHeader");
    $fields->addFieldToTab("Root.Main", new TextField('Country', 'Country'), "MemberUserDetailsHeader");

		$fields->addFieldToTab("Root.Products", new CheckboxSetField('Products','Interested in products (check all that apply)', DataObject::get("Product", "", "Title ASC")->toDropdownMap()));
	}
}

_config.php

DataObject::add_extension('Member', 'User');
DataObject::add_extension('File', 'TrackDownload');

Avatar
MarijnKampf

Community Member, 176 Posts

23 December 2010 at 4:55am

Removing the line 170: ob_flush(); from SecureFileController.php seems to solve this issue.

Avatar
MarijnKampf

Community Member, 176 Posts

24 December 2010 at 7:58am

Another issue I came across is using the above code I get a 'ugly' HTTP Error 403 (Forbidden) error when users are logged into the system, but do not have group permission to access a file. (Using group permissions). If they aren't logged in they get a 'pretty' error with login form.

Avatar
Hamish

Community Member, 712 Posts

24 December 2010 at 9:21am

With ob_flush() it should be safe to suppress errors (@)... not quite sure exactly what is causing the error - usually other server modules are doing something else with the output buffer so they might not work 'as expected' but can still be safely called.

Could you log the issues you've had at http://redmine.polemic.net.nz/projects/secure-files and I can take a look when I'm back from holiday :)

Avatar
MarijnKampf

Community Member, 176 Posts

6 January 2011 at 10:23pm

Requested an account on your Redmine site, please ignore the second account as I didn't notice that the account had to be manually approved first.