Flynntes / Sleeky

🎨 A sleek and simple frontend & backend theme for YOURLS
http://sleeky.flynntes.com
MIT License
462 stars 97 forks source link

Adding HTTP / HTTPS Automatically when not Added #132

Open mrlufus opened 3 months ago

mrlufus commented 3 months ago

I would like to Add HTTP to the entered URL if not done by the User.

This Code from Geek For Geeks could solve this issue. https://www.geeksforgeeks.org/how-to-add-http-if-it-doesnt-exists-in-the-url-in-php/

Unfortunately I wasn't able to add this in a working state to the index.php file. I thought adding it after Code line 51, could do the work.

Maybe some of you have an Idea?

mrlufus commented 3 months ago

I've also tried this here (plugin.php - line 15)

// Part to be executed if FORM has been submitted
if ( isset( $_REQUEST['url'] ) && $_REQUEST['url'] != 'http://' ) {
    // Check if "https://" is missing in the URL
    if (!preg_match("~^(?:f|ht)tps?://~i", $_REQUEST['url'])) {
        $_REQUEST['url'] = "https://" . $_REQUEST['url'];
    }
    if (enableRecaptcha) {
        // Use reCAPTCHA
        $token = $_POST['token'];
        $action = $_POST['action'];

        // call curl to POST request
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => recaptchaV3SecretKey, 'response' => $token)));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        $arrResponse = json_decode($response, true);

        // verify the response
        if($arrResponse["success"] == '1' && $arrResponse["action"] == $action && $arrResponse["score"] >= 0.5) {
            // reCAPTCHA succeeded
            shorten();
        } else {
            // reCAPTCHA failed
            $message = "reCAPTCHA failed";
        }
    } else {
        // Don't use reCAPTCHA
        shorten();
    }
}
mrlufus commented 3 months ago

This also didn't work (plugin.php - line 46)

function shorten() {
    // Get parameters -- they will all be sanitized in yourls_add_new_link()
    $url     = $_REQUEST['url'];
    $keyword = isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' ;
    $title   = isset( $_REQUEST['title'] ) ?  $_REQUEST['title'] : '' ;
    $text    = isset( $_REQUEST['text'] ) ?  $_REQUEST['text'] : '' ;

    // Prepend "https://" if not already provided in the URL
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "https://" . $url;
    }

    // Create short URL, receive array $return with various information
    $return  = yourls_add_new_link( $url, $keyword, $title );

    // Make visible to UI
    global $shorturl, $message, $status, $title;

    $shorturl = isset( $return['shorturl'] ) ? $return['shorturl'] : '';
    $message  = isset( $return['message'] ) ? $return['message'] : '';
    $title    = isset( $return['title'] ) ? $return['title'] : '';
    $status   = isset( $return['status'] ) ? $return['status'] : '';

    // Stop here if bookmarklet with a JSON callback function ("instant" bookmarklets)
    if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) {
        $short = $return['shorturl'] ? $return['shorturl'] : '';
        $message = "Short URL (Ctrl+C to copy)";
        header('Content-type: application/json');
        echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" );
        die();
    }
}