File: ck_web.php

package info (click to toggle)
python-ck 1.9.4-1.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,004 kB
  • sloc: python: 6,505; php: 344; sh: 34; makefile: 3
file content (254 lines) | stat: -rw-r--r-- 5,267 bytes parent folder | download | duplicates (3)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/*

#
# Collective Knowledge (CK web service)
#
# See CK LICENSE.txt for licensing details
# See CK COPYRIGHT.txt for copyright details
#
# Developer: Grigori Fursin, 2014-2015
#

*/

 # initalize path to CK
 $ckr=getenv("CK_ROOT"); 
 if ($ckr=="") $ckr=getcwd();

 # Load configuration 
 $pcfg=$ckr . '/ck/repo/module/web/.cm/meta.json';

 $cfg=array();

 if (!file_exists($pcfg)) 
 {
   $er="Internal CK web service error (Can\'t find meta.json)";
   if ($xt=='json')
   {
     header("content-type: application/json; charset=UTF-8"); 
     print '{"return":1, "error":"'.$er.'"}';
   }
   else
   {
     header("content-type: text/html; charset=UTF-8"); 
     print $er;
   }
   return;
 }

 try
 {
    $x=file_get_contents($pcfg);
    $cfg=json_decode($x,true);
 } 
 catch(Exception $e)
 {
   $er="Internal CK web service error (can't parse web meta.json)";
   if ($xt=='json')
   {
     header("content-type: application/json; charset=UTF-8"); 
     print '{"return":1, "error":"'.$er.'"}';
   }
   else
   {
     header("content-type: text/html; charset=UTF-8"); 
     print $er;
   }
   exit(1);
 }

 # Load OpenME for CK
 $om=$ckr . '/ck/repo/module/web/php/openme.php';

 # Check if library exists
 if (!file_exists($om)) {
   $er="Internal CK web service error (Can\'t find openme.php)";
   if ($xt=='json')
   {
     header("content-type: application/json; charset=UTF-8"); 
     print '{"return":1, "error":"'.$er.'"}';
   }
   else
   {
     header("content-type: text/html; charset=UTF-8"); 
     print $er;
   }
   return;
 }
 else
   require_once $om;

 # Get web environment variables
 $session=$_SESSION;
 $cookie=$_COOKIE;
 $get=openme_web_to_array($_GET, "");
 $post=openme_web_to_array($_POST, "");
 $files=openme_web_file_to_array($_FILES, "");

 $ii=$get;

 $ii=array_merge($ii, $files);

 $o="";

 # Process ck_json
 $ii=array_merge($ii, $post);

 if (array_key_exists("ck_json", $ii) && $ii['ck_json']!='')
 {
   try
   {
     $jd=json_decode(urldecode($ii["ck_json"]));//,TRUE);
   } 
   catch(Exception $e)
   {
     openme_web_err($cfg, $xt, 1, $e->getMessage());
     exit(1);
   }

   unset($ii["ck_json"]);
   $ii=array_merge($ii, get_object_vars($jd));
 }

 # Check action
 $act='';
 if (array_key_exists("action", $ii)) $act=$ii['action'];

 # Check output type
 if (array_key_exists("out", $ii) && $ii['out']!='') $xt=$ii['out'];

 if ($xt=='') $xt='web';

 if ($xt!='json' && $xt!='con' && $xt!='web')
 {
    header("content-type: text/html; charset=UTF-8"); 
    print 'Unknown CK request ('.$xt.')!';
    return;
 }

 # Set output to tmp file
 $fn=tempnam("", "ck-");
 if ($xt!='json' && $act!='pull')
   $ii['web']='yes';
 
 $ii['out_file']=$fn;
 $ii['web']='yes';
 if ($xt=='json' or $xt=='web')
    $ii['out']='json_file';
 # else output to console (for remote access for example)

 $ii['con_encoding']='utf8';

 # Call CK ***************************************************************************
 if (!array_key_exists('action', $ii))
 {
   if (array_key_exists("if_web_action_not_defined", $cfg) &&
       array_key_exists("if_web_module_not_defined", $cfg))
   {
     $ii["action"]=$cfg["if_web_action_not_defined"];
     $ii["module_uoa"]=$cfg["if_web_module_not_defined"];
   } 
 }

 $r=openme_ck_access($ii, false);

 # Process output
 if ($r["return"]>0)
 {
   if (file_exists($fn)) unlink($fn);
   openme_web_err($cfg, $xt, $r['return'], $r['error']);
   exit(1);
 }

 # If output to console or detached console
 if ($xt=='con')
 {
   if (file_exists($fn)) unlink($fn);
   openme_web_out($cfg, $xt, $r['std'], '');
   return;
 }

 # If json or web
 # Try to load output file
 if (!file_exists($fn))
 {
   openme_web_err($cfg, $xt, 1, 'Output json file was not created, see output ('.$r['std'].')');
   return;
 } 

 $bin=file_get_contents($fn);
 unlink($fn);

 if ($bin=='' && array_key_exists("stderr", $r) && $r["stderr"]!="")
 {
   $x=', see error output ('.$r['stderr'].')';
   openme_web_err($cfg, $xt, $r['return'], 'returned file is empty'.$x);
   exit(1);
 }

 # Output
 $fx='';

 try
 {
    $rr=json_decode($bin,true);
 } 
 catch(Exception $e)
 {
   $er="Can't parse output json file (".$e->getMessage().")";
   openme_web_err($cfg, $xt, 1, $er);
   exit(1);
 }

 if ($rr['return']>0)
 {
   openme_web_err($cfg, $xt, $rr['return'], $rr['error']);
   exit(1);
 }

 # Check if file was returned
 $fr=false;
 if (array_key_exists("file_content_base64", $rr) &&
     array_key_exists("filename", $rr) &&
     $rr['filename']!='')
   $fr=true;

 # Check if download
 if (($xt=='web' && $fr) || ($act=='pull' && $xt!='json'))
 {
   if (array_key_exists("file_content_base64", $rr))
     $x=$rr['file_content_base64'];

   $fx='';

   if (array_key_exists("filename", $rr))
     $fx=$rr['filename'];

   if ($fx=='')
      $fx='ck-archive.zip';

   try
   {
     $bin=urlsafe_b64decode($x);
   } 
   catch(Exception $e)
   {
     $er="Internal CK web service error (".$e->getMessage().")";
     openme_web_err($cfg, $xt, 1, $er);
     exit(1);
   }

   # Process extension
   $xt = pathinfo($fx, PATHINFO_EXTENSION);
 }
 else
 {
   # If html mode and output file is empty, use stdout from module ...
   if (array_key_exists("html", $rr))
      $bin=$rr['html'];
 }

 openme_web_out($cfg, $xt, $bin, $fx);

?>