OSDN Git Service

admin.php / NP_admin 0.1.9
[nucleus-jp/nucleus-plugins.git] / NP_LatestWritebacks / trunk / NP_LatestWritebacks.php
1 <?php
2 class NP_LatestWritebacks extends NucleusPlugin
3 {
4  
5         function supportsFeature($what)
6         {
7                 switch ($what) {
8                         case 'SqlTablePrefix':
9                                 return 1;
10                         default:
11                                 return 0;
12                 }
13         }
14
15 //      function getEventList()
16 //      {
17 //              return array();
18 //      }
19
20         function getName()
21         {
22                 return 'Latest Writebacks';
23         }
24
25         function getAuthor()
26         {
27                 return 'nakahara21 + Fujisaki + kimitake + shizuki';
28         }
29
30         function getURL()
31         {
32                 return 'http://nakahara21.com/';
33         }
34
35         function getVersion()
36         {
37                 return '1.6';
38         }
39
40         function getDescription()
41         {
42                 // include language file for this plugin 
43                 $language = ereg_replace( '[\\|/]', '', getLanguageName()); 
44                 if (file_exists($this->getDirectory() . $language . '.php')) {
45                         include_once($this->getDirectory() . $language . '.php'); 
46                 } else {
47                         include_once($this->getDirectory() . 'english.php');
48                 }
49                 $description = _NP_LRWITEBACKS_DESC;
50 //              $description = 'This plugin can be used to display the last few comments'
51 //                                       . 'and Trackbacks.<br />'
52 //                                       . 'Usage:&lt;%LatestWritebacks(10,current,all)%&gt;';
53                 return $description;
54         }
55  
56         function install()
57         {
58                 // include language file for this plugin 
59                 $language = ereg_replace( '[\\|/]', '', getLanguageName()); 
60                 if (file_exists($this->getDirectory() . $language . '.php')) {
61                         include_once($this->getDirectory() . $language . '.php'); 
62                 } else {
63                         include_once($this->getDirectory() . 'english.php');
64                 }
65                 $this->createOption('timelocale',   _NP_LRWITEBACKS_TZLOC, 'text',     'ja_JP.' . _CHARSET);
66                 $this->createOption('cmdateformat', _NP_LRWITEBACKS_CDFMT, 'text',     '%Y-%m-%d %H:%M:%S');
67                 $this->createOption('tbdateformat', _NP_LRWITEBACKS_TEFMT, 'text',     '%m-%d');
68                 $this->createOption('cmlisthead',   _NP_LRWITEBACKS_CHEAD, 'textarea', '<ul class="nobullets">');
69                 $this->createOption('cmttemplate',  _NP_LRWITEBACKS_CBODY, 'textarea',
70                 '<li>&clubs;<a href="<%itemlink%>#c<%commentid%>"><%commentdate%>|<%commentator%>&gt;<%commentbody%></a></li>');
71                 $this->createOption('cmlistfoot',   _NP_LRWITEBACKS_CFOOT, 'textarea', '</ul>');
72                 $this->createOption('tblisthead',   _NP_LRWITEBACKS_THEAD, 'textarea', '<ul class="nobullets">');
73                 $this->createOption('tbktemplate',  _NP_LRWITEBACKS_TBODY, 'textarea',
74                 '<li>&hellip;<a href="<%itemlink%>#trackback"><%tbdate%>|<%blogname%> ping: "<%entrytitle%>"</a></li>');
75                 $this->createOption('tblistfoot',   _NP_LRWITEBACKS_TFOOT, 'textarea', '</ul>');
76 /*
77 Comment list template sample
78  Header
79   <ol class="recent-comment">
80  Body
81   <li><a href="<%itemlink%>#c<%commentid%>" title="<%commentbody%>"><%commentator%>(<%commentday%>)</a></li>
82  Footer
83   </ol>
84
85 TrackBack list template sample
86  Header
87   <ol class="recent-trackback">
88  Body
89   <li><a href="<%itemlink%>#tb<%tbid%>" title="<%expect%>"><%blogname%> : <%entrytitle%>(<%tbday%>)</a></li>
90  Footer
91   </ol>
92 */
93         }
94
95         function pluginCheck($pluginName)
96         {
97                 global $manager;
98                 if (!$manager->pluginInstalled('NP_' . $pluginName)) {
99                         return false;
100                 }
101                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
102                 return $plugin;
103         }
104
105         function doSkinVar($skinType,
106                                            $numberOfWritebacks      = 5,
107                                            $filter                  = '',
108                                            $TBorCm                  = 'all',
109                                            $numberOfCharacters      = 60,
110                                            $numberOfTitleCharacters = 40,
111                                            $toadd                   = "...")
112         {
113                 global $manager, $CONF, $blog;
114
115                 if (!is_numeric($numberOfWritebacks)) {
116                         $filter             = $numberOfWritebacks;
117                         $numberOfWritebacks = 5;                                        // defaults to 5
118                 }
119                 $b                    =& $manager->getBlog($CONF['DefaultBlog']);
120                 $this->defaultblogurl = $b->getURL() ;
121                 if (!$this->defaultblogurl)
122                         $this->defaultblogurl = $CONF['IndexURL'] ;
123                 if ($blog) {
124                         $b =& $blog;
125                 }
126                 $blogid = $b->getID();                                                  //for select
127
128                 $filter = trim($filter);
129                 if($filter == 'current'){
130                         $filter = 'cblog = ' . $blogid;
131                 } elseif (strstr($filter, '=')) {
132                         $filter = str_replace('=', '', $filter);
133                         $filter = ' cblog IN(' . str_replace('/', ',', $filter) . ')';
134                 } elseif (strstr($filter, '<>')) {
135                         $filter = str_replace('<>', '', $filter);
136                         $filter = ' cblog <> ' . str_replace('/', ' AND cblog <> ', $filter);
137                 }
138
139                 setlocale(LC_TIME, $this->getOption('timelocale'));
140                 $arr_res = array();
141
142                 if ($TBorCm != 't') {
143                         $join  = '';
144                         $query = 'SELECT'
145                                    . ' c.cnumber as commentid,'
146                                    . ' c.cuser   as commentator,'
147                                    . ' c.cbody   as commentbody,'
148                                    . ' c.citem   as itemid,'
149                                    . ' c.cmember as memberid,'
150 //                                 . ' c.ctime   as commentdate,'
151                                    . ' SUBSTRING(c.ctime, 6, 5) as commentday,'
152                                    . ' UNIX_TIMESTAMP(c.ctime)  as ctimest';
153                         if ($EzComment2 = $this->pluginCheck('EzComment2')) {
154                                 if (method_exists($EzComment2, 'getTemplateParts')) {
155                                         $query .= ', s.comid   as cid, '
156                                                         . 's.secflg  as secret, '
157                                                         . 's.module  as modname, '
158                                                         . 's.userID  as identity ';
159                                         $join   = ' LEFT OUTER JOIN ' . sql_table('plug_ezcomment2') . ' as s '
160                                                         . ' ON c.cnumber = s.comid ';
161                                 }
162                         }
163                         // select
164                         $query .= ' FROM ' . sql_table('comment') .' as c ' . $join;
165                         if ($filter) {
166                                 $query .= ' WHERE ' . $filter;
167                         }
168                         $query .= ' ORDER by c.ctime DESC LIMIT 0, ' . $numberOfWritebacks;
169
170                         $comments = sql_query($query);
171
172                         if (mysql_num_rows($comments)) {
173                                 while ($row = mysql_fetch_object($comments)) {
174                                         $content                = (array)$row;
175                                         $tempBody               = strip_tags($content['commentbody']);
176                                         $tempBody               = htmlspecialchars($tempBody, ENT_QUOTES);
177                                         $tempBody               = shorten($tempBody, $numberOfCharacters, $toadd);
178                                         $tempBody               = htmlspecialchars(htmlspecialchars_decode($tempBody), ENT_QUOTES);
179                                         $tempBody               = str_replace("\r\n", ' ', $tempBody);
180                                         $content['commentdate'] = strftime($this->getOption('cmdateformat'), $content['ctimest']);
181                                         $content['commentbody'] = str_replace("&amp;amp;", '&amp;', $tempBody);
182                                         if (!empty($row->memberid)) {
183                                                 $mem                    = new MEMBER;
184                                                 $mem->readFromID(intval($row->memberid));
185                                                 $content['commentator'] = $mem->getRealName();
186                                         }
187                                         if ($EzComment2) {
188                                                 $bid = intval(getBlogIDFromItemID(intval($comment['itemid'])));
189                                                 if ($EzComment2->getBlogOption($bid, 'secret') == 'yes') {
190                                                         $b     = $manager->getBlog($bid);
191                                                         global $member;
192                                                         $judge = $EzComment2->setSecretJudge($bid, $member, $b);
193                                                 }
194                                         }
195                                         if ($judge && $content['secret']) {
196                                                 $content = $EzComment2->JudgementCommentSecrets($content, $judge);
197                                         }
198
199 /*                                      $cid  = $row->cnumber;
200                                         $ct  = $row->ctimest;
201                                         $ctst  = date("y-m-d H:i",$ct);
202                                         $text = strip_tags($row->cbody);
203                                         $text = htmlspecialchars($text, ENT_QUOTES);
204                                         $ctext = shorten($text,$numberOfCharacters,$toadd);
205  
206                                         if (!$row->cmember) $myname = $row->cuser;
207                                         else {
208                                                 $mem = new MEMBER;
209                                                 $mem->readFromID(intval($row->cmember));
210                                                 $myname = $mem->getRealName();
211                                         }*/
212  
213 //                                      $itemlink = $this->_createItemLink($row->citem, '');
214 //                                      $arr_res[$ct] =  "<li>&clubs;<a href=\"".$itemlink."#c".$cid."\">$ctst|".$myname."&gt;".$ctext."</a></li>" ;
215                                         $itemlink                     = $this->_createItemLink($content['itemid']);
216                                         $content['itemlink']          = $itemlink;
217                                         $arr_res[$content['ctimest']] = TEMPLATE::fill($this->getOption('cmttemplate'), $content);
218                                 }
219                         }
220                 }
221  
222 //=========================
223  
224                 if ($manager->pluginInstalled('NP_TrackBack') && $TBorCm != 'c') {
225                         $query = 'SELECT'
226                                    . ' t.id        as tbid,'
227                                    . ' t.title     as entrytitle,'
228                                    . ' t.excerpt   as expect,'
229                                    . ' t.url       as tburl,'
230                                    . ' t.tb_id     as trackbackid,'
231                                    . ' t.blog_name as blogname,'
232                                    . ' t.timestamp as tbdate,'
233                                    . ' SUBSTRING(t.timestamp, 6, 5) as tbday,'
234                                    . ' UNIX_TIMESTAMP(t.timestamp)  as ttimest'
235                                    . ' FROM ' . sql_table('plugin_tb') . ' t,'
236                                    . sql_table('item') . ' i'
237                                    . ' WHERE t.tb_id = i.inumber';
238                         if ($this->checkTBVersion()) {
239                                 $query .= ' and t.block = 0';
240                         }
241                         if ($filter) {
242                                 $tfilter = str_replace('cblog', 'i.iblog', $filter);
243                                 $query .= ' and ' . $tfilter;
244                         }
245                         $query .= ' ORDER by t.timestamp DESC LIMIT 0, ' . $numberOfWritebacks;
246
247                         $comments = mysql_query($query);
248
249                         if (mysql_num_rows($comments)) {
250                                 while ($row = mysql_fetch_object($comments)) {
251                                         $content               = (array)$row;
252                                         $entrytitle            = strip_tags($content['entrytitle']);
253                                         $entrytitle            = htmlspecialchars($entrytitle, ENT_QUOTES);
254                                         $entrytitle            = shorten($entrytitle, $numberOfCharacters, $toadd);
255                                         $entrytitle            = htmlspecialchars($entrytitle, ENT_QUOTES);
256                                         $content['entrytitle'] = $entrytitle;
257                                         $content['expect']     = str_replace("\r\n", ' ', $content['expect']);
258                                         $blogname              = htmlspecialchars($content['blogname'], ENT_QUOTES);
259                                         $content['blogname']   = $blogname;
260                                         $content['tbdate']     = strftime($this->getOption('tbdateformat'), $content['ttimest']);
261
262 /*                                      $title = strip_tags($row->title);
263                                         $title = htmlspecialchars($title, ENT_QUOTES);
264                                         $ctitle = shorten($title,$numberOfCharacters,$toadd);
265                                         $blogname = htmlspecialchars($row->blog_name, ENT_QUOTES);
266                                         $tbtime = $row->ttimest;
267                                         $ttst  = date("y-m-d H:i",$tbtime);*/
268
269 //                                      $itemlink = $this->_createItemLink($row->tb_id, '');
270 //                                      $arr_res[$tbtime] = '<li>&hellip;<a href="'.$itemlink.'#trackback">'.$ttst.'|'.$blogname.' ping: "'.$ctitle.'"</a></li>';
271                                         $itemlink                     = $this->_createItemLink($content['trackbackid']);
272                                         $content['itemlink']          = $itemlink;
273                                         $arr_res[$content['ttimest']] = TEMPLATE::fill($this->getOption('tbktemplate'), $content);
274                                 }
275                         }
276                 }
277 //=========================
278                 krsort ($arr_res);
279                 $ress        = array_values($arr_res);
280                 $show_rescnt = min(intval($numberOfWritebacks), count($arr_res));
281
282                 switch ($TBorCm) {
283                         case 'c':
284                                 $head = $this->getOption('cmlisthead');
285                                 $foot = $this->getOption('cmlistfoot');
286                                 break;
287                         case 't':
288                                 $head = $this->getOption('tblisthead');
289                                 $foot = $this->getOption('tblistfoot');
290                                 break;
291                         default:
292                                 $head = ' <ul class="nobullets"> ';
293                                 $foot = ' </ul> ';
294                                 break;
295                 }
296 //              echo ' <ul class="nobullets"> ';
297                 echo $head;
298                 for ($j=0; $j < $show_rescnt; $j++) {
299                         echo $ress[$j] . "\n";
300                 }
301                 echo $foot;
302 //              echo " </ul> ";
303         }
304
305         function checkTBVersion()
306         {
307                 $res = sql_query('SHOW FIELDS FROM ' . sql_table('plugin_tb') );
308                 $fieldnames = array();
309                 while ($co = mysql_fetch_assoc($res)) {
310                         $fieldnames[] = $co['Field'];
311                 }
312                 if (in_array('block', $fieldnames)) {
313                         return TRUE;
314                 } else {
315                         return FALSE;
316                 }
317         }
318
319         function _createItemLink($itemid)
320         {
321                 global $CONF, $manager, $blog;
322
323                 $blogid  =  getBlogIDFromItemID($itemid);
324                 $b       =& $manager->getBlog($blogid);
325                 $blogurl =  $b->getURL();
326
327                 if (!$blogurl) {
328                         if ($blog) {
329                                 $b_tmp   =& $manager->getBlog($CONF['DefaultBlog']);
330                                 $blogurl =  $b_tmp->getURL();
331                         }
332                         if (!$blogurl) {
333                                 $blogurl = $CONF['IndexURL'];
334                                 if ($CONF['URLMode'] != 'pathinfo') {
335                                         $blogurl = $CONF['Self'];
336                                 }
337                         }
338                 }
339                 if ($CONF['URLMode'] == 'pathinfo') {
340                         $blogurl = preg_replace('/\/$/', '', $blogurl);
341                 }
342                 $CONF['ItemURL'] = $blogurl;
343
344                 return createItemLink($itemid);
345         }
346  
347 }