]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-formatting.php
Better yourls_int2string() & yourls_string2int()
[Github/YOURLS.git] / includes / functions-formatting.php
1 <?php
2 /*
3  * YOURLS
4  * Function library for anything related to formatting / validating / sanitizing
5  */
6
7 /**
8  * Convert an integer (1337) to a string (3jk).
9  *
10  */
11 function yourls_int2string( $int, $chars = null ) {
12     if( $chars == null )
13                 $chars = yourls_get_shorturl_charset();
14     
15     $base = strlen( $chars );
16
17     $str = '';
18     do {
19         $i   = fmod( $int, $base );
20         $str = $chars[ intval( $i ) ] . $str;
21         $int = ( $int - $i ) / $base;
22     } while( $int > 0 );
23         
24         return yourls_apply_filter( 'int2string', $str, $int, $chars );
25 }
26
27 /**
28  * Convert a string (3jk) to an integer (1337)
29  *
30  */
31 function yourls_string2int( $string, $chars = null ) {
32     if( $chars == null )
33                 $chars = yourls_get_shorturl_charset();
34     
35     $base = strlen( $chars );
36
37     $len = strlen( $string );
38     $integer = 0;
39     $arr = array_flip( str_split( $chars ) );
40     for( $i = 0; $i < $len; ++$i ) {
41         $integer += $arr[ $string[ $i ] ] * pow( $base, $len - $i -1 );
42     }
43         return yourls_apply_filter( 'string2int', $integer, $string, $chars );
44 }
45
46 /**
47  * Return a unique(ish) hash for a string to be used as a valid HTML id
48  *
49  */
50 function yourls_string2htmlid( $string ) {
51         return yourls_apply_filter( 'string2htmlid', 'y'.abs( crc32( $string ) ) );
52 }
53
54 /**
55  * Make sure a link keyword (ie "1fv" as in "site.com/1fv") is valid.
56  *
57  */
58 function yourls_sanitize_string( $string ) {
59         // make a regexp pattern with the shorturl charset, and remove everything but this
60         $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
61         $valid = substr( preg_replace( '![^'.$pattern.']!', '', $string ), 0, 199 );
62         
63         return yourls_apply_filter( 'sanitize_string', $valid, $string );
64 }
65
66 /**
67  * Alias function. I was always getting it wrong.
68  *
69  */
70 function yourls_sanitize_keyword( $keyword ) {
71         return yourls_sanitize_string( $keyword );
72 }
73
74 /**
75  * Sanitize a page title. No HTML per W3C http://www.w3.org/TR/html401/struct/global.html#h-7.4.2
76  *
77  */
78 function yourls_sanitize_title( $unsafe_title ) {
79         $title = $unsafe_title;
80         $title = strip_tags( $title );
81         $title = preg_replace( "/\s+/", ' ', trim( $title ) );
82         return yourls_apply_filter( 'sanitize_title', $title, $unsafe_title );
83 }
84
85 /**
86  * A few sanity checks on the URL. Used for redirection or DB. For display purpose, see yourls_esc_url()
87  *
88  * @param string $unsafe_url unsafe URL
89  * @param array $protocols Optional allowed protocols, default to global $yourls_allowedprotocols
90  * @return string Safe URL
91  */
92 function yourls_sanitize_url( $unsafe_url, $protocols = array() ) {
93         $url = yourls_esc_url( $unsafe_url, 'redirection', $protocols );
94         return yourls_apply_filter( 'sanitize_url', $url, $unsafe_url );
95 }
96
97 /**
98  * Perform a replacement while a string is found, eg $subject = '%0%0%0DDD', $search ='%0D' -> $result =''
99  *
100  * Stolen from WP's _deep_replace
101  *
102  */
103 function yourls_deep_replace( $search, $subject ){
104         $found = true;
105         while($found) {
106                 $found = false;
107                 foreach( (array) $search as $val ) {
108                         while( strpos( $subject, $val ) !== false ) {
109                                 $found = true;
110                                 $subject = str_replace( $val, '', $subject );
111                         }
112                 }
113         }
114         
115         return $subject;
116 }
117
118 /**
119  * Make sure an integer is a valid integer (PHP's intval() limits to too small numbers)
120  *
121  */
122 function yourls_sanitize_int( $in ) {
123         return ( substr( preg_replace( '/[^0-9]/', '', strval( $in ) ), 0, 20 ) );
124 }
125
126 /**
127  * Escape a string or an array of strings before DB usage. ALWAYS escape before using in a SQL query. Thanks.
128  *
129  * @param string|array $data string or array of strings to be escaped
130  * @return string|array escaped data
131  */
132 function yourls_escape( $data ) {
133         if( is_array( $data ) ) {
134                 foreach( $data as $k => $v ) {
135                         if( is_array( $v ) ) {
136                                 $data[ $k ] = yourls_escape( $v );
137                         } else {
138                                 $data[ $k ] = yourls_escape_real( $v );
139                         }
140                 }
141         } else {
142                 $data = yourls_escape_real( $data );
143         }
144         
145         return $data;
146 }
147
148 /**
149  * "Real" escape. This function should NOT be called directly. Use yourls_escape() instead. 
150  *
151  * This function uses a "real" escape if possible, using PDO, MySQL or MySQLi functions,
152  * with a fallback to a "simple" addslashes
153  * If you're implementing a custom DB engine or a custom cache system, you can define an
154  * escape function using filter 'custom_escape_real'
155  *
156  * @since 1.7
157  * @param string $a string to be escaped
158  * @return string escaped string
159  */
160 function yourls_escape_real( $string ) {
161         global $ydb;
162         if( isset( $ydb ) && ( $ydb instanceof ezSQLcore ) )
163                 return $ydb->escape( $string );
164         
165         // YOURLS DB classes have been bypassed by a custom DB engine or a custom cache layer
166         return yourls_apply_filter( 'custom_escape_real', addslashes( $string ), $string );     
167 }
168
169 /**
170  * Sanitize an IP address
171  *
172  */
173 function yourls_sanitize_ip( $ip ) {
174         return preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
175 }
176
177 /**
178  * Make sure a date is m(m)/d(d)/yyyy, return false otherwise
179  *
180  */
181 function yourls_sanitize_date( $date ) {
182         if( !preg_match( '!^\d{1,2}/\d{1,2}/\d{4}$!' , $date ) ) {
183                 return false;
184         }
185         return $date;
186 }
187
188 /**
189  * Sanitize a date for SQL search. Return false if malformed input.
190  *
191  */
192 function yourls_sanitize_date_for_sql( $date ) {
193         if( !yourls_sanitize_date( $date ) )
194                 return false;
195         return date( 'Y-m-d', strtotime( $date ) );
196 }
197
198 /**
199  * Return trimmed string
200  *
201  */
202 function yourls_trim_long_string( $string, $length = 60, $append = '[...]' ) {
203         $newstring = $string;
204         if( function_exists( 'mb_substr' ) ) {
205                 if ( mb_strlen( $newstring ) > $length ) {
206                         $newstring = mb_substr( $newstring, 0, $length - mb_strlen( $append ), 'UTF-8' ) . $append;     
207                 }
208         } else {
209                 if ( strlen( $newstring ) > $length ) {
210                         $newstring = substr( $newstring, 0, $length - strlen( $append ) ) . $append;    
211                 }
212         }
213         return yourls_apply_filter( 'trim_long_string', $newstring, $string, $length, $append );
214 }
215
216 /**
217  * Sanitize a version number (1.4.1-whatever -> 1.4.1)
218  *
219  */
220 function yourls_sanitize_version( $ver ) {
221         return preg_replace( '/[^0-9.]/', '', $ver );
222 }
223
224 /**
225  * Sanitize a filename (no Win32 stuff)
226  *
227  */
228 function yourls_sanitize_filename( $file ) {
229         $file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs
230         $file = preg_replace( '|/+|' ,'/', $file ); // remove any duplicate slash
231         return $file;
232 }
233
234 /**
235  * Check if a string seems to be UTF-8. Stolen from WP.
236  *
237  */
238 function yourls_seems_utf8( $str ) {
239         $length = strlen( $str );
240         for ( $i=0; $i < $length; $i++ ) {
241                 $c = ord( $str[ $i ] );
242                 if ( $c < 0x80 ) $n = 0; # 0bbbbbbb
243                 elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
244                 elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
245                 elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
246                 elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
247                 elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
248                 else return false; # Does not match any model
249                 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
250                         if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
251                                 return false;
252                 }
253         }
254         return true;
255 }
256
257 /**
258  * Checks for invalid UTF8 in a string. Stolen from WP
259  *
260  * @since 1.6
261  *
262  * @param string $string The text which is to be checked.
263  * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
264  * @return string The checked text.
265  */
266 function yourls_check_invalid_utf8( $string, $strip = false ) {
267         $string = (string) $string;
268
269         if ( 0 === strlen( $string ) ) {
270                 return '';
271         }
272
273         // Check for support for utf8 in the installed PCRE library once and store the result in a static
274         static $utf8_pcre;
275         if ( !isset( $utf8_pcre ) ) {
276                 $utf8_pcre = @preg_match( '/^./u', 'a' );
277         }
278         // We can't demand utf8 in the PCRE installation, so just return the string in those cases
279         if ( !$utf8_pcre ) {
280                 return $string;
281         }
282
283         // preg_match fails when it encounters invalid UTF8 in $string
284         if ( 1 === @preg_match( '/^./us', $string ) ) {
285                 return $string;
286         }
287
288         // Attempt to strip the bad chars if requested (not recommended)
289         if ( $strip && function_exists( 'iconv' ) ) {
290                 return iconv( 'utf-8', 'utf-8', $string );
291         }
292
293         return '';
294 }
295
296 /**
297  * Converts a number of special characters into their HTML entities. Stolen from WP.
298  *
299  * Specifically deals with: &, <, >, ", and '.
300  *
301  * $quote_style can be set to ENT_COMPAT to encode " to
302  * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
303  *
304  * @since 1.6
305  *
306  * @param string $string The text which is to be encoded.
307  * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
308  * @param boolean $double_encode Optional. Whether to encode existing html entities. Default is false.
309  * @return string The encoded text with HTML entities.
310  */
311 function yourls_specialchars( $string, $quote_style = ENT_NOQUOTES, $double_encode = false ) {
312         $string = (string) $string;
313
314         if ( 0 === strlen( $string ) )
315                 return '';
316
317         // Don't bother if there are no specialchars - saves some processing
318         if ( ! preg_match( '/[&<>"\']/', $string ) )
319                 return $string;
320
321         // Account for the previous behaviour of the function when the $quote_style is not an accepted value
322         if ( empty( $quote_style ) )
323                 $quote_style = ENT_NOQUOTES;
324         elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
325                 $quote_style = ENT_QUOTES;
326
327         $charset = 'UTF-8';
328
329         $_quote_style = $quote_style;
330
331         if ( $quote_style === 'double' ) {
332                 $quote_style = ENT_COMPAT;
333                 $_quote_style = ENT_COMPAT;
334         } elseif ( $quote_style === 'single' ) {
335                 $quote_style = ENT_NOQUOTES;
336         }
337
338         // Handle double encoding ourselves
339         if ( $double_encode ) {
340                 $string = @htmlspecialchars( $string, $quote_style, $charset );
341         } else {
342                 // Decode &amp; into &
343                 $string = yourls_specialchars_decode( $string, $_quote_style );
344
345                 // Guarantee every &entity; is valid or re-encode the &
346                 $string = yourls_kses_normalize_entities( $string );
347
348                 // Now re-encode everything except &entity;
349                 $string = preg_split( '/(&#?x?[0-9a-z]+;)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
350
351                 for ( $i = 0; $i < count( $string ); $i += 2 )
352                         $string[$i] = @htmlspecialchars( $string[$i], $quote_style, $charset );
353
354                 $string = implode( '', $string );
355         }
356
357         // Backwards compatibility
358         if ( 'single' === $_quote_style )
359                 $string = str_replace( "'", '&#039;', $string );
360
361         return $string;
362 }
363
364 /**
365  * Converts a number of HTML entities into their special characters. Stolen from WP.
366  *
367  * Specifically deals with: &, <, >, ", and '.
368  *
369  * $quote_style can be set to ENT_COMPAT to decode " entities,
370  * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
371  *
372  * @since 1.6
373  *
374  * @param string $string The text which is to be decoded.
375  * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
376  * @return string The decoded text without HTML entities.
377  */
378 function yourls_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
379         $string = (string) $string;
380
381         if ( 0 === strlen( $string ) ) {
382                 return '';
383         }
384
385         // Don't bother if there are no entities - saves a lot of processing
386         if ( strpos( $string, '&' ) === false ) {
387                 return $string;
388         }
389
390         // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
391         if ( empty( $quote_style ) ) {
392                 $quote_style = ENT_NOQUOTES;
393         } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
394                 $quote_style = ENT_QUOTES;
395         }
396
397         // More complete than get_html_translation_table( HTML_SPECIALCHARS )
398         $single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
399         $single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
400         $double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
401         $double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
402         $others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
403         $others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
404
405         if ( $quote_style === ENT_QUOTES ) {
406                 $translation = array_merge( $single, $double, $others );
407                 $translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
408         } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
409                 $translation = array_merge( $double, $others );
410                 $translation_preg = array_merge( $double_preg, $others_preg );
411         } elseif ( $quote_style === 'single' ) {
412                 $translation = array_merge( $single, $others );
413                 $translation_preg = array_merge( $single_preg, $others_preg );
414         } elseif ( $quote_style === ENT_NOQUOTES ) {
415                 $translation = $others;
416                 $translation_preg = $others_preg;
417         }
418
419         // Remove zero padding on numeric entities
420         $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
421
422         // Replace characters according to translation table
423         return strtr( $string, $translation );
424 }
425
426
427 /**
428  * Escaping for HTML blocks. Stolen from WP
429  *
430  * @since 1.6
431  *
432  * @param string $text
433  * @return string
434  */
435 function yourls_esc_html( $text ) {
436         $safe_text = yourls_check_invalid_utf8( $text );
437         $safe_text = yourls_specialchars( $safe_text, ENT_QUOTES );
438         return yourls_apply_filter( 'esc_html', $safe_text, $text );
439 }
440
441 /**
442  * Escaping for HTML attributes.  Stolen from WP
443  *
444  * @since 1.6
445  *
446  * @param string $text
447  * @return string
448  */
449 function yourls_esc_attr( $text ) {
450         $safe_text = yourls_check_invalid_utf8( $text );
451         $safe_text = yourls_specialchars( $safe_text, ENT_QUOTES );
452         return yourls_apply_filter( 'esc_attr', $safe_text, $text );
453 }
454
455 /**
456  * Checks and cleans a URL before printing it. Stolen from WP.
457  *
458  * A number of characters are removed from the URL. If the URL is for displaying
459  * (the default behaviour) ampersands are also replaced.
460  *
461  * @since 1.6
462  *
463  * @param string $url The URL to be cleaned.
464  * @param string $context 'display' or something else. Use yourls_sanitize_url() for database or redirection usage.
465  * @param array $protocols Optional. Array of allowed protocols, defaults to global $yourls_allowedprotocols
466  * @return string The cleaned $url
467  */
468 function yourls_esc_url( $url, $context = 'display', $protocols = array() ) {
469         // make sure there's only one 'http://' at the beginning (prevents pasting a URL right after the default 'http://')
470         $url = str_replace( 
471                 array( 'http://http://', 'http://https://' ),
472                 array( 'http://',        'https://'        ),
473                 $url
474         );
475
476         if ( '' == $url )
477                 return $url;
478
479         // make sure there's a protocol, add http:// if not
480         if ( ! yourls_get_protocol( $url ) )
481                 $url = 'http://'.$url;
482
483         $original_url = $url;
484
485         // force scheme and domain to lowercase - see issues 591 and 1630
486     // We're not using parse_url() here because its opposite, http_build_url(), requires PECL. Plus, who doesn't love a neat Regexp? :)
487         if( preg_match( '!^([a-zA-Z0-9\+\.-]+:)(//)?(.*?@)?([^/#?]+)(.*)$!', $url, $matches ) ) {
488         list( $all, $scheme, $slashes, $userinfo, $domain, $rest ) = $matches;
489         $scheme = strtolower( $scheme );
490         // Domain to lowercase. On URN eg "urn:example:animal:ferret:nose" don't lowercase anything else
491         if( $slashes == '//' )
492             $domain = strtolower( $domain );
493         $url = $scheme . $slashes . $userinfo . $domain . $rest;
494     }
495
496         $url = preg_replace( '|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url );
497         // Previous regexp in YOURLS was '|[^a-z0-9-~+_.?\[\]\^#=!&;,/:%@$\|*`\'<>"()\\x80-\\xff\{\}]|i'
498         // TODO: check if that was it too destructive
499         $strip = array( '%0d', '%0a', '%0D', '%0A' );
500         $url = yourls_deep_replace( $strip, $url );
501         $url = str_replace( ';//', '://', $url );
502
503         // Replace ampersands and single quotes only when displaying.
504         if ( 'display' == $context ) {
505                 $url = yourls_kses_normalize_entities( $url );
506                 $url = str_replace( '&amp;', '&#038;', $url );
507                 $url = str_replace( "'", '&#039;', $url );
508         }
509         
510         if ( ! is_array( $protocols ) or ! $protocols ) {
511                 global $yourls_allowedprotocols;
512                 $protocols = yourls_apply_filter( 'esc_url_protocols', $yourls_allowedprotocols );
513                 // Note: $yourls_allowedprotocols is also globally filterable in functions-kses.php/yourls_kses_init()
514         }
515
516         if ( !yourls_is_allowed_protocol( $url, $protocols ) )
517                 return '';
518         
519         // I didn't use KSES function kses_bad_protocol() because it doesn't work the way I liked (returns //blah from illegal://blah)
520
521         return yourls_apply_filter( 'esc_url', $url, $original_url, $context );
522 }
523
524 /**
525  * Escape single quotes, htmlspecialchar " < > &, and fix line endings. Stolen from WP.
526  *
527  * Escapes text strings for echoing in JS. It is intended to be used for inline JS
528  * (in a tag attribute, for example onclick="..."). Note that the strings have to
529  * be in single quotes. The filter 'js_escape' is also applied here.
530  *
531  * @since 1.6
532  *
533  * @param string $text The text to be escaped.
534  * @return string Escaped text.
535  */
536 function yourls_esc_js( $text ) {
537         $safe_text = yourls_check_invalid_utf8( $text );
538         $safe_text = yourls_specialchars( $safe_text, ENT_COMPAT );
539         $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
540         $safe_text = str_replace( "\r", '', $safe_text );
541         $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
542         return yourls_apply_filter( 'esc_js', $safe_text, $text );
543 }
544
545 /**
546  * Escaping for textarea values. Stolen from WP.
547  *
548  * @since 1.6
549  *
550  * @param string $text
551  * @return string
552  */
553 function yourls_esc_textarea( $text ) {
554         $safe_text = htmlspecialchars( $text, ENT_QUOTES );
555         return yourls_apply_filter( 'esc_textarea', $safe_text, $text );
556 }
557
558
559 /**
560 * PHP emulation of JS's encodeURI
561 *
562 * @link https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
563 * @param $url
564 * @return string
565 */
566 function yourls_encodeURI( $url ) {
567         // Decode URL all the way
568         $result = yourls_rawurldecode_while_encoded( $url );
569         // Encode once
570         $result = strtr( rawurlencode( $result ), array (
571         '%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', '%40' => '@',
572                 '%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', '%21' => '!', '%2A' => '*',
573                 '%27' => '\'', '%28' => '(', '%29' => ')', '%23' => '#',
574     ) );
575         // @TODO:
576         // Known limit: this will most likely break IDN URLs such as http://www.académie-française.fr/
577         // To fully support IDN URLs, advocate use of a plugin.
578         return yourls_apply_filter( 'encodeURI', $result, $url );
579 }
580
581 /**
582  * Adds backslashes before letters and before a number at the start of a string. Stolen from WP.
583  *
584  * @since 1.6
585  *
586  * @param string $string Value to which backslashes will be added.
587  * @return string String with backslashes inserted.
588  */
589 function yourls_backslashit($string) {
590     $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
591     $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
592     return $string;
593 }
594
595 /**
596  * Check if a string seems to be urlencoded
597  *
598  * We use rawurlencode instead of urlencode to avoid messing with '+'
599  *
600  * @since 1.7
601  * @param string $string
602  * @return bool
603  */
604 function yourls_is_rawurlencoded( $string ) {
605         return rawurldecode( $string ) != $string;
606 }
607
608 /**
609  * rawurldecode a string till it's not encoded anymore
610  *
611  * Deals with multiple encoding (eg "%2521" => "%21" => "!").
612  * See https://github.com/YOURLS/YOURLS/issues/1303
613  *
614  * @since 1.7
615  * @param string $string
616  * @return string
617  */
618 function yourls_rawurldecode_while_encoded( $string ) {
619         $string = rawurldecode( $string );
620         if( yourls_is_rawurlencoded( $string ) ) {
621                 $string = yourls_rawurldecode_while_encoded( $string );
622         }
623         return $string;
624 }
625
626 /**
627  * Converts readable Javascript code into a valid bookmarklet link
628  *
629  * Uses https://github.com/ozh/bookmarkletgen
630  *
631  * @since 1.7.1
632  * @param  string $code  Javascript code
633  * @return string        Bookmarklet link
634  */
635 function yourls_make_bookmarklet( $code ) {
636     if ( !class_exists( 'BookmarkletGen', false ) ) {
637         require_once YOURLS_INC . '/BookmarkletGen/BookmarkletGen.php';
638     }
639
640     $book = new BookmarkletGen;
641     return $book->crunch( $code );
642 }