]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-html.php
Merge duplicate HTML elements
[Github/YOURLS.git] / includes / functions-html.php
1 <?php
2
3 /**
4  * Display <h1> header and logo
5  *
6  */
7 function yourls_html_logo() {
8         yourls_do_action( 'pre_html_logo' );
9         ?>
10         <header role="banner">
11         <h1>
12                 <a href="<?php echo yourls_admin_url( 'index.php' ) ?>" title="YOURLS"><span>YOURLS</span>: <span>Y</span>our <span>O</span>wn <span>URL</span> <span>S</span>hortener<br/>
13                 <img src="<?php yourls_site_url(); ?>/images/yourls-logo.png" alt="YOURLS" title="YOURLS" border="0" style="border: 0px;" /></a>
14         </h1>
15         </header>
16         <?php
17         yourls_do_action( 'html_logo' );
18 }
19
20 /**
21  * Display HTML head and <body> tag
22  *
23  * @param string $context Context of the page (stats, index, infos, ...)
24  * @param string $title HTML title of the page
25  */
26 function yourls_html_head( $context = 'index', $title = '' ) {
27
28         yourls_do_action( 'pre_html_head', $context, $title );
29         
30         // All components to false, except when specified true
31         $share = $insert = $tablesorter = $tabs = $cal = $charts = false;
32         
33         // Load components as needed
34         switch ( $context ) {
35                 case 'infos':
36                         $share = $tabs = $charts = true;
37                         break;
38                         
39                 case 'bookmark':
40                         $share = $insert = $tablesorter = true;
41                         break;
42                         
43                 case 'index':
44                         $insert = $tablesorter = $cal = $share = true;
45                         break;
46                         
47                 case 'plugins':
48                 case 'tools':
49                         $tablesorter = true;
50                         break;
51                 
52                 case 'install':
53                 case 'login':
54                 case 'new':
55                 case 'upgrade':
56                         break;
57         }
58         
59         // Force no cache for all admin pages
60         if( yourls_is_admin() && !headers_sent() ) {
61                 header( 'Expires: Thu, 23 Mar 1972 07:00:00 GMT' );
62                 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
63                 header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
64                 header( 'Pragma: no-cache' );
65                 yourls_content_type_header( yourls_apply_filter( 'html_head_content-type', 'text/html' ) );
66                 yourls_do_action( 'admin_headers', $context, $title );
67         }
68         
69         // Store page context in global object
70         global $ydb;
71         $ydb->context = $context;
72         
73         // Body class
74         $bodyclass = yourls_apply_filter( 'bodyclass', '' );
75         $bodyclass .= ( yourls_is_mobile_device() ? 'mobile' : 'desktop' );
76         
77         // Page title
78         $_title = 'YOURLS &mdash; Your Own URL Shortener | ' . yourls_link();
79         $title = $title ? $title . " &laquo; " . $_title : $_title;
80         $title = yourls_apply_filter( 'html_title', $title, $context );
81         
82         ?>
83 <!DOCTYPE html>
84 <html <?php yourls_html_language_attributes(); ?>>
85 <head>
86         <title><?php echo $title ?></title>
87         <link rel="shortcut icon" href="<?php yourls_favicon(); ?>" />
88         <meta http-equiv="Content-Type" content="<?php echo yourls_apply_filter( 'html_head_meta_content-type', 'text/html; charset=utf-8' ); ?>" />
89         <meta name="generator" content="YOURLS <?php echo YOURLS_VERSION ?>" />
90         <meta name="description" content="YOURLS &raquo; Your Own URL Shortener' | <?php yourls_site_url(); ?>" />
91     <meta name="referrer" content="always" />
92         <script src="<?php yourls_site_url(); ?>/js/jquery-1.9.1.min.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
93         <script src="<?php yourls_site_url(); ?>/js/common.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
94         <script src="<?php yourls_site_url(); ?>/js/jquery.notifybar.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
95         <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/style.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
96         <?php if ( $tabs ) { ?>
97                 <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/infos.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
98                 <script src="<?php yourls_site_url(); ?>/js/infos.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
99         <?php } ?>
100         <?php if ( $tablesorter ) { ?>
101                 <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/tablesorter.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
102                 <script src="<?php yourls_site_url(); ?>/js/jquery.tablesorter.min.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
103         <?php } ?>
104         <?php if ( $insert ) { ?>
105                 <script src="<?php yourls_site_url(); ?>/js/insert.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
106         <?php } ?>
107         <?php if ( $share ) { ?>
108                 <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/share.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
109                 <script src="<?php yourls_site_url(); ?>/js/share.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
110                 <script src="<?php yourls_site_url(); ?>/js/jquery.zclip.min.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
111         <?php } ?>
112         <?php if ( $cal ) { ?>
113                 <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/cal.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
114                 <?php yourls_l10n_calendar_strings(); ?>
115                 <script src="<?php yourls_site_url(); ?>/js/jquery.cal.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
116         <?php } ?>
117         <?php if ( $charts ) { ?>
118                         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
119                         <script type="text/javascript">
120                                          google.load('visualization', '1.0', {'packages':['corechart', 'geochart']});
121                         </script>
122         <?php } ?>
123         <script type="text/javascript">
124         //<![CDATA[
125                 var ajaxurl  = '<?php echo yourls_admin_url( 'admin-ajax.php' ); ?>';
126                 var zclipurl = '<?php yourls_site_url(); ?>/js/ZeroClipboard.swf';
127         //]]>
128         </script>
129         <?php yourls_do_action( 'html_head', $context ); ?>
130 </head>
131 <body class="<?php echo $context; ?> <?php echo $bodyclass; ?>">
132 <div id="wrap">
133         <?php
134 }
135
136 /**
137  * Display HTML footer (including closing body & html tags)
138  *
139  */
140 function yourls_html_footer() {
141         global $ydb;
142         
143         $num_queries = sprintf( yourls_n( '1 query', '%s queries', $ydb->num_queries ), $ydb->num_queries );
144         ?>
145         </div><?php // wrap ?>
146         <footer id="footer" role="contentinfo"><p>
147                 <?php
148                 $footer  = yourls_s( 'Powered by %s', '<a href="http://yourls.org/" title="YOURLS">YOURLS</a> v ' . YOURLS_VERSION );
149                 $footer .= ' &ndash; '.$num_queries;
150                 echo yourls_apply_filter( 'html_footer_text', $footer );
151                 ?>
152         </p></footer>
153         <?php if( defined( 'YOURLS_DEBUG' ) && YOURLS_DEBUG == true ) {
154                 echo '<div style="text-align:left"><pre>';
155                 echo join( "\n", $ydb->debug_log );
156                 echo '</div>';
157         } ?>
158         <?php yourls_do_action( 'html_footer', $ydb->context ); ?>
159         </body>
160         </html>
161         <?php
162 }
163
164 /**
165  * Display "Add new URL" box
166  *
167  * @param string $url URL to prefill the input with
168  * @param string $keyword Keyword to prefill the input with
169  */
170 function yourls_html_addnew( $url = '', $keyword = '' ) {
171         ?>
172         <main role="main">
173         <div id="new_url">
174                 <div>
175                         <form id="new_url_form" action="" method="get">
176                                 <div><strong><?php yourls_e( 'Enter the URL' ); ?></strong>:<input type="text" id="add-url" name="url" value="<?php echo $url; ?>" class="text" size="80" placeholder="http://" />
177                                 <?php yourls_e( 'Optional '); ?> : <strong><?php yourls_e('Custom short URL'); ?></strong>:<input type="text" id="add-keyword" name="keyword" value="<?php echo $keyword; ?>" class="text" size="8" />
178                                 <?php yourls_nonce_field( 'add_url', 'nonce-add' ); ?>
179                                 <input type="button" id="add-button" name="add-button" value="<?php yourls_e( 'Shorten The URL' ); ?>" class="button" onclick="add_link();" /></div>
180                         </form>
181                         <div id="feedback" style="display:none"></div>
182                 </div>
183                 <?php yourls_do_action( 'html_addnew' ); ?>
184         </div>
185         <?php 
186 }
187
188 /**
189  * Display main table's footer
190  *
191  * The $param array is defined in /admin/index.php, check the yourls_html_tfooter() call
192  *
193  * @param array $params Array of all required parameters
194  * @return string Result
195  */
196 function yourls_html_tfooter( $params = array() ) {
197         extract( $params ); // extract $search_text, $page, $search_in ...
198         ?>
199         <tfoot>
200                 <tr>
201                         <th colspan="6">
202                         <div id="filter_form">
203                                 <form action="" method="get">
204                                         <div id="filter_options">
205                                                 <?php
206                                                 
207                                                 // First search control: text to search
208                                                 $_input = '<input type="text" name="search" class="text" size="12" value="' . yourls_esc_attr( $search_text ) . '" />';
209                                                 $_options = array(
210                             'all'     => yourls__( 'All fields' ),
211                                                         'keyword' => yourls__( 'Short URL' ),
212                                                         'url'     => yourls__( 'URL' ),
213                                                         'title'   => yourls__( 'Title' ),
214                                                         'ip'      => yourls__( 'IP' ),
215                                                 );                                                      
216                                                 $_select = yourls_html_select( 'search_in', $_options, $search_in );
217                                                 /* //translators: "Search for <input field with text to search> in <select dropdown with URL, title...>" */
218                                                 yourls_se( 'Search for %1$s in %2$s', $_input , $_select );
219                                                 echo "&ndash;\n";
220                                                 
221                                                 // Second search control: order by
222                                                 $_options = array(
223                                                         'keyword'      => yourls__( 'Short URL' ),
224                                                         'url'          => yourls__( 'URL' ),
225                                                         'timestamp'    => yourls__( 'Date' ),
226                                                         'ip'           => yourls__( 'IP' ),
227                                                         'clicks'       => yourls__( 'Clicks' ),
228                                                 );
229                                                 $_select = yourls_html_select( 'sort_by', $_options, $sort_by );
230                                                 $sort_order = isset( $sort_order ) ? $sort_order : 'desc' ;
231                                                 $_options = array(
232                                                         'asc'  => yourls__( 'Ascending' ),
233                                                         'desc' => yourls__( 'Descending' ),
234                                                 );
235                                                 $_select2 = yourls_html_select( 'sort_order', $_options, $sort_order );
236                                                 /* //translators: "Order by <criteria dropdown (date, clicks...)> in <order dropdown (Descending or Ascending)>" */
237                                                 yourls_se( 'Order by %1$s %2$s', $_select , $_select2 );
238                                                 echo "&ndash;\n";
239                                                 
240                                                 // Third search control: Show XX rows
241                                                 /* //translators: "Show <text field> rows" */
242                                                 yourls_se( 'Show %s rows',  '<input type="text" name="perpage" class="text" size="2" value="' . $perpage . '" />' );
243                                                 echo "<br/>\n";
244
245                                                 // Fourth search control: Show links with more than XX clicks
246                                                 $_options = array(
247                                                         'more' => yourls__( 'more' ),
248                                                         'less' => yourls__( 'less' ),
249                                                 );
250                                                 $_select = yourls_html_select( 'click_filter', $_options, $click_filter );
251                                                 $_input  = '<input type="text" name="click_limit" class="text" size="4" value="' . $click_limit . '" /> ';
252                                                 /* //translators: "Show links with <more/less> than <text field> clicks" */
253                                                 yourls_se( 'Show links with %1$s than %2$s clicks', $_select, $_input );
254                                                 echo "<br/>\n";
255
256                                                 // Fifth search control: Show links created before/after/between ...
257                                                 $_options = array(
258                                                         'before'  => yourls__('before'),
259                                                         'after'   => yourls__('after'),
260                                                         'between' => yourls__('between'),
261                                                 );
262                                                 $_select = yourls_html_select( 'date_filter', $_options, $date_filter );
263                                                 $_input  = '<input type="text" name="date_first" id="date_first" class="text" size="12" value="' . $date_first . '" />';
264                                                 $_and    = '<span id="date_and"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '> &amp; </span>';
265                                                 $_input2 = '<input type="text" name="date_second" id="date_second" class="text" size="12" value="' . $date_second . '"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '/>';
266                                                 /* //translators: "Show links created <before/after/between> <date input> <"and" if applicable> <date input if applicable>" */
267                                                 yourls_se( 'Show links created %1$s %2$s %3$s %4$s', $_select, $_input, $_and, $_input2 );
268                                                 ?>
269
270                                                 <div id="filter_buttons">
271                                                         <input type="submit" id="submit-sort" value="<?php yourls_e('Search'); ?>" class="button primary" />
272                                                         &nbsp;
273                                                         <input type="button" id="submit-clear-filter" value="<?php yourls_e('Clear'); ?>" class="button" onclick="window.parent.location.href = 'index.php'" />
274                                                 </div>
275                                 
276                                         </div>
277                                 </form>
278                         </div>
279                         
280                         <?php
281                         // Remove empty keys from the $params array so it doesn't clutter the pagination links
282                         $params = array_filter( $params, 'yourls_return_if_not_empty_string' ); // remove empty keys
283
284                         if( isset( $search_text ) ) {
285                                 $params['search'] = $search_text;
286                                 unset( $params['search_text'] );
287                         }
288                         ?>
289                         
290                         <div id="pagination">
291                                 <span class="navigation">
292                                 <?php if( $total_pages > 1 ) { ?>
293                                         <span class="nav_total"><?php echo sprintf( yourls_n( '1 page', '%s pages', $total_pages ), $total_pages ); ?></span>
294                                         <?php
295                                         $base_page = yourls_admin_url( 'index.php' );
296                                         // Pagination offsets: min( max ( zomg! ) );
297                                         $p_start = max(  min( $total_pages - 4, $page - 2 ), 1 );
298                                         $p_end = min( max( 5, $page + 2 ), $total_pages );
299                                         if( $p_start >= 2 ) {
300                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => 1 ) ), $base_page );
301                                                 echo '<span class="nav_link nav_first"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( '&laquo; First' ) . '</a></span>';
302                                                 echo '<span class="nav_link nav_prev"></span>';
303                                         }
304                                         for( $i = $p_start ; $i <= $p_end; $i++ ) {
305                                                 if( $i == $page ) {
306                                                         echo "<span class='nav_link nav_current'>$i</span>";
307                                                 } else {
308                                                         $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $i ) ), $base_page );
309                                                         echo '<span class="nav_link nav_goto"><a href="' . $link . '" title="' . sprintf( yourls_esc_attr( 'Page %s' ), $i ) .'">'.$i.'</a></span>';
310                                                 }
311                                         }
312                                         if( ( $p_end ) < $total_pages ) {
313                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $total_pages ) ), $base_page );
314                                                 echo '<span class="nav_link nav_next"></span>';
315                                                 echo '<span class="nav_link nav_last"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( 'Last &raquo;' ) . '</a></span>';
316                                         }
317                                         ?>
318                                 <?php } ?>
319                                 </span>
320                         </div>
321                         </th>
322                 </tr>
323                 <?php yourls_do_action( 'html_tfooter' ); ?>
324         </tfoot>
325         <?php
326 }
327
328 /**
329  * Return a select box
330  *
331  * @since 1.6
332  *
333  * @param string $name HTML 'name' (also use as the HTML 'id')
334  * @param array $options array of 'value' => 'Text displayed'
335  * @param string $selected optional 'value' from the $options array that will be highlighted
336  * @param boolean $display false (default) to return, true to echo
337  * @return string HTML content of the select element
338  */
339 function yourls_html_select( $name, $options, $selected = '', $display = false ) {
340         $html = "<select name='$name' id='$name' size='1'>\n";
341         foreach( $options as $value => $text ) {
342                 $html .= "<option value='$value' ";
343                 $html .= $selected == $value ? ' selected="selected"' : '';
344                 $html .= ">$text</option>\n";
345         }
346         $html .= "</select>\n";
347         $html  = yourls_apply_filter( 'html_select', $html, $name, $options, $selected, $display );
348         if( $display )
349                 echo $html;
350         return $html;
351 }
352
353 /**
354  * Display the Quick Share box
355  *
356  */
357 function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlink_title = '', $share_title = '', $hidden = false ) {
358         if ( $shortlink_title == '' )
359                 $shortlink_title = '<h2>' . yourls__( 'Your short link' ) . '</h2>';
360         if ( $share_title == '' )
361                 $share_title = '<h2>' . yourls__( 'Quick Share' ) . '</h2>';
362         
363         // Allow plugins to short-circuit the whole function
364         $pre = yourls_apply_filter( 'shunt_share_box', false );
365         if ( false !== $pre )
366                 return $pre;
367                 
368         $text   = ( $text ? '"'.$text.'" ' : '' );
369         $title  = ( $title ? "$title " : '' );
370         $share  = yourls_esc_textarea( $title.$text.$shorturl );
371         $count  = 140 - strlen( $share );
372         $hidden = ( $hidden ? 'style="display:none;"' : '' );
373         
374         // Allow plugins to filter all data
375         $data = compact( 'longurl', 'shorturl', 'title', 'text', 'shortlink_title', 'share_title', 'share', 'count', 'hidden' );
376         $data = yourls_apply_filter( 'share_box_data', $data );
377         extract( $data );
378         
379         $_share = rawurlencode( $share );
380         $_url   = rawurlencode( $shorturl );
381         ?>
382         
383         <div id="shareboxes" <?php echo $hidden; ?>>
384
385                 <?php yourls_do_action( 'shareboxes_before', $longurl, $shorturl, $title, $text ); ?>
386
387                 <div id="copybox" class="share">
388                 <?php echo $shortlink_title; ?>
389                         <p><input id="copylink" class="text" size="32" value="<?php echo yourls_esc_url( $shorturl ); ?>" /></p>
390                         <p><small><?php yourls_e( 'Long link' ); ?>: <a id="origlink" href="<?php echo yourls_esc_url( $longurl ); ?>"><?php echo yourls_esc_url( $longurl ); ?></a></small>
391                         <?php if( yourls_do_log_redirect() ) { ?>
392                         <br/><small><?php yourls_e( 'Stats' ); ?>: <a id="statlink" href="<?php echo yourls_esc_url( $shorturl ); ?>+"><?php echo yourls_esc_url( $shorturl ); ?>+</a></small>
393                         <input type="hidden" id="titlelink" value="<?php echo yourls_esc_attr( $title ); ?>" />
394                         <?php } ?>
395                         </p>
396                 </div>
397
398                 <?php yourls_do_action( 'shareboxes_middle', $longurl, $shorturl, $title, $text ); ?>
399
400                 <div id="sharebox" class="share">
401                         <?php echo $share_title; ?>
402                         <div id="tweet">
403                                 <span id="charcount" class="hide-if-no-js"><?php echo $count; ?></span>
404                                 <textarea id="tweet_body"><?php echo $share; ?></textarea>
405                         </div>
406                         <p id="share_links"><?php yourls_e( 'Share with' ); ?> 
407                                 <a id="share_tw" href="http://twitter.com/home?status=<?php echo $_share; ?>" title="<?php yourls_e( 'Tweet this!' ); ?>" onclick="share('tw');return false">Twitter</a>
408                                 <a id="share_fb" href="http://www.facebook.com/share.php?u=<?php echo $_url; ?>" title="<?php yourls_e( 'Share on Facebook' ); ?>" onclick="share('fb');return false;">Facebook</a>
409                                 <a id="share_ff" href="http://friendfeed.com/share/bookmarklet/frame#title=<?php echo $_share; ?>" title="<?php yourls_e( 'Share on Friendfeed' ); ?>" onclick="share('ff');return false;">FriendFeed</a>
410                                 <?php
411                                 yourls_do_action( 'share_links', $longurl, $shorturl, $title, $text );
412                                 // Note: on the main admin page, there are no parameters passed to the sharebox when it's drawn.
413                                 ?>
414                         </p>
415                 </div>
416                 
417                 <?php yourls_do_action( 'shareboxes_after', $longurl, $shorturl, $title, $text ); ?>
418         
419         </div>
420         
421         <?php
422 }
423
424 /**
425  * Die die die
426  *
427  */
428 function yourls_die( $message = '', $title = '', $header_code = 200 ) {
429     yourls_do_action( 'pre_yourls_die', $message, $title, $header_code );
430
431         yourls_status_header( $header_code );
432         
433         if( !yourls_did_action( 'html_head' ) ) {
434                 yourls_html_head();
435                 yourls_html_logo();
436         }
437         echo yourls_apply_filter( 'die_title', "<h2>$title</h2>" );
438         echo yourls_apply_filter( 'die_message', "<p>$message</p>" );
439     // Hook into 'yourls_die' to add more elements or messages to that page
440         yourls_do_action( 'yourls_die' );
441         if( !yourls_did_action( 'html_footer' ) ) {
442                 yourls_html_footer();
443         }
444         die();
445 }
446
447 /**
448  * Return an "Edit" row for the main table
449  *
450  * @param string $keyword Keyword to edit
451  * @return string HTML of the edit row
452  */
453 function yourls_table_edit_row( $keyword ) {
454         $keyword = yourls_sanitize_string( $keyword );
455         $id = yourls_string2htmlid( $keyword ); // used as HTML #id
456         $url = yourls_get_keyword_longurl( $keyword );
457         $title = htmlspecialchars( yourls_get_keyword_title( $keyword ) );
458         $safe_url = yourls_esc_attr( rawurldecode( $url ) );
459         $safe_title = yourls_esc_attr( $title );
460     
461     // Make strings sprintf() safe: '%' -> '%%'
462     $safe_url = str_replace( '%', '%%', $safe_url );
463     $safe_title = str_replace( '%', '%%', $safe_title );
464
465         $www = yourls_link();
466     
467         $nonce = yourls_create_nonce( 'edit-save_'.$id );
468         
469         if( $url ) {
470                 $return = <<<RETURN
471 <tr id="edit-$id" class="edit-row"><td colspan="5" class="edit-row"><strong>%s</strong>:<input type="text" id="edit-url-$id" name="edit-url-$id" value="$safe_url" class="text" size="70" /><br/><strong>%s</strong>: $www<input type="text" id="edit-keyword-$id" name="edit-keyword-$id" value="$keyword" class="text" size="10" /><br/><strong>%s</strong>: <input type="text" id="edit-title-$id" name="edit-title-$id" value="$safe_title" class="text" size="60" /></td><td colspan="1"><input type="button" id="edit-submit-$id" name="edit-submit-$id" value="%s" title="%s" class="button" onclick="edit_link_save('$id');" />&nbsp;<input type="button" id="edit-close-$id" name="edit-close-$id" value="%s" title="%s" class="button" onclick="edit_link_hide('$id');" /><input type="hidden" id="old_keyword_$id" value="$keyword"/><input type="hidden" id="nonce_$id" value="$nonce"/></td></tr>
472 RETURN;
473                 $return = sprintf( $return, yourls__( 'Long URL' ), yourls__( 'Short URL' ), yourls__( 'Title' ), yourls__( 'Save' ), yourls__( 'Save new values' ), yourls__( 'Cancel' ), yourls__( 'Cancel editing' ) );
474         } else {
475                 $return = '<tr class="edit-row notfound"><td colspan="6" class="edit-row notfound">' . yourls__( 'Error, URL not found' ) . '</td></tr>';
476         }
477         
478         $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title );
479
480         return $return;
481 }
482
483 /**
484  * Return an "Add" row for the main table
485  *
486  * @return string HTML of the edit row
487  */
488 function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) {
489         $keyword  = yourls_sanitize_string( $keyword );
490         $id       = yourls_string2htmlid( $keyword ); // used as HTML #id
491         $shorturl = yourls_link( $keyword );
492
493         $statlink = yourls_statlink( $keyword );
494                 
495         $delete_link = yourls_nonce_url( 'delete-link_'.$id,
496                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'delete', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
497         );
498         
499         $edit_link = yourls_nonce_url( 'edit-link_'.$id,
500                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
501         );
502         
503         // Action link buttons: the array
504         $actions = array(
505                 'stats' => array(
506                         'href'    => $statlink,
507                         'id'      => "statlink-$id",
508                         'title'   => yourls_esc_attr__( 'Stats' ),
509                         'anchor'  => yourls__( 'Stats' ),
510                 ),
511                 'share' => array(
512                         'href'    => '',
513                         'id'      => "share-button-$id",
514                         'title'   => yourls_esc_attr__( 'Share' ),
515                         'anchor'  => yourls__( 'Share' ),
516                         'onclick' => "toggle_share('$id');return false;",
517                 ),
518                 'edit' => array(
519                         'href'    => $edit_link,
520                         'id'      => "edit-button-$id",
521                         'title'   => yourls_esc_attr__( 'Edit' ),
522                         'anchor'  => yourls__( 'Edit' ),
523                         'onclick' => "edit_link_display('$id');return false;",
524                 ),
525                 'delete' => array(
526                         'href'    => $delete_link,
527                         'id'      => "delete-button-$id",
528                         'title'   => yourls_esc_attr__( 'Delete' ),
529                         'anchor'  => yourls__( 'Delete' ),
530                         'onclick' => "remove_link('$id');return false;",
531                 )
532         );
533         $actions = yourls_apply_filter( 'table_add_row_action_array', $actions );
534         
535         // Action link buttons: the HTML
536         $action_links = '';
537         foreach( $actions as $key => $action ) {
538                 $onclick = isset( $action['onclick'] ) ? 'onclick="' . $action['onclick'] . '"' : '' ;
539                 $action_links .= sprintf( '<a href="%s" id="%s" title="%s" class="%s" %s>%s</a>',
540                         $action['href'], $action['id'], $action['title'], 'button button_'.$key, $onclick, $action['anchor']
541                 );
542         }
543         $action_links = yourls_apply_filter( 'action_links', $action_links, $keyword, $url, $ip, $clicks, $timestamp );
544
545         if( ! $title )
546                 $title = $url;
547
548         $protocol_warning = '';
549         if( ! in_array( yourls_get_protocol( $url ) , array( 'http://', 'https://' ) ) )
550                 $protocol_warning = yourls_apply_filter( 'add_row_protocol_warning', '<span class="warning" title="' . yourls__( 'Not a common link' ) . '">&#9733;</span>' );
551
552         // Row cells: the array
553         $cells = array(
554                 'keyword' => array(
555                         'template'      => '<a href="%shorturl%">%keyword_html%</a>',
556                         'shorturl'      => yourls_esc_url( $shorturl ),
557                         'keyword_html'  => yourls_esc_html( $keyword ),
558                 ),
559                 'url' => array(
560                         'template'      => '<a href="%long_url%" title="%title_attr%">%title_html%</a><br/><small>%warning%<a href="%long_url%">%long_url_html%</a></small>',
561                         'long_url'      => yourls_esc_url( $url ),
562                         'title_attr'    => yourls_esc_attr( $title ),
563                         'title_html'    => yourls_esc_html( yourls_trim_long_string( $title ) ),
564                         'long_url_html' => yourls_esc_html( yourls_trim_long_string( $url ) ),
565                         'warning'       => $protocol_warning,
566                 ),
567                 'timestamp' => array(
568                         'template' => '%date%',
569                         'date'     => date( 'M d, Y H:i', $timestamp +( YOURLS_HOURS_OFFSET * 3600 ) ),
570                 ),
571                 'ip' => array(
572                         'template' => '%ip%',
573                         'ip'       => $ip,
574                 ),
575                 'clicks' => array(
576                         'template' => '%clicks%',
577                         'clicks'   => yourls_number_format_i18n( $clicks, 0, '', '' ),
578                 ),
579                 'actions' => array(
580                         'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',
581                         'actions'  => $action_links,
582                         'id'       => $id,
583                         'keyword'  => $keyword,
584                 ),
585         );
586         $cells = yourls_apply_filter( 'table_add_row_cell_array', $cells, $keyword, $url, $title, $ip, $clicks, $timestamp );
587         
588         // Row cells: the HTML. Replace every %stuff% in 'template' with 'stuff' value.
589         $row = "<tr id=\"id-$id\">";
590         foreach( $cells as $cell_id => $elements ) {
591                 $callback = new yourls_table_add_row_callback( $elements );
592                 $row .= sprintf( '<td class="%s" id="%s">', $cell_id, $cell_id . '-' . $id );
593                 $row .= preg_replace_callback( '/%([^%]+)?%/', array( $callback, 'callback' ), $elements['template'] );
594                 // For the record, in PHP 5.3+ we don't need to introduce a class in order to pass additional parameters
595                 // to the callback function. Instead, we would have used the 'use' keyword :
596                 // $row .= preg_replace_callback( '/%([^%]+)?%/', function( $match ) use ( $elements ) { return $elements[ $match[1] ]; }, $elements['template'] );
597                 
598                 $row .= '</td>';
599         }
600         $row .= "</tr>";
601         $row  = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $title, $ip, $clicks, $timestamp );
602         
603         return $row;
604 }
605
606 /**
607  * Callback class for yourls_table_add_row
608  *
609  * See comment about PHP 5.3+ in yourls_table_add_row()
610  *
611  * @since 1.7
612  */
613 class yourls_table_add_row_callback {
614     private $elements;
615         
616     function __construct($elements) {
617                 $this->elements = $elements;
618         }
619         
620     function callback( $matches ) {
621                 return $this->elements[ $matches[1] ];
622     }
623 }
624
625
626 /**
627  * Echo the main table head
628  *
629  */
630 function yourls_table_head() {
631         $start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";
632         echo yourls_apply_filter( 'table_head_start', $start );
633         
634         $cells = yourls_apply_filter( 'table_head_cells', array(
635                 'shorturl' => yourls__( 'Short URL' ),
636                 'longurl'  => yourls__( 'Original URL' ),
637                 'date'     => yourls__( 'Date' ),
638                 'ip'       => yourls__( 'IP' ),
639                 'clicks'   => yourls__( 'Clicks' ),
640                 'actions'  => yourls__( 'Actions' )
641         ) );
642         foreach( $cells as $k => $v ) {
643                 echo "<th id='main_table_head_$k'>$v</th>\n";
644         }
645         
646         $end = "</tr></thead>\n";
647         echo yourls_apply_filter( 'table_head_end', $end );
648 }
649
650 /**
651  * Echo the tbody start tag
652  *
653  */
654 function yourls_table_tbody_start() {
655         echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );
656 }
657
658 /**
659  * Echo the tbody end tag
660  *
661  */
662 function yourls_table_tbody_end() {
663         echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );
664 }
665
666 /**
667  * Echo the table start tag
668  *
669  */
670 function yourls_table_end() {
671         echo yourls_apply_filter( 'table_end', '</table></main>' );
672 }
673
674 /**
675  * Echo HTML tag for a link
676  *
677  */
678 function yourls_html_link( $href, $title = '', $element = '' ) {
679         if( !$title )
680                 $title = $href;
681         if( $element )
682                 $element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );
683         $link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );
684         echo yourls_apply_filter( 'html_link', $link );
685 }
686
687 /**
688  * Display the login screen. Nothing past this point.
689  *
690  */
691 function yourls_login_screen( $error_msg = '' ) {
692         yourls_html_head( 'login' );
693         
694         $action = ( isset( $_GET['action'] ) && $_GET['action'] == 'logout' ? '?' : '' );
695
696         yourls_html_logo();
697         ?>
698         <div id="login">
699                 <form method="post" action="<?php echo $action; ?>"> <?php // reset any QUERY parameters ?>
700                         <?php
701                                 if( !empty( $error_msg ) ) {
702                                         echo '<p class="error">'.$error_msg.'</p>';
703                                 }
704                         ?>
705                         <p>
706                                 <label for="username"><?php yourls_e( 'Username' ); ?></label><br />
707                                 <input type="text" id="username" name="username" size="30" class="text" />
708                         </p>
709                         <p>
710                                 <label for="password"><?php yourls_e( 'Password' ); ?></label><br />
711                                 <input type="password" id="password" name="password" size="30" class="text" />
712                         </p>
713                         <p style="text-align: right;">
714                                 <input type="submit" id="submit" name="submit" value="<?php yourls_e( 'Login' ); ?>" class="button" />
715                         </p>
716                 </form>
717                 <script type="text/javascript">$('#username').focus();</script>
718         </div>
719         <?php
720         yourls_html_footer();
721         die();
722 }
723
724 /**
725  * Display the admin menu
726  *
727  */
728 function yourls_html_menu() {
729
730         // Build menu links
731         if( defined( 'YOURLS_USER' ) ) {
732                 $logout_link = yourls_apply_filter( 'logout_link', sprintf( yourls__('Hello <strong>%s</strong>'), YOURLS_USER ) . ' (<a href="?action=logout" title="' . yourls_esc_attr__( 'Logout' ) . '">' . yourls__( 'Logout' ) . '</a>)' );
733         } else {
734                 $logout_link = yourls_apply_filter( 'logout_link', '' );
735         }
736         $help_link   = yourls_apply_filter( 'help_link',   '<a href="' . yourls_site_url( false ) .'/readme.html">' . yourls__( 'Help' ) . '</a>' );
737         
738         $admin_links    = array();
739         $admin_sublinks = array();
740         
741         $admin_links['admin'] = array(
742                 'url'    => yourls_admin_url( 'index.php' ),
743                 'title'  => yourls__( 'Go to the admin interface' ),
744                 'anchor' => yourls__( 'Admin interface' )
745         );
746         
747         if( yourls_is_admin() ) {
748                 $admin_links['tools'] = array(
749                         'url'    => yourls_admin_url( 'tools.php' ),
750                         'anchor' => yourls__( 'Tools' )
751                 );
752                 $admin_links['plugins'] = array(
753                         'url'    => yourls_admin_url( 'plugins.php' ),
754                         'anchor' => yourls__( 'Manage Plugins' )
755                 );
756                 $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages();
757         }
758         
759         $admin_links    = yourls_apply_filter( 'admin_links',    $admin_links );
760         $admin_sublinks = yourls_apply_filter( 'admin_sublinks', $admin_sublinks );
761         
762         // Now output menu
763         echo '<nav role="navigation"><ul id="admin_menu">'."\n";
764         if ( yourls_is_private() && !empty( $logout_link ) )
765                 echo '<li id="admin_menu_logout_link">' . $logout_link .'</li>';
766
767         foreach( (array)$admin_links as $link => $ar ) {
768                 if( isset( $ar['url'] ) ) {
769                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
770                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
771                         printf( '<li id="admin_menu_%s_link" class="admin_menu_toplevel"><a href="%s" %s>%s</a>', $link, $ar['url'], $title, $anchor );
772                 }
773                 // Output submenu if any. TODO: clean up, too many code duplicated here
774                 if( isset( $admin_sublinks[$link] ) ) {
775                         echo "<ul>\n";
776                         foreach( $admin_sublinks[$link] as $link => $ar ) {
777                                 if( isset( $ar['url'] ) ) {
778                                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
779                                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
780                                         printf( '<li id="admin_menu_%s_link" class="admin_menu_sublevel admin_menu_sublevel_%s"><a href="%s" %s>%s</a>', $link, $link, $ar['url'], $title, $anchor );
781                                 }
782                         }
783                         echo "</ul>\n";
784                 }
785         }
786         
787         if ( isset( $help_link ) )
788                 echo '<li id="admin_menu_help_link">' . $help_link .'</li>';
789                 
790         yourls_do_action( 'admin_menu' );
791         echo "</ul></nav>\n";
792         yourls_do_action( 'admin_notices' );
793         yourls_do_action( 'admin_notice' ); // because I never remember if it's 'notices' or 'notice'
794         /*
795         To display a notice:
796         $message = "<div>OMG, dude, I mean!</div>" );
797         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
798         */
799 }
800
801 /**
802  * Wrapper function to display admin notices
803  *
804  */
805 function yourls_add_notice( $message, $style = 'notice' ) {
806         // Escape single quotes in $message to avoid breaking the anonymous function
807         $message = yourls_notice_box( strtr( $message, array( "'" => "\'" ) ), $style );
808         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
809 }
810
811 /**
812  * Return a formatted notice
813  *
814  */
815 function yourls_notice_box( $message, $style = 'notice' ) {
816         return <<<HTML
817         <div class="$style">
818         <p>$message</p>
819         </div>
820 HTML;
821 }
822
823 /**
824  * Display a page
825  *
826  */
827 function yourls_page( $page ) {
828         $include = YOURLS_ABSPATH . "/pages/$page.php";
829         if( !file_exists( $include ) ) {
830                 yourls_die( "Page '$page' not found", 'Not found', 404 );
831         }
832         yourls_do_action( 'pre_page', $page );
833         include_once( $include );
834         yourls_do_action( 'post_page', $page );
835         die();  
836 }
837
838 /**
839  * Display the language attributes for the HTML tag.
840  *
841  * Builds up a set of html attributes containing the text direction and language
842  * information for the page. Stolen from WP.
843  *
844  * @since 1.6
845  */
846 function yourls_html_language_attributes() {
847         $attributes = array();
848         $output = '';
849         
850         $attributes[] = ( yourls_is_rtl() ? 'dir="rtl"' : 'dir="ltr"' );
851         
852         $doctype = yourls_apply_filter( 'html_language_attributes_doctype', 'html' );
853         // Experimental: get HTML lang from locale. Should work. Convert fr_FR -> fr-FR
854         if ( $lang = str_replace( '_', '-', yourls_get_locale() ) ) {
855                 if( $doctype == 'xhtml' ) {
856                         $attributes[] = "xml:lang=\"$lang\"";
857                 } else {
858                         $attributes[] = "lang=\"$lang\"";
859                 }
860         }
861
862         $output = implode( ' ', $attributes );
863         $output = yourls_apply_filter( 'html_language_attributes', $output );
864         echo $output;
865 }
866
867 /**
868  * Output translated strings used by the Javascript calendar
869  *
870  * @since 1.6
871  */
872 function yourls_l10n_calendar_strings() {
873         echo "\n<script>\n";
874         echo "var l10n_cal_month = " . json_encode( array_values( yourls_l10n_months() ) ) . ";\n";
875         echo "var l10n_cal_days = " . json_encode( array_values( yourls_l10n_weekday_initial() ) ) . ";\n";
876         echo "var l10n_cal_today = \"" . yourls_esc_js( yourls__( 'Today' ) ) . "\";\n";
877         echo "var l10n_cal_close = \"" . yourls_esc_js( yourls__( 'Close' ) ) . "\";\n";
878         echo "</script>\n";
879         
880         // Dummy returns, to initialize l10n strings used in the calendar
881         yourls__( 'Today' );
882         yourls__( 'Close' );
883 }
884
885
886 /**
887  * Display a notice if there is a newer version of YOURLS available
888  *
889  * @since 1.7
890  */
891 function yourls_new_core_version_notice() {
892
893         yourls_debug_log( 'Check for new version: ' . ( yourls_maybe_check_core_version() ? 'yes' : 'no' ) );
894         
895         $checks = yourls_get_option( 'core_version_checks' );
896         
897         if( isset( $checks->last_result->latest ) AND version_compare( $checks->last_result->latest, YOURLS_VERSION, '>' ) ) {
898                 $msg = yourls_s( '<a href="%s">YOURLS version %s</a> is available. Please update!', 'http://yourls.org/download', $checks->last_result->latest );
899                 yourls_add_notice( $msg );
900         }
901 }
902
903 /**
904  * Send a filerable content type header
905  *
906  * @since 1.7
907  * @param string $type content type ('text/html', 'application/json', ...)
908  * @return bool whether header was sent
909  */
910 function yourls_content_type_header( $type ) {
911     yourls_do_action( 'content_type_header', $type );
912         if( !headers_sent() ) {
913                 $charset = yourls_apply_filter( 'content_type_header_charset', 'utf-8' );
914                 header( "Content-Type: $type; charset=$charset" );
915                 return true;
916         }
917         return false;
918 }
919
920 /**
921  * Get search text from query string variables search_protocol, search_slashes and search
922  *
923  * Some servers don't like query strings containing "(ht|f)tp(s)://". A javascript bit
924  * explodes the search text into protocol, slashes and the rest (see JS function
925  * split_search_text_before_search()) and this function glues pieces back together
926  * See issue https://github.com/YOURLS/YOURLS/issues/1576
927  *
928  * @since 1.7
929  * @return string Search string
930  */
931 function yourls_get_search_text() {
932         $search = '';
933         if( isset( $_GET['search_protocol'] ) )
934                 $search .= $_GET['search_protocol'];
935         if( isset( $_GET['search_slashes'] ) )
936                 $search .= $_GET['search_slashes'];
937         if( isset( $_GET['search'] ) )
938                 $search .= $_GET['search'];
939         
940         return htmlspecialchars( trim( $search ) );
941 }
942
943 /**
944  * Display or return HTML for a bookmarklet link
945  *
946  * @since 1.7.1
947  * @param string $href    bookmarklet link (presumably minified code with "javascript:" scheme)
948  * @param string $anchor  link anchor
949  * @param bool   $echo    true to display, false to return the HTML
950  * @return string         the HTML for a bookmarklet link
951  */
952 function yourls_bookmarklet_link( $href, $anchor, $echo = true ) {
953     $alert = yourls_esc_attr__( 'Drag to your toolbar!' );
954     $link = <<<LINK
955     <a href="$href" class="bookmarklet" onclick="alert('$alert');return false;">$anchor</a>
956 LINK;
957     
958     if( $echo )
959         echo $link;
960     return $link;
961 }
962