From a8db6e49d0850ed7855cda8e9b545b72c363719b Mon Sep 17 00:00:00 2001 From: ozhozh Date: Fri, 25 Jun 2010 15:40:05 +0000 Subject: [PATCH] Save title in database - updated saving/editing functions - updated install/upgrade functions - updated display - updated ajax git-svn-id: http://yourls.googlecode.com/svn/trunk@415 12232710-3e20-11de-b438-597f59cd7555 --- admin/index.php | 11 ++-- admin/index_ajax.php | 2 +- css/share.css | 7 +++ css/style.css | 3 + includes/functions-html.php | 4 +- includes/functions-install.php | 1 + includes/functions-upgrade.php | 11 +++- includes/functions.php | 102 +++++++++++++++++++++++---------- includes/version.php | 2 +- js/insert.js | 15 ++++- 10 files changed, 113 insertions(+), 45 deletions(-) diff --git a/admin/index.php b/admin/index.php index 4075f21..c989c76 100644 --- a/admin/index.php +++ b/admin/index.php @@ -214,7 +214,7 @@ if ( !$is_bookmark ) { yourls_share_box( '', '', '', '', '

Your short link

', '

Quick Share

', true ); } else { - echo '

' . $return['message'] . '

'; + echo ''; } ?> @@ -258,13 +258,14 @@ $url_results = $ydb->get_results("SELECT * FROM `$table_url` WHERE 1=1 $where ORDER BY `$sort_by_sql` $sort_order_sql LIMIT $offset, $perpage;"); if( $url_results ) { foreach( $url_results as $url_result ) { - $keyword = yourls_sanitize_string($url_result->keyword); - $timestamp = strtotime($url_result->timestamp); - $url = stripslashes($url_result->url); + $keyword = yourls_sanitize_string( $url_result->keyword ); + $timestamp = strtotime( $url_result->timestamp ); + $url = stripslashes( $url_result->url ); $ip = $url_result->ip; + $title = $url_result->title ? $url_result->title : ''; $clicks = $url_result->clicks; - echo yourls_table_add_row( $keyword, $url, $ip, $clicks, $timestamp ); + echo yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp ); } } else { echo 'No URL Found'; diff --git a/admin/index_ajax.php b/admin/index_ajax.php index 9385720..69975e6 100644 --- a/admin/index_ajax.php +++ b/admin/index_ajax.php @@ -21,7 +21,7 @@ break; case 'edit_save': - $return = yourls_edit_link( $_REQUEST['url'], $_REQUEST['keyword'], $_REQUEST['newkeyword'] ); + $return = yourls_edit_link( $_REQUEST['url'], $_REQUEST['keyword'], $_REQUEST['newkeyword'], $_REQUEST['title'] ); echo json_encode($return); break; diff --git a/css/share.css b/css/share.css index 888ae88..6ed1d67 100644 --- a/css/share.css +++ b/css/share.css @@ -17,6 +17,13 @@ div.share { float:left; height:140px; } +#origlink{ + display:inline-block; + white-space:pre; + width:183px; + overflow:hidden; + vertical-align:-2px; +} #copybox { width:250px; } diff --git a/css/style.css b/css/style.css index 66bd714..e76b20e 100644 --- a/css/style.css +++ b/css/style.css @@ -129,6 +129,9 @@ tr.edit-row td { #new_url #feedback .fail { color:#f55; } +td.url small a{ + color:#bbc; +} td.actions input, td.actions a { visibility:hidden; } diff --git a/includes/functions-html.php b/includes/functions-html.php index 566fa28..607990c 100644 --- a/includes/functions-html.php +++ b/includes/functions-html.php @@ -245,9 +245,9 @@ function yourls_share_box( $longurl, $shorturl, $title='', $text='', $shortlink_

-

Original link: +

Long link: -
Real time stats: + +
Stats: +

diff --git a/includes/functions-install.php b/includes/functions-install.php index 88298a0..29a7e46 100644 --- a/includes/functions-install.php +++ b/includes/functions-install.php @@ -129,6 +129,7 @@ function yourls_create_sql_tables() { 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('. '`keyword` varchar(200) BINARY NOT NULL,'. '`url` text BINARY NOT NULL,'. + '`title` text BINARY ,'. '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'. '`ip` VARCHAR(41) NOT NULL,'. '`clicks` INT(10) UNSIGNED NOT NULL,'. diff --git a/includes/functions-upgrade.php b/includes/functions-upgrade.php index 7dbfbdf..dadccb2 100644 --- a/includes/functions-upgrade.php +++ b/includes/functions-upgrade.php @@ -7,7 +7,7 @@ function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) { 1.4 200 1.4.1 210 1.4.3 220 - 1.5 240 + 1.5 250 */ // special case for 1.3: the upgrade is a multi step procedure @@ -26,7 +26,7 @@ function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) { if( $oldsql < 220 ) yourls_upgrade_to_143(); - if( $oldsql < 240 ) + if( $oldsql < 250 ) yourls_upgrade_to_15(); yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=3" ) ); @@ -50,6 +50,13 @@ function yourls_upgrade_to_15( ) { yourls_add_option( 'active_plugins', array() ); echo "

Enabling the plugin API. Please wait...

"; + // Alter URL table to store titles + global $ydb; + $table_url = YOURLS_DB_TABLE_URL; + $sql = "ALTER TABLE `$table_url` ADD `title` TEXT BINARY AFTER `url`;"; + $ydb->query( $sql ); + echo "

Updating table structure. Please wait...

"; + // Update .htaccess yourls_create_htaccess(); echo "

Updating .htaccess file. Please wait...

"; diff --git a/includes/functions.php b/includes/functions.php index d707575..db2a63b 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -81,6 +81,15 @@ function yourls_sanitize_keyword( $keyword ) { return yourls_sanitize_string( $keyword ); } +// Sanitize a page title. No HTML per W3C http://www.w3.org/TR/html401/struct/global.html#h-7.4.2 +function yourls_sanitize_title( $title ) { + // TODO: make stronger. Implement KSES? + $title = strip_tags( $title ); + return $title; +} + + + // Is an URL a short URL? function yourls_is_shorturl( $shorturl ) { // TODO: make sure this function evolves with the feature set. @@ -218,33 +227,48 @@ function yourls_table_edit_row( $keyword ) { $table = YOURLS_DB_TABLE_URL; $keyword = yourls_sanitize_string( $keyword ); $id = yourls_string2int( $keyword ); // used as HTML #id - $url = $ydb->get_row("SELECT `url` FROM `$table` WHERE `keyword` = '$keyword';"); - $safe_url = stripslashes( $url->url ); + $url = yourls_get_keyword_longurl( $keyword ); + $title = yourls_get_keyword_title( $keyword ); + $safe_url = stripslashes( $url ); + $safe_title = stripslashes( $title ); $www = YOURLS_SITE; if( $url ) { $return = <<Original URL: Short URL: $www/  +Original URL: Short URL: $www/
Title:   RETURN; } else { $return = 'Error, URL not found'; } - $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url ); + $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title ); return $return; } // Add a link row -function yourls_table_add_row( $keyword, $url, $ip, $clicks, $timestamp ) { - $keyword = yourls_sanitize_string( $keyword ); - $id = yourls_string2int( $keyword ); // used as HTML #id - $date = date( 'M d, Y H:i', $timestamp+( YOURLS_HOURS_OFFSET * 3600) ); - $clicks = number_format($clicks, 0, '', ''); - $shorturl = YOURLS_SITE.'/'.$keyword; +function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) { + $keyword = yourls_sanitize_string( $keyword ); + $display_keyword = htmlentities( $keyword ); + + $url = yourls_sanitize_url( $url ); $display_url = htmlentities( yourls_trim_long_string( $url ) ); + + $title = yourls_sanitize_title( $title ) ; + $display_title = htmlentities( yourls_trim_long_string( $title ) ); + + $id = yourls_string2int( $keyword ); // used as HTML #id + $date = date( 'M d, Y H:i', $timestamp+( YOURLS_HOURS_OFFSET * 3600) ); + $clicks = number_format($clicks, 0, '', ''); + + $shorturl = YOURLS_SITE.'/'.$keyword; $statlink = $shorturl.'+'; - $url = htmlentities( $url ); + + if( $title ) { + $display_link = "$display_title
$display_url"; + } else { + $display_link = "$display_url"; + } $actions = <<      @@ -252,9 +276,9 @@ function yourls_table_add_row( $keyword, $url, $ip, $clicks, $timestamp ) { $actions = yourls_apply_filter( 'action_links', $actions, $keyword, $url, $ip, $clicks, $timestamp ); $row = <<$keyword$display_url$date$ip$clicks$actions +$display_keyword$display_link$date$ip$clicks$actions ROW; - $row = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $ip, $clicks, $timestamp ); + $row = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $title, $ip, $clicks, $timestamp ); return $row; } @@ -283,16 +307,20 @@ function yourls_delete_link_by_keyword( $keyword ) { return $delete; } -// SQL query to insert a new link in the DB. Needs sanitized data. Returns boolean for success or failure of the inserting -function yourls_insert_link_in_db($url, $keyword) { +// SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting +function yourls_insert_link_in_db( $url, $keyword, $title = '' ) { global $ydb; + + $url = addslashes( yourls_sanitize_url( $url ) ); + $keyword = addslashes( yourls_sanitize_keyword( $keyword ) ); + $title = addslashes( yourls_sanitize_title( $title ) ); $table = YOURLS_DB_TABLE_URL; $timestamp = date('Y-m-d H:i:s'); $ip = yourls_get_IP(); - $insert = $ydb->query("INSERT INTO `$table` VALUES('$keyword', '$url', '$timestamp', '$ip', 0);"); + $insert = $ydb->query("INSERT INTO `$table` VALUES('$keyword', '$url', '$title', '$timestamp', '$ip', 0);"); - yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $timestamp, $ip ); + yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip ); return (bool)$insert; } @@ -336,6 +364,8 @@ function yourls_add_new_link( $url, $keyword = '' ) { // New URL : store it -- or: URL exists, but duplicates allowed if( !$url_exists || yourls_allow_duplicate_longurls() ) { + + $title = yourls_get_remote_title( $url ); // Custom keyword provided if ( $keyword ) { @@ -348,11 +378,12 @@ function yourls_add_new_link( $url, $keyword = '' ) { $return['message'] = 'Short URL '.$keyword.' already exists in database or is reserved'; } else { // all clear, store ! - yourls_insert_link_in_db($url, $keyword); - $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip ); + 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'] = $strip_url.' added to database'; - $return['html'] = yourls_table_add_row( $keyword, $url, $ip, 0, time() ); + $return['title'] = $title; + $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() ); $return['shorturl'] = YOURLS_SITE .'/'. $keyword; } @@ -365,7 +396,7 @@ function yourls_add_new_link( $url, $keyword = '' ) { $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); + $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) @@ -373,18 +404,20 @@ function yourls_add_new_link( $url, $keyword = '' ) { $return['extra_info'] .= '(deleted '.$keyword.')'; } else { // everything ok, populate needed vars - $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'date' => $timestamp, 'ip' => $ip ); + $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip ); $return['status'] = 'success'; $return['message'] = $strip_url.' added to database'; - $return['html'] = yourls_table_add_row( $keyword, $url, $ip, 0, time() ); + $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 { - // URL was already stored $return['status'] = 'fail'; $return['code'] = 'error:url'; $return['url'] = array( 'keyword' => $keyword, 'url' => $strip_url ); @@ -400,14 +433,16 @@ function yourls_add_new_link( $url, $keyword = '' ) { // Edit a link -function yourls_edit_link($url, $keyword, $newkeyword='') { +function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) { global $ydb; $table = YOURLS_DB_TABLE_URL; $url = yourls_escape(yourls_sanitize_url($url)); - $keyword = yourls_sanitize_string( $keyword ); - $newkeyword = yourls_sanitize_string( $newkeyword ); + $keyword = yourls_escape(yourls_sanitize_string( $keyword )); + $title = yourls_escape(yourls_sanitize_title( $title )); + $newkeyword = yourls_escape(yourls_sanitize_string( $newkeyword )); $strip_url = stripslashes($url); + $strip_title = stripslashes($title); $old_url = $ydb->get_var("SELECT `url` FROM `$table` WHERE `keyword` = '$keyword';"); $old_id = $id = yourls_string2int( $keyword ); $new_id = ( $newkeyword == '' ? $old_id : yourls_string2int( $newkeyword ) ); @@ -430,9 +465,9 @@ function yourls_edit_link($url, $keyword, $newkeyword='') { // All clear, update if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) { - $update_url = $ydb->query("UPDATE `$table` SET `url` = '$url', `keyword` = '$newkeyword' WHERE `keyword` = '$keyword';"); + $update_url = $ydb->query("UPDATE `$table` SET `url` = '$url', `keyword` = '$newkeyword', `title` = '$title' WHERE `keyword` = '$keyword';"); if( $update_url ) { - $return['url'] = array( 'keyword' => $newkeyword, 'shorturl' => YOURLS_SITE.'/'.$newkeyword, 'url' => $strip_url, 'display_url' => yourls_trim_long_string( $strip_url ), 'new_id' => $new_id ); + $return['url'] = array( 'keyword' => $newkeyword, 'shorturl' => YOURLS_SITE.'/'.$newkeyword, 'url' => $strip_url, 'display_url' => yourls_trim_long_string( $strip_url ), 'new_id' => $new_id, 'title' => $strip_title, 'display_title' => yourls_trim_long_string( $strip_title ) ); $return['status'] = 'success'; $return['message'] = 'Link updated in database'; } else { @@ -552,6 +587,11 @@ function yourls_get_keyword_info( $keyword, $field, $notfound = false ) { return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound ); } +// Return title associated with keyword. Optional $notfound = string default message if nothing found +function yourls_get_keyword_title( $keyword, $notfound = false ) { + return yourls_get_keyword_info( $keyword, 'title', $notfound ); +} + // Return long URL associated with keyword. Optional $notfound = string default message if nothing found function yourls_get_keyword_longurl( $keyword, $notfound = false ) { return yourls_get_keyword_info( $keyword, 'url', $notfound ); @@ -1432,10 +1472,10 @@ function yourls_get_remote_title( $url ) { // if title not found, guess if returned content was actually an error message if( $title == false && strpos( $content, 'Error' ) === 0 ) { $title = $content; - } else { - $title = $url; } + $title = yourls_sanitize_title( $title ); + return yourls_apply_filter( 'get_remote_title', $title ); } diff --git a/includes/version.php b/includes/version.php index 6a25a8a..2498365 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,5 +1,5 @@ \ No newline at end of file diff --git a/js/insert.js b/js/insert.js index ce77bb6..2e193f1 100644 --- a/js/insert.js +++ b/js/insert.js @@ -35,7 +35,8 @@ function add() { $('#copylink').val( data.shorturl ); $('#origlink').attr( 'href', data.url.url ).html( data.url.url ); $('#statlink').attr( 'href', data.shorturl+'+' ).html( data.shorturl+'+' ); - $('#tweet_body').val( data.shorturl ).keypress(); + var tweet = ( data.url.title ? data.url.title + ' ' + data.shorturl : data.shorturl ); + $('#tweet_body').val( tweet ).keypress(); $('#shareboxes').slideDown(); end_loading("#add-button"); @@ -98,14 +99,22 @@ function edit_save(id) { add_loading("#edit-close-" + id); var newurl = $("#edit-url-" + id).val(); var newkeyword = $("#edit-keyword-" + id).val(); + var title = $("#edit-title-" + id).val(); var keyword = $('#old_keyword_'+id).val(); var www = $('#yourls-site').val(); $.getJSON( "index_ajax.php", - {mode:'edit_save', url: newurl, keyword: keyword, newkeyword: newkeyword }, + {mode:'edit_save', url: newurl, keyword: keyword, newkeyword: newkeyword, title: title }, function(data){ if(data.status == 'success') { - $("#url-" + id).html('' + data.url.display_url + ''); + + if( data.url.title != '' ) { + var display_link = '' + data.url.display_title + '
' + data.url.display_url + ''; + } else { + var display_link = '' + data.url.display_url + ''; + } + + $("#url-" + id).html(display_link); $("#keyword-" + id).html('' + data.url.keyword + ''); $("#timestamp-" + id).html(data.url.date); $("#edit-" + id).fadeOut(200, function(){ -- 2.45.0