function auth_data($transfer = array(), $config = array())
 {
     if (!$config) {
         $config = config_get();
     }
     if (!config_error($config)) {
         // use HTTP authentication - eg. http://User:Pass@www.example.com/path/
         $transfer['user'] = empty($_SERVER['PHP_AUTH_USER']) ? '' : $_SERVER['PHP_AUTH_USER'];
         $transfer['pass'] = empty($_SERVER['PHP_AUTH_PW']) ? '' : $_SERVER['PHP_AUTH_PW'];
     }
     return $transfer;
 }
 function log_file($s, $config)
 {
     if ($s) {
         if (!$config) {
             $config = config_get();
         }
         if (!(config_error($config) || empty($config['log_file']))) {
             if (file_safe($config['log_file'], $config)) {
                 $prelog = date('H:i:s') . ' ';
                 $prelog .= basename($_SERVER['SCRIPT_NAME']);
                 $existed = file_exists($config['log_file']);
                 $fp = fopen($config['log_file'], 'a');
                 if ($fp) {
                     $s = $prelog . ' ' . $s . "\n";
                     fwrite($fp, $s, strlen($s));
                     fclose($fp);
                 }
                 if (!$existed) {
                     file_mode($config['log_file'], $config, TRUE);
                 }
             }
         }
     }
 }
 function service_export_module_source($config = array())
 {
     $result = array();
     if (!$config) {
         $config = config_get();
     }
     $err = config_error($config);
     // should wind up calling __service_load
     if (!$err) {
         $function_name = $config['file'] . '_file_export_module_source';
         if (function_exists($function_name)) {
             // optional method
             $result = $function_name($config);
         }
     }
     if ($err) {
         $result['error'] = $err;
     }
     return $result;
 }
If possible, the response to client is logged.
*/
$response = array();
$err = '';
$config = array();
$log_responses = '';
if (!@(include_once dirname(__FILE__) . '/include/loadutils.php')) {
    $err = 'Problem loading utility script';
}
if (!$err && !load_utils('auth', 'mime')) {
    $err = 'Problem loading utility scripts';
}
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)) {
 function data_search($options = array(), $config = array())
 {
     $found_obs = array();
     $json_obs = array();
     if (!$config) {
         $config = config_get();
     }
     if (!config_error($config)) {
         $dir_host = $config['web_root_directory'];
         $module_dir = path_concat(path_concat($dir_host, $config['module_directory']), 'module');
         $userid = empty($options['uid']) ? '' : $options['uid'];
         $group = empty($options['group']) ? '' : $options['group'];
         $index = empty($options['index']) ? 0 : $options['index'];
         $count = empty($options['count']) ? 1000 : $options['count'];
         $query = empty($options['query']) ? '' : $options['query'];
         if ($group) {
             switch ($group) {
                 // group parameter determines which xml file we search through
                 case 'mash':
                 case 'video':
                 case 'audio':
                 case 'image':
                     if ($userid) {
                         $path = path_concat(path_concat(path_concat($dir_host, $config['user_data_directory']), $userid), 'media_' . $group . '.json');
                         if (file_exists($path)) {
                             $json_obs = array_merge(json_array(file_get($path)), $json_obs);
                         }
                     }
                     break;
                 default:
                     // modules
                     // look for filenames ending in $group.json in first and second tiers of module directory
                     $base_path = path_add_slash_end($module_dir) . '*/*';
                     $glob_path = $base_path . $group . '.json';
                     $glob_files = glob($glob_path);
                     $glob_path = $base_path . '/*' . $group . '.json';
                     $glob_files = array_merge($glob_files, glob($glob_path));
                     foreach ($glob_files as $file_path) {
                         $obs = json_array(file_get($file_path));
                         $html_path = str_replace('.json', '.html', $file_path);
                         if (file_exists($html_path)) {
                             $html_path = substr($html_path, strlen(path_add_slash_end($dir_host)) - 1);
                             for ($i = 0; $i < sizeof($obs); $i++) {
                                 $obs[$i]['html'] = $html_path;
                             }
                         }
                         $json_obs = array_merge($obs, $json_obs);
                     }
             }
             if ($json_obs) {
                 foreach ($json_obs as $ob) {
                     $ok = 1;
                     if ($query) {
                         reset($query);
                         // loop through all parameters
                         foreach ($query as $k => $v) {
                             $test = (string) $ob[$k];
                             // will match if parameter is empty, equal to or (for label) within attribute
                             $ok = !$v || $v == $test || $k == 'label' && strpos(strtolower($test), strtolower($v)) !== FALSE;
                             if (!$ok) {
                                 break;
                             }
                         }
                     }
                     if ($ok) {
                         if ($index) {
                             $index--;
                         } else {
                             // only add tag if within specified range
                             $found_obs[] = $ob;
                             $count--;
                             if (!$count) {
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $found_obs;
 }
 function api_queue_job($data, $config = array())
 {
     $result = array();
     if (!$config) {
         $config = config_get();
     }
     $result['error'] = config_error($config);
     // queue job
     if (empty($result['error'])) {
         if ($config['log_api_request']) {
             log_file("{$config['client']} request:\n" . json_encode($data), $config);
         }
         // post job to the Transcoder
         $result = service_enqueue($data, $config);
         if (empty($result['error']) && empty($result['id'])) {
             $result['error'] = 'Got no Job ID';
         }
     }
     return $result;
 }
 function file_safe($path, $config = array())
 {
     $result = FALSE;
     if ($path) {
         if (!$config) {
             $config = config_get();
         }
         if (!config_error($config)) {
             $ext = file_extension($path);
             // will be empty if path is directory
             $dirs = explode('/', $path);
             if ($ext) {
                 array_pop($dirs);
             }
             // get rid of file name if path is file
             $to_create = array();
             $dir = join('/', $dirs);
             while ($dirs && !file_exists($dir)) {
                 $to_create[] = array_pop($dirs);
                 $dir = join('/', $dirs);
             }
             $z = sizeof($to_create);
             if ($z) {
                 for ($i = $z - 1; $i > -1; $i--) {
                     $dir .= '/' . $to_create[$i];
                     $result = @mkdir($dir, octdec($config['chmod_directory_new']), TRUE);
                     if ($result) {
                         $result = file_mode($dir, $config, TRUE);
                     }
                     if (!$result) {
                         break;
                     }
                 }
             } else {
                 $result = file_mode($dir, $config);
             }
         }
     }
     return $result;
 }