1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # This file is part of doTwit, a plugin for Dotclear. |
---|
4 | # |
---|
5 | # Copyright (c) 2007 Valentin VAN MEEUWEN |
---|
6 | # <adresse email> |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # ***** END LICENSE BLOCK ***** |
---|
12 | |
---|
13 | if (!defined('DC_RC_PATH')) { return; } |
---|
14 | |
---|
15 | require dirname(__FILE__).'/_widgets.php'; |
---|
16 | |
---|
17 | class doTwit |
---|
18 | { |
---|
19 | private static $clean = 0; |
---|
20 | private static $path_cache = 'cache/'; |
---|
21 | |
---|
22 | public static function dotwitWidget(&$w) { |
---|
23 | global $core; |
---|
24 | $cache_file = self::$path_cache.'dotwit_'.md5($w->idTwitter.$w->timeline_friends); |
---|
25 | |
---|
26 | //Affichage page d'accueil seulement |
---|
27 | if ($w->homeonly && $core->url->type != 'default') { |
---|
28 | return; |
---|
29 | } |
---|
30 | |
---|
31 | try{ |
---|
32 | $host = 'twitter.com'; |
---|
33 | if( $w->timeline_friends ) { |
---|
34 | $url = 'http://twitter.com/statuses/friends_timeline/'.$w->idTwitter.'.xml'; |
---|
35 | //$path = '/statuses/friends_timeline/'.$w->idTwitter.'.xml'; |
---|
36 | }else{ |
---|
37 | $url = 'http://twitter.com/statuses/user_timeline/'.$w->idTwitter.'.xml'; |
---|
38 | //$path = '/statuses/user_timeline/'.$w->idTwitter.'.xml'; |
---|
39 | } |
---|
40 | |
---|
41 | if( @filemtime($cache_file) < time() - 60*15 ) |
---|
42 | { |
---|
43 | $oHttp = new netHttp(''); |
---|
44 | $oHttp->readURL($url,$ssl,$host,$port,$path,$user,$pass); |
---|
45 | $oHttp->setHost($host,$port); |
---|
46 | //$oHttp->useSSL($ssl); |
---|
47 | |
---|
48 | $user = $w->idTwitter; |
---|
49 | $pass = $w->pwdTwitter; |
---|
50 | |
---|
51 | $oHttp->setAuthorization($user,$pass); |
---|
52 | $oHttp->get($path); |
---|
53 | $xml_file = $oHttp->getContent(); |
---|
54 | |
---|
55 | if ( $xml = @simplexml_load_string($xml_file) ) |
---|
56 | { |
---|
57 | if ( $xml->error == '' && $fp = @fopen($cache_file, 'wb')) |
---|
58 | { |
---|
59 | fwrite($fp, $xml_file); |
---|
60 | fclose($fp); |
---|
61 | } |
---|
62 | } else { |
---|
63 | $xml = @simplexml_load_string(@file_get_contents($cache_file)); |
---|
64 | } |
---|
65 | } elseif ( file_exists($cache_file) ) { |
---|
66 | $xml = @simplexml_load_string(@file_get_contents($cache_file)); |
---|
67 | } |
---|
68 | |
---|
69 | }catch (Exception $e){ |
---|
70 | |
---|
71 | } |
---|
72 | $res = |
---|
73 | '<div class="doTwit">'. |
---|
74 | ($w->title ? '<h2><a href="http://twitter.com/'.$w->idTwitter.'">'.$w->title.'</a></h2>' : ''). |
---|
75 | '<ul>'; |
---|
76 | |
---|
77 | $nb = 0; |
---|
78 | |
---|
79 | if( count($xml->status) == 0 ) |
---|
80 | { |
---|
81 | $res .= '<p>'.__('Data inalienable on Twitter!').'</p>'; |
---|
82 | return $res; |
---|
83 | } |
---|
84 | |
---|
85 | foreach($xml->status as $elm) { |
---|
86 | |
---|
87 | $search = array( |
---|
88 | 'url' => "/(http|mailto|news|ftp|https):\/\/(([-A-z0-9\/.?_=#@+%&:~])*)/", |
---|
89 | 'reply' =>"/@([A-Za-z0-9-_]+)/", |
---|
90 | 'hashtag' => "/#([A-z0-9-_]+)/" |
---|
91 | ); |
---|
92 | $replace = array( |
---|
93 | 'url' => "<span class=\"doTwitlink\"><a href=\"$1://$2\" onclick=\"window.open(this.href); return false;\">$1://$2</a></span>", |
---|
94 | 'reply' =>"<span class=\"doTwitreply\"><a href=\"http://twitter.com/$1\" onclick=\"window.open(this.href); return false;\">@$1</a></span>", |
---|
95 | 'hashtag' => "<span class=\"doTwithashtag\"><a href=\"http://search.twitter.com/search?q=%23$1\">#$1</a></span>" |
---|
96 | ); |
---|
97 | $twi['id'][$nb] = (int) $elm->id; |
---|
98 | $twi['desc'][$nb] = preg_replace($search, $replace, $elm->text); |
---|
99 | $twi['screen_name'][$nb] = (string) $elm->user->screen_name; |
---|
100 | $twi['name'][$nb] = (string) $elm->user->name; |
---|
101 | $twi['location'][$nb] = (string) $elm->user->location; |
---|
102 | |
---|
103 | if( $w->display_profil_image ) $twi['img'][$nb] = eregi_replace("_normal.", "_mini.",$elm->user->profile_image_url); |
---|
104 | if( $w->display_timeout) { |
---|
105 | $twi['time'][$nb] = ((int) strtotime($elm->created_at)); |
---|
106 | $twi['date'][$nb] = date('d/m/y H\hi', $twi['time'][$nb]); |
---|
107 | $twi['desc'][$nb] .= ' <br /><span class="doTwitstatut"><a href="http://twitter.com/'.$twi['screen_name'][$nb].'/statuses/'.$elm->id.'" onclick="window.open(this.href); return false;">'.__('on').' '. $twi['date'][$nb].'</a></span>'; |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | $nb++; |
---|
112 | |
---|
113 | if($nb >= $w->limit)break; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | for ($i=0;$i<$nb;$i++) { |
---|
118 | |
---|
119 | if( $w->display_profil_image && $twi['img'][$i] != '' ) { |
---|
120 | $res .= '<li class="doTwitavatar">'; |
---|
121 | $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" onclick="window.open(this.href); return false;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">'; |
---|
122 | $res .= '<img src="'.$twi['img'][$i].'" alt="'.$twi['name'][$i].'" />'; |
---|
123 | $res .= '</a>'; |
---|
124 | $res .= '<div class="doTwittxt">'; |
---|
125 | $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" onclick="window.open(this.href); return false;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">'; |
---|
126 | $res .= $twi['screen_name'][$i].'</a> '; |
---|
127 | $res .= $twi['desc'][$i].'</div>'; |
---|
128 | $res .= '<div class="doTwitclear"> </div></li>'; |
---|
129 | }else { |
---|
130 | $res .= '<li class="doTwitnoavatar">'; |
---|
131 | $res .= '<div class="doTwittxt">'; |
---|
132 | $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" onclick="window.open(this.href); return false;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">'; |
---|
133 | $res .= $twi['screen_name'][$i]; |
---|
134 | $res .= '</a> '.$twi['desc'][$i].'</div>'; |
---|
135 | $res .= '</li>'; |
---|
136 | } |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | $res .= '</ul></div>'; |
---|
141 | |
---|
142 | self::clean_cache(); |
---|
143 | |
---|
144 | return $res; |
---|
145 | } |
---|
146 | |
---|
147 | private static function clean_cache() { |
---|
148 | |
---|
149 | if( self::$clean == 1) return true; |
---|
150 | |
---|
151 | if( $dir = @opendir(self::$path_cache) ) { |
---|
152 | |
---|
153 | while ($f = @readdir($dir)) { |
---|
154 | if(is_file(self::$path_cache.$f) && substr($f,0,7) == 'dotwit_' ) { |
---|
155 | if( filemtime(self::$path_cache.$f) < time() - 60 * 15 ) { |
---|
156 | @unlink(self::$path_cache.$f); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | @closedir($dir); |
---|
162 | } |
---|
163 | |
---|
164 | self::$clean = 1; |
---|
165 | return true; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|
169 | ?> |
---|