OSDN Git Service

バグフィックス
[nucleus-jp/nucleus-plugins.git] / trunk / NP_ShowBlogs / NP_ShowBlogs.php
1 <?php 
2 /**
3  *
4  * SHOWING BLOGS PLUG-IN FOR NucleusCMS
5  * PHP versions 4 and 5
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  *
13  * @author              Original Author nakahara21
14  * @copyright   2005-2006 nakahara21
15  * @license             http://www.gnu.org/licenses/gpl.txt  GNU GENERAL PUBLIC LICENSE Version 2, June 1991
16  * @version     2.62
17  * @link                http://nakahara21.com
18  *
19  * 2.65 add AD code control
20  *      add Category mode
21  *      fix stickies bug
22  * 2.64 fix page switch URL generate
23  * 2.62 security fix and tag related
24  * 2.61 security fix
25  * 2.6 security fix
26  *
27  ****************************************************************************
28  *
29  * THESE PLUG-INS ARE DEDICATED TO ALL THOSE NucleusCMS USERS
30  * WHO FIGHT CORRUPTION AND IRRATIONAL IN EVERY DAY OF THEIR LIVES.
31  *
32  ****************************************************************************/
33
34 class NP_ShowBlogs extends NucleusPlugin
35 {
36
37         function getName()
38         {
39                 return 'Show Blogs';
40         }
41
42         function getMinNucleusVersion()
43         {
44                 return '322';
45         }
46
47         function getAuthor()
48         {
49                 return 'Taka + nakahara21 + kimitake + shizuki';
50         }
51
52         function getURL()
53         {
54                 return 'http://nakahara21.com/';
55         }
56
57         function getVersion()
58         {
59                 return '2.64';
60         }
61
62         function getDescription()
63         {
64                 return _SHOWB_DESC; 
65         } 
66
67         function supportsFeature($what)
68         {
69                 switch($what){
70                         case 'SqlTablePrefix':
71                                 return 1;
72                         default:
73                                 return 0;
74                 }
75         }
76
77         function getEventList()
78         {
79                 return array(
80                         'InitSkinParse'
81                 );
82         }
83
84         function init()
85         {
86                 $language = ereg_replace( '[\\|/]', '', getLanguageName());
87                 if (file_exists($this->getDirectory()  . $language . '.php')) {
88                         include_once($this->getDirectory() . $language . '.php');
89                 }else {
90                         include_once($this->getDirectory() . 'english.php');
91                 }
92         }
93
94         function install()
95         {
96 //              $this->createOption('catnametoshow', '[allblog mode only] category name to show (0:catname on blogname, 1:catname only, 2:blogname only)','text','0');
97 //              $this->createOption('stickmode',        '[currentblog mode only] 0:show all stickyID, 1:show current blog stickyID only', 'text', '1');
98 //              $this->createOption('ads', '[Ads code] code displayed under first and second item of the page', 'textarea', '' . "\n");
99 // <mod by shizuki>
100                 $this->createOption('catformat',                _CAT_FORMAT,    'text',         '<%category%> on <%blogname%>');
101 //              $this->createOption('catnametoshow',    _CATNAME_SHOW,  'text',         '0');
102                 $this->createOption('stickmode',                _STICKMODE,             'text',         '1');
103                 $this->createOption('ads',                              _ADCODE_1,              'textarea',     '' . "\n");
104                 $this->createOption('ads2',                             _ADCODE_2,              'textarea',     '' . "\n");
105                 $this->createOption('tagMode',                  _TAG_MODE,              'select',               '2',    _TAG_SELECT);
106 /* todo can't install ? only warning ?
107  * douyatte 'desc' ni keikoku wo daseba iinoka wakaranai desu
108                 $ver_min = (getNucleusVersion() < $this->getMinNucleusVersion());
109                 $pat_min = ((getNucleusVersion() == $this->getMinNucleusVersion()) &&
110                                 (getNucleusPatchLevel() < $this->getMinNucleusPatchLevel()));
111                 if ($ver_min) { // || $pat_min) {
112                         global $DIR_LIBS;
113                         // uninstall plugin again...
114                         include_once($DIR_LIBS . 'ADMIN.php');
115                         $admin = new ADMIN();
116                         $admin->deleteOnePlugin($this->getID());
117                 
118                         // ...and show error
119                         $admin->error(_ERROR_NUCLEUSVERSIONREQ .
120                         $this->getMinNucleusVersion() . ' patch ' .
121                         $this->getMinNucleusPatchLevel());
122                 }
123 */
124 // </mod by shizuki>
125         }
126
127         function doSkinVar($skinType,
128                                            $template      = 'default/index',
129                                            $amount        = 10,
130                                            $bmode         = '',
131                                            $type          = 1,
132                                            $sort          = 'DESC',
133                                            $sticky        = '',
134                                            $sticktemplate = '',
135                                            $catmode       = 'all',
136                                            $showAdCode    = 1,
137                                            $catStick      = 0
138                                           )
139         {
140                 global $manager, $CONF, $blog, $blogid, $catid, $itemid, $archive, $subcatid;
141
142 //***************************************
143 //      extra setting
144 //***************************************
145 /* blogid(blogshortname) you want to hide 
146         example:
147          $hide =array(5)
148          $hide = array(3,5);
149          $hide = array('private');
150          $hide =array('private','atloss');
151 */
152                 $hide = array();
153 //show blogID
154                 $show = array();
155 // limit number of pages(months) 
156 $pagelimit = 0;
157 $monthlimit = 0;
158                 $catformat = $this->getOption('catformat');
159
160 /**************************************************************************************/
161
162                 $type = (float) $type;
163                 $typeExp = intval(($type - floor($type))*10); //0 or 1 or 9
164                 $this->showAdCode = $showAdCode;
165
166                 list ($pageamount, $offset) = sscanf($amount, '%d(%d)');
167                 if (!$pageamount) {
168                         $pageamount = 10;
169                 }
170                 if ($sort != 'ASC' && $sort != 'DESC') {
171                         $sticktemplate = $sticky;
172                         $sticky = $sort;
173                         $sort = 'DESC';
174                 }
175                 if (!empty($sticky) && empty($sticktemplate)) {
176                         $sticktemplate = $template;
177                 }
178
179                 if (preg_match("/^(<>)?([0-9\/]+)$/", $bmode, $matches)) {
180                         if ($matches[1]) {
181                                 $hide = explode("/", $matches[2]);
182                                 $show = array();
183                         } else {
184                                 $hide = array();
185                                 $show = explode("/", $matches[2]);
186                         }
187                         $bmode = 'all';
188                 }
189
190                 if (preg_match("/^(<>)?([0-9\/]+)$/", $catmode, $matches)) {
191                         if ($matches[1]) {
192                                 $hideCat = explode("/", $matches[2]);
193                                 $showCat = array();
194                         } else {
195                                 $hideCat = array();
196                                 $showCat = explode("/", $matches[2]);
197                         }
198                         $catmode = 'all';
199                 }
200
201                 if ($blog) {
202                         $b =& $blog; 
203                 } else {
204                         $b =& $manager->getBlog($CONF['DefaultBlog']);
205                 }
206                 $this->nowbid = $nowbid = intval($b->getID());
207
208                 $where = '';
209                 $catblogname = 0;
210
211                 if ($bmode != 'all') {
212                         $where .= ' AND i.iblog = ' . $nowbid;
213                 } elseif (isset($hide[0]) && $bmode == 'all') {
214                         foreach ($hide as $val) {
215                                 if (!is_numeric($val)) {
216                                         $val = getBlogIDFromName(intval($val));
217                                 }
218                                 $where .= ' AND i.iblog != ' . intval($val);
219                         }
220                         $catblogname = 1;
221                 } elseif (isset($show[0]) && $bmode == 'all') {
222                         foreach ($show as $val) {
223                                 if (!is_numeric($val)) {
224                                         $val = getBlogIDFromName(intval($val));
225                                 }
226                                 $w[] = intval($val);
227                         }
228                         $catblogname = (count($w) > 1) ? 1 : 0;
229                         $where .= ' AND i.iblog in (' . implode(',', $w) . ')';
230                 }
231
232                 if (isset($hidecat[0]) && $catmode == 'all') {
233                         foreach($hideCat as $val){
234                                 if(!is_numeric($val)){
235                                         $val = getCatIDFromName(intval($val));
236                                 }
237                                 $where .= ' AND i.icat != ' . intval($val);
238                         }
239                         $catblogname = 1;
240                 } elseif (isset($showCat[0]) && $catmode == 'all') {
241                         foreach ($showCat as $val) {
242                                 if (!is_numeric($val)) {
243                                         $val = getBlogIDFromName(intval($val));
244                                 }
245                                 $w[] = intval($val);
246                         }
247                         $catblogname = (count($w) > 1) ? 1 : 0;
248                         $where .= ' AND i.icat in (' . implode(',', $w) . ')';
249                 }
250                 $stickWhere = $where;
251
252                 if ($skinType == 'item' || $skinType == 'index' || $skinType == 'archive') {
253                         $catformat = '"' . addslashes($catformat) . '"';
254                         $nArr      = array(
255                                                            '",c.cname,"',
256                                                            '",b.bname,"',
257                                                            '",c.cdesc,"'
258                                                           );
259                         $fArr      = array(
260                                                            '/<%category%>/',
261                                                            '/<%blogname%>/',
262                                                            '/<%catdesc%>/'
263                                                           );
264                         $catformat = preg_replace($fArr, $nArr, $catformat);
265                         $mtable = "";
266                         if ($manager->pluginInstalled('NP_TagEX')) {
267                                 $t_where = $this->_getTagsInum($where, $skinType, $bmode, $amount);
268                                 $where .= $t_where['where'];
269                         }
270
271                         if ($skinType == 'item') {
272                                 $where .= ' and i.inumber != ' . intval($itemid);
273                         } else {
274
275                                 $sticCatFlag = (!$catid || (!empty($catStick) && $sticktemplate != ''));
276 //                              if (!$catid && $sticky != '') {
277                                 if ($sticCatFlag && $sticky != '') {
278                                         $stickys = explode('/',  $sticky);
279                                         foreach ($stickys as $stickynumber) {
280                                                 $where .= ' AND i.inumber <> ' . intval($stickynumber);
281                                         }
282                                 }
283
284 //                              $hidden = '';
285                                 $temp = $y = $m = $d = '';
286                                 if ($archive) {
287                                         sscanf($archive, '%d-%d-%d', $y, $m, $d);
288                                         if ($d) {
289                                                 $timestamp_start = mktime(0, 0, 0, $m, $d, $y);
290                                                 $timestamp_end = mktime(0, 0, 0, $m, $d+1, $y);
291                                                 $date_str = 'SUBSTRING(i.itime, 1, 10)';
292                                         } else {
293                                                 $timestamp_start = mktime(0, 0, 0, $m, 1, $y);
294                                                 $timestamp_end = mktime(0, 0, 0, $m+1, 1, $y);
295                                                 $date_str = 'SUBSTRING(i.itime,1,7)';
296                                         }
297                                         $where .= ' AND i.itime >= ' . mysqldate($timestamp_start) .
298                                                         ' AND i.itime < ' . mysqldate($timestamp_end);
299                                 } elseif (!empty($monthlimit)) {
300                                         $timestamp_end = mysqldate($b->getCorrectTime());
301                                         sscanf($timestamp_end, '"%d-%d-%d %s"', $y, $m, $d, $temp);
302                                         $timestamp_start = mktime(0, 0, 0, $m-$monthlimit, $d, $y);
303                                         $where .= ' AND i.itime >= ' . mysqldate($timestamp_start)
304                                                         . ' AND i.itime <= ' . $timestamp_end;
305                                 } else {
306                                         $where .= ' AND i.itime <= ' . mysqldate($b->getCorrectTime());
307                                 }
308
309                                 if (!empty($catid)) {
310                                         if ($manager->pluginInstalled('NP_MultipleCategories')) {
311                                                 $mcat_query = $this->_getSubcategoriesWhere($catid);
312                                                 $mtable = $mcat_query['m'];
313                                                 $where .= $mcat_query['w'];
314                                         } else {
315                                                 $where .= ' AND i.icat=' . intval($catid);
316                                         }
317                                         $linkparams['catid'] = $todayparams['catid'] = intval($catid);
318                                 }
319
320                                 if ($type >= 1) {
321                                         $page_switch = $this->PageSwitch($type, $pageamount, $offset, $where, $sort, $mtable);
322                                         if ($typeExp != 9 && $skinType != 'item') {
323                                                 echo $page_switch['buf'];
324                                         }
325                                 }
326                         }
327
328                         $sh_query = 'SELECT i.inumber as itemid, i.ititle as title, i.ibody as body,' .
329                                         ' m.mname as author, m.mrealname as authorname,' .
330                                         ' UNIX_TIMESTAMP(i.itime) as timestamp, i.itime,' .
331                                         ' i.imore as more, m.mnumber as authorid,';
332                         if (!$catblogname) {
333                                 $sh_query .= ' c.cname as category,';
334                         } else {
335                                 $sh_query .= ' concat(' . $catformat . ') as category,';
336                         }
337                         $sh_query .= ' i.icat as catid, i.iclosed as closed'
338                                            . ' FROM '
339                                            . sql_table('member') . ' as m, '
340                                            . sql_table('category') . ' as c, '
341                                            . sql_table('item') . ' as i'
342                                            . $mtable;
343                         if ($bmode == 'all') {
344                                 $sh_query .= ', ' . sql_table('blog') . ' as b ';
345                         }
346                         $sh_query .= ' WHERE i.iauthor = m.mnumber AND i.icat = c.catid';
347                         if ($bmode == 'all') {
348                                 $sh_query .= ' AND b.bnumber = c.cblog';
349                         }
350
351 //                      if ($page_switch['startpos'] == 0 && !$catid && $sticky != '' && $skinType != 'item' && !$this->tagSelected) {
352                         $ads = 0;
353                         $sticCatFlag = ($page_switch['startpos'] == 0 && (!$catid || (!empty($catStick) && $sticktemplate != '')));
354                         if ($sticCatFlag && $sticky != '' && $skinType != 'item' && !$this->tagSelected) {
355                                 foreach ($stickys as $stickynumber) {
356                                         $sticky_query = $sh_query;
357                                         $tempblogid = getBlogIDFromItemID($stickynumber);
358                                         if ($bmode != 'all') {
359                                                 $sticky_query .= ' AND i.iblog = ' . $nowbid;
360                                         }
361                                         $sticky_query .= ' AND i.inumber = ' . intval($stickynumber)
362                                                                    . ' AND i.itime <= ' . mysqldate($b->getCorrectTime())
363                                                                    . ' AND i.idraft = 0';
364                                         if ($catid) {
365                                                 $sticky_query .= ' AND i.icat = ' . intval($catid);
366                                         }
367                                         if ($subcatid) {
368                                                 $sticky_query .= ' AND p.subcategories = ' . intval($subcatid);
369                                         }
370                                         $sticky_query .= $stickWhere;
371                                         if ($this->getOption('stickmode') == 1 && intval($nowbid) == $tempblogid) {
372                                                 $b->showUsingQuery($sticktemplate, $sticky_query, 0, 1, 0); 
373                                         } elseif (!$this->getOption('stickmode')) {
374                                                 $b->showUsingQuery($sticktemplate, $sticky_query, 0, 1, 0); 
375                                         }
376                                         //echo $stickynumber;
377                                         if ($showAdCode > 0 && mysql_num_rows(sql_query($sticky_query))) {
378                                                 if ($ads == 0) {
379                                                         echo $this->getOption('ads');
380                                                 } elseif ($ads == 1) {
381                                                         echo $this->getOption('ads2');
382                                                 } elseif ($ads >= 2) {
383                                                 }
384                                                 $ads++;
385                                         }
386                                 }
387                         }
388
389                         $sh_query .= ' AND i.idraft = 0' . $where;
390
391                         if ($skinType == 'item') {
392                                 $sh_query .= ' ORDER BY FIND_IN_SET(i.inumber,\'' . @join(',', $t_where['inumsres']) . '\')';
393                         } else {
394                                 $sh_query .= ' ORDER BY i.itime ' . $sort;
395                         }
396
397                         if ($skinType != 'item') {
398                                 $this->_showUsingQuery($template, $sh_query, $page_switch['startpos'], $pageamount, $b, $ads);
399                                 if ($type >= 1 && $typeExp != 1) echo $page_switch['buf'];
400                         } elseif ($skinType == 'item') {
401                                 $sh_query .= ' LIMIT 0, ' . $pageamount;
402                                 $b->showUsingQuery($template, $sh_query, 0, 1, 1); 
403                         }
404                 }
405         }
406
407         function _showUsingQuery($template, $showQuery, $q_startpos, $q_amount, $b, $ads)
408         {
409                 global $catid;
410                 $onlyone_query = $showQuery . ' LIMIT ' . intval($q_startpos) .', 1';
411                 $b->showUsingQuery($template, $onlyone_query, 0, 1, 1);
412                 if (intval($ads) == 0 && $this->showAdCode > 0) {
413                         echo $this->getOption('ads');
414 //              }
415 //------------SECOND AD CODE-------------
416                 } elseif (intval($ads) == 1 && $this->showAdCode > 0) {
417                         echo $this->getOption('ads2');
418                 }
419                 $q_startpos++;
420                 $q_amount--;
421                 if ($q_amount < 0) return;
422                 $onlyone_query = $showQuery . ' LIMIT ' . intval($q_startpos) . ', 1';
423                 $b->showUsingQuery($template, $onlyone_query, 0, 1, 1); 
424                 if (mysql_num_rows(sql_query($onlyone_query)) && empty($ads) && $this->showAdCode > 0) {
425                         echo $this->getOption('ads2');
426                 }
427 //------------SECOND AD CODE END-------------
428                 $q_startpos++;
429                 $q_amount--;
430                 if ($q_amount <= 0) return;
431                 $second_query = $showQuery . ' LIMIT ' . intval($q_startpos) . ',' . intval($q_amount);
432                 $b->showUsingQuery($template, $second_query, 0, 1, 1);
433         }
434
435         function event_InitSkinParse($data)
436         {
437                 global $CONF, $manager;
438                 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
439                 if (serverVar('REQUEST_URI') == '') {
440                         $uri = (serverVar('QUERY_STRING')) ?
441                                 serverVar('SCRIPT_NAME') . serverVar('QUERY_STRING') : serverVar('SCRIPT_NAME');
442                 } else { 
443                         $uri = serverVar('REQUEST_URI');
444                 }
445                 $page_str = ($usePathInfo) ? 'page/' : 'page=';
446                 if ($manager->pluginInstalled('NP_CustomURL') || $manager->pluginInstalled('NP_Magical')) {
447                         $page_str = 'page_';
448                 }
449                 list($org_uri, $currPage) = explode($page_str, $uri, 2);
450                 if (getVar('page')) {
451                         $currPage = intGetVar('page');
452                 }
453                 $_GET['page'] = intval($currPage);
454                 $this->currPage = intval($currPage);
455                 $this->pagestr = $page_str;
456         }
457
458         function PageSwitch($type, $pageamount, $offset, $where, $sort, $mtable = '')
459         {
460                 global $CONF, $manager, $archive, $catid, $subcatid;
461
462 // initialize
463                 $startpos = 0;
464                 $catid = intval($catid);
465                 $subcatid = intval($subcatid);
466                 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
467                 $pageamount = intval($pageamount);
468                 $offset = intval($offset);
469                 if ($archive) {
470                         $y = $m = $d = '';
471                         sscanf($archive, '%d-%d-%d', $y, $m, $d);
472                         if (!empty($d)) {
473                                 $archive = sprintf('%04d-%02d-%02d', $y, $m, $d);
474                         } else {
475                                 $archive = sprintf('%04d-%02d', $y, $m);
476                         }
477                 }
478
479                 $page_str = $this->pagestr;
480                 $currentpage = $this->currPage; 
481
482 // createBaseURL
483                 if (!empty($catid)) {
484                         $catrequest = ($usePathInfo) ? $CONF['CategoryKey'] : 'catid';
485                         if (!empty($subcatid)) {
486                                 $mplugin =& $manager->getPlugin('NP_MultipleCategories');
487                                 $subrequest = $mplugin->getRequestName(array());
488                                 if (!empty($archive)) {
489                                         $linkParam = array(
490                                                                            $catrequest => $catid,
491                                                                            $subrequest => $subcatid
492                                                                           );
493                                         $pagelink  = createArchiveLink($archive, $linkParam);
494                                 } else {
495                                         $linkParam = array(
496                                                                            $subrequest => $subcatid
497                                                                           );
498                                         $pagelink = createCategoryLink($catid, $linkParam);
499                                 }
500                         } else {
501                                 if (!empty($archive)) {
502                                         $linkParam = array(
503                                                                            $catrequest => $catid,
504                                                                           );
505                                         $pagelink  = createArchiveLink($archive, $linkParam);
506                                 } else {
507                                         $pagelink = createCategoryLink($catid);
508                                 }
509                         }
510                 } else {
511                         if (!empty($archive)) {
512                                 $pagelink = createArchiveLink($this->nowbid, $archive);
513                         } else {
514                                 $pagelink = createBlogidLink($this->nowbid);
515                         }
516                 }
517                 if ($manager->pluginInstalled('NP_TagEX')) {
518                         $tplugin =& $manager->getPlugin('NP_TagEX');
519                         $requestTag = $tplugin->getNoDecodeQuery('tag');
520                         if (!empty($requestTag)) {
521                                 $requestTarray = $tplugin->splitRequestTags($requestTag);
522                                 $tag = array_shift($requestTarray['and']);
523                                 $tag = $tplugin->_rawdecode($tag);
524                                 if (!empty($requestTarray['and'])) {
525                                         $requestT = implode('+', $requestTarray['and']);
526                                 }
527                                 if (!empty($requestTarray['or'])) {
528                                         $requestTor = implode(':', $requestTarray['or']);
529                                 }
530                                 if (!empty($requestT) && !empty($requestTor)) {
531                                         $tags     = $requestT . ':' . $requestTor;
532                                         $pagelink = $tplugin->creatTagLink($tag, $this->getOption('tagMode'), $tags, '+');
533                                 } elseif (empty($requestT) && !empty($requestTor)) {
534                                         $pagelink = $tplugin->creatTagLink($tag, $this->getOption('tagMode'), $requestTor, ':');
535                                 } else {
536                                         $pagelink = $tplugin->creatTagLink($tag, $this->getOption('tagMode'));
537                                 }
538                         }
539                 }
540
541                 if (strstr ($pagelink, '//')) {
542                         $pagelink = preg_replace("/([^:])\/\//", "$1/", $pagelink);
543                 }
544                 $uri = parse_url($pagelink);
545                 if (!$usePathInfo) {
546                         if ($pagelink == $CONF['BlogURL']) { // add
547                                 $pagelink .= '?';
548                         } elseif ($uri['query']) {
549                                 $pagelink .= '&amp;';
550                         }
551                         $pagelink = str_replace('&amp;&amp;', '&amp;', $pagelink);
552                 } elseif ($usePathInfo && substr($pagelink, -1) != '/') {
553                         if ($uri['query']) {
554                                 $pagelink .= '&amp;';
555                                 $page_str = 'page=';
556                         } else {
557                                 $pagelink .= '/';
558                                 if (strstr ($pagelink, '//')) $link = preg_replace("/([^:])\/\//", "$1/", $pagelink);
559                         }
560                 }
561
562                 if ($currentpage > 0) {
563                         $startpos = ($currentpage - 1) * $pageamount;
564                 } else {
565                         $currentpage = 1;
566                 }
567
568                 $totalamount = 0;
569                 if (is_numeric($where)) {
570                         $totalamount = $where;
571                 } elseif (is_array($where)) {
572                         $totalamount = count($where);
573                 } else {
574                         $p_query = 'SELECT COUNT(i.inumber) FROM %s as i%s WHERE i.idraft = 0%s';
575                         $p_query = sprintf($p_query, sql_table('item'), $mtable, $where);
576 //                      $p_query = 'SELECT COUNT(i.inumber) FROM ' . sql_table('item') . ' as i' . $mtable . ' WHERE i.idraft=0' . $where;
577                         $entries = sql_query($p_query);
578                         if ($row = mysql_fetch_row($entries)) {
579                                 $totalamount = $row[0];
580                         }
581                 }
582                 $totalamount = intval($totalamount);
583                 if (!$archive && !empty($pagelimit) && ($pagelimit * $pageamount < $totalamount)) {
584                         $totalamount = intval($pagelimit) * $pageamount;
585                 }
586                 if ($offset) {
587                         $startpos += $offset;
588                         $totalamount -= $offset;
589                 }
590                 if ($this->maxamount && $this->maxamount < $totalamount) {
591                         $totalamount = intval($this->maxamount);
592                 }
593                 $totalpages = ceil($totalamount/$pageamount);
594                 $totalpages = intval($totalpages);
595                 if ($startpos > $totalamount) {
596                         $currentpage = $totalpages;
597                         $startpos = $totalamount-$pageamount;
598                 }
599                 if ($offset) {
600                         $startpos += $offset;
601                         $totalamount -= $offset;
602                 }
603                 $totalpages = ceil($totalamount/$pageamount);
604                 if ($startpos > $totalamount) {
605                         $currentpage = $totalpages;
606                         $startpos = $totalamount-$pageamount;
607                 }
608                 $prevpage = ($currentpage > 1) ? $currentpage - 1 : 0;
609                 $nextpage = $currentpage + 1;
610                 $firstpagelink = $pagelink . $page_str . '1';
611                 if ($page_str == 'page_') {
612                         $firstpagelink .= '.html';
613                 }
614                 $lastpagelink = $pagelink . $page_str . $totalpages;
615                 if ($page_str == 'page_') {
616                         $lastpagelink .= '.html';
617                 }
618
619                 if ($type >= 1) {
620                         $buf .= '<div class="pageswitch">' . "\n";
621 //                      $buf .= "<a rel=\"first\" title=\"first page\" href=\"{$firstpagelink}\">&lt;TOP&gt;</a> | \n";
622                         if (!empty($prevpage)) {
623                                 $prevpagelink = $pagelink . $page_str . $prevpage;
624                                 if ($page_str == 'page_') {
625                                         $prevpagelink .= '.html';
626                                 }
627                                 $buf .= '<a href="' . $prevpagelink . '" title="Previous page" rel="Prev">&laquo;Prev</a> |';
628                         } elseif ($type >= 2) {
629                                 $buf .= "&laquo;Prev |";
630                         }
631                         $buf .= "\n";
632                         if (intval($type) == 2) {
633                                 $sepstr = '&middot;';
634                                 $buf   .= "|";
635                                 for ($i=1; $i<=$totalpages; $i++) {
636                                         $i_pagelink = $pagelink . $page_str . $i;
637                                         if ($page_str == 'page_') {
638                                                 $i_pagelink .= '.html';
639                                         }
640                                         if ($i == $currentpage) {
641                                                 $buf .= ' <strong>' . $i . '</strong> |' . "\n";
642                                         } elseif ($totalpages<10 || $i<4 || $i>$totalpages-3) {
643                                                 $buf .= ' <a href="' . $i_pagelink . '" title="Page No.' . $i . '">'
644                                                           . $i . '</a> |' . "\n";
645                                         } else {
646                                                 if ($i<$currentpage-1 || $i>$currentpage+1) {
647                                                         if (($i == 4 && ($currentpage > 5 || $currentpage == 1)) || $i == $currentpage + 2) {
648                                                                 $buf = rtrim($buf);
649                                                                 $buf .= "...|\n";
650                                                         }
651                                                 } else {
652                                                         $buf .= ' <a href="' . $i_pagelink . '" title=\"Page No.' . $i . '">'
653                                                                   . $i . '</a> |' . "\n";
654                                                 }
655                                         }
656                                 }
657                                 $buf = rtrim($buf);
658                         }
659                         if (intval($type) == 3) {
660                                 $buf .= '|';
661                                 $sepstr = '&middot;';
662                                 for ($i = 1; $i <= $totalpages; $i++) {
663                                         $i_pagelink = $pagelink . $page_str . $i;
664                                         if ($page_str == 'page_') {
665                                                 $i_pagelink .= '.html';
666                                         }
667                                         $paging = 5;
668                                         if ($i == $currentpage) {
669                                                 $buf .= ' <strong>' . $i . '</strong> ' . $sepstr . "\n";
670                                         } elseif ($totalpages < 10 || ($i < ($currentpage + $paging) && ($currentpage - $paging) < $i)) {
671                                                 $buf .= ' <a href="' . $i_pagelink . '" title="Page No.' . $i . '">'
672                                                           . $i . '</a> ' . $sepstr . "\n";
673                                         } elseif ($currentpage - $paging == $i) {
674                                                 $buf = rtrim($buf);
675                                                 $buf .= ' ...'."\n";
676                                         } elseif ($currentpage + $paging == $i) {
677                                                 $buf = rtrim($buf);
678                                                 $buf = preg_replace('/$sepstr$/', '', $buf);
679                                                 $buf .= "... |\n";
680                                         }
681                                 }
682                         }
683                         if ($totalpages >= $nextpage) {
684                                 $nextpagelink = $pagelink . $page_str . $nextpage;
685                                 if ($page_str == 'page_') {
686                                         $nextpagelink .= '.html';
687                                 }
688                                 $buf .= '| <a href="' . $nextpagelink . '" title="Next page" rel="Next">Next&raquo;</a>' . "\n";
689                         } elseif ($type >= 2) {
690                                 $buf .= "| Next&raquo;\n";
691                         }
692 //                      $buf .= " | <a rel=\"last\" title=\"Last page\" href=\"{$lastpagelink}\">&lt;LAST&gt;</a>\n";
693                         $buf .= "</div>\n";
694                         return array('buf' => $buf, 'startpos' => intval($startpos));
695                 }
696         }
697
698         function _getSubcategoriesWhere($catid)
699         {
700                 global $manager;
701                 $mwhere = '';
702                 $mwhere = ' AND ((i.inumber = p.item_id'
703                                 . ' AND (p.categories REGEXP "(^|,)' . intval($catid) . '(,|$)"'
704                                 . ' OR i.icat = ' . intval($catid) . '))'
705                                 . ' OR (i.icat = ' . intval($catid)
706                                 . ' AND p.item_id IS NULL))';
707                 $mtable = ' LEFT JOIN ' . sql_table('plug_multiple_categories') . ' as p'
708                                 . ' ON i.inumber = p.item_id';
709                 $mplugin =& $manager->getPlugin('NP_MultipleCategories');
710                 if (method_exists($mplugin, 'getRequestName')) {
711                         $mplugin->event_PreSkinParse(array());
712                         global $subcatid;
713                         if ($subcatid) {
714
715                                 $mque = 'SELECT * FROM %s WHERE scatid = %d';
716                                 $tres = sql_query(sprintf($mque, sql_table('plug_multiple_categories_sub'), intval($subcatid)));
717 //                              $tres = sql_query('SELECT * FROM ' . sql_table('plug_multiple_categories_sub') .
718 //                                                              ' WHERE scatid = ' . intval($subcatid));
719                                 $ra = mysql_fetch_array($tres, MYSQL_ASSOC);
720                                 if (array_key_exists('parentid', $ra)) {
721                                         $Children = array();
722                                         $Children = explode('/', intval($subcatid) . $this->getChildren(intval($subcatid)));
723                                 }
724                                 if ($Children[1]) {
725                                         for ($i=0;$i<count($Children);$i++) {
726                                                 $temp_whr[] = ' p.subcategories REGEXP "(^|,)' . intval($Children[$i]) . '(,|$)" ';
727                                         }
728                                         $mwhere .= ' AND ( ';
729                                         $mwhere .= implode(' OR ', $temp_whr);
730                                         $mwhere .= ' )';
731                                 } else {
732                                         $mwhere .= ' AND p.subcategories REGEXP "(^|,)' . intval($subcatid) . '(,|$)"';
733                                 }
734                         }
735                 }
736                 return array(w => $mwhere, m => $mtable);
737         }
738
739         function getParents($subcat_id)
740         {
741                 $subcatTable = sql_table('plug_multiple_categories_sub');
742                 $que         = 'SELECT scatid, parentid, sname FROM %s WHERE scatid = %d';
743                 $que         = sprintf($que, $subcatTable, intval($subcat_id));
744                 $res         = sql_query($que);
745                 list($sid, $parent, $sname) = mysql_fetch_row($res);
746                 if ($parent != 0) {
747                         $r = $this->getParent(intval($parent)) . '/' . intval($sid);
748                 } else {
749                         $r = intval($sid);
750                 }
751                 return $r;
752         }
753
754         function getChildren($subcat_id)
755         {
756                 $subcatTable = sql_table('plug_multiple_categories_sub');
757                 $que         = 'SELECT scatid, parentid, sname FROM %s WHERE parentid = %d';
758                 $que         = sprintf($que, $subcatTable, intval($subcat_id));
759                 $res         = sql_query();
760                 while ($so =  mysql_fetch_object($res)) {
761                         $r .= $this->getChildren(intval($so->scatid)) . '/' . intval($so->scatid);
762                 }
763                 return $r;
764         }
765
766         function _getTagsInum($where, $skin_type, $bmode, $p_amount)
767         {
768                 global $manager, $itemid;
769                 $tagTable   =  sql_table('plug_tagex');
770                 $tplugin    =& $manager->getPlugin('NP_TagEX');
771                 $requestTag =  $tplugin->getNoDecodeQuery('tag');
772                 if (!empty($requestTag) || $skin_type == 'item') {
773                         $this->tagSelected = TRUE;
774                         if ($bmode == 'all') {
775                                 $allTags = $tplugin->scanExistTags(0);
776                         } else {
777                                 $allTags = $tplugin->scanExistTags(2);
778                         }
779                         $arr = $tplugin->splitRequestTags($requestTag);
780                         if ($skin_type == 'item') {
781                                 $item =& $manager->getItem(intval($itemid), 0, 0);
782                                 $q    =  'SELECT * FROM %s WHERE inum = %d';
783                                 $q    =  sprintf($q, $tagTable, intval($itemid));
784                                 $res  =  sql_query($q);
785                                 while ($o = mysql_fetch_object($res)) {
786                                         $temp_tags_array = preg_split('/[\n,]+/', trim($o->itags));
787                                         for ($i=0; $i < count($temp_tags_array); $i++) {
788                                                 $arr['or'][] = trim($temp_tags_array[$i]);
789                                         }
790                                 }
791                         }
792                         if ($skin_type != 'item') {
793                                 for ($i=0; $i < count($arr['and']); $i++) {
794                                         $deTag = $tplugin->_rawdecode($arr['and'][$i]);
795                                         if ($allTags[$deTag]) {
796                                                 if (empty($inumsand)) {
797                                                         $inumsand = $allTags[$deTag];
798                                                 } else {
799                                                         $inumsand = array_intersect($inumsand, $allTags[$deTag]);
800                                                 }
801                                         } else {
802                                                 $inumsand = array();
803                                         }
804                                         if (empty($inumsand)) {
805                                                 break;
806                                         }
807                                 }
808                                 if (!empty($inumsand)) {
809                                         $inumsres = array_values($inumsand);
810                                         unset($inumsand);
811                                 }
812                         }
813                         $inumsor = array();
814                         for ($i=0;$i<count($arr['or']);$i++) {
815                                 if ($skin_type == 'item') {
816                                         $deTag = $arr['or'][$i];
817                                 } else {
818                                         $deTag = $tplugin->_rawdecode($arr['or'][$i]);
819                                 if ($allTags[$deTag]) {
820                                         $inumsor = array_merge($inumsor, $allTags[$deTag]);
821                                 }
822                         }
823                         if ($inumsres && $inumsor) {
824                                 $inumsres = array_merge($inumsres, $inumsor);
825                                 $inumsres = array_unique($inumsres);
826                         } elseif (!$inumsres && $inumsor) {
827                                 $inumsres = array_unique($inumsor);
828                         }
829                         if ($inumsres) {
830                                 if ($skin_type == 'item') {
831                                         foreach ($inumsres as $resinum) {
832                                                 $iTags = array();
833                                                 $q   = 'SELECT itags FROM %s WHERE inum = %d';
834                                                 $q   = sprintf($q, $tagTable, intval($resinum));
835                                                 $res = sql_query($q);
836                                                 while ($o = mysql_fetch_object($res)) {
837                                                         $resTags = preg_split("/[\n,]+/", trim($o->itags));
838                                                         for ($i=0; $i < count($resTags); $i++) {
839                                                                 $iTags[] = trim($resTags[$i]);
840                                                         }
841                                                 }
842                                                         $relatedTags        = array_intersect($arr['or'], $iTags);
843                                                         $tagCount[$resinum] = count($relatedTags);
844                                         }
845                                         asort($tagCount);
846                                         $inumsres = array();
847                                         foreach ($tagCount as $resinum => $val) {
848                                                 $relatedInums[] = intval($resinum);
849                                         }
850                                         for ($i=0; $i < =$p_amount; $i++) {
851                                                 $inumsres[$i] = array_pop($relatedInums);
852                                         }
853                                 }
854                                 $where .= ' and i.inumber IN ('. @join(',', $inumsres) . ')';
855                         } else {
856                                 $where .= ' and i.inumber=0';
857                         }
858                 }
859                 $retArr = array(
860                                                 'where' => $where,
861                                                 'inumsres' => $inumsres
862                                            );
863                 return $retArr;
864         }
865
866 }
867
868 ?>