Skip to content

Instantly share code, notes, and snippets.

@alfasado
Last active December 21, 2015 17:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfasado/6338704 to your computer and use it in GitHub Desktop.
Save alfasado/6338704 to your computer and use it in GitHub Desktop.
<?php
define( 'DataAPICacheDir', '/tmp/api_cache' );
define( 'DataAPIUploadCacheDir', '/tmp/upload' );
define( 'DataAPICacheTtl', 1200 );
define( 'DataAPICacheWithAuth', TRUE );
define( 'DataAPIURL', 'http://localhost/mt/mt-data-api.cgi' );
define( 'SendHTTPHeaderMethod', '' );
$cache_ttl = DataAPICacheTtl;
$cache_with_auth = DataAPICacheWithAuth;
$cache_dir = DataAPICacheDir;
$upload_dir = DataAPIUploadCacheDir;
$http_header_method = SendHTTPHeaderMethod;
$mtime = time();
$path_info = $_SERVER[ 'PATH_INFO' ];
$to_encoding = 'UTF-8';
$vars = $_REQUEST;
$params = array();
$method = $_SERVER[ 'REQUEST_METHOD' ];
foreach ( $vars as $key => $value ) {
if ( $_GET[ $key ] || $_POST[ $key ] ) {
if ( is_string( $value ) ) {
$from_encoding = mb_detect_encoding( $value, 'UTF-8,EUC-JP,SJIS,JIS' );
$value = mb_convert_encoding( $value, $to_encoding, $from_encoding );
}
if ( $method == 'POST' ) {
/*
$value = str_replace( '{\"', '{"', $value );
$value = str_replace( '\":\"', '":"', $value );
$value = str_replace( '\"}', '"}', $value );
$value = str_replace( '\",\"', '","', $value );
$value = str_replace( '\\\'', '\'', $value );
$value = str_replace( '\\\\n', '\n', $value );
$value = str_replace( '\\\\', '', $value );
*/
$params[ $key ] = urlencode( $value );
} else {
$params[ $key ] = $value;
}
}
}
$query_string = '';
$params_array = array();
if ( is_array( $params ) ) {
foreach ( $params as $key => $value ) {
array_push( $params_array, "${key}=${value}" );
}
if ( $params_array ) {
$query_string = join( '&', $params_array );
}
}
if ( $method === 'POST' ) {
$cache_ttl = 0;
}
if ( isset( $vars[ 'cache_ttl' ] ) ) {
$cache_ttl = $vars[ 'cache_ttl' ];
if ( $query_string )
$query_string = preg_replace( '/&{0,1}cache_ttl=[0-9]{0,}/', '', $query_string );
}
$is_multipart;
$with_auth = FALSE;
$get_headers = getallheaders();
$client_headers = array();
foreach ( $get_headers as $key => $value ) {
if ( strtolower( $key ) === 'content-type' ) {
if ( strpos( $value, 'multipart/form-data,' ) === 0 ) {
$is_multipart = 1;
} else {
$header = $key . ': ' . $value;
array_push( $client_headers, $header );
}
} else {
if ( strtolower( $key ) != 'content-length' ) {
$header = $key . ': ' . $value;
array_push( $client_headers, $header );
}
}
if ( strtolower( $key ) === 'x-mt-authorization' ) {
$with_auth = TRUE;
}
}
if ( $with_auth && ( $cache_with_auth === FALSE ) ) {
$cache_ttl = 0;
}
$updated_at = '';
$force = 0;
if ( isset( $vars[ 'force' ] ) ) {
$force = $vars[ 'force' ];
if ( $query_string )
$query_string = preg_replace( '/&{0,1}force=1/', '', $query_string );
}
if ( isset( $vars[ 'updated_at' ] ) ) {
$updated_at = $vars[ 'updated_at' ];
if ( $query_string )
$query_string = preg_replace( '/&{0,1}updated_at=[a-zA-Z]{0,}/', '', $query_string );
}
$api = DataAPIURL . $path_info;
if ( $method === 'GET' ) {
if ( $query_string ) $api .= '?' . $query_string;
}
$filename = md5( $api );
if ( $updated_at ) {
$filename = $updated_at . '.' . $filename;
}
if ( $cache_ttl )
$cache_file = $cache_dir . DIRECTORY_SEPARATOR . $filename;
if ( (! $force ) && isset( $cache_file ) && file_exists( $cache_file ) ) {
$mtime = filemtime( $cache_file );
$time = time();
if ( ( $time - $cache_ttl ) < $mtime ) {
$buf = file_get_contents( $cache_file );
}
}
$upload_file;
$error;
if (! $buf ) {
$curl = curl_init( $api );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $client_headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
if ( $method === 'POST' ) {
curl_setopt( $curl, CURLOPT_POST, TRUE );
if (! $is_multipart ) {
curl_setopt( $curl, CURLOPT_POSTFIELDS, $query_string );
} else {
$upload_params = array();
$upload_params = $_POST;
if ( $file = $_FILES[ 'file' ] ) {
$file_path = $file[ 'tmp_name' ];
$name = $file[ 'name' ];
move_uploaded_file( $file_path, "$upload_dir/$name" );
$upload_file = "$upload_dir/$name";
$upload_params[ 'file' ] = "@"."$upload_dir/$name";
}
curl_setopt( $curl, CURLOPT_POSTFIELDS, $upload_params );
}
}
$buf = curl_exec( $curl );
if ( curl_errno( $curl ) ) {
$json = array();
$json[ 'error' ] = array( 'code' => curl_errno( $curl ),
'message' => 'CURL Error' );
$buf = json_encode( $json );
$error = 1;
}
curl_close( $curl );
if ( isset( $cache_file ) ) {
if (! $error ) {
file_put_contents( $cache_file, $buf );
$mtime = filemtime( $cache_file );
} else {
if ( is_readable( $cache_file ) ) {
unlink( $cache_file );
}
}
}
}
$type = 'application/json';
$length = strlen( $buf );
if ( $http_header_method == 'echo' ) {
$headers[] = "content-type: $type";
} else {
header( "content-type: $type" );
}
$last_modified = gmdate( "D, d M Y H:i:s", $mtime ) . ' GMT';
$etag = '"' . md5( $last_modified ) . '"';
if ( $http_header_method == 'echo' ) {
$headers[] = "Last-Modified: $last_modified";
$headers[] = "ETag: $etag";
} else {
header( "Last-Modified: $last_modified" );
header( "ETag: $etag" );
}
if ( $http_header_method == 'echo' ) {
$headers[] = "Content-Length: $length";
} else {
header( "Content-Length: $length" );
}
if ( isset( $headers ) ) {
echo implode( "\n", $headers ) . "\n\n";
}
echo $buf;
if ( $upload_file ) {
unlink( $upload_file );
}
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment