]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-html.php
Go to First Page -> Go to Last Page
[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 Last 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                                 <?php
410                                 yourls_do_action( 'share_links', $longurl, $shorturl, $title, $text );
411                                 // Note: on the main admin page, there are no parameters passed to the sharebox when it's drawn.
412                                 ?>
413                         </p>
414                 </div>
415                 
416                 <?php yourls_do_action( 'shareboxes_after', $longurl, $shorturl, $title, $text ); ?>
417         
418         </div>
419         
420         <?php
421 }
422
423 /**
424  * Die die die
425  *
426  */
427 function yourls_die( $message = '', $title = '', $header_code = 200 ) {
428     yourls_do_action( 'pre_yourls_die', $message, $title, $header_code );
429
430         yourls_status_header( $header_code );
431         
432         if( !yourls_did_action( 'html_head' ) ) {
433                 yourls_html_head();
434                 yourls_html_logo();
435         }
436         echo yourls_apply_filter( 'die_title', "<h2>$title</h2>" );
437         echo yourls_apply_filter( 'die_message', "<p>$message</p>" );
438     // Hook into 'yourls_die' to add more elements or messages to that page
439         yourls_do_action( 'yourls_die' );
440         if( !yourls_did_action( 'html_footer' ) ) {
441                 yourls_html_footer();
442         }
443         die();
444 }
445
446 /**
447  * Return an "Edit" row for the main table
448  *
449  * @param string $keyword Keyword to edit
450  * @return string HTML of the edit row
451  */
452 function yourls_table_edit_row( $keyword ) {
453         $keyword = yourls_sanitize_string( $keyword );
454         $id = yourls_string2htmlid( $keyword ); // used as HTML #id
455         $url = yourls_get_keyword_longurl( $keyword );
456         $title = htmlspecialchars( yourls_get_keyword_title( $keyword ) );
457         $safe_url = yourls_esc_attr( rawurldecode( $url ) );
458         $safe_title = yourls_esc_attr( $title );
459     
460     // Make strings sprintf() safe: '%' -> '%%'
461     $safe_url = str_replace( '%', '%%', $safe_url );
462     $safe_title = str_replace( '%', '%%', $safe_title );
463
464         $www = yourls_link();
465     
466         $nonce = yourls_create_nonce( 'edit-save_'.$id );
467         
468         if( $url ) {
469                 $return = <<<RETURN
470 <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>
471 RETURN;
472                 $return = sprintf( $return, yourls__( 'Long URL' ), yourls__( 'Short URL' ), yourls__( 'Title' ), yourls__( 'Save' ), yourls__( 'Save new values' ), yourls__( 'Cancel' ), yourls__( 'Cancel editing' ) );
473         } else {
474                 $return = '<tr class="edit-row notfound"><td colspan="6" class="edit-row notfound">' . yourls__( 'Error, URL not found' ) . '</td></tr>';
475         }
476         
477         $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title );
478
479         return $return;
480 }
481
482 /**
483  * Return an "Add" row for the main table
484  *
485  * @return string HTML of the edit row
486  */
487 function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) {
488         $keyword  = yourls_sanitize_string( $keyword );
489         $id       = yourls_string2htmlid( $keyword ); // used as HTML #id
490         $shorturl = yourls_link( $keyword );
491
492         $statlink = yourls_statlink( $keyword );
493                 
494         $delete_link = yourls_nonce_url( 'delete-link_'.$id,
495                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'delete', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
496         );
497         
498         $edit_link = yourls_nonce_url( 'edit-link_'.$id,
499                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
500         );
501         
502         // Action link buttons: the array
503         $actions = array(
504                 'stats' => array(
505                         'href'    => $statlink,
506                         'id'      => "statlink-$id",
507                         'title'   => yourls_esc_attr__( 'Stats' ),
508                         'anchor'  => yourls__( 'Stats' ),
509                 ),
510                 'share' => array(
511                         'href'    => '',
512                         'id'      => "share-button-$id",
513                         'title'   => yourls_esc_attr__( 'Share' ),
514                         'anchor'  => yourls__( 'Share' ),
515                         'onclick' => "toggle_share('$id');return false;",
516                 ),
517                 'edit' => array(
518                         'href'    => $edit_link,
519                         'id'      => "edit-button-$id",
520                         'title'   => yourls_esc_attr__( 'Edit' ),
521                         'anchor'  => yourls__( 'Edit' ),
522                         'onclick' => "edit_link_display('$id');return false;",
523                 ),
524                 'delete' => array(
525                         'href'    => $delete_link,
526                         'id'      => "delete-button-$id",
527                         'title'   => yourls_esc_attr__( 'Delete' ),
528                         'anchor'  => yourls__( 'Delete' ),
529                         'onclick' => "remove_link('$id');return false;",
530                 )
531         );
532         $actions = yourls_apply_filter( 'table_add_row_action_array', $actions );
533         
534         // Action link buttons: the HTML
535         $action_links = '';
536         foreach( $actions as $key => $action ) {
537                 $onclick = isset( $action['onclick'] ) ? 'onclick="' . $action['onclick'] . '"' : '' ;
538                 $action_links .= sprintf( '<a href="%s" id="%s" title="%s" class="%s" %s>%s</a>',
539                         $action['href'], $action['id'], $action['title'], 'button button_'.$key, $onclick, $action['anchor']
540                 );
541         }
542         $action_links = yourls_apply_filter( 'action_links', $action_links, $keyword, $url, $ip, $clicks, $timestamp );
543
544         if( ! $title )
545                 $title = $url;
546
547         $protocol_warning = '';
548         if( ! in_array( yourls_get_protocol( $url ) , array( 'http://', 'https://' ) ) )
549                 $protocol_warning = yourls_apply_filter( 'add_row_protocol_warning', '<span class="warning" title="' . yourls__( 'Not a common link' ) . '">&#9733;</span>' );
550
551         // Row cells: the array
552         $cells = array(
553                 'keyword' => array(
554                         'template'      => '<a href="%shorturl%">%keyword_html%</a>',
555                         'shorturl'      => yourls_esc_url( $shorturl ),
556                         'keyword_html'  => yourls_esc_html( $keyword ),
557                 ),
558                 'url' => array(
559                         'template'      => '<a href="%long_url%" title="%title_attr%">%title_html%</a><br/><small>%warning%<a href="%long_url%">%long_url_html%</a></small>',
560                         'long_url'      => yourls_esc_url( $url ),
561                         'title_attr'    => yourls_esc_attr( $title ),
562                         'title_html'    => yourls_esc_html( yourls_trim_long_string( $title ) ),
563                         'long_url_html' => yourls_esc_html( yourls_trim_long_string( $url ) ),
564                         'warning'       => $protocol_warning,
565                 ),
566                 'timestamp' => array(
567                         'template' => '%date%',
568                         'date'     => date( 'M d, Y H:i', $timestamp +( YOURLS_HOURS_OFFSET * 3600 ) ),
569                 ),
570                 'ip' => array(
571                         'template' => '%ip%',
572                         'ip'       => $ip,
573                 ),
574                 'clicks' => array(
575                         'template' => '%clicks%',
576                         'clicks'   => yourls_number_format_i18n( $clicks, 0, '', '' ),
577                 ),
578                 'actions' => array(
579                         'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',
580                         'actions'  => $action_links,
581                         'id'       => $id,
582                         'keyword'  => $keyword,
583                 ),
584         );
585         $cells = yourls_apply_filter( 'table_add_row_cell_array', $cells, $keyword, $url, $title, $ip, $clicks, $timestamp );
586         
587         // Row cells: the HTML. Replace every %stuff% in 'template' with 'stuff' value.
588         $row = "<tr id=\"id-$id\">";
589         foreach( $cells as $cell_id => $elements ) {
590                 $callback = new yourls_table_add_row_callback( $elements );
591                 $row .= sprintf( '<td class="%s" id="%s">', $cell_id, $cell_id . '-' . $id );
592                 $row .= preg_replace_callback( '/%([^%]+)?%/', array( $callback, 'callback' ), $elements['template'] );
593                 // For the record, in PHP 5.3+ we don't need to introduce a class in order to pass additional parameters
594                 // to the callback function. Instead, we would have used the 'use' keyword :
595                 // $row .= preg_replace_callback( '/%([^%]+)?%/', function( $match ) use ( $elements ) { return $elements[ $match[1] ]; }, $elements['template'] );
596                 
597                 $row .= '</td>';
598         }
599         $row .= "</tr>";
600         $row  = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $title, $ip, $clicks, $timestamp );
601         
602         return $row;
603 }
604
605 /**
606  * Callback class for yourls_table_add_row
607  *
608  * See comment about PHP 5.3+ in yourls_table_add_row()
609  *
610  * @since 1.7
611  */
612 class yourls_table_add_row_callback {
613     private $elements;
614         
615     function __construct($elements) {
616                 $this->elements = $elements;
617         }
618         
619     function callback( $matches ) {
620                 return $this->elements[ $matches[1] ];
621     }
622 }
623
624
625 /**
626  * Echo the main table head
627  *
628  */
629 function yourls_table_head() {
630         $start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";
631         echo yourls_apply_filter( 'table_head_start', $start );
632         
633         $cells = yourls_apply_filter( 'table_head_cells', array(
634                 'shorturl' => yourls__( 'Short URL' ),
635                 'longurl'  => yourls__( 'Original URL' ),
636                 'date'     => yourls__( 'Date' ),
637                 'ip'       => yourls__( 'IP' ),
638                 'clicks'   => yourls__( 'Clicks' ),
639                 'actions'  => yourls__( 'Actions' )
640         ) );
641         foreach( $cells as $k => $v ) {
642                 echo "<th id='main_table_head_$k'>$v</th>\n";
643         }
644         
645         $end = "</tr></thead>\n";
646         echo yourls_apply_filter( 'table_head_end', $end );
647 }
648
649 /**
650  * Echo the tbody start tag
651  *
652  */
653 function yourls_table_tbody_start() {
654         echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );
655 }
656
657 /**
658  * Echo the tbody end tag
659  *
660  */
661 function yourls_table_tbody_end() {
662         echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );
663 }
664
665 /**
666  * Echo the table start tag
667  *
668  */
669 function yourls_table_end() {
670         echo yourls_apply_filter( 'table_end', '</table></main>' );
671 }
672
673 /**
674  * Echo HTML tag for a link
675  *
676  */
677 function yourls_html_link( $href, $title = '', $element = '' ) {
678         if( !$title )
679                 $title = $href;
680         if( $element )
681                 $element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );
682         $link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );
683         echo yourls_apply_filter( 'html_link', $link );
684 }
685
686 /**
687  * Display the login screen. Nothing past this point.
688  *
689  */
690 function yourls_login_screen( $error_msg = '' ) {
691         yourls_html_head( 'login' );
692         
693         $action = ( isset( $_GET['action'] ) && $_GET['action'] == 'logout' ? '?' : '' );
694
695         yourls_html_logo();
696         ?>
697         <div id="login">
698                 <form method="post" action="<?php echo $action; ?>"> <?php // reset any QUERY parameters ?>
699                         <?php
700                                 if( !empty( $error_msg ) ) {
701                                         echo '<p class="error">'.$error_msg.'</p>';
702                                 }
703                         ?>
704                         <p>
705                                 <label for="username"><?php yourls_e( 'Username' ); ?></label><br />
706                                 <input type="text" id="username" name="username" size="30" class="text" />
707                         </p>
708                         <p>
709                                 <label for="password"><?php yourls_e( 'Password' ); ?></label><br />
710                                 <input type="password" id="password" name="password" size="30" class="text" />
711                         </p>
712                         <p style="text-align: right;">
713                                 <input type="submit" id="submit" name="submit" value="<?php yourls_e( 'Login' ); ?>" class="button" />
714                         </p>
715                 </form>
716                 <script type="text/javascript">$('#username').focus();</script>
717         </div>
718         <?php
719         yourls_html_footer();
720         die();
721 }
722
723 /**
724  * Display the admin menu
725  *
726  */
727 function yourls_html_menu() {
728
729         // Build menu links
730         if( defined( 'YOURLS_USER' ) ) {
731                 $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>)' );
732         } else {
733                 $logout_link = yourls_apply_filter( 'logout_link', '' );
734         }
735         $help_link   = yourls_apply_filter( 'help_link',   '<a href="' . yourls_site_url( false ) .'/readme.html">' . yourls__( 'Help' ) . '</a>' );
736         
737         $admin_links    = array();
738         $admin_sublinks = array();
739         
740         $admin_links['admin'] = array(
741                 'url'    => yourls_admin_url( 'index.php' ),
742                 'title'  => yourls__( 'Go to the admin interface' ),
743                 'anchor' => yourls__( 'Admin interface' )
744         );
745         
746         if( yourls_is_admin() ) {
747                 $admin_links['tools'] = array(
748                         'url'    => yourls_admin_url( 'tools.php' ),
749                         'anchor' => yourls__( 'Tools' )
750                 );
751                 $admin_links['plugins'] = array(
752                         'url'    => yourls_admin_url( 'plugins.php' ),
753                         'anchor' => yourls__( 'Manage Plugins' )
754                 );
755                 $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages();
756         }
757         
758         $admin_links    = yourls_apply_filter( 'admin_links',    $admin_links );
759         $admin_sublinks = yourls_apply_filter( 'admin_sublinks', $admin_sublinks );
760         
761         // Now output menu
762         echo '<nav role="navigation"><ul id="admin_menu">'."\n";
763         if ( yourls_is_private() && !empty( $logout_link ) )
764                 echo '<li id="admin_menu_logout_link">' . $logout_link .'</li>';
765
766         foreach( (array)$admin_links as $link => $ar ) {
767                 if( isset( $ar['url'] ) ) {
768                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
769                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
770                         printf( '<li id="admin_menu_%s_link" class="admin_menu_toplevel"><a href="%s" %s>%s</a>', $link, $ar['url'], $title, $anchor );
771                 }
772                 // Output submenu if any. TODO: clean up, too many code duplicated here
773                 if( isset( $admin_sublinks[$link] ) ) {
774                         echo "<ul>\n";
775                         foreach( $admin_sublinks[$link] as $link => $ar ) {
776                                 if( isset( $ar['url'] ) ) {
777                                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
778                                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
779                                         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 );
780                                 }
781                         }
782                         echo "</ul>\n";
783                 }
784         }
785         
786         if ( isset( $help_link ) )
787                 echo '<li id="admin_menu_help_link">' . $help_link .'</li>';
788                 
789         yourls_do_action( 'admin_menu' );
790         echo "</ul></nav>\n";
791         yourls_do_action( 'admin_notices' );
792         yourls_do_action( 'admin_notice' ); // because I never remember if it's 'notices' or 'notice'
793         /*
794         To display a notice:
795         $message = "<div>OMG, dude, I mean!</div>" );
796         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
797         */
798 }
799
800 /**
801  * Wrapper function to display admin notices
802  *
803  */
804 function yourls_add_notice( $message, $style = 'notice' ) {
805         // Escape single quotes in $message to avoid breaking the anonymous function
806         $message = yourls_notice_box( strtr( $message, array( "'" => "\'" ) ), $style );
807         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
808 }
809
810 /**
811  * Return a formatted notice
812  *
813  */
814 function yourls_notice_box( $message, $style = 'notice' ) {
815         return <<<HTML
816         <div class="$style">
817         <p>$message</p>
818         </div>
819 HTML;
820 }
821
822 /**
823  * Display a page
824  *
825  */
826 function yourls_page( $page ) {
827         $include = YOURLS_ABSPATH . "/pages/$page.php";
828         if( !file_exists( $include ) ) {
829                 yourls_die( "Page '$page' not found", 'Not found', 404 );
830         }
831         yourls_do_action( 'pre_page', $page );
832         include_once( $include );
833         yourls_do_action( 'post_page', $page );
834         die();  
835 }
836
837 /**
838  * Display the language attributes for the HTML tag.
839  *
840  * Builds up a set of html attributes containing the text direction and language
841  * information for the page. Stolen from WP.
842  *
843  * @since 1.6
844  */
845 function yourls_html_language_attributes() {
846         $attributes = array();
847         $output = '';
848         
849         $attributes[] = ( yourls_is_rtl() ? 'dir="rtl"' : 'dir="ltr"' );
850         
851         $doctype = yourls_apply_filter( 'html_language_attributes_doctype', 'html' );
852         // Experimental: get HTML lang from locale. Should work. Convert fr_FR -> fr-FR
853         if ( $lang = str_replace( '_', '-', yourls_get_locale() ) ) {
854                 if( $doctype == 'xhtml' ) {
855                         $attributes[] = "xml:lang=\"$lang\"";
856                 } else {
857                         $attributes[] = "lang=\"$lang\"";
858                 }
859         }
860
861         $output = implode( ' ', $attributes );
862         $output = yourls_apply_filter( 'html_language_attributes', $output );
863         echo $output;
864 }
865
866 /**
867  * Output translated strings used by the Javascript calendar
868  *
869  * @since 1.6
870  */
871 function yourls_l10n_calendar_strings() {
872         echo "\n<script>\n";
873         echo "var l10n_cal_month = " . json_encode( array_values( yourls_l10n_months() ) ) . ";\n";
874         echo "var l10n_cal_days = " . json_encode( array_values( yourls_l10n_weekday_initial() ) ) . ";\n";
875         echo "var l10n_cal_today = \"" . yourls_esc_js( yourls__( 'Today' ) ) . "\";\n";
876         echo "var l10n_cal_close = \"" . yourls_esc_js( yourls__( 'Close' ) ) . "\";\n";
877         echo "</script>\n";
878         
879         // Dummy returns, to initialize l10n strings used in the calendar
880         yourls__( 'Today' );
881         yourls__( 'Close' );
882 }
883
884
885 /**
886  * Display a notice if there is a newer version of YOURLS available
887  *
888  * @since 1.7
889  */
890 function yourls_new_core_version_notice() {
891
892         yourls_debug_log( 'Check for new version: ' . ( yourls_maybe_check_core_version() ? 'yes' : 'no' ) );
893         
894         $checks = yourls_get_option( 'core_version_checks' );
895         
896         if( isset( $checks->last_result->latest ) AND version_compare( $checks->last_result->latest, YOURLS_VERSION, '>' ) ) {
897                 $msg = yourls_s( '<a href="%s">YOURLS version %s</a> is available. Please update!', 'http://yourls.org/download', $checks->last_result->latest );
898                 yourls_add_notice( $msg );
899         }
900 }
901
902 /**
903  * Send a filerable content type header
904  *
905  * @since 1.7
906  * @param string $type content type ('text/html', 'application/json', ...)
907  * @return bool whether header was sent
908  */
909 function yourls_content_type_header( $type ) {
910     yourls_do_action( 'content_type_header', $type );
911         if( !headers_sent() ) {
912                 $charset = yourls_apply_filter( 'content_type_header_charset', 'utf-8' );
913                 header( "Content-Type: $type; charset=$charset" );
914                 return true;
915         }
916         return false;
917 }
918
919 /**
920  * Get search text from query string variables search_protocol, search_slashes and search
921  *
922  * Some servers don't like query strings containing "(ht|f)tp(s)://". A javascript bit
923  * explodes the search text into protocol, slashes and the rest (see JS function
924  * split_search_text_before_search()) and this function glues pieces back together
925  * See issue https://github.com/YOURLS/YOURLS/issues/1576
926  *
927  * @since 1.7
928  * @return string Search string
929  */
930 function yourls_get_search_text() {
931         $search = '';
932         if( isset( $_GET['search_protocol'] ) )
933                 $search .= $_GET['search_protocol'];
934         if( isset( $_GET['search_slashes'] ) )
935                 $search .= $_GET['search_slashes'];
936         if( isset( $_GET['search'] ) )
937                 $search .= $_GET['search'];
938         
939         return htmlspecialchars( trim( $search ) );
940 }
941
942 /**
943  * Display or return HTML for a bookmarklet link
944  *
945  * @since 1.7.1
946  * @param string $href    bookmarklet link (presumably minified code with "javascript:" scheme)
947  * @param string $anchor  link anchor
948  * @param bool   $echo    true to display, false to return the HTML
949  * @return string         the HTML for a bookmarklet link
950  */
951 function yourls_bookmarklet_link( $href, $anchor, $echo = true ) {
952     $alert = yourls_esc_attr__( 'Drag to your toolbar!' );
953     $link = <<<LINK
954     <a href="$href" class="bookmarklet" onclick="alert('$alert');return false;">$anchor</a>
955 LINK;
956     
957     if( $echo )
958         echo $link;
959     return $link;
960 }
961