PageRenderTime 471ms CodeModel.GetById 161ms app.highlight 163ms RepoModel.GetById 138ms app.codeStats 1ms

/upload/inc/misc.php

https://github.com/AKSW/geolift-webbuilder
PHP | 657 lines | 532 code | 96 blank | 29 comment | 177 complexity | a67fba16a90d405b64c256f3d791bffb MD5 | raw file
  1<?php
  2/*
  3 * Copyright 2014 Alrik Hausdorf and Eugen Rein
  4 *
  5 * Licensed under the Apache License, Version 2.0 (the "License");
  6 * you may not use this file except in compliance with the License.
  7 * You may obtain a copy of the License at
  8 *
  9 *    http://www.apache.org/licenses/LICENSE-2.0
 10 *
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 */
 17global $base_dir;
 18if (!isset($_REQUEST['function']))
 19    die('{"error":"no function selected"}');
 20
 21if ($_REQUEST['function'] == "getDatasets") {
 22    if (!isset($_REQUEST['user']))
 23        die('{"error":"user-field required"}');
 24    if (trim($_REQUEST['user']) == "")
 25        die('{"error":"user-field required"}');
 26    $user_dir = $base_dir . md5($_REQUEST['user']) . DIRECTORY_SEPARATOR;
 27    $results_array = array();
 28    foreach (glob($user_dir . '*.json') as $file) {
 29//        echo $file;
 30        if (!endsWith($file, "user.json")) {
 31            $result1 = json_decode(file_get_contents($file));
 32            $result1->jobs = array();
 33            foreach (glob($user_dir . $result1->input . DIRECTORY_SEPARATOR . '*.job') as $file1) {
 34                $json = json_decode(file_get_contents($file1));
 35                $json->file = substr(strrchr($file1, DIRECTORY_SEPARATOR), 1);
 36                $result1->jobs[] = $json;
 37            }
 38
 39            $results_array[] = $result1;
 40        }
 41    }
 42    echo(json_encode($results_array));
 43} elseif ($_REQUEST['function'] == "login") {
 44    if (!isset($_REQUEST['user']))
 45        die('{"error":"user-field required"}');
 46    if (trim($_REQUEST['user']) == "")
 47        die('{"error":"user-field required"}');
 48    $user_dir = $base_dir . md5($_REQUEST['user']) . DIRECTORY_SEPARATOR;
 49
 50    if (is_dir($user_dir)) {
 51        if (is_file($user_dir . DIRECTORY_SEPARATOR . "user.json")) {
 52            setJsonResponse();
 53            $json = file_get_contents($user_dir . "user.json");
 54            echo($json);
 55            die('');
 56        }
 57    } else {
 58        mkdir($user_dir);
 59        $json = array('mail' => $_REQUEST['user']);
 60        file_put_contents($user_dir . "user.json", json_encode($json));
 61        echo(json_encode($json));
 62    }
 63} elseif ($_REQUEST['function'] == "getSchema") {
 64    setJsonResponse();
 65    $json = shell_exec(GEOLIFT_SCHEMA_COMMAND);
 66    echo($json);
 67    die('');
 68} elseif ($_REQUEST['function'] == "saveData") {
 69    setJsonResponse();
 70    if (!isset($_REQUEST['user']))
 71        die('{"error":"user-field required"}');
 72    if (trim($_REQUEST['user']) == "")
 73        die('{"error":"user-field required"}');
 74    if (!isset($_REQUEST['file']))
 75        die('{"error":"file-field required"}');
 76    if (trim($_REQUEST['file']) == "")
 77        die('{"error":"file-field required"}');
 78    if (!isset($_REQUEST['data']))
 79        die('{"error":"data-field required"}');
 80    if (trim($_REQUEST['data']) == "")
 81        die('{"error":"data-field required"}');
 82    $file_dir = $base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR;
 83    if (isset($_REQUEST['job'])) {
 84        $newFilename = $_REQUEST['job'];
 85        unlink($file_dir . $newFilename);
 86    } else {
 87        $newFilename = "query_0.job";
 88        $run = 0;
 89        while (is_dir($file_dir . $newFilename) or is_file($file_dir . $newFilename)) {
 90            $newFilename = "query_" . $run++ . ".job";
 91        }
 92    }
 93    file_put_contents($file_dir . $newFilename, urldecode($_REQUEST['data']));
 94    $return = array(
 95        'success' => "Saved.");
 96
 97    //create config file
 98    $file_dir = realpath($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
 99    $file = $file_dir . ($newFilename);
100
101    $input_file_name = $file_dir . 'input.ttl';
102
103    $config_file_name = str_replace('.job', '.ttl', $newFilename);
104    $config_file_name = $file_dir . $config_file_name;
105    $config_file_handler = @fopen($config_file_name, 'wb');
106
107    $ouput_file_name = str_replace('.job', '.out', $newFilename);
108    $ouput_file_name = $file_dir . $ouput_file_name;
109
110    $json = file_get_contents($file);
111    $query = json_decode($json, true);
112    
113    if (!$query) {
114        die('{"error": "Job config can not be parsed."}');
115    }
116    if (!isset($query['job'])) {
117        die('{"error": "Job config can not be parsed."}');
118    }
119    if (!$query['job']) {
120        die('{"error": "Job config can not be parsed."}');
121    }
122    if (FALSE === $config_file_handler) {
123        die('{"error": "Job config can not be written."}');
124    }
125
126    $user_file_dir = realpath($base_dir . md5($_REQUEST['user'])) . DS . ($_REQUEST['file']) . '.json';
127
128    $Filejson = file_get_contents($user_file_dir);
129    $Filequery = json_decode($Filejson);
130
131    if (!$Filequery->url) {
132        if (is_dir($file) || !is_file($file)) {
133            die('{"error":"Job not found."}');
134        }
135        if (is_dir($input_file_name) || !is_file($input_file_name)) {
136            die('{"error":"Job not found."}');
137        }
138
139        $input_file_name = $input_file_name;
140
141    } else {
142        $input_file_name = $Filequery->filename;
143    }
144
145    $config_elements = $query['job'];
146    
147    $dataset_counter = 1;
148    $parameter_counter = 1;
149
150    $end = null;
151    $next = array();
152
153    $config_data = array();
154    $datasets = array();
155    $parameters = array();
156
157    foreach ($config_elements as $key => $element) {
158        if ('start' == $element['name']) {
159            $name = ':d' . $dataset_counter;
160            $dataset_counter++;
161
162            $next_id = $element['next'];
163            $next[] = $next_id;
164            
165            $config_data[$next_id] = array(
166                ':hasInput' => array(':d1')
167            );
168            if(!$Filequery->url) {
169                $datasets[':d1'] = array(
170                    'a' => ':Dataset',
171                    ':inputFile' => '"' . $input_file_name . '"'
172                );
173            } else {
174                $datasets[':d1'] = array(
175                    'a' => ':Dataset',
176                    ':hasUri' => '<' . $input_file_name . '>',
177                    ':FromEndPoint' => '<' . $Filequery->endpoint . '>'
178                );
179            }
180
181            break;
182        }
183    }
184
185    foreach ($config_elements as $key => $element) {
186        if ('end' == $element['name']) {
187            $end = $element;
188            break;
189        }
190    }
191
192    while(!empty($next)) {
193        $next_id = array_shift($next);
194
195        foreach ($config_elements as $key => $element) {
196            if($element['id'] == $next_id && $end['id'] !== $next_id) {
197                
198                $parameter_type_prefix = ucfirst($element['name']);
199                $config_data_type_prefix = ucfirst($element['name']);
200                $config_data_label_prefix = ucfirst($element['name']);
201                if('nlp' == $element['name']) {
202                    $parameter_type_prefix = strtoupper($element['name']);
203                    $config_data_type_prefix = strtoupper($element['name']);
204                    $config_data_label_prefix = strtoupper($element['name']);
205                }
206
207                $parameter_name = ':' . $element['name'] . ucfirst($element['type']) . 'Parameter';
208                $parameter_type = ':' . ucfirst($element['type']) . 'Parameter, ' . ':' . $parameter_type_prefix . ucfirst($element['type']) . 'Parameter';
209                $config_data_name = ':' . $element['name'] . $element['id'];
210                $config_data_type = ':' . ucfirst($element['type']) . ', ' . ':' . $config_data_type_prefix . ucfirst($element['type']);
211                $config_data_label = '"' . $config_data_label_prefix . ' ' . ucfirst($element['type']) . '"';
212
213                $next_element_ids = array();
214                if(is_array($element['next'])) {
215                    $next_element_ids = $element['next'];
216                } else if(null != $element['next']) {
217                    $next_element_ids[] = $element['next'];
218                }
219
220                if(!isset($config_data[$next_id])) {
221                    $config_data[$next_id] = array();
222                }
223
224                if(!isset($config_data[$next_id]['name'])) {
225                    $config_data[$next_id]['name'] = $config_data_name;
226                }
227
228                if(!isset($config_data[$next_id]['a'])) {
229                    $config_data[$next_id]['a'] = $config_data_type;
230                }
231
232                if(!isset($config_data[$next_id]['rdfs:label'])) {
233                    $config_data[$next_id]['rdfs:label'] = $config_data_label;
234                }
235
236                foreach ($next_element_ids as $next_element_id) {
237                    $name = ':d' . $dataset_counter;
238                    $dataset_counter++;
239
240                    if($next_element_id != $end['id']) {
241                        if(!isset($config_data[$next_element_id])) {
242                            $config_data[$next_element_id] = array();
243                        }
244
245                        if(!isset($config_data[$next_element_id][':hasInput'])) {
246                            $config_data[$next_element_id][':hasInput'] = array();
247                        }
248
249                        $config_data[$next_element_id][':hasInput'][] = $name;
250
251                        $datasets[$name] = array(
252                            'a' => ':Dataset'
253                        );
254                    }
255
256                    if(!in_array($next_element_id, $next)) {
257                        $next[] = $next_element_id;
258                    }
259
260                    if(!isset($config_data[$next_id][':hasOutput'])) {
261                        $config_data[$next_id][':hasOutput'] = array();
262                    }
263
264                    $config_data[$next_id][':hasOutput'][] = $name;
265
266                    if($end['id'] == $next_element_id) {
267                        $datasets[$name] = array(
268                            'a' => ':Dataset',
269                            ':outputFile' => '"' . $ouput_file_name . '"',
270                            ':outputFormat' => '"Turtle"'
271                        );
272                    } else {
273                        $datasets[$name] = array(
274                            'a' => ':Dataset'
275                        );
276                    }
277                }
278
279                if(isset($element['properties'])) {
280                    if(!isset($config_data[$next_id][':hasParameter'])) {
281                        $config_data[$next_id][':hasParameter'] = array();
282                    }
283
284                    if('dereferencing' == $element['name']) {
285                        if(isset($element['properties']['predicate'], $element['properties']['predicate value'])) {
286                            $parameter_key = $element['properties']['predicate'];
287                            $parameter_value = $element['properties']['predicate value'];
288
289                            $element['properties'][$parameter_key] = $parameter_value;
290                        }
291                        unset($element['properties']['predicate']);
292                        unset($element['properties']['predicate value']);
293                    }
294
295                    foreach ($element['properties'] as $parameter_key => $parameter_value) {
296                        //simple match against a URI
297                        if(!preg_match('@^\s*([^:]+://.*)$@', $parameter_value, $matches) && false !== strpos($parameter_value, ':')) {
298                            $parameter_value = trim($parameter_value);
299                            $parameter['a'] = $parameter_type;
300                            $parameter[':hasKey'] = '"' . $parameter_key . '"';
301                            $parameter[':hasValue'] = $parameter_value;
302                        } else if (is_bool($parameter_value)) {
303                            $parameter_value = ($parameter_value ? 'true' : 'false');
304                            
305                            $parameter_value = trim($parameter_value);
306                            $parameter['a'] = $parameter_type;
307                            $parameter[':hasKey'] = '"' . $parameter_key . '"';
308                            $parameter[':hasValue'] = $parameter_value;
309                        } else {
310                            $parameter_value = trim($parameter_value);
311                            $parameter['a'] = $parameter_type;
312                            $parameter[':hasKey'] = '"' . $parameter_key . '"';
313                            $parameter[':hasValue'] = '"' . $parameter_value . '"';
314                        }
315                        
316                        $config_data[$next_id][':hasParameter'][] = $parameter_name . $parameter_counter;
317                        $parameters[$parameter_name . $parameter_counter] = $parameter;
318                        $parameter_counter++;
319                    }
320                }
321            }
322        }
323    }
324
325    //create config file
326    //write header
327    $newline = PHP_EOL;
328    $lines = array(
329        "@prefix : <http://geoknow.org/specsontology/> .{$newline}",
330        "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .{$newline}",
331        "@prefix geo: <http://w3.org/2003/01/geo/wgs84_pos#> .{$newline}"
332    );
333    foreach ($lines as $line) {
334        fwrite($config_file_handler, $line);
335    }
336
337    //write datasets
338    foreach ($datasets as $name => $dataset) {
339        $i = 0;
340        foreach ($dataset as $key => $value) {
341            $separator = ';';
342            $pre = '';
343            if($i >= count($dataset) - 1) {
344                $separator = '.';
345            }
346
347            if($i == 0) {
348                $pre = $name;
349            }
350
351            $line = "{$pre} {$key} {$value} {$separator}{$newline}";
352            fwrite($config_file_handler, $line);
353
354            $i++;
355        }
356    }
357
358    //write parameters
359    foreach ($parameters as $name => $parameter) {
360        $i = 0;
361        foreach ($parameter as $key => $value) {
362            $separator = ';';
363            $pre = '';
364            if($i >= count($parameter) - 1) {
365                $separator = '.';
366            }
367
368            if($i == 0) {
369                $pre = $name;
370            }
371
372            $line = "{$pre} {$key} {$value} {$separator}{$newline}";
373            fwrite($config_file_handler, $line);
374
375            $i++;
376        }
377    }
378
379    //write modules and operators
380    foreach ($config_data as $config_element) {
381        $name = $config_element['name'];
382        unset($config_element['name']);
383        $i = 0;
384        foreach ($config_element as $key => $value) {
385            $separator = ';';
386            $pre = '';
387            if($i >= count($config_element) - 1) {
388                $separator = '.';
389            }
390
391            if($i == 0) {
392                $pre = $name;
393            }
394
395            if(is_array($value)) {
396                $value = implode(', ', $value);
397            }
398
399            $line = "{$pre} {$key} {$value} {$separator}{$newline}";
400            fwrite($config_file_handler, $line);
401
402            $i++;
403        }
404    }
405    fclose($config_file_handler);
406
407    echo(json_encode($return));
408} elseif ($_REQUEST['function'] == "removeJob") {
409    setJsonResponse();
410    if (!isset($_REQUEST['user']))
411        die('{"error":"user-field required"}');
412    if (trim($_REQUEST['user']) == "")
413        die('{"error":"user-field required"}');
414    if (!isset($_REQUEST['file']))
415        die('{"error":"file-field required"}');
416    if (trim($_REQUEST['file']) == "")
417        die('{"error":"file-field required"}');
418    if (!isset($_REQUEST['job']))
419        die('{"error":"job-field required"}');
420    if (trim($_REQUEST['job']) == "")
421        die('{"error":"job-field required"}');
422    $file_dir = $base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR;
423    $newFilename = $_REQUEST['job'];
424    unlink($file_dir . $newFilename);
425
426    $return = array(
427        'success' => "Removed.");
428    echo(json_encode($return));
429} elseif ($_REQUEST['function'] == "removeFile") {
430    setJsonResponse();
431    if (!isset($_REQUEST['user']))
432        die('{"error":"user-field required"}');
433    if (trim($_REQUEST['user']) == "")
434        die('{"error":"user-field required"}');
435    if (!isset($_REQUEST['file']))
436        die('{"error":"file-field required"}');
437    if (trim($_REQUEST['file']) == "")
438        die('{"error":"file-field required"}');
439    $file_dir = $base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR;
440    unlink($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . '.json');
441    deleteDir($file_dir);
442
443    $return = array(
444        'success' => "Removed.");
445    echo(json_encode($return));
446} elseif ($_REQUEST['function'] == "loadJob") {
447    setJsonResponse();
448    if (!isset($_REQUEST['user']))
449        die('{"error":"user-field required"}');
450    if (trim($_REQUEST['user']) == "")
451        die('{"error":"user-field required"}');
452    if (!isset($_REQUEST['file']))
453        die('{"error":"file-field required"}');
454    if (trim($_REQUEST['file']) == "")
455        die('{"error":"file-field required"}');
456    if (!isset($_REQUEST['job']))
457        die('{"error":"job-field required"}');
458    if (trim($_REQUEST['job']) == "")
459        die('{"error":"job-field required"}');
460    $file = $base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR . ($_REQUEST['job']);
461    if (!is_dir($file) && is_file($file)) {
462        $json = file_get_contents($file);
463        echo($json);
464        die('');
465    } else {
466        die('{"error":"Job not found."}');
467    }
468} elseif ($_REQUEST['function'] == 'runJob') {
469    setJsonResponse();
470    $success = array('success' => true);
471
472    if (!isset($_REQUEST['user']))
473        die('{"error":"user-field required"}');
474    if (trim($_REQUEST['user']) == "")
475        die('{"error":"user-field required"}');
476    if (!isset($_REQUEST['file']))
477        die('{"error":"file-field required"}');
478    if (trim($_REQUEST['file']) == "")
479        die('{"error":"file-field required"}');
480    if (!isset($_REQUEST['job']))
481        die('{"error":"job-field required"}');
482    if (trim($_REQUEST['job']) == "")
483        die('{"error":"job-field required"}');
484
485    $file_dir = realpath($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
486    $file = $file_dir . ($_REQUEST['job']);
487
488    $input_file_name = $file_dir . 'input.ttl';
489
490    $config_file_name = str_replace('.job', '.ttl', $_REQUEST['job']);
491    $config_file_name = $file_dir . $config_file_name;
492
493    $ouput_file_name = str_replace('.job', '.out', $_REQUEST['job']);
494    $ouput_file_name = $file_dir . $ouput_file_name;
495
496    $pid_file_name = str_replace('.job', '.pid', $_REQUEST['job']);
497    $pid_file_name = $file_dir . $pid_file_name;
498
499    $log_file_name = str_replace('.job', '.log', $_REQUEST['job']);
500    $log_file_name = $file_dir . $log_file_name;
501
502    $json = file_get_contents($file);
503    $query = json_decode($json, true);
504    if (!$query) {
505        die('{"error": "Job config can not be parsed."}');
506    }
507    if (!isset($query['job'])) {
508        die('{"error": "Job config can not be parsed."}');
509    }
510    if (!$query['job']) {
511        die('{"error": "Job config can not be parsed."}');
512    }
513
514    $user_file_dir = realpath($base_dir . md5($_REQUEST['user'])) . DS . ($_REQUEST['file']) . '.json';
515
516    $Filejson = file_get_contents($user_file_dir);
517    $Filequery = json_decode($Filejson);
518    $query['state'] = 'running';
519    file_put_contents($file, json_encode($query));
520
521    if (!$Filequery->url) {
522        if (is_dir($file) || !is_file($file)) {
523            die('{"error":"Job not foundd."}');
524        }
525        if (is_dir($input_file_name) || !is_file($input_file_name)) {
526            die('{"error":"Job not found."}');
527        }
528
529        $input_file_name = $input_file_name;
530
531    } else {
532        $input_file_name = $Filequery->filename;
533    }
534
535    if (is_dir($ouput_file_name)) {
536        die('{"error":"Output file can not be written."}');
537    }
538    if (is_file($ouput_file_name) && !is_writable($ouput_file_name)) {
539        die('{"error":"Output file can not be written."}');
540    }
541    if (is_dir($pid_file_name)) {
542        die('{"error":"PID file can not be written."}');
543    }
544    if (is_file($pid_file_name) && !is_writable($pid_file_name)) {
545        die('{"error":"PID file can not be written."}');
546    }
547     if (is_dir($config_file_name)) {
548        die('{"error":"Config file is not readable."}');
549    }
550    if (is_file($config_file_name) && !is_readable($config_file_name)) {
551        die('{"error":"Config file is not readable."}');
552    }
553
554    //create output and log files if they don't exists already
555    touch($ouput_file_name);
556
557    @unlink($log_file_name);
558    touch($log_file_name);
559
560    //prepare geolift command
561    $command = str_replace('{{CONFIG_FILE}}', $config_file_name, GEOLIFT_RUN_COMMAND);
562    //start geolift in separate process, suppress all outputs and save PID to pid file
563    $command = "nohup {$command} > {$log_file_name} 2>&1 ".PHP_EOL;
564
565    $command.= "php " . GEOLIFT_BASE_PATH . DS . "upload" . DS . "inc" . DS . "sendMail.php " . md5(($_REQUEST['user'])) . " " . (($_REQUEST['file'])) . " " . (($_REQUEST['job'])) . " ";
566    //write SH for command and run it
567    $run_file_name = str_replace('.job', '.sh', $_REQUEST['job']);
568
569    $run_file_name = $file_dir . $run_file_name;
570    file_put_contents($run_file_name, $command);
571
572    exec("sh $run_file_name & echo $!", $out, $return_var);
573
574    //$return_var = 0 : exec started command successfully
575    //$out[0] : contains PID
576    if (0 === $return_var && isset($out[0])) {
577        file_put_contents($pid_file_name, $out[0]);
578        echo json_encode($success);
579        die('');
580    }
581
582    die('{"error":"Unknown error occurred."}');
583} elseif ($_REQUEST['function'] == 'getOutput') {
584    setJsonResponse();
585    $success = array('success' => true);
586
587    if (!isset($_REQUEST['user']))
588        die('{"error":"user-field required"}');
589    if (trim($_REQUEST['user']) == "")
590        die('{"error":"user-field required"}');
591    if (!isset($_REQUEST['file']))
592        die('{"error":"file-field required"}');
593    if (trim($_REQUEST['file']) == "")
594        die('{"error":"file-field required"}');
595    if (!isset($_REQUEST['job']))
596        die('{"error":"job-field required"}');
597    if (trim($_REQUEST['job']) == "")
598        die('{"error":"job-field required"}');
599
600    $ouput_file_name = str_replace('.job', '.out', $_REQUEST['job']);
601
602    $file_dir = realpath($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
603    $file = $file_dir . ($ouput_file_name);
604    header("Content-disposition: attachment; filename=" . $ouput_file_name);
605    readfile($file);
606} elseif ($_REQUEST['function'] == 'getLogOutput') {
607    setJsonResponse();
608    $success = array('success' => true);
609
610    if (!isset($_REQUEST['user']))
611        die('{"error":"user-field required"}');
612    if (trim($_REQUEST['user']) == "")
613        die('{"error":"user-field required"}');
614    if (!isset($_REQUEST['file']))
615        die('{"error":"file-field required"}');
616    if (trim($_REQUEST['file']) == "")
617        die('{"error":"file-field required"}');
618    if (!isset($_REQUEST['job']))
619        die('{"error":"job-field required"}');
620    if (trim($_REQUEST['job']) == "")
621        die('{"error":"job-field required"}');
622
623    $ouput_file_name = str_replace('.job', '.log', $_REQUEST['job']);
624
625    $file_dir = realpath($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
626    $file = $file_dir . ($ouput_file_name);
627    header("Content-disposition: attachment; filename=" . $ouput_file_name);
628    readfile($file);
629} elseif ($_REQUEST['function'] == 'getConfig') {
630    setJsonResponse();
631    $success = array('success' => true);
632
633    if (!isset($_REQUEST['user']))
634        die('{"error":"user-field required"}');
635    if (trim($_REQUEST['user']) == "")
636        die('{"error":"user-field required"}');
637    if (!isset($_REQUEST['file']))
638        die('{"error":"file-field required"}');
639    if (trim($_REQUEST['file']) == "")
640        die('{"error":"file-field required"}');
641    if (!isset($_REQUEST['job']))
642        die('{"error":"job-field required"}');
643    if (trim($_REQUEST['job']) == "")
644        die('{"error":"job-field required"}');
645
646    $config_file_name = str_replace('.job', '.ttl', $_REQUEST['job']);
647    $file_dir = realpath($base_dir . md5(($_REQUEST['user'])) . DIRECTORY_SEPARATOR . ($_REQUEST['file']) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
648    $file = $file_dir . ($config_file_name);
649
650    if (!file_exists($file)) {
651        die('{"error":"Config file could not be found"}');
652    }
653
654    header("Content-disposition: attachment; filename=" . $config_file_name);
655    readfile($file);
656}
657?>