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