Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Abitility To Use Certificate (PEM) Passwords. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

ManagerAPNS/certs/MyAppName/production.pem

ManagerAPNS/certs/MyAppName/production.pem
7 changes: 7 additions & 0 deletions ManagerAPNS/certs/MyAppName/cert_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?PHP
$cert_config = array(
'passphrase'=>'PEM_PASS_IF_REQUIRED',
'development_cert'=>'ABSOLUTE_PATH_TO_PEM',
'production_cert'=>'ABSOLUTE_PATH_TO_PEM'
);
?>
Empty file modified ManagerAPNS/certs/MyAppName/sandbox.pem
100644 → 100755
Empty file.
11 changes: 7 additions & 4 deletions ManagerAPNS/include/apns-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
$db = new DbConnect($config->dbAddress, $config->dbUsername, $config->dbPassword, $config->dbName);
$db->show_errors();


$db_aux = mysql_connect($config->dbAddress, $config->dbUsername, $config->dbPassword);
mysql_select_db($config->dbName, $db_aux);

Expand All @@ -27,15 +26,19 @@
}

foreach ($appNameList as $appname) {
$production = $config->absolutePath."/certs/".$appname."/production.pem";
$sandbox = $config->absolutePath."/certs/".$appname."/sandbox.pem";
include_once($config->absolutePath."/certs/".$appname."/cert_config.php");

$production = $cert_config['production_cert'];
$sandbox = $cert_config['development_cert'];
$log = $config->absolutePath.$config->logFile;
$passPhrase = $cert_config['passphrase'];

// FETCH $_GET OR CRON ARGUMENTS TO AUTOMATE TASKS
$args = (!empty($_GET)) ? $_GET:array('task'=>$argv[1]);

$args['appname']=$appname;

// CREATE APNS OBJECT, WITH DATABASE OBJECT AND ARGUMENTS
$apns= new APNS($db, $args, $production, $sandbox);
$apns= new APNS($db, $args, $production, $sandbox, $log, $passPhrase);
}
?>
24 changes: 19 additions & 5 deletions ManagerAPNS/include/class_APNS.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
session_start();

if(!session_id()) {
session_start(); // NEVER forget this!
}

#################################################################################
## Developed by Manifest Interactive, LLC ##
Expand Down Expand Up @@ -190,7 +193,7 @@ class APNS {
* @param string $logPath Path to the log file.
* @access public
*/
function __construct($db, $args=NULL, $certificate=NULL, $sandboxCertificate=NULL, $logPath=NULL) {
function __construct($db, $args=NULL, $certificate=NULL, $sandboxCertificate=NULL, $logPath=NULL, $certificate_password=NULL) {

if(!empty($certificate) && file_exists($certificate))
{
Expand All @@ -209,12 +212,14 @@ function __construct($db, $args=NULL, $certificate=NULL, $sandboxCertificate=NUL
'production'=>array(
'certificate'=>$this->certificate,
'ssl'=>$this->ssl,
'feedback'=>$this->feedback
'feedback'=>$this->feedback,
'certificate_password'=>$certificate_password
),
'sandbox'=>array(
'certificate'=>$this->sandboxCertificate,
'ssl'=>$this->sandboxSsl,
'feedback'=>$this->sandboxFeedback
'feedback'=>$this->sandboxFeedback,
'certificate_password'=>$certificate_password
)
);
if ($logPath !== null) {
Expand Down Expand Up @@ -523,6 +528,11 @@ private function _iterateMessages($sql) {
private function _connectSSLSocket($development) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->apnsData[$development]['certificate']);
if ($this->apnsData[$development]['certificate_password'] != NULL &&
$this->apnsData[$development]['certificate_password']) {
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->apnsData[$development]['certificate_password']);
}

$this->sslStreams[$development] = stream_socket_client($this->apnsData[$development]['ssl'], $error, $errorString, 100, (STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT), $ctx);
if(!$this->sslStreams[$development]){
$this->_triggerError("Failed to connect to APNS: {$error} {$errorString}.");
Expand Down Expand Up @@ -662,6 +672,10 @@ private function _pushMessage($pid, $message, $token, $development){
*/
private function _checkFeedback($development){
$ctx = stream_context_create();
if ($this->apnsData[$development]['certificate_password'] != NULL &&
$this->apnsData[$development]['certificate_password']) {
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->apnsData[$development]['certificate_password']);
}
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->apnsData[$development]['certificate']);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
$fp = stream_socket_client($this->apnsData[$development]['feedback'], $error,$errorString, 100, (STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT), $ctx);
Expand Down Expand Up @@ -1160,4 +1174,4 @@ public function processQueue(){
$this->_fetchMessages();
}
}
?>
?>
11 changes: 6 additions & 5 deletions ManagerAPNS/include/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

class EasyAPNSConfiguration {
private $appList = array();
public $dbUsername = "user"; //TO CHANGE
public $dbPassword = "pass";//TO CHANGE
public $dbName = "database";//TO CHANGE
public $dbUsername = "DB_USERNAME"; //TO CHANGE
public $dbPassword = "DB_PASS";//TO CHANGE
public $dbName = "DB_NAME";//TO CHANGE
public $dbAddress = "localhost";//TO CHANGE
public $absolutePath = "/absolute/path/to/install/dir/";//TO CHANGE
public $logFile = "/log/APNSLog.log";
public $absolutePath = "/var/www/httpdocs/MY_DOMAIN/public_html/ManagerAPNS";
}
?>
?>
16 changes: 12 additions & 4 deletions ManagerAPNS/include/sendFetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@
}

$appname = $_POST['appname'];

$production = $config->absolutePath."/certs/".$appname."/production.pem";
$sandbox = $config->absolutePath."/certs/".$appname."/sandbox.pem";
require_once($config->absolutePath."/certs/".$appname."/cert_config.php");

$production = $cert_config['production_cert'];
$sandbox = $cert_config['development_cert'];

$db = new DbConnect($config->dbAddress, $config->dbUsername, $config->dbPassword, $config->dbName);
$db->show_errors();

$args = array();
$args["task"] = "fetch";
$args["appname"]=$appname;
$apns= new APNS($db, $args, $production, $sandbox);
$apns= new APNS(
$db,
$args,
$production,
$sandbox,
$config->absolutePath.$config->logFile,
$cert_config['passphrase']
);

?>
25 changes: 20 additions & 5 deletions ManagerAPNS/include/sendFlush.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
* @link https://github.com/apascual/ManagerAPNS
*/

session_start(); // NEVER forget this!
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(!session_id()) {
session_start(); // NEVER forget this!
}

if(!isset($_SESSION['loggedin']))
{
header("Location: index.php");
Expand All @@ -23,16 +29,25 @@
}

$appname = $_POST['appname'];

$production = $config->absolutePath."/certs/".$appname."/production.pem";
$sandbox = $config->absolutePath."/certs/".$appname."/sandbox.pem";
require_once($config->absolutePath."/certs/".$appname."/cert_config.php");

$production = $cert_config['production_cert'];
$sandbox = $cert_config['development_cert'];

$db = new DbConnect($config->dbAddress, $config->dbUsername, $config->dbPassword, $config->dbName);
$db->show_errors();

$args = array();
$args["task"] = "flush";
$args["appname"]=$appname;
$apns= new APNS($db, $args, $production, $sandbox);

$apns= new APNS(
$db,
$args,
$production,
$sandbox,
$config->absolutePath.$config->logFile,
$cert_config['passphrase']
);

?>
29 changes: 22 additions & 7 deletions ManagerAPNS/include/sendPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
* @link https://github.com/apascual/ManagerAPNS
*/

session_start(); // NEVER forget this!
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(!session_id()) {
session_start(); // NEVER forget this!
}

if(!isset($_SESSION['loggedin']))
{
header("Location: index.php");
Expand All @@ -26,30 +32,39 @@
}

$appname = $_POST['appname'];

$production = $config->absolutePath."/certs/".$appname."/production.pem";
$sandbox = $config->absolutePath."/certs/".$appname."/sandbox.pem";
require_once($config->absolutePath."/certs/".$appname."/cert_config.php");

$production = $cert_config['production_cert'];
$sandbox = $cert_config['development_cert'];

$db = new DbConnect($config->dbAddress, $config->dbUsername, $config->dbPassword, $config->dbName);
$db->show_errors();

$apns= new APNS(
$db,
NULL,
$production,
$sandbox,
$config->absolutePath.$config->logFile,
$cert_config['passphrase']
);

$apns= new APNS($db, NULL, $production, $sandbox);
$pidList = explode(';', $_POST['pid']);

settype($_POST['badge'], "int");
foreach ($pidList as $key => $value) {
$pid = $value;
settype($pid, "int");

if($_POST['date'])
if(array_key_exists('date', $_POST))
$apns->newMessage($pid,$_POST['date']);
else
$apns->newMessage($pid);

$apns->addMessageAlert($_POST['message']);
$apns->addMessageBadge($_POST['badge']);
$apns->addMessageSound($_POST['sound']);
$apns->addMessageCustom('url', $_POST['url']);
$apns->addMessageCustom('url', (array_key_exists('url', $_POST) ? $_POST['url'] : null));
$apns->queueMessage();
}
?>
File renamed without changes.