diff options
author | Florian Pritz <bluewind@xinu.at> | 2013-07-13 22:30:54 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2013-07-13 22:58:07 +0200 |
commit | cabec2e1c8e468279f17bee9fb87f1676e8cec9a (patch) | |
tree | 9caf931dbf8edd67470a778493ac2740049c3c90 | |
parent | 2015e32e3bd6922d0697a875ecdc810243c887f0 (diff) |
Add profile page to allow changing the upload id limits
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | application/config/migration.php | 2 | ||||
-rw-r--r-- | application/controllers/file.php | 6 | ||||
-rw-r--r-- | application/controllers/user.php | 59 | ||||
-rw-r--r-- | application/migrations/008_add_profiles.php | 31 | ||||
-rw-r--r-- | application/models/muser.php | 59 | ||||
-rw-r--r-- | application/views/user/profile.php | 33 |
6 files changed, 187 insertions, 3 deletions
diff --git a/application/config/migration.php b/application/config/migration.php index d083e7eb8..943ff0880 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = true; | be upgraded / downgraded to. | */ -$config['migration_version'] = 7; +$config['migration_version'] = 8; /* diff --git a/application/controllers/file.php b/application/controllers/file.php index 01836258a..fcb8717c5 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -542,7 +542,8 @@ class File extends CI_Controller { return; } - $id = $this->mfile->new_id(); + $limits = $this->muser->get_upload_id_limits(); + $id = $this->mfile->new_id($limits[0], $limits[1]); $hash = md5($content); $folder = $this->mfile->folder($hash); @@ -612,7 +613,8 @@ class File extends CI_Controller { } foreach ($files as $key => $file) { - $id = $this->mfile->new_id(); + $limits = $this->muser->get_upload_id_limits(); + $id = $this->mfile->new_id($limits[0], $limits[1]); $hash = md5_file($file['tmp_name']); // work around a curl bug and allow the client to send the real filename base64 encoded diff --git a/application/controllers/user.php b/application/controllers/user.php index 79e54e84a..abbb846a3 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -317,6 +317,65 @@ class User extends CI_Controller { $this->load->view('footer', $this->data); } + function profile() + { + $this->muser->require_access(); + + $this->data["profile_data"] = $this->muser->get_profile_data(); + + $this->load->view('header', $this->data); + $this->load->view($this->var->view_dir.'profile', $this->data); + $this->load->view('footer', $this->data); + } + + function save_profile() + { + $this->muser->require_access(); + + /* + * Key = name of the form field + * Value = function that sanatizes the value and returns it + * TODO: some kind of error handling that doesn't loose correctly filled out fields + */ + $value_processor = array(); + + $value_processor["upload_id_limits"] = function($value) { + $values = explode("-", $value); + + if (!is_array($values) || count($values) != 2) { + show_error("Invalid upload id limit value"); + } + + $lower = intval($values[0]); + $upper = intval($values[1]); + + if ($lower > $upper) { + show_error("lower limit > upper limit"); + } + + if ($lower < 3 || $upper > 64) { + show_error("upper or lower limit out of bounds (3-64)"); + } + + return $lower."-".$upper; + }; + + $data = array(); + foreach (array_keys($value_processor) as $field) { + $value = $this->input->post($field); + + if ($value !== false) { + $data[$field] = $value_processor[$field]($value); + } + } + + if (!empty($data)) { + $this->muser->update_profile($data); + } + + redirect("user/profile"); + } + function logout() { $this->muser->logout(); diff --git a/application/migrations/008_add_profiles.php b/application/migrations/008_add_profiles.php new file mode 100644 index 000000000..3fea33c08 --- /dev/null +++ b/application/migrations/008_add_profiles.php @@ -0,0 +1,31 @@ +<?php +defined('BASEPATH') OR exit('No direct script access allowed'); + +class Migration_Add_profiles extends CI_Migration { + + public function up() + { + $this->db->query(" + CREATE TABLE `profiles` ( + `user` int(8) unsigned NOT NULL, + `upload_id_limits` varchar(255) COLLATE utf8_bin NOT NULL, + PRIMARY KEY (`user`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin + "); + + $this->db->query(" + ALTER TABLE `files` CHANGE `id` `id` VARCHAR( 255 ); + "); + + } + + public function down() + { + $this->db->query(" + DROP TABLE `profiles`; + "); + $this->db->query(" + ALTER TABLE `files` CHANGE `id` `id` VARCHAR( 6 ); + "); + } +} diff --git a/application/models/muser.php b/application/models/muser.php index 947b87d97..f7da8c1fd 100644 --- a/application/models/muser.php +++ b/application/models/muser.php @@ -8,6 +8,9 @@ */ class Muser extends CI_Model { + + private $default_upload_id_limits = "3-6"; + function __construct() { parent::__construct(); @@ -157,6 +160,62 @@ class Muser extends CI_Model { return $query; } + public function get_profile_data() + { + $userid = $this->get_userid(); + + $fields = array( + "user" => $userid, + "upload_id_limits" => $this->default_upload_id_limits, + ); + + $query = $this->db->query(" + SELECT ".implode(", ", array_keys($fields))." + FROM `profiles` + WHERE user = ? + ", array($userid))->row_array(); + + $extra_fields = array( + "username" => $this->get_username(), + "email" => $this->get_email($userid), + ); + + return array_merge($fields, $query, $extra_fields); + } + + public function update_profile($data) + { + assert(is_array($data)); + + $data["user"] = $this->get_userid(); + + $exists_in_db = $this->db->get_where("profiles", array("user" => $data["user"]))->num_rows() > 0; + + if ($exists_in_db) { + $this->db->where("user", $data["user"]); + $this->db->update("profiles", $data); + } else { + $this->db->insert("profiles", $data); + } + } + + public function get_upload_id_limits() + { + $userid = $this->get_userid(); + + $query = $this->db->query(" + SELECT upload_id_limits + FROM `profiles` + WHERE user = ? + ", array($userid))->row_array(); + + if (empty($query)) { + return $this->default_upload_id_limits; + } + + return explode("-", $query["upload_id_limits"]); + } + function hash_password($password) { diff --git a/application/views/user/profile.php b/application/views/user/profile.php new file mode 100644 index 000000000..d382883d8 --- /dev/null +++ b/application/views/user/profile.php @@ -0,0 +1,33 @@ +<?php echo form_open("user/save_profile", array("class" => "form-horizontal")); ?> + + <div class="control-group"> + <label class="control-label" for="inputUsername">Username</label> + <div class="controls"> + <input type="text" id="inputUsername" name="username" placeholder="Username" disabled="disabled" value="<?php echo $profile_data["username"]; ?>"> + </div> + </div> + + <?php if(auth_driver_function_implemented("get_email")) { ?> + <div class="control-group"> + <label class="control-label" for="inputEmail">Email</label> + <div class="controls"> + <input type="text" id="inputEmail" name="email" placeholder="Email" disabled="disabled" value="<?php echo $profile_data["email"]; ?>"> + </div> + </div> + <?php } ?> + + <div class="control-group"> + <label class="control-label" for="inputUploadIDLimits">Upload ID length limits</label> + <div class="controls"> + <input type="text" id="inputUploadIDLimits" name="upload_id_limits" placeholder="number-number" value="<?php echo $profile_data["upload_id_limits"]; ?>"> + <span class="help-block"><number>-<number> (min: 3, max: 64; example: "5-9")</span> + </div> + </div> + + <div class="control-group"> + <div class="controls"> + <button type="submit" class="btn" name="process">Update</button> + </div> + </div> + +</form> |