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