? Entries.Log Index: shorturl.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/Attic/shorturl.admin.inc,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 shorturl.admin.inc --- shorturl.admin.inc 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.admin.inc 1 Dec 2009 09:55:16 -0000 @@ -1,16 +1,15 @@ 'textfield', @@ -27,9 +26,6 @@ function shorturl_admin_settings_form(&$ '#required' => TRUE, '#description' => t('3844 is approximately the first 3-characters long short URI. Indicate a number that is less or more, depending on your needs.'), ); - - return system_settings_form($form); } - Index: shorturl.engine.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.engine.inc,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 shorturl.engine.inc --- shorturl.engine.inc 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.engine.inc 1 Dec 2009 09:55:17 -0000 @@ -1,84 +1,91 @@ 15) return -1; - - $seq = str_split ($url); - if (!is_array($seq) || !(sizeof($seq)>0)) return -1; - - $seq = array_reverse(str_split ($url)); - - $i = 0; - foreach ($seq as $c) { - if (isset($mapping[$c])) { - $val = (int)$mapping[$c]; - $num += $val * pow($base, $i); - $i++; - } +function shorturl_decode_url($url) { + $num = 0; + $mapping = shorturl_base_decode_mapping(); + $base = sizeof($mapping); + + //There's just no chance encoded URL will ever be too long so if + //we get something like that - somebody is messing with us trying + //to eat up CPU cycles on decoding or cause some other kind of overflow. + if (drupal_strlen($url) > 15) { + return -1; + } + + $seq = str_split($url); + if (!is_array($seq) || !(sizeof($seq)>0)) { + return -1; + } + + $seq = array_reverse(str_split($url)); + + $i = 0; + foreach ($seq as $c) { + if (isset($mapping[$c])) { + $val = (int)$mapping[$c]; + $num += $val * pow($base, $i); + $i++; } - - return $num; + } + + return $num; } /** -* Encode number (index of the URL) into url using alpha-numeric mapping -*/ -function shorturl_encode_url ($var) { - + * Encode number (index of the URL) into url using alpha-numeric mapping + */ +function shorturl_encode_url($var) { $mapping = shorturl_base_encode_mapping(); $var = shorturl_base_encode($var, sizeof($mapping)); $arr = split('-', $var); - - if (empty($arr)) { return $mapping[0]; } - if (!is_array($arr)) { $arr = array($arr); } - + + if (empty($arr)) { + return $mapping[0]; + } + if (!is_array($arr)) { + $arr = array($arr); + } + $new_num = ""; foreach ($arr as $pos) { - $new_num .= $mapping[(int)trim($pos)]; + $new_num .= $mapping[(int) trim($pos)]; } - + return $new_num; - } function shorturl_base_decode_mapping() { @@ -86,16 +93,16 @@ function shorturl_base_decode_mapping() } /** -* The very first 4-char number is 238328 -* The very first 3-char number is 3844 -*/ + * The very first 4-char number is 238328 + * The very first 3-char number is 3844 + */ function shorturl_base_encode_mapping() { return array( - 'q','l','i','3','O','c','x','a','C','1','r','Y', - 'g','4','2','T','b','j','D','W','Z','B','K','k', - '0','8','9','X','I','5','N','R','n','Q','U','P', - 'E','m','y','V','p','d','v','w','f','G','7','A', - 'o','s','H','6','F','L','M','e','t','z','u','J', - 'S','h' + 'q', 'l', 'i', '3', 'O', 'c', 'x', 'a', 'C', '1', 'r', 'Y', + 'g', '4', '2', 'T', 'b', 'j', 'D', 'W', 'Z', 'B', 'K', 'k', + '0', '8', '9', 'X', 'I', '5', 'N', 'R', 'n', 'Q', 'U', 'P', + 'E', 'm', 'y', 'V', 'p', 'd', 'v', 'w', 'f', 'G', '7', 'A', + 'o', 's', 'H', '6', 'F', 'L', 'M', 'e', 't', 'z', 'u', 'J', + 'S', 'h' ); } Index: shorturl.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.info,v retrieving revision 1.1 diff -u -p -r1.1 shorturl.info --- shorturl.info 13 May 2009 17:41:39 -0000 1.1 +++ shorturl.info 1 Dec 2009 09:55:18 -0000 @@ -3,3 +3,10 @@ name = Short URL description = API to Shorten Any URL a-la bit.ly, tinyURL, is.gd etc. package = Development core = 6.x + +; Information added by drupal.org packaging script on 2009-08-11 +version = "6.x-1.x-dev" +core = "6.x" +project = "shorturl" +datestamp = "1249950326" + Index: shorturl.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.install,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 shorturl.install --- shorturl.install 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.install 1 Dec 2009 09:55:19 -0000 @@ -1,59 +1,57 @@ 'ShortURL Links Table.', 'fields' => array( 'lid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 22), 'orig_url' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), - 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => FALSE, 'default' => ''), - ), + 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => FALSE, 'default' => ''), + ), 'indexes' => array( 'shorturl_orig_url' => array('orig_url'), - ), + ), 'primary key' => array('lid'), - ); + ); $schema['shorturl_access'] = array( 'fields' => array( 'aid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 22), - 'url_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), + 'url_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), 'url_key' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => ''), - 'referer' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), - 'browser' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), - 'access_time' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), + 'referer' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), + 'browser' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), + 'access_time' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), ), 'primary key' => array('aid'), - ); - + ); + return $schema; } - - /** - * Implementation of hook_install() + +/** + * Implements hook_install(). */ function shorturl_install() { drupal_install_schema('shorturl'); - + // Set this module at the top! db_query("UPDATE {system} SET weight = -100 WHERE name = 'shorturl'"); - } - /** - * Implementation of hook_uninstall(). +/** + * Implements hook_uninstall(). */ function shorturl_uninstall() { drupal_uninstall_schema('shorturl'); } - Index: shorturl.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.module,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 shorturl.module --- shorturl.module 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.module 1 Dec 2009 09:55:19 -0000 @@ -1,49 +1,47 @@ TRUE)); //Let drupal make relative URLs absolute + if (stripos($orig_url, 'http') === FALSE && stripos($orig_url, 'https') === FALSE) { + $orig_url = url($orig_url, array('absolute' => TRUE)); //Let drupal make relative URLs absolute } - + drupal_goto($orig_url); - exit(); + exit(); } } - } -/** -* hook_perm implementation -*/ +/** + * Implements hook_perm(). + */ function shorturl_perm() { - return array('administer shorturl'); + return array('administer shorturl', 'create shorturl'); } /** -* hook_menu() implementation -*/ + * Implements hook_menu(); + */ function shorturl_menu() { + $items = array(); $items['admin/settings/shorturl'] = array( 'title' => 'ShortURL', @@ -55,49 +53,58 @@ function shorturl_menu() { 'type' => MENU_NORMAL_ITEM, ); + $items['shorturl'] = array( + 'page callback' => 'shorturl_api', + 'access arguments' => array('create shorturl'), + 'type' => MENU_CALLBACK, + ); + return $items; } +function shorturl_api() { + $result = new stdClass(); + $result->shortURL = url(shorturl_shorten($_GET['url']), array('absolute' => TRUE)); + print drupal_json($result); +} /** -* Generate unique token for arbitrary URL. Both absolute and relative -* URLs are supported. -* -* @param $long_url -* the original URL -* -* @return shortened URI token -* -*/ -function shorturl_shorten ($long_url) { - + * Generate unique token for arbitrary URL. Both absolute and relative + * URLs are supported. + * + * @param $long_url + * the original URL + * + * @return shortened URI token + * + */ +function shorturl_shorten($long_url) { //Do we already have this URL? - $existing_id = db_result(db_query('SELECT lid FROM {shorturl_link} WHERE orig_url=\'%s\' ', $long_url)); + $existing_id = db_result(db_query("SELECT lid FROM {shorturl_link} WHERE orig_url = '%s'", $long_url)); if (!empty($existing_id)) { $encoded = shorturl_encode_url($existing_id); return $encoded; } - + $found_vacant = FALSE; - $remote_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '' : $_SERVER['REMOTE_ADDR']; + $remote_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '' : $_SERVER['REMOTE_ADDR']; $link = new stdClass(); - $link->orig_url = $long_url; - $link->created = (int)time(); + $link->orig_url = $long_url; + $link->created = (int) time(); $link->remote_ip = $remote_ip; - + $last_id = db_result(db_query('SELECT max(lid) FROM {shorturl_link}')); - if ($last_id < SHORTURL_START_FROM) {$last_id = SHORTURL_START_FROM;} - $last_id++; - - while (! $found_vacant ) { + $last_id = (($last_id < SHORTURL_START_FROM) ? SHORTURL_START_FROM : $last_id) + 1; + + while (!$found_vacant) { $encoded = shorturl_encode_url($last_id); $found_vacant = shorturl_check_availability($encoded); if (!$found_vacant) { $last_id++; } - // Perform multi-threading safety check. - // Make sure nobody inserted a record with this $lid, while we were checking other stuff + // Perform multi-threading safety check. + // Make sure nobody inserted a record with this $lid, while we were checking other stuff else { $link->lid = $last_id; $status = @drupal_write_record('shorturl_link', $link); @@ -107,34 +114,39 @@ function shorturl_shorten ($long_url) { } } } - - return $encoded; + + return $encoded; } function shorturl_check_availability($token) { $reserved = shorturl_reserved_tokens(); - foreach ($reserved as $key) { - if (stripos($token, $key)!==FALSE) { // contains reserved word! + if (stripos($token, $key) !== FALSE) { + // contains reserved word! return FALSE; } } - - $menu_handler = menu_get_item($token); - if (!empty($menu_handler)) { return FALSE; } // there's already a handler for this path! - - $alias_found = db_result(db_query('SELECT count(*) FROM {url_alias} WHERE dst = \'%s\' ', $token)); - - if (!empty($alias_found)) { return FALSE; } // such alias already exists! - + + $menu_handler = menu_get_item($token); + if (!empty($menu_handler)) { + // there's already a handler for this path! + return FALSE; + } + + $alias_found = db_result(db_query("SELECT count(*) FROM {url_alias} WHERE dst = '%s'", $token)); + if (!empty($alias_found)) { + // such alias already exists! + return FALSE; + } + return TRUE; } function shorturl_reserved_tokens() { $reserved_settings = variable_get('shorturl_reserved_tokens', array()); - - $reserved_system = array('node', 'admin', 'term', 'user'); - + + $reserved_system = array('node', 'admin', 'term', 'user', 'shorturl'); + return $reserved_system + $reserved_settings; -} \ No newline at end of file +}