]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-html.php
Fix end of line
[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_do_action( 'admin_headers', $context, $title );
64         }
65         
66         // Store page context in global object
67         global $ydb;
68         $ydb->context = $context;
69         
70         // Body class
71         $bodyclass = yourls_apply_filter( 'bodyclass', '' );
72         $bodyclass .= ( yourls_is_mobile_device() ? 'mobile' : 'desktop' );
73         
74         // Page title
75         $_title = 'YOURLS &mdash; Your Own URL Shortener | ' . yourls_link();
76         $title = $title ? $title . " &laquo; " . $_title : $_title;
77         $title = yourls_apply_filter( 'html_title', $title, $context );
78         
79         ?>
80 <!DOCTYPE html>
81 <html <?php yourls_html_language_attributes(); ?>>
82 <head>
83         <title><?php echo $title ?></title>
84         <link rel="shortcut icon" href="<?php yourls_favicon(); ?>" />
85         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
86         <meta http-equiv="X-UA-Compatible" content="IE-9"/>
87         <meta name="author" content="Ozh RICHARD & Lester CHAN for http://yourls.org/" />
88         <meta name="generator" content="YOURLS <?php echo YOURLS_VERSION ?>" />
89         <meta name="description" content="Insert URL &laquo; YOURLS &raquo; Your Own URL Shortener' | <?php yourls_site_url(); ?>" />
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 '<p>'. $ydb->all_queries .'<p>';
153         } ?>
154         <?php yourls_do_action( 'html_footer', $ydb->context ); ?>
155         </body>
156         </html>
157         <?php
158 }
159
160 /**
161  * Display "Add new URL" box
162  *
163  * @param string $url URL to prefill the input with
164  * @param string $keyword Keyword to prefill the input with
165  */
166 function yourls_html_addnew( $url = '', $keyword = '' ) {
167         $url = $url ? $url : 'http://';
168         ?>
169         <div id="new_url">
170                 <div>
171                         <form id="new_url_form" action="" method="get">
172                                 <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" />
173                                 <?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" />
174                                 <?php yourls_nonce_field( 'add_url', 'nonce-add' ); ?>
175                                 <input type="button" id="add-button" name="add-button" value="<?php yourls_e( 'Shorten The URL' ); ?>" class="button" onclick="add_link();" /></div>
176                         </form>
177                         <div id="feedback" style="display:none"></div>
178                 </div>
179                 <?php yourls_do_action( 'html_addnew' ); ?>
180         </div>
181         <?php 
182 }
183
184 /**
185  * Display main table's footer
186  *
187  * The $param array is defined in /admin/index.php, check the yourls_html_tfooter() call
188  *
189  * @param array $params Array of all required parameters
190  * @return string Result
191  */
192 function yourls_html_tfooter( $params = array() ) {
193         extract( $params ); // extract $search_text, $page, $search_in ...
194         ?>
195         <tfoot>
196                 <tr>
197                         <th colspan="6">
198                         <div id="filter_form">
199                                 <form action="" method="get">
200                                         <div id="filter_options">
201                                                 <?php
202                                                 
203                                                 // First search control: text to search
204                                                 $_input = '<input type="text" name="search" class="text" size="12" value="' . yourls_esc_attr( $search_text ) . '" />';
205                                                 $_options = array(
206                                                         'keyword' => yourls__( 'Short URL' ),
207                                                         'url'     => yourls__( 'URL' ),
208                                                         'title'   => yourls__( 'Title' ),
209                                                         'ip'      => yourls__( 'IP' ),
210                                                 );                                                      
211                                                 $_select = yourls_html_select( 'search_in', $_options, $search_in );
212                                                 /* //translators: "Search for <input field with text to search> in <select dropdown with URL, title...>" */
213                                                 yourls_se( 'Search for %1$s in %2$s', $_input , $_select );
214                                                 echo "&ndash;\n";
215                                                 
216                                                 // Second search control: order by
217                                                 $_options = array(
218                                                         'keyword'      => yourls__( 'Short URL' ),
219                                                         'url'          => yourls__( 'URL' ),
220                                                         'timestamp'    => yourls__( 'Date' ),
221                                                         'ip'           => yourls__( 'IP' ),
222                                                         'clicks'       => yourls__( 'Clicks' ),
223                                                 );
224                                                 $_select = yourls_html_select( 'sort_by', $_options, $sort_by );
225                                                 $sort_order = isset( $sort_order ) ? $sort_order : 'desc' ;
226                                                 $_options = array(
227                                                         'asc'  => yourls__( 'Ascending' ),
228                                                         'desc' => yourls__( 'Descending' ),
229                                                 );
230                                                 $_select2 = yourls_html_select( 'sort_order', $_options, $sort_order );
231                                                 /* //translators: "Order by <criteria dropdown (date, clicks...)> in <order dropdown (Descending or Ascending)>" */
232                                                 yourls_se( 'Order by %1$s %2$s', $_select , $_select2 );
233                                                 echo "&ndash;\n";
234                                                 
235                                                 // Third search control: Show XX rows
236                                                 /* //translators: "Show <text field> rows" */
237                                                 yourls_se( 'Show %s rows',  '<input type="text" name="perpage" class="text" size="2" value="' . $perpage . '" />' );
238                                                 echo "<br/>\n";
239
240                                                 // Fourth search control: Show links with more than XX clicks
241                                                 $_options = array(
242                                                         'more' => yourls__( 'more' ),
243                                                         'less' => yourls__( 'less' ),
244                                                 );
245                                                 $_select = yourls_html_select( 'click_filter', $_options, $click_filter );
246                                                 $_input  = '<input type="text" name="click_limit" class="text" size="4" value="' . $click_limit . '" /> ';
247                                                 /* //translators: "Show links with <more/less> than <text field> clicks" */
248                                                 yourls_se( 'Show links with %1$s than %2$s clicks', $_select, $_input );
249                                                 echo "<br/>\n";
250
251                                                 // Fifth search control: Show links created before/after/between ...
252                                                 $_options = array(
253                                                         'before'  => yourls__('before'),
254                                                         'after'   => yourls__('after'),
255                                                         'between' => yourls__('between'),
256                                                 );
257                                                 $_select = yourls_html_select( 'date_filter', $_options, $date_filter );
258                                                 $_input  = '<input type="text" name="date_first" id="date_first" class="text" size="12" value="' . $date_first . '" />';
259                                                 $_and    = '<span id="date_and"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '> &amp; </span>';
260                                                 $_input2 = '<input type="text" name="date_second" id="date_second" class="text" size="12" value="' . $date_second . '"' . ( $date_filter === 'between' ? ' style="display:inline"' : '' ) . '/>';
261                                                 /* //translators: "Show links created <before/after/between> <date input> <"and" if applicable> <date input if applicable>" */
262                                                 yourls_se( 'Show links created %1$s %2$s %3$s %4$s', $_select, $_input, $_and, $_input2 );
263                                                 ?>
264
265                                                 <div id="filter_buttons">
266                                                         <input type="submit" id="submit-sort" value="<?php yourls_e('Search'); ?>" class="button primary" />
267                                                         &nbsp;
268                                                         <input type="button" id="submit-clear-filter" value="<?php yourls_e('Clear'); ?>" class="button" onclick="window.parent.location.href = 'index.php'" />
269                                                 </div>
270                                 
271                                         </div>
272                                 </form>
273                         </div>
274                         
275                         <?php
276                         // Remove empty keys from the $params array so it doesn't clutter the pagination links
277                         $params = array_filter( $params, 'yourls_return_if_not_empty_string' ); // remove empty keys
278
279                         if( isset( $search_text ) ) {
280                                 $params['search'] = $search_text;
281                                 unset( $params['search_text'] );
282                         }
283                         ?>
284                         
285                         <div id="pagination">
286                                 <span class="navigation">
287                                 <?php if( $total_pages > 1 ) { ?>
288                                         <span class="nav_total"><?php echo sprintf( yourls_n( '1 page', '%s pages', $total_pages ), $total_pages ); ?></span>
289                                         <?php
290                                         $base_page = yourls_admin_url( 'index.php' );
291                                         // Pagination offsets: min( max ( zomg! ) );
292                                         $p_start = max(  min( $total_pages - 4, $page - 2 ), 1 );
293                                         $p_end = min( max( 5, $page + 2 ), $total_pages );
294                                         if( $p_start >= 2 ) {
295                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => 1 ) ), $base_page );
296                                                 echo '<span class="nav_link nav_first"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( '&laquo; First' ) . '</a></span>';
297                                                 echo '<span class="nav_link nav_prev"></span>';
298                                         }
299                                         for( $i = $p_start ; $i <= $p_end; $i++ ) {
300                                                 if( $i == $page ) {
301                                                         echo "<span class='nav_link nav_current'>$i</span>";
302                                                 } else {
303                                                         $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $i ) ), $base_page );
304                                                         echo '<span class="nav_link nav_goto"><a href="' . $link . '" title="' . sprintf( yourls_esc_attr( 'Page %s' ), $i ) .'">'.$i.'</a></span>';
305                                                 }
306                                         }
307                                         if( ( $p_end ) < $total_pages ) {
308                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $total_pages ) ), $base_page );
309                                                 echo '<span class="nav_link nav_next"></span>';
310                                                 echo '<span class="nav_link nav_last"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( 'Last &raquo;' ) . '</a></span>';
311                                         }
312                                         ?>
313                                 <?php } ?>
314                                 </span>
315                         </div>
316                         </th>
317                 </tr>
318                 <?php yourls_do_action( 'html_tfooter' ); ?>
319         </tfoot>
320         <?php
321 }
322
323 /**
324  * Return a select box
325  *
326  * @since 1.6
327  *
328  * @param string $name HTML 'name' (also use as the HTML 'id')
329  * @param array $options array of 'value' => 'Text displayed'
330  * @param string $selected optional 'value' from the $options array that will be highlighted
331  * @param boolean $display false (default) to return, true to echo
332  * @return HTML content of the select element
333  */
334 function yourls_html_select( $name, $options, $selected = '', $display = false ) {
335         $html = "<select name='$name' id='$name' size='1'>\n";
336         foreach( $options as $value => $text ) {
337                 $html .= "<option value='$value' ";
338                 $html .= $selected == $value ? ' selected="selected"' : '';
339                 $html .= ">$text</option>\n";
340         }
341         $html .= "</select>\n";
342         $html  = yourls_apply_filters( 'html_select', $html, $name, $options, $selected, $display );
343         if( $display )
344                 echo $html;
345         return $html;
346 }
347
348 /**
349  * Display the Quick Share box
350  *
351  */
352 function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlink_title = '', $share_title = '', $hidden = false ) {
353         if ( $shortlink_title == '' )
354         $shortlink_title = '<h2>' . yourls__( 'Your short link' ) . '</h2>';
355         if ( $share_title == '' )
356         $share_title = '<h2>' . yourls__( 'Quick Share' ) . '</h2>';
357     
358     // Allow plugins to short-circuit the whole function
359         $pre = yourls_apply_filter( 'shunt_share_box', false );
360         if ( false !== $pre )
361                 return $pre;
362                 
363         $text   = ( $text ? '"'.$text.'" ' : '' );
364         $title  = ( $title ? "$title " : '' );
365         $share  = yourls_esc_textarea( $title.$text.$shorturl );
366         $count  = 140 - strlen( $share );
367         $hidden = ( $hidden ? 'style="display:none;"' : '' );
368         
369         // Allow plugins to filter all data
370         $data = compact( 'longurl', 'shorturl', 'title', 'text', 'shortlink_title', 'share_title', 'share', 'count', 'hidden' );
371         $data = yourls_apply_filter( 'share_box_data', $data );
372         extract( $data );
373         
374         $_share = rawurlencode( $share );
375         $_url   = rawurlencode( $shorturl );
376         ?>
377         
378         <div id="shareboxes" <?php echo $hidden; ?>>
379
380                 <?php yourls_do_action( 'shareboxes_before', $longurl, $shorturl, $title, $text ); ?>
381
382                 <div id="copybox" class="share">
383                 <?php echo $shortlink_title; ?>
384                         <p><input id="copylink" class="text" size="32" value="<?php echo yourls_esc_url( $shorturl ); ?>" /></p>
385                         <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>
386                         <?php if( yourls_do_log_redirect() ) { ?>
387                         <br/><small><?php yourls_e( 'Stats' ); ?>: <a id="statlink" href="<?php echo yourls_esc_url( $shorturl ); ?>+"><?php echo yourls_esc_url( $shorturl ); ?>+</a></small>
388                         <input type="hidden" id="titlelink" value="<?php echo yourls_esc_attr( $title ); ?>" />
389                         <?php } ?>
390                         </p>
391                 </div>
392
393                 <?php yourls_do_action( 'shareboxes_middle', $longurl, $shorturl, $title, $text ); ?>
394
395                 <div id="sharebox" class="share">
396                         <?php echo $share_title; ?>
397                         <div id="tweet">
398                                 <span id="charcount" class="hide-if-no-js"><?php echo $count; ?></span>
399                                 <textarea id="tweet_body"><?php echo $share; ?></textarea>
400                         </div>
401                         <p id="share_links"><?php yourls_e( 'Share with' ); ?> 
402                                 <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>
403                                 <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>
404                                 <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>
405                                 <?php
406                                 yourls_do_action( 'share_links', $longurl, $shorturl, $title, $text );
407                                 // Note: on the main admin page, there are no parameters passed to the sharebox when it's drawn.
408                                 ?>
409                         </p>
410                 </div>
411                 
412                 <?php yourls_do_action( 'shareboxes_after', $longurl, $shorturl, $title, $text ); ?>
413         
414         </div>
415         
416         <?php
417 }
418
419 /**
420  * Die die die
421  *
422  */
423 function yourls_die( $message = '', $title = '', $header_code = 200 ) {
424         yourls_status_header( $header_code );
425         
426         if( !yourls_did_action( 'html_head' ) ) {
427                 yourls_html_head();
428                 yourls_html_logo();
429         }
430         echo yourls_apply_filter( 'die_title', "<h2>$title</h2>" );
431         echo yourls_apply_filter( 'die_message', "<p>$message</p>" );
432         yourls_do_action( 'yourls_die' );
433         if( !yourls_did_action( 'html_head' ) ) {
434                 yourls_html_footer();
435         }
436         die();
437 }
438
439 /**
440  * Return an "Edit" row for the main table
441  *
442  * @param string $keyword Keyword to edit
443  * @return string HTML of the edit row
444  */
445 function yourls_table_edit_row( $keyword ) {
446         global $ydb;
447         
448         $table = YOURLS_DB_TABLE_URL;
449         $keyword = yourls_sanitize_string( $keyword );
450         $id = yourls_string2htmlid( $keyword ); // used as HTML #id
451         $url = yourls_get_keyword_longurl( $keyword );
452         
453         $title = htmlspecialchars( yourls_get_keyword_title( $keyword ) );
454         $safe_url = yourls_esc_attr( $url );
455         $safe_title = yourls_esc_attr( $title );
456         $www = yourls_link();
457         
458         $save_link = yourls_nonce_url( 'save-link_'.$id,
459                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit_save', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
460         );
461         
462         $nonce = yourls_create_nonce( 'edit-save_'.$id );
463         
464         if( $url ) {
465                 $return = <<<RETURN
466 <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>
467 RETURN;
468                 $return = sprintf( urldecode( $return ), yourls__( 'Long URL' ), yourls__( 'Short URL' ), yourls__( 'Title' ), yourls__( 'Save' ), yourls__( 'Save new values' ), yourls__( 'Cancel' ), yourls__( 'Cancel editing' ) );
469         } else {
470                 $return = '<tr class="edit-row notfound">><td colspan="6" class="edit-row notfound">' . yourls__( 'Error, URL not found' ) . '</td></tr>';
471         }
472         
473         $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title );
474
475         return $return;
476 }
477
478 /**
479  * Return an "Add" row for the main table
480  *
481  * @return string HTML of the edit row
482  */
483 function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) {
484         $keyword  = yourls_sanitize_string( $keyword );
485         $id       = yourls_string2htmlid( $keyword ); // used as HTML #id
486         $shorturl = yourls_link( $keyword );
487
488         $statlink = yourls_statlink( $keyword );
489                 
490         $delete_link = yourls_nonce_url( 'delete-link_'.$id,
491                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'delete', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
492         );
493         
494         $edit_link = yourls_nonce_url( 'edit-link_'.$id,
495                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) 
496         );
497         
498         // Action link buttons: the array
499         $actions = array(
500                 'stats' => array(
501                         'href'    => $statlink,
502                         'id'      => "statlink-$id",
503                         'title'   => yourls_esc_attr__( 'Stats' ),
504                         'anchor'  => yourls__( 'Stats' ),
505                 ),
506                 'share' => array(
507                         'href'    => '',
508                         'id'      => "share-button-$id",
509                         'title'   => yourls_esc_attr__( 'Share' ),
510                         'anchor'  => yourls__( 'Share' ),
511                         'onclick' => "toggle_share('$id');return false;",
512                 ),
513                 'edit' => array(
514                         'href'    => $edit_link,
515                         'id'      => "edit-button-$id",
516                         'title'   => yourls_esc_attr__( 'Edit' ),
517                         'anchor'  => yourls__( 'Edit' ),
518                         'onclick' => "edit_link_display('$id');return false;",
519                 ),
520                 'delete' => array(
521                         'href'    => $delete_link,
522                         'id'      => "delete-button-$id",
523                         'title'   => yourls_esc_attr__( 'Delete' ),
524                         'anchor'  => yourls__( 'Delete' ),
525                         'onclick' => "remove_link('$id');return false;",
526                 )
527         );
528         $actions = yourls_apply_filter( 'table_add_row_action_array', $actions );
529         
530         // Action link buttons: the HTML
531         $action_links = '';
532         foreach( $actions as $key => $action ) {
533                 $onclick = isset( $action['onclick'] ) ? 'onclick="' . $action['onclick'] . '"' : '' ;
534                 $action_links .= sprintf( '<a href="%s" id="%s" title="%s" class="%s" %s>%s</a>',
535                         $action['href'], $action['id'], $action['title'], 'button button_'.$key, $onclick, $action['anchor']
536                 );
537         }
538         $action_links = yourls_apply_filter( 'action_links', $action_links, $keyword, $url, $ip, $clicks, $timestamp );
539
540         if( ! $title )
541                 $title = $url;
542
543         $protocol_warning = '';
544         if( ! in_array( yourls_get_protocol( $url ) , array( 'http://', 'https://' ) ) )
545                 $protocol_warning = yourls_apply_filters( 'add_row_protocol_warning', '<span class="warning" title="' . yourls__( 'Not a common link' ) . '">&#9733;</span>' );
546
547         // Row cells: the array
548         $cells = array(
549                 'keyword' => array(
550                         'template'      => '<a href="%shorturl%">%keyword_html%</a>',
551                         'shorturl'      => yourls_esc_url( $shorturl ),
552                         'keyword_html'  => yourls_esc_html( $keyword ),
553                 ),
554                 'url' => array(
555                         'template'      => '<a href="%long_url%" title="%title_attr%">%title_html%</a><br/><small>%warning%<a href="%long_url%">%long_url_html%</a></small>',
556                         'long_url'      => yourls_esc_url( $url ),
557                         'title_attr'    => yourls_esc_attr( $title ),
558                         'title_html'    => yourls_esc_html( yourls_trim_long_string( $title ) ),
559                         'long_url_html' => yourls_esc_html( yourls_trim_long_string( $url ) ),
560                         'warning'       => $protocol_warning,
561                 ),
562                 'timestamp' => array(
563                         'template' => '%date%',
564                         'date'     => date( 'M d, Y H:i', $timestamp +( YOURLS_HOURS_OFFSET * 3600 ) ),
565                 ),
566                 'ip' => array(
567                         'template' => '%ip%',
568                         'ip'       => $ip,
569                 ),
570                 'clicks' => array(
571                         'template' => '%clicks%',
572                         'clicks'   => yourls_number_format_i18n( $clicks, 0, '', '' ),
573                 ),
574                 'actions' => array(
575                         'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',
576                         'actions'  => $action_links,
577                         'id'       => $id,
578                         'keyword'  => $keyword,
579                 ),
580         );
581         $cells = yourls_apply_filter( 'table_add_row_cell_array', $cells, $keyword, $url, $title, $ip, $clicks, $timestamp );
582         
583         // Row cells: the HTML. Replace every %stuff% in 'template' with 'stuff' value.
584         $row = "<tr id=\"id-$id\">";
585         foreach( $cells as $cell_id => $elements ) {
586                 $row .= sprintf( '<td class="%s" id="%s">', $cell_id, $cell_id . '-' . $id );
587                 $row .= preg_replace( '/%([^%]+)?%/e', '$elements["$1"]', $elements['template'] );
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  * Echo the main table head
598  *
599  */
600 function yourls_table_head() {
601         $start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";
602         echo yourls_apply_filter( 'table_head_start', $start );
603         
604         $cells = yourls_apply_filter( 'table_head_cells', array(
605                 'shorturl' => yourls__( 'Short URL' ),
606                 'longurl'  => yourls__( 'Original URL' ),
607                 'date'     => yourls__( 'Date' ),
608                 'ip'       => yourls__( 'IP' ),
609                 'clicks'   => yourls__( 'Clicks' ),
610                 'actions'  => yourls__( 'Actions' )
611         ) );
612         foreach( $cells as $k => $v ) {
613                 echo "<th id='main_table_head_$k'>$v</th>\n";
614         }
615         
616         $end = "</tr></thead>\n";
617         echo yourls_apply_filter( 'table_head_end', $end );
618 }
619
620 /**
621  * Echo the tbody start tag
622  *
623  */
624 function yourls_table_tbody_start() {
625         echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );
626 }
627
628 /**
629  * Echo the tbody end tag
630  *
631  */
632 function yourls_table_tbody_end() {
633         echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );
634 }
635
636 /**
637  * Echo the table start tag
638  *
639  */
640 function yourls_table_end() {
641         echo yourls_apply_filter( 'table_end', '</table>' );
642 }
643
644 /**
645  * Echo HTML tag for a link
646  *
647  */
648 function yourls_html_link( $href, $title = '', $element = '' ) {
649         if( !$title )
650                 $title = $href;
651         if( $element )
652                 $element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );
653         $link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );
654         echo yourls_apply_filter( 'html_link', $link );
655 }
656
657 /**
658  * Display the login screen. Nothing past this point.
659  *
660  */
661 function yourls_login_screen( $error_msg = '' ) {
662         yourls_html_head( 'login' );
663         
664         $action = ( isset( $_GET['action'] ) && $_GET['action'] == 'logout' ? '?' : '' );
665
666         yourls_html_logo();
667         ?>
668         <div id="login">
669                 <form method="post" action="<?php echo $action; ?>"> <?php // reset any QUERY parameters ?>
670                         <?php
671                                 if( !empty( $error_msg ) ) {
672                                         echo '<p class="error">'.$error_msg.'</p>';
673                                 }
674                         ?>
675                         <p>
676                                 <label for="username"><?php yourls_e( 'Username' ); ?></label><br />
677                                 <input type="text" id="username" name="username" size="30" class="text" />
678                         </p>
679                         <p>
680                                 <label for="password"><?php yourls_e( 'Password' ); ?></label><br />
681                                 <input type="password" id="password" name="password" size="30" class="text" />
682                         </p>
683                         <p style="text-align: right;">
684                                 <input type="submit" id="submit" name="submit" value="<?php yourls_e( 'Login' ); ?>" class="button" />
685                         </p>
686                 </form>
687                 <script type="text/javascript">$('#username').focus();</script>
688         </div>
689         <?php
690         yourls_html_footer();
691         die();
692 }
693
694 /**
695  * Display the admin menu
696  *
697  */
698 function yourls_html_menu() {
699
700         // Build menu links
701         if( defined( 'YOURLS_USER' ) ) {
702                 $logout_link = yourls_apply_filter( 'logout_link', sprintf( yourls__('Hello <strong>%s</strong>'), YOURLS_USER ) . ' </strong> (<a href="?action=logout" title="' . yourls_esc_attr__( 'Logout' ) . '">' . yourls__( 'Logout' ) . '</a>)' );
703         } else {
704                 $logout_link = yourls_apply_filter( 'logout_link', '' );
705         }
706         $help_link   = yourls_apply_filter( 'help_link',   '<a href="' . yourls_site_url( false ) .'/readme.html">' . yourls__( 'Help' ) . '</a>' );
707         
708         $admin_links    = array();
709         $admin_sublinks = array();
710         
711         $admin_links['admin'] = array(
712                 'url'    => yourls_admin_url( 'index.php' ),
713                 'title'  => yourls__( 'Go to the admin interface' ),
714                 'anchor' => yourls__( 'Admin interface' )
715         );
716         
717         if( yourls_is_admin() ) {
718                 $admin_links['tools'] = array(
719                         'url'    => yourls_admin_url( 'tools.php' ),
720                         'anchor' => yourls__( 'Tools' )
721                 );
722                 $admin_links['plugins'] = array(
723                         'url'    => yourls_admin_url( 'plugins.php' ),
724                         'anchor' => yourls__( 'Manage Plugins' )
725                 );
726                 $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages();
727         }
728         
729         $admin_links    = yourls_apply_filter( 'admin_links',    $admin_links );
730         $admin_sublinks = yourls_apply_filter( 'admin_sublinks', $admin_sublinks );
731         
732         // Now output menu
733         echo '<ul id="admin_menu">'."\n";
734         if ( yourls_is_private() && !empty( $logout_link ) )
735                 echo '<li id="admin_menu_logout_link">' . $logout_link .'</li>';
736
737         foreach( (array)$admin_links as $link => $ar ) {
738                 if( isset( $ar['url'] ) ) {
739                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
740                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
741                         printf( '<li id="admin_menu_%s_link" class="admin_menu_toplevel"><a href="%s" %s>%s</a>', $link, $ar['url'], $title, $anchor );
742                 }
743                 // Output submenu if any. TODO: clean up, too many code duplicated here
744                 if( isset( $admin_sublinks[$link] ) ) {
745                         echo "<ul>\n";
746                         foreach( $admin_sublinks[$link] as $link => $ar ) {
747                                 if( isset( $ar['url'] ) ) {
748                                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;
749                                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';
750                                         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 );
751                                 }
752                         }
753                         echo "</ul>\n";
754                 }
755         }
756         
757         if ( isset( $help_link ) )
758                 echo '<li id="admin_menu_help_link">' . $help_link .'</li>';
759                 
760         yourls_do_action( 'admin_menu' );
761         echo "</ul>\n";
762         yourls_do_action( 'admin_notices' );
763         yourls_do_action( 'admin_notice' ); // because I never remember if it's 'notices' or 'notice'
764         /*
765         To display a notice:
766         $message = "<div>OMG, dude, I mean!</div>" );
767         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
768         */
769 }
770
771 /**
772  * Wrapper function to display admin notices
773  *
774  */
775 function yourls_add_notice( $message, $style = 'notice' ) {
776         $message = yourls_notice_box( $message, $style );
777         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );
778 }
779
780 /**
781  * Return a formatted notice
782  *
783  */
784 function yourls_notice_box( $message, $style = 'notice' ) {
785         return <<<HTML
786         <div class="$style">
787         <p>$message</p>
788         </div>
789 HTML;
790 }
791
792 /**
793  * Display a page
794  *
795  */
796 function yourls_page( $page ) {
797         $include = YOURLS_ABSPATH . "/pages/$page.php";
798         if( !file_exists($include) ) {
799                 yourls_die( "Page '$page' not found", 'Not found', 404 );
800         }
801         yourls_do_action( 'pre_page', $page );
802         include($include);
803         yourls_do_action( 'post_page', $page );
804         die();  
805 }
806
807 /**
808  * Display the language attributes for the HTML tag.
809  *
810  * Builds up a set of html attributes containing the text direction and language
811  * information for the page. Stolen from WP.
812  *
813  * @since 1.6
814  */
815 function yourls_html_language_attributes() {
816     $attributes = array();
817     $output = '';
818         
819         $attributes[] = ( yourls_is_rtl() ? 'dir="rtl"' : 'dir="ltr"' );
820         
821         $doctype = yourls_apply_filters( 'html_language_attributes_doctype', 'html' );
822         // Experimental: get HTML lang from locale. Should work. Convert fr_FR -> fr-FR
823     if ( $lang = str_replace( '_', '-', yourls_get_locale() ) ) {
824                 if( $doctype == 'xhtml' ) {
825                         $attributes[] = "xml:lang=\"$lang\"";
826                 } else {
827                         $attributes[] = "lang=\"$lang\"";
828                 }
829     }
830
831     $output = implode( ' ', $attributes );
832     $output = yourls_apply_filters( 'html_language_attributes', $output );
833     echo $output;
834 }
835
836 /**
837  * Output translated strings used by the Javascript calendar
838  *
839  * @since 1.6
840  */
841 function yourls_l10n_calendar_strings() {
842         echo "\n<script>\n";
843         echo "var l10n_cal_month = " . json_encode( array_values( yourls_l10n_months() ) ) . ";\n";
844         echo "var l10n_cal_days = " . json_encode( array_values( yourls_l10n_weekday_initial() ) ) . ";\n";
845         echo "var l10n_cal_today = \"" . yourls_esc_js( yourls__( 'Today' ) ) . "\";\n";
846         echo "var l10n_cal_close = \"" . yourls_esc_js( yourls__( 'Close' ) ) . "\";\n";
847         echo "</script>\n";
848         
849         // Dummy returns, to initialize l10n strings used in the calendar
850         yourls__( 'Today' );
851         yourls__( 'Close' );
852 }