1: <?php
   2: 
   3:    4:    5:    6:    7:    8: 
   9:   10:   11:   12: 
  13: 
  14: define('ZENPAGE_COMBINEWS', false);
  15: define('ZP_COMBINEWS', false);
  16: 
  17: define('ZP_SHORTENINDICATOR', $shortenindicator = getOption('zenpage_textshorten_indicator'));
  18: define('ZP_SHORTEN_LENGTH', getOption('zenpage_text_length'));
  19: define('ZP_READ_MORE', getOption("zenpage_read_more"));
  20: define('ZP_ARTICLES_PER_PAGE', getOption("zenpage_articles_per_page"));
  21: if (!defined('MENU_TRUNCATE_STRING'))
  22:     define('MENU_TRUNCATE_STRING', getOption('menu_truncate_string'));
  23: if (!defined('MENU_TRUNCATE_INDICATOR'))
  24:     define('MENU_TRUNCATE_INDICATOR', getOption('menu_truncate_indicator'));
  25: if (!defined('ZP_NEWS_ENABLED')) {
  26:   if (getOption('enabled-zenpage-items') == 'news-and-pages' || getOption('enabled-zenpage-items') == 'news') {
  27:     define('ZP_NEWS_ENABLED', true);
  28:   } else {
  29:     define('ZP_NEWS_ENABLED', false);
  30:   }
  31: }
  32: if (!defined('ZP_PAGES_ENABLED')) {
  33:   if (getOption('enabled-zenpage-items') == 'news-and-pages' || getOption('enabled-zenpage-items') == 'pages') {
  34:     define('ZP_PAGES_ENABLED', true);
  35:   } else {
  36:     define('ZP_PAGES_ENABLED', false);
  37:   }
  38: }
  39: 
  40: class Zenpage {
  41: 
  42:     public $categoryStructure = array();
  43:     
  44:     protected $sortorder = 'date';
  45:     protected $sortdirection = true;
  46:     protected $sortSticky = true;
  47:     
  48:     protected $page_sortorder;
  49:     protected $page_sortdirection;
  50:       51:   52: 
  53:     function __construct() {
  54:         $allcategories = query_full_array("SELECT * FROM " . prefix('news_categories') . " ORDER by sort_order");
  55:         $this->categoryStructure = array();
  56:         foreach ($allcategories as $cat) {
  57:             $this->categoryStructure[$cat['id']] = $cat;
  58:         }
  59:     }
  60: 
  61:     static function expiry() {
  62:           63:   64:   65: 
  66:         $sql = ' WHERE `date`<="' . date('Y-m-d H:i:s') . '" AND `show`="1"' .
  67:                         ' AND `expiredate`<="' . date('Y-m-d H:i:s') . '"' .
  68:                         ' AND `expiredate`!="0000-00-00 00:00:00"' .
  69:                         ' AND `expiredate` IS NOT NULL';
  70:         foreach (array('news' => 'ZenpageNews', 'pages' => 'ZenpagePage') as $table => $class) {
  71:             $result = query_full_array('SELECT * FROM ' . prefix($table) . $sql);
  72:             if ($result) {
  73:                 foreach ($result as $item) {
  74:                     $obj = new $class($item['titlelink']);
  75:                     $obj->setShow(0);
  76:                     $obj->save();
  77:                 }
  78:             }
  79:         }
  80:     }
  81: 
  82:       83:   84:   85:   86: 
  87:     private function getCategoryStructure() {
  88:         return $this->categoryStructure;
  89:     }
  90: 
  91:     
  92:     
  93:     
  94: 
  95:     function visibleCategory($cat) {
  96:         return $this->categoryStructure[$cat['cat_id']]['show'];
  97:     }
  98: 
  99:      100:  101:  102:  103:  104:  105:  106:  107:  108:  109:  110: 
 111:     function getPages($published = NULL, $toplevel = false, $number = NULL, $sorttype = NULL, $sortdirection = NULL) {
 112:         global $_zp_loggedin;
 113:         if (is_null($sortdirection)) {
 114:             $sortdirection = $this->getSortDirection('pages');
 115:         }
 116:         if (is_null($sorttype)) {
 117:             $sorttype = $this->getSortType('pages');
 118:         }
 119:         if (is_null($published)) {
 120:             $published = !zp_loggedin();
 121:             $all = zp_loggedin(MANAGE_ALL_PAGES_RIGHTS);
 122:         } else {
 123:             $all = !$published;
 124:         }
 125:         $gettop = '';
 126:         if ($published) {
 127:             if ($toplevel)
 128:                 $gettop = " AND parentid IS NULL";
 129:             $show = " WHERE `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'" . $gettop;
 130:         } else {
 131:             if ($toplevel)
 132:                 $gettop = " WHERE parentid IS NULL";
 133:             $show = $gettop;
 134:         }
 135:         if ($sortdirection) {
 136:             $sortdir = ' DESC';
 137:         } else {
 138:             $sortdir = ' ASC';
 139:         }
 140:         switch ($sorttype) {
 141:             case 'date':
 142:                 $sortorder = 'date';
 143:                 break;
 144:             case 'lastchange':
 145:                 $sortorder = 'lastchange';
 146:                 break;
 147:             case 'title':
 148:                 $sortorder = 'title';
 149:                 break;
 150:             case 'id':
 151:                 $sortorder = 'id';
 152:                 break;
 153:             case 'popular':
 154:                 $sortorder = 'hitcounter';
 155:                 break;
 156:             case 'mostrated':
 157:                 $sortorder = 'total_votes';
 158:                 break;
 159:             case 'toprated':
 160:                 if (empty($sortdir))
 161:                     $sortdir = ' DESC';
 162:                 $sortorder = '(total_value/total_votes) ' . $sortdir . ', total_value';
 163:                 break;
 164:             case 'random':
 165:                 $sortorder = 'RAND()';
 166:                 $sortdir = '';
 167:                 break;
 168:             default:
 169:                 $sortorder = 'sort_order';
 170:                 break;
 171:         }
 172:         $all_pages = array(); 
 173:         $result = query('SELECT * FROM ' . prefix('pages') . $show . ' ORDER by `' . $sortorder . '`' . $sortdir);
 174:         if ($result) {
 175:             while ($row = db_fetch_assoc($result)) {
 176:                 if ($all || $row['show']) {
 177:                     $all_pages[] = $row;
 178:                 } else if ($_zp_loggedin) {
 179:                     $page = new ZenpagePage($row['titlelink']);
 180:                     if ($page->isMyItem(LIST_RIGHTS)) {
 181:                         $all_pages[] = $row;
 182:                         if ($number && count($result) >= $number) {
 183:                             break;
 184:                         }
 185:                     }
 186:                 }
 187:             }
 188:             db_free_result($result);
 189:         }
 190:         return $all_pages;
 191:     }
 192: 
 193:   194:  195:  196:  197:  198:  199: 
 200:   function getNotViewablePages() {
 201:     global $_zp_not_viewable_pages_list;
 202:     if (zp_loggedin(ADMIN_RIGHTS | ALL_PAGES_RIGHTS)) {
 203:       return array(); 
 204:     }
 205:     if (is_null($_zp_not_viewable_pages_list)) {
 206:       $items = $this->getPages(true, false, NULL, NULL, NULL);
 207:       if (!is_null($items)) {
 208:         $_zp_not_viewable_pages_list = array();
 209:         foreach ($items as $item) {
 210:           $obj = new ZenpageNews($item['titlelink']);
 211:           if (!$obj->isProtected()) {
 212:             $_zp_not_viewable_pages_list[] = $obj->getID();
 213:           }
 214:         }
 215:       }
 216:     }
 217:     return $_zp_not_viewable_pages_list;
 218:   }
 219: 
 220:     
 221:     
 222:     
 223: 
 224:      225:  226:  227:  228:  229:  230:  231:  232:  233:  234:  235:  236:  237:  238:  239:  240:  241:  242: 
 243:     function getArticles($articles_per_page = 0, $published = NULL, $ignorepagination = false, $sortorder = NULL, $sortdirection = NULL, $sticky = NULL, $category = NULL) {
 244:         global $_zp_current_category, $_zp_post_date, $_zp_newsCache;
 245:         if (empty($published)) {
 246:             if (zp_loggedin(ALL_NEWS_RIGHTS) || ($category && $category->isMyItem(ALL_NEWS_RIGHTS))) {
 247:                 $published = "all";
 248:             } else {
 249:                 $published = "published";
 250:             }
 251:         }
 252:         if ($category) {
 253:             $sortObj = $category;
 254:         } else {
 255:             $sortObj = $this;
 256:         }
 257:         if (is_null($sticky)) {
 258:             $sticky = $sortObj->getSortSticky();
 259:         }
 260: 
 261:         if (is_null($sortdirection)) {
 262:             $sortdirection = $sortObj->getSortDirection('news');
 263:         }
 264:         if (is_null($sortorder)) {
 265:             $sortorder = $sortObj->getSortType('news');
 266:         }
 267:     $newsCacheIndex = "$sortorder-$sortdirection-$published-" . (bool) $sticky;
 268:     if ($category) {
 269:             $newsCacheIndex .= '-' . $category->getTitlelink();
 270:         }
 271:         if (isset($_zp_newsCache[$newsCacheIndex])) {
 272:             $result = $_zp_newsCache[$newsCacheIndex];
 273:         } else {
 274:             $show = $currentcategory = false;
 275:             if ($category) {
 276:                 if (is_object($_zp_current_category)) {
 277:                     $currentcategory = $_zp_current_category->getTitlelink();
 278:                 }
 279:                 $showConjunction = ' AND ';
 280:                 
 281:                 $catid = $category->getID();
 282:                 $subcats = $category->getSubCategories();
 283:                 if ($subcats) {
 284:                     $cat = " (cat.cat_id = '" . $catid . "'";
 285:                     foreach ($subcats as $subcat) {
 286:                         $subcatobj = new ZenpageCategory($subcat);
 287:                         $cat .= "OR cat.cat_id = '" . $subcatobj->getID() . "' ";
 288:                     }
 289:                     $cat .= ") AND cat.news_id = news.id ";
 290:                 } else {
 291:                     $cat = " cat.cat_id = '" . $catid . "' AND cat.news_id = news.id ";
 292:                 }
 293:             } else {
 294:                 $showConjunction = ' WHERE ';
 295:             }
 296: 
 297:             if ($sticky) {
 298:                 $sticky = 'sticky DESC,';
 299:             }
 300:             if ($sortdirection) {
 301:                 $dir = " DESC";
 302:             } else {
 303:                 $dir = " ASC";
 304:             }
 305:             
 306:             switch ($sortorder) {
 307:                 case "date":
 308:                 default:
 309:                     $sort1 = "date" . $dir;
 310:                     break;
 311:                 case 'lastchange':
 312:                     $sort1 = 'lastchange' . $dir;
 313:                     break;
 314:                 case "id":
 315:                     $sort1 = "id" . $dir;
 316:                     break;
 317:                 case "title":
 318:                     $sort1 = "title" . $dir;
 319:                     break;
 320:                 case "popular":
 321:                     $sort1 = 'hitcounter' . $dir;
 322:                     break;
 323:                 case "mostrated":
 324:                     $sort1 = 'total_votes' . $dir;
 325:                     break;
 326:                 case "toprated":
 327:                     $sort1 = '(total_value/total_votes) DESC, total_value';
 328:                     break;
 329:                 case "random":
 330:                     $sort1 = 'RAND()';
 331:                     break;
 332:             }
 333: 
 334:             
 335:             switch ($published) {
 336:                 case "published":
 337:                     $show = "$showConjunction `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
 338:                     $getUnpublished = false;
 339:                     break;
 340:                 case "published-unpublished":
 341:                     $show = "$showConjunction `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
 342:                     $getUnpublished = true;
 343:                     break;
 344:                 case "unpublished":
 345:                     $show = "$showConjunction `show` = 0 AND date <= '" . date('Y-m-d H:i:s') . "'";
 346:                     $getUnpublished = true;
 347:                     break;
 348:                 case 'sticky':
 349:                     $show = "$showConjunction `sticky` <> 0";
 350:                     $getUnpublished = true;
 351:                     break;
 352:                 case "all":
 353:                     $getUnpublished = true;
 354:                     $show = false;
 355:                     break;
 356:             }
 357:             $order = " ORDER BY $sticky";
 358: 
 359:             if (in_context(ZP_ZENPAGE_NEWS_DATE)) {
 360:                 $datesearch = '';
 361:                 switch ($published) {
 362:                     case "published":
 363:                         $datesearch = "date LIKE '$_zp_post_date%' ";
 364:                         break;
 365:                     case "unpublished":
 366:                         $datesearch = "date LIKE '$_zp_post_date%' ";
 367:                         break;
 368:                     case "all":
 369:                         $datesearch = "date LIKE '$_zp_post_date%' ";
 370:                         break;
 371:                 }
 372:                 if ($datesearch) {
 373:                     if ($show) {
 374:                         $datesearch = ' AND ' . $datesearch;
 375:                     } else {
 376:                         $datesearch = ' WHERE ' . $datesearch;
 377:                     }
 378:                 }
 379:                 $order .= " date DESC";
 380:             } else {
 381:                 $datesearch = "";
 382:                 if ($category) {
 383:                     $order .= ' news.';
 384:                 } else {
 385:                     $order .= ' ';
 386:                 }
 387:                 $order .= $sort1;
 388:             }
 389:             if ($category) {
 390:                 $sql = "SELECT DISTINCT news.date, news.title, news.titlelink, news.sticky FROM " . prefix('news') . " as news, " . prefix('news2cat') . " as cat WHERE" . $cat . $show . $order;
 391:             } else {
 392:                 $sql = "SELECT date, title, titlelink, sticky FROM " . prefix('news') . $show . $datesearch . " " . $order;
 393:             }
 394:             $resource = query($sql);
 395:             $result = array();
 396:             if ($resource) {
 397:                 while ($item = db_fetch_assoc($resource)) {
 398:                     $article = new ZenpageNews($item['titlelink']);
 399:                     if ($getUnpublished || $article->isMyItem(LIST_RIGHTS) || $currentcategory && ($article->inNewsCategory($currentcategory)) || $article->categoryIsVisible()) {
 400:                         $result[] = $item;
 401:                     }
 402:                 }
 403:                 db_free_result($resource);
 404:                 if ($sortorder == 'title') { 
 405:                     $result = sortByMultilingual($result, 'title', $sortdirection);
 406:                     if ($sticky) {
 407:                         $stickyItems = array();
 408:                         foreach ($result as $key => $element) {
 409:                             if ($element['sticky']) {
 410:                                 array_unshift($stickyItems, $element);
 411:                                 unset($result[$key]);
 412:                             }
 413:                         }
 414:                         $stickyItems = sortMultiArray($stickyItems, 'sticky', true);
 415:                         $result = array_merge($stickyItems, $result);
 416:                     }
 417:                 }
 418:             }
 419:             $_zp_newsCache[$newsCacheIndex] = $result;
 420:         }
 421: 
 422:         if ($articles_per_page) {
 423:             if ($ignorepagination) {
 424:                 $offset = 0;
 425:             } else {
 426:                 $offset = self::getOffset($articles_per_page);
 427:             }
 428:             $result = array_slice($result, $offset, $articles_per_page);
 429:         }
 430:         return $result;
 431:     }
 432: 
 433:   434:  435:  436:  437:  438:  439: 
 440:   function getNotViewableNews() {
 441:     global $_zp_not_viewable_news_list;
 442:     if (zp_loggedin(ADMIN_RIGHTS | ALL_NEWS_RIGHTS)) {
 443:       return array(); 
 444:     }
 445:     if (is_null($_zp_not_viewable_news_list)) {
 446:       $items = $this->getArticles(0, 'published', true, NULL, NULL, NULL, NULL);
 447:       if (!is_null($items)) {
 448:         $_zp_not_viewable_news_list = array();
 449:         foreach ($items as $item) {
 450:           $obj = new ZenpageNews($item['titlelink']);
 451:           if ($obj->isProtected()) {
 452:             $_zp_not_viewable_news_list[] = $obj->getID();
 453:           }
 454:         }
 455:       }
 456:     }
 457:     return $_zp_not_viewable_news_list;
 458:   }
 459: 
 460:    461:  462:  463:  464:  465: 
 466:     function getArticle($index, $published = NULL, $sortorder = NULL, $sortdirection = NULL, $sticky = true) {
 467:         $articles = $this->getArticles(0, NULL, true, $sortorder, $sortdirection, $sticky);
 468:         if ($index >= 0 && $index < count($articles)) {
 469:             $article = $articles[$index];
 470:             $obj = new ZenpageNews($articles[$index]['titlelink']);
 471:             return $obj;
 472:         }
 473:         return false;
 474:     }
 475: 
 476:      477:  478:  479:  480:  481:  482: 
 483:     static function getOffset($articles_per_page, $ignorepagination = false) {
 484:         global $_zp_page, $subpage;
 485:         if (OFFSET_PATH) {
 486:             $page = $subpage + 1;
 487:         } else {
 488:             $page = $_zp_page;
 489:         }
 490:         if ($ignorepagination || is_null($page)) { 
 491:             $offset = 0;
 492:         } else {
 493:             $offset = ($page - 1) * $articles_per_page;
 494:         }
 495:         return $offset;
 496:     }
 497: 
 498:      499:  500:  501:  502: 
 503:     function getTotalArticles() {
 504:         global $_zp_current_category;
 505:         if (empty($_zp_current_category)) {
 506:             if (isset($_GET['category'])) {
 507:                 $cat = sanitize($_GET['category']);
 508:                 $catobj = new ZenpageCategory($cat);
 509:             } else {
 510:                 return count($this->getArticles(0));
 511:             }
 512:         } else {
 513:             $catobj = $_zp_current_category;
 514:         }
 515:         return count($catobj->getArticles());
 516:     }
 517: 
 518:      519:  520:  521:  522:  523: 
 524:     function getAllArticleDates($yearsonly = false, $order = 'desc') {
 525:         $alldates = array();
 526:         $cleandates = array();
 527:         $sql = "SELECT date FROM " . prefix('news');
 528:         if (!zp_loggedin(MANAGE_ALL_NEWS_RIGHTS)) {
 529:             $sql .= " WHERE `show` = 1";
 530:         }
 531:         $result = query_full_array($sql);
 532:         foreach ($result as $row) {
 533:             $alldates[] = $row['date'];
 534:         }
 535:         foreach ($alldates as $adate) {
 536:             if (!empty($adate)) {
 537:                 if ($yearsonly) {
 538:                     $cleandates[] = substr($adate, 0, 4);
 539:                 } else {
 540:                     $cleandates[] = substr($adate, 0, 7) . "-01";
 541:                 }
 542:             }
 543:         }
 544:         $datecount = array_count_values($cleandates);
 545:         switch ($order) {
 546:             case 'desc':
 547:             default:
 548:                 krsort($datecount);
 549:                 break;
 550:             case 'asc':
 551:                 ksort($datecount);
 552:                 break;
 553:         }
 554:         return $datecount;
 555:     }
 556: 
 557:      558:  559:  560:  561:  562:  563: 
 564:     private function siftResults($sql, $offset, $limit) {
 565:         $resource = $result = query($sql);
 566:         if ($resource) {
 567:             $result = array();
 568:             while ($item = db_fetch_assoc($resource)) {
 569:                 if ($item['type'] == 'news') {
 570:                     $article = new ZenpageNews($item['titlelink']);
 571:                     if (!$article->categoryIsVisible()) {
 572:                         continue;
 573:                     }
 574:                 }
 575:                 $offset--;
 576:                 if ($offset < 0) {
 577:                     $result[] = $item;
 578:                     if ($limit && count($result) >= $limit) {
 579:                         break;
 580:                     }
 581:                 }
 582:             }
 583:             db_free_result($resource);
 584:         }
 585:         return $result;
 586:     }
 587: 
 588:      589:  590:  591:  592:  593:  594:  595:  596:  597:  598:  599:  600:  601:  602:  603:  604:  605:  606:  607:  608:  609:  610:  611:  612:  613:  614:  615:  616: 
 617:     protected function getCombiNews($articles_per_page = '', $mode = '', $published = NULL, $sortorder = NULL, $sticky = true, $sortdirection = 'desc') {
 618:         global $_zp_combiNews_cache, $_zp_gallery;
 619: 
 620:         if (is_null($published)) {
 621:             if (zp_loggedin(ZENPAGE_NEWS_RIGHTS | ALL_NEWS_RIGHTS)) {
 622:                 $published = "all";
 623:             } else {
 624:                 $published = "published";
 625:             }
 626:         }
 627: 
 628:         if (empty($mode)) {
 629:             $mode = getOption('zenpage_combinews_mode');
 630:         }
 631: 
 632:         if (isset($_zp_combiNews_cache[$published . $mode . $sticky . $sortorder . $sortdirection])) {
 633:             return $_zp_combiNews_cache[$published . $mode . $sticky . $sortorder . $sortdirection];
 634:         }
 635: 
 636:         if ($published == "published") {
 637:             $show = " WHERE `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
 638:             $imagesshow = " AND images.show = 1 ";
 639:         } else {
 640:             $show = "";
 641:             $imagesshow = "";
 642:         }
 643:         getAllAccessibleAlbums($_zp_gallery, $albumlist, false);
 644:         if (empty($albumlist)) {
 645:             $albumWhere = 'albums.`id` is NULL';
 646:         } else {
 647:             $albumWhere = 'albums.`id` in (' . implode(',', $albumlist) . ')';
 648:         }
 649:         if ($articles_per_page) {
 650:             $offset = self::getOffset($articles_per_page);
 651:         } else {
 652:             $offset = 0;
 653:         }
 654:         if (empty($sortorder)) {
 655:             $combinews_sortorder = getOption("zenpage_combinews_sortorder");
 656:         } else {
 657:             $combinews_sortorder = $sortorder;
 658:         }
 659:         $stickyorder = '';
 660:         if ($sticky) {
 661:             $stickyorder = 'sticky DESC,';
 662:         }
 663:         switch (strtolower($sortdirection)) {
 664:             case 'desc':
 665:             default:
 666:                 $sortdir = 'DESC';
 667:                 break;
 668:             case 'asc':
 669:                 $sortdir = 'ASC';
 670:                 break;
 671:         }
 672:         $type3 = query("SET @type3:='0'");
 673:         switch ($mode) {
 674:             case "latestimages-thumbnail":
 675:             case "latestimages-thumbnail-customcrop":
 676:             case "latestimages-sizedimage":
 677:             case "latestimages-sizedimage-maxspace":
 678:             case "latestimages-fullimage":
 679:                 $albumWhere = ' AND ' . $albumWhere;
 680:                 $sortorder = $combinews_sortorder;
 681:                 $type1 = query("SET @type1:='news'");
 682:                 $type2 = query("SET @type2:='images'");
 683:                 switch ($combinews_sortorder) {
 684:                     case 'id':
 685:                     case 'date':
 686:                         $imagequery = "(SELECT albums.folder, images.filename, images.date, @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 687:                             WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 688:                         break;
 689:                     case 'publishdate':
 690:                         $imagequery = "(SELECT albums.folder, images.filename, IFNULL(images.publishdate,images.date), @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 691:                                                     WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 692:                     case 'mtime':
 693:                         $imagequery = "(SELECT albums.folder, images.filename, FROM_UNIXTIME(images.mtime), @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 694:                             WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 695:                         break;
 696:                 }
 697:                 $result = $this->siftResults("(SELECT title as albumname, titlelink, date, @type1 as type, sticky FROM " . prefix('news') . " " . $show . ")
 698:                                                                         UNION
 699:                                                                         " . $imagequery . "
 700:                                                                         ORDER BY $stickyorder date " . $sortdir, $offset, $articles_per_page);
 701:                 break;
 702:             case "latestalbums-thumbnail":
 703:             case "latestalbums-thumbnail-customcrop":
 704:             case "latestalbums-sizedimage":
 705:             case "latestalbums-sizedimage-maxspace":
 706:             case "latestalbums-fullimage":
 707:             default:
 708:                 if (empty($show)) {
 709:                     $albumWhere = ' WHERE ' . $albumWhere;
 710:                 } else {
 711:                     $albumWhere = ' AND ' . $albumWhere;
 712:                 }
 713:                 $sortorder = $combinews_sortorder;
 714:                 $type1 = query("SET @type1:='news'");
 715:                 $type2 = query("SET @type2:='albums'");
 716:                 switch ($combinews_sortorder) {
 717:                     case 'id':
 718:                     case 'date':
 719:                         $albumquery = "(SELECT albums.folder, albums.title, albums.date, @type2, @type3 as sticky FROM " . prefix('albums') . " AS albums
 720:                             " . $show . $albumWhere . ")";
 721:                         break;
 722:                     case 'publishdate':
 723:                         $albumquery = "(SELECT albums.folder, albums.title, IFNULL(albums.publishdate,albums.date), @type2, @type3 as sticky FROM " . prefix('albums') . " AS albums
 724:                                                     " . $show . $albumWhere . ")";
 725:                         break;
 726:                     case 'mtime':
 727:                     default:
 728:                         $albumquery = "(SELECT albums.folder, albums.title, FROM_UNIXTIME(albums.mtime), @type2, @type3 as sticky FROM " . prefix('albums') . " AS albums
 729:                             " . $show . $albumWhere . ")";
 730:                         break;
 731:                 }
 732:                 $result = $this->siftResults("(SELECT title as albumname, titlelink, date, @type1 as type, sticky FROM " . prefix('news') . " " . $show . ")
 733:                                                                         UNION
 734:                                                                         " . $albumquery . "
 735:                                                                         ORDER BY $stickyorder date " . $sortdir, $offset, $articles_per_page);
 736:                 break;
 737:             case "latestimagesbyalbum-thumbnail":
 738:             case "latestimagesbyalbum-thumbnail-customcrop":
 739:             case "latestimagesbyalbum-sizedimage":
 740:             case "latestimagesbyalbum-sizedimage-maxspace":
 741:             case "latestimagesbyalbum-fullimage":
 742:                 $albumWhere = ' AND ' . $albumWhere;
 743:                 $type1 = query("SET @type1:='news'");
 744:                 $type2 = query("SET @type2:='albums'");
 745:                 if (empty($combinews_sortorder) || $combinews_sortorder != "date" || $combinews_sortorder != "mtime" || $combinews_sortorder != "publishdate") {
 746:                     $combinews_sortorder = "date";
 747:                 }
 748:                 $sortorder = "images." . $combinews_sortorder;
 749:                 switch ($combinews_sortorder) {
 750:                     case "date":
 751:                         $imagequery = "(SELECT DISTINCT DATE_FORMAT(" . $sortorder . ",'%Y-%m-%d'), albums.folder, DATE_FORMAT(images.date,'%Y-%m-%d'), @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 752:                                                         WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 753:                         break;
 754:                     case "mtime":
 755:                         $imagequery = "(SELECT DISTINCT FROM_UNIXTIME(" . $sortorder . ",'%Y-%m-%d'), albums.folder, DATE_FORMAT(images.mtime,'%Y-%m-%d'), @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 756:                                                         WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 757:                     case "publishdate":
 758:                         $imagequery = "(SELECT DISTINCT FROM_UNIXTIME(" . $sortorder . ",'%Y-%m-%d'), albums.folder, DATE_FORMAT(images.publishdate,'%Y-%m-%d'), @type2, @type3 as sticky FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums
 759:                                                                                 WHERE albums.id = images.albumid " . $imagesshow . $albumWhere . ")";
 760:                         break;
 761:                 }
 762:                 $result = $this->siftResults("(SELECT title as albumname, titlelink, date, @type1 as type, sticky FROM " . prefix('news') . " " . $show . ")
 763:                                                                         UNION
 764:                                                                         " . $imagequery . "
 765:                                                                         ORDER By $stickyorder date " . $sortdir, $offset, $articles_per_page);
 766:                 break;
 767:             case "latestupdatedalbums-thumbnail":
 768:             case "latestupdatedalbums-thumbnail-customcrop":
 769:             case "latestupdatedalbums-sizedimage":
 770:             case "latestupdatedalbums-sizedimage-maxspace":
 771:             case "latestupdatedalbums-fullimage":
 772:                 $latest = $this->getArticles($articles_per_page, NULL, true, 'date', $sortdirection);
 773:                 $counter = '';
 774:                 foreach ($latest as $news) {
 775:                     $article = new ZenpageNews($news['titlelink']);
 776:                     if ($article->checkAccess()) {
 777:                         $counter++;
 778:                         $latestnews[$counter] = array(
 779:                                         "albumname"  => $article->getTitle(),
 780:                                         "titlelink"  => $article->getTitlelink(),
 781:                                         "date"           => $article->getDateTime(),
 782:                                         "type"           => "news",
 783:                         );
 784:                     }
 785:                 }
 786:                 $albums = getAlbumStatistic($articles_per_page, "latestupdated", '', $sortdirection);
 787:                 $latestalbums = array();
 788:                 $counter = "";
 789:                 foreach ($albums as $album) {
 790:                     $counter++;
 791:                     $tempalbum = $album;
 792:                     $tempalbumthumb = $tempalbum->getAlbumThumbImage();
 793:                     $timestamp = $tempalbum->get('mtime');
 794:                     if ($timestamp == 0) {
 795:                         $albumdate = $tempalbum->getDateTime();
 796:                     } else {
 797:                         $albumdate = strftime('%Y-%m-%d %H:%M:%S', $timestamp);
 798:                     }
 799:                     $latestalbums[$counter] = array(
 800:                                     "albumname"  => $tempalbum->getFileName(),
 801:                                     "titlelink"  => $tempalbum->getTitle(),
 802:                                     "date"           => $albumdate,
 803:                                     "type"           => 'albums',
 804:                     );
 805:                 }
 806:                 
 807:                 $latest = array_merge($latestnews, $latestalbums);
 808:                 $result = sortMultiArray($latest, "date", $sortdirection != 'asc');
 809:                 if (count($result) > $articles_per_page) {
 810:                     $result = array_slice($result, 0, $articles_per_page);
 811:                 }
 812:                 break;
 813:         }
 814:         $_zp_combiNews_cache[$published . $mode . $sticky . $sortorder . $sortdirection] = $result;
 815:         return $result;
 816:     }
 817: 
 818:      819:  820:  821:  822: 
 823:     function getNewsIndexURL() {
 824:         Zenpage_internal_deprecations::getNewsIndexURL();
 825:         return getNewsIndexURL();
 826:     }
 827: 
 828:      829:  830:  831:  832:  833: 
 834:     function getNewsCategoryPath($category, $page = NULL) {
 835:         Zenpage_internal_deprecations::getNewsCategoryPath();
 836:         $rewrite = '/' . _CATEGORY_ . '/' . $category;
 837:         $plain = "/index.php?p=news&category=$category";
 838:         if ($page > 1) {
 839:             $rewrite .='/' . $page;
 840:             $plain .= '&page=' . $page;
 841:         }
 842:         return rewrite_path($rewrite, $plain); 
 843:     }
 844: 
 845:      846:  847:  848:  849:  850: 
 851:     function getNewsArchivePath($date, $page = NULL) {
 852:         Zenpage_internal_deprecations::getNewsArchivePath();
 853:         return getNewsArchivePath($date, $page);
 854:     }
 855: 
 856:      857:  858:  859:  860:  861: 
 862:     function getNewsTitlePath($title) {
 863:         Zenpage_internal_deprecations::getNewsTitlePath();
 864:         return rewrite_path(_NEWS_ . "/$title", "/index.php?p=news&title=$title"); 
 865:     }
 866: 
 867:     
 868:     
 869:     
 870: 
 871:      872:  873:  874:  875:  876:  877: 
 878:     function getCategoryLink($catname) {
 879:         Zenpage_internal_deprecations::getCategoryLink();
 880:         foreach ($this->getAllCategories(false) as $cat) {
 881:             if ($cat['titlelink'] == $catname) {
 882:                 return $cat['title'];
 883:             }
 884:         }
 885:     }
 886: 
 887:      888:  889:  890:  891:  892: 
 893:     function getCategory($id) {
 894:         foreach ($this->getAllCategories(false) as $cat) {
 895:             if ($cat['id'] == $id) {
 896:                 return $cat;
 897:             }
 898:         }
 899:         return '';
 900:     }
 901: 
 902:      903:  904:  905:  906:  907:  908: 
 909:     function getAllCategories($visible = true, $sorttype = NULL, $sortdirection = NULL) {
 910:         $structure = $this->getCategoryStructure();
 911:         if (is_null($sortdirection))
 912:             $sortdirection = $this->sortdirection;
 913: 
 914:         switch ($sorttype) {
 915:             case "id":
 916:                 $sortorder = "id";
 917:                 break;
 918:             case "title":
 919:                 $sortorder = "title";
 920:                 break;
 921:             case "popular":
 922:                 $sortorder = 'hitcounter';
 923:                 break;
 924:             case "random":
 925:                 $sortorder = 'random';
 926:                 break;
 927:             default:
 928:                 $sortorder = "sort_order";
 929:                 break;
 930:         }
 931:         if ($visible) {
 932:             foreach ($structure as $key => $cat) {
 933:                 $catobj = new ZenpageCategory($cat['titlelink']);
 934:                 if ($catobj->getShow() || $catobj->isMyItem(LIST_RIGHTS)) {
 935:                     $structure[$key]['show'] = 1;
 936:                 } else {
 937:                     unset($structure[$key]);
 938:                 }
 939:             }
 940:         }
 941:         if (!is_null($sorttype) || !is_null($sortdirection)) {
 942:             if ($sorttype == 'random') {
 943:                 shuffle($structure);
 944:             } else {
 945:      
 946:      if($sortdirection) {
 947:        $sortdir = false;
 948:      } else {
 949:        $sortdir = true;
 950:      }
 951:                 $structure = sortMultiArray($structure, $sortorder, $sortdir, true, false, false);
 952:             }
 953:         }
 954:         return $structure;
 955:     }
 956: 
 957:      958:  959:  960:  961: 
 962:     public function __toString() {
 963:         return 'Zenpage';
 964:     }
 965: 
 966:     function getSortDirection($what = 'news') {
 967:         if ($what == 'pages') {
 968:             return $this->page_sortdirection;
 969:         } else {
 970:             return $this->sortdirection;
 971:         }
 972:     }
 973: 
 974:     function setSortDirection($value, $what = 'news') {
 975:         if ($what == 'pages') {
 976:             $this->page_sortdirection = (int) ($value && true);
 977:         } else {
 978:             $this->sortdirection = (int) ($value && true);
 979:         }
 980:     }
 981: 
 982:     function getSortType($what = 'news') {
 983:         if ($what == 'pages') {
 984:             return $this->page_sortorder;
 985:         } else {
 986:             return $this->sortorder;
 987:         }
 988:     }
 989: 
 990:     function setSortType($value, $what = 'news') {
 991:         if ($what == 'pages') {
 992:             $this->page_sortorder = $value;
 993:         } else {
 994:             $this->sortorder = $value;
 995:         }
 996:     }
 997: 
 998:     function getSortSticky() {
 999:         return $this->sortSticky;
1000:     }
1001: 
1002:     function setSortSticky($value) {
1003:         $this->sortSticky = (bool) $value;
1004:     }
1005: 
1006: }
1007: 
1008: 
1009: 
1010: 1011: 1012: 1013: 1014: 
1015: class ZenpageRoot extends ThemeObject {
1016: 
1017:     protected $sortorder;
1018:     protected $sortdirection;
1019:     protected $sortSticky = true;
1020: 
1021:     1022: 1023: 1024: 1025: 
1026:     function getPermalink() {
1027:         return $this->get("permalink");
1028:     }
1029: 
1030:     1031: 1032: 
1033: 
1034:     function setPermalink($v) {
1035:         $this->set('permalink', $v);
1036:     }
1037: 
1038:     1039: 1040: 1041: 1042: 
1043:     function getTitlelink() {
1044:         return $this->get("titlelink");
1045:     }
1046: 
1047:     1048: 1049: 1050: 
1051:     function setTitlelink($v) {
1052:         $this->set("titlelink", $v);
1053:     }
1054: 
1055: }
1056: 
1057: 
1058: 
1059: 1060: 1061: 1062: 1063: 
1064: class ZenpageItems extends ZenpageRoot {
1065: 
1066:     1067: 1068: 
1069:     function __construct() {
1070:         
1071:     }
1072: 
1073:     1074: 1075: 1076: 1077: 
1078:     function getAuthor() {
1079:         return $this->get("author");
1080:     }
1081: 
1082:     1083: 1084: 1085: 1086: 
1087:     function setAuthor($a) {
1088:         $this->set("author", $a);
1089:     }
1090: 
1091:     1092: 1093: 1094: 1095: 
1096:     function getContent($locale = NULL) {
1097:         $text = $this->get("content");
1098:         if ($locale == 'all') {
1099:             return zpFunctions::unTagURLs($text);
1100:         } else {
1101:             return applyMacros(zpFunctions::unTagURLs(get_language_string($text, $locale)));
1102:         }
1103:     }
1104: 
1105:     1106: 1107: 1108: 1109: 
1110:     function setContent($c) {
1111:         $c = zpFunctions::tagURLs($c);
1112:         $this->set("content", $c);
1113:     }
1114: 
1115:     1116: 1117: 1118: 1119: 
1120:     function getLastchange() {
1121:         return $this->get("lastchange");
1122:     }
1123: 
1124:     1125: 1126: 1127: 
1128:     function setLastchange($d) {
1129:         if ($d) {
1130:             $newtime = dateTimeConvert($d);
1131:             if ($newtime === false)
1132:                 return;
1133:             $this->set('lastchange', $newtime);
1134:         } else {
1135:             $this->set('lastchange', NULL);
1136:         }
1137:     }
1138: 
1139:     1140: 1141: 1142: 1143: 
1144:     function getLastchangeAuthor() {
1145:         return $this->get("lastchangeauthor");
1146:     }
1147: 
1148:     1149: 1150: 1151: 
1152:     function setLastchangeAuthor($a) {
1153:         $this->set("lastchangeauthor", $a);
1154:     }
1155: 
1156:     1157: 1158: 1159: 1160: 
1161:     function getLocked() {
1162:         return $this->get("locked");
1163:     }
1164: 
1165:     1166: 1167: 1168: 
1169:     function setLocked($l) {
1170:         $this->set("locked", $l);
1171:     }
1172: 
1173:     1174: 1175: 1176: 1177: 
1178:     function getExtraContent($locale = NULL) {
1179:         $text = $this->get("extracontent");
1180:         if ($locale == 'all') {
1181:             return zpFunctions::unTagURLs($text);
1182:         } else {
1183:             return applyMacros(zpFunctions::unTagURLs(get_language_string($text, $locale)));
1184:         }
1185:     }
1186: 
1187:     1188: 1189: 1190: 
1191:     function setExtraContent($ec) {
1192:         $this->set("extracontent", zpFunctions::tagURLs($ec));
1193:     }
1194: 
1195:     1196: 1197: 1198: 1199: 
1200:     function getExpireDate() {
1201:         $dt = $this->get("expiredate");
1202:         if ($dt == '0000-00-00 00:00:00') {
1203:             return NULL;
1204:         } else {
1205:             return $dt;
1206:         }
1207:     }
1208: 
1209:     1210: 1211: 1212: 
1213:     function setExpireDate($ed) {
1214:         if ($ed) {
1215:             $newtime = dateTimeConvert($ed);
1216:             if ($newtime === false)
1217:                 return;
1218:             $this->set('expiredate', $newtime);
1219:         } else {
1220:             $this->set('expiredate', NULL);
1221:         }
1222:     }
1223: 
1224: }
1225: 
1226: ?>