if (!isset($aggregate) && isset($_GET['all']) && $_GET['all'] == 1 && yourls_allow_duplicate_longurls()) { $aggregate = true; } if (!isset($keyword)) { yourls_do_action('infos_no_keyword'); yourls_redirect(YOURLS_SITE, 302); } // Get basic infos for this shortened URL $keyword = yourls_sanitize_string($keyword); $longurl = yourls_get_keyword_longurl($keyword); $clicks = yourls_get_keyword_clicks($keyword); $timestamp = yourls_get_keyword_timestamp($keyword); $title = yourls_get_keyword_title($keyword); // Update title if it hasn't been stored yet if ($title == '') { $title = yourls_get_remote_title($longurl); yourls_edit_link_title($keyword, $title); } if ($longurl === false) { yourls_do_action('infos_keyword_not_found'); yourls_redirect(YOURLS_SITE, 302); } yourls_do_action('pre_yourls_infos', $keyword); if (yourls_do_log_redirect()) { $table = YOURLS_DB_TABLE_LOG; $referrers = array(); $direct = $notdirect = 0; $countries = array(); $dates = array(); $list_of_days = array(); $list_of_months = array();
function ozh_toolbar_do($args) { global $ozh_toolbar; // Does this redirection need a toolbar? if (!$ozh_toolbar['do']) { return; } // Do we have a cookie stating the user doesn't want a toolbar? if (isset($_COOKIE['yourls_no_toolbar']) && $_COOKIE['yourls_no_toolbar'] == 1) { return; } // Get URL and page title $url = $args[0]; $pagetitle = yourls_get_keyword_title($ozh_toolbar['keyword']); // Update title if it hasn't been stored yet if ($pagetitle == '') { $pagetitle = yourls_get_remote_title($url); yourls_edit_link_title($ozh_toolbar['keyword'], $pagetitle); } $_pagetitle = htmlentities(yourls_get_remote_title($url)); $www = YOURLS_SITE; $ver = YOURLS_VERSION; $md5 = md5($url); $sql = yourls_get_num_queries(); // When was the link created (in days) $diff = abs(time() - strtotime(yourls_get_keyword_timestamp($ozh_toolbar['keyword']))); $days = floor($diff / (60 * 60 * 24)); if ($days == 0) { $created = 'today'; } else { $created = $days . ' ' . yourls_plural('day', $days) . ' ago'; } // How many hits on the page $hits = 1 + yourls_get_keyword_clicks($ozh_toolbar['keyword']); $hits = $hits . ' ' . yourls_plural('view', $hits); // Plugin URL (no URL is hardcoded) $pluginurl = YOURLS_PLUGINURL . '/' . yourls_plugin_basename(dirname(__FILE__)); // All set. Draw the toolbar itself. echo <<<PAGE <html> <head> \t<title>{$pagetitle} — YOURLS</title> \t<link rel="icon" type="image/gif" href="{$www}/images/favicon.gif" /> \t<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> \t<meta http-equiv="X-UA-Compatible" content="chrome=1" /> \t<meta name="generator" content="YOURLS v{$ver}" /> \t<meta name="ROBOTS" content="NOINDEX, FOLLOW" /> \t<link rel="stylesheet" href="{$pluginurl}/css/toolbar.css" type="text/css" media="all" /> </head> <body> <div id="yourls-bar"> \t<div id="yourls-about"> \t\tShort link powered by <a href="http://yourls.org/">YOURLS</a> and created {$created}. {$hits}. \t\t<!-- {$sql} queries --> \t</div> \t \t<div id="yourls-delicious"> \t<img src="http://static.delicious.com/img/delicious.small.gif" height="10" width="10" alt="Delicious" /> \t<a id="yourls-delicious-link" title="Bookmark on delicious" href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;"> Bookmark on Delicious</a> \t</div> \t<script type="text/javascript" id="topsy_global_settings"> \tvar topsy_theme = "light-blue"; \tvar topsy_nick = " "; \tvar topsy_style = "small"; \tvar topsy_order = "count,retweet,badge"; \t</script> \t<div id="yourls-topsy" class="topsy_widget_data"> \t\t<!--{ \t\t "url": "{$www}/{$ozh_toolbar['keyword']}", \t\t "title": "{$_pagetitle}", \t\t}--> \t</div> \t \t<div id="yourls-selfclose"> \t\t<a id="yourls-once" href="{$url}" title="Close this toolbar">close</a> \t\t<a id="yourls-always" href="{$url}" title="Never show me this toolbar again">close</a> \t\t \t</div> </div> <iframe id="yourls-frame" frameborder="0" noresize="noresize" src="{$url}" name="yourlsFrame"></iframe> <script type="text/javascript" src="{$pluginurl}/js/toolbar.js"></script> <script type="text/javascript" src="http://cdn.topsy.com/topsy.js?init=topsyWidgetCreator"></script> <script type="text/javascript" src="http://feeds.delicious.com/v2/json/urlinfo/{$md5}?callback=yourls_get_books"></script> </body> </html> PAGE; // Don't forget to die, to interrupt the flow of normal events (ie redirecting to long URL) die; }
/** * Add a new link in the DB, either with custom keyword, or find one * */ function yourls_add_new_link($url, $keyword = '', $title = '') { // Allow plugins to short-circuit the whole function $pre = yourls_apply_filter('shunt_add_new_link', false, $url, $keyword, $title); if (false !== $pre) { return $pre; } $url = yourls_encodeURI($url); $url = yourls_escape(yourls_sanitize_url($url)); if (!$url || $url == 'http://' || $url == 'https://') { $return['status'] = 'fail'; $return['code'] = 'error:nourl'; $return['message'] = yourls__('Missing or malformed URL'); $return['errorCode'] = '400'; return yourls_apply_filter('add_new_link_fail_nourl', $return, $url, $keyword, $title); } // Prevent DB flood $ip = yourls_get_IP(); yourls_check_IP_flood($ip); // Prevent internal redirection loops: cannot shorten a shortened URL if (yourls_get_relative_url($url)) { if (yourls_is_shorturl($url)) { $return['status'] = 'fail'; $return['code'] = 'error:noloop'; $return['message'] = yourls__('URL is a short URL'); $return['errorCode'] = '400'; return yourls_apply_filter('add_new_link_fail_noloop', $return, $url, $keyword, $title); } } yourls_do_action('pre_add_new_link', $url, $keyword, $title); $strip_url = stripslashes($url); $return = array(); // duplicates allowed or new URL => store it if (yourls_allow_duplicate_longurls() || !($url_exists = yourls_url_exists($url))) { if (isset($title) && !empty($title)) { $title = yourls_sanitize_title($title); } else { $title = yourls_get_remote_title($url); } $title = yourls_apply_filter('add_new_title', $title, $url, $keyword); // Custom keyword provided if ($keyword) { yourls_do_action('add_new_link_custom_keyword', $url, $keyword, $title); $keyword = yourls_escape(yourls_sanitize_string($keyword)); $keyword = yourls_apply_filter('custom_keyword', $keyword, $url, $title); if (!yourls_keyword_is_free($keyword)) { // This shorturl either reserved or taken already $return['status'] = 'fail'; $return['code'] = 'error:keyword'; $return['message'] = yourls_s('Short URL %s already exists in database or is reserved', $keyword); } else { // all clear, store ! yourls_insert_link_in_db($url, $keyword, $title); $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip); $return['status'] = 'success'; $return['message'] = yourls_s('%s added to database', yourls_trim_long_string($strip_url)); $return['title'] = $title; $return['html'] = yourls_table_add_row($keyword, $url, $title, $ip, 0, time()); $return['shorturl'] = YOURLS_SITE . '/' . $keyword; } // Create random keyword } else { yourls_do_action('add_new_link_create_keyword', $url, $keyword, $title); $timestamp = date('Y-m-d H:i:s'); $id = yourls_get_next_decimal(); $ok = false; do { $keyword = yourls_int2string($id); $keyword = yourls_apply_filter('random_keyword', $keyword, $url, $title); if (yourls_keyword_is_free($keyword)) { if (@yourls_insert_link_in_db($url, $keyword, $title)) { // everything ok, populate needed vars $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip); $return['status'] = 'success'; $return['message'] = yourls_s('%s added to database', yourls_trim_long_string($strip_url)); $return['title'] = $title; $return['html'] = yourls_table_add_row($keyword, $url, $title, $ip, 0, time()); $return['shorturl'] = YOURLS_SITE . '/' . $keyword; } else { // database error, couldnt store result $return['status'] = 'fail'; $return['code'] = 'error:db'; $return['message'] = yourls_s('Error saving url to database'); } $ok = true; } $id++; } while (!$ok); @yourls_update_next_decimal($id); } // URL was already stored } else { yourls_do_action('add_new_link_already_stored', $url, $keyword, $title); $return['status'] = 'fail'; $return['code'] = 'error:url'; $return['url'] = array('keyword' => $url_exists->keyword, 'url' => $strip_url, 'title' => $url_exists->title, 'date' => $url_exists->timestamp, 'ip' => $url_exists->ip, 'clicks' => $url_exists->clicks); $return['message'] = yourls_s('%s already exists in database', yourls_trim_long_string($strip_url)); $return['title'] = $url_exists->title; $return['shorturl'] = YOURLS_SITE . '/' . $url_exists->keyword; } yourls_do_action('post_add_new_link', $url, $keyword, $title); $return['statusCode'] = 200; // regardless of result, this is still a valid request return yourls_apply_filter('add_new_link', $return, $url, $keyword, $title); }
function yourls_add_new_link($url, $keyword = '', $title = '') { global $ydb; if (!$url || $url == 'http://' || $url == 'https://') { $return['status'] = 'fail'; $return['code'] = 'error:nourl'; $return['message'] = 'Missing URL input'; $return['errorCode'] = '400'; yourls_do_action('add_new_link_fail_nourl'); return $return; } // Prevent DB flood $ip = yourls_get_IP(); yourls_check_IP_flood($ip); // Prevent internal redirection loops: cannot shorten a shortened URL $url = yourls_escape(yourls_sanitize_url($url)); if (preg_match('!^' . YOURLS_SITE . '/!', $url)) { if (yourls_is_shorturl($url)) { $return['status'] = 'fail'; $return['code'] = 'error:noloop'; $return['message'] = 'URL is a short URL'; $return['errorCode'] = '400'; yourls_do_action('add_new_link_fail_noloop'); return $return; } } yourls_do_action('pre_add_new_link', $url, $keyword); $table = YOURLS_DB_TABLE_URL; $strip_url = stripslashes($url); $url_exists = $ydb->get_row("SELECT * FROM `{$table}` WHERE `url` = '" . $strip_url . "';"); $return = array(); // New URL : store it -- or: URL exists, but duplicates allowed if (!$url_exists || yourls_allow_duplicate_longurls()) { if (isset($title) && !empty($title)) { $title = yourls_sanitize_title($title); } else { $title = yourls_get_remote_title($url); } $title = yourls_apply_filter('add_new_title', $title); // Custom keyword provided if ($keyword) { $keyword = yourls_escape(yourls_sanitize_string($keyword)); $keyword = yourls_apply_filter('custom_keyword', $keyword); if (!yourls_keyword_is_free($keyword)) { // This shorturl either reserved or taken already $return['status'] = 'fail'; $return['code'] = 'error:keyword'; $return['message'] = 'Short URL ' . $keyword . ' already exists in database or is reserved'; } else { // all clear, store ! yourls_insert_link_in_db($url, $keyword, $title); $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip); $return['status'] = 'success'; $return['message'] = yourls_trim_long_string($strip_url) . ' added to database'; $return['title'] = $title; $return['html'] = yourls_table_add_row($keyword, $url, $title, $ip, 0, time()); $return['shorturl'] = YOURLS_SITE . '/' . $keyword; } // Create random keyword } else { $timestamp = date('Y-m-d H:i:s'); $id = yourls_get_next_decimal(); $ok = false; do { $keyword = yourls_int2string($id); $keyword = yourls_apply_filter('random_keyword', $keyword); $free = yourls_keyword_is_free($keyword); $add_url = @yourls_insert_link_in_db($url, $keyword, $title); $ok = $free && $add_url; if ($ok === false && $add_url === 1) { // we stored something, but shouldn't have (ie reserved id) $delete = yourls_delete_link_by_keyword($keyword); $return['extra_info'] .= '(deleted ' . $keyword . ')'; } else { // everything ok, populate needed vars $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip); $return['status'] = 'success'; $return['message'] = yourls_trim_long_string($strip_url) . ' added to database'; $return['title'] = $title; $return['html'] = yourls_table_add_row($keyword, $url, $title, $ip, 0, time()); $return['shorturl'] = YOURLS_SITE . '/' . $keyword; } $id++; } while (!$ok); @yourls_update_next_decimal($id); } // URL was already stored } else { $return['status'] = 'fail'; $return['code'] = 'error:url'; $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $url_exists->title, 'date' => $url_exists->timestamp, 'ip' => $url_exists->ip, 'clicks' => $url_exists->clicks); $return['message'] = yourls_trim_long_string($strip_url) . ' already exists in database'; $return['title'] = $url_exists->title; $return['shorturl'] = YOURLS_SITE . '/' . $url_exists->keyword; } yourls_do_action('post_add_new_link', $url, $keyword); $return['statusCode'] = 200; // regardless of result, this is still a valid request return $return; }