/** * undocumented function * * @param string $path * @return void * @author Andy Bennett */ protected function render($path, $download = false, $orig_name = null) { Kohana::close_buffers(false); if (is_null($orig_name)) { $orig_name = basename($path); } $file_type = uploads::check_filetype(file::mime($path), $path); header('Content-type: ' . $file_type); if (!file::is_image($file_type) or $download) { header('Content-Disposition: attachment; filename="' . $orig_name . '"'); } header("Content-Length: " . filesize($path)); readfile($path); exit; }
public function contentType() { switch ($this->get('type')) { case 'wav': return 'audio/x-wav'; case 'ogg': return 'audio/ogg'; case 'mp1': case 'mp3': return 'audio/mpeg'; default: return file::mime($this->filepath(TRUE)); } }
public function rule_allow() { if (empty($this->upload['tmp_name']) or count($types = func_get_args()) == 0) { return; } if (($mime = file::mime($this->upload['tmp_name'])) === FALSE) { // Trust the browser $mime = $this->upload['type']; } // Allow nothing by default $allow = FALSE; foreach ($types as $type) { // Load the mime types $type = Kohana::config('mimes.' . $type); if (is_array($type) and in_array($mime, $type)) { // Type is valid $allow = TRUE; break; } } if ($allow === FALSE) { $this->errors['invalid_type'] = TRUE; } }
public function rule_allow() { if (empty($this->upload['tmp_name']) or count($types = func_get_args()) == 0) { return; } if (($mime = file::mime($this->upload['tmp_name'])) === NO) { // Trust the browser $mime = $this->upload['type']; } // Clean up charset garbage $mime = arr::get(explode(';', $mime), 0); // Allow nothing by default $allow = NO; foreach ($types as $type) { // Load the mime types $type = Eight::config('mimes.' . $type); if (is_array($type) and in_array($mime, $type)) { // Type is valid $allow = YES; break; } } if ($allow === NO) { $this->errors['invalid_type'] = YES; } }
public static function rows($item) { // print Kohana::debug($item); $output = ''; foreach ($item->as_array() as $column_name => $column_value) { if ($column_name === 'id') { continue; } $output .= '<li class="row">'; $output .= form::label($column_name, self::label($item, $column_name)); if (empty($item->table_columns[$column_name]['null'])) { $required = 'required'; } else { $required = NULL; } // print_r($item->table_columns[$column_name]); switch ($item->table_columns[$column_name]['type']) { case 'boolean': if ($column_value) { $checked = TRUE; } else { $checked = FALSE; } $output .= form::checkbox($column_name, $column_value, $checked, 'class="checkbox ' . $required . '"'); break; case 'string': if (empty($item->table_columns[$column_name]['length'])) { if (empty($item->table_columns[$column_name]['format'])) { $output .= form::textarea($column_name, $column_value); } else { $output .= form::input($column_name, $column_value, 'class="date ' . $required . '"'); } } else { if (array_key_exists('binary', $item->table_columns[$column_name])) { if ($column_value) { $checked = TRUE; } else { $checked = FALSE; } $output .= form::checkbox($column_name, $column_value, $checked, 'class="checkbox ' . $required . '"'); } else { if (property_exists($item, 'admin') && array_key_exists($column_name, $item->admin) && $item->admin[$column_name]['type'] === 'file') { $attributes = array('name' => $column_name); $output .= form::upload($attributes, $column_value); $file_name = basename($column_value); $upload_to = '/'; if (array_key_exists('upload_to', $item->admin[$column_name])) { $upload_to = '/' . $item->admin[$column_name]['upload_to'] . '/'; } $link = '/' . Kohana::config('upload.relative_path') . $upload_to . $file_name; $full_file_path = rtrim(DOCROOT, '/') . $column_value; $file_type = explode('/', file::mime($full_file_path)); $file_type = $file_type[0]; // print Kohana::debug($link); if ($file_type == 'image') { $image_source = $link; $file_data = pathinfo($full_file_path); $thumb = $file_data['dirname'] . '/' . $file_data['filename'] . '_small.' . $file_data['extension']; if (file_exists($thumb)) { $image_source = '/' . Kohana::config('upload.relative_path') . $upload_to . $file_data['filename'] . '_small.' . $file_data['extension']; } $output .= '<div class="picture-container">' . html::anchor($link, html::image(array('src' => $image_source, 'width' => 100)), array('target' => '_blank')) . '</div>'; } else { $output .= html::anchor($link, $file_name, array('target' => '_blank')); } } else { $output .= form::input($column_name, $column_value, 'class="' . $required . '" size="' . $item->table_columns[$column_name]['length'] . '" maxlength="' . $item->table_columns[$column_name]['length'] . '"'); $output .= '<div class="hint">Maximum length is ' . $item->table_columns[$column_name]['length'] . '.</div>'; } } } break; case 'int': if (array_key_exists('max', $item->table_columns[$column_name]) and $item->table_columns[$column_name]['max'] == 127) { if ($column_value) { $checked = TRUE; } else { $checked = FALSE; } $output .= form::checkbox($column_name, $column_value, $checked, 'class="checkbox ' . $required . '"'); break; } $belongs_to = FALSE; $model_name = ''; $selection = array(); foreach (array_values($item->belongs_to) as $model_name) { if ($model_name . '_id' == $column_name) { $belongs_to = TRUE; } } if ($belongs_to) { preg_match('/(\\w+)_id/', $column_name, $matcher); $model_name = $matcher[1]; $selection = array(); $current_model = ORM::factory($model_name); $objects = $current_model->find_all(); foreach ($objects as $object) { $selection[$object->id] = (string) $object; } // Check if we have has_one relation if (property_exists('has_one', $current_model) and in_array($model_name, $current_model->has_one)) { // Select is not multiple $output .= form::dropdown($column_name, $selection, $column_value); } else { $output .= form::dropdown($column_name, $selection, array($column_value)); } } else { if (property_exists($item, 'children')) { preg_match('/(\\w+)_id/', $column_name, $matcher); $model_name = $matcher[1]; $objects = ORM::factory($item->object_name)->find_all(); $selection = array(); $objects = ORM::factory($model_name)->find_all(); foreach ($objects as $object) { if ($object->id == $column_value) { continue; } $selection[$object->id] = (string) $object; } $output .= form::dropdown($column_name, $selection, array($column_value)); } else { if ($column_name == 'parent_id') { $objects = ORM::factory($item->object_name)->find_all(); $objects = ORM::factory($item->object_name)->find_all(); foreach ($objects as $object) { // Don't allow to set parent to self // to prevent infinite loop if ($item->id == $object->id) { continue; } $selection[$object->id] = (string) $object; } $output .= form::dropdown($column_name, $selection, array($column_value)); } else { $output .= form::input($column_name, $column_value, 'class="' . $required . '"'); } } } break; case 'float': $output .= form::input($column_name, sprintf('%F', $column_value), 'class="' . $required . '"'); break; } $output .= '</li>'; } return $output; }
static function download($file, $name) { header("Content-disposition:attachment;filename={$name}"); header('Content-type:' + file::mime($file)); readfile($file); }
public function rule_allow() { if (empty($this->upload['tmp_name']) or count($types = func_get_args()) == 0) { return; } if (($mime = file::mime($this->upload['tmp_name'])) === FALSE) { // Trust the browser $mime = $this->upload['type']; } // Get rid of the ";charset=binary" that can occasionally occur and is // legal via RFC2045 $mime = preg_replace('/; charset=binary/', '', $mime); // Allow nothing by default $allow = FALSE; foreach ($types as $type) { // Load the mime types $type = Kohana::config('mimes.' . $type); if (is_array($type) and in_array($mime, $type)) { // Type is valid $allow = TRUE; break; } } if ($allow === FALSE) { $this->errors['invalid_type'] = TRUE; } }
protected function _image_upload($gallery) { if (request::is_ajax()) { $this->_use_json_errors(); } if ($gallery->id == 0) { return View::global_error('Invalid Gallery id'); } if (isset($_POST['username']) && isset($_POST['password'])) { Auth::instance()->login($_POST['username'], $_POST['password']); } if (!Auth::instance()->logged_in('login')) { return View::global_error('Image upload requires login'); } if ($gallery->user_id != 0 && $gallery->user_id != Auth::instance()->get_user()->id) { return View::global_error('User not gallery owner'); } if (empty($_FILES['file'])) { return View::global_error('Error with upload'); } if ($this->input->post('name') == '') { View::global_error('Missing Image Name'); } if ($_FILES['file']['name'] == '') { View::global_error('Missing File'); } if (View::errors_set()) { return; } $image = ORM::factory('image'); $image->gallery_id = $gallery->id; $image->name = $this->input->post('name'); $image->mime = file::mime($_FILES['file']['tmp_name']); $image->description = $_FILES['file']['name']; $image->size = $_FILES['file']['size']; $image->uploaded_on = time(); $image->uploaded_by = Auth::instance()->get_user()->id; if (!$image->validate()) { return View::global_error('Error validating Image'); } if (!$image->move_uploaded_file($_FILES['file']['tmp_name'])) { return View::global_error('Error moving Image'); } if (!$image->save()) { return View::global_error('Error saving Image'); } if (!$image->generate_thumb()) { return View::global_error('Error generating thumb'); } $_POST = array(); if (request::is_ajax()) { die(json_encode(array('result' => 'OK', 'id' => $image->id, 'name' => $image->name, 'url' => $image->generate_url()))); } }