57
57
" ?>" ,
58
58
};
59
59
elseif strcmp(name , " pass_args_post" ) == 1
60
- octave_php_wrapper_example_string = pass_args_post();
60
+ text_cell = {
61
+ " <?php" ,
62
+ " if (isset($_POST['n'])) {" ,
63
+ " $n = $_POST['n'];" ,
64
+ " echo escapeshellarg($n);" ,
65
+ " } else {" ,
66
+ " echo \" Param n should not be null.\" ;" ,
67
+ " }",
68
+ " ?>" ,
69
+ };
61
70
elseif strcmp(name , " pass_args_put" ) == 1
62
- octave_php_wrapper_example_string = pass_args_put();
71
+ text_cell = {
72
+ " <?php" ,
73
+ " if ($_SERVER['REQUEST_METHOD'] === 'PUT') {" ,
74
+ " $n = file_get_contents('php://input');" ,
75
+ " echo escapeshellarg($n);" ,
76
+ " } else {" ,
77
+ " echo \" This script only handles PUT requests.\" ;" ,
78
+ " }",
79
+ " ?>" ,
80
+ };
63
81
elseif strcmp(name , " pass_args_patch" ) == 1
64
- octave_php_wrapper_example_string = pass_args_patch();
82
+ text_cell = {
83
+ " <?php" ,
84
+ " if ($_SERVER['REQUEST_METHOD'] === 'PATCH') {" ,
85
+ " $n = file_get_contents('php://input');" ,
86
+ " echo escapeshellarg($n);" ,
87
+ " } else {" ,
88
+ " echo \" This script only handles PATCH requests.\" ;" ,
89
+ " }",
90
+ " ?>" ,
91
+ };
65
92
elseif strcmp(name , " pass_args_delete" ) == 1
66
- octave_php_wrapper_example_string = pass_args_delete();
93
+ text_cell = {
94
+ " <?php" ,
95
+ " if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {" ,
96
+ " parse_str($_SERVER['QUERY_STRING'], $queryParams);" ,
97
+ " if (isset($queryParams['n'])) {" ,
98
+ " $n = $queryParams['n'];" ,
99
+ " echo escapeshellarg($n);" ,
100
+ " } else {" ,
101
+ " echo \" Param n should not be null.\" ;" ,
102
+ " }",
103
+ " } else {" ,
104
+ " echo \" This script only handles DELETE requests.\" ;" ,
105
+ " }",
106
+ " ?>" ,
107
+ };
67
108
elseif strcmp(name , " jsondecode" ) == 1
68
- octave_php_wrapper_example_string = jsondecode();
109
+ text_cell = {
110
+ " args = argv();" ,
111
+ " n = jsondecode(args(1));" ,
112
+ " disp(sum(n));" ,
113
+ };
69
114
elseif strcmp(name , " jsonencode" ) == 1
70
- octave_php_wrapper_example_string = jsonencode();
115
+ text_cell = {
116
+ " n = [1,2,3,4,5,6,7];" ,
117
+ " disp(jsonencode(n));" ,
118
+ };
71
119
elseif strcmp(name , " base64_decode" ) == 1
72
- octave_php_wrapper_example_string = base64_decode();
120
+ text_cell = {
121
+ " args = argv();" ,
122
+ " n = base64_decode(args(1));" ,
123
+ " disp(sum(n));" ,
124
+ };
73
125
elseif strcmp(name , " base64_encode" ) == 1
74
- octave_php_wrapper_example_string = base64_encode();
126
+ text_cell = {
127
+ " n = [1,2,3,4,5,6,7];" ,
128
+ " disp(base64_encode(n));" ,
129
+ };
75
130
elseif strcmp(name , " upload_file" ) == 1
76
- octave_php_wrapper_example_string = upload_file();
131
+ text_cell = {
132
+ " <?php" ,
133
+ " if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {" ,
134
+ " $fileTmpPath = $_FILES['fileToUpload']['tmp_name'];" ,
135
+ " $fileName = $_FILES['fileToUpload']['name'];" ,
136
+ " $fileSize = $_FILES['fileToUpload']['size'];" ,
137
+ " $fileType = $_FILES['fileToUpload']['type'];" ,
138
+ " $fileNameCmps = explode(\" .\" , $fileName);" ,
139
+ " $fileExtension = strtolower(end($fileNameCmps));" ,
140
+ " $allowedfileExtensions = array('jpg', 'jpeg', 'png', 'gif');" ,
141
+ " if (in_array($fileExtension, $allowedfileExtensions)) {" ,
142
+ " $uploadFileDir = 'uploads/';" ,
143
+ " if (!is_dir($uploadFileDir)) {" ,
144
+ " mkdir($uploadFileDir, 0777, true);" ,
145
+ " }" ,
146
+ " $dest_path = $uploadFileDir . $fileName;" ,
147
+ " if(move_uploaded_file($fileTmpPath, $dest_path)) {" ,
148
+ " echo \" File is valid and was successfully uploaded.\n\" ;" ,
149
+ " } else {" ,
150
+ " echo \" There was some error moving the file to upload directory. Please make sure the upload directory is writable.\" ;" ,
151
+ " }",
152
+ " } else {" ,
153
+ " echo \" Sorry, only JPG, JPEG, PNG & GIF files are allowed.\" ;" ,
154
+ " }",
155
+ " } else {" ,
156
+ " echo \" No file was uploaded. Sorry, your file was not uploaded.\" ;" ,
157
+ " }",
158
+ " ?>" ,
159
+ };
77
160
elseif strcmp(name, "download_file") == 1
78
- octave_php_wrapper_example_string = download_file();
161
+ text_cell = {
162
+ " <?php" ,
163
+ " $filePath = 'path/to/your/file.zip';" ,
164
+ " if (!file_exists($filePath)) {" ,
165
+ " die('File not found');" ,
166
+ " }" ,
167
+ " $fileSize = filesize($filePath);" ,
168
+ " header('Content-Description: File Transfer');" ,
169
+ " header('Content-Type: application/octet-stream');" ,
170
+ " header('Content-Disposition: attachment; filename=\" ' . basename($filePath) . '\" ');" ,
171
+ " header('Content-Transfer-Encoding: binary');" ,
172
+ " header('Expires: 0');" ,
173
+ " header('Cache-Control: must-revalidate');" ,
174
+ " header('Pragma: public');" ,
175
+ " header('Content-Length: ' . $fileSize);" ,
176
+ " ob_clean();" ,
177
+ " flush();" ,
178
+ " readfile($filePath);" ,
179
+ " exit;" ,
180
+ " ?>" ,
181
+ };
79
182
else
80
183
error(' name must be one of the supported examples.' );
184
+ endif
81
185
octave_php_wrapper_example_string = strjoin(text_cell , " \n" );
82
186
endfunction
0 commit comments