Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 3bbb5c164976113bba47e7b425e7d59a667703ea 0 parents
@leggetter leggetter authored
2  .gitignore
@@ -0,0 +1,2 @@
+*.DS_Store
+src/config.php
0  README.md
No changes.
0  src/config.example.php
No changes.
0  src/css/styles.css
No changes.
19 src/index.php
@@ -0,0 +1,19 @@
+<?php
+ require_once('config.php');
+?>
+
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8" />
+ <title>Realtime Visitor Tracker Example</title>
+ <meta name="description" content="Realtime Visitor Tracker Example" />
+ <link rel="stylesheet" href="css/styles.css?v=1.0" />
+ <!--[if lt IE 9]>
+ <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
+ <![endif]-->
+</head>
+<body>
+
+</body>
+</html>
0  src/js/tracker.js
No changes.
223 src/pusher/Pusher.php
@@ -0,0 +1,223 @@
+<?php
+
+/*
+ Pusher PHP Library
+ /////////////////////////////////
+ This was a very simple PHP library to the Pusher API.
+
+ $pusher = new Pusher(APIKEY, SECRET, APP_ID, CHANNEL, [Debug: true/false, HOST, PORT]);
+ $pusher->trigger('my_event', 'test_channel', [socket_id, Debug: true/false]);
+ $pusher->socket_auth('socket_id');
+
+ Copyright 2011, Squeeks. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
+
+ Contributors:
+ + Paul44 (http://github.com/Paul44)
+ + Ben Pickles (http://github.com/benpickles)
+ + Mastercoding (http://www.mastercoding.nl)
+ + Alias14 (mali0037@gmail.com)
+ + Max Williams (max@pusher.com)
+ + Zack Kitzmiller (delicious@zackisamazing.com)
+ + Andrew Bender (igothelp@gmail.com)
+*/
+
+class PusherInstance {
+
+ private static $instance = null;
+ private static $app_id = '';
+ private static $secret = '';
+ private static $api_key = '';
+
+ private function __construct() { }
+ private function __clone() { }
+
+ public static function get_pusher()
+ {
+ if (self::$instance !== null) return self::$instance;
+
+ self::$instance = new Pusher(
+ self::$api_key,
+ self::$secret,
+ self::$app_id
+ );
+
+ return self::$instance;
+ }
+}
+
+class Pusher
+{
+
+ private $settings = array ();
+
+ /**
+ * PHP5 Constructor.
+ *
+ * Initializes a new Pusher instance with key, secret , app ID and channel.
+ * You can optionally turn on debugging for all requests by setting debug to true.
+ *
+ * @param string $auth_key
+ * @param string $secret
+ * @param int $app_id
+ * @param bool $debug [optional]
+ * @param string $host [optional]
+ * @param int $port [optional]
+ * @param int $timeout [optional]
+ */
+ public function __construct( $auth_key, $secret, $app_id, $debug = false, $host = 'http://api.pusherapp.com', $port = '80', $timeout = 30 )
+ {
+
+ // Check compatibility, disable for speed improvement
+ $this->check_compatibility();
+
+ // Setup defaults
+ $this->settings['server'] = $host;
+ $this->settings['port'] = $port;
+ $this->settings['auth_key'] = $auth_key;
+ $this->settings['secret'] = $secret;
+ $this->settings['app_id'] = $app_id;
+ $this->settings['url'] = '/apps/' . $this->settings['app_id'];
+ $this->settings['debug'] = $debug;
+ $this->settings['timeout'] = $timeout;
+
+ }
+
+ /**
+ * Check if the current PHP setup is sufficient to run this class
+ */
+ private function check_compatibility()
+ {
+
+ // Check for dependent PHP extensions (JSON, cURL)
+ if ( ! extension_loaded( 'curl' ) || ! extension_loaded( 'json' ) )
+ {
+ die( 'There is missing dependant extensions - please ensure both cURL and JSON modules are installed' );
+ }
+
+ # Supports SHA256?
+ if ( ! in_array( 'sha256', hash_algos() ) )
+ {
+ die( 'SHA256 appears to be unsupported - make sure you have support for it, or upgrade your version of PHP.' );
+ }
+
+ }
+
+ /**
+ * Trigger an event by providing event name and payload.
+ * Optionally provide a socket ID to exclude a client (most likely the sender).
+ *
+ * @param string $event
+ * @param mixed $payload
+ * @param int $socket_id [optional]
+ * @param string $channel [optional]
+ * @param bool $debug [optional]
+ * @return bool|string
+ */
+ public function trigger( $channel, $event, $payload, $socket_id = null, $debug = false, $already_encoded = false )
+ {
+
+ # Check if we can initialize a cURL connection
+ $ch = curl_init();
+ if ( $ch === false )
+ {
+ die( 'Could not initialise cURL!' );
+ }
+
+ # Add channel to URL..
+ $s_url = $this->settings['url'] . '/channels/' . $channel . '/events';
+
+ # Build the request
+ $signature = "POST\n" . $s_url . "\n";
+ $payload_encoded = $already_encoded ? $payload : json_encode( $payload );
+ $query = "auth_key=" . $this->settings['auth_key'] . "&auth_timestamp=" . time() . "&auth_version=1.0&body_md5=" . md5( $payload_encoded ) . "&name=" . $event;
+
+ # Socket ID set?
+ if ( $socket_id !== null )
+ {
+ $query .= "&socket_id=" . $socket_id;
+ }
+
+ # Create the signed signature...
+ $auth_signature = hash_hmac( 'sha256', $signature . $query, $this->settings['secret'], false );
+ $signed_query = $query . "&auth_signature=" . $auth_signature;
+ $full_url = $this->settings['server'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query;
+
+ # Set cURL opts and execute request
+ curl_setopt( $ch, CURLOPT_URL, $full_url );
+ curl_setopt( $ch, CURLOPT_HTTPHEADER, array ( "Content-Type: application/json" ) );
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
+ curl_setopt( $ch, CURLOPT_POST, 1 );
+ curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload_encoded );
+ curl_setopt( $ch, CURLOPT_TIMEOUT, $this->settings['timeout'] );
+
+ $response = curl_exec( $ch );
+
+ curl_close( $ch );
+
+ if ( $response == "202 ACCEPTED\n" && $debug == false )
+ {
+ return true;
+ }
+ elseif ( $debug == true || $this->settings['debug'] == true )
+ {
+ return $response;
+ }
+ else
+ {
+ return false;
+ }
+
+ }
+
+ /**
+ * Creates a socket signature
+ *
+ * @param int $socket_id
+ * @param string $custom_data
+ * @return string
+ */
+ public function socket_auth( $channel, $socket_id, $custom_data = false )
+ {
+
+ if($custom_data == true)
+ {
+ $signature = hash_hmac( 'sha256', $socket_id . ':' . $channel . ':' . $custom_data, $this->settings['secret'], false );
+ }
+ else
+ {
+ $signature = hash_hmac( 'sha256', $socket_id . ':' . $channel, $this->settings['secret'], false );
+ }
+
+ $signature = array ( 'auth' => $this->settings['auth_key'] . ':' . $signature );
+ // add the custom data if it has been supplied
+ if($custom_data){
+ $signature['channel_data'] = $custom_data;
+ }
+ return json_encode( $signature );
+
+ }
+
+ /**
+ * Creates a presence signature (an extension of socket signing)
+ *
+ * @param int $socket_id
+ * @param string $user_id
+ * @param mixed $user_info
+ * @return string
+ */
+ public function presence_auth( $channel, $socket_id, $user_id, $user_info = false )
+ {
+
+ $user_data = array( 'user_id' => $user_id );
+ if($user_info == true)
+ {
+ $user_data['user_info'] = $user_info;
+ }
+
+ return $this->socket_auth($channel, $socket_id, json_encode($user_data) );
+ }
+
+
+}
+
+?>
3  src/pusher/new_hit.php
@@ -0,0 +1,3 @@
+<?php
+ require_once('../config.php');
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.