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 -r1.1.2.1 shorturl.admin.inc --- shorturl.admin.inc 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.admin.inc 26 Aug 2009 15:46:03 -0000 @@ -19,7 +19,6 @@ '#required' => TRUE, '#description' => t('The domain to be used in short URLs'), ); - $form['shorturl_start_from'] = array( '#type' => 'textfield', '#title' => t('Start From ID#:'), @@ -27,8 +26,13 @@ '#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.'), ); - - + $form['shorturl_allow_duplicates'] = array( + '#type' => 'checkbox', + '#title' => t('Allow duplicate submissions'), + '#description' => t('Allows user to create more than one link to the same URL. This is important for tracking links by users and for targeting different links to different roups for the purpose of comparing metrics.'), + '#default_value' => variable_get('shorturl_allow_duplicates', 0), + '#return_value' => 1, + ); return system_settings_form($form); } Index: shorturl.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.module,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 shorturl.module --- shorturl.module 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.module 26 Aug 2009 23:49:42 -0000 @@ -1,9 +1,4 @@ TRUE)); //Let drupal make relative URLs absolute } + $last_id = db_result(db_query('SELECT max(aid) FROM {shorturl_access} ')); + $last_id++; + $remote_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '' : $_SERVER['REMOTE_ADDR']; + $referer = (empty($_SERVER['HTTP_REFERER'])) ? '' : $_SERVER['HTTP_REFERER']; + $browser = (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']; + db_query("INSERT INTO {shorturl_access} (`aid`, `url_id`, `url_key`, `remote_ip`, `referer`, `browser`, `access_time`) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d) ", $last_id, $indx, $token, $remote_ip, $referer, $browser, (int)time()); + + //echo $orig_url; drupal_goto($orig_url); exit(); } } - } -/** -* hook_perm implementation -*/ +/** + * Implementation of hook_perm(). + */ function shorturl_perm() { - return array('administer shorturl'); + return array('view statistics'); } /** -* hook_menu() implementation -*/ + * Implementation of hook_menu(). + */ function shorturl_menu() { - + + $items = array(); + $items['shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'access arguments' => array('view statistics'), + 'type' => MENU_NORMAL_ITEM + ); + $items['shorturl/user'] = array( + 'title' => 'Your URLs', + 'page callback' => 'shorturl_statistics', + 'access arguments' => array('view statistics'), + 'type' => MENU_DEFAULT_LOCAL_TASK + ); + $items['shorturl/all'] = array( + 'title' => 'All users', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all'), + 'access arguments' => array('view statistics'), + 'type' => MENU_LOCAL_TASK + ); + $items['shorturl/link/%shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics_link', + 'page arguments' => array(2), + 'access arguments' => array('view statistics'), + 'type' => MENU_CALLBACK + ); + $items['shorturl/user/%user'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array(2), + 'access arguments' => array('view statistics'), + 'type' => MENU_CALLBACK + ); $items['admin/settings/shorturl'] = array( 'title' => 'ShortURL', 'description' => 'Configure the settings for ShortURL.', @@ -52,12 +88,82 @@ 'page arguments' => array('shorturl_admin_settings_form'), 'access arguments' => array('administer shorturl'), 'file' => 'shorturl.admin.inc', - 'type' => MENU_NORMAL_ITEM, ); - + $items['admin/reports/shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all'), + 'access arguments' => array('administer shorturl'), + ); return $items; } +function shorturl_load($lid) { + return db_fetch_object(db_query("SELECT * FROM {shorturl_link} WHERE lid = '%d'", $lid)); +} + +function shorturl_statistics($user = NULL) { + if (empty($user)) { + global $user; + $sql = "SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id WHERE sl.uid='$user->uid' GROUP BY lid, orig_url ORDER BY clicks DESC"; + $sql_count = "SELECT COUNT(lid) FROM {shorturl_link} WHERE uid='$user->uid'"; + //dsm($sql); + } + elseif (is_object($user)) { + $sql = "SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id WHERE sl.uid='$user->uid' GROUP BY lid, orig_url ORDER BY clicks DESC"; + $sql_count = "SELECT COUNT(lid) FROM {shorturl_link} WHERE uid='$user->uid'"; + } + else { + $sql = 'SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id GROUP BY lid, orig_url ORDER BY clicks DESC'; + $sql_count = 'SELECT COUNT(lid) FROM {shorturl_link}'; + } + $result = pager_query($sql, 50, 0, $sql_count); + + $rows = array(); + while ($link = db_fetch_object($result)) { + $link_short = (strlen($link->orig_url) > 75) ? substr($link->orig_url, 0, 75).'...' : $link->orig_url; + $shorturl = shorturl_encode_url($link->lid); + $user = user_load($link->uid); + $details = (!empty($link->title)) ? l($link->title, $shorturl).'
'.$link_short.'
'.$link->description : l($link_short, $shorturl); + $rows[] = array( + l($shorturl, 'shorturl/link/'.$link->lid), + $details, + array('class' => 'replies', 'data' => $link->clicks), + t('!time ago', array('!time' => format_interval(time() - $link->created))), + l($user->name, 'shorturl/user/'.$user->uid), + ); + } + + if (!$rows) { + $rows[] = array(array('data' => t('No links available.'), 'colspan' => '5')); + } + + $header = array(t('Key'), t('Original URL'), t('Clicks'), t('Created'), t('User')); + + $output = '
'; + $output .= theme('table', $header, $rows); + $output .= theme('pager', NULL, 25, 0); + $output .= '
'; + + return $output; +} + +function shorturl_statistics_link($link) { + drupal_set_title($link->title); + $output = ''; + + $user = user_load($link->uid); + $link_short = (strlen($link->orig_url) > 150) ? substr($link->orig_url, 0, 150).'...' : $link->orig_url; + $clicks = db_result(db_query("SELECT COUNT(aid) FROM {shorturl_access} WHERE url_id='%d'", $link->lid)); + + $output .= '

'.l($link_short, $link->orig_url).'

'; + $output .= '

'.t('Posted by !user on !date', array('!user' => l($user->name, 'shorturl/user/'.$user->uid), '!date' => format_date($link->created, 'medium'))).'

'; + $output .= '

'.$link->description.'

'; + $output .= '

'.$clicks.' Clickthroughs

'; + + return $output; +} + /** * Generate unique token for arbitrary URL. Both absolute and relative @@ -69,28 +175,36 @@ * @return shortened URI token * */ -function shorturl_shorten ($long_url) { +function shorturl_shorten($long_url) { + global $user; //Do we already have this 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; + if (!variable_get('shorturl_allow_duplicates', 0)) { + $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']; + $url_data = _shorturl_urldata($long_url); $link = new stdClass(); + $link->uid = $user->uid; $link->orig_url = $long_url; $link->created = (int)time(); $link->remote_ip = $remote_ip; + $link->title = $url_data['title']; + $link->description = $url_data['metaTags']['description']['value']; + $link->keywords = $url_data['metaTags']['keywords']['value']; - $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 = db_result(db_query('SELECT max(lid) FROM {shorturl_link} ')); + if ($last_id == 0) $last_id = SHORTURL_START_FROM; $last_id++; - while (! $found_vacant ) { + while (!$found_vacant ) { $encoded = shorturl_encode_url($last_id); $found_vacant = shorturl_check_availability($encoded); if (!$found_vacant) { @@ -108,6 +222,10 @@ } } + //dsm($url_data); + + //db_query('INSERT INTO {shorturl_link} (`lid`, `uid`, `orig_url`, `title`, `description`, `keywords`, `created`, `remote_ip` ) VALUES (%d, %d, \'%s\', \'%s\', \'%s\', \'%s\', %d, \'%s\') ', $last_id, $user->uid, $long_url, $url_data['title'], $url_data['metaTags']['description']['value'], $url_data['metaTags']['keywords']['value'], (int)time(), $remote_ip); + return $encoded; } @@ -137,4 +255,68 @@ $reserved_system = array('node', 'admin', 'term', 'user'); return $reserved_system + $reserved_settings; -} \ No newline at end of file +} + +function _shorturl_urldata($url) { + $result = false; + $contents = _shorturl_geturl($url); + + if (isset($contents) && is_string($contents)) { + $title = ''; + $metaTags = NULL; + + preg_match('/]*>([^>]*)<\/title>/si', $contents, $match ); + + if (isset($match) && is_array($match) && count($match) > 0) { + $title = strip_tags($match[1]); + } + + preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match); + + if (isset($match) && is_array($match) && count($match) == 3) { + $originals = $match[0]; + $names = $match[1]; + $values = $match[2]; + + if (count($originals) == count($names) && count($names) == count($values)) { + $metaTags = array(); + + for ($i=0, $limiti=count($names); $i < $limiti; $i++) { + $metaTags[$names[$i]] = array ( + 'html' => htmlentities($originals[$i]), + 'value' => $values[$i] + ); + } + } + } + + $result = array ( + 'title' => $title, + 'metaTags' => $metaTags + ); + } + + return $result; +} + +function _shorturl_geturl($url, $maximumRedirections = null, $currentRedirection = 0) { + $result = false; + $contents = @file_get_contents($url); + + // Check if we need to go somewhere else + if (isset($contents) && is_string($contents)) { + preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match); + + if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1) { + if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections) { + return _shorturl_geturl($match[1][0], $maximumRedirections, ++$currentRedirection); + } + $result = false; + } + else { + $result = $contents; + } + } + return $contents; +} +?> \ No newline at end of file