/**
 * API function wrapper: Shorten a URL
 *
 * @since 1.6
 * @return array Result of API call
 */
function yourls_api_action_shorturl()
{
    $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
    $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
    $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : '';
    $return = yourls_add_new_link($url, $keyword, $title);
    $return['simple'] = isset($return['shorturl']) ? $return['shorturl'] : '';
    // This one will be used in case output mode is 'simple'
    unset($return['html']);
    // in API mode, no need for our internal HTML output
    return yourls_apply_filter('api_result_shorturl', $return);
}
function bulk_api_bulkshortener($action)
{
    if ($action[0] != 'bulkshortener') {
        return;
    }
    if (!isset($_REQUEST['urls'])) {
        $return = array('errorCode' => 400, 'message' => 'bulkshortener: missing URLS parameter', 'simple' => 'bulkshortener: missing URLS parameter');
        echo $return['errorCode'] . ": " . $return['simple'];
        die;
    }
    $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
    $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : '';
    $urls = isset($_REQUEST['urls']) ? $_REQUEST['urls'] : array();
    foreach ($urls as $url) {
        $return = yourls_add_new_link($url, $keyword, $title);
        echo $return['shorturl'] . "\n";
    }
    die;
}
Exemple #3
0
<?php

define('YOURLS_API', true);
require_once dirname(__FILE__) . '/includes/load-yourls.php';
yourls_maybe_require_auth();
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
yourls_do_action('api', $action);
switch ($action) {
    // Shorten a URL
    case 'shorturl':
        $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : '';
        $return = yourls_add_new_link($url, $keyword, $title);
        $return['simple'] = isset($return['shorturl']) ? $return['shorturl'] : '';
        // This one will be used in case output mode is 'simple'
        unset($return['html']);
        // in API mode, no need for our internal HTML output
        break;
        // Global stats
    // Global stats
    case 'stats':
        $filter = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : '';
        $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : '';
        $return = yourls_api_stats($filter, $limit);
        break;
        // Stats for a shorturl
    // Stats for a shorturl
    case 'url-stats':
        $shorturl = isset($_REQUEST['shorturl']) ? $_REQUEST['shorturl'] : '';
        $return = yourls_api_url_stats($shorturl);
Exemple #4
0
define('YOURLS_ADMIN', true);
define('YOURLS_AJAX', true);
require_once dirname(dirname(__FILE__)) . '/includes/load-yourls.php';
yourls_maybe_require_auth();
// This file will output a JSON string
yourls_content_type_header('application/json');
if (!isset($_REQUEST['action'])) {
    die;
}
// Pick action
$action = $_REQUEST['action'];
switch ($action) {
    case 'add':
        yourls_verify_nonce('add_url', $_REQUEST['nonce'], false, 'omg error');
        $return = yourls_add_new_link($_REQUEST['url'], $_REQUEST['keyword']);
        echo json_encode($return);
        break;
    case 'edit_display':
        yourls_verify_nonce('edit-link_' . $_REQUEST['id'], $_REQUEST['nonce'], false, 'omg error');
        $row = yourls_table_edit_row($_REQUEST['keyword']);
        echo json_encode(array('html' => $row));
        break;
    case 'edit_save':
        yourls_verify_nonce('edit-save_' . $_REQUEST['id'], $_REQUEST['nonce'], false, 'omg error');
        $return = yourls_edit_link($_REQUEST['url'], $_REQUEST['keyword'], $_REQUEST['newkeyword'], $_REQUEST['title']);
        echo json_encode($return);
        break;
    case 'delete':
        yourls_verify_nonce('delete-link_' . $_REQUEST['id'], $_REQUEST['nonce'], false, 'omg error');
        $query = yourls_delete_link_by_keyword($_REQUEST['keyword']);
Exemple #5
0
 yourls_do_action('bookmarklet');
 // No sanitization needed here: everything happens in yourls_add_new_link()
 if (isset($_GET['u'])) {
     // Old school bookmarklet: ?u=<url>
     $url = rawurldecode($_GET['u']);
 } else {
     // New style bookmarklet: ?up=<url protocol>&us=<url slashes>&ur=<url rest>
     $url = rawurldecode($_GET['up'] . $_GET['us'] . $_GET['ur']);
 }
 $keyword = isset($_GET['k']) ? $_GET['k'] : '';
 $title = isset($_GET['t']) ? $_GET['t'] : '';
 $return = yourls_add_new_link($url, $keyword, $title);
 // If fails because keyword already exist, retry with no keyword
 if (isset($return['status']) && $return['status'] == 'fail' && isset($return['code']) && $return['code'] == 'error:keyword') {
     $msg = $return['message'];
     $return = yourls_add_new_link($url, '', $ydb);
     $return['message'] .= ' (' . $msg . ')';
 }
 // Stop here if bookmarklet with a JSON callback function
 if (isset($_GET['jsonp']) && $_GET['jsonp'] == 'yourls') {
     $short = $return['shorturl'] ? $return['shorturl'] : '';
     $message = $return['message'];
     yourls_content_type_header('application/javascript');
     echo yourls_apply_filter('bookmarklet_jsonp', "yourls_callback({'short_url':'{$short}','message':'{$message}'});");
     die;
 }
 // Now use the URL that has been sanitized and returned by yourls_add_new_link()
 $url = $return['url']['url'];
 $where = sprintf(" AND `url` LIKE '%s' ", yourls_escape($url));
 $page = $total_pages = $perpage = 1;
 $offset = 0;
/**
 * Populates the URL table with a few sample links
 *
 * @since 1.7
 * @return bool
 */
function yourls_insert_sample_links()
{
    $link1 = yourls_add_new_link('http://blog.yourls.org/', 'yourlsblog', 'YOURLS\' Blog');
    $link2 = yourls_add_new_link('http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener');
    $link3 = yourls_add_new_link('http://ozh.org/', 'ozh', 'ozh.org');
    return (bool) ($link1['status'] == 'success' & $link2['status'] == 'success' & $link3['status'] == 'success');
}
 function jd_shorten_link($url, $thisposttitle, $post_ID, $testmode = false)
 {
     if (WPT_DEBUG && function_exists('wpt_pro_exists')) {
         wpt_mail("Initial Link Data: #{$post_ID}", "{$url}, {$thisposttitle}, {$post_ID}, {$testmode}");
         // DEBUG
     }
     // filter link before sending to shortener or adding analytics
     $url = apply_filters('wpt_shorten_link', $url, $post_ID);
     if ($testmode == false) {
         if (get_option('use-twitter-analytics') == 1 || get_option('use_dynamic_analytics') == 1) {
             if (get_option('use_dynamic_analytics') == '1') {
                 $campaign_type = get_option('jd_dynamic_analytics');
                 if ($campaign_type == "post_category" && $testmode != 'link') {
                     $category = get_the_category($post_ID);
                     $campaign = sanitize_title($category[0]->cat_name);
                 } else {
                     if ($campaign_type == "post_ID") {
                         $campaign = $post_ID;
                     } else {
                         if ($campaign_type == "post_title" && $testmode != 'link') {
                             $post = get_post($post_ID);
                             $campaign = sanitize_title($post->post_title);
                         } else {
                             if ($testmode != 'link') {
                                 $post = get_post($post_ID);
                                 $post_author = $post->post_author;
                                 $campaign = get_the_author_meta('user_login', $post_author);
                             } else {
                                 $post_author = '';
                                 $campaign = '';
                             }
                         }
                     }
                 }
             } else {
                 $campaign = get_option('twitter-analytics-campaign');
             }
             if (strpos($url, "%3F") === FALSE && strpos($url, "?") === FALSE) {
                 $ct = "?";
             } else {
                 $ct = "&";
             }
             $medium = apply_filters('wpt_utm_medium', 'twitter');
             $source = apply_filters('wpt_utm_source', 'twitter');
             $ga = "utm_campaign={$campaign}&utm_medium={$medium}&utm_source={$source}";
             $url .= $ct .= $ga;
         }
         $url = urldecode(trim($url));
         // prevent double-encoding
         $encoded = urlencode($url);
     } else {
         $url = urldecode(trim($url));
         // prevent double-encoding
         $encoded = urlencode($url);
     }
     // custom word setting
     $keyword_format = get_option('jd_keyword_format') == '1' ? $post_ID : false;
     $keyword_format = get_option('jd_keyword_format') == '2' ? get_post_meta($post_ID, '_yourls_keyword', true) : $keyword_format;
     $error = '';
     // Generate and grab the short url
     switch (get_option('jd_shortener')) {
         case 0:
         case 1:
         case 3:
             $shrink = $url;
             break;
         case 4:
             if (function_exists('wp_get_shortlink')) {
                 // wp_get_shortlink doesn't natively support custom post types; but don't return an error in that case.
                 $shrink = $post_ID != false ? wp_get_shortlink($post_ID, 'post') : $url;
             }
             if (!$shrink) {
                 $shrink = $url;
             }
             break;
         case 2:
             // updated to v3 3/31/2010
             $bitlyapi = trim(get_option('bitlyapi'));
             $bitlylogin = trim(strtolower(get_option('bitlylogin')));
             $decoded = jd_remote_json("https://api-ssl.bitly.com/v3/shorten?longUrl=" . $encoded . "&login="******"&apiKey=" . $bitlyapi . "&format=json");
             if ($decoded) {
                 if ($decoded['status_code'] != 200) {
                     $shrink = $decoded;
                     $error = $decoded['status_txt'];
                 } else {
                     $shrink = $decoded['data']['url'];
                 }
             } else {
                 $shrink = false;
             }
             if (!is_valid_url($shrink)) {
                 $shrink = false;
             }
             break;
         case 5:
             // local YOURLS installation
             global $yourls_reserved_URL;
             define('YOURLS_INSTALLING', true);
             // Pretend we're installing YOURLS to bypass test for install or upgrade
             define('YOURLS_FLOOD_DELAY_SECONDS', 0);
             // Disable flood check
             $opath = get_option('yourlspath');
             $ypath = str_replace('user', 'includes', $opath);
             if (file_exists(dirname($ypath) . '/load-yourls.php')) {
                 // YOURLS 1.4+
                 require_once dirname($ypath) . '/load-yourls.php';
                 global $ydb;
                 if (function_exists('yourls_add_new_link')) {
                     $yourls_result = yourls_add_new_link($url, $keyword_format, $thisposttitle);
                 } else {
                     $yourls_result = $url;
                 }
             } else {
                 // YOURLS 1.3
                 if (file_exists(get_option('yourslpath'))) {
                     require_once get_option('yourlspath');
                     $yourls_db = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
                     $yourls_result = yourls_add_new_link($url, $keyword_format, $yourls_db);
                 }
             }
             if ($yourls_result) {
                 $shrink = $yourls_result['shorturl'];
             } else {
                 $shrink = false;
             }
             break;
         case 6:
             // remote YOURLS installation
             $yourlslogin = trim(get_option('yourlslogin'));
             $yourlsapi = stripcslashes(get_option('yourlsapi'));
             $api_url = sprintf(get_option('yourlsurl') . '?username=%s&password=%s&url=%s&format=json&action=shorturl&keyword=%s', $yourlslogin, $yourlsapi, $encoded, $keyword_format);
             $json = jd_remote_json($api_url, false);
             if ($json) {
                 $shrink = $json->shorturl;
             } else {
                 $shrink = false;
             }
             break;
         case 7:
             $suprapi = trim(get_option('suprapi'));
             $suprlogin = trim(get_option('suprlogin'));
             if ($suprapi != '') {
                 $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $encoded . "&login="******"&apiKey=" . $suprapi);
             } else {
                 $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $encoded);
             }
             if ($decoded['statusCode'] == 'OK') {
                 $page = str_replace("&", "&#38;", urldecode($url));
                 $shrink = $decoded['results'][$page]['shortUrl'];
                 $error = $decoded['errorMessage'];
             } else {
                 $shrink = false;
                 $error = $decoded['errorMessage'];
             }
             if (!is_valid_url($shrink)) {
                 $shrink = false;
             }
             break;
         case 8:
             // Goo.gl
             $target = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBSnqQOg3vX1gwR7y2l-40yEG9SZiaYPUQ";
             $body = "{'longUrl':'{$url}'}";
             //$body = json_encode($data);
             $json = jd_fetch_url($target, 'POST', $body, 'Content-Type: application/json');
             $decoded = json_decode($json);
             //$url = $decoded['id'];
             $shrink = $decoded->id;
             if (!is_valid_url($shrink)) {
                 $shrink = false;
             }
             break;
         case 9:
             // Twitter Friendly Links
             $shrink = $url;
             if (function_exists('twitter_link')) {
                 // use twitter_link if available
                 $shrink = twitter_link($post_ID);
             }
             break;
         case 10:
             // jotURL
             //jotURL, added: 2013-04-10
             $joturlapi = trim(get_option('joturlapi'));
             $joturllogin = trim(get_option('joturllogin'));
             $joturl_longurl_params = trim(get_option('joturl_longurl_params'));
             if ($joturl_longurl_params != '') {
                 if (strpos($url, "%3F") === FALSE && strpos($url, "?") === FALSE) {
                     $ct = "?";
                 } else {
                     $ct = "&";
                 }
                 $url .= $ct . $joturl_longurl_params;
                 $encoded = urlencode(urldecode(trim($url)));
                 // prevent double-encoding
             }
             //\jotURL
             $decoded = jd_fetch_url("https://api.joturl.com/a/v1/shorten?url=" . $encoded . "&login="******"&key=" . $joturlapi . "&format=plain");
             if ($decoded !== false) {
                 $shrink = $decoded;
                 //jotURL, added: 2013-04-10
                 $joturl_shorturl_params = trim(get_option('joturl_shorturl_params'));
                 if ($joturl_shorturl_params != '') {
                     if (strpos($shrink, "%3F") === FALSE && strpos($shrink, "?") === FALSE) {
                         $ct = "?";
                     } else {
                         $ct = "&";
                     }
                     $shrink .= $ct . $joturl_shorturl_params;
                 }
                 //\jotURL
             } else {
                 $error = $decoded;
                 $shrink = false;
             }
             if (!is_valid_url($shrink)) {
                 $shrink = false;
             }
             break;
             update_option('wpt_shortener_status', "{$shrink} : {$error}");
     }
     if (!$testmode) {
         if ($shrink === false || filter_var($shrink, FILTER_VALIDATE_URL) === false) {
             update_option('wp_url_failure', '1');
             $shrink = urldecode($url);
         } else {
             update_option('wp_url_failure', '0');
         }
     }
     wpt_store_url($post_ID, $shrink);
     return $shrink;
 }
Exemple #8
0
function wp_ozh_yourls_api_call($api, $url)
{
    global $wp_ozh_yourls;
    $shorturl = '';
    switch ($api) {
        case 'yourls-local':
            global $yourls_reserved_URL;
            require_once $wp_ozh_yourls['yourls_path'];
            $yourls_db = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
            $yourls_result = yourls_add_new_link($url, '', $yourls_db);
            if ($yourls_result) {
                $shorturl = $yourls_result['shorturl'];
            }
            break;
        case 'yourls-remote':
            $api_url = sprintf($wp_ozh_yourls['yourls_url'] . '?username=%s&password=%s&url=%s&format=json&action=shorturl', $wp_ozh_yourls['yourls_login'], $wp_ozh_yourls['yourls_password'], urlencode($url));
            $json = wp_ozh_yourls_remote_json($api_url);
            if ($json) {
                $shorturl = $json->shorturl;
            }
            break;
        case 'bitly':
            $api_url = sprintf('http://api.bit.ly/shorten?version=2.0.1&longUrl=%s&login=%s&apiKey=%s', urlencode($url), $wp_ozh_yourls['bitly_login'], $wp_ozh_yourls['bitly_password']);
            $json = wp_ozh_yourls_remote_json($api_url);
            if ($json) {
                $shorturl = $json->results->{$url}->shortUrl;
            }
            // bit.ly's API makes ugly JSON, seriously, tbh
            break;
        case 'rply':
            $api_url = sprintf('http://rp.ly/api/trim_url.json?url=%s&username=%s&password=%s', urlencode($url), $wp_ozh_yourls['rply_login'], $wp_ozh_yourls['rply_password']);
            $json = wp_ozh_yourls_remote_json($api_url);
            if ($json) {
                $shorturl = $json->url;
            }
            break;
        case 'trim':
            $api_url = sprintf('http://api.tr.im/api/trim_url.json?url=%s&username=%s&password=%s', urlencode($url), $wp_ozh_yourls['trim_login'], $wp_ozh_yourls['trim_password']);
            $json = wp_ozh_yourls_remote_json($api_url);
            if ($json) {
                $shorturl = $json->url;
            }
            break;
        case 'pingfm':
            $api_url = 'http://api.ping.fm/v1/url.create';
            $body = array('api_key' => 'd0e1aad9057142126728c3dcc03d7edb', 'user_app_key' => $wp_ozh_yourls['pingfm_user_app_key'], 'long_url' => $url);
            $xml = wp_ozh_yourls_fetch_url($api_url, 'POST', $body);
            if ($xml) {
                preg_match_all('!<short_url>[^<]+</short_url>!', $xml, $matches);
                $shorturl = $matches[0][0];
            }
            break;
        case 'tinyurl':
            $api_url = sprintf('http://tinyurl.com/api-create.php?url=%s', urlencode($url));
            $shorturl = wp_ozh_yourls_remote_simple($api_url);
            break;
        case 'isgd':
            $api_url = sprintf('http://is.gd/api.php?longurl=%s', urlencode($url));
            $shorturl = wp_ozh_yourls_remote_simple($api_url);
            break;
        default:
            die('Error, unknown service: ' . $api);
    }
    // at this point, if ($shorturl), it should contain expected short URL. Potential TODO: deal with edge cases?
    return $shorturl;
}
function jd_shorten_link($thispostlink, $thisposttitle, $post_ID, $testmode = 'false')
{
    // filter link before sending to shortener or adding analytics
    $thispostlink = apply_filters('wpt_shorten_link', $thispostlink, $post_ID);
    $suprapi = trim(get_option('suprapi'));
    $suprlogin = trim(get_option('suprlogin'));
    $bitlyapi = trim(get_option('bitlyapi'));
    $bitlylogin = trim(strtolower(get_option('bitlylogin')));
    $yourlslogin = trim(get_option('yourlslogin'));
    $yourlsapi = stripcslashes(get_option('yourlsapi'));
    if ($testmode == 'false') {
        if (get_option('use-twitter-analytics') == 1 || get_option('use_dynamic_analytics') == 1) {
            if (get_option('use_dynamic_analytics') == '1') {
                $campaign_type = get_option('jd_dynamic_analytics');
                if ($campaign_type == "post_category" && $testmode != 'link') {
                    $category = get_the_category($post_ID);
                    $campaign = $category[0]->cat_name;
                } else {
                    if ($campaign_type == "post_ID") {
                        $campaign = $post_ID;
                    } else {
                        if ($campaign_type == "post_title" && $testmode != 'link') {
                            $post = get_post($post_ID);
                            $campaign = $post->post_title;
                        } else {
                            if ($testmode != 'link') {
                                $post = get_post($post_ID);
                                $post_author = $post->post_author;
                                $campaign = get_the_author_meta('user_login', $post_author);
                            } else {
                                $post_author = '';
                                $campaign = '';
                            }
                        }
                    }
                }
            } else {
                $campaign = get_option('twitter-analytics-campaign');
            }
            $campaign = urlencode($campaign);
            if (strpos($thispostlink, "%3F") === FALSE && strpos($thispostlink, "?") === FALSE) {
                $ct = "?";
            } else {
                $ct = "&";
            }
            $ga = "utm_campaign={$campaign}&utm_medium=twitter&utm_source=twitter";
            $thispostlink .= $ct .= $ga;
        }
    }
    $thispostlink = urldecode(trim($thispostlink));
    $thispostlink = urlencode($thispostlink);
    // custom word setting
    $keyword_format = get_option('jd_keyword_format') == '1' ? $post_ID : '';
    $keyword_format = get_option('jd_keyword_format') == '2' ? get_post_meta($post_ID, '_yourls_keyword', true) : $keyword_format;
    // Generate and grab the short url
    switch (get_option('jd_shortener')) {
        case 0:
        case 1:
            $shrink = urldecode($thispostlink);
        case 4:
            if (function_exists('wp_get_shortlink')) {
                $shrink = wp_get_shortlink($post_ID);
            } else {
                $shrink = urldecode($thispostlink);
            }
            break;
        case 2:
            // updated to v3 3/31/2010
            $decoded = jd_remote_json("http://api.bitly.com/v3/shorten?longUrl=" . $thispostlink . "&login="******"&apiKey=" . $bitlyapi . "&format=json");
            $error = '';
            if ($decoded) {
                if ($decoded['status_code'] != 200) {
                    $shrink = $decoded;
                    $error = $decoded['status_txt'];
                } else {
                    $shrink = $decoded['data']['url'];
                }
            } else {
                $shrink = false;
                update_option('wp_bitly_error', "JSON result could not be decoded");
            }
            if (!is_valid_url($shrink)) {
                $shrink = false;
                update_option('wp_bitly_error', $error);
            }
            break;
        case 3:
            $shrink = urldecode($thispostlink);
            break;
        case 5:
            // local YOURLS installation
            $thispostlink = urldecode($thispostlink);
            global $yourls_reserved_URL;
            define('YOURLS_INSTALLING', true);
            // Pretend we're installing YOURLS to bypass test for install or upgrade
            define('YOURLS_FLOOD_DELAY_SECONDS', 0);
            // Disable flood check
            $opath = get_option('yourlspath');
            $ypath = str_replace('user', 'includes', $opath);
            if (file_exists(dirname($ypath) . '/load-yourls.php')) {
                // YOURLS 1.4+
                global $ydb;
                require_once dirname($ypath) . '/load-yourls.php';
                if (function_exists('yourls_add_new_link')) {
                    $yourls_result = yourls_add_new_link($thispostlink, $keyword_format);
                } else {
                    $yourls_result = $thispostlink;
                }
            } else {
                // YOURLS 1.3
                require_once get_option('yourlspath');
                $yourls_db = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
                $yourls_result = yourls_add_new_link($thispostlink, $keyword_format, $yourls_db);
            }
            if ($yourls_result) {
                $shrink = $yourls_result['shorturl'];
            } else {
                $shrink = false;
            }
            break;
        case 6:
            // remote YOURLS installation
            $api_url = sprintf(get_option('yourlsurl') . '?username=%s&password=%s&url=%s&format=json&action=shorturl&keyword=%s', $yourlslogin, $yourlsapi, $thispostlink, $keyword_format);
            $json = jd_remote_json($api_url, false);
            if ($json) {
                $shrink = $json->shorturl;
            } else {
                $shrink = false;
            }
            break;
        case 7:
            if ($suprapi != '') {
                $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $thispostlink . "&login="******"&apiKey=" . $suprapi);
            } else {
                $decoded = jd_remote_json("http://su.pr/api/shorten?longUrl=" . $thispostlink);
            }
            update_option('wp_supr_error', "Su.pr API result: {$decoded}");
            if ($decoded['statusCode'] == 'OK') {
                $page = str_replace("&", "&#38;", urldecode($thispostlink));
                $shrink = $decoded['results'][$page]['shortUrl'];
                $error = $decoded['errorMessage'];
            } else {
                $shrink = false;
                $error = $decoded['errorMessage'];
                update_option('wp_supr_error', "JSON result could not be decoded");
            }
            if (!is_valid_url($shrink)) {
                $shrink = false;
                update_option('wp_supr_error', $error);
            }
            break;
        case 8:
            // Goo.gl
            $url = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBSnqQOg3vX1gwR7y2l-40yEG9SZiaYPUQ";
            $link = urldecode($thispostlink);
            $body = "{'longUrl':'{$link}'}";
            //$body = json_encode($data);
            $json = jd_fetch_url($url, 'POST', $body, 'Content-Type: application/json');
            $decoded = json_decode($json);
            //$url = $decoded['id'];
            $shrink = $decoded->id;
            if (!is_valid_url($shrink)) {
                $shrink = false;
            }
            break;
    }
    if ($testmode != 'true') {
        if ($shrink === false || stristr($shrink, "http://") === FALSE) {
            update_option('wp_url_failure', '1');
            $shrink = urldecode($thispostlink);
        } else {
            update_option('wp_url_failure', '0');
        }
    }
    return $shrink;
}
Exemple #10
0
<?php

define('YOURLS_API', true);
require_once dirname(__FILE__) . '/includes/load-yourls.php';
yourls_maybe_require_auth();
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
switch ($action) {
    case 'shorturl':
        $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return = yourls_add_new_link($url, $keyword);
        $return['simple'] = isset($return['shorturl']) ? $return['shorturl'] : '';
        // This one will be used in case output mode is 'simple'
        unset($return['html']);
        // in API mode, no need for our internal HTML output
        break;
    case 'stats':
        $filter = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : '';
        $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : '';
        $return = yourls_api_stats($filter, $limit);
        break;
    case 'url-stats':
        $shorturl = isset($_REQUEST['shorturl']) ? $_REQUEST['shorturl'] : '';
        $return = yourls_api_url_stats($shorturl);
        break;
    case 'expand':
        $shorturl = isset($_REQUEST['shorturl']) ? $_REQUEST['shorturl'] : '';
        $return = yourls_api_expand($shorturl);
        break;
    default:
        $return = array('errorCode' => 400, 'message' => 'Unknown or missing "action" parameter', 'simple' => 'Unknown or missing "action" parameter');
function my_upload_and_shorten_save_files()
{
    // once again for translations:
    $my_upload_and_shorten_domain = 'upload-and-shorten';
    // did the user select any file?
    if ($_FILES['file_upload']['error'] == UPLOAD_ERR_NO_FILE) {
        return yourls_esc_html__('You need to select a file to upload.', $my_upload_and_shorten_domain);
    }
    // yes!
    $my_url = SHARE_URL;
    // has to be defined in user/config.php like this:
    // define( 'SHARE_URL', 'http://my.domain.tld/directory/' );
    $my_uploaddir = SHARE_DIR;
    // has to be defined in user/config.php like this:
    // define( 'SHARE_DIR', '/full/path/to/httpd/directory/' );
    $my_extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
    $my_filename = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
    if (isset($_POST['randomize_filename'])) {
        // make up a random name for the uploaded file
        // see http://www.mattytemple.com/projects/yourls-share-files/?replytocom=26686#respond
        $my_safe_filename = substr(md5($my_filename . strtotime("now")), 0, 12);
        // end randomize filename
    } else {
        // original code:
        $my_filename_trim = trim($my_filename);
        $my_RemoveChars = array("([ ])", "([^a-zA-Z0-9-])", "(-{2,})");
        $my_ReplaceWith = array("-", "", "-");
        $my_safe_filename = preg_replace($my_RemoveChars, $my_ReplaceWith, $my_filename_trim);
        // end original code
    }
    // avoid duplicate filenames
    $my_count = 2;
    $my_path = $my_uploaddir . $my_safe_filename . '.' . $my_extension;
    $my_final_file_name = $my_safe_filename . '.' . $my_extension;
    while (file_exists($my_path)) {
        $my_path = $my_uploaddir . $my_safe_filename . '-' . $my_count . '.' . $my_extension;
        $my_final_file_name = $my_safe_filename . '-' . $my_count . '.' . $my_extension;
        $my_count++;
    }
    // move the file from /tmp/ to destination and initiate link creation
    if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $my_path)) {
        $my_custom_keyword = NULL;
        if (isset($_POST['custom_keyword']) && $_POST['custom_keyword'] != '') {
            $my_custom_keyword = $_POST['custom_keyword'];
        }
        $my_short_url = yourls_add_new_link($my_url . $my_final_file_name, $my_custom_keyword, $my_final_file_name);
        return yourls_esc_html__('Upload finished. This is your short-URL: ', $my_upload_and_shorten_domain) . '<a href="' . $my_short_url['shorturl'] . '" target="_blank">' . $my_short_url['shorturl'] . '</a></strong>';
    } else {
        return yourls_esc_html__('Upload failed! Something went wrong, sorry! :(', $my_upload_and_shorten_domain);
    }
}
 // No sanitization needed here: everything happens in yourls_add_new_link()
 if (isset($_GET['u'])) {
     // Old school bookmarklet: ?u=<url>
     $url = rawurldecode($_GET['u']);
 } else {
     // New style bookmarklet: ?up=<url protocol>&us=<url slashes>&ur=<url rest>
     $url = rawurldecode($_GET['up'] . $_GET['us'] . $_GET['ur']);
 }
 $keyword = isset($_GET['k']) ? $_GET['k'] : '';
 $title = isset($_GET['t']) ? $_GET['t'] : '';
 $pass = isset($_GET['p']) ? $_GET['p'] : '';
 $return = yourls_add_new_link($url, $keyword, $title, $pass);
 // If fails because keyword already exist, retry with no keyword
 if (isset($return['status']) && $return['status'] == 'fail' && isset($return['code']) && $return['code'] == 'error:keyword') {
     $msg = $return['message'];
     $return = yourls_add_new_link($url, '', $ydb, $pass);
     $return['message'] .= ' (' . $msg . ')';
 }
 // Stop here if bookmarklet with a JSON callback function
 if (isset($_GET['jsonp']) && $_GET['jsonp'] == 'yourls') {
     $short = $return['shorturl'] ? $return['shorturl'] : '';
     $message = $return['message'];
     yourls_content_type_header('application/javascript');
     echo yourls_apply_filter('bookmarklet_jsonp', "yourls_callback({'short_url':'{$short}','message':'{$message}'});");
     die;
 }
 // Now use the URL that has been sanitized and returned by yourls_add_new_link()
 $url = $return['url']['url'];
 $where = sprintf(" AND `url` LIKE '%s' ", yourls_escape($url));
 $page = $total_pages = $perpage = 1;
 $offset = 0;