Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added alias option for makefiles.

  • Loading branch information...
commit 12b4f942f98812a2bd646e6987111c3368301886 1 parent b9bddee
@rupl authored
View
5 .htaccess
@@ -44,9 +44,10 @@ DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
- # Rewrite URLs of the form '/a/XXX' to the form 'file.php?short=XXX'.
+ # Rewrite URLs of the form '/a/XXX' to the form 'file.php?alias=XXX'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
- RewriteRule ^a/(.*)$ file.php?short=$1 [L,QSA]
+ RewriteRule ^a/([^/]*)/raw$ file.php?alias=$1&raw [L,QSA]
+ RewriteRule ^a/([^/]*)$ file.php?alias=$1 [L,QSA]
</IfModule>
View
51 _lib.php
@@ -298,6 +298,7 @@ function formLibs(){
*/
function formOpts($version){
$output = '';
+ $domain = $_SERVER['HTTP_HOST'];
$output .= '<h4>Put modules in: </h4>
<label for="o-contribdir">
@@ -310,12 +311,12 @@ function formOpts($version){
include <a href="https://github.com/rupl/drush_make_generator/raw/master/prep.sh" target="_blank">prep.sh</a>&nbsp;
<input id="o-prep" type="checkbox" name="makefile[opts][prep]" value="include" />
</label>
- <h4>Short URL:</h4>
+*/
+ $output .= '<br><h4>Short URL:</h4>
<label for="o-short">
- http://drushmake.me/a/
- <input id="o-prep" type="text" name="makefile[opts][short]" placeholder="" />
+ http://'.$domain.'/a/
+ <input id="o-prep" type="text" name="makefile[opts][alias]" placeholder="alias" />
</label>';
-*/
return $output;
}
@@ -388,7 +389,7 @@ function formDownload($type='libraries',$download=array()){
/**
* fetch makefile and output
*/
-function generateMakefile($token,$mode=''){
+function generateMakefile($token,$opts=array()){
$makefile = '';
$clean = sanitize('token',$token);
@@ -405,9 +406,7 @@ function generateMakefile($token,$mode=''){
$opts = unserialize($m['opts']);
$share = TRUE;
- if ($mode == 'raw') {$opts['raw'] = TRUE; }
-
- $makefile = makeFile($clean,$version,$core,$modules,$themes,$libs,$opts);
+ $makefile = makeMakefile($clean,$version,$core,$modules,$themes,$libs,$opts);
return $makefile;
} else {
@@ -421,12 +420,12 @@ function generateMakefile($token,$mode=''){
/**
* makefile template
*/
-function makeFile($token,$v,$core,$modules,$themes,$libs,$opts){
+function makeMakefile($token,$v,$core,$modules,$themes,$libs,$opts){
$opts['version'] = $v;
$makefile = '; ----------------
; Generated makefile from http://drushmake.me
-; Permanent URL: '.fileUrl($token,$opts).'
+; Permanent URL: '.fileURL($token,$opts).'
; ----------------
;
; This is a working makefile - try it! Any line starting with a `;` is a comment.
@@ -471,7 +470,6 @@ function makeFile($token,$v,$core,$modules,$themes,$libs,$opts){
'; // end of makefile
-
return $makefile;
}
@@ -761,9 +759,16 @@ function sanitize($type='token',$data){
switch ($type) {
case 'token':
// only accept 12 chars made of a-f and 0-9
- $clean = (isset($data) && preg_match('/^[a-f0-9]{12}/',$data)) ? $data : FALSE;
+ $clean = ($data && preg_match('/^[a-f0-9]{12}/',$data)) ? $data : FALSE;
break;
+ case 'alias':
+ // only accept 1-64 chars a-z, 0-9, hyphens, and underscores
+ $data = strtolower($data);
+ $string = preg_replace('/[^a-z0-9\-_]/','',$data);
+ $clean = ($string && preg_match('/^[a-z0-9\-_]{1,64}$/',$string)) ? $string : FALSE;
+ break;
+
default:
$clean = FALSE;
break;
@@ -774,20 +779,28 @@ function sanitize($type='token',$data){
/**
- * Generate URL requests for a token. For easy switching later.
+ * Generate URL requests for a token.
*/
function fileURL($token='',$opts=array()){
+ $domain = $_SERVER['HTTP_HOST'];
- // http://drushmake.me/a/short-url
-
+ // raw output mode
$raw = '';
- if (isset($opts['raw'])) {
- $raw = '&raw';
+ if (isset($opts['raw']) && $opts['raw'] == 'raw') {
+ $raw = TRUE;
}
- $domain = $_SERVER['HTTP_HOST'];
+ // format URL
+ if (!empty($opts['alias'])) {
+ $raw = ($raw) ? '/raw' : '';
+ $token = $opts['alias'];
+ $url = 'http://'.$domain.'/a/'.$token.$raw;
+ } else {
+ $raw = ($raw) ? '&raw' : '';
+ $url = 'http://'.$domain.'/file.php?token='.$token.$raw;
+ }
- return 'http://'.$domain.'/file.php?token='.$token.$raw;
+ return $url;
}
View
12 drushmake_alter.sql
@@ -12,11 +12,13 @@ ALTER TABLE `versions` ADD `url` TEXT NULL AFTER `release`;
* Add table for short URLs
*/
CREATE TABLE `aliases` (
- `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
- `makefile` INT NOT NULL ,
- `alias` VARCHAR( 255 ) NOT NULL ,
- UNIQUE (`alias`)
-) ENGINE = MYISAM ;
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `token` varchar(255) NOT NULL,
+ `alias` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `alias` (`alias`),
+ UNIQUE KEY `token` (`token`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
/*
* Add column for release types, mostly to distinguish stable vs dev.
View
30 file.php
@@ -2,16 +2,30 @@
include('_lib.php');
-// sanitize token and pull makefile from db
-$token = (isset($_REQUEST['token']) && sanitize('token',$_REQUEST['token'])) ? $_REQUEST['token'] : '';
-$mode = (isset($_GET['raw'])) ? 'raw' : FALSE;
-$makefile = generateMakefile($token,$mode);
+// sanitize token or alias
+$token = (isset($_REQUEST['token']) && sanitize('token',$_REQUEST['token'])) ? $_REQUEST['token'] : FALSE;
+$alias = (isset($_REQUEST['alias']) && sanitize('alias',$_REQUEST['alias'])) ? $_REQUEST['alias'] : FALSE;
+
+// pull token if alias was supplied
+if ($alias) {
+ $aliasSQL = sprintf("SELECT token FROM aliases WHERE alias = '%s';",$alias);
+ $aliasResult = mysql_query($aliasSQL);
+ if ($a = mysql_fetch_assoc($aliasResult)){
+ $token = $a['token'];
+ }
+}
+
+// pull makefile from DB
+$opts['raw'] = (isset($_GET['raw'])) ? TRUE : FALSE;
+$opts['alias'] = ($alias) ? $alias : FALSE;
+$makefile = generateMakefile($token,$opts);
if (!$makefile){
$fail = 'fail';
$error = "Something broke :(\r\n
Check your URL...\r\n\r\n\r\n
-...or if there's an error onscreen post it at https://github.com/rupl/drush_make_generator/issues - thanks!";
+...or if there's an error onscreen and you feel generous,
+you could post it at https://github.com/rupl/drush_make_generator/issues - thanks!";
}
@@ -20,7 +34,7 @@
// output mode?
-if ($mode == 'raw') {
+if (isset($opts['raw']) && $opts['raw'] == 'raw') {
// raw/permalink
header("Content-type: text/plain\r\n");
@@ -78,8 +92,8 @@
<h2>Your makefile is ready</h2>
<p>We've saved it for you as well!</p>
<ul>
- <li><a href="<?php print fileURL($token); ?>">Bookmark</a></li>
- <li><a href="<?php print fileURL($token,'raw'); ?>">raw makefile</a></li>
+ <li><a href="<?php print fileURL($token,$opts); ?>">Bookmark</a></li>
+ <li><a href="<?php $opts['raw'] = 'raw'; print fileURL($token,$opts); ?>">raw makefile</a></li>
<li>or (in the future) update previous makefiles</li>
</ul>
<textarea name="makefile" id="makefile"><?php print $makefile; ?></textarea>
View
45 generate.php
@@ -13,8 +13,20 @@
// new or old makefile?
if (isset($_REQUEST['token']) && sanitize('token',$_REQUEST['token'])) {
+
// old; pass on through
$token = $_REQUEST['token'];
+
+ // pull makefile data for its options so the redirect works smoothly
+ $pullSQL = sprintf("SELECT * FROM `makefiles` WHERE token = '%s'; ",$token);
+ $pullResult = mysql_query($pullSQL);
+ if ($m = mysql_fetch_assoc($pullResult)){
+ $urlOpts = unserialize($m['opts']);
+ }
+
+ // tell the next page what we did
+ $share = FALSE;
+
} else {
// new; random string, aliases can be added later
@@ -25,6 +37,9 @@
if (isset($_REQUEST['makefile']['themes']['|THIS|'])) {unset($_REQUEST['makefile']['themes']['|THIS|']); }
if (isset($_REQUEST['makefile']['libs']['|THIS|'])) {unset($_REQUEST['makefile']['libs']['|THIS|']); }
+ // sanitize alias
+ $_REQUEST['makefile']['opts']['alias'] = sanitize('alias',$_REQUEST['makefile']['opts']['alias']);
+
// escape and flatten data
$version = (isset($_REQUEST['makefile']['version'])) ? mysql_real_escape_string($_REQUEST['makefile']['version']) : $version;
$core = (isset($_REQUEST['makefile']['core'])) ? mysql_real_escape_string(serialize($_REQUEST['makefile']['core'])) : FALSE;
@@ -37,8 +52,8 @@
/* debug
print '<pre>'."\r\n";
- print $token." -token\r\n";
- print $version." -version\r\n";
+ print $token."\r\n";
+ print $version."\r\n";
print $core."\r\n";
print $modules."\r\n";
print $themes."\r\n";
@@ -53,14 +68,30 @@
$storeSQL = sprintf("INSERT INTO `makefiles` (token,version,core,modules,themes,libs,opts) VALUES ('%s','%s','%s','%s','%s','%s','%s'); ",$token,$version,$core,$modules,$themes,$libs,$opts);
$storeResult = mysql_query($storeSQL) or die(mysql_error());
- // sharing.. later
- if ($storeResult) {$share = TRUE; }
- else {$share = FALSE; }
+ // store makefile alias
+ if ($storeResult && !empty($_REQUEST['makefile']['opts']['alias'])) {
+ // security
+ $alias = $urlOpts['alias'] = sanitize('alias',$_REQUEST['makefile']['opts']['alias']);
+
+ // store alias
+ $shareSQL = sprintf("INSERT INTO aliases (token, alias) VALUES ('%s','%s');",$token,$alias);
+ $shareResult = mysql_query($shareSQL) or die(mysql_error());
+
+ // tell the next page what we did
+ $share = TRUE;
+ } else {
+ // tell the next page what we did
+ $share = FALSE;
+ }
}
-// redirect
-header("Location: ".fileURL($token));
+// redirect appropriately
+if ($share) {
+ header("Location: ".fileURL($alias,$urlOpts));
+} else {
+ header("Location: ".fileURL($token,$urlOpts));
+}
ob_flush();
Please sign in to comment.
Something went wrong with that request. Please try again.