Skip to content

Instantly share code, notes, and snippets.

@steamraven
Created June 7, 2011 15:31
Show Gist options
  • Save steamraven/1012495 to your computer and use it in GitHub Desktop.
Save steamraven/1012495 to your computer and use it in GitHub Desktop.
FileZ file logging
76c5
Author: matthewh <matthewh@donaanacounty.org>
Date: Mon Jun 6 16:15:46 2011 -0600
Add File Logging
diff --git a/app/controllers/File.php b/app/controllers/File.php
index 6663520..0b2375e 100644
--- a/app/controllers/File.php
+++ b/app/controllers/File.php
@@ -37,7 +37,7 @@ class App_Controller_File extends Fz_Controller {
$file->download_count = $file->download_count + 1;
$file->save ();
-
+ $this->logFile($file, "DOWNLOAD");
return $this->sendFile ($file);
}
@@ -51,7 +51,7 @@ class App_Controller_File extends Fz_Controller {
$file->download_count = $file->download_count + 1;
$file->save ();
-
+ $this->logFile($file, "DOWNLOAD");
return $this->sendFile ($file, $file->isImage () ? false : true);
}
diff --git a/app/controllers/Upload.php b/app/controllers/Upload.php
index 6f1eec1..3ed90ca 100644
--- a/app/controllers/Upload.php
+++ b/app/controllers/Upload.php
@@ -135,6 +135,7 @@ class App_Controller_Upload extends Fz_Controller {
if ($file->moveUploadedFile ($uploadedFile)) {
fz_log ('Saved "'.$file->file_name.'"['.$file->id.'] uploaded by '.$user);
+ $this->logFile($file, "UPLOAD");
return $file;
}
else {
diff --git a/app/models/DbTable/FileLog.php b/app/models/DbTable/FileLog.php
new file mode 100644
index 0000000..c1ab13e
--- /dev/null
+++ b/app/models/DbTable/FileLog.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * @app/models/DbTable/FileLog.php
+ * table for log of files
+ *
+ * Long description.
+ *
+ * @package FileZ
+ */
+
+
+class App_Model_DbTable_FileLog extends Fz_Db_Table_Abstract {
+ protected $_rowClass = 'App_Model_FileLogEntry';
+ protected $_name = 'fz_file_log';
+ protected $_columns = array (
+ 'id',
+ 'file_id',
+ 'logged_at',
+ 'ip',
+ 'log_type',
+ 'uid',
+ 'guest_id',
+ 'file_name',
+ 'file_size',
+ 'file_user',
+ 'invite_user',
+ 'invite_email',
+ 'forward_ips',
+ );
+};
diff --git a/app/models/FileLogEntry.php b/app/models/FileLogEntry.php
new file mode 100644
index 0000000..de5f52f
--- /dev/null
+++ b/app/models/FileLogEntry.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * @app/models/FileLogEntry
+ * Entry in the file log
+ *
+ * Long description.
+ *
+ * @package FileZ
+ */
+
+/*
+ * @property int $id
+ * @property int $file_id App_Model_File
+ * @property string $logged_at DATE
+ * @property string $ip
+ * @property string $log_type
+ * @property string $uid App_Model_User
+ * @property itn $guest_id
+ */
+
+class App_Model_FileLogEntry extends Fz_Db_Table_Row_Abstract {
+ protected $_tableClass = 'App_Model_DbTable_FileLog';
+
+ /**
+ * Constructor
+ *
+ * @param boolean $exists Whether the object exists in database or not.
+ * If false, most info is automatically populated.
+ */
+ public function __construct ($exists = false) {
+ parent::__construct ($exists);
+ if (! $exists) {
+ $this->ip = $_SERVER['REMOTE_ADDR'];
+ if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER))
+ $this->forward_ips = $_SERVER['HTTP_X_FORWARDED_FOR'];
+ if (array_key_exists('uid', $_SESSION))
+ $this->uid = $_SESSION['uid'];
+ if (array_key_exists('guest_id', $_SESSION)) {
+ $this->guest_id = $_SESSION['guest_id'];
+ $invite = z_Db::getTable('Invite')->findById ($this->guest_id);
+ if ($invite !== null) {
+ $this->invite_email = $invite->email;
+ $this->invite_user = $invite->created_by;
+
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Return the logged at date
+ * @return Zend_Date
+ */
+ public function getLoggedAt () {
+ return new Zend_Date ($this->logged_at, Zend_Date::ISO_8601);
+ }
+
+ /**
+ * Return logged file
+ * @return App_Model_File
+ */
+
+ public function getFile() {
+ return Fz_Db::getTable('File')->findById ($this->file_id);
+ }
+
+ /**
+ * Set logged file
+ * @param App_Model_File $file File to log
+ */
+
+ public function setFile(App_Model_File $file) {
+ $this->file_id = $file->id;
+ $this->file_name = $file->file_name;
+ $this->file_size = $file->file_size;
+ $this->file_user = $file->created_by;
+ }
+
+ /**
+ * Return the logged user if there is one
+ * @return App_Model_User
+ */
+
+ public function getUser() {
+ if ($this->uid !== null) {
+ return Fz_Db::getTable('User')->findUsername ($this->uid);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Return the logged guest id if there is one
+ * @return int
+ */
+
+ public function getGuest() {
+ return $this->guest_id;
+ }
+};
+
+
+
diff --git a/lib/Fz/Controller.php b/lib/Fz/Controller.php
index 87804d9..86cecc8 100644
--- a/lib/Fz/Controller.php
+++ b/lib/Fz/Controller.php
@@ -136,6 +136,18 @@ class Fz_Controller {
protected function goBack () {
redirect ($_SERVER["HTTP_REFERER"]);
}
+
+ protected function logFile(App_Model_File $file, $log_type)
+ {
+ try {
+ $log = new App_Model_FileLogEntry ();
+ $log->setFile($file);
+ $log->setLogType($log_type);
+ $log->save();
+ } catch (Exception $e) {
+ fz_log("Error with log" . (string) $e, FZ_LOG_ERROR);
+ }
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment