Skip to content

Commit

Permalink
Merge branch 'CL-255'
Browse files Browse the repository at this point in the history
  • Loading branch information
geremy cohen committed Dec 14, 2012
2 parents d518eaa + 0103964 commit 172c18d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 36 deletions.
74 changes: 62 additions & 12 deletions php/3.3/Pubnub.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
<?php
require_once('PubnubAES.php');

/**
* PubNub 3.3 Real-time Push Cloud API
* PubNub 3.4 Real-time Push Cloud API
* @package Pubnub
*/
class Pubnub
{
private $ORIGIN = 'pubsub.pubnub.com';
private $ORIGIN = 'PHP.pubnub.com'; // Change this to your custom origin, or IUNDERSTAND.pubnub.com
private $PUBLISH_KEY = 'demo';
private $SUBSCRIBE_KEY = 'demo';
private $SECRET_KEY = false;
private $CIPHER_KEY = '';
private $SSL = false;
private $SESSION_UUID = '';

// New style response contains channel after timetoken
// Old style response does not

private $NEW_STYLE_RESPONSE = true;
private $PEM_PATH = __DIR__;

/**
* Pubnub
*
Expand All @@ -33,7 +40,8 @@ function Pubnub(
$secret_key = false,
$cipher_key = false,
$ssl = false,
$origin = false
$origin = false,
$pem_path = false
)
{

Expand All @@ -49,9 +57,17 @@ function Pubnub(

$this->SSL = $ssl;

if ($pem_path != false)
$this->PEM_PATH = $pem_path;

if ($origin)
$this->ORIGIN = $origin;

if ($this->ORIGIN == "PHP.pubnub.com") {
trigger_error("Before running in production, please contact support@pubnub.com for your custom origin.\nPlease set the origin from PHP.pubnub.com to IUNDERSTAND.pubnub.com to remove this warning.\n", E_USER_NOTICE);
}


if ($ssl)
$this->ORIGIN = 'https://' . $this->ORIGIN;
else
Expand Down Expand Up @@ -206,20 +222,44 @@ function subscribe($args, $presence = false)
$messages = $response[0];
$timetoken = $response[1];

// determine the channel

if ((count($response) == 3)) {
$derivedChannel = explode(",", $response[2]);
} else {
$channel_array = array();
for ($a = 0; $a < sizeof($messages); $a++) {
array_push($channel_array, $channel);
}
$derivedChannel = $channel_array;
}


if (!count($messages)) {
continue;
}

$receivedMessages = $this->decodeAndDecrypt($messages, $mode);

$returnArray = array($receivedMessages, $timetoken);

$cbReturn = $callback($returnArray);
$returnArray = $this->NEW_STYLE_RESPONSE ? array($receivedMessages, $derivedChannel, $timetoken) : array($receivedMessages, $timetoken);

# Call once for each message for each channel

$exit_now = false;
for ($i = 0; $i < sizeof($receivedMessages); $i++) {

$cbReturn = $callback(array("message" => $returnArray[0][$i], "channel" => $returnArray[1][$i], "timetoken" => $returnArray[2]));

if ($cbReturn == false) {
$exit_now = true;
}

if ($cbReturn == false) {
return;
}

if ($exit_now) {
return;
}


} catch (Exception $error) {
Expand Down Expand Up @@ -251,8 +291,8 @@ public function decodeAndDecrypt($messages, $mode = "default")

} elseif ($mode == "detailedHistory") {

$messageArray = $messages[0];
$receivedMessages = $this->decodeDecryptLoop($messageArray);
$decodedMessages = $this->decodeDecryptLoop($messages);
$receivedMessages = array($decodedMessages[0], $messages[1], $messages[2] );
}

return $receivedMessages;
Expand Down Expand Up @@ -452,20 +492,30 @@ private function _request($request, $urlParams = false)

$ch = curl_init();

$pubnubHeaders = array("V: 3.3", "Accept: */*");
$pubnubHeaders = array("V: 3.4", "Accept: */*");
curl_setopt($ch, CURLOPT_HTTPHEADER, $pubnubHeaders);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 310);

curl_setopt($ch, CURLOPT_URL, $urlString);

if ($this->SSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/pubnub.com.pem");


$pemPathAndFilename = $this->PEM_PATH . "/pubnub.com.pem";
if (file_exists($pemPathAndFilename))
curl_setopt($ch, CURLOPT_CAINFO, $pemPathAndFilename);
else {
trigger_error("Can't find PEM file. Please set pem_path in initializer.");
exit;
}

}


$output = curl_exec($ch);
$curlError = curl_errno($ch);
$curlResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand Down
42 changes: 18 additions & 24 deletions php/3.3/pubnubPlaintextTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@
## ---------------------------------------------------------------------------
## Define Messaging Channel
## ---------------------------------------------------------------------------
$channel = "hello_world";
$sub_channels = "hello_world,hello_world_a,hello_world_b,hello_world_c";
$pub_channel = "hello_world";

## ---------------------------------------------------------------------------
## Publish Example
## ---------------------------------------------------------------------------
echo "Running publish\r\n";
$publish_success = $pubnub->publish(array(
'channel' => $channel,
'channel' => $pub_channel,
'message' => 'Hello from PHP!'
));
echo($publish_success[0] . $publish_success[1]);

print_r($publish_success);
echo "\r\n";

$publish_success = $pubnub->publish(array(
'channel' => $channel,
'channel' => $pub_channel,
'message' => '漢語'
));
echo($publish_success[0] . $publish_success[1]);
print_r($publish_success);
echo "\r\n";

// Publish an associative array
Expand All @@ -52,35 +54,27 @@
$big_array["this stuff"]["can get"] = "complicated!";

$publish_success = $pubnub->publish(array(
'channel' => $channel,
'channel' => $pub_channel,
'message' => $big_array
));

print_r($publish_success);
echo "\r\n";

// This should return a failure (0) JSON Array
$publish_success = $pubnub->publish(array(
'channel' => $channel,
'channel' => $pub_channel,
'message' => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
));
echo($publish_success[0] . $publish_success[1]);
echo "\r\n";

## ---------------------------------------------------------------------------
## History Example
## ---------------------------------------------------------------------------
echo "Running history\r\n";
$history = $pubnub->history(array(
'channel' => $channel,
'limit' => 2
));
print_r($history);
print_r($publish_success);
echo "\r\n";

## ---------------------------------------------------------------------------
## detailedHistory Example
## ---------------------------------------------------------------------------
echo "Running detailedHistory\r\n";
$history = $pubnub->detailedHistory(array(
'channel' => $channel,
'channel' => $pub_channel,
'count' => 5,
'end' => "13466530169226760"
));
Expand All @@ -92,17 +86,17 @@
## ---------------------------------------------------------------------------
echo "Running here_now\r\n";
$here_now = $pubnub->here_now(array(
'channel' => $channel
'channel' => $pub_channel
));
var_dump($here_now);
print_r($here_now);
echo "\r\n";

## ---------------------------------------------------------------------------
## Timestamp Example
## ---------------------------------------------------------------------------
echo "Running timestamp\r\n";
$timestamp = $pubnub->time();
echo('Timestamp: ' . $timestamp);
print_r($timestamp);
echo "\r\n";

## ---------------------------------------------------------------------------
Expand All @@ -125,7 +119,7 @@
echo("\nWaiting for Publish message... Hit CTRL+C to finish.\n");

$pubnub->subscribe(array(
'channel' => $channel,
'channel' => $sub_channels,
'callback' => function($message) {
print_r($message);
echo "\r\n";
Expand Down

0 comments on commit 172c18d

Please sign in to comment.