From 1a197900c8983effa98b9a2812365a085fdf4f08 Mon Sep 17 00:00:00 2001 From: Christopher Beckett Date: Fri, 31 Jan 2014 14:21:55 +1100 Subject: [PATCH] Allow the user to pass an optional filepermissions octal parameter to the file consumer, which will be used instead of 0777 on the chmod(). --- lib/Analytics/Consumer/File.php | 6 +++++- test/ConsumerFileTest.php | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Analytics/Consumer/File.php b/lib/Analytics/Consumer/File.php index 5ebb8a7..433bed8 100644 --- a/lib/Analytics/Consumer/File.php +++ b/lib/Analytics/Consumer/File.php @@ -20,7 +20,11 @@ public function __construct($secret, $options = array()) { try { $this->file_handle = fopen($options["filename"], "a"); - chmod($options["filename"], 0777); + if (isset($options["filepermissions"])) { + chmod($options["filename"], $options["filepermissions"]); + } else { + chmod($options["filename"], 0777); + } } catch (Exception $e) { $this->handleError($e->getCode(), $e->getMessage()); } diff --git a/test/ConsumerFileTest.php b/test/ConsumerFileTest.php index b7a7b20..356e875 100644 --- a/test/ConsumerFileTest.php +++ b/test/ConsumerFileTest.php @@ -50,11 +50,30 @@ function testProductionProblems() { $this->assertFalse($tracked); } + function testFileSecurity() { + $client = new Analytics_Client("testsecret", + array("consumer" => "file", + "filename" => $this->filename, + "filepermissions" => 0700 )); + + $tracked = $client->track("some_user", "File PHP Event"); + $this->assertEquals(0700, (fileperms($this->filename) & 0777)); + } + + function testFileSecurityDefaults() { + $client = new Analytics_Client("testsecret", + array("consumer" => "file", + "filename" => $this->filename )); + + $tracked = $client->track("some_user", "File PHP Event"); + $this->assertEquals(0777, (fileperms($this->filename) & 0777)); + } + function checkWritten() { exec("wc -l " . $this->filename, $output); - $this->assertEquals($output[0], "1 " . $this->filename); + $this->assertEquals(trim($output[0]), "1 " . $this->filename); unlink($this->filename); } } -?> \ No newline at end of file +?>