<commit_before><?php
/**
 * php-twitter-proxy
 * 
 * http://twproxy.eu
 * https://github.com/timwhitlock/php-twitter-proxy
 * @author <NAME> <@timwhitlock>
 */

 

/**
 * Top-level Proxy class. All methods and properties are static
 */
abstract class Proxy {

    /* content type, currently JSON only */
    const TYPE_JSON = 'application/json; charset=utf-8';

    private static $type;


    /* caching configurations */
    private static $cache_engine = 'apc';
    private static $cache_prefix = 'twproxy_';
    private static $cache_minttl = 60;
    
    /* restricted users */
    private static $user_lock = array();
    
    /**
     * @var TwitterApiClient
     */
    private static $client;


    /**
     * Proxy a Twitter API call as authenticated user.
     * @param string e.g. account/verify_credentials
     * @param int Expiry in seconds
     * @return void
     */
    public static function relay( $path, $ttl = 60 ){
        try {
            
            // Twitter API params supported in GET and POST only
            if( 'POST' === $_SERVER['REQUEST_METHOD'] ){
                $method = 'POST';
                $args  = $_POST;
                $cache = false;
                $ttl   = 0;
            }
            else {
                $method = 'GET';
                $args  = $_GET;
                $cache = self::$cache_engine;
                $ttl   = $cache ? max( $ttl, self::$cache_minttl ) : 0;
            }
            
            // Twitter doesn't complain about unecessary parameters, but removing junk and "cache-busters" will improve caching
            // @todo proper array intersect of all suppoerted Twitter API args across methods
            unset( $args['_'] );
            
            // We want to ensure that the cache is hit for requests even when the JSONP callback is different
            unset( $args['callback'] );
                    
            // Fetch from cache if engine specified. Currently only APC supported
            // @todo use a faster method than md5 for key hash?
            if( $cache ){
                ksort( $args );
                $key  = self::$cache_prefix.'_'.md5( $path.serialize($args) );
                $data = apc_fetch($key) or $data = null;
            }

            if( isset($data) ){
                header('X-Cache: Proxy HIT' );
                // reduce TTL to life of cached data
                if( $ttl ){
                    $age = time() - $data['t'];
                    $ttl = max( 0, $ttl - $age );
                }
            }
            else {
                header('X-Cache: Proxy MISS' );

                // Request via pre-configured Twitter client
                $data = self::$client->raw( $path, $args, $method );

                if( $ttl ){
                    // extend TTL if rate limit has been reached for this request
                    $meta = self::$client->last_rate_limit_data();<commit_msg>more human readable cache key<commit_after><?php
/**
 * php-twitter-proxy
 * 
 * http://twproxy.eu
 * https://github.com/timwhitlock/php-twitter-proxy
 * @author <NAME> <@timwhitlock>
 */

 

/**
 * Top-level Proxy class. All methods and properties are static
 */
abstract class Proxy {

    /* content type, currently JSON only */
    const TYPE_JSON = 'application/json; charset=utf-8';

    private static $type;


    /* caching configurations */
    private static $cache_engine = 'apc';
    private static $cache_prefix = 'twproxy_';
    private static $cache_minttl = 60;
    
    /* restricted users */
    private static $user_lock = array();
    
    /**
     * @var TwitterApiClient
     */
    private static $client;


    /**
     * Proxy a Twitter API call as authenticated user.
     * @param string e.g. account/verify_credentials
     * @param int Expiry in seconds
     * @return void
     */
    public static function relay( $path, $ttl = 60 ){
        try {
            
            // Twitter API params supported in GET and POST only
            if( 'POST' === $_SERVER['REQUEST_METHOD'] ){
                $method = 'POST';
                $args  = $_POST;
                $cache = false;
                $ttl   = 0;
            }
            else {
                $method = 'GET';
                $args  = $_GET;
                $cache = self::$cache_engine;
                $ttl   = $cache ? max( $ttl, self::$cache_minttl ) : 0;
            }
            
            // Twitter doesn't complain about unecessary parameters, but removing junk and "cache-busters" will improve caching
            // @todo proper array intersect of all suppoerted Twitter API args across methods
            unset( $args['_'] );
            
            // We want to ensure that the cache is hit for requests even when the JSONP callback is different
            unset( $args['callback'] );
                    
            // Fetch from cache if engine specified. Currently only APC supported
            // @todo establish a faster method for key hash?
            if( $cache ){
                ksort( $args );
                $key  = self::$cache_prefix.'_'.str_replace('/','_',$path).'_'.md5( serialize($args) );
                $data = apc_fetch($key) or $data = null;
            }

            if( isset($data) ){
                header('X-Cache: Proxy HIT' );
                // reduce TTL to life of cached data
                if( $ttl ){
                    $age = time() - $data['t'];
                    $ttl = max( 0, $ttl - $age );
                }
            }
            else {
                header('X-Cache: Proxy MISS' );

                // Request via pre-configured Twitter client
                $data = self::$client->raw( $path, $args, $method );

                if( $ttl ){
                    // extend TTL if rate limit has been reached for this request
                    $meta = self::$client->last_rate_limit_data();
{'fraction_non_alphanumeric': 0.11435357991177468, 'fraction_numerical': 0.0032236172378690194, 'mean_word_length': 1.501060670343657, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '304394', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1561, 'n_words': 495}