public function download($img_name, $img_dir1, $img_dir2, $img_dir3, $img_dir4, $img_filename, $ori_filename) { $imgArr = explode('.', $img_filename); $img = $imgArr[0]; $img_type = isset($imgArr[1]) ? $imgArr[1] : 'jpg'; $img_id = $img_dir1 . '/' . $img_dir2 . '/' . $img_dir3 . '/' . $img_dir4 . '/' . $imgArr[0]; $filename = AttService::get_instance($img_name)->get_img_dir($img_id, 0, 0, $img_type); //$file_type_current = page::getImageType($filename); //$file_type_current = (($file_type_current == 'jpg')?'jpeg':'gif'); ob_end_clean(); $mime = 'application/force-download'; header('Pragma: public'); // required header('Expires: 0'); // no cache header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); header('Content-Type: ' . $mime); header('Content-Disposition: attachment; filename="' . basename($ori_filename) . '"'); header('Content-Transfer-Encoding: binary'); header('Connection: close'); readfile($filename); // push it out ob_end_flush(); exit; }
/** * 图片附件显示 */ public function index($img_dir, $img = NULL) { if ($img_dir && !$img) { $img = $img_dir; $img_dir = "att"; } //$img_type='jpg' $imgArr = explode('.', $img); $img = $imgArr[0]; $img_type = isset($imgArr[1]) ? $imgArr[1] : 'jpg'; //$img_id=NULL, $w=0, $h=0 $imgArr = explode('_', $img); $img_id = isset($imgArr[0]) ? $imgArr[0] : NULL; $w = isset($imgArr[1]) ? $imgArr[1] : 0; $h = isset($imgArr[2]) ? $imgArr[2] : 0; //兼容格式:40x40 if (strpos(strtolower($w), 'x')) { $x = explode("x", $w); $w = isset($x[0]) ? $x[0] : 0; $h = isset($x[1]) ? $x[1] : 0; } $filename = AttService::get_instance($img_dir)->get_img_dir($img_id, $w, $h, $img_type); // 尝试通过图片类型判断 $file_type_current = page::getImageType($filename); $file_type_current = $file_type_current == 'jpg' ? 'jpeg' : 'gif'; ob_end_clean(); header("Content-type: image/{$file_type_current}"); @readfile($filename); ob_end_flush(); die; }
/** * 得到存储文件 * */ public function get_attach($attach_id) { if (!isset($attach_id) || empty($attach_id)) { exit('Bad Request!'); } $filedata = AttService::get_instance("kc")->get_img_data($attach_id, 'kc'); if (empty($filedata) || $filedata == false) { $return['error'] = 'not_found'; return $return; } else { $return['filedata'] = $filedata['filedata']; $return['filesize'] = $filedata['filesize']; return $return; } /* require_once(Kohana::find_file('vendor', 'phprpc/phprpc_client',TRUE)); !isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); !isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); $args = array($attach_id); $sign = md5(json_encode($args).$phprpcApiKey); $attachment_data = $attachmentService->phprpc_getAttachmentDataById($attach_id, $sign); if(empty($attachment_data) || $attachment_data instanceof PHPRPC_Error) { $return['error'] = 'not_found'; return $return; } $attachment_allow_view = in_array($attachment_data['filePostfix'], Kohana::config('mimemap.allowViewTypes')); $is_img_type = in_array($attachment_data['filePostfix'],Kohana::config('mimemap.isImgType')); $args = array($attachment_data['storeId']); $sign = md5(json_encode($args).$phprpcApiKey); $store_data = $attachmentService->phprpc_getStoreDataByStoreId($attachment_data['storeId'], $sign); $return['attachment_data'] = $attachment_data; $return['store_data'] = $store_data; return $return;*/ }
public function upload_attach($name, $dirname, $type = 'pic') { $return = array('code' => 0, 'msg' => ''); if ($_FILES) { //var_dump($_FILES);die(); $file_input = $name; if (!isset($_FILES[$file_input]) || !is_uploaded_file($_FILES[$file_input]["tmp_name"]) || $_FILES[$file_input]["error"] != 0) { $return['msg'] = '上传文件出错'; //echo json_encode($return); return $return; } $file_obj = $_FILES[$file_input]; $filename = $file_obj['name']; $file_ext = strtolower(tool::fileext($filename)); if ($type == 'pic') { $type = kohana::config('upload.pic_file_ext'); $file_max_size = kohana::config('upload.pic_max_size'); } else { $type = kohana::config('upload.model_file_ext'); $file_max_size = kohana::config('upload.file_max_size'); } if (!in_array($file_ext, $type)) { $return['msg'] = '文件格式错误'; //echo json_encode($return); return $return; } $file_size = filesize($file_obj['tmp_name']); if ($file_size > $file_max_size) { $return['msg'] = '文件太大'; //echo json_encode($return); return $return; } $AttService = AttService::get_instance($dirname); if ($type == 'pic') { $r = $AttService->save_default_img($file_obj["tmp_name"]); if ($r == FALSE) { $return['msg'] = '上传出错'; //echo json_encode($return); return $return; } } else { $r = $AttService->save_default_file($file_obj["tmp_name"], $file_ext); if ($r == FALSE) { $return['msg'] = '上传出错'; //echo json_encode($return); return $return; } } $img_info = array('name' => $filename, 'path' => $dirname . '/' . $r); $return['code'] = 1; $return['data'] = $img_info; //echo json_encode($return); return $return; } }
public function pic_upload() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); $request_data = $this->input->post(); $request_product_id = $request_data['product_id']; // 请求的对应的图片说明 $request_pic_title_assoc = !empty($request_data) && isset($request_data['myPorductpicTitle']) && is_array($request_data['myPorductpicTitle']) && !empty($request_data['myPorductpicTitle']) ? $request_data['myPorductpicTitle'] : array(); //多附件上传 上传的表单域名字 $attach_field = 'myPorductpic'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果有上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_upload_error'), 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_not_flow') . $attach_setup['fileSizePreLimit'], 400); } $file_type_current = FALSE; $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 //array_walk($attach_setup['allowTypes'], 'strtolower'); foreach ($attach_setup['allowTypes'] as $idx => $item) { $attach_setup['allowTypes'][$idx] = strtolower($item); } if (!empty($attach_setup['allowTypes']) && !in_array(strtolower($file_type_current), $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.pic_type_error'), 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整mime类型 if (!array_key_exists($file_mime_current, $mime_type2postfix)) { if (array_key_exists($file_type_current, $mime_postfix2type)) { $file_mime_current = $mime_postfix2type[$file_type_current]; } else { $file_mime_current = 'application/octet-stream'; } } //存储文件meta信息 $file_meta_data[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } else { throw new MyRuntimeException(Kohana::lang('o_product.pic_upload_failed'), 400); } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } /* // 当前时间戳 $timestamp_current = time(); //预备一些数据 $src_ip_address = $this->input->ip_address(); $attach_meta = array( 'siteId'=>$site_id, 'siteDomain'=>$site_domain, ); // 调用附件服务 //$attachmentService = AttachmentService::get_instance(); require_once(Kohana::find_file('vendor', 'phprpc/phprpc_client',TRUE)); !isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); !isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); */ $route_prefix = Kohana::config('attach.routePrefix'); $route_mask_view = Kohana::config('attach.routeMaskViewProduct'); // 调用productpic服务 $productpicService = ProductpicService::get_instance(); // 遍历所有的上传meta域 foreach ($file_meta_data as $index => $file_meta) { $row_meta_struct = array(); /*$attachment_data_original = array( 'filePostfix'=>$file_meta['type'], 'fileMimeType'=>$file_meta['mime'], 'fileSize'=>$file_meta['size'], 'fileName'=>$file_meta['name'], 'srcIp'=>$src_ip_address, 'attachMeta'=>json_encode($attach_meta), 'createTimestamp'=>$timestamp_current, 'modifyTimestamp'=>$timestamp_current, ); // 调用后端添加附件信息,并调用存储服务存储文件 $args_org = array($attachment_data_original); $sign_org = md5(json_encode($args_org).$phprpcApiKey); $attachment_original_id = $attachmentService->phprpc_addAttachmentFileData($attachment_data_original,@file_get_contents($file_meta['tmpfile']),$sign_org); if (!is_numeric($attachment_original_id)) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); }*/ $att = AttService::get_instance($this->img_dir_name); $img_id = $att->save_default_img($file_meta['tmpfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 500); } $productpic_data = array('product_id' => $request_product_id, 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'title' => isset($request_pic_title_assoc[$index]) ? strip_tags(trim($request_pic_title_assoc[$index])) : '', 'image_id' => $img_id); $productpic_row_id = $productpicService->add($productpic_data); $return_row_struct = array('id' => $productpic_row_id, 'is_default' => $productpic_data['is_default'], 'title' => $productpic_data['title'], 'image_id' => $img_id, 'picurl_o' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_ORIGINAL, $file_meta['type'], $route_mask_view), 'picurl_t' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_THUMBNAIL, $file_meta['type'], $route_mask_view), 'picurl_l' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_LARGE, $file_meta['type'], $route_mask_view)); $return_data[] = $return_row_struct; // 清理临时文件 @unlink($file_meta['thumb']['tmpfile']); } if (!empty($return_data) && $request_product_id > 0) { //检测是否存在默认图片,没有则设置商品默认图片 if ($productpicService->has_default_pic($request_product_id) == FALSE) { $default_productpic_id = $return_data[0]['id']; $productpicService->set_default_pic_by_productpic_id($default_productpic_id, $request_product_id); $return_data[0]['is_default'] = ProductpicService::PRODUCTPIC_IS_DEFAULT_TRUE; } } //Cache::remove('product_pictures.'.$request_product_id); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //$pics = $productpicService->get_stand_pic_by_pic_id($productpic_row_id, ProductpicService::PRODUCTPIC_STANDARDS_COMMON); //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ //$this->template->content->request_data = $request_data; //* 返回结构体绑定 */ //$this->template->content->return_struct = $return_struct; $this->template->content->picture = $return_struct['content']; } // end of request type determine } catch (MyRuntimeException $ex) { $this->template = new View('layout/default_html'); $this->_ex($ex, $return_struct, $request_data); } }
/** * 清理指定主题的图片资源信息 * @param int $theme_id 主题ID */ public function clear_theme_img($img_id) { AttService::get_instance($this->img_dir_name)->delete_img($img_id); return true; }
/** * 删除站点LOGO */ public function delete_logo() { //更新数据库标识 $data = Mysite::instance()->get(); $data['logo'] = NULL; if (Mysite::instance()->update_site_config($data)) { $img_id = 'logo'; AttService::get_instance($this->img_dir_name)->delete_img($img_id, false); //Storage_server::instance()->delete_site($this->site_id, $filename); remind::set(Kohana::lang('o_global.delete_success'), 'site/config', 'success'); } else { remind::set(Kohana::lang('o_global.delete_error'), 'site/config'); } }
/** * 删除商品对应的附件数据和存储文件 * @param $image_id */ private function delete_img($image_id) { !empty($image_id) && AttService::get_instance("att")->delete_img($image_id); }
protected function set_product($l) { $product_id = 0; $str_bad = array(" ", '(', ')', '(', ')'); $sku = str_replace($str_bad, '', $l['2']); $title = trim($l['5']); $datas = array('status' => 0, 'front_visible' => 1, 'classify_id' => (int) $l['0'], 'category_id' => (int) $l['1'], 'sku' => $sku, 'name_manage' => $l['4'], 'title' => $title, 'brand_id' => (int) $l['6'], 'on_sale' => (int) $l['7'], 'price' => $l['8'], 'market_price' => $l['9'], 'cost' => $l['14'], 'brief' => $l['12'], 'store' => (int) $l['15'], 'weight' => (int) $l['16'], 'meta_title' => $l['17'], 'meta_keywords' => $l['18'], 'meta_description' => $l['19'], 'create_time' => time(), 'update_timestamp' => time()); //save data $query_struct = array('orderby' => array('id' => 'ASC'), 'limit' => array('page' => 1, 'per_page' => 1)); if (!empty($sku)) { $query_struct['where'] = array('sku' => $sku); } else { $query_struct['where'] = array('title' => $title); } $product = ProductService::get_instance()->query_assoc($query_struct); if (!empty($product)) { $product_id = $product[0]['id']; $datas['id'] = $product_id; } if ($product_id > 0) { ProductService::get_instance()->update($datas); } else { $product_id = ProductService::get_instance()->create($datas); } //save pic $pictures = str_replace(',', ',', $l['10']); $pictures = trim($pictures, ','); if ($product_id > 0 && !empty($pictures)) { $i = 0; $pictures = explode(',', $pictures); foreach ($pictures as $pic_name) { $str_bad = array(" ", ",", "\t", "\r", "\n", "\r\n"); $pic_name = str_replace($str_bad, '', $pic_name); if (empty($pic_name)) { continue; } $img_file = ATTPATH . 'upimg/' . iconv('UTF-8', 'GBK//IGNORE', $pic_name); if (!file_exists($img_file)) { $this->set_error(new MyRuntimeException("图片不存在:" . $pic_name)); continue; } $str_bad = array('(', ')', '(', ')'); $picname = str_replace($str_bad, '', $pic_name); $default_img_id = strtolower(substr($picname, 0, strrpos($picname, '.'))); if (file_exists(ATTPATH . 'product/' . $default_img_id . '/default.jpg')) { $img_id = $default_img_id; } else { $att = AttService::get_instance("product"); $img_id = $att->save_default_img($img_file, $default_img_id); } if (empty($img_id)) { $this->set_error(new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed') . $pic_name)); continue; } $query_struct = array('where' => array('product_id' => $product_id, 'image_id' => $img_id), 'orderby' => array('id' => 'ASC'), 'limit' => array('page' => 1, 'per_page' => 1)); $pic_data = ProductpicService::get_instance()->query_assoc($query_struct); if (empty($pic_data)) { $productpic_data = array('product_id' => $product_id, 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'image_id' => $img_id); $productpic_id = ProductpicService::get_instance()->add($productpic_data); if ($i == 0 && $productpic_id > 0) { ProductpicService::get_instance()->set_default_pic_by_productpic_id($productpic_id, $product_id); } } $i++; } } }
public function upload() { role::check('product_category'); $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $request_data = $this->input->post(); // 上传的表单域名字 $attach_field = 'category_img'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果有上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException('请选择所要上传的图片', 400); } //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_not_uploaded') . $index, 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_prelimit') . $attach_setup['fileSizePreLimit'] . Kohana::lang('o_product.index') . $index . Kohana::lang('o_product.size') . $file_size_current, 400); } $file_type_current = FALSE; $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 if (!empty($attachSetup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.file_type_invalid') . $index, 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整mime类型 if (!array_key_exists($file_mime_current, $mime_type2postfix)) { if (array_key_exists($file_type_current, $mime_postfix2type)) { $file_mime_current = $mime_postfix2type[$file_type_current]; } else { $file_mime_current = 'application/octet-stream'; } } //存储文件meta信息 $file_meta_data[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } // 当前时间戳 //$timestamp_current = time(); //预备一些数据 //$src_ip_address = $this->input->ip_address(); //$attach_meta = array (); // 调用附件服务 //$attachmentService = AttachmentService::get_instance(); //require_once (Kohana::find_file('vendor', 'phprpc/phprpc_client', TRUE)); //!isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); //!isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); /*$attachment_data_original = array ( // 'site_id'=>$site_id, 'filePostfix' => $file_meta['type'], 'fileMimeType' => $file_meta['mime'], 'fileSize' => $file_meta['size'], 'fileName' => $file_meta['name'], 'srcIp' => $src_ip_address, 'attachMeta' => json_encode($attach_meta), 'createTimestamp' => $timestamp_current, 'modifyTimestamp' => $timestamp_current ); // 调用后端添加附件信息,并调用存储服务存储文件 $args_org = array ( $attachment_data_original ); $sign_org = md5(json_encode($args_org) . $phprpcApiKey); $attachment_original_id = $attachmentService->phprpc_addAttachmentFileData($attachment_data_original, @file_get_contents($file_meta['tmpfile']), $sign_org); */ $AttService = AttService::get_instance($this->img_dir_name); $file_meta = $file_meta_data[0]; $img_id = $AttService->save_default_img($file_meta['tmpfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); } $return_data['pic_attach_id'] = $img_id; $return_data['pic_url'] = $AttService->get_img_url($img_id); // 清理临时文件 @unlink($file_meta['tmpfile']); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $this->_ex($ex, $return_struct, $request_data, 'info2', 'layout/default_html'); } }
/** * 删除对应的附件数据和存储文件 * @param int $id 附件id */ public function delete_attachment($image_id) { // 调用附件服务 /*require_once (Kohana::find_file('vendor', 'phprpc/phprpc_client', TRUE)); !isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); !isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); $args = array ( $id ); $sign = md5(json_encode($args) . $phprpcApiKey); $attachmentService->phprpc_removeAttachmentDataByAttachmentId($id, $sign); */ AttService::get_instance("att")->delete_img($image_id); }
/** * 文件上传处理 * @param int $folder_id * @return array attachment_ids * @author weizhifeng **/ protected function do_upload($folder_id) { // 上传的表单域名字 $attach_field = 'upload'; // 附件应用类型 $attach_app_type = 'productPicAttach'; $file_count_total = 0; $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 如果有上传请求 if (!isset($_FILES[$attach_field]) || empty($_FILES[$attach_field])) { exit(Kohana::lang('o_global.bad_request')); } $title = array(); //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); $file_type_current = FALSE; $file_size_current = ''; $file_mime_current = ''; // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'])) { $this->error_msg(Kohana::lang('o_promotion.file_not_uploaded')); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name']); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { $this->error_msg(Kohana::lang('o_kc.file_big_than_max', $attach_setup['fileSizePreLimit'] / 1024 / 1024)); } // 尝试通过图片类型判断 $file_type_current = $file_type_current ? $file_type_current : kc_page::get_image_type($_FILES[$attach_field]['tmp_name']); // 尝试通过Mime类型判断 $file_type_current = $file_type_current ? $file_type_current : kc_page::get_file_type($attach_field); // 尝试通过后缀截取 $file_type_current = $file_type_current ? $file_type_current : kc_page::get_postfix($attach_field); if (!empty($attach_setup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) { $this->error_msg(Kohana::lang('o_promotion.file_type_invalid')); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type']) ? $_FILES[$attach_field]['type'] : ''; // 检测规整mime类型 if (!array_key_exists($file_mime_current, $mime_type2postfix)) { if (array_key_exists($file_type_current, $mime_postfix2type)) { $file_mime_current = $mime_postfix2type[$file_type_current]; } else { $file_mime_current = 'application/octet-stream'; } } //存储文件meta信息 $file_meta_data = array('site_id' => $this->site_id, 'name' => strip_tags(trim($_FILES[$attach_field]['name'])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name']); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } else { $this->error_msg(Kohana::lang('o_product.pic_upload_failed')); } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { $this->error_msg(Kohana::lang('o_promotion.file_count_limit') . $attach_setup['fileCountLimit']); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { $this->error_msg(Kohana::lang('o_promotion.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_promotion.size') . $file_size_total); } $att = AttService::get_instance($this->img_dir_name); $attachment_id = $att->save_default_img($file_meta_data['tmpfile']); if (!$attachment_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 500); } /*$attachment_id = Mykc_image::instance()->add_attach($file_meta_data); if (!is_numeric($attachment_id)) { $this->error_msg(Kohana::lang('o_product.phprpc_pic_save_failed')); }*/ $image_name = strip_tags(trim($_FILES[$attach_field]['name'])); while (Mykc_image::instance()->count(array('where' => array('site_id' => $this->site_id, 'kc_folder_id' => $folder_id, 'image_name' => $image_name))) >= 1) { $point_pos = strrpos($image_name, '.'); if ($point_pos !== FALSE) { $postfix = substr($image_name, $point_pos + 1); $pre_name = substr($image_name, 0, $point_pos); $image_name = $pre_name . '_' . $attachment_id . '.' . $postfix; } else { $image_name = $image_name . '_' . $attachment_id; } } $image_data = array('site_id' => $this->site_id, 'kc_folder_id' => $folder_id, 'attach_id' => $attachment_id, 'image_type' => $file_type_current, 'image_size' => $file_size_current, 'image_name' => $image_name, 'image_mime' => $file_mime_current, 'date_add' => date('Y-m-d H:i:s'), 'date_upd' => date('Y-m-d H:i:s')); $image_id = Mykc_image::instance()->create($image_data); if (!$image_id) { exit(Kohana::lang('o_kc.can_not_write_folder')); } return $attachment_id; }
public function upload() { role::check('product_attribute'); try { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); //* 初始化返回数据 */ $return_data = array(); //* 收集请求数据 ==根据业务逻辑定制== */ $return_data = $request_data = $this->input->post(); // 上传的表单域名字 $attach_field = 'attribute_img'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果无上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException('请选择需要上传的图片', 400); } $title = isset($request_data['attribute_img_title']) && is_array($request_data['attribute_img_title']) && !empty($request_data['attribute_img_title']) ? $request_data['attribute_img_title'] : array(); //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_not_uploaded') . $index, 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_prelimit') . $attach_setup['fileSizePreLimit'] . Kohana::lang('o_product.index') . $index . Kohana::lang('o_product.size') . $file_size_current, 400); } $file_type_current = FALSE; $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 if (isset($attach_setup['allowTypes']) && !in_array($file_type_current, $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.file_type_invalid') . $_FILES[$attach_field]['name'][$index], 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整mime类型 if (!array_key_exists($file_mime_current, $mime_type2postfix)) { if (array_key_exists($file_type_current, $mime_postfix2type)) { $file_mime_current = $mime_postfix2type[$file_type_current]; } else { $file_mime_current = 'application/octet-stream'; } } //存储文件meta信息 $file_meta_data[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmbfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } // 当前时间戳 //$timestamp_current = time(); //预备一些数据 //$src_ip_address = $this->input->ip_address(); //$attach_meta = array (); //require_once (Kohana::find_file('vendor', 'phprpc/phprpc_client', TRUE)); //!isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); //!isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); // 调用附件存储服务 for ($index = 0; $index < $file_count_total; $index++) { $img_id = AttService::get_instance()->save_default_img($file_meta_data[$index]['tmbfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); } $attribute_image = array('attach_id' => $img_id, 'title' => isset($title[$index]) ? strip_tags(trim($title[$index])) : ''); $return_data['picurl'] = $attribute_image['picurl'] = AttService::get_instance()->get_img_url($img_id); $return_data['meta'] = implode('|', $attribute_image); // 清理临时文件 @unlink($file_meta_data[$index]['tmbfile']); } //echo "<pre>";print_r($file_meta_data);print_r($return_data);die(); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; //:: 当前应用专用数据 $this->template->content->title = Kohana::config('site.name'); } // end of request type determine } catch (MyRuntimeException $ex) { $return_struct['status'] = 0; $return_struct['code'] = $ex->getCode(); $return_struct['msg'] = $ex->getMessage(); //TODO 异常处理 //throw $ex; if ($this->is_ajax_request()) { $this->template->content = $return_struct; } else { //$return_struct['action']['type']= 'close'; // 当前应用为弹出窗口所以定义失败后续动作为关闭窗口 $this->template = new View('layout/default_html'); $this->template->return_struct = $return_struct; $content = new View('info2'); $this->template->content = $content; //* 请求结构数据绑定 */ $this->template->content->request_data = $request_data; $return_struct['action'] = array('type' => 'location', 'url' => request::referrer()); //* 返回结构体绑定 */ $this->template->content->return_struct = $return_struct; } } }
/** * 模板增加可变内容 */ public function config_add($id = 0) { if (!$id) { remind::set(Kohana::lang('o_manage.theme_id_not_exist'), '/manage/theme'); } //默认类型 $type = $this->input->get('type') > 0 ? $this->input->get('type') : 1; if ($_POST) { $val = ''; //资源类型 $config_type = trim($this->input->post('config_type')); //资源名称 $name = trim($this->input->post('config_name')); //资源标识 $flag = trim($this->input->post('config_flag')); //图片链接 $url = trim($this->input->post('img_url')); //图片链接 $alt = trim($this->input->post('img_alt')); //资源文件最大大小(default:1M) $file_max_size = kohana::config('theme.file_max_size'); $file_max_size = $file_max_size > 0 ? $file_max_size : 1048576; switch ($config_type) { case 1: $val = trim($this->input->post('text_val')); break; case 2: $file_type = kohana::config('theme.image_file_type'); $type = count($file_type) > 0 ? $file_type : array('jpg'); //判断文件类型 if (!in_array(strtolower($this->fileext($_FILES['img_val']['name'])), $type)) { remind::set(Kohana::lang('o_manage.pic_type_incorrect'), '/manage/theme/config_add/' . $id . '&type=2'); } $val = $_FILES['img_val']['name']; $file_size = filesize($_FILES['img_val']['tmp_name']); if ($file_size > $file_max_size) { remind::set(Kohana::lang('o_manage.pic_size_out_range'), '/manage/theme/config_add/' . $id . '&type=2'); } break; case 3: $val = $this->input->post('link_val'); break; } if (empty($val)) { remind::set(Kohana::lang('o_global.add_error'), '/manage/theme/config/' . $id); } $theme = Mytheme::instance($id)->get(); $configs = empty($theme['config']) ? null : unserialize($theme['config']); if ($configs && isset($configs['val'])) { foreach ($configs['val'] as $k => $v) { if ($k == $flag) { remind::set(Kohana::lang('o_manage.key_conflict'), '/manage/theme/config_add/' . $id); } } } $configs['val'][$flag] = $val; $configs['name'][$flag] = $name; $configs['type'][$flag] = $config_type; $configs['desc'][$flag] = array('url' => $url, 'alt' => $alt); $data = array(); $data['config'] = serialize($configs); if (Mytheme::instance($id)->edit($data)) { if ($config_type == 2) { //$file = file_get_contents($_FILES['img_val']["tmp_name"]); //$filename = $flag . '_' . $val; //Storage_server::instance()->cache_theme($id,'images',$filename,$file); if ($_FILES['img_val']['name']) { $AttService = AttService::get_instance($this->img_dir_name); $img_id = $AttService->save_default_img($_FILES['img_val']["tmp_name"], $this->img_dir_name . $id . $flag); if (!$img_id) { remind::set(Kohana::lang('o_product.phprpc_pic_save_failed'), '/manage/theme/config_edit/' . $id . '?key=' . $flag); } } } remind::set(Kohana::lang('o_global.add_success'), '/manage/theme/config/' . $id, 'success'); } else { remind::set(Kohana::lang('o_global.add_error'), '/manage/theme/config_add/' . $id); } } $this->template->content = new View("manage/theme_config_add"); $this->template->content->type = $type; $this->template->content->id = $id; }
/** * 删除对应的附件数据和存储文件 * @param int $id 附件id */ public function delete_pic($id) { AttService::get_instance("category")->delete_img($id); }
/** * 删除站点LOGO */ public function del_newpic($id = "") { //更新数据库标识 $newpic_h = $this->input->get('img'); $data['newpic'] = ""; $news = Mynews::instance($id); if ($news->edit($data)) { $img_id = $newpic_h; AttService::get_instance($this->img_dir_name)->delete_img($img_id, false); $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = $img_id; $return_struct['content'] = '删除成功'; exit(json_encode($return_struct)); } else { $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = 'Success'; $return_struct['content'] = '删除失败'; exit(json_encode($return_struct)); } }