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