/var/www/hkosl.com/ifapc/html_20210817/IFAPC2021/application/controllers/Application_file.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
require_once APPPATH 'core/MY_Controller_2.php';

class 
Application_file extends MY_Controller_2
{
    private 
$scope 'application_file';
    public 
$apl_upload_path 'assets/files/application/';

    public function 
__construct()
    {
        
// set $this->table and $this->model
        
parent::__construct(__CLASS__);
    }

    
//kelvin, upload file
    
public function modify($id)
    {
        
$application Application_model::with('application_file')->whereDeleted(0)->find($id);
        if (!empty(
$application)) {
            
$data['reference_no'] = $application['reference_no'];
            
$data['id'] = $id;
            
$data['scope'] = $id;

            
$count_application_file Application_file_model::whereDeleted(0)->where('application_id'$id)->count();
            
$data['count_application_file'] = $count_application_file;
            
$data['base64_path'] = $this->apl_upload_path;
            
$data['application_file'] = $application['application_file'];

            
$this->load->view('main/' $this->scope '_form'$data);
        }
    }

    public function 
add($id)
    {
        
header('Content-Type: application/json');

        
_log('application id: '.$id.', application_file/add');

        if (
$_SESSION['application'][$id]['membership'] != 'nominee') {
            
_log('Not allow to upload files.');
            
$response['msg'] = __('Not allow to upload files.');
            echo 
json_encode($response);
            exit;
        }

        
$response = array('response' => 0'msg' => '''data' => array());

        
$application Application_model::whereDeleted(0)->find($id);
        if (empty(
$application)) {
            
_log('Cannot find valid application.');
            
$response['msg'] = __('Cannot find valid application.');
            echo 
json_encode($response);
            exit;
        }

        
$title _h($this->input->post('title'));
        
_log('data: title'$title);
        
$data = array();

        
//check file
        
$upload_file_list $this->init_upload_file();

        foreach (
$upload_file_list as $key => $row) {
            if (empty(
$_FILES[$key]['name'])) {
                
_log('empty of file');
                
$response['msg'] = __('Cannot find valid application.');
                echo 
json_encode($response);
                exit;
            } else {
                if (
$row['base64']) {
                    
$single_upload base64_upload($key$row['upload_path'], $row['label'], $row['allowed_types_base64'], 0);
                } else {
                    
$single_upload single_upload($key$row);
                }

                
//if success return $single_upload['filename'] else return $single_upload['error']
                
if ($single_upload['error']) {
                    
_log('upload error'$single_upload['error']);
                    
$response['msg'] = $single_upload['error'];
                    echo 
json_encode($response);
                    exit;
                    
//history_back();
                
} else if ($single_upload['filename']) {
                    
/*$upload_file_list[$key]['file_name'] = $single_upload['filename'];
                    $upload_file_list[$key]['ori_file_name'] = $single_upload['ori_filename'];
                    $upload_file_list[$key]['extension'] = $single_upload['extension'];
                    $upload_file_list[$key]['base64'] = $row['base64'];*/

                    
$data2 = array(
                        
'creator_session_id' => session_id(),
                        
'application_id' => $id,
                        
'title' => $title,
                        
'file_name' => $single_upload['filename'],
                        
'ori_file_name' => $single_upload['ori_filename'],
                        
'extension' => $single_upload['extension'],
                        
'base64' => (int) $row['base64'],
                    );

                    
$application_file Application_file_model::create($data2);

                    
$data['id'] = $application_file->id;

                    
_log('application file id'$data['id']);
                    if (
$row['base64']) {
                        if (
preg_match("/jpg|jpeg|png|gif/i"$single_upload["extension"])) {
                            
$data['file_content'] = base64_upload_decode($row['upload_path2'] . $single_upload['filename'], $single_upload['extension'], $single_upload['ori_filename'], ''1);
                        } else {
                            
$data['file_content'] = base64_upload_decode($row['upload_path2'] . $single_upload['filename'], $single_upload['extension'], $single_upload['ori_filename'], ''1);
                        }
                    }

                    
_log('file content'substr($data['file_content'], 0100));
                }
            }
        }
        
//end

        // if($application_file->id){
        
_log('Upload Successfully');
        
$response = array('response' => 1'msg' => __('Upload Successfully.'), 'data' => $data);
        echo 
json_encode($response);
        exit;
        
// }
    
}

    private function 
init_upload_file()
    {
        return 
$upload_file_list = array(
            
'upload_file' => array(
                
'upload_path' => FCPATH $this->apl_upload_path,
                
'upload_path2' => $this->apl_upload_path,
                
'relative_upload_path' => $this->apl_upload_path,
                
'allowed_types' => 'jpeg|jpg|png|tif|gif|doc|docx|ppt|pptx|pdf',
                
'allowed_types_base64' => array('image/jpeg''image/png''image/gif''image/tiff''application/pdf''application/vnd.ms-powerpoint''application/vnd.openxmlformats-officedocument.presentationml.presentation''application/msword''application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
                
'max_size' => 1024 20,
                
'encrypt_name' => true,
                
'type' => '',
                
'file_name' => null,
                
'ori_file_name' => null,
                
'extension' => null,
                
'deleted' => false,
                
'required' => true,
                
'label' => __('Upload File'),
                
'base64' => true,
            ),
        );
    }

    public function 
delete($id)
    {
        
header('Content-Type: application/json');
        
$response = array('response' => 0'msg' => '''data' => array());

        
$application_file Application_file_model::with('application')->where('id'$id)->first();

        
//->where('member_id', $_SESSION['member_id'])
        
if (empty($application_file)) {
            
$response['msg'] = __('Cannot find valid application file.');
            echo 
json_encode($response);
            exit;
        } else {
            if (
$_SESSION['application'][$application_file['application']['id']]['membership'] != 'nominee' || $application_file['creator_session_id'] != session_id()) {
                
$response['msg'] = __('Not allow to delete application file.');
                echo 
json_encode($response);
                exit;
            }

            
//unlink file
            
unlink(FCPATH $this->apl_upload_path $application_file['file_name']);

            
$data2 = array(
                
'deleted_at' => date('Y-m-d H:i:s'),
                
'deleted_by' => $_SESSION['member_id'],
                
'deleted' => 1,
            );

            
$application_file->update($data2);

            
$response = array('response' => 1'msg' => __('Delete Successfully.'), 'data' => array());
            echo 
json_encode($response);
            exit;
        }
    }

    public function 
download($id)
    {
        
$application_file Application_file_model::with('application')->where('id'$id)->first();

        if (empty(
$application_file)) {
            echo 
'<script>alert("' __('Cannot find valid application file.') . '"); history.back();</script>';
            exit;
        } else {
            if (!empty(
$_SESSION['judge_id'])) {
                
$groups_judges Group_judge_model::get_data_by_field_with_value(['judge_id' => $_SESSION['judge_id']]);
                
$groups_id = array();
                foreach (
$groups_judges as $row) {
                    
$groups_id[] = $row['group_id'];
                }
            }

            
$application $application_file['application'];
            if ((
$_SESSION['application'][$application['id']]['membership'] != 'nominee' || $application_file['creator_session_id'] != session_id()) && (empty($_SESSION['judge_id']) || !in_array($application['group_id'], $groups_id))) {
                
$response['msg'] = __('Not allow to download application file.');
                echo 
json_encode($response);
                exit;
            }
        }

        
$content base64_upload_decode($this->apl_upload_path $application_file['file_name'], $application_file['extension'], $application_file['ori_file_name'], ''11);

        if(empty(
$application_file['ori_file_name'])){
            
$application_file['ori_file_name'] = $application_file['title'];
        }
        
header('Content-Description: File Transfer');
        
header('Content-Type: application/octet-stream');
        
header("Content-Disposition: attachment; filename=" $application_file['ori_file_name'] . '.' $application_file['extension']);
        
header('Expires: 0');
        
header('Cache-Control: must-revalidate');
        
header('Pragma: public');
        echo 
base64_decode($content);
    }
}