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