OSDN Git Service

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