YOURLS is a nice and powerful PHP scripts that will allow you to run your own URL shortening service. You can make it private or public, pick custom keyword URLs.
With some simple steps, I installed YOURLS 1.5 (the latest and featured version) on my Linux VPS successfully.
When I logged in the admin panel, I was attracted by the simple and beautiful interface. However I found I can’t use the Edit and Delete buttons in the panel, it seems the AJAX functions doesn’t work properly.
After some searching with Google, I found many users occurred the same problem as mentioned above. For example, there is a thread on Google Code, http://code.google.com/p/yourls/issues/detail?id=587 .
And the author OZH gave a resolution on http://code.google.com/p/yourls/wiki/EndlessSpinningIcon , but it not works, because I wasn’t facing the problem of Endless Spinning Icon.
So I decided to see what had happened.
By using FireBugs, I found the AJAX returned “omg error” which caused by the following code in admin-ajax.php:
case 'edit_display':
yourls_verify_nonce( 'edit-link_'.$_REQUEST['id'], $_REQUEST['nonce'], false, 'omg error' );
The $_REQUEST['nonce'] was null so the verify was always failed.
And this issue was caused by the following code in insert.js
var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );
The JQuery can’t select the correct id, if I replaced with getElementById(), it works.
Well, I found the edit button’s IDs which the script generated were something like this “1234.000000″, it contained “.” so JQuery selector didn’t work.
After reading some codes, I know the IDs was generated by function yourls_string2int( $string, $chars = null ) in functions.php .
function yourls_string2int( $string, $chars = null ) {
if( $chars == null )
$chars = yourls_get_shorturl_charset();
$integer = 0;
$string = strrev( $string );
$baselen = strlen( $chars );
$inputlen = strlen( $string );
for ($i = 0; $i < $inputlen; $i++) {
$index = strpos( $chars, $string[$i] );
$integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) );
}
return yourls_apply_filter( 'string2int', $integer, $string, $chars );
}
This function is to convert a string (3jk) to an integer (1337) by baselen 36 or 62 which defined in config.php .
However there is an optional scale parameter of bcadd which is used to set the number of digits after the decimal place in the result.
"You can also set the global default scale for all functions by using bcscale()." wrote by PHP manual.
This is the reason why some people can't use Edit or Delete buttons but the authors can.
Just change the line
$integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) );
to
$integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) ,0 );
Upload and overwrite the functions.php, refresh the admin panel, it will work.
Here is the modified functions.php if you don't want to modify it manually.
Download it, unzip it, and overwrite the functions.php in folder "includes".
functions.zip
matte
I was having an issue but it was because I don’t have BCMath installed on my server. I quickly wrote a plugin to use MD5 hashes for the IDs in the admin area. Due to the way the filters are applied in YOURLS, this filter is applied after all the bcmath functions are called. But it still works in my testing.
You can download it here.
http://www.silent-e.com/code/php/md5-ids-for-yourls.zip
My email is in the zipfile if anyone has problems.
(e)
Pep
Hey matte, thank you for sharing the code and experience.
Phil
Hi PEP,
on my Website (with Apache 4.4.9 & MySQL 5) your hack does’t work
Solution#1:
I changed the jquery from jquery-1.4.3.min.js to older version jquery-1.3.2.min.js.
Works in FIREFOX but not in IE7+IE8.
Solution#2:
After responce with my provider I must parse the PHP-Files to PHP5 in the HTACCESS-File:
AddType x-mapp-php5 .php + AddHandler x-mapp-php5 .php
Works also in FIREFOX but not in IE7+IE8.
Pep
Hi Phil,
My hack only works when your ID of edit or delete buttons contain “xxx.000000″.
And I tested it on my Chrome/FireFox/IE6 (With PHP 5.2.14 ), it’s OK.
If you faced the endless spinning icon after click, try http://code.google.com/p/yourls/wiki/EndlessSpinningIcon
Rafael Bonifaz
Great Work @Pep, I would test this soon
Regards,
RAfael
Pep
Hope it will work to you:)
Ozh
Interesting fix, thanks for letting me know. Unfortunately this won’t work for people who have not BC functions enabled on their server. I’m going to use another function to generate HTML id’s, probably a simple hashing function.
Pep
PHP manual said “Since PHP 4.0.4, libbcmath is bundled with PHP”, it should be OK to most users, but rewrite it or use another function may be better.
Ozh
Yeah, it’s bundled in the package, but for some reasons (security or performance I guess) some hosts don’t enable it
Pep
I see……, I believe the algorithm is something like convert hexadecimal (base 36 or 62) to decimal, so rewrite it without BC function would not be very difficult.