Empty commit

This commit is contained in:
Git 2014-03-31 08:01:19 +00:00
parent 8cd5c4e1fe
commit e5b57d8c27
6 changed files with 162 additions and 3 deletions

View File

@ -675,7 +675,12 @@ public static function save_settings($UserID, $Settings) {
WHERE UserID = '$UserID'");
$PushService = (int) $_POST['pushservice'];
$PushOptions = db_string(serialize(array("PushKey" => $_POST['pushkey'])));
$PushOptionsArray = array("PushKey" => $_POST['pushkey']);
if ($PushService === 6) { //pushbullet
$PushOptionsArray['PushDevice'] = $_POST['pushdevice'];
}
$PushOptions = db_string(serialize($PushOptionsArray));
if ($PushService != 0) {
G::$DB->query("
@ -752,13 +757,23 @@ public static function send_push($UserIDs, $Title, $Body, $URL = '', $Type = 'Gl
case '5':
$Service = "Pushover";
break;
case '6':
$Service = "PushBullet";
break;
default:
break;
}
if (!empty($Service) && !empty($PushOptions['PushKey'])) {
$JSON = json_encode(array("service" => strtolower($Service),
$Options = array("service" => strtolower($Service),
"user" => array("key" => $PushOptions['PushKey']),
"message" => array("title" => $Title, "body" => $Body, "url" => $URL)));
"message" => array("title" => $Title, "body" => $Body, "url" => $URL));
if ($Service === 'PushBullet') {
$Options["user"]["device"] = $PushOptions['PushDevice'];
}
$JSON = json_encode($Options);
G::$DB->query("
INSERT INTO push_notifications_usage
(PushService, TimesUsed)

View File

@ -20,6 +20,9 @@ public static function load_js() {
private static function render_push_settings() {
$PushService = self::$Settings['PushService'];
$PushOptions = unserialize(self::$Settings['PushOptions']);
if (empty($PushOptions['PushDevice'])) {
$PushOptions['PushDevice'] = '';
}
?>
<tr>
<td class="label"><strong>Push notifications</strong></td>
@ -31,10 +34,15 @@ private static function render_push_settings() {
<!-- No option 3, notifo died. -->
<option value="4"<? if ($PushService == 4) { ?> selected="selected"<? } ?>>Super Toasty</option>
<option value="5"<? if ($PushService == 5) { ?> selected="selected"<? } ?>>Pushover</option>
<option value="6"<? if ($PushService == 6) { ?> selected="selected"<? } ?>>PushBullet</option>
</select>
<div id="pushsettings" style="display: none;">
<label id="pushservice_title" for="pushkey">API key</label>
<input type="text" size="50" name="pushkey" id="pushkey" value="<?=display_str($PushOptions['PushKey'])?>" />
<label class="pushdeviceid" id="pushservice_device" for="pushdevice">Device ID</label>
<select class="pushdeviceid" name="pushdevice" id="pushdevice">
<option value="<?= display_str($PushOptions['PushDevice'])?>" selected="selected"><?= display_str($PushOptions['PushDevice'])?></option>
</select>
<br />
<a href="user.php?action=take_push&amp;push=1&amp;userid=<?=G::$LoggedUser['ID']?>&amp;auth=<?=G::$LoggedUser['AuthKey']?>" class="brackets">Test push</a>
<a href="wiki.php?action=article&amp;id=1017" class="brackets">View wiki guide</a>

View File

@ -52,6 +52,16 @@ private function parse_data($Data) {
case 'pushover':
$this->push_pushover($JSON['user']['key'], $JSON['message']['title'], $JSON['message']['body'], $JSON['message']['url']);
break;
case 'pushbullet':
$this->push_pushbullet(
$JSON['user']['key'],
$JSON['user']['device'],// <strip>
$JSON['user']['userid'],
$JSON['user']['email'],//</strip>
$JSON['message']['title'],
$JSON['message']['body'],
$JSON['message']['url']
);
default:
break;
}
@ -134,6 +144,44 @@ private function push_pushover($UserKey, $Title, $Message, $URL) {
curl_close($ch);
echo "Push sent to Pushover";
}
/**
* Notify via pushbullet
*
* @param $UserKey User API key
* @param $DeviceID device to push to <strip>
* @param $UserID UserID to check IP for
* @param $Email Last email gotten from pushbullet API. Used for anti-cheat.</strip>
* @param $Title Notification title
* @param $Message Notification message
* @param $URL For compatibility with other command. Just gets appended.
*/
private function push_pushbullet($UserKey, $DeviceID,
$Title, $Message, $URL) {
if (!empty($URL)) {
$Message .= ' ' . $URL;
}
curl_setopt_array($Curl = curl_init(), array(
CURLOPT_URL => 'https://api.pushbullet.com/api/pushes',
CURLOPT_POSTFIELDS => array(
'type' => 'note',
'title' => $Title,
'body' => $Message,
'device_iden' => $DeviceID
),
CURLOPT_USERPWD => $UserKey . ':',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => True
));
$Result = curl_exec($Curl);
echo "Push sent to Pushbullet";
curl_close($Curl);
}
}
$PushServer = new PushServer();

View File

@ -157,6 +157,9 @@
case 'clear_user_notification':
require(SERVER_ROOT . '/sections/ajax/clear_user_notification.php');
break;
case 'pushbullet_devices':
require(SERVER_ROOT . '/sections/ajax/pushbullet_devices.php');
break;
default:
// If they're screwing around with the query string
json_die("failure");

View File

@ -0,0 +1,21 @@
<?php
if (!isset($_GET['apikey']) || empty($_GET['apikey'])) {
echo '{"error": { "message": "No API Key specified" }}';
die();
}
$ApiKey = $_GET['apikey'];
curl_setopt_array($Ch = curl_init(), array(
CURLOPT_URL => 'https://api.pushbullet.com/api/devices',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $ApiKey . ':'
));
$Result = curl_exec($Ch);
curl_close($Ch);
echo json_encode($Result);

View File

@ -1,5 +1,6 @@
var PUSHOVER = 5;
var TOASTY = 4;
var PUSHBULLET = 6;
$(document).ready(function() {
var top = $('#settings_sections').offset().top - parseFloat($('#settings_sections').css('marginTop').replace(/auto/, 0));
@ -59,8 +60,14 @@ $(document).ready(function() {
});
if ($("#pushservice").val() > 0) {
$('.pushdeviceid').hide();
$('#pushsettings').show();
if ($('#pushservice').val() == PUSHBULLET) {
fetchPushbulletDevices($('#pushkey').val());
$('.pushdeviceid').show();
}
}
$("#pushservice").change(function() {
if ($(this).val() > 0) {
$('#pushsettings').show(500);
@ -75,6 +82,19 @@ $(document).ready(function() {
} else {
$('#pushsettings').hide(500);
}
if ($(this).val() == PUSHBULLET) {
fetchPushbulletDevices($('#pushkey').val());
$('.pushdeviceid').show(500);
} else {
$('.pushdeviceid').hide(500);
}
});
$("#pushkey").blur(function() {
if($("#pushservice").val() == PUSHBULLET) {
fetchPushbulletDevices($(this).val());
}
});
});
@ -82,3 +102,47 @@ function fuzzyMatch(str, pattern){
pattern = pattern.split("").reduce(function(a,b){ return a+".*"+b; });
return new RegExp(pattern).test(str);
};
/**
* Gets device IDs from the pushbullet API
*
* @return array of dictionaries with devices
*/
function fetchPushbulletDevices(apikey) {
$.ajax({
url: 'ajax.php',
data: {
"action": 'pushbullet_devices',
"apikey": apikey
},
type: 'GET',
success: function(data, textStatus, xhr) {
var data = jQuery.parseJSON(data);
var field = $('#pushdevice');
var value = field.val();
if (data.error || textStatus !== 'success' ) {
if (data.error) {
field.html('<option>' + data.error.message + '</option>');
} else {
$('#pushdevice').html('<option>No devices fetched</option>');
}
} else {
if(data['devices'].length > 0) {
field.html('');
}
for (var i = 0; i < data['devices'].length; i++) {
var model = data['devices'][i]['extras']['model'];
var nickname = data['devices'][i]['extras']['nickname'];
var name = nickname !== undefined ? nickname : model;
var option = new Option(name, data['devices'][i]['iden']);
option.selected = (option.value == value);
field[0].add(option);
}
}
},
error: function(data,textStatus,xhr) {
$('#pushdevice').html('<option>' + textStatus + '</option>');
}
});
}