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