function auth_ok($config = array())
 {
     $ok = FALSE;
     // check for configuration problem
     if (!$config) {
         $config = config_get();
     }
     if (!config_error($config)) {
         // check for aleady signed in
         $uid = auth_userid();
         $ok = !!$uid;
         if ($ok) {
             // we found a username, check for password
             switch ($config['authentication']) {
                 case '':
                     break;
                     // any password is okay, just look for user
                 // any password is okay, just look for user
                 default:
                     $ok = FALSE;
                     if (is_string($config['authentication'])) {
                         $password = http_get_contents($config['authentication']);
                         $supplied = empty($_SERVER['PHP_AUTH_PW']) ? '' : $_SERVER['PHP_AUTH_PW'];
                         $ok = $password && $supplied && $password == $supplied;
                     }
             }
         }
     }
     return $ok;
 }
if (!$err) {
    // pull in configuration so we can log other errors
    $config = config_get();
    $err = config_error($config);
    $log_responses = $config['log_response'];
}
if (!$err) {
    // see if the user is authenticated (does not redirect or exit)
    if (!auth_ok()) {
        $err = 'Unauthenticated access';
    }
}
if (!$err) {
    // make sure required parameters have been set
    $id = empty($_REQUEST['id']) ? '' : $_REQUEST['id'];
    $uid = auth_userid();
    $type = empty($_REQUEST['type']) ? '' : $_REQUEST['type'];
    $extension = empty($_REQUEST['extension']) ? '' : $_REQUEST['extension'];
    if (!($uid && $id && $extension && $type)) {
        $err = 'Required parameters omitted';
    }
}
if (!$err) {
    // make sure $_FILES populated
    if (empty($_FILES) || empty($_FILES['file'])) {
        $err = 'No file was uploaded';
    }
}
if (!$err) {
    // make sure file is valid
    $file = $_FILES['file'];
}
if (!$err) {
    $query = array();
    foreach ($_GET as $k => $v) {
        switch ($k) {
            case 'index':
            case 'count':
            case 'group':
            case 'unique':
                break;
            default:
                $query[$k] = $v;
        }
    }
    $options = array();
    $options['group'] = $group;
    $options['index'] = $index;
    $options['count'] = $count;
    $options['query'] = $query;
    $options['uid'] = auth_userid();
    $json_str = json_encode(data_search($options, $config));
}
if ($err) {
    log_file($err, $config);
    print '[]';
} else {
    print $json_str;
}
if (!empty($log_responses)) {
    log_file($json_str, $config);
}
}
if (!$err) {
    // pull in other configuration and check for required input
    if (!($php_input = file_get_contents('php://input'))) {
        $err = 'JSON payload required';
    } else {
        if (!($request = @json_decode($php_input, TRUE))) {
            $err = 'Could not parse JSON payload';
        }
    }
}
if (!$err) {
    if ($config['log_request']) {
        log_file(print_r($request, 1), $config);
    }
    $request['uid'] = auth_userid();
    $request['mime'] = empty($request['type']) ? '' : $request['type'];
    $request['type'] = '';
    $response = service_import_init($request, $config);
}
if ($err) {
    $response['error'] = $err;
}
if (empty($response['error'])) {
    $response['ok'] = 1;
}
$json = json_encode($response);
print $json . "\n\n";
if ($log_responses) {
    log_file($json, $config);
}
    if (!($id && $php_input)) {
        $err = 'Mash id and data required';
    }
}
if (!$err) {
    // check to make sure JSON data is parsable
    if (!($request = @json_decode($php_input, TRUE))) {
        $err = 'Could not parse JSON payload';
    }
}
if (!$err) {
    // make sure label was set
    $label = $request['label'];
    if (!$label) {
        $err = 'Could not determine mash label';
    }
}
if (!$err) {
    // save mash xml to local file
    $err = data_save_mash($request, auth_userid(), $config);
}
if ($err) {
    $response['error'] = $err;
} else {
    $response['ok'] = 1;
}
$json = json_encode($response);
print $json . "\n\n";
if (!empty($log_responses)) {
    log_file($json, $config);
}
    auth_challenge($config);
}
if (!$err) {
    $id = empty($_GET['id']) ? 0 : $_GET['id'];
    // see if we want requests logged, and potentially do so
    $log_responses = empty($config['log_response']) ? '' : $config['log_response'];
    if ($config['log_request']) {
        log_file($_SERVER['QUERY_STRING'], $config);
    }
    // make sure required parameters have been sent
    if (!$id) {
        $err = 'Parameter id required';
    }
}
if (!$err) {
    $mash_data = data_mash($id, auth_userid(), $config);
    if (!empty($mash_data['error'])) {
        $err = $mash_data['error'];
    } else {
        $response['data'] = $mash_data;
    }
}
if ($err) {
    $response['error'] = $err;
} else {
    $response['ok'] = 1;
}
$json = json_encode($response);
print $json . "\n\n";
if (!empty($log_responses)) {
    log_file($json, $config);
 function api_job_import($input = array(), $output = array(), $config = array())
 {
     $result = array('callbacks' => array(), 'inputs' => array(), 'outputs' => array());
     if (!$config) {
         $config = config_get();
     }
     $err = config_error($config);
     if (!$err) {
         // check for required input
         if (empty($input['uid'])) {
             $input['uid'] = auth_userid();
         }
         // make sure required input parameters have been set
         $id = empty($input['id']) ? '' : $input['id'];
         $extension = empty($input['extension']) ? '' : $input['extension'];
         $type = empty($input['type']) ? mime_type_from_extension($extension) : $input['type'];
         $label = empty($input['label']) ? $id : $input['label'];
         if (!($id && $extension && $type)) {
             $err = 'Required parameter omitted';
         }
     }
     if (!$err) {
         // create job for transcoder
         $input_tag = array('type' => $type);
         if ($type != 'audio') {
             $input_tag['fill'] = 'none';
         }
         // DESTINATION
         $result['destination'] = service_destination($input, $config);
         // CALLBACKS
         $progress_callback_payload = array('job' => '{job.id}', 'progress' => '{job.progress}');
         $complete_callback_payload = array('job' => '{job.id}', 'id' => $id, 'uid' => $input['uid'], 'extension' => $extension, 'type' => $type, 'label' => $label, 'error' => '{job.error}', 'log' => '{job.log}', 'commands' => '{job.commands}', 'no_audio' => '{job.inputs.0.no_audio}', 'no_video' => '{job.inputs.0.no_video}');
         if ('image' != $type) {
             $complete_callback_payload['duration'] = '{job.duration}';
         }
         if (!empty($output['include_progress'])) {
             $result['callbacks'][] = service_import_progress_callback($progress_callback_payload, $config);
         }
         $result['callbacks'][] = service_import_complete_callback($complete_callback_payload, $config);
         $input_tag['source'] = service_source($input, $config);
         $result['inputs'][] = $input_tag;
         // OUTPUTS
         if ($type == 'image') {
             // add output for image file
             $result['outputs'][] = array('type' => 'image', 'name' => '0', 'path' => $config['import_dimensions'] . 'x1', 'dimensions' => $config['import_dimensions'], 'extension' => $extension, 'quality' => $config['import_image_quality']);
         } else {
             // add output for audio/video file
             $result['outputs'][] = array('type' => 'audio', 'precision' => 0, 'audio_bitrate' => $config['import_audio_bitrate'], 'name' => $config['import_audio_basename'], 'extension' => $config['import_audio_extension'], 'audio_rate' => $config['import_audio_rate']);
             // add output for waveform file
             $result['outputs'][] = array('type' => 'waveform', 'forecolor' => $config['import_waveform_forecolor'], 'backcolor' => $config['import_waveform_backcolor'], 'name' => $config['import_waveform_basename'], 'dimensions' => $config['import_waveform_dimensions'], 'extension' => $config['import_waveform_extension']);
         }
         if ($type == 'video') {
             // add output for sequence files
             $result['outputs'][] = array('type' => 'sequence', 'video_rate' => $config['import_video_rate'], 'quality' => $config['import_image_quality'], 'extension' => $config['import_extension'], 'dimensions' => $config['import_dimensions'], 'path' => '{output.dimensions}x{output.video_rate}');
         }
     }
     if ($err) {
         $result['error'] = $err;
     }
     return $result;
 }