]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - user/plugins/sample-toolbar/plugin.php
Use __DIR__ instead of dirname(__FILE__)
[Github/YOURLS.git] / user / plugins / sample-toolbar / plugin.php
1 <?php
2 /*
3 Plugin Name: YOURLS Toolbar
4 Plugin URI: http://yourls.org/
5 Description: Add a social toolbar to your redirected short URLs. Fork this plugin if you want to make your own toolbar.
6 Version: 1.0
7 Author: Ozh
8 Author URI: http://ozh.org/
9 Disclaimer: Toolbars ruin the user experience. Be warned.
10 */
11
12 // No direct call
13 if( !defined( 'YOURLS_ABSPATH' ) ) die();
14
15 global $ozh_toolbar;
16 $ozh_toolbar['do'] = false;
17 $ozh_toolbar['keyword'] = '';
18
19 // When a redirection to a shorturl is about to happen, register variables
20 yourls_add_action( 'redirect_shorturl', 'ozh_toolbar_add' );
21 function ozh_toolbar_add( $args ) {
22         global $ozh_toolbar;
23         $ozh_toolbar['do'] = true;
24         $ozh_toolbar['keyword'] = $args[1];
25 }
26
27 // On redirection, check if this is a toolbar and draw it if needed
28 yourls_add_action( 'pre_redirect', 'ozh_toolbar_do' );
29 function ozh_toolbar_do( $args ) {
30         global $ozh_toolbar;
31         
32         // Does this redirection need a toolbar?
33         if( !$ozh_toolbar['do'] )
34                 return;
35
36         // Do we have a cookie stating the user doesn't want a toolbar?
37         if( isset( $_COOKIE['yourls_no_toolbar'] ) && $_COOKIE['yourls_no_toolbar'] == 1 )
38                 return;
39         
40         // Get URL and page title
41         $url = $args[0];
42         $pagetitle = yourls_get_keyword_title( $ozh_toolbar['keyword'] );
43
44         // Update title if it hasn't been stored yet
45         if( $pagetitle == '' ) {
46                 $pagetitle = yourls_get_remote_title( $url );
47                 yourls_edit_link_title( $ozh_toolbar['keyword'], $pagetitle );
48         }
49         $_pagetitle = htmlentities( yourls_get_remote_title( $url ) );
50         
51         $www = YOURLS_SITE;
52         $ver = YOURLS_VERSION;
53         $md5 = md5( $url );
54         $sql = yourls_get_num_queries();
55
56         // When was the link created (in days)
57         $diff = abs( time() - strtotime( yourls_get_keyword_timestamp( $ozh_toolbar['keyword'] ) ) );
58         $days = floor( $diff / (60*60*24) );
59         if( $days == 0 ) {
60                 $created = 'today';
61         } else {
62                 $created = $days . ' ' . yourls_n( 'day', 'days', $days ) . ' ago';
63         }
64         
65         // How many hits on the page
66         $hits = 1 + yourls_get_keyword_clicks( $ozh_toolbar['keyword'] );
67         $hits = $hits . ' ' . yourls_n( 'view', 'views', $hits );
68         
69         // Plugin URL (no URL is hardcoded)
70         $pluginurl = YOURLS_PLUGINURL . '/'.yourls_plugin_basename( __DIR__ );
71
72         // All set. Draw the toolbar itself.
73         echo <<<PAGE
74 <html>
75 <head>
76         <title>$pagetitle &mdash; YOURLS</title>
77         <link rel="icon" type="image/gif" href="$www/images/favicon.gif" />
78         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
79         <meta http-equiv="X-UA-Compatible" content="IE-9"/>
80         <meta name="generator" content="YOURLS v$ver" />
81         <meta name="ROBOTS" content="NOINDEX, FOLLOW" />
82         <link rel="stylesheet" href="$pluginurl/css/toolbar.css" type="text/css" media="all" />
83 </head>
84 <body>
85 <div id="yourls-bar">
86         <div id="yourls-about">
87                 Short link powered by <a href="http://yourls.org/">YOURLS</a> and created $created. $hits.
88                 <!-- $sql queries -->
89         </div>
90         
91         <div id="yourls-delicious">
92         <img src="http://static.delicious.com/img/delicious.small.gif" height="10" width="10" alt="Delicious" />
93         <a id="yourls-delicious-link" title="Bookmark on delicious" href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;"> Bookmark on Delicious</a>
94         </div>
95
96         <script type="text/javascript" id="topsy_global_settings">
97         var topsy_theme = "light-blue";
98         var topsy_nick = " ";
99         var topsy_style = "small";
100         var topsy_order = "count,retweet,badge";
101         </script>
102         <div id="yourls-topsy" class="topsy_widget_data">
103                 <!--{
104                         "url": "$www/{$ozh_toolbar['keyword']}",
105                         "title": "$_pagetitle",
106                 }-->
107         </div>
108         
109         <div id="yourls-selfclose">
110                 <a id="yourls-once" href="$url" title="Close this toolbar">close</a>
111                 <a id="yourls-always" href="$url" title="Never show me this toolbar again">close</a>
112                 
113         </div>
114 </div>
115
116 <iframe id="yourls-frame" frameborder="0" noresize="noresize" src="$url" name="yourlsFrame"></iframe>
117 <script type="text/javascript" src="$pluginurl/js/toolbar.js"></script>
118 <script type="text/javascript" src="http://cdn.topsy.com/topsy.js?init=topsyWidgetCreator"></script>
119 <script type="text/javascript" src="http://feeds.delicious.com/v2/json/urlinfo/$md5?callback=yourls_get_books"></script>
120 </body>
121 </html>
122 PAGE;
123         
124         // Don't forget to die, to interrupt the flow of normal events (ie redirecting to long URL)
125         die();
126 }