OSDN Git Service

v1.16
[nucleus-jp/nucleus-plugins.git] / trunk / NP_UpdateTime / NP_UpdateTime.php
1 <?php
2
3 //history
4 //      0.72:   Internationalize.
5 //                      Fixed typo.
6 //      0.71:   Fixed security issue.
7 //                      Fixed typo.
8
9 class NP_UpdateTime extends NucleusPlugin
10 {
11         function getName()
12         {
13                 return 'UpdateTime';
14         }
15
16         function getAuthor()
17         {
18                 return 'nakahara21 + shizuki';
19         }
20
21         function getURL()
22         {
23                 return 'http://nakahara21.com';
24         }
25
26         function getVersion()
27         {
28                 return '0.72';
29         }
30
31         function getDescription()
32         {
33                 return _UPDATETIME_DESCRIPTION;
34         }
35
36         function supportsFeature($what)
37         {
38                 switch ($what) {
39                         case 'SqlTablePrefix':
40                                 return 1;
41                         default:
42                                 return 0;
43                 }
44         }
45
46         function getTableList()
47         {
48                 return array (
49                                           sql_table('plugin_rectime')
50                                          );
51         }
52
53         function getEventList()
54         {
55                 return array (
56                                           'EditItemFormExtras',
57                                           'PreUpdateItem'
58                                          );
59         }
60
61         function install()
62         {
63                 $query = 'CREATE TABLE IF NOT EXISTS ' . sql_table('plugin_rectime') . ' ('
64                            . ' up_id      INT(11)  not null,'
65                            . ' updatetime DATETIME,'
66                            . ' PRIMARY KEY (up_id)'
67                            . ')';
68                 sql_query($query);
69                 $this->createOption('DefautMode', _UPDATETIME_DEFAULT_MODE, 'select', '1', _UPDATETIME_DEFAULT_MODE_VALUE);
70                 $this->createOption('BeforeTime', _UPDATETIME_BEFORE_TIME,  'text',        _UPDATETIME_BEFORE_TIME_VALUE);
71                 $this->createOption('AfterTime',  _UPDATETIME_AFTER_TIME,   'text',        _UPDATETIME_AFTER_TIME_VALUE);
72                 $this->createOption('DateFormat', _UPDATETIME_DATE_FORMAT,  'text',        'Y-m-d H:i:s');
73                 $this->createOption('sLists',     _UPDATETIME_S_LISTS,      'text',        '<ul class="nobullets">');
74                 $this->createOption('eLists',     _UPDATETIME_E_LISTS,      'text',        '</ul>');
75                 $this->createOption('sItems',     _UPDATETIME_S_ITEMS,      'text',        '<li>');
76                 $this->createOption('eItems',     _UPDATETIME_E_ITEMS,      'text',        '</li>');
77                 $this->createOption('uninstFlag', _UPDATETIME_UNINST_FLAG,  'yesno',      'no');
78         }
79
80         function unInstall()
81         { 
82                 if ($this->getOption('uninstFlag') == 'yes') {
83                         sql_query ('DROP TABLE IF EXISTS ' . sql_table('plugin_rectime'));
84                 }
85         }
86
87         function init()
88         {
89                 $language = ereg_replace( '[\\|/]', '', getLanguageName());
90                 if (file_exists($this->getDirectory() . $language . '.php')) {
91                         include_once($this->getDirectory() . $language . '.php');
92                 } else {
93                         include_once($this->getDirectory() . 'english.php');
94                 }
95                 $this->defMode = intval($this->getOption('DefautMode'));
96                 if ($this->defMode > 2) {
97                         $this->defMode = 0;
98                 }
99         }
100
101         function event_EditItemFormExtras($data)
102         {
103                 $checkedFlag[$this->defMode] = ' checked="checked"';
104                 $updateMode = _UPDATETIME_MODE;
105                 $updateOver = _UPDATETIME_OVERWRITE;
106                 $recordOnly = _UPDATETIME_RECORDEONLY;
107                 $noAction   = _UPDATETIME_NOACTION;
108                 $printData  = '<h3 style="margin-bottom:0;">' . $updateMode . "</h3>\n"
109                                         . '<input type="radio" name="updatetime" value="2" id="updatetime_2"' . $checkedFlag[2] . ' />'
110                                         . '<label for="updatetime_2">' . $updateOverwrite . "</label><br />\n"
111                                         . '<input type="radio" name="updatetime" value="1" id="updatetime_1"' . $checkedFlag[1] . ' />'
112                                         . '<label for="updatetime_1">' . $recordOnly . "</label><br />\n"
113                                         . '<input type="radio" name="updatetime" value="0" id="updatetime_0"' . $checkedFlag[0] . ' />'
114                                         . '<label for="updatetime_0">' . $noAction . "</label><br />\n";
115                 echo $printData;
116         }
117
118         function event_PreUpdateItem($data)
119         {
120                 $recd = intRequestVar('updatetime');
121                 if (!$recd) {
122                         return;
123                 }
124                 if (postVar('actiontype') == 'adddraft') {
125                         return;
126                 }
127
128                 $updatetime = mysqldate($data['blog']->getCorrectTime());
129                 if ($recd == 2) {
130                         $upTimeQue  = 'SELECT itime as result '
131                                                 . 'FROM ' . sql_table('item')
132                                                 . ' WHERE inumber=' . intval($data['itemid']);
133                         $tmpTimeQue = 'SELECT updatetime as result '
134                                                 . 'FROM ' . sql_table('plugin_rectime')
135                                                 . ' WHERE up_id = ' . intval($data['itemid']);
136                         $updatetime = '"' . quickQuery($upTimeQue) . '"';
137                         $tmptime = '"' . quickQuery($tmpTimeQue) . '"';
138                         $upQuery    = 'UPDATE ' . sql_table('item')
139                                                 . ' SET   itime   = ' . $updatetime
140                                                 . ' WHERE inumber = ' . intval($data['itemid']);
141                         if ($tmptime > $updatetime) {
142                                 $updatetime = $tmptime;
143                         }
144                         sql_query($upQuery);
145                 }
146                 $delQuery = 'DELETE FROM ' . sql_table('plugin_rectime')
147                                   . ' WHERE up_id = ' . intval($data['itemid']);
148                 sql_query($delQuery);
149                 $query = 'INSERT INTO ' . sql_table('plugin_rectime')
150                            . ' (up_id, updatetime) '
151                            . 'VALUES'
152                            . ' (' . intval($data['itemid']) . ', "' . $updatetime . '")';
153                 $res   = sql_query($query);
154                 if (strpos($res, 'mySQL')) {
155                         return '<p>Could not save data: ' . $res;
156                 }
157                 return '';
158         }
159
160         function doSkinVar($skinType, $maxtoshow = 5, $bmode = 'current')
161         {
162                 global $manager, $CONF, $blogid;
163                 if (is_numeric($blogid)) {
164                         $blogid = intval($blogid);
165                 } else {
166                         $blogid = gttBlogIDFromName($blogid);
167                 }
168                 if (!$blogid) {
169                         $blogid = $CONF['DefaultBlog'];
170                 }
171
172                 $b                    =& $manager->getBlog($blogid);
173                 $this->defaultBlogURL = $b->getURL() ;
174                 if (!$this->defaultBlogURL) {
175                         $this->defaultBlogURL = $CONF['IndexURL'];
176                 }
177
178                 if ($maxtoshow == '') {
179                         $maxtoshow = 5;
180                 }
181                 if ($bmode == '') {
182                         $bmode = 'current';
183                 }
184
185                 echo $this->getOption('sLists') . "\n";
186                 $query = 'SELECT'
187                            . ' r.up_id as up_id, '
188                            . ' IF(INTERVAL(r.updatetime, i.itime), UNIX_TIMESTAMP(r.updatetime), UNIX_TIMESTAMP(i.itime)) as utime '
189                            . 'FROM '
190                            .   sql_table('plugin_rectime') . ' as r, '
191                            .   sql_table('item') .           ' as i '
192                            . 'WHERE'
193                            . ' r.up_id=i.inumber';
194                 if ($bmode != 'all') {
195                         $query .= ' and i.iblog=' . intval($blogid);
196                 }
197                 $query .= ' ORDER BY utime DESC'
198                                 . ' LIMIT 0, ' . intval($maxtoshow);
199                 $res    = sql_query($query);
200                 while ($row = mysql_fetch_object($res)) {
201                         $item =& $manager->getItem($row->up_id, 0, 0);
202                         if ($item) {
203                                 $itemlink  = $this->createGlobalItemLink($item['itemid']);
204                                 $itemtitle = strip_tags($item['title']);
205                                 $itemtitle = shorten($itemtitle,26,'..');
206                                 $itemdate  = date('m/d H:i',$row->utime);
207
208                                 $printData = $this->getOption('sItems') . "\n"
209                                                    . '<a href="' . $itemlink . '">'
210                                                    . htmlspecialchars($itemtitle, ENT_QUOTES, _CHARSET)
211                                                    .'</a> <small>' . $itemdate . "</small>\n"
212                                                    . $this->getOption('eItems') . "\n";
213                                 echo $printData;
214
215                         }
216                 }
217                 echo $this->getOption('eLists');
218         }
219
220         function doTemplateVar(&$item)
221         {
222                 $query = 'SELECT'
223                            . '   r.up_id,'
224                            . '   UNIX_TIMESTAMP(r.updatetime) as updatetime,'
225                            . '   UNIX_TIMESTAMP(i.itime)      as itemtime '
226                            . 'FROM '
227                            .     sql_table('plugin_rectime') . ' as r, '
228                            .     sql_table('item') .           ' as i '
229                            . 'WHERE'
230                            . '     r.up_id = ' . intval($item->itemid)
231                            . ' and r.up_id = i.inumber';
232                 $res   = sql_query($query);
233                 if ($row = mysql_fetch_assoc($res)) {
234                         $data['utime'] = date($this->getOption('DateFormat'), $row['updatetime']);
235                         if ($row['updatetime'] > $row['itemtime']) {
236                                 echo TEMPLATE::fill($this->getOption('AfterTime'), $data);
237                         } elseif ($row['updatetime'] < $row['itemtime']) {
238                                 echo TEMPLATE::fill($this->getOption('BeforeTime'), $data);
239                         }
240                 }
241         }
242
243         function createGlobalItemLink($itemid, $extra = '')
244         {
245                 global $CONF, $manager;
246 /*              if ($CONF['URLMode'] == 'pathinfo') {
247                         $link = $CONF['ItemURL'] . '/item/' . $itemid;
248                 }else{
249                         $blogid = getBlogIDFromItemID($itemid);
250                         $b_tmp =& $manager->getBlog($blogid);
251                         $blogurl = $b_tmp->getURL() ;
252                         if(!$blogurl){
253                                 $blogurl = $this->defaultBlogURL;
254                         }
255                         if(substr($blogurl, -4) != '.php'){
256                                 if(substr($blogurl, -1) != '/')
257                                         $blogurl .= '/';
258                                 $blogurl .= 'index.php';
259                         }
260                         $link = $blogurl . '?itemid=' . $itemid;
261                 }
262                 return addLinkParams($link, $extra);*/
263                 $blogid  =  getBlogIDFromItemID($itemid);
264                 $b_tmp   =& $manager->getBlog($blogid);
265                 $blogurl =  $b_tmp->getURL() ;
266                 if (!$blogurl) {
267                         $blogurl = $this->defaultBlogURL;
268                 }
269                 if (substr($blogurl, -4) != '.php') {
270                         if(substr($blogurl, -1) != '/')
271                                 $blogurl .= '/';
272                         $blogurl .= 'index.php';
273                 }
274                 if (($CONF['URLMode'] == 'pathinfo') && (substr($blogurl, -4) == '.php')) {
275                         $originalURLMode = $CONF['URLMode'];
276                         $CONF['URLMode'] = 'normal';
277                 }
278                 $originalItemURL = $CONF['ItemURL'];
279                 $CONF['ItemURL'] = $blogurl;
280                 $link            = createItemLink($itemid, $extra);
281                 $CONF['ItemURL'] = $originalItemURL;
282                 if ($CONF['URLMode'] <> $originalURLMode) {
283                         $CONF['URLMode'] = $originalURLMode;
284                 }
285                 return $link;
286         }
287 }