summaryrefslogtreecommitdiffstats
path: root/application/service/files.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2014-10-25 13:55:08 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-01-16 17:38:38 +0100
commitd59962443687127ea1defc2f8ac41af1c2c02fe4 (patch)
treef33a9f358efdd1e30a27e0244314d0ef81b2ce7e /application/service/files.php
parent9535ede862e01d834ebdd553184b1f6544b06d2c (diff)
first go at reworking; needs to be redesigned
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/service/files.php')
-rw-r--r--application/service/files.php116
1 files changed, 116 insertions, 0 deletions
diff --git a/application/service/files.php b/application/service/files.php
new file mode 100644
index 000000000..68072e95a
--- /dev/null
+++ b/application/service/files.php
@@ -0,0 +1,116 @@
+<?php
+/*
+ * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+namespace service;
+
+class files {
+
+ static public function history($user)
+ {
+ $CI =& get_instance();
+ $query = array();
+
+ $fields = array("id", "filename", "mimetype", "date", "hash", "filesize");
+
+ $items = $CI->db->select(implode(',', $fields))
+ ->from('files')
+ ->where('user', $user)
+ ->get()->result_array();
+
+ // TODO: split this and provide an array of pastes for a multipaste?
+ $query = $CI->db->query("
+ SELECT m.url_id id, sum(f.filesize) filesize, m.date, '' hash, '' mimetype, concat(count(*), ' file(s)') filename
+ FROM multipaste m
+ JOIN multipaste_file_map mfm ON m.multipaste_id = mfm.multipaste_id
+ JOIN files f ON f.id = mfm.file_url_id
+ WHERE m.user_id = ?
+ GROUP BY m.url_id
+ ", array($user))->result_array();
+
+ $items = array_merge($items, $query);
+
+ $total_size = $CI->db->query("
+ SELECT sum(filesize) sum
+ FROM (
+ SELECT DISTINCT hash, filesize
+ FROM files
+ WHERE user = ?
+ ) sub
+ ", array($user))->row_array();
+
+ $ret["items"] = $items;
+ $ret["total_size"] = $total_size["sum"];
+
+ return $ret;
+ }
+
+ static public function add_file($id, $file, $filename)
+ {
+ $CI =& get_instance();
+ $hash = md5_file($file);
+
+ $dir = $CI->mfile->folder($hash);
+ file_exists($dir) || mkdir ($dir);
+ $new_path = $CI->mfile->file($hash);
+
+ // TODO: make this operation atomic (move to temp name, then to final)
+ // the source can be a different file system so this might do a copy
+ move_uploaded_file($file, $new_path);
+ $CI->mfile->add_file($hash, $id, $filename);
+ }
+
+ static public function verify_uploaded_files($files)
+ {
+ $CI =& get_instance();
+ $errors = array();
+
+ foreach ($files as $key => $file) {
+ $error_message = "";
+
+ // getNormalizedFILES() removes any file with error == 4
+ if ($file['error'] !== UPLOAD_ERR_OK) {
+ // ERR_OK only for completeness, condition above ignores it
+ $error_msgs = array(
+ UPLOAD_ERR_OK => "There is no error, the file uploaded with success",
+ UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
+ UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
+ UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded",
+ UPLOAD_ERR_NO_FILE => "No file was uploaded",
+ UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder",
+ UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk",
+ UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload",
+ );
+
+ $error_message = "Unknown error.";
+
+ if (isset($error_msgs[$file['error']])) {
+ $error_message = $error_msgs[$file['error']];
+ } else {
+ $error_message = "Unknown error code: ".$file['error'].". Please report a bug.";
+ }
+
+ }
+
+ $filesize = filesize($file['tmp_name']);
+ if ($filesize > $CI->config->item('upload_max_size')) {
+ $error_message = "File too big";
+ }
+
+ if ($error_message != "") {
+ $errors[] = array(
+ "filename" => $file["name"],
+ "formfield" => $file["formfield"],
+ "message" => $error_message,
+ );
+ }
+ }
+
+ return $errors;
+ }
+}