4 * Display <h1> header and logo
7 function yourls_html_logo() {
8 yourls_do_action( 'pre_html_logo' );
10 <header role="banner">
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>
17 yourls_do_action( 'html_logo' );
21 * Display HTML head and <body> tag
23 * @param string $context Context of the page (stats, index, infos, ...)
24 * @param string $title HTML title of the page
26 function yourls_html_head( $context = 'index', $title = '' ) {
28 yourls_do_action( 'pre_html_head', $context, $title );
30 // All components to false, except when specified true
31 $share = $insert = $tablesorter = $tabs = $cal = $charts = false;
33 // Load components as needed
36 $share = $tabs = $charts = true;
40 $share = $insert = $tablesorter = true;
44 $insert = $tablesorter = $cal = $share = true;
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 );
69 // Store page context in global object
71 $ydb->context = $context;
74 $bodyclass = yourls_apply_filter( 'bodyclass', '' );
75 $bodyclass .= ( yourls_is_mobile_device() ? 'mobile' : 'desktop' );
78 $_title = 'YOURLS — Your Own URL Shortener | ' . yourls_link();
79 $title = $title ? $title . " « " . $_title : $_title;
80 $title = yourls_apply_filter( 'html_title', $title, $context );
84 <html <?php yourls_html_language_attributes(); ?>>
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 » Your Own URL Shortener' | <?php yourls_site_url(); ?>" />
91 <meta name="referrer" content="always" />
92 <script src="<?php yourls_site_url(); ?>/js/jquery-2.2.4.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>
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>
104 <?php if ( $insert ) { ?>
105 <script src="<?php yourls_site_url(); ?>/js/insert.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
107 <?php if ( $share ) { ?>
108 <link rel="stylesheet" href="<?php yourls_site_url(); ?>/css/share.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
109 <script src="<?php yourls_site_url(); ?>/js/share.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
110 <script src="<?php yourls_site_url(); ?>/js/clipboard.min.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
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>
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']});
123 <script type="text/javascript">
125 var ajaxurl = '<?php echo yourls_admin_url( 'admin-ajax.php' ); ?>';
128 <?php yourls_do_action( 'html_head', $context ); ?>
130 <body class="<?php echo $context; ?> <?php echo $bodyclass; ?>">
136 * Display HTML footer (including closing body & html tags)
139 function yourls_html_footer() {
142 $num_queries = sprintf( yourls_n( '1 query', '%s queries', $ydb->num_queries ), $ydb->num_queries );
144 </div><?php // wrap ?>
145 <footer id="footer" role="contentinfo"><p>
147 $footer = yourls_s( 'Powered by %s', '<a href="http://yourls.org/" title="YOURLS">YOURLS</a> v ' . YOURLS_VERSION );
148 $footer .= ' – '.$num_queries;
149 echo yourls_apply_filter( 'html_footer_text', $footer );
152 <?php if( defined( 'YOURLS_DEBUG' ) && YOURLS_DEBUG == true ) {
153 echo '<div style="text-align:left"><pre>';
154 echo join( "\n", $ydb->debug_log );
157 <?php yourls_do_action( 'html_footer', $ydb->context ); ?>
164 * Display "Add new URL" box
166 * @param string $url URL to prefill the input with
167 * @param string $keyword Keyword to prefill the input with
169 function yourls_html_addnew( $url = '', $keyword = '' ) {
174 <form id="new_url_form" action="" method="get">
175 <div><strong><?php yourls_e( 'Enter the URL' ); ?></strong>:<input type="text" id="add-url" name="url" value="<?php echo $url; ?>" class="text" size="80" placeholder="http://" />
176 <?php yourls_e( 'Optional '); ?> : <strong><?php yourls_e('Custom short URL'); ?></strong>:<input type="text" id="add-keyword" name="keyword" value="<?php echo $keyword; ?>" class="text" size="8" />
177 <?php yourls_nonce_field( 'add_url', 'nonce-add' ); ?>
178 <input type="button" id="add-button" name="add-button" value="<?php yourls_e( 'Shorten The URL' ); ?>" class="button" onclick="add_link();" /></div>
180 <div id="feedback" style="display:none"></div>
182 <?php yourls_do_action( 'html_addnew' ); ?>
188 * Display main table's footer
190 * The $param array is defined in /admin/index.php, check the yourls_html_tfooter() call
192 * @param array $params Array of all required parameters
193 * @return string Result
195 function yourls_html_tfooter( $params = array() ) {
196 // Manually extract all parameters from the array. We prefer doing it this way, over using extract(),
197 // to make things clearer and more explicit about what var is used.
198 $search = $params['search'];
199 $search_text = $params['search_text'];
200 $search_in = $params['search_in'];
201 $sort_by = $params['sort_by'];
202 $sort_order = $params['sort_order'];
203 $page = $params['page'];
204 $perpage = $params['perpage'];
205 $click_filter = $params['click_filter'];
206 $click_limit = $params['click_limit'];
207 $total_pages = $params['total_pages'];
208 $date_filter = $params['date_filter'];
209 $date_first = $params['date_first'];
210 $date_second = $params['date_second'];
216 <div id="filter_form">
217 <form action="" method="get">
218 <div id="filter_options">
221 // First search control: text to search
222 $_input = '<input type="text" name="search" class="text" size="12" value="' . yourls_esc_attr( $search_text ) . '" />';
224 'all' => yourls__( 'All fields' ),
225 'keyword' => yourls__( 'Short URL' ),
226 'url' => yourls__( 'URL' ),
227 'title' => yourls__( 'Title' ),
228 'ip' => yourls__( 'IP' ),
230 $_select = yourls_html_select( 'search_in', $_options, $search_in );
231 /* //translators: "Search for <input field with text to search> in <select dropdown with URL, title...>" */
232 yourls_se( 'Search for %1$s in %2$s', $_input , $_select );
235 // Second search control: order by
237 'keyword' => yourls__( 'Short URL' ),
238 'url' => yourls__( 'URL' ),
239 'timestamp' => yourls__( 'Date' ),
240 'ip' => yourls__( 'IP' ),
241 'clicks' => yourls__( 'Clicks' ),
243 $_select = yourls_html_select( 'sort_by', $_options, $sort_by );
244 $sort_order = isset( $sort_order ) ? $sort_order : 'desc' ;
246 'asc' => yourls__( 'Ascending' ),
247 'desc' => yourls__( 'Descending' ),
249 $_select2 = yourls_html_select( 'sort_order', $_options, $sort_order );
250 /* //translators: "Order by <criteria dropdown (date, clicks...)> in <order dropdown (Descending or Ascending)>" */
251 yourls_se( 'Order by %1$s %2$s', $_select , $_select2 );
254 // Third search control: Show XX rows
255 /* //translators: "Show <text field> rows" */
256 yourls_se( 'Show %s rows', '<input type="text" name="perpage" class="text" size="2" value="' . $perpage . '" />' );
259 // Fourth search control: Show links with more than XX clicks
261 'more' => yourls__( 'more' ),
262 'less' => yourls__( 'less' ),
264 $_select = yourls_html_select( 'click_filter', $_options, $click_filter );
265 $_input = '<input type="text" name="click_limit" class="text" size="4" value="' . $click_limit . '" /> ';
266 /* //translators: "Show links with <more/less> than <text field> clicks" */
267 yourls_se( 'Show links with %1$s than %2$s clicks', $_select, $_input );
270 // Fifth search control: Show links created before/after/between ...
272 'before' => yourls__('before'),
273 'after' => yourls__('after'),
274 'between' => yourls__('between'),
276 $_select = yourls_html_select( 'date_filter', $_options, $date_filter );
277 $_input = '<input type="text" name="date_first" id="date_first" class="text" size="12" value="' . $date_first . '" />';
278 $_and = '<span id="date_and"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '> & </span>';
279 $_input2 = '<input type="text" name="date_second" id="date_second" class="text" size="12" value="' . $date_second . '"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '/>';
280 /* //translators: "Show links created <before/after/between> <date input> <"and" if applicable> <date input if applicable>" */
281 yourls_se( 'Show links created %1$s %2$s %3$s %4$s', $_select, $_input, $_and, $_input2 );
284 <div id="filter_buttons">
285 <input type="submit" id="submit-sort" value="<?php yourls_e('Search'); ?>" class="button primary" />
287 <input type="button" id="submit-clear-filter" value="<?php yourls_e('Clear'); ?>" class="button" onclick="window.parent.location.href = 'index.php'" />
295 // Remove empty keys from the $params array so it doesn't clutter the pagination links
296 $params = array_filter( $params, 'yourls_return_if_not_empty_string' ); // remove empty keys
298 if( isset( $search_text ) ) {
299 $params['search'] = $search_text;
300 unset( $params['search_text'] );
304 <div id="pagination">
305 <span class="navigation">
306 <?php if( $total_pages > 1 ) { ?>
307 <span class="nav_total"><?php echo sprintf( yourls_n( '1 page', '%s pages', $total_pages ), $total_pages ); ?></span>
309 $base_page = yourls_admin_url( 'index.php' );
310 // Pagination offsets: min( max ( zomg! ) );
311 $p_start = max( min( $total_pages - 4, $page - 2 ), 1 );
312 $p_end = min( max( 5, $page + 2 ), $total_pages );
313 if( $p_start >= 2 ) {
314 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => 1 ) ), $base_page );
315 echo '<span class="nav_link nav_first"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( '« First' ) . '</a></span>';
316 echo '<span class="nav_link nav_prev"></span>';
318 for( $i = $p_start ; $i <= $p_end; $i++ ) {
320 echo "<span class='nav_link nav_current'>$i</span>";
322 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $i ) ), $base_page );
323 echo '<span class="nav_link nav_goto"><a href="' . $link . '" title="' . sprintf( yourls_esc_attr( 'Page %s' ), $i ) .'">'.$i.'</a></span>';
326 if( ( $p_end ) < $total_pages ) {
327 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $total_pages ) ), $base_page );
328 echo '<span class="nav_link nav_next"></span>';
329 echo '<span class="nav_link nav_last"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to Last Page') . '">' . yourls__( 'Last »' ) . '</a></span>';
337 <?php yourls_do_action( 'html_tfooter' ); ?>
343 * Return a select box
347 * @param string $name HTML 'name' (also use as the HTML 'id')
348 * @param array $options array of 'value' => 'Text displayed'
349 * @param string $selected optional 'value' from the $options array that will be highlighted
350 * @param boolean $display false (default) to return, true to echo
351 * @return string HTML content of the select element
353 function yourls_html_select( $name, $options, $selected = '', $display = false ) {
354 $html = "<select name='$name' id='$name' size='1'>\n";
355 foreach( $options as $value => $text ) {
356 $html .= "<option value='$value' ";
357 $html .= $selected == $value ? ' selected="selected"' : '';
358 $html .= ">$text</option>\n";
360 $html .= "</select>\n";
361 $html = yourls_apply_filter( 'html_select', $html, $name, $options, $selected, $display );
368 * Display the Quick Share box
371 function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlink_title = '', $share_title = '', $hidden = false ) {
372 if ( $shortlink_title == '' )
373 $shortlink_title = '<h2>' . yourls__( 'Your short link' ) . '</h2>';
374 if ( $share_title == '' )
375 $share_title = '<h2>' . yourls__( 'Quick Share' ) . '</h2>';
377 // Allow plugins to short-circuit the whole function
378 $pre = yourls_apply_filter( 'shunt_share_box', false );
379 if ( false !== $pre )
382 $text = ( $text ? '"'.$text.'" ' : '' );
383 $title = ( $title ? "$title " : '' );
384 $share = yourls_esc_textarea( $title.$text.$shorturl );
385 $count = 140 - strlen( $share );
386 $hidden = ( $hidden ? 'style="display:none;"' : '' );
388 // Allow plugins to filter all data
389 $data = compact( 'longurl', 'shorturl', 'title', 'text', 'shortlink_title', 'share_title', 'share', 'count', 'hidden' );
390 $data = yourls_apply_filter( 'share_box_data', $data );
393 $_share = rawurlencode( $share );
394 $_url = rawurlencode( $shorturl );
397 <div id="shareboxes" <?php echo $hidden; ?>>
399 <?php yourls_do_action( 'shareboxes_before', $longurl, $shorturl, $title, $text ); ?>
401 <div id="copybox" class="share">
402 <?php echo $shortlink_title; ?>
403 <p><input id="copylink" class="text" size="32" value="<?php echo yourls_esc_url( $shorturl ); ?>" /></p>
404 <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>
405 <?php if( yourls_do_log_redirect() ) { ?>
406 <br/><small><?php yourls_e( 'Stats' ); ?>: <a id="statlink" href="<?php echo yourls_esc_url( $shorturl ); ?>+"><?php echo yourls_esc_url( $shorturl ); ?>+</a></small>
407 <input type="hidden" id="titlelink" value="<?php echo yourls_esc_attr( $title ); ?>" />
412 <?php yourls_do_action( 'shareboxes_middle', $longurl, $shorturl, $title, $text ); ?>
414 <div id="sharebox" class="share">
415 <?php echo $share_title; ?>
417 <span id="charcount" class="hide-if-no-js"><?php echo $count; ?></span>
418 <textarea id="tweet_body"><?php echo $share; ?></textarea>
420 <p id="share_links"><?php yourls_e( 'Share with' ); ?>
421 <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>
422 <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>
424 yourls_do_action( 'share_links', $longurl, $shorturl, $title, $text );
425 // Note: on the main admin page, there are no parameters passed to the sharebox when it's drawn.
430 <?php yourls_do_action( 'shareboxes_after', $longurl, $shorturl, $title, $text ); ?>
441 function yourls_die( $message = '', $title = '', $header_code = 200 ) {
442 yourls_do_action( 'pre_yourls_die', $message, $title, $header_code );
444 yourls_status_header( $header_code );
446 if( !yourls_did_action( 'html_head' ) ) {
450 echo yourls_apply_filter( 'die_title', "<h2>$title</h2>" );
451 echo yourls_apply_filter( 'die_message', "<p>$message</p>" );
452 // Hook into 'yourls_die' to add more elements or messages to that page
453 yourls_do_action( 'yourls_die' );
454 if( !yourls_did_action( 'html_footer' ) ) {
455 yourls_html_footer();
461 * Return an "Edit" row for the main table
463 * @param string $keyword Keyword to edit
464 * @return string HTML of the edit row
466 function yourls_table_edit_row( $keyword ) {
467 $keyword = yourls_sanitize_string( $keyword );
468 $id = yourls_string2htmlid( $keyword ); // used as HTML #id
469 $url = yourls_get_keyword_longurl( $keyword );
470 $title = htmlspecialchars( yourls_get_keyword_title( $keyword ) );
471 $safe_url = yourls_esc_attr( rawurldecode( $url ) );
472 $safe_title = yourls_esc_attr( $title );
474 // Make strings sprintf() safe: '%' -> '%%'
475 $safe_url = str_replace( '%', '%%', $safe_url );
476 $safe_title = str_replace( '%', '%%', $safe_title );
478 $www = yourls_link();
480 $nonce = yourls_create_nonce( 'edit-save_'.$id );
484 <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');" /> <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>
486 $return = sprintf( $return, yourls__( 'Long URL' ), yourls__( 'Short URL' ), yourls__( 'Title' ), yourls__( 'Save' ), yourls__( 'Save new values' ), yourls__( 'Cancel' ), yourls__( 'Cancel editing' ) );
488 $return = '<tr class="edit-row notfound"><td colspan="6" class="edit-row notfound">' . yourls__( 'Error, URL not found' ) . '</td></tr>';
491 $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title );
497 * Return an "Add" row for the main table
499 * @return string HTML of the edit row
501 function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) {
502 $keyword = yourls_sanitize_string( $keyword );
503 $id = yourls_string2htmlid( $keyword ); // used as HTML #id
504 $shorturl = yourls_link( $keyword );
506 $statlink = yourls_statlink( $keyword );
508 $delete_link = yourls_nonce_url( 'delete-link_'.$id,
509 yourls_add_query_arg( array( 'id' => $id, 'action' => 'delete', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) )
512 $edit_link = yourls_nonce_url( 'edit-link_'.$id,
513 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) )
516 // Action link buttons: the array
520 'id' => "statlink-$id",
521 'title' => yourls_esc_attr__( 'Stats' ),
522 'anchor' => yourls__( 'Stats' ),
526 'id' => "share-button-$id",
527 'title' => yourls_esc_attr__( 'Share' ),
528 'anchor' => yourls__( 'Share' ),
529 'onclick' => "toggle_share('$id');return false;",
532 'href' => $edit_link,
533 'id' => "edit-button-$id",
534 'title' => yourls_esc_attr__( 'Edit' ),
535 'anchor' => yourls__( 'Edit' ),
536 'onclick' => "edit_link_display('$id');return false;",
539 'href' => $delete_link,
540 'id' => "delete-button-$id",
541 'title' => yourls_esc_attr__( 'Delete' ),
542 'anchor' => yourls__( 'Delete' ),
543 'onclick' => "remove_link('$id');return false;",
546 $actions = yourls_apply_filter( 'table_add_row_action_array', $actions );
548 // Action link buttons: the HTML
550 foreach( $actions as $key => $action ) {
551 $onclick = isset( $action['onclick'] ) ? 'onclick="' . $action['onclick'] . '"' : '' ;
552 $action_links .= sprintf( '<a href="%s" id="%s" title="%s" class="%s" %s>%s</a>',
553 $action['href'], $action['id'], $action['title'], 'button button_'.$key, $onclick, $action['anchor']
556 $action_links = yourls_apply_filter( 'action_links', $action_links, $keyword, $url, $ip, $clicks, $timestamp );
561 $protocol_warning = '';
562 if( ! in_array( yourls_get_protocol( $url ) , array( 'http://', 'https://' ) ) )
563 $protocol_warning = yourls_apply_filter( 'add_row_protocol_warning', '<span class="warning" title="' . yourls__( 'Not a common link' ) . '">★</span>' );
565 // Row cells: the array
568 'template' => '<a href="%shorturl%">%keyword_html%</a>',
569 'shorturl' => yourls_esc_url( $shorturl ),
570 'keyword_html' => yourls_esc_html( $keyword ),
573 'template' => '<a href="%long_url%" title="%title_attr%">%title_html%</a><br/><small>%warning%<a href="%long_url%">%long_url_html%</a></small>',
574 'long_url' => yourls_esc_url( $url ),
575 'title_attr' => yourls_esc_attr( $title ),
576 'title_html' => yourls_esc_html( yourls_trim_long_string( $title ) ),
577 'long_url_html' => yourls_esc_html( yourls_trim_long_string( $url ) ),
578 'warning' => $protocol_warning,
580 'timestamp' => array(
581 'template' => '%date%',
582 'date' => date( 'M d, Y H:i', $timestamp +( YOURLS_HOURS_OFFSET * 3600 ) ),
585 'template' => '%ip%',
589 'template' => '%clicks%',
590 'clicks' => yourls_number_format_i18n( $clicks, 0, '', '' ),
593 'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',
594 'actions' => $action_links,
596 'keyword' => $keyword,
599 $cells = yourls_apply_filter( 'table_add_row_cell_array', $cells, $keyword, $url, $title, $ip, $clicks, $timestamp );
601 // Row cells: the HTML. Replace every %stuff% in 'template' with 'stuff' value.
602 $row = "<tr id=\"id-$id\">";
603 foreach( $cells as $cell_id => $elements ) {
604 $callback = new yourls_table_add_row_callback( $elements );
605 $row .= sprintf( '<td class="%s" id="%s">', $cell_id, $cell_id . '-' . $id );
606 $row .= preg_replace_callback( '/%([^%]+)?%/', array( $callback, 'callback' ), $elements['template'] );
607 // For the record, in PHP 5.3+ we don't need to introduce a class in order to pass additional parameters
608 // to the callback function. Instead, we would have used the 'use' keyword :
609 // $row .= preg_replace_callback( '/%([^%]+)?%/', function( $match ) use ( $elements ) { return $elements[ $match[1] ]; }, $elements['template'] );
614 $row = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $title, $ip, $clicks, $timestamp );
620 * Callback class for yourls_table_add_row
622 * See comment about PHP 5.3+ in yourls_table_add_row()
626 class yourls_table_add_row_callback {
629 function __construct($elements) {
630 $this->elements = $elements;
633 function callback( $matches ) {
634 return $this->elements[ $matches[1] ];
640 * Echo the main table head
643 function yourls_table_head() {
644 $start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";
645 echo yourls_apply_filter( 'table_head_start', $start );
647 $cells = yourls_apply_filter( 'table_head_cells', array(
648 'shorturl' => yourls__( 'Short URL' ),
649 'longurl' => yourls__( 'Original URL' ),
650 'date' => yourls__( 'Date' ),
651 'ip' => yourls__( 'IP' ),
652 'clicks' => yourls__( 'Clicks' ),
653 'actions' => yourls__( 'Actions' )
655 foreach( $cells as $k => $v ) {
656 echo "<th id='main_table_head_$k'>$v</th>\n";
659 $end = "</tr></thead>\n";
660 echo yourls_apply_filter( 'table_head_end', $end );
664 * Echo the tbody start tag
667 function yourls_table_tbody_start() {
668 echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );
672 * Echo the tbody end tag
675 function yourls_table_tbody_end() {
676 echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );
680 * Echo the table start tag
683 function yourls_table_end() {
684 echo yourls_apply_filter( 'table_end', '</table></main>' );
688 * Echo HTML tag for a link
691 function yourls_html_link( $href, $title = '', $element = '' ) {
695 $element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );
696 $link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );
697 echo yourls_apply_filter( 'html_link', $link );
701 * Display the login screen. Nothing past this point.
704 function yourls_login_screen( $error_msg = '' ) {
705 yourls_html_head( 'login' );
707 $action = ( isset( $_GET['action'] ) && $_GET['action'] == 'logout' ? '?' : '' );
712 <form method="post" action="<?php echo $action; ?>"> <?php // reset any QUERY parameters ?>
714 if( !empty( $error_msg ) ) {
715 echo '<p class="error">'.$error_msg.'</p>';
719 <label for="username"><?php yourls_e( 'Username' ); ?></label><br />
720 <input type="text" id="username" name="username" size="30" class="text" />
723 <label for="password"><?php yourls_e( 'Password' ); ?></label><br />
724 <input type="password" id="password" name="password" size="30" class="text" />
726 <p style="text-align: right;">
727 <input type="submit" id="submit" name="submit" value="<?php yourls_e( 'Login' ); ?>" class="button" />
730 <script type="text/javascript">$('#username').focus();</script>
733 yourls_html_footer();
738 * Display the admin menu
741 function yourls_html_menu() {
744 if( defined( 'YOURLS_USER' ) ) {
745 $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>)' );
747 $logout_link = yourls_apply_filter( 'logout_link', '' );
749 $help_link = yourls_apply_filter( 'help_link', '<a href="' . yourls_site_url( false ) .'/readme.html">' . yourls__( 'Help' ) . '</a>' );
751 $admin_links = array();
752 $admin_sublinks = array();
754 $admin_links['admin'] = array(
755 'url' => yourls_admin_url( 'index.php' ),
756 'title' => yourls__( 'Go to the admin interface' ),
757 'anchor' => yourls__( 'Admin interface' )
760 if( yourls_is_admin() ) {
761 $admin_links['tools'] = array(
762 'url' => yourls_admin_url( 'tools.php' ),
763 'anchor' => yourls__( 'Tools' )
765 $admin_links['plugins'] = array(
766 'url' => yourls_admin_url( 'plugins.php' ),
767 'anchor' => yourls__( 'Manage Plugins' )
769 $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages();
772 $admin_links = yourls_apply_filter( 'admin_links', $admin_links );
773 $admin_sublinks = yourls_apply_filter( 'admin_sublinks', $admin_sublinks );
776 echo '<nav role="navigation"><ul id="admin_menu">'."\n";
777 if ( yourls_is_private() && !empty( $logout_link ) )
778 echo '<li id="admin_menu_logout_link">' . $logout_link .'</li>';
780 foreach( (array)$admin_links as $link => $ar ) {
781 if( isset( $ar['url'] ) ) {
782 $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
783 $title = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
784 printf( '<li id="admin_menu_%s_link" class="admin_menu_toplevel"><a href="%s" %s>%s</a>', $link, $ar['url'], $title, $anchor );
786 // Output submenu if any. TODO: clean up, too many code duplicated here
787 if( isset( $admin_sublinks[$link] ) ) {
789 foreach( $admin_sublinks[$link] as $link => $ar ) {
790 if( isset( $ar['url'] ) ) {
791 $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
792 $title = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
793 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 );
800 if ( isset( $help_link ) )
801 echo '<li id="admin_menu_help_link">' . $help_link .'</li>';
803 yourls_do_action( 'admin_menu' );
804 echo "</ul></nav>\n";
805 yourls_do_action( 'admin_notices' );
806 yourls_do_action( 'admin_notice' ); // because I never remember if it's 'notices' or 'notice'
809 $message = "<div>OMG, dude, I mean!</div>" );
810 yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
815 * Wrapper function to display admin notices
818 function yourls_add_notice( $message, $style = 'notice' ) {
819 // Escape single quotes in $message to avoid breaking the anonymous function
820 $message = yourls_notice_box( strtr( $message, array( "'" => "\'" ) ), $style );
821 yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
825 * Return a formatted notice
828 function yourls_notice_box( $message, $style = 'notice' ) {
840 function yourls_page( $page ) {
841 $include = YOURLS_ABSPATH . "/pages/$page.php";
842 if( !file_exists( $include ) ) {
843 yourls_die( "Page '$page' not found", 'Not found', 404 );
845 yourls_do_action( 'pre_page', $page );
846 include_once( $include );
847 yourls_do_action( 'post_page', $page );
852 * Display the language attributes for the HTML tag.
854 * Builds up a set of html attributes containing the text direction and language
855 * information for the page. Stolen from WP.
859 function yourls_html_language_attributes() {
860 $attributes = array();
863 $attributes[] = ( yourls_is_rtl() ? 'dir="rtl"' : 'dir="ltr"' );
865 $doctype = yourls_apply_filter( 'html_language_attributes_doctype', 'html' );
866 // Experimental: get HTML lang from locale. Should work. Convert fr_FR -> fr-FR
867 if ( $lang = str_replace( '_', '-', yourls_get_locale() ) ) {
868 if( $doctype == 'xhtml' ) {
869 $attributes[] = "xml:lang=\"$lang\"";
871 $attributes[] = "lang=\"$lang\"";
875 $output = implode( ' ', $attributes );
876 $output = yourls_apply_filter( 'html_language_attributes', $output );
881 * Output translated strings used by the Javascript calendar
885 function yourls_l10n_calendar_strings() {
887 echo "var l10n_cal_month = " . json_encode( array_values( yourls_l10n_months() ) ) . ";\n";
888 echo "var l10n_cal_days = " . json_encode( array_values( yourls_l10n_weekday_initial() ) ) . ";\n";
889 echo "var l10n_cal_today = \"" . yourls_esc_js( yourls__( 'Today' ) ) . "\";\n";
890 echo "var l10n_cal_close = \"" . yourls_esc_js( yourls__( 'Close' ) ) . "\";\n";
893 // Dummy returns, to initialize l10n strings used in the calendar
900 * Display a notice if there is a newer version of YOURLS available
904 function yourls_new_core_version_notice() {
906 yourls_debug_log( 'Check for new version: ' . ( yourls_maybe_check_core_version() ? 'yes' : 'no' ) );
908 $checks = yourls_get_option( 'core_version_checks' );
910 if( isset( $checks->last_result->latest ) AND version_compare( $checks->last_result->latest, YOURLS_VERSION, '>' ) ) {
911 $msg = yourls_s( '<a href="%s">YOURLS version %s</a> is available. Please update!', 'http://yourls.org/download', $checks->last_result->latest );
912 yourls_add_notice( $msg );
917 * Send a filerable content type header
920 * @param string $type content type ('text/html', 'application/json', ...)
921 * @return bool whether header was sent
923 function yourls_content_type_header( $type ) {
924 yourls_do_action( 'content_type_header', $type );
925 if( !headers_sent() ) {
926 $charset = yourls_apply_filter( 'content_type_header_charset', 'utf-8' );
927 header( "Content-Type: $type; charset=$charset" );
934 * Get search text from query string variables search_protocol, search_slashes and search
936 * Some servers don't like query strings containing "(ht|f)tp(s)://". A javascript bit
937 * explodes the search text into protocol, slashes and the rest (see JS function
938 * split_search_text_before_search()) and this function glues pieces back together
939 * See issue https://github.com/YOURLS/YOURLS/issues/1576
942 * @return string Search string
944 function yourls_get_search_text() {
946 if( isset( $_GET['search_protocol'] ) )
947 $search .= $_GET['search_protocol'];
948 if( isset( $_GET['search_slashes'] ) )
949 $search .= $_GET['search_slashes'];
950 if( isset( $_GET['search'] ) )
951 $search .= $_GET['search'];
953 return htmlspecialchars( trim( $search ) );
957 * Display or return HTML for a bookmarklet link
960 * @param string $href bookmarklet link (presumably minified code with "javascript:" scheme)
961 * @param string $anchor link anchor
962 * @param bool $echo true to display, false to return the HTML
963 * @return string the HTML for a bookmarklet link
965 function yourls_bookmarklet_link( $href, $anchor, $echo = true ) {
966 $alert = yourls_esc_attr__( 'Drag to your toolbar!' );
968 <a href="$href" class="bookmarklet" onclick="alert('$alert');return false;">$anchor</a>