00001 <?php
00002
00003
00004
00005
00006
00007
00008 class Binaries {
00009
00010
00011
00012
00013
00014
00015 public static function get($id) {
00016 return DB::get_item('binaries', 'id', $id);
00017 }
00018
00019
00020
00021
00022
00023
00024
00025 public static function read_content($id) {
00026 $ret = '';
00027
00028 $b = self::get($id);
00029 if ($b) {
00030 $ret = $b->data;
00031 }
00032 return $ret;
00033 }
00034
00035
00036
00037
00038
00039
00040
00041
00042 public static function is_upload($data) {
00043 $ret = false;
00044 if (is_array($data)) {
00045 $ret = (Arr::get_item($data, 'error', UPLOAD_ERR_NO_FILE) != UPLOAD_ERR_NO_FILE);
00046 }
00047 return $ret;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 public static function create($data, $name, $mime_type, &$created) {
00060 $ret = new Status();
00061 $params = array(
00062 'name' => $name,
00063 'mimetype' => $mime_type,
00064 'data' => $data
00065 );
00066 $cmd = CommandsFactory::create_command('binaries', 'create', $params);
00067 $ret->merge($cmd->execute());
00068 $created = $cmd->get_result();
00069 return $ret;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 public static function create_from_file($file, $name, $mime_type, &$created) {
00082 $ret = new Status();
00083 if (file_exists($file)) {
00084 if (empty($mime_type)) {
00085 $mime_type = 'application/octect-stream';
00086 if (function_exists('finfo_open')) {
00087 $handle = finfo_open(FILEINFO_MIME);
00088 $mime_type = finfo_file($handle, $file);
00089 finfo_close($handle);
00090 }
00091 else if (function_exists('mime_content_type')) {
00092 $mime_type = mime_content_type($file);
00093 }
00094 }
00095 $ret->merge(self::create(file_get_contents($file), $name, $mime_type, $created));
00096 }
00097 else {
00098 $ret->append(tr('File %f not found', 'binaries', array('%f' => $file)));
00099 }
00100 return $ret;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 public static function create_from_post($post_item, &$created) {
00115 $ret = new Status();
00116 $i_err = Arr::get_item($post_item, 'error', UPLOAD_ERR_NO_FILE);
00117 switch ($i_err) {
00118 case UPLOAD_ERR_OK:
00119 $tmp_file = Arr::get_item($post_item, 'tmp_name', '');
00120 $org_file = Arr::get_item($post_item, 'name', '');
00121 $mime = Arr::get_item($post_item, 'type', '');
00122 $ret->merge(Binaries::create_from_file($tmp_file, $org_file, $mime, $created));
00123 break;
00124 case UPLOAD_ERR_NO_FILE:
00125 $ret->append(tr('No file was uploaded', 'binaries'));
00126 break;
00127 case UPLOAD_ERR_INI_SIZE:
00128 case UPLOAD_ERR_FORM_SIZE:
00129 $ret->append(tr('The uploaded file is too big', 'binaries'));
00130 break;
00131 default:
00132 $ret->append(tr('An unknown error code was retrieved while uploading the file.', 'binaries'));
00133 break;
00134 }
00135 return $ret;
00136 }
00137 }