Feb 10

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

Tags:

10 Responses to “Resolve YOURLS V1.5 cannot Edit or Delete Links”

  1. August 16, 2011 at 9:41 pm
    matte
    • August 17, 2011 at 2:14 am
      Pep
  2. February 12, 2011 at 12:43 am
    Phil
    • February 12, 2011 at 3:15 am
      Pep
  3. February 10, 2011 at 6:39 pm
    Rafael Bonifaz
    • February 11, 2011 at 2:38 am
      Pep
  4. February 10, 2011 at 7:36 am
    Ozh
    • February 10, 2011 at 1:55 pm
      Pep
      • February 10, 2011 at 5:09 pm
        Ozh
        • February 11, 2011 at 2:48 am
          Pep

Leave a Reply (Set Globally Recognized Avatar