More work for custom domains - Ability to use autogenerated aliases for URLs shortened with a custom domain

This commit is contained in:
Daniel15 2011-06-13 11:20:44 +10:00
parent 707cf169f6
commit 3deec9770a
8 changed files with 97 additions and 16 deletions

View File

@ -40,7 +40,7 @@ ini_set('unserialize_callback_func', 'spl_autoload_call');
/**
* Set the production status based on domain
*/
define('IN_PRODUCTION', $_SERVER['HTTP_HOST'] != 'dev.zurl.ws:82');
define('IN_PRODUCTION', !in_array($_SERVER['HTTP_HOST'], array('dev.zurl.ws:82', 'customzurltest.local', 'customzurltest2.local')));
//-- Configuration and initialization -----------------------------------------

View File

@ -63,9 +63,15 @@ class Controller_Url extends Controller_Template
{
$post
->rule('domain_custom', 'Controller_Url::validate_domain')
->rule('domain_alias', 'not_empty')
->filter('domain_alias', 'strtolower')
->rule('domain_alias', 'regex', array('~^[a-z0-9\-_]+$~'));
}
elseif (Arr::get($_POST, 'type') == 'domain')
{
$post
->rule('domain', 'Controller_Url::validate_domain');
}
}
else
{
@ -106,6 +112,11 @@ class Controller_Url extends Controller_Template
{
$url->and_where('user_id', '=', $this->user);
$url->and_where('type', '=', $post['type']);
// For domain URLs, need to check the domain too
if ($post['type'] == 'domain')
$url->and_where('domain_id', '=', $post['domain']);
elseif ($post['type'] == 'domain_custom')
$url->and_where('domain_id', '=', $post['domain_custom']);
}
$url->find();
@ -124,11 +135,20 @@ class Controller_Url extends Controller_Template
$url->type = $post['type'];
if (in_array($url->type, array('custom', 'user')))
$url->custom_alias = $post['alias'];
// Custom domain, custom alias
elseif ($url->type == 'domain_custom')
{
$url->custom_alias = empty($post['domain_alias']) ? null : $post['domain_alias'];
$url->domain_id = $post['domain_custom'];
}
// Custom domain, "normal" alias
elseif ($url->type == 'domain')
{
$domain = ORM::factory('domain', $post['domain']);
$url->domain_id = $domain->id;
// Domain_url_id is a unique ID for this domain on this URL.
$url->domain_url_id = $domain->next_url_id();
}
}
$url->save();
@ -235,7 +255,15 @@ class Controller_Url extends Controller_Template
// Try to find the URL they're talking about
$url_pieces = parse_url($post['url']);
$url = Shortener::get_url(substr($url_pieces['path'], 1), $url_pieces['host']);
try
{
$url = Shortener::get_url(substr($url_pieces['path'], 1), $url_pieces['host']);
}
catch (Exception $ex)
{
// Use a dummy URL
$url = Model::factory('url');
}
// Is it a valid URL?
if ($url != false && $url->loaded())
@ -264,8 +292,12 @@ Email: ' . $post['email'];
URL ID: ' . $url . '
Number of complaints: ' . $url->complaints;
}
$headers = array('From: "zURL Abuse" <abuse@zurl.ws>');
if (!empty($post['email']))
$headers[] = 'Reply-To: ' . $post['email'];
mail('zurl@d15.biz', '[URL ' . $url->id . '] ' . $post['url'], $email, 'From: "zURL Abuse" <abuse@zurl.ws>');
mail('zurl@dan.cx', '[URL ' . $url->id . '] ' . $post['url'], $email, implode("\r\n", $headers), '-fabuse@zurl.ws');
$page = $this->template->body = new View('url/complaint_received');
}

View File

@ -23,11 +23,11 @@ class Model_Url extends ORM
}
elseif ($name == 'short_url')
{
return self::get_short_url($this->id, $this->type, $this->custom_alias, $this->user_id, $this->domain_id);
return self::get_short_url($this->id, $this->type, $this->custom_alias, $this->user_id, $this->domain_id, $this->domain_url_id);
}
elseif ($name == 'preview_url')
{
return self::get_preview_url($this->id, $this->type, $this->custom_alias, $this->user_id, $this->domain_id);
return self::get_preview_url($this->id, $this->type, $this->custom_alias, $this->user_id, $this->domain_id, $this->domain_url_id);
}
return parent::__get($name);
@ -71,12 +71,27 @@ class Model_Url extends ORM
->find();
}
public static function find_by_domain_custom($alias, $domain)
/**
* Find a URL on a custom domain, either by custom alias, or "normal" alias.
* @param string Alias in the URL
* @param int Domain ID
* @returns The URL, or null if not found
*/
public static function find_by_domain($alias, $domain)
{
return ORM::factory('url')
->where('type', '=', 'domain_custom')
->where('domain_id', '=', $domain)
->where('custom_alias', '=', $alias)
->and_where_open()
->or_where_open()
->where('type', '=', 'domain_custom')
->where('custom_alias', '=', $alias)
->or_where_close()
->or_where_open()
->where('type', '=', 'domain')
->where('domain_url_id', '=', Shortener::alias_to_id($alias))
->or_where_close()
->and_where_close()
->order_by('id')
->find();
}
@ -86,7 +101,7 @@ class Model_Url extends ORM
//public static function get_short_url($id)
// TODO: This is UUUUGLY!
///public static function get_short_url($row)
public static function get_short_url($id, $type, $custom_alias, $user_id, $domain_id = null)
public static function get_short_url($id, $type, $custom_alias, $user_id, $domain_id = null, $domain_url_id = null)
{
if (strstr($_SERVER['HTTP_HOST'], 'dev.zurl'))
$base = 'dev.zurl.ws:82';
@ -113,6 +128,11 @@ class Model_Url extends ORM
return 'http://' . $domain->domain . '/' . $custom_alias;
break;
case 'domain':
$domain = ORM::factory('domain', $domain_id);
return 'http://' . $domain->domain . '/' . Shortener::id_to_alias($domain_url_id);
break;
default:
return 'http://' . $base . '/' . Shortener::id_to_alias($id);
break;
@ -123,7 +143,7 @@ class Model_Url extends ORM
* Get the preview URL for a URL
* TODO: Remove duplication with get_short_url above
*/
public static function get_preview_url($id, $type, $custom_alias, $user_id, $domain_id)
public static function get_preview_url($id, $type, $custom_alias, $user_id, $domain_id, $domain_url_id = null)
{
switch ($type)
{
@ -139,6 +159,12 @@ class Model_Url extends ORM
case 'domain_custom':
$domain = ORM::factory('domain', $domain_id);
return 'http://' . $domain->domain . '/p/' . $custom_alias;
break;
case 'domain':
$domain = ORM::factory('domain', $domain_id);
return 'http://' . $domain->domain . '/p/' . Shortener::id_to_alias($domain_url_id);
break;
default:
return 'http://' . $_SERVER['HTTP_HOST'] . '/p/' . Shortener::id_to_alias($id);

View File

@ -79,7 +79,9 @@ class Shortener
if (!$domain->loaded())
throw new Exception('Domain ' . htmlspecialchars($hostname) . ' not found!');
return Model_Url::find_by_domain_custom($alias, $domain);
// URL model handles differences between custom and standard aliases, so just pass the
// alias to it.
return Model_Url::find_by_domain($alias, $domain);
}
}
}

View File

@ -61,7 +61,7 @@ foreach ($urls as $url)
foreach ($visits as $visit)
{
$wrapped_url = chunk_split($visit->url, 15, '&#8203;');
$short_url = Model_Url::get_short_url($visit->id, $visit->type, $visit->custom_alias, $visit->user_id, $visit->domain_id);
$short_url = Model_Url::get_short_url($visit->id, $visit->type, $visit->custom_alias, $visit->user_id, $visit->domain_id, $visit->domain_url_id);
echo '
<tr>

View File

@ -25,6 +25,14 @@ Due to spam, users are now required to create an account to use zURL. Sorry for
<p id="types">
Type (<a href="about.htm#custom">what?</a>):
<?php if (!empty($domains)) : ?>
<?php echo Form::radio('type', 'domain', $values['type'] == 'domain', array('id' => 'type_domain')); ?>
<label for="type_domain" title="Domain URL: Like a standard URL, except using your custom domain">Domain (standard)</label>
<?php echo Form::radio('type', 'domain_custom', $values['type'] == 'domain_custom', array('id' => 'type_domain_custom')); ?>
<label for="type_domain_custom" title="Custom domain URL: Like a user URL, except using your custom domain">Domain (custom)</label>
<?php endif; ?>
<?php echo Form::radio('type', 'standard', $values['type'] == '' || $values['type'] == 'standard', array('id' => 'type_standard')); ?>
<label for="type_standard" title="Standard URL: A normal zURL link containing random letters and numbers in the URL">Standard</label>
@ -33,15 +41,14 @@ Due to spam, users are now required to create an account to use zURL. Sorry for
<?php echo Form::radio('type', 'user', $values['type'] == 'user', array('id' => 'type_user')); ?>
<label for="type_user" title="User URL: Like a custom URL, except with your username at the start">User</label>
<?php if (!empty($domains)) : ?>
<?php echo Form::radio('type', 'domain_custom', $values['type'] == 'domain_custom', array('id' => 'type_domain_custom')); ?>
<label for="type_domain_custom" title="Custom domain URL: Like a user URL, except using your custom domain">Custom Domain</label>
<?php endif; ?>
</p>
<p id="alias_p">
Short URL: http://<strong id="prefix"></strong>.zurl.ws/<input type="text" name="alias" id="alias" value="<?php echo !empty($values['alias']) ? htmlspecialchars($values['alias']) : ''; ?>" />
</p>
<?php if (!empty($domains)) : ?>
<p id="domain_p">
Domain: <?php echo Form::select('domain', $domains, Arr::get($values, 'domain'), array('id' => 'domain')); ?>
</p>
<p id="domain_custom_p">
Short URL: http://<?php echo Form::select('domain_custom', $domains, Arr::get($values, 'domain_custom'), array('id' => 'domain_custom')); ?>/
<input type="text" name="domain_alias" id="domain_alias" value="<?php echo !empty($values['domain_alias']) ? htmlspecialchars($values['domain_alias']) : ''; ?>" />

View File

@ -72,6 +72,8 @@ var Shorten =
{
$('alias_p').setStyle('display', '');
$('prefix').set('html', 'c');
if ($('domain_p'))
$('domain_p').setStyle('display', 'none');
if ($('domain_custom_p'))
$('domain_custom_p').setStyle('display', 'none');
}
@ -79,12 +81,21 @@ var Shorten =
{
$('alias_p').setStyle('display', '');
$('prefix').set('html', $('current_user').get('text'));
if ($('domain_p'))
$('domain_p').setStyle('display', 'none');
if ($('domain_custom_p'))
$('domain_custom_p').setStyle('display', 'none');
}
else if ($('type_domain') && $('type_domain').checked)
{
$('alias_p').setStyle('display', 'none');
$('domain_p').setStyle('display', '');
$('domain_custom_p').setStyle('display', 'none');
}
else if ($('type_domain_custom') && $('type_domain_custom').checked)
{
$('alias_p').setStyle('display', 'none');
$('domain_p').setStyle('display', 'none');
$('domain_custom_p').setStyle('display', '');
}
else
@ -92,6 +103,8 @@ var Shorten =
$('alias_p').setStyle('display', 'none');
if ($('domain_custom_p'))
$('domain_custom_p').setStyle('display', 'none');
if ($('domain_p'))
$('domain_p').setStyle('display', 'none');
}
}
}

View File

@ -120,6 +120,7 @@ CREATE TABLE IF NOT EXISTS `urls` (
`type` enum('standard','custom','user','domain','domain_custom') NOT NULL DEFAULT 'standard',
`custom_alias` varchar(255) DEFAULT NULL,
`domain_id` int(10) unsigned DEFAULT NULL,
`domain_url_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;