get_row("SELECT `url` FROM `$table` WHERE `id` = '$id';"); $safe_url = stripslashes($url->url); $keyword = yourls_int2string($id); if($url) { $return = << Edit: original URL: short URL:   RETURN; } else { $return = 'Invalid URL ID'; } return $return; } // Add a link row function yourls_table_add_row( $id, $keyword, $url, $ip, $clicks, $timestamp ) { $date = date( 'Y M d H:i', $timestamp+( yourls_HOURS_OFFSET * 3600) ); $clicks = number_format($clicks); $www = YOURLS_SITE; return << $keyword $url $www/$keyword $date $ip $clicks   ROW; } // Get next id a new link will have if no custom keyword provided function yourls_get_next_decimal($db) { $table = YOURLS_DB_TABLE_NEXTDEC; return $db->get_var("SELECT `next_id` FROM `$table`"); } // Update id for next link with no custom keyword function yourls_update_next_decimal($int = '', $db) { $int = ( $int == '' ) ? 'next+1' : (int)$int ; $table = YOURLS_DB_TABLE_NEXTDEC; return $db->query("UPDATE `$table` set next_id=$int"); } // Delete a link in the DB function yourls_delete_link_by_id($id, $db) { $table = YOURLS_DB_TABLE_URL; $id = yourls_intval($id); return $db->query("DELETE FROM `$table` WHERE `id` = $id;"); } // 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, $id, $db) { $table = YOURLS_DB_TABLE_URL; $timestamp = date('Y-m-d H:i:s'); $ip = yourls_get_IP(); return $db->query("INSERT INTO `$table` VALUES($id, '$url', '$timestamp', '$ip', 0);"); } // Add a new link in the DB, either with custom keyword, or find one function yourls_add_new_link($url, $keyword = '', $db) { if ( !$url or $url == 'http://' ) { $return['status'] = 'fail'; $return['message'] = 'Missing URL input'; return $return; } $table = YOURLS_DB_TABLE_URL; $url = mysql_real_escape_string(yourls_sanitize_url($url)); $strip_url = stripslashes($url); $url_exists = $db->get_row("SELECT id,url FROM `$table` WHERE `url` = '".$strip_url."';"); $ip = yourls_get_IP(); $return = array(); // New URL : store it if( !$url_exists ) { // Custom keyword provided if ($keyword) { $keyword = yourls_sanitize_string($keyword); if (!yourls_keyword_is_free($keyword, $db)) { // This id either reserved or taken already $return['status'] = 'fail'; $return['message'] = 'URL id '.$keyword.' already exists in database or is reserved'; } else { // all clear, store ! $id = yourls_string2int($keyword); yourls_insert_link_in_db($url, $id, $db); $return['url'] = array('id' => $id, 'keyword' => $keyword, 'url' => $strip_url, 'date' => date('Y-m-d H:i:s'), 'ip' => yourls_get_IP() ); $return['status'] = 'success'; $return['message'] = $strip_url.' (ID: '.$keyword.') added to database'; $return['html'] = yourls_table_add_row( $id, $keyword, $url, yourls_get_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($db); do { $add_url = yourls_insert_link_in_db($url, $id, $db); $free = !yourls_is_reserved_id( $id ); $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_id( $id, $db ); $return['extra_info'] .= '(deleted '.$id.')'; } else { // everything ok, populate needed vars $keyword = yourls_int2string($id); $return['url'] = array('id' => $id, 'keyword' => $keyword, 'url' => $strip_url, 'date' => $timestamp, 'ip' => $ip); $return['status'] = 'success'; $return['message'] = $strip_url.' (ID: '.$id.') added to database'; $return['html'] = yourls_table_add_row( $id, $keyword, $url, $ip, 0, time() ); $return['shorturl'] = YOURLS_SITE .'/'. $keyword; } $id++; } while (!$ok); yourls_update_next_decimal($id, $db); } } else { // URL was already stored $return['status'] = 'fail'; $return['message'] = $strip_url.' already exists in database'; $return['shorturl'] = YOURLS_SITE .'/'. yourls_int2string( $url_exists->id ); } return $return; } // Edit a link function yourls_edit_link($url, $id, $keyword='', $db) { $table = YOURLS_DB_TABLE_URL; $url = mysql_real_escape_string(yourls_sanitize_url($url)); $id = yourls_intval($id); $strip_url = stripslashes($url); $old_url = $db->get_var("SELECT `url` FROM `$table` WHERE `id` = '".$id."';"); // Check if new URL is not here already if ($old_url != $url) { $url_exists = intval($db->get_var("SELECT id FROM `$table` WHERE `url` = '".$strip_url."';")); } else { $url_exists = false; } // Check if the new keyword is not here already $newid = ( $keyword ? yourls_string2int($keyword) : $id ); if ($newid != $id) { $id_exists = intval($db->get_var("SELECT id FROM `$table` WHERE `id` = '".$newid."';")); $id_free = yourls_keyword_is_free($keyword, $db); $id_is_ok = ($id_exists == 0) && $id_free; } else { $id_is_ok = true; } // All clear, update if($url_exists == 0 && $id_is_ok ) { $timestamp4screen = date( 'Y M d H:i', time()+( yourls_HOURS_OFFSET * 3600) ); $timestamp4db = date('Y-m-d H:i:s', time()+( yourls_HOURS_OFFSET * 3600) ); $update_url = $db->query("UPDATE `$table` SET `url` = '$url', `timestamp` = '$timestamp4db', `id` = '$newid' WHERE `id` = $id;"); if($update_url) { $return['url'] = array('id' => $newid, 'keyword' => $keyword, 'shorturl' => YOURLS_SITE.'/'.$keyword, 'url' => $strip_url, 'date' => $timestamp4screen); $return['status'] = 'success'; $return['message'] = 'Link updated in database'; } else { $return['status'] = 'fail'; $return['message'] = 'Error updating '.$strip_url.' (ID: '.$id.') to database'; } // Nope } else { $return['status'] = 'fail'; $return['message'] = 'URL or keyword already exists in database'; } return $return; } // Check if keyword id is free (ie not already taken, and not reserved) function yourls_keyword_is_free($str, $db) { $table = YOURLS_DB_TABLE_URL; $id = yourls_string2int($str); if ( yourls_is_reserved_id($id) ) return false; $already_exists = intval($db->get_var("SELECT `id` FROM `$table` WHERE `id` = '".$id."';")); if ( $already_exists ) return false; return true; } // Display a page function yourls_page($page) { $include = dirname(dirname(__FILE__))."/pages/$page.php"; if (!file_exists($include)) { die("Page '$page' not found"); } include($include); die(); } // Connect to DB function yourls_db_connect() { if (!defined('YOURLS_DB_USER') or !defined('YOURLS_DB_PASS') or !defined('YOURLS_DB_NAME') or !defined('YOURLS_DB_HOST') or !class_exists('ezSQL_mysql') ) die ('DB config/class missing'); return new ezSQL_mysql(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST); } // Return JSON output. Compatible with PHP prior to 5.2 function yourls_json_encode($array) { if (function_exists('json_encode')) { return json_encode($array); } else { require_once(dirname(__FILE__).'/functions-json.php'); return yourls_array_to_json($array); } } // Return XML output. function yourls_xml_encode($array) { require_once(dirname(__FILE__).'/functions-xml.php'); $converter= new yourls_array2xml; return $converter->array2xml($array); } // Return array for API stat requests function yourls_api_stats( $filter, $limit, $db ) { switch( $filter ) { case 'bottom': $sort_by = 'clicks'; $sort_order = 'asc'; break; case 'last': $sort_by = 'timestamp'; $sort_order = 'desc'; break; case 'top': default: $sort_by = 'clicks'; $sort_order = 'desc'; break; } $limit = intval( $limit ); $table_url = YOURLS_DB_TABLE_URL; $results = $db->get_results("SELECT * FROM $table_url WHERE 1=1 ORDER BY $sort_by $sort_order LIMIT 0, $limit;"); $return = array(); $i = 1; foreach ($results as $res) { $return['links']['link_'.$i++] = array( 'shorturl' => YOURLS_SITE .'/'. yourls_int2string($res->id), 'url' => $res->url, 'timestamp' => $res->timestamp, 'ip' => $res->ip, 'clicks' => $res->clicks ); } $totals = $db->get_row("SELECT COUNT(id) as c, SUM(clicks) as s FROM $table_url WHERE 1=1"); $return['stats'] = array( 'total_links' => $totals->c, 'total_clicks' => $totals->s ); return $return; } // Return API result. Dies after this function yourls_api_output( $mode, $return ) { switch ( $mode ) { case 'json': header('Content-type: application/json'); echo yourls_json_encode($return); break; case 'xml': header('Content-type: application/xml'); echo yourls_xml_encode($return); break; case 'simple': default: echo $return['shorturl']; break; } die(); }