Exemple #1
0
 /**
  * Function to upload multi file
  * 
  * @param string 	$field			Field name to upload
  * @param string 	$uploadPath		Path to upload
  * @param boolean 	$thumb			Auto generate thumbnail or not
  * @param string 	$thumbName		If auto generate, this will be used for thumbnail
  */
 function files($field, $uploadPath, $newName = null, $thumb = false, $uploadPathThumbs = null, $thumbName = null)
 {
     $files = JRequest::get('files');
     $arrFiles = array();
     //rebuild array to re-use function upload::file
     foreach ($files['jform']['error'][$field] as $key => $error) {
         if (!$error) {
             $arrFiles[$key]['jform']['name'][$field] = $files['jform']['name'][$field][$key];
             $arrFiles[$key]['jform']['type'][$field] = $files['jform']['type'][$field][$key];
             $arrFiles[$key]['jform']['tmp_name'][$field] = $files['jform']['tmp_name'][$field][$key];
             $arrFiles[$key]['jform']['error'][$field] = $files['jform']['error'][$field][$key];
             $arrFiles[$key]['jform']['size'][$field] = $files['jform']['size'][$field][$key];
         }
     }
     $result = array();
     $i = 1;
     foreach ($arrFiles as $key => $file) {
         if ($newName) {
             $newName = $i . '-' . $newName;
         }
         $i++;
         $result[$key] = upload::file($file['jform'], $field, $uploadPath, $newName, $thumb, $uploadPathThumbs, $thumbName);
     }
     return $result;
 }
 public function editFont($id = '')
 {
     $file = dirname(ROOT) . DS . 'data' . DS . 'fonts.json';
     include_once ROOT . DS . 'includes' . DS . 'functions.php';
     include_once ROOT . DS . 'includes' . DS . 'upload.php';
     $dg = new dg();
     $fonts = $dg->readFile($file);
     $fonts = json_decode($fonts, true);
     $font = $fonts['fonts']['fonts'];
     $data = array();
     $data['error'] = '';
     $data['id'] = $id;
     // get categories.
     $cate_font = ROOT . DS . 'data' . DS . 'font_categories.json';
     $cate = $dg->readFile($cate_font);
     $cates = json_decode($cate, true);
     $data['categories'] = $cates;
     // post data.
     if (isset($_POST['title']) && $_POST['title'] != '') {
         $title = $_POST['title'];
         //upload.
         $path = dirname(ROOT) . DS . 'data' . DS . 'fonts';
         if (!file_exists($path)) {
             mkdir($path, 0755);
         }
         $up = new upload();
         $up->permission = 755;
         $up->path = $path;
         $up->file_size = 2097152;
         // 2mb.
         $count = 0;
         $result = array();
         if (count($_FILES) == 3) {
             foreach ($_FILES as $key => $value) {
                 if (isset($_FILES[$key]['name']) && $_FILES[$key]['name'] != '') {
                     $checkname = array('~', '`', '!', '@', '#', '$', '%', '^', '&', '(', ')', '+', '=', '[', ']', '{', '}', ':', ' ', ',', '\'', ';');
                     $up->file_name = str_replace($checkname, '', $_FILES[$key]['name']);
                     if ($count == 0) {
                         $up->file_type[0] = 'woff';
                         $val = $up->file($_FILES[$key]);
                         if ($val['error'] == 1) {
                             $data['error'] = $val['msg'];
                             break;
                         }
                         $result[$count] = $val;
                     } elseif ($count == 1) {
                         $up->file_type[0] = 'ttf';
                         $up->file_type[1] = 'TTF';
                         $val = $up->file($_FILES[$key]);
                         if ($val['error'] == 1) {
                             $data['error'] = $val['msg'];
                             if (file_exists($result[0]['full_path'])) {
                                 unlink($result[0]['full_path']);
                             }
                             break;
                         }
                         $result[$count] = $val;
                     } else {
                         $up->file_type[0] = 'jpg';
                         $up->file_type[1] = 'png';
                         $up->file_type[2] = 'gif';
                         $up->file_type[3] = 'jpeg';
                         $val = $up->file($_FILES[$key]);
                         if ($val['error'] == 1) {
                             $data['error'] = $val['msg'];
                             if (file_exists($result[0]['full_path'])) {
                                 unlink($result[0]['full_path']);
                             }
                             if (file_exists($result[1]['full_path'])) {
                                 unlink($result[1]['full_path']);
                             }
                             break;
                         }
                         $result[$count] = $val;
                     }
                 }
                 $count++;
             }
         }
         //process.
         $subtitle = '';
         $cate_id = '';
         $catename = '';
         if (isset($_POST['subtitle'])) {
             $subtitle = $_POST['subtitle'];
         }
         if (isset($_POST['cate_id'])) {
             $cate_id = $_POST['cate_id'];
         }
         if ($catename == '' && isset($cates[$cate_id])) {
             $catename = $cates[$cate_id];
         }
         if ($id != '') {
             if (count($font) && $data['error'] == '') {
                 $font_out = array();
                 foreach ($font as $key => $val) {
                     if ($val['id'] == $id && $val['type'] == '') {
                         $filename = $val['filename'];
                         $path = $val['path'];
                         $thumb = $val['thumb'];
                         if ($data['error'] == '' && count($result) == 3) {
                             $filename = array('woff' => $result[0]['file_name'], 'ttf' => $result[1]['file_name']);
                             $filename = json_encode($filename);
                             $path = $path;
                             $thumb = $result[2]['file_name'];
                         }
                         $font_out[$key] = array('id' => $val['id'], 'title' => $_POST['title'], 'subtitle' => $subtitle, 'filename' => $filename, 'path' => 'data/fonts', 'thumb' => $thumb, 'shop_id' => '', 'cate_id' => $cate_id, 'published' => '1', 'catename' => $catename, 'type' => '');
                     } else {
                         $font_out[$key] = $val;
                     }
                 }
                 // out data.
                 $out['status'] = '1';
                 $out['fonts']['google_fonts'] = $fonts['fonts']['google_fonts'];
                 $out['fonts']['fonts'] = $font_out;
                 // get cates.
                 foreach ($cates as $k => $v) {
                     $out['fonts']['cateFonts'][$k]['fonts'] = $this->updateFonts($out['fonts']['fonts'], $k, $v, 'catefont');
                     $out['fonts']['categories'][$k] = $this->updateCate($k, $v);
                 }
                 $res = json_encode($out);
             }
         } else {
             if ($data['error'] == '' && count($result) == 3) {
                 // get last id.
                 if (count($font)) {
                     $font_end = end($fonts['fonts']['fonts']);
                     $font_id = $font_end['id'] + 1;
                 } else {
                     $font_id = 0;
                 }
                 //add a font.
                 $filename = array('woff' => $result[0]['file_name'], 'ttf' => $result[1]['file_name']);
                 $font[] = array('id' => (string) $font_id, 'title' => $_POST['title'], 'subtitle' => $subtitle, 'filename' => json_encode($filename), 'path' => 'data/fonts', 'thumb' => $result[2]['file_name'], 'shop_id' => '', 'cate_id' => $cate_id, 'published' => '1', 'catename' => $catename, 'type' => '');
                 // out data.
                 $out['status'] = '1';
                 $out['fonts']['google_fonts'] = $fonts['fonts']['google_fonts'];
                 $out['fonts']['fonts'] = $font;
                 // get cates.
                 foreach ($cates as $key => $val) {
                     $out['fonts']['cateFonts'][$key]['fonts'] = $this->updateFonts($out['fonts']['fonts'], $key, $val, 'catefont');
                     $out['fonts']['categories'][$key] = $this->updateCate($key, $val);
                 }
                 $res = json_encode($out);
             } elseif ($data['error'] == '') {
                 $data['error'] = lang('fonts_add_font_file_error_msg', true);
             }
         }
         if (isset($res) && $res != '') {
             $dg->WriteFile($file, $res);
             if ($id == '') {
                 $data['msg'] = lang('fonts_add_font_file_success_msg', true);
             } else {
                 $data['msg'] = lang('fonts_edit_font_file_success_msg', true);
             }
         }
     }
     if (empty($subtitle)) {
         $subtitle = '';
     }
     if (empty($title)) {
         $title = '';
     }
     $data['font'] = array('title' => $title, 'subtitle' => $subtitle, 'filename' => '', 'path' => '', 'cate_id' => '', 'thumb' => '');
     if ($id == '') {
         $data['title'] = lang('breadcrumb_add_font', true);
     } else {
         $fonts = $dg->readFile($file);
         $fonts = json_decode($fonts, true);
         $font = $fonts['fonts']['fonts'];
         foreach ($font as $key => $val) {
             if ($val['id'] == $id && $val['type'] == '') {
                 $data['font'] = array('title' => $val['title'], 'subtitle' => $val['subtitle'], 'filename' => $val['filename'], 'path' => $val['path'], 'cate_id' => $val['cate_id'], 'thumb' => $val['thumb']);
             }
         }
         $data['title'] = lang('breadcrumb_edit_font', true);
     }
     $data['sub_title'] = lang('breadcrumb_manager', true);
     $this->view('edit_font', $data);
 }
Exemple #3
0
 static function replaceFile()
 {
     global $page;
     $filename = get('filename');
     $file = $page->files()->find($filename);
     if (!$file) {
         return array('status' => 'error', 'msg' => l::get('files.replace.errors.notfound'));
     }
     $upload = upload::file('file', $page->root() . '/' . $filename);
     if (error($upload)) {
         return $upload;
     }
     self::killCache();
     return array('status' => 'success', 'msg' => l::get('files.replace.success'));
 }
Exemple #4
0
 function uploadFiles($fileName, $uploadPath)
 {
     //require upload file
     require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers/upload.class.php';
     //define upload path
     $uploadPathMore = date('Y') . DS . date('m') . DS . date('d') . DS;
     $uploadPath .= $uploadPathMore;
     //echo $uploadPath; die;
     $files = JRequest::get('files');
     $post = JRequest::get('post');
     $files = $files['jform'];
     $name = array();
     $data = array();
     foreach ($post['jform']['images']['type'] as $key => $type) {
         if ($files['name']['images'][$key] != '' && !$files['error']['images'][$key]) {
             //set image name
             $imageName = $fileName . '-' . time() . '.' . end(explode('.', $files['name']['images'][$key]));
             //upload file
             $upload = upload::file($files, 'images', $uploadPath, $imageName, $key, false);
             //if upload OK
             if (is_array($upload) && $upload['result'] == 'OK') {
                 $data[] = array('image' => str_replace(DS, '/', $uploadPathMore) . $upload['file_name'], 'type' => $type);
             } else {
                 JError::raiseNotice('UPLOAD_ERROR', 'Upload Error');
             }
         }
     }
     //$data = serialize($data);
     return $data;
 }