Dotclear

source: plugins/doTwit/_public.php @ 2158

Revision 2158, 4.9 KB checked in by exilius, 14 years ago (diff)

version 1.2

Line 
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
13class doTwit
14{
15     private static $clean = 0;
16     private static $path_cache = 'cache/';
17     
18     public static function dotwitWidget(&$w) {
19     global $core;
20          $cache_file = self::$path_cache.'dotwit_'.md5($w->idTwitter.$w->timeline_friends);
21   
22     //Affichage page d'accueil seulement
23          if ($w->homeonly && $core->url->type != 'default') {
24               return;
25          }
26         
27    try{
28          $host = 'twitter.com';
29          if( $w->timeline_friends  ) {
30               $url = 'http://twitter.com/statuses/friends_timeline/'.$w->idTwitter.'.xml';
31               //$path = '/statuses/friends_timeline/'.$w->idTwitter.'.xml';
32          }else{
33               $url = 'http://twitter.com/statuses/user_timeline/'.$w->idTwitter.'.xml';
34               //$path = '/statuses/user_timeline/'.$w->idTwitter.'.xml';
35          }         
36         
37          if( @filemtime($cache_file) < time() - 60*15 ) 
38          {             
39               $oHttp = new netHttp('');
40               $oHttp->readURL($url,$ssl,$host,$port,$path,$user,$pass);
41               $oHttp->setHost($host,$port);
42               //$oHttp->useSSL($ssl);
43               
44               $user = $w->idTwitter;
45               $pass = $w->pwdTwitter;
46               
47               $oHttp->setAuthorization($user,$pass);
48               $oHttp->get($path);
49               $xml_file = $oHttp->getContent();
50               
51               if ( $xml = @simplexml_load_string($xml_file) )
52               {
53                    if ( $xml->error == '' && $fp = @fopen($cache_file, 'wb'))
54                    {
55                         fwrite($fp, $xml_file);
56                         fclose($fp);
57                    }
58               } else {
59                    $xml = @simplexml_load_string(@file_get_contents($cache_file));
60               }
61          } elseif ( file_exists($cache_file) ) {
62               $xml = @simplexml_load_string(@file_get_contents($cache_file));
63          }
64         
65     }catch (Exception $e){
66           
67     }
68          $res =
69          '<div class="doTwit">'.
70          ($w->title ? '<h2><a href="http://twitter.com/'.$w->idTwiter.'">'.$w->title.'</a></h2>' : '').
71          '<ul>';
72         
73          $nb = 0;
74         
75          if( count($xml->status) == 0 )
76          {
77               $res .= 'Données indisponible sur Twitter !';
78               return $res;
79          }
80         
81          foreach($xml->status as $elm) {
82               
83               $twi['id'][$nb] = (int) $elm->id;
84               $twi['desc'][$nb] = eregi_replace("(http|mailto|news|ftp|https)://(([-éa-z0-9\/\.\?_=#@:~])*)", "<a href=\"\\1://\\2\" target=\"_blank\" style=\"color:blue;\">\\1://\\2</a>",$elm->text);
85               $twi['screen_name'][$nb] = (string) $elm->user->screen_name;
86               $twi['name'][$nb] = (string) $elm->user->name;
87               $twi['location'][$nb] = (string) $elm->user->location;
88               
89               if( $w->display_profil_image ) $twi['img'][$nb] = eregi_replace("_normal.", "_mini.",$elm->user->profile_image_url);
90               if( $w->display_timeout) {
91                    $twi['time'][$nb] = ((int) strtotime($elm->created_at));
92                    $twi['date'][$nb] = date('d/m/Y H\hi', $twi['time'][$nb]);
93                    $twi['desc'][$nb] .= ' <a href="http://twitter.com/'.$twi['screen_name'][$nb].'/statuses/'.$twi['id'][$nb].'" target="_blank">depuis '. $twi['date'][$nb].'</a>';
94                   
95               }
96                             
97               $nb++;
98               
99               if($nb >= $w->limit)break;
100          }
101         
102               
103          for ($i=0;$i<$nb;$i++) {
104
105               if( $w->display_profil_image && $twi['img'][$i] != '' ) {
106                    $res .= '<li class="img" style="background: none;padding-left:2px;border-bottom: 1px solid silver;">';
107                    $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" target="_blank" style="font-weight:bold;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">';
108                    $res .= '<img src="'.$twi['img'][$i].'" alt="'.$twi['name'][$i].'" style="float:left;margin-right:3px;margin-bottom:2px;border:1px solid silver;" />';
109                    $res .= '</a>';
110                    $res .= '<div style="word-wrap: break-word;padding-left:30px;">';
111                    $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" target="_blank" style="font-weight:bold;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">';
112                    $res .= $twi['screen_name'][$i].'</a> ';
113                    $res .= $twi['desc'][$i].'</div>';
114                    $res .= '<div style="clear:both;height:1px;">&nbsp;</div></li>';
115               }else {
116                    $res .= '<li style="border-bottom: 1px solid silver;">';
117                    $res .= '<div style="word-wrap: break-word;">';
118                    $res .= '<a href="http://twitter.com/'.$twi['screen_name'][$i].'" target="_blank" style="font-weight:bold;" title="'.$twi['name'][$i].' ('.$twi['location'][$i].')">';
119                    $res .= $twi['screen_name'][$i];
120                    $res .= '</a> '.$twi['desc'][$i].'</div>';
121                    $res .= '</li>';                   
122               }
123               
124          }
125         
126          $res .= '</ul></div>';
127         
128          self::clean_cache();
129         
130          return $res;
131  }
132 
133  private static function clean_cache() {
134         
135          if( self::$clean == 1) return true;
136         
137          if( $dir = @opendir(self::$path_cache) ) {
138
139               while ($f = @readdir($dir)) {
140                    if(is_file(self::$path_cache.$f) && substr($f,0,7) == 'dotwit_' ) {
141                         if( filemtime(self::$path_cache.$f) < time() - 60 * 15 ) {
142                              @unlink(self::$path_cache.$f);
143                         }
144                    }
145               }
146               
147               @closedir($dir);
148          }
149         
150          self::$clean = 1;
151          return true;
152  }
153 
154}
155?>
Note: See TracBrowser for help on using the repository browser.

Sites map