Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Initial Commit
  • Loading branch information
XLixl4snSU committed Jan 10, 2023
1 parent 3b948c1 commit 86a96e6
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 38 deletions.
77 changes: 77 additions & 0 deletions includes/admin-page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Telegram Notifier admin page
*
*/

// Display admin page
function telegram_notifier_display_page() {
// Check if the values were updated by the form
if( isset( $_POST['telegram_notifier_api_key'] ) ) {
// Check nonce
yourls_verify_nonce( 'telegram_notifier_admin' );

// Process form, update stored values
telegram_notifier_update_options( 'telegram_notifier_api_key' );
telegram_notifier_update_options( 'telegram_notifier_user_id' );
telegram_notifier_update_options( 'telegram_notifier_user_notification_text' );
telegram_notifier_update_options( 'telegram_notifier_delete_settings_on_uninstall' );
yourls_redirect( yourls_admin_url( 'plugins.php?page=telegram_notifier' ) );
}

// Check if user requested a test message
if( isset( $_POST['test_message'] ) ) {
$message = 'If you get this message your Yourls Telegram Notifier is set up correctly.';
telegram_notifier_api_request( $message );
}

// Get values from database
$telegram_notifier_api_key = yourls_get_option( 'telegram_notifier_api_key' );
$telegram_notifier_user_id = yourls_get_option( 'telegram_notifier_user_id' );
$telegram_notifier_user_notification_text = yourls_get_option( 'telegram_notifier_user_notification_text' );
$telegram_notifier_delete_settings_on_uninstall = yourls_get_option( 'telegram_notifier_delete_settings_on_uninstall' );

// Set standard notification text
$telegram_notifier_user_notification_text = yourls_get_option( 'telegram_notifier_user_notification_text' );
if ( empty( $telegram_notifier_user_notification_text ) OR $telegram_notifier_user_notification_text == " " ) {
$telegram_notifier_user_notification_text = 'The URL %url was shorted with the shortlink %shortlink by IP %ip';
yourls_update_option( 'telegram_notifier_user_notification_text', $telegram_notifier_user_notification_text );
}

// Create nonce
$nonce = yourls_create_nonce( 'telegram_notifier_admin' );

$checkbox=$telegram_notifier_delete_settings_on_uninstall=="true" ? 'checked' : '';
echo <<<HTML
<h2>Telegram Notifier</h2>
<p>This plugin allows you to send notifications of newly shortened links via Telegram.
See the <a href="https://github.com/XLixl4snSU/yourls-telegram-notifier">plugin documentation</a> for setup instructions.</p>
<h3>Configure the plugin</h3>
<form method="post">
<input type="hidden" name="nonce" value="$nonce" />
<p><label for="telegram_notifier_api_key">Telegram Bot Token</label> <input type="text" id="telegram_notifier_api_key" name="telegram_notifier_api_key" value="$telegram_notifier_api_key" size="70" /></p>
<p><label for="telegram_notifier_user_id">Telegram User ID(s) (separated by comma)</label> <input type="text" id="telegram_notifier_user_id" name="telegram_notifier_user_id" value="$telegram_notifier_user_id" size="70" /></p>
<p><label for="telegram_notifier_user_notification_text">Notification text (use %url, %shortlink and %ip as variables)</label> <input type="text" id="telegram_notifier_user_notification_text" name="telegram_notifier_user_notification_text" value="$telegram_notifier_user_notification_text" size="70" /></p>
<input type='hidden' id="telegram_notifier_delete_settings_on_uninstall" name="telegram_notifier_delete_settings_on_uninstall" value='false' >
<p><label for="telegram_notifier_delete_settings_on_uninstall">Check to delete all stored settings on uninstall/deactivation. </label> <input type="checkbox" id="telegram_notifier_delete_settings_on_uninstall" name="telegram_notifier_delete_settings_on_uninstall" value="true" $checkbox><br>
<p><input type="submit" value="Update settings" /></p>
</form>
<form method="post">
<p><input type="submit" name="test_message" value="Send test message" /></p>
</form>
HTML;
}

// Update options in database
function telegram_notifier_update_options($option) {
$in = $_POST["$option"];
if( $in ) {
// Update value in database
yourls_update_option( "$option", $in );

}
}


74 changes: 55 additions & 19 deletions plugin.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: https://github.com/your_name/your_plugin
Description: One line description of your plugin
Plugin Name: Telegram Notifier
Plugin URI: https://github.com/XLixl4snSU/yourls-telegram-notifier
Description: This plugin provides Telegram notifications for newly created shortlinks
Version: 1.0
Author: Your Name
Author URI: https://your-site-if-any/
Author: XLixl4snSU
Author URI: https://github.com/XLixl4snSU
*/

// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Register on plugin page
yourls_add_action( 'plugins_loaded', 'telegram_notifier_init' );
function telegram_notifier_init() {
yourls_register_plugin_page( 'telegram_notifier', 'Telegram Notifier', 'telegram_notifier_admin_page' );
}

/*
// Display admin page
function telegram_notifier_admin_page() {
include_once dirname( __FILE__ ) . '/includes/admin-page.php';
telegram_notifier_display_page();
}

Your code goes here.
Suggested read:
https://docs.yourls.org/guide/extend/plugins.html
https://docs.yourls.org/development/plugins.html
https://docs.yourls.org/development/coding-standards.html
and in general the whole "Development" section on https://docs.yourls.org/
Have fun!
*/
// Send notification when new link is added
yourls_add_action( 'insert_link', 'telegram_notifier_on_link');
function telegram_notifier_on_link( $args ) {
// Get Info about added link
$url = $args[1];
$shortlink = $args[2];
$ip = $args[5];
// Get Variables for Notification
$base_url = yourls_get_yourls_site();
$message = yourls_get_option( 'telegram_notifier_user_notification_text' );

// Replace variables set by User with data
$message = str_replace('%url', $url, $message);
$message = str_replace('%shortlink', "$base_url/$shortlink", $message);
$message = str_replace('%ip', $ip, $message);
telegram_notifier_api_request( $message );
}

// This function sends the actual API request
function telegram_notifier_api_request( $message ) {
$api_key = yourls_get_option( 'telegram_notifier_api_key' );
$user_id = yourls_get_option( 'telegram_notifier_user_id' );
$user_id_array = explode(",", str_replace(' ', '', $user_id));
foreach ( $user_id_array as $current_id ) {
// Build HTTP Telegram API request
$telegram_api_url = "https://api.telegram.org/bot$api_key/sendMessage";
$request_data = array('chat_id' => $current_id, 'text' => $message);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($request_data)
)
);

// Submit request
$context = stream_context_create($options);
$resp = file_get_contents($telegram_api_url, false, $context);
}
}
30 changes: 11 additions & 19 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<?php

// No direct call.
if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) die();

/**
* This is an optional file that will be executed when a user deactivates your plugin.
*
* Example of an uninstall procedure :
*
* - delete custom option :
* yourls_delete_option('joe_plugin');
*
* - delete custom table :
* $table = 'JOE_CUSTOM_TABLE';
* $sql = "DROP TABLE IF EXISTS :table";
* $binds = array('table' => $table);
* $update = yourls_get_db()->fetchAffected($sql, $binds);
*
* - delete specific files,
* - ping plugin's mothership to tell about uninstalling,
* - etc.
*
*/
// Check if settings should be deleted on deactivation, delete if true

$delete_settings = yourls_get_option( 'telegram_notifier_delete_settings_on_uninstall' );

if ( $delete_settings == "true" ) {
yourls_delete_option( 'telegram_notifier_api_key' );
yourls_delete_option( 'telegram_notifier_user_id' );
yourls_delete_option( 'telegram_notifier_user_notification_text' );
yourls_delete_option( 'telegram_notifier_delete_settings_on_uninstall' );
}

0 comments on commit 86a96e6

Please sign in to comment.