]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-html.php
Translation plural forms + deprecate function yourls_plural()
[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.8.2.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();" /></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 yourls_e( 'Search for' ); ?>\r
201                                                 <input type="text" name="search" class="text" size="12" value="<?php echo yourls_esc_attr( $search_text ); ?>" />\r
202                                                 <?php yourls_e( 'in' ); ?>\r
203                                                 <select name="search_in" size="1">\r
204                                                         <option value="keyword"<?php if( $search_in == 'keyword' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Short URL' ); ?></option>\r
205                                                         <option value="url"<?php if( $search_in == 'url' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'URL' ); ?></option>\r
206                                                         <option value="title"<?php if( $search_in == 'title' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Title' ); ?></option>\r
207                                                         <option value="ip"<?php if( $search_in == 'ip' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'IP' ); ?></option>\r
208                                                 </select>\r
209                                                 &ndash; <?php yourls_e( 'Order by' ); ?>\r
210                                                 <?php $sort_by = isset( $sort_by ) ? $sort_by : 'timestamp' ; ?>\r
211                                                 <select name="sort_by" size="1">\r
212                                                         <option value="id"<?php if( $sort_by == 'keyword' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Short URL' ); ?></option>\r
213                                                         <option value="url"<?php if( $sort_by == 'url' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'URL' ); ?></option>\r
214                                                         <option value="timestamp"<?php if( $sort_by == 'timestamp' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Date' ); ?></option>\r
215                                                         <option value="ip"<?php if( $sort_by == 'ip' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'IP' ); ?></option>\r
216                                                         <option value="clicks"<?php if( $sort_by == 'clicks' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Clicks' ); ?></option>\r
217                                                 </select>\r
218                                                 <?php $sort_order = isset( $sort_order ) ? $sort_order : 'desc' ; ?>\r
219                                                 <select name="sort_order" size="1">\r
220                                                         <option value="asc" <?php if( $sort_order == 'asc' )  { echo ' selected="selected"'; } ?>><?php yourls_e( 'Ascending' ); ?></option>\r
221                                                         <option value="desc"<?php if( $sort_order == 'desc' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'Descending' ); ?></option>\r
222                                                 </select>\r
223                                                 &ndash; <?php yourls_e( 'Show' ); ?>\r
224                                                 <input type="text" name="perpage" class="text" size="2" value="<?php echo $perpage; ?>" /> <?php yourls_e('rows'); ?><br/>\r
225                                                 \r
226                                                 <?php yourls_e( 'Show links with' ); ?>\r
227                                                 <select name="click_filter" size="1">\r
228                                                         <option value="more"<?php if( $click_filter === 'more' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'more' ); ?></option>\r
229                                                         <option value="less"<?php if( $click_filter === 'less' ) { echo ' selected="selected"'; } ?>><?php yourls_e( 'less' ); ?></option>\r
230                                                 </select>\r
231                                                 <?php yourls_e( 'than' ); ?>\r
232                                                 <input type="text" name="click_limit" class="text" size="4" value="<?php echo $click_limit; ?>" /> <?php yourls_e('clicks'); ?><br/>\r
233                                                 \r
234                                                 <?php yourls_e( 'Show links created' ); ?>\r
235                                                 <select name="date_filter" id="date_filter" size="1">\r
236                                                         <option value="before"<?php if($date_filter === 'before') { echo ' selected="selected"'; } ?>><?php yourls_e('before'); ?></option>\r
237                                                         <option value="after"<?php if($date_filter === 'after') { echo ' selected="selected"'; } ?>><?php yourls_e('after'); ?></option>\r
238                                                         <option value="between"<?php if($date_filter === 'between') { echo ' selected="selected"'; } ?>><?php yourls_e( 'between' ); ?></option>\r
239                                                 </select>\r
240                                                 <input type="text" name="date_first" id="date_first" class="text" size="12" value="<?php echo $date_first; ?>" />\r
241                                                 <span id="date_and" <?php if($date_filter === 'between') { echo ' style="display:inline"'; } ?>> and </span>\r
242                                                 <input type="text" name="date_second" id="date_second" class="text" size="12" value="<?php echo $date_second; ?>" <?php if($date_filter === 'between') { echo ' style="display:inline"'; } ?>/>\r
243                                                 \r
244                                                 <div id="filter_buttons">\r
245                                                         <input type="submit" id="submit-sort" value="<?php yourls_e('Search'); ?>" class="button primary" />\r
246                                                         &nbsp;\r
247                                                         <input type="button" id="submit-clear-filter" value="<?php yourls_e('Clear'); ?>" class="button" onclick="window.parent.location.href = 'index.php'" />\r
248                                                 </div>\r
249                                 \r
250                                         </div>\r
251                                 </form>\r
252                         </div>\r
253                         \r
254                         <?php\r
255                         // Remove empty keys from the $params array so it doesn't clutter the pagination links\r
256                         $params = array_filter( $params, 'yourls_return_if_not_empty_string' ); // remove empty keys\r
257 \r
258                         if( isset( $search_text ) ) {\r
259                                 $params['search'] = $search_text;\r
260                                 unset( $params['search_text'] );\r
261                         }\r
262                         ?>\r
263                         \r
264                         <div id="pagination">\r
265                                 <span class="navigation">\r
266                                 <?php if( $total_pages > 1 ) { ?>\r
267                                         <span class="nav_total"><?php echo sprintf( yourls_n( '1 page', '%s pages', $total_pages ), $total_pages ); ?></span>\r
268                                         <?php\r
269                                         $base_page = yourls_admin_url( 'index.php' );\r
270                                         // Pagination offsets: min( max ( zomg! ) );\r
271                                         $p_start = max(  min( $total_pages - 4, $page - 2 ), 1 );\r
272                                         $p_end = min( max( 5, $page + 2 ), $total_pages );\r
273                                         if( $p_start >= 2 ) {\r
274                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => 1 ) ), $base_page );\r
275                                                 echo '<span class="nav_link nav_first"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( '&laquo; First' ) . '</a></span>';\r
276                                                 echo '<span class="nav_link nav_prev"></span>';\r
277                                         }\r
278                                         for( $i = $p_start ; $i <= $p_end; $i++ ) {\r
279                                                 if( $i == $page ) {\r
280                                                         echo "<span class='nav_link nav_current'>$i</span>";\r
281                                                 } else {\r
282                                                         $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $i ) ), $base_page );\r
283                                                         echo '<span class="nav_link nav_goto"><a href="' . $link . '" title="' . sprintf( yourls_esc_attr( 'Page %s' ), $i ) .'">'.$i.'</a></span>';\r
284                                                 }\r
285                                         }\r
286                                         if( ( $p_end ) < $total_pages ) {\r
287                                                 $link = yourls_add_query_arg( array_merge( $params, array( 'page' => $total_pages ) ), $base_page );\r
288                                                 echo '<span class="nav_link nav_next"></span>';\r
289                                                 echo '<span class="nav_link nav_last"><a href="' . $link . '" title="' . yourls_esc_attr__('Go to First Page') . '">' . yourls__( 'Last &raquo;' ) . '</a></span>';\r
290                                         }\r
291                                         ?>\r
292                                 <?php } ?>\r
293                                 </span>\r
294                         </div>\r
295                         </th>\r
296                 </tr>\r
297                 <?php yourls_do_action( 'html_tfooter' ); ?>\r
298         </tfoot>\r
299         <?php\r
300 }\r
301 \r
302 /**\r
303  * Display the Quick Share box\r
304  *\r
305  */\r
306 function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlink_title = '', $share_title = '', $hidden = false ) {\r
307         if ( $shortlink_title == '' )\r
308         $shortlink_title = '<h2>' . yourls__( 'Your short link' ) . '</h2>';\r
309         if ( $share_title == '' )\r
310         $share_title = '<h2>' . yourls__( 'Quick Share' ) . '</h2>';\r
311     \r
312     // Allow plugins to short-circuit the whole function\r
313         $pre = yourls_apply_filter( 'shunt_share_box', false );\r
314         if ( false !== $pre )\r
315                 return $pre;\r
316                 \r
317         $text   = ( $text ? '"'.$text.'" ' : '' );\r
318         $title  = ( $title ? "$title " : '' );\r
319         $share  = yourls_esc_textarea( $title.$text.$shorturl );\r
320         $count  = 140 - strlen( $share );\r
321         $hidden = ( $hidden ? 'style="display:none;"' : '' );\r
322         \r
323         // Allow plugins to filter all data\r
324         $data = compact( 'longurl', 'shorturl', 'title', 'text', 'shortlink_title', 'share_title', 'share', 'count', 'hidden' );\r
325         $data = yourls_apply_filter( 'share_box_data', $data );\r
326         extract( $data );\r
327         \r
328         $_share = rawurlencode( $share );\r
329         $_url   = rawurlencode( $shorturl );\r
330         ?>\r
331         \r
332         <div id="shareboxes" <?php echo $hidden; ?>>\r
333 \r
334                 <?php yourls_do_action( 'shareboxes_before', $longurl, $shorturl, $title, $text ); ?>\r
335 \r
336                 <div id="copybox" class="share">\r
337                 <?php echo $shortlink_title; ?>\r
338                         <p><input id="copylink" class="text" size="32" value="<?php echo yourls_esc_url( $shorturl ); ?>" /></p>\r
339                         <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
340                         <?php if( yourls_do_log_redirect() ) { ?>\r
341                         <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
342                         <input type="hidden" id="titlelink" value="<?php echo yourls_esc_attr( $title ); ?>" />\r
343                         <?php } ?>\r
344                         </p>\r
345                 </div>\r
346 \r
347                 <?php yourls_do_action( 'shareboxes_middle', $longurl, $shorturl, $title, $text ); ?>\r
348 \r
349                 <div id="sharebox" class="share">\r
350                         <?php echo $share_title; ?>\r
351                         <div id="tweet">\r
352                                 <span id="charcount" class="hide-if-no-js"><?php echo $count; ?></span>\r
353                                 <textarea id="tweet_body"><?php echo $share; ?></textarea>\r
354                         </div>\r
355                         <p id="share_links"><?php yourls_e( 'Share with' ); ?> \r
356                                 <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
357                                 <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
358                                 <a id="share_ff" href="http://friendfeed.com/share/bookmarklet/frame#title=<?php echo $_share; ?>" title="<?php yourls_e( 'Share on Friendfeed' ); ?>" onclick="javascript:share('ff');return false;">FriendFeed</a>\r
359                                 <?php\r
360                                 yourls_do_action( 'share_links', $longurl, $shorturl, $title, $text );\r
361                                 // Note: on the main admin page, there are no parameters passed to the sharebox when it's drawn.\r
362                                 ?>\r
363                         </p>\r
364                 </div>\r
365                 \r
366                 <?php yourls_do_action( 'shareboxes_after', $longurl, $shorturl, $title, $text ); ?>\r
367         \r
368         </div>\r
369         \r
370         <?php\r
371 }\r
372 \r
373 /**\r
374  * Die die die\r
375  *\r
376  */\r
377 function yourls_die( $message = '', $title = '', $header_code = 200 ) {\r
378         yourls_status_header( $header_code );\r
379         \r
380         if( !yourls_did_action( 'html_head' ) ) {\r
381                 yourls_html_head();\r
382                 yourls_html_logo();\r
383         }\r
384         echo yourls_apply_filter( 'die_title', "<h2>$title</h2>" );\r
385         echo yourls_apply_filter( 'die_message', "<p>$message</p>" );\r
386         yourls_do_action( 'yourls_die' );\r
387         if( !yourls_did_action( 'html_head' ) ) {\r
388                 yourls_html_footer();\r
389         }\r
390         die();\r
391 }\r
392 \r
393 /**\r
394  * Return an "Edit" row for the main table\r
395  *\r
396  * @param string $keyword Keyword to edit\r
397  * @return string HTML of the edit row\r
398  */\r
399 function yourls_table_edit_row( $keyword ) {\r
400         global $ydb;\r
401         \r
402         $table = YOURLS_DB_TABLE_URL;\r
403         $keyword = yourls_sanitize_string( $keyword );\r
404         $id = yourls_string2htmlid( $keyword ); // used as HTML #id\r
405         $url = yourls_get_keyword_longurl( $keyword );\r
406         \r
407         $title = htmlspecialchars( yourls_get_keyword_title( $keyword ) );\r
408         $safe_url = yourls_esc_attr( $url );\r
409         $safe_title = yourls_esc_attr( $title );\r
410         $www = yourls_link();\r
411         \r
412         $save_link = yourls_nonce_url( 'save-link_'.$id,\r
413                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit_save', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) \r
414         );\r
415         \r
416         $nonce = yourls_create_nonce( 'edit-save_'.$id );\r
417         \r
418         if( $url ) {\r
419                 $return = <<<RETURN\r
420 <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_save('$id');" />&nbsp;<input type="button" id="edit-close-$id" name="edit-close-$id" value="%s" title="%s" class="button" onclick="hide_edit('$id');" /><input type="hidden" id="old_keyword_$id" value="$keyword"/><input type="hidden" id="nonce_$id" value="$nonce"/></td></tr>\r
421 RETURN;\r
422                 $return = sprintf( urldecode( $return ), yourls__( 'Long URL' ), yourls__( 'Short URL' ), yourls__( 'Title' ), yourls__( 'Save' ), yourls__( 'Save new values' ), yourls__( 'Cancel' ), yourls__( 'Cancel editing' ) );\r
423         } else {\r
424                 $return = '<tr class="edit-row notfound">><td colspan="6" class="edit-row notfound">' . yourls__( 'Error, URL not found' ) . '</td></tr>';\r
425         }\r
426         \r
427         $return = yourls_apply_filter( 'table_edit_row', $return, $keyword, $url, $title );\r
428 \r
429         return $return;\r
430 }\r
431 \r
432 /**\r
433  * Return an "Add" row for the main table\r
434  *\r
435  * @return string HTML of the edit row\r
436  */\r
437 function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $timestamp ) {\r
438         $keyword  = yourls_sanitize_string( $keyword );\r
439         $id       = yourls_string2htmlid( $keyword ); // used as HTML #id\r
440         $shorturl = yourls_link( $keyword );\r
441 \r
442         $statlink = yourls_statlink( $keyword );\r
443                 \r
444         $delete_link = yourls_nonce_url( 'delete-link_'.$id,\r
445                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'delete', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) \r
446         );\r
447         \r
448         $edit_link = yourls_nonce_url( 'edit-link_'.$id,\r
449                 yourls_add_query_arg( array( 'id' => $id, 'action' => 'edit', 'keyword' => $keyword ), yourls_admin_url( 'admin-ajax.php' ) ) \r
450         );\r
451         \r
452         // Action link buttons: the array\r
453         $actions = array(\r
454                 'stats' => array(\r
455                         'href'    => $statlink,\r
456                         'id'      => "statlink-$id",\r
457                         'title'   => yourls_esc_attr__( 'Stats' ),\r
458                         'anchor'  => yourls__( 'Stats' ),\r
459                 ),\r
460                 'share' => array(\r
461                         'href'    => '',\r
462                         'id'      => "share-button-$id",\r
463                         'title'   => yourls_esc_attr__( 'Share' ),\r
464                         'anchor'  => yourls__( 'Share' ),\r
465                         'onclick' => "toggle_share('$id');return false;",\r
466                 ),\r
467                 'edit' => array(\r
468                         'href'    => $edit_link,\r
469                         'id'      => "edit-button-$id",\r
470                         'title'   => yourls_esc_attr__( 'Edit' ),\r
471                         'anchor'  => yourls__( 'Edit' ),\r
472                         'onclick' => "edit('$id');return false;",\r
473                 ),\r
474                 'delete' => array(\r
475                         'href'    => $delete_link,\r
476                         'id'      => "delete-button-$id",\r
477                         'title'   => yourls_esc_attr__( 'Delete' ),\r
478                         'anchor'  => yourls__( 'Delete' ),\r
479                         'onclick' => "remove('$id');return false;",\r
480                 )\r
481         );\r
482         $actions = yourls_apply_filter( 'table_add_row_action_array', $actions );\r
483         \r
484         // Action link buttons: the HTML\r
485         $action_links = '';\r
486         foreach( $actions as $key => $action ) {\r
487                 $onclick = isset( $action['onclick'] ) ? 'onclick="' . $action['onclick'] . '"' : '' ;\r
488                 $action_links .= sprintf( '<a href="%s" id="%s" title="%s" class="%s" %s>%s</a>',\r
489                         $action['href'], $action['id'], $action['title'], 'button button_'.$key, $onclick, $action['anchor']\r
490                 );\r
491         }\r
492         $action_links = yourls_apply_filter( 'action_links', $action_links, $keyword, $url, $ip, $clicks, $timestamp );\r
493 \r
494         if( ! $title )\r
495                 $title = $url;\r
496 \r
497         $protocol_warning = '';\r
498         if( ! in_array( yourls_get_protocol( $url ) , array( 'http://', 'https://' ) ) )\r
499                 $protocol_warning = yourls_apply_filters( 'add_row_protocol_warning', '<span class="warning" title="' . yourls__( 'Not a common link' ) . '">&#9733;</span>' );\r
500 \r
501         // Row cells: the array\r
502         $cells = array(\r
503                 'keyword' => array(\r
504                         'template'      => '<a href="%shorturl%">%keyword_html%</a>',\r
505                         'shorturl'      => yourls_esc_url( $shorturl ),\r
506                         'keyword_html'  => yourls_esc_html( $keyword ),\r
507                 ),\r
508                 'url' => array(\r
509                         'template'      => '<a href="%long_url%" title="%title_attr%">%title_html%</a><br/><small>%warning%<a href="%long_url%">%long_url_html%</a></small>',\r
510                         'long_url'      => yourls_esc_url( $url ),\r
511                         'title_attr'    => yourls_esc_attr( $title ),\r
512                         'title_html'    => yourls_esc_html( yourls_trim_long_string( $title ) ),\r
513                         'long_url_html' => yourls_esc_html( yourls_trim_long_string( $url ) ),\r
514                         'warning'       => $protocol_warning,\r
515                 ),\r
516                 'timestamp' => array(\r
517                         'template' => '%date%',\r
518                         'date'     => date( 'M d, Y H:i', $timestamp +( YOURLS_HOURS_OFFSET * 3600 ) ),\r
519                 ),\r
520                 'ip' => array(\r
521                         'template' => '%ip%',\r
522                         'ip'       => $ip,\r
523                 ),\r
524                 'clicks' => array(\r
525                         'template' => '%clicks%',\r
526                         'clicks'   => number_format( $clicks, 0, '', '' ),\r
527                 ),\r
528                 'actions' => array(\r
529                         'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',\r
530                         'actions'  => $action_links,\r
531                         'id'       => $id,\r
532                         'keyword'  => $keyword,\r
533                 ),\r
534         );\r
535         $cells = yourls_apply_filter( 'table_add_row_cell_array', $cells, $keyword, $url, $title, $ip, $clicks, $timestamp );\r
536         \r
537         // Row cells: the HTML. Replace every %stuff% in 'template' with 'stuff' value.\r
538         $row = "<tr id=\"id-$id\">";\r
539         foreach( $cells as $cell_id => $elements ) {\r
540                 $row .= sprintf( '<td class="%s" id="%s">', $cell_id, $cell_id . '-' . $id );\r
541                 $row .= preg_replace( '/%([^%]+)?%/e', '$elements["$1"]', $elements['template'] );\r
542                 $row .= '</td>';\r
543         }\r
544         $row .= "</tr>";\r
545         $row  = yourls_apply_filter( 'table_add_row', $row, $keyword, $url, $title, $ip, $clicks, $timestamp );\r
546         \r
547         return $row;\r
548 }\r
549 \r
550 /**\r
551  * Echo the main table head\r
552  *\r
553  */\r
554 function yourls_table_head() {\r
555         $start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";\r
556         echo yourls_apply_filter( 'table_head_start', $start );\r
557         \r
558         $cells = yourls_apply_filter( 'table_head_cells', array(\r
559                 'shorturl' => yourls__( 'Short URL' ),\r
560                 'longurl'  => yourls__( 'Original URL' ),\r
561                 'date'     => yourls__( 'Date' ),\r
562                 'ip'       => yourls__( 'IP' ),\r
563                 'clicks'   => yourls__( 'Clicks' ),\r
564                 'actions'  => yourls__( 'Actions' )\r
565         ) );\r
566         foreach( $cells as $k => $v ) {\r
567                 echo "<th id='main_table_head_$k'>$v</th>\n";\r
568         }\r
569         \r
570         $end = "</tr></thead>\n";\r
571         echo yourls_apply_filter( 'table_head_end', $end );\r
572 }\r
573 \r
574 /**\r
575  * Echo the tbody start tag\r
576  *\r
577  */\r
578 function yourls_table_tbody_start() {\r
579         echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );\r
580 }\r
581 \r
582 /**\r
583  * Echo the tbody end tag\r
584  *\r
585  */\r
586 function yourls_table_tbody_end() {\r
587         echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );\r
588 }\r
589 \r
590 /**\r
591  * Echo the table start tag\r
592  *\r
593  */\r
594 function yourls_table_end() {\r
595         echo yourls_apply_filter( 'table_end', '</table>' );\r
596 }\r
597 \r
598 /**\r
599  * Echo HTML tag for a link\r
600  *\r
601  */\r
602 function yourls_html_link( $href, $title = '', $element = '' ) {\r
603         if( !$title )\r
604                 $title = $href;\r
605         if( $element )\r
606                 $element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );\r
607         $link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );\r
608         echo yourls_apply_filter( 'html_link', $link );\r
609 }\r
610 \r
611 /**\r
612  * Display the login screen. Nothing past this point.\r
613  *\r
614  */\r
615 function yourls_login_screen( $error_msg = '' ) {\r
616         yourls_html_head( 'login' );\r
617         \r
618         $action = ( isset( $_GET['action'] ) && $_GET['action'] == 'logout' ? '?' : '' );\r
619 \r
620         yourls_html_logo();\r
621         ?>\r
622         <div id="login">\r
623                 <form method="post" action="<?php echo $action; ?>"> <?php // reset any QUERY parameters ?>\r
624                         <?php\r
625                                 if( !empty( $error_msg ) ) {\r
626                                         echo '<p class="error">'.$error_msg.'</p>';\r
627                                 }\r
628                         ?>\r
629                         <p>\r
630                                 <label for="username"><?php yourls_e( 'Username' ); ?></label><br />\r
631                                 <input type="text" id="username" name="username" size="30" class="text" />\r
632                         </p>\r
633                         <p>\r
634                                 <label for="password"><?php yourls_e( 'Password' ); ?></label><br />\r
635                                 <input type="password" id="password" name="password" size="30" class="text" />\r
636                         </p>\r
637                         <p style="text-align: right;">\r
638                                 <input type="submit" id="submit" name="submit" value="<?php yourls_e( 'Login' ); ?>" class="button" />\r
639                         </p>\r
640                 </form>\r
641                 <script type="text/javascript">$('#username').focus();</script>\r
642         </div>\r
643         <?php\r
644         yourls_html_footer();\r
645         die();\r
646 }\r
647 \r
648 /**\r
649  * Display the admin menu\r
650  *\r
651  */\r
652 function yourls_html_menu() {\r
653 \r
654         // Build menu links\r
655         if( defined( 'YOURLS_USER' ) ) {\r
656                 $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
657         } else {\r
658                 $logout_link = yourls_apply_filter( 'logout_link', '' );\r
659         }\r
660         $help_link   = yourls_apply_filter( 'help_link',   '<a href="' . yourls_site_url( false ) .'/readme.html">' . yourls__( 'Help' ) . '</a>' );\r
661         \r
662         $admin_links    = array();\r
663         $admin_sublinks = array();\r
664         \r
665         $admin_links['admin'] = array(\r
666                 'url'    => yourls_admin_url( 'index.php' ),\r
667                 'title'  => yourls__( 'Go to the admin interface' ),\r
668                 'anchor' => yourls__( 'Admin interface' )\r
669         );\r
670         \r
671         if( yourls_is_admin() ) {\r
672                 $admin_links['tools'] = array(\r
673                         'url'    => yourls_admin_url( 'tools.php' ),\r
674                         'anchor' => yourls__( 'Tools' )\r
675                 );\r
676                 $admin_links['plugins'] = array(\r
677                         'url'    => yourls_admin_url( 'plugins.php' ),\r
678                         'anchor' => yourls__( 'Manage Plugins' )\r
679                 );\r
680                 $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages();\r
681         }\r
682         \r
683         $admin_links    = yourls_apply_filter( 'admin_links',    $admin_links );\r
684         $admin_sublinks = yourls_apply_filter( 'admin_sublinks', $admin_sublinks );\r
685         \r
686         // Now output menu\r
687         echo '<ul id="admin_menu">'."\n";\r
688         if ( yourls_is_private() && !empty( $logout_link ) )\r
689                 echo '<li id="admin_menu_logout_link">' . $logout_link .'</li>';\r
690 \r
691         foreach( (array)$admin_links as $link => $ar ) {\r
692                 if( isset( $ar['url'] ) ) {\r
693                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;\r
694                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';\r
695                         printf( '<li id="admin_menu_%s_link" class="admin_menu_toplevel"><a href="%s" %s>%s</a>', $link, $ar['url'], $title, $anchor );\r
696                 }\r
697                 // Output submenu if any. TODO: clean up, too many code duplicated here\r
698                 if( isset( $admin_sublinks[$link] ) ) {\r
699                         echo "<ul>\n";\r
700                         foreach( $admin_sublinks[$link] as $link => $ar ) {\r
701                                 if( isset( $ar['url'] ) ) {\r
702                                         $anchor = isset( $ar['anchor'] ) ? $ar['anchor'] : $link;\r
703                                         $title  = isset( $ar['title'] ) ? 'title="' . $ar['title'] . '"' : '';\r
704                                         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
705                                 }\r
706                         }\r
707                         echo "</ul>\n";\r
708                 }\r
709         }\r
710         \r
711         if ( isset( $help_link ) )\r
712                 echo '<li id="admin_menu_help_link">' . $help_link .'</li>';\r
713                 \r
714         yourls_do_action( 'admin_menu' );\r
715         echo "</ul>\n";\r
716         yourls_do_action( 'admin_notices' );\r
717         yourls_do_action( 'admin_notice' ); // because I never remember if it's 'notices' or 'notice'\r
718         /*\r
719         To display a notice:\r
720         $message = "<div>OMG, dude, I mean!</div>" );\r
721         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );\r
722         */\r
723 }\r
724 \r
725 /**\r
726  * Wrapper function to display admin notices\r
727  *\r
728  */\r
729 function yourls_add_notice( $message, $style = 'notice' ) {\r
730         $message = yourls_notice_box( $message, $style );\r
731         yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) );\r
732 }\r
733 \r
734 /**\r
735  * Return a formatted notice\r
736  *\r
737  */\r
738 function yourls_notice_box( $message, $style = 'notice' ) {\r
739         return <<<HTML\r
740         <div class="$style">\r
741         <p>$message</p>\r
742         </div>\r
743 HTML;\r
744 }\r
745 \r
746 /**\r
747  * Display a page\r
748  *\r
749  */\r
750 function yourls_page( $page ) {\r
751         $include = YOURLS_ABSPATH . "/pages/$page.php";\r
752         if( !file_exists($include) ) {\r
753                 yourls_die( "Page '$page' not found", 'Not found', 404 );\r
754         }\r
755         yourls_do_action( 'pre_page', $page );\r
756         include($include);\r
757         yourls_do_action( 'post_page', $page );\r
758         die();  \r
759 }