function archive_extract($path, $archive_dir, $config = array()) { $result = FALSE; if (class_exists('Archive_Tar')) { if (file_safe(path_concat($archive_dir, 'file.txt'), $config)) { $tar = new Archive_Tar($path); $tar->extract($archive_dir); $result = file_exists($archive_dir); } } return $result; }
function local_client_enqueue($data, $config = array()) { $result = array(); $err = ''; $id = empty($data['id']) ? id_unique() : $data['id']; $result['id'] = $data['id'] = $id; $job_path = path_concat($config['queue_directory'], $id . '.json'); $json_str = @json_encode($data); if (!$json_str) { $err = 'could not encode json'; } else { file_safe($job_path); if (!file_put($job_path, $json_str)) { $err = 'could not write job to ' . $job_path; } } if ($err) { $result['error'] = $err; } return $result; }
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); } } } } }
} } if (!$err) { // enforce size limit from configuration, if defined $max = empty($config["max_meg_{$type}"]) ? '' : $config["max_meg_{$type}"]; if ($max) { $file_megs = round($file_size / (1024 * 1024)); if ($file_megs > $max) { $err = $type . ' files must be less than ' . $max . ' meg'; } } } if (!$err) { // try to move upload into its media directory and change permissions $path = path_concat(path_concat(path_concat(path_concat($config['web_root_directory'], $config['user_media_directory']), $uid), $id), $config['import_original_basename'] . '.' . $file_extension); if (!file_safe($path, $config)) { $err = 'Problem creating media directory'; } else { if (!file_move_upload($file['tmp_name'], $path)) { $err = 'Problem moving file'; } else { if (!file_mode($path, $config)) { $err = 'Problem setting permissions of media: ' . $path; } else { log_file('Saved to: ' . $path, $config); } } } $response['status'] = 'uploaded ok'; } if ($err) {
$archive_dir = $tmp_path; if (!archive_extract($file['tmp_name'], $archive_dir, $config)) { $err = 'Could not extract to ' . $archive_dir; } } if (!$err) { // move select files from the archive to media directory switch ($type) { case 'audio': case 'video': // move any soundtrack $frag = $config['import_audio_basename'] . '.' . $config['import_audio_extension']; $media_path = path_concat($media_dir, $frag); $archive_path = path_concat($archive_dir, $frag); if (file_exists($archive_path)) { if (!file_safe($media_path, $config)) { $err = 'Could not create directories for ' . $media_path; } elseif (!@rename($archive_path, $media_path)) { $err = 'Could not move audio file from ' . $archive_path . ' to ' . $media_path; } else { // move any soundtrack waveform graphic $frag = $config['import_waveform_basename'] . '.' . $config['import_waveform_extension']; $archive_path = path_concat($archive_dir, $frag); $media_path = path_concat($media_dir, $frag); if (file_exists($archive_path)) { if (!@rename($archive_path, $media_path)) { $err = 'Could not move audio file from ' . $archive_path . ' to ' . $media_path; } } } }
function file_put($path, $data, $config = array()) { $result = TRUE; $file_existed = file_exists($path); if (!$file_existed) { $result = file_safe($path, $config); } if ($result) { $result = @file_put_contents($path, $data); if (!$file_existed) { if ($result) { $result = file_exists($path); } if ($result) { $result = file_mode($path, $config, TRUE); } } } return $result; }