1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 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: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 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: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 895: 896: 897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911: 912: 913: 914: 915: 916: 917: 918: 919: 920: 921: 922: 923: 924: 925: 926: 927: 928: 929: 930: 931: 932: 933: 934: 935: 936: 937: 938: 939: 940: 941: 942: 943: 944: 945: 946: 947: 948: 949: 950: 951: 952: 953: 954: 955: 956: 957: 958: 959: 960: 961: 962: 963: 964: 965: 966: 967: 968: 969: 970: 971: 972: 973: 974: 975: 976: 977: 978: 979: 980: 981: 982: 983: 984: 985: 986: 987: 988: 989: 990: 991: 992: 993: 994: 995: 996: 997: 998: 999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198: 1199: 1200: 1201: 1202: 1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286: 1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317: 1318: 1319: 1320: 1321: 1322: 1323: 1324: 1325: 1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360: 1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370: 1371: 1372: 1373: 1374: 1375: 1376: 1377: 1378: 1379: 1380: 1381: 1382: 1383: 1384: 1385: 1386: 1387: 1388: 1389: 1390: 1391: 1392: 1393: 1394: 1395: 1396: 1397: 1398: 1399: 1400: 1401: 1402: 1403: 1404: 1405: 1406: 1407: 1408: 1409: 1410: 1411: 1412: 1413: 1414: 1415: 1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423: 1424: 1425: 1426: 1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434: 1435: 1436: 1437: 1438: 1439: 1440: 1441: 1442: 1443: 1444: 1445: 1446: 1447: 1448: 1449: 1450: 1451: 1452: 1453: 1454: 1455: 1456: 1457: 1458: 1459: 1460: 1461: 1462: 1463: 1464: 1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474: 1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482: 1483: 1484: 1485: 1486: 1487: 1488: 1489: 1490: 1491: 1492: 1493: 1494: 1495: 1496: 1497: 1498: 1499: 1500: 1501: 1502: 1503: 1504: 1505: 1506: 1507: 1508: 1509: 1510: 1511: 1512: 1513: 1514: 1515: 1516: 1517: 1518: 1519: 1520: 1521: 1522: 1523: 1524: 1525: 1526: 1527: 1528: 1529: 1530: 1531: 1532: 1533: 1534: 1535: 1536: 1537: 1538: 1539: 1540: 1541: 1542: 1543: 1544: 1545: 1546: 1547: 1548: 1549: 1550: 1551: 1552: 1553: 1554: 1555: 1556: 1557: 1558: 1559: 1560: 1561: 1562: 1563: 1564: 1565: 1566: 1567: 1568: 1569: 1570: 1571: 1572: 1573: 1574: 1575: 1576: 1577: 1578: 1579: 1580: 1581: 1582: 1583: 1584: 1585: 1586: 1587: 1588: 1589: 1590: 1591: 1592: 1593: 1594: 1595: 1596: 1597: 1598: 1599: 1600: 1601: 1602: 1603: 1604: 1605: 1606: 1607: 1608: 1609: 1610: 1611: 1612: 1613: 1614: 1615: 1616: 1617: 1618: 1619: 1620: 1621: 1622: 1623: 1624: 1625: 1626: 1627: 1628: 1629: 1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640: 1641: 1642: 1643: 1644: 1645: 1646: 1647: 1648: 1649: 1650: 1651: 1652: 1653: 1654: 1655: 1656: 1657: 1658: 1659: 1660: 1661: 1662: 1663: 1664: 1665: 1666: 1667: 1668: 1669: 1670: 1671: 1672: 1673: 1674: 1675: 1676: 1677: 1678: 1679: 1680: 1681: 1682: 1683: 1684: 1685: 1686: 1687: 1688: 1689: 1690: 1691: 1692: 1693: 1694: 1695: 1696: 1697: 1698: 1699: 1700: 1701: 1702: 1703: 1704: 1705: 1706: 1707: 1708: 1709: 1710: 1711: 1712: 1713: 1714: 1715: 1716: 1717: 1718: 1719: 1720: 1721: 1722: 1723: 1724: 1725: 1726: 1727: 1728: 1729: 1730: 1731: 1732: 1733: 1734: 1735: 1736: 1737: 1738: 1739: 1740: 1741: 1742: 1743: 1744: 1745: 1746: 1747: 1748: 1749: 1750: 1751: 1752: 1753: 1754: 1755: 1756: 1757: 1758: 1759: 1760: 1761: 1762: 1763: 1764: 1765: 1766: 1767: 1768: 1769: 1770: 1771: 1772: 1773: 1774: 1775: 1776: 1777: 1778: 1779: 1780: 1781: 1782: 1783: 1784: 1785: 1786: 1787: 1788: 1789: 1790: 1791: 1792: 1793: 1794: 1795: 1796: 1797: 1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805: 1806: 1807: 1808: 1809: 1810: 1811: 1812: 1813: 1814: 1815: 1816: 1817: 1818: 1819: 1820: 1821: 1822: 1823: 1824: 1825: 1826: 1827: 1828: 1829: 1830: 1831: 1832: 1833: 1834: 1835: 1836: 1837: 1838: 1839: 1840: 1841: 1842: 1843: 1844: 1845: 1846: 1847: 1848: 1849: 1850: 1851: 1852: 1853: 1854: 1855: 1856: 1857: 1858: 1859: 1860: 1861: 1862: 1863: 1864: 1865: 1866: 1867: 1868: 1869: 1870: 1871: 1872: 1873: 1874: 1875: 1876: 1877: 1878: 1879: 1880: 1881: 1882: 1883: 1884: 1885: 1886: 1887: 1888: 1889: 1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897: 1898: 1899: 1900: 1901: 1902: 1903: 1904: 1905: 1906: 1907: 1908: 1909: 1910: 1911: 1912: 1913: 1914: 1915: 1916: 1917: 1918: 1919: 1920: 1921: 1922: 1923: 1924: 1925: 1926: 1927: 1928: 1929: 1930: 1931: 1932: 1933: 1934: 1935: 1936: 1937: 1938: 1939: 1940: 1941: 1942: 1943: 1944: 1945: 1946: 1947: 1948: 1949: 1950: 1951: 1952: 1953: 1954: 1955: 1956: 1957: 1958: 1959: 1960: 1961: 1962: 1963: 1964: 1965: 1966: 1967: 1968: 1969: 1970: 1971: 1972: 1973: 1974: 1975: 1976: 1977: 1978: 1979: 1980: 1981: 1982: 1983: 1984: 1985: 1986: 1987: 1988: 1989: 1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003: 2004: 2005: 2006: 2007: 2008: 2009: 2010: 2011: 2012: 2013: 2014: 2015: 2016: 2017: 2018: 2019: 2020: 2021: 2022: 2023: 2024: 2025: 2026: 2027: 2028: 2029: 2030:
<?php
define('EXACT_TAG_MATCH', getOption('exact_tag_match'));
define('SEARCH_DURATION', 3000);
define('SEARCH_CACHE_DURATION', getOption('search_cache_duration'));
class SearchEngine {
public $fieldList = NULL;
public $page = 1;
public $images = NULL;
public $albums = NULL;
public $articles = NULL;
public $pages = NULL;
public $pattern;
public $tagPattern;
private $exact = false;
protected $dynalbumname = NULL;
protected $album = NULL;
protected $words;
protected $dates;
protected $whichdates = 'date';
protected $search_no_albums = false;
protected $search_no_images = false;
protected $search_no_pages = false;
protected $search_no_news = false;
protected $search_unpublished = false;
protected $search_structure;
protected $iteration = 0;
protected $processed_search = NULL;
protected $album_list = NULL;
protected $album_list_exclude = null;
protected $category_list = NULL;
protected $searches = NULL;
protected $extraparams = array();
public $loaded = false;
public $table = 'albums';
public $transient = true;
function __construct($dynamic_album = false) {
global $_zp_exifvars, $_zp_gallery;
switch ((int) getOption('exact_tag_match')) {
case 0:
$this->tagPattern = array('type' => 'like', 'open' => '%', 'close' => '%');
break;
case 1:
$this->tagPattern = array('type' => '=', 'open' => '', 'close' => '');
break;
case 2:
$this->tagPattern = array('type' => 'regexp', 'open' => '[[:<:]]', 'close' => '[[:>:]]');
break;
}
switch ((int) getOption('exact_string_match')) {
case 0:
$this->pattern = array('type' => 'like', 'open' => '%', 'close' => '%');
break;
case 1:
$this->pattern = array('type' => 'regexp', 'open' => '[[:<:]]', 'close' => '');
break;
case 2:
$this->pattern = array('type' => 'regexp', 'open' => '[[:<:]]', 'close' => '[[:>:]]');
break;
}
$this->extraparams['albumssorttype'] = getOption('search_album_sort_type');
$this->extraparams['albumssortdirection'] = getOption('search_album_sort_direction') ? 'DESC' : '';
$this->extraparams['imagessorttype'] = getOption('search_image_sort_type');
$this->extraparams['imagessortdirection'] = getOption('search_image_sort_direction') ? 'DESC' : '';
$this->extraparams['newssorttype'] = getOption('search_newsarticle_sort_type');
$this->extraparams['newssortdirection'] = getOption('search_newsarticle_sort_direction') ? 'DESC' : '';
$this->extraparams['pagessorttype'] = getOption('search_page_sort_type');
$this->extraparams['pagessortdirection'] = getOption('search_page_sort_direction') ? 'DESC' : '';
$this->search_structure['title'] = gettext('Title');
$this->search_structure['desc'] = gettext('Description');
$this->search_structure['tags'] = gettext('Tags');
$this->search_structure['tags_exact'] = '';
$this->search_structure['filename'] = gettext('File/Folder name');
$this->search_structure['date'] = gettext('Date');
$this->search_structure['custom_data'] = gettext('Custom data');
$this->search_structure['location'] = gettext('Location/Place');
$this->search_structure['city'] = gettext('City');
$this->search_structure['state'] = gettext('State');
$this->search_structure['country'] = gettext('Country');
$this->search_structure['copyright'] = gettext('Copyright');
$this->search_structure['owner'] = gettext('Owner');
$this->search_structure['credit'] = gettext('Credit');
$this->search_structure['lastchangeuser'] = gettext('Last change user');
if (extensionEnabled('zenpage') && !$dynamic_album) {
$this->search_structure['content'] = gettext('Content');
$this->search_structure['extracontent'] = gettext('ExtraContent');
$this->search_structure['author'] = gettext('Author');
$this->search_structure['titlelink'] = gettext('TitleLink');
$this->search_structure['news_categories'] = gettext('Categories');
}
foreach ($_zp_exifvars as $field => $row) {
if ($row[4] && $row[5]) {
$this->search_structure[strtolower($field)] = $row[2];
}
}
$this->search_structure = zp_apply_filter('searchable_fields', $this->search_structure);
if (isset($_REQUEST['words'])) {
$this->words = removeTrailingSlash(strtr(sanitize($_REQUEST['words'], 4), array('__23__' => '#', '__25__' => '%', '__26__' => '&', '__2F__' => '/')));
} else {
$this->words = NULL;
if (isset($_REQUEST['date'])) {
$this->dates = removeTrailingSlash(sanitize($_REQUEST['date'], 3));
if (isset($_REQUEST['whichdate'])) {
$this->whichdates = sanitize($_REQUEST['whichdate']);
}
} else {
$this->dates = NULL;
}
}
$this->fieldList = $this->parseQueryFields();
if (isset($_REQUEST['inalbums'])) {
$list = trim(sanitize($_REQUEST['inalbums'], 3));
if (strlen($list) > 0) {
switch ($list) {
case "0":
$this->search_no_albums = true;
setOption('search_no_albums', 1, false);
break;
case "1":
$this->search_no_albums = false;
setOption('search_no_albums', 0, false);
break;
default:
$this->album_list = explode(',', $list);
break;
}
}
}
if (isset($_REQUEST['excludealbums'])) {
$list = trim(sanitize($_REQUEST['excludealbums'], 3));
if (!empty($list)) {
$this->album_list_exclude = explode(',', $list);
}
}
if (isset($_REQUEST['inimages'])) {
$list = trim(sanitize($_REQUEST['inimages'], 3));
if (strlen($list) > 0) {
switch ($list) {
case "0":
$this->search_no_images = true;
setOption('search_no_images', 1, false);
break;
case "1":
$this->search_no_images = false;
setOption('search_no_images', 0, false);
break;
}
}
}
if (isset($_REQUEST['inpages'])) {
$list = trim(sanitize($_REQUEST['inpages'], 3));
if (strlen($list) > 0) {
switch ($list) {
case "0":
$this->search_no_pages = true;
setOption('search_no_pages', 1, false);
break;
}
}
}
if (isset($_REQUEST['innews'])) {
$list = trim(sanitize($_REQUEST['innews'], 3));
if (strlen($list) > 0) {
switch ($list) {
case "0":
$this->search_no_news = true;
setOption('search_no_news', 1, false);
break;
case "1":
break;
default:
$this->category_list = explode(',', $list);
break;
}
}
}
$this->images = NULL;
$this->albums = NULL;
$this->searches = array('images' => NULL, 'albums' => NULL, 'pages' => NULL, 'news' => NULL);
zp_apply_filter('search_instantiate', $this);
}
function getID() {
return 0;
}
function getSearchFieldList() {
$list = array();
foreach ($this->search_structure as $key => $display) {
if ($display) {
$list[$display] = $key;
}
}
return $list;
}
function allowedSearchFields() {
$setlist = array();
$fields = strtolower(getOption('search_fields'));
if (is_numeric($fields)) {
$setlist = $this->numericFields($fields);
} else {
$list = explode(',', $fields);
foreach ($this->search_structure as $key => $display) {
if (in_array($key, $list)) {
$setlist[$display] = $key;
}
}
}
return $setlist;
}
protected function numericFields($fields) {
if ($fields == 0)
$fields = 0x0fff;
if ($fields & 0x01)
$list[$this->search_structure['title']] = 'title';
if ($fields & 0x02)
$list[$this->search_structure['desc']] = 'desc';
if ($fields & 0x04)
$list[$this->search_structure['tags']] = 'tags';
if ($fields & 0x08)
$list[$this->search_structure['filename']] = 'filename';
return $list;
}
function getSearchParams($long = true) {
global $_zp_page;
$r = '';
$w = urlencode(trim($this->codifySearchString()));
if (!empty($w)) {
$r .= '&words=' . $w;
}
$d = trim($this->dates);
if (!empty($d)) {
$r .= '&date=' . $d;
$d = trim($this->whichdates);
if ($d != 'date') {
$r.= '&whichdates=' . $d;
}
}
$r .= $this->getSearchFieldsText($this->fieldList);
if ($long) {
$a = $this->dynalbumname;
if ($a) {
$r .= '&albumname=' . $a;
}
if (empty($this->album_list)) {
if ($this->search_no_albums) {
$r .= '&inalbums=0';
}
} else {
$r .= '&inalbums=' . implode(',', $this->album_list);
}
if (empty($this->album_list_exclude)) {
if ($this->search_no_albums) {
$r .= '&inalbums=0';
}
} else {
$r .= '&excludealbums=' . implode(',', $this->album_list_exclude);
}
if ($this->search_no_images) {
$r .= '&inimages=0';
}
if ($this->search_no_pages) {
$r .= '&inpages=0';
}
if (empty($this->categories)) {
if ($this->search_no_news) {
$r .= '&innews=0';
}
} else {
$r .= '&innews=' . implode(',', $this->categories);
}
if ($_zp_page > 1) {
$this->page = $_zp_page;
$r .= '&page=' . $_zp_page;
}
}
foreach ($this->extraparams as $p => $v) {
$r .= '&' . $p . '=' . $v;
}
return $r;
}
function getSearchExtra() {
return $this->extraparams;
}
function setSearchExtra($extra) {
$this->extraparams = $extra;
}
function setSortDirection($val, $what = 'images') {
if ($val) {
$this->extraparams[$what . 'sortdirection'] = 'DESC';
} else {
$this->extraparams[$what . 'sortdirection'] = 'ASC';
}
}
function setSortType($sorttype, $what = 'images') {
$this->extraparams[$what . 'sorttype'] = $sorttype;
}
function getSearchFieldsText($fields, $param = '&searchfields=') {
$default = $this->allowedSearchFields();
$diff = array_diff($default, $fields);
if (count($diff) > 0) {
foreach ($fields as $field) {
$param .= $field . ',';
}
return substr($param, 0, -1);
}
return '';
}
function setSearchParams($paramstr) {
$params = explode('&', $paramstr);
foreach ($params as $param) {
$e = strpos($param, '=');
$p = substr($param, 0, $e);
$v = substr($param, $e + 1);
switch ($p) {
case 'words':
$this->words = urldecode($v);
break;
case 'date':
$this->dates = $v;
break;
case 'whichdates':
$this->whichdates = $v;
break;
case 'searchfields':
if (is_numeric($v)) {
$this->fieldList = $this->numericFields($v);
} else {
$this->fieldList = array();
$list = explode(',', strtolower($v));
foreach ($this->search_structure as $key => $row) {
if (in_array(strtolower($key), $list)) {
$this->fieldList[] = $key;
}
}
}
break;
case 'page':
$this->page = $v;
break;
case 'albumname':
$alb = newAlbum($v, true, true);
if ($alb->loaded) {
$this->album = $alb;
$this->dynalbumname = $v;
$this->setSortType($this->album->getSortType('album'), 'albums');
$this->setSortDirection($this->album->getSortDirection('album'), 'albums');
$this->setSortType($this->album->getSortType(), 'images');
$this->setSortDirection($this->album->getSortDirection('image'), 'images');
}
break;
case 'inimages':
if (strlen($v) > 0) {
switch ($v) {
case "0":
$this->search_no_images = true;
setOption('search_no_images', 1, false);
break;
case "1":
$this->search_no_images = false;
setOption('search_no_images', 0, false);
break;
}
}
break;
case 'inalbums':
if (strlen($v) > 0) {
switch ($v) {
case "0":
$this->search_no_albums = true;
setOption('search_no_albums', 1, false);
break;
case "1":
$this->search_no_albums = false;
setOption('search_no_albums', 0, false);
break;
default:
$this->album_list = explode(',', $v);
break;
}
}
break;
case 'excludealbums':
if (strlen($v) > 0) {
$this->album_list_exclude = explode(',', $v);
}
break;
case 'unpublished':
$this->search_unpublished = (bool) $v;
break;
default:
$this->extraparams[$p] = $v;
break;
}
}
if (!empty($this->words)) {
$this->dates = '';
}
}
function setSearchUnpublished() {
$this->search_unpublished = true;
}
function getSearchWords() {
return $this->words;
}
function getSearchDate() {
return $this->dates;
}
function getSearchFields($array = false) {
if ($array)
return $this->fieldList;
return implode(',', $this->fieldList);
}
function getSearchString() {
if ($this->processed_search) {
return $this->processed_search;
}
$searchstring = trim($this->words);
$space_is = getOption('search_space_is');
$opChars = array('&' => 1, '|' => 1, '!' => 1, ',' => 1, '(' => 2);
if ($space_is) {
$opChars[' '] = 1;
}
$c1 = ' ';
$result = array();
$target = "";
$i = 0;
do {
$c = substr($searchstring, $i, 1);
$op = '';
switch ($c) {
case "'":
case '"':
case '`':
$j = strpos(str_replace('\\' . $c, '__', $searchstring), $c, $i + 1);
if ($j !== false) {
$target .= stripcslashes(substr($searchstring, $i + 1, $j - $i - 1));
$i = $j;
} else {
$target .= $c;
}
$c1 = $c;
break;
case ' ':
$j = $i + 1;
while ($j < strlen($searchstring) && $searchstring[$j] == ' ') {
$j++;
}
switch ($space_is) {
case 'OR':
case 'AND':
if ($j < strlen($searchstring)) {
$c3 = $searchstring[$j];
if (array_key_exists($c3, $opChars) && $opChars[$c3] == 1) {
$nextop = $c3 != '!';
} else if (substr($searchstring . ' ', $j, 4) == 'AND ') {
$nextop = true;
} else if (substr($searchstring . ' ', $j, 3) == 'OR ') {
$nextop = true;
} else {
$nextop = false;
}
}
if (!$nextop) {
if (!empty($target)) {
$r = trim($target);
if (!empty($r)) {
$last = $result[] = $r;
$target = '';
}
}
if ($space_is == 'AND') {
$c1 = '&';
} else {
$c1 = '|';
}
$target = '';
$last = $result[] = $c1;
}
break;
default:
$c1 = $c;
$target .= str_pad('', $j - $i);
break;
}
$i = $j - 1;
break;
case ',':
if (!empty($target)) {
$r = trim($target);
if (!empty($r)) {
switch ($r) {
case 'AND':
$r = '&';
break;
case 'OR':
$r = '|';
break;
case 'NOT':
$r = '!';
break;
}
$last = $result[] = $r;
$target = '';
}
}
$c2 = substr($searchstring, $i + 1, 1);
switch ($c2) {
case 'A':
if (substr($searchstring . ' ', $i + 1, 4) == 'AND ')
$c2 = '&';
break;
case 'O':
if (substr($searchstring . ' ', $i + 1, 3) == 'OR ')
$c2 = '|';
break;
case 'N':
if (substr($searchstring . ' ', $i + 1, 4) == 'NOT ')
$c2 = '!';
break;
}
if (!((isset($opChars[$c2]) && $opChars[$c2] == 1) || (isset($opChars[$last]) && $opChars[$last] == 1))) {
$last = $result[] = '|';
$c1 = $c;
}
break;
case '!':
case '&':
case '|':
case '(':
case ')':
if (!empty($target)) {
$r = trim($target);
if (!empty($r)) {
$last = $result[] = $r;
$target = '';
}
}
$c1 = $c;
$target = '';
$last = $result[] = $c;
$j = $i + 1;
break;
case 'A':
if (substr($searchstring . ' ', $i, 4) == 'AND ') {
$op = '&';
$skip = 3;
}
case 'O':
if (substr($searchstring . ' ', $i, 3) == 'OR ') {
$op = '|';
$skip = 2;
}
case 'N':
if (substr($searchstring . ' ', $i, 4) == 'NOT ') {
$op = '!';
$skip = 3;
}
if ($op) {
if (!empty($target)) {
$r = trim($target);
if (!empty($r)) {
$last = $result[] = $r;
$target = '';
}
}
$c1 = $op;
$target = '';
$last = $result[] = $op;
$j = $i + $skip;
while ($j < strlen($searchstring) && substr($searchstring, $j, 1) == ' ') {
$j++;
}
$i = $j - 1;
} else {
$c1 = $c;
$target .= $c;
}
break;
default:
$c1 = $c;
$target .= $c;
break;
}
} while ($i++ < strlen($searchstring));
if (!empty($target)) {
$last = $result[] = trim($target);
}
$lasttoken = '';
foreach ($result as $key => $token) {
if ($token == '|' && $lasttoken == '|') {
unset($result[$key]);
}
$lasttoken = $token;
}
if (array_key_exists($lasttoken, $opChars) && $opChars[$lasttoken] == 1) {
array_pop($result);
}
$this->processed_search = zp_apply_filter('search_criteria', $result);
return $this->processed_search;
}
function codifySearchString() {
$searchstring = $this->getSearchString();
$sanitizedwords = '';
if (is_array($searchstring)) {
foreach ($searchstring as $singlesearchstring) {
switch ($singlesearchstring) {
case '&':
$sanitizedwords .= " AND ";
break;
case '|':
$sanitizedwords .= " OR ";
break;
case '!':
$sanitizedwords .= " NOT ";
break;
case '(':
case ')':
$sanitizedwords .= $singlesearchstring;
break;
default:
$sanitizedwords .= search_quote(sanitize($singlesearchstring, 3));
break;
}
}
}
$sanitizedwords = trim(str_replace(array(' ', ' ',), ' ', $sanitizedwords));
$sanitizedwords = trim(str_replace('( ', '(', $sanitizedwords));
$sanitizedwords = trim(str_replace(' )', ')', $sanitizedwords));
return $sanitizedwords;
}
function getNumAlbums() {
if (is_null($this->albums)) {
$this->getAlbums(0, NULL, NULL, false);
}
return count($this->albums);
}
function parseQueryFields() {
$fields = array();
if (isset($_REQUEST['searchfields'])) {
$fs = sanitize($_REQUEST['searchfields']);
if (is_numeric($fs)) {
$fields = array_flip($this->numericFields($fs));
} else {
$fields = explode(',', $fs);
}
} else {
foreach ($_REQUEST as $key => $value) {
if (strpos($key, 'SEARCH_') !== false) {
$fields[substr($key, 7)] = $value;
}
}
}
return $fields;
}
protected function subsetNewsCategories() {
global $_zp_zenpage;
if (!is_array($this->category_list))
return false;
$cat = '';
$list = $_zp_zenpage->getAllCategories();
if (!empty($list)) {
foreach ($list as $category) {
if (in_array($category['title'], $this->category_list)) {
$catobj = new ZenpageCategory($category['titlelink']);
$cat .= ' `cat_id`=' . $catobj->getID() . ' OR';
$subcats = $catobj->getCategories();
if ($subcats) {
foreach ($subcats as $subcat) {
$catobj = new ZenpageCategory($subcat);
$cat .= ' `cat_id`=' . $catobj->getID() . ' OR';
}
}
}
}
if ($cat) {
$cat = ' WHERE ' . substr($cat, 0, -3);
}
}
$sql = 'SELECT DISTINCT `news_id` FROM ' . prefix('news2cat') . $cat;
$result = query($sql);
$list = array();
if ($result) {
while ($row = db_fetch_assoc($result)) {
$list[] = $row['news_id'];
}
db_free_result($result);
}
return $list;
}
protected static function compressedIDList($idlist) {
$idlist = array_unique($idlist);
asort($idlist);
return '`id` IN (' . implode(',', $idlist) . ')';
}
protected function sortKey($sorttype, $sortdirection, $defaulttype, $table) {
if (is_null($sorttype)) {
if (array_key_exists($table . 'sorttype', $this->extraparams)) {
$sorttype = $this->extraparams[$table . 'sorttype'];
} else if (array_key_exists('sorttype', $this->extraparams)) {
$sorttype = $this->extraparams['sorttype'];
}
}
$sorttype = lookupSortKey($sorttype, $defaulttype, $table);
if (is_null($sortdirection)) {
if (array_key_exists($table . 'sortdirection', $this->extraparams)) {
$sortdirection = $this->extraparams[$table . 'sortdirection'];
} else if (array_key_exists('sortdirection', $this->extraparams)) {
$sortdirection = $this->extraparams['sortdirection'];
}
}
return array($sorttype, $sortdirection);
}
function searchDate($searchstring, $searchdate, $tbl, $sorttype, $sortdirection, $whichdate = 'date') {
global $_zp_current_album, $_zp_gallery;
$sql = 'SELECT DISTINCT `id`, `show`,`title`';
switch ($tbl) {
case 'pages':
case 'news':
$sql .= ',`titlelink` ';
break;
case 'albums':
$sql .= ",`desc`,`folder` ";
break;
default:
$sql .= ",`desc`,`albumid`,`filename`,`location`,`city`,`state`,`country` ";
break;
}
$sql .= "FROM " . prefix($tbl) . " WHERE ";
if (!zp_loggedin()) {
$sql .= "`show` = 1 AND (";
}
if (!empty($searchdate)) {
if ($searchdate == "0000-00") {
$sql .= "`$whichdate`=\"0000-00-00 00:00:00\"";
} else {
$datesize = sizeof(explode('-', $searchdate));
if ($datesize == 3) {
$d1 = $searchdate . " 00:00:00";
$d2 = $searchdate . " 23:59:59";
$sql .= "`$whichdate` >= \"$d1\" AND `$whichdate` < \"$d2\"";
}
else if ($datesize == 2) {
$d1 = $searchdate . "-01 00:00:00";
$d = strtotime($d1);
$d = strtotime('+ 1 month', $d);
$d2 = substr(date('Y-m-d H:m:s', $d), 0, 7) . "-01 00:00:00";
$sql .= "`$whichdate` >= \"$d1\" AND `$whichdate` < \"$d2\"";
} else {
$sql .= "`$whichdate`<\"0000-00-00 00:00:00\"";
}
}
}
if (!zp_loggedin()) {
$sql .= ")";
}
switch ($tbl) {
case 'news':
case 'pages':
if (empty($sorttype)) {
$key = '`date` DESC';
} else {
$key = trim($sorttype . ' ' . $sortdirection);
}
break;
case 'albums':
if (is_null($sorttype)) {
if (empty($this->album)) {
list($key, $sortdirection) = $this->sortKey($_zp_gallery->getSortType(), $sortdirection, 'title', 'albums');
if ($key != '`sort_order`') {
if ($_zp_gallery->getSortDirection()) {
$key .= " DESC";
}
}
} else {
$key = $this->album->getAlbumSortKey();
if ($key != '`sort_order`' && $key != 'RAND()') {
if ($this->album->getSortDirection('album')) {
$key .= " DESC";
}
}
}
} else {
list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'albums');
$key = trim($key . ' ' . $sortdirection);
}
break;
default:
$hidealbums = getNotViewableAlbums();
if (!is_null($hidealbums)) {
foreach ($hidealbums as $id) {
$sql .= ' AND `albumid`!=' . $id;
}
}
if (is_null($sorttype)) {
if (empty($this->album)) {
list($key, $sortdirection) = $this->sortKey(IMAGE_SORT_TYPE, $sortdirection, 'title', 'images');
if ($key != '`sort_order`') {
if (IMAGE_SORT_DIRECTION) {
$key .= " DESC";
}
}
} else {
$key = $thie->album->getImageSortKey();
if ($key != '`sort_order`' && $key != 'RAND()') {
if ($this->album->getSortDirection('image')) {
$key .= " DESC";
}
}
}
} else {
list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images');
$key = trim($key . ' ' . $sortdirection);
}
break;
}
$sql .= " ORDER BY " . $key;
return $sql;
}
protected function searchFieldsAndTags($searchstring, $tbl, $sorttype, $sortdirection) {
global $_zp_gallery;
$weights = $idlist = array();
$sql = $allIDs = NULL;
$tagPattern = $this->tagPattern;
$tag_objects = array();
$fields = $this->fieldList;
if (count($fields) == 0) {
$fields = $this->allowedSearchFields();
}
foreach ($fields as $key => $field) {
switch ($field) {
case 'news_categories':
if ($tbl != 'news') {
break;
}
unset($fields[$key]);
query('SET @serachfield="news_categories"');
$tagsql = 'SELECT @serachfield AS field, t.`title` AS name, o.`news_id` AS `objectid` FROM ' . prefix('news_categories') . ' AS t, ' . prefix('news2cat') . ' AS o WHERE t.`id`=o.`cat_id` AND (';
foreach ($searchstring as $singlesearchstring) {
switch ($singlesearchstring) {
case '&':
case '!':
case '|':
case '(':
case ')':
break;
case '*':
$targetfound = true;
$tagsql .= "COALESCE(title, '') != '' OR ";
break;
default:
$targetfound = true;
$tagsql .= '`title` = ' . db_quote($singlesearchstring) . ' OR ';
}
}
$tagsql = substr($tagsql, 0, strlen($tagsql) - 4) . ') ORDER BY t.`id`';
$objects = query_full_array($tagsql, false);
if (is_array($objects)) {
$tag_objects = $objects;
}
break;
case 'tags_exact':
$tagPattern = array('type' => '=', 'open' => '', 'close' => '');
case 'tags':
unset($fields[$key]);
query('SET @serachfield="tags"');
$tagsql = 'SELECT @serachfield AS field, t.`name`, o.`objectid` FROM ' . prefix('tags') . ' AS t, ' . prefix('obj_to_tag') . ' AS o WHERE t.`id`=o.`tagid` AND o.`type`="' . $tbl . '" AND (';
foreach ($searchstring as $singlesearchstring) {
switch ($singlesearchstring) {
case '&':
case '!':
case '|':
case '(':
case ')':
break;
case '*':
query('SET @emptyfield="*"');
$tagsql = str_replace('t.`name`', '@emptyfield as name', $tagsql);
$tagsql .= "t.`name` IS NOT NULL OR ";
break;
default:
$targetfound = true;
if ($tagPattern['type'] == 'like') {
$target = db_LIKE_escape($singlesearchstring);
} else {
$target = $singlesearchstring;
}
$tagsql .= 't.`name` ' . strtoupper($tagPattern['type']) . ' ' . db_quote($tagPattern['open'] . $target . $tagPattern['close']) . ' OR ';
}
}
$tagsql = substr($tagsql, 0, strlen($tagsql) - 4) . ') ORDER BY t.`id`';
$objects = query_full_array($tagsql, false);
if (is_array($objects)) {
$tag_objects = array_merge($tag_objects, $objects);
}
break;
default:
break;
}
}
$field_objects = array();
if (count($fields) > 0) {
$columns = array();
$dbfields = db_list_fields($tbl);
if (is_array($dbfields)) {
foreach ($dbfields as $row) {
$columns[] = strtolower($row['Field']);
}
}
foreach ($searchstring as $singlesearchstring) {
switch ($singlesearchstring) {
case '!':
case '&':
case '|':
case '(':
case ')':
break;
default:
$targetfound = true;
query('SET @serachtarget=' . db_quote($singlesearchstring));
foreach ($fields as $fieldname) {
if ($tbl == 'albums' && strtolower($fieldname) == 'filename') {
$fieldname = 'folder';
} else {
$fieldname = strtolower($fieldname);
}
if ($fieldname && in_array($fieldname, $columns)) {
query('SET @serachfield=' . db_quote($fieldname));
switch ($singlesearchstring) {
case '*':
$sql = 'SELECT @serachtarget AS name, @serachfield AS field, `id` AS `objectid` FROM ' . prefix($tbl) . ' WHERE (' . "COALESCE(`$fieldname`, '') != ''" . ') ORDER BY `id`';
break;
default:
if ($this->pattern['type'] == 'like') {
$target = db_LIKE_escape($singlesearchstring);
} else {
$target = $singlesearchstring;
}
$fieldsql = ' `' . $fieldname . '` ' . strtoupper($this->pattern['type']) . ' ' . db_quote($this->pattern['open'] . $target . $this->pattern['close']);
$sql = 'SELECT @serachtarget AS name, @serachfield AS field, `id` AS `objectid` FROM ' . prefix($tbl) . ' WHERE (' . $fieldsql . ') ORDER BY `id`';
}
$objects = query_full_array($sql, false);
if (is_array($objects)) {
$field_objects = array_merge($field_objects, $objects);
}
}
}
}
}
}
$exact = $tagPattern['type'] == '=';
$objects = array_merge($tag_objects, $field_objects);
if (count($objects) != 0) {
$tagid = '';
$taglist = array();
foreach ($objects as $object) {
$tagid = strtolower($object['name']);
if (!isset($taglist[$tagid]) || !is_array($taglist[$tagid])) {
$taglist[$tagid] = array();
}
$taglist[$tagid][] = $object['objectid'];
}
$op = '';
$idstack = array();
$opstack = array();
while (count($searchstring) > 0) {
$singlesearchstring = array_shift($searchstring);
switch ($singlesearchstring) {
case '&':
case '!':
case '|':
$op = $op . $singlesearchstring;
break;
case '(':
array_push($idstack, $idlist);
array_push($opstack, $op);
$idlist = array();
$op = '';
break;
case ')':
$objectid = $idlist;
$idlist = array_pop($idstack);
$op = array_pop($opstack);
switch ($op) {
case '&':
if (is_array($objectid)) {
$idlist = array_intersect($idlist, $objectid);
} else {
$idlist = array();
}
break;
case '!':
break;
case '&!':
if (is_array($objectid)) {
$idlist = array_diff($idlist, $objectid);
}
break;
case '';
case '|':
if (is_array($objectid)) {
$idlist = array_merge($idlist, $objectid);
}
break;
}
$op = '';
break;
default:
$lookfor = strtolower($singlesearchstring);
$objectid = NULL;
foreach ($taglist as $key => $objlist) {
if (($exact && $lookfor == $key) || (!$exact && preg_match('|' . preg_quote($lookfor) . '|', $key))) {
if (is_array($objectid)) {
$objectid = array_merge($objectid, $objlist);
} else {
$objectid = $objlist;
}
}
}
switch ($op) {
case '&':
if (is_array($objectid)) {
$idlist = array_intersect($idlist, $objectid);
} else {
$idlist = array();
}
break;
case '!':
if (is_null($allIDs)) {
$allIDs = array();
$result = query("SELECT `id` FROM " . prefix($tbl));
if ($result) {
while ($row = db_fetch_assoc($result)) {
$allIDs[] = $row['id'];
}
db_free_result($result);
}
}
if (is_array($objectid)) {
$idlist = array_merge($idlist, array_diff($allIDs, $objectid));
}
break;
case '&!':
if (is_array($objectid)) {
$idlist = array_diff($idlist, $objectid);
}
break;
case '';
case '|':
if (is_array($objectid)) {
$idlist = array_merge($idlist, $objectid);
}
break;
}
$op = '';
break;
}
}
}
if (count($idlist) > 0) {
$weights = array_count_values($idlist);
arsort($weights, SORT_NUMERIC);
$sql = 'SELECT DISTINCT `id`,`show`,';
switch ($tbl) {
case 'news':
if ($this->search_unpublished || zp_loggedin(MANAGE_ALL_NEWS_RIGHTS)) {
$show = '';
} else {
$show = "`show` = 1 AND ";
}
$sql .= '`title`, `titlelink` ';
if (is_array($this->category_list)) {
$news_list = $this->subsetNewsCategories();
$idlist = array_intersect($news_list, $idlist);
if (count($idlist) == 0) {
return array(false, array());
}
}
if (empty($sorttype)) {
$key = '`date` DESC';
} else {
$key = trim($sorttype . $sortdirection);
}
if ($show) {
$show .= '`date`<=' . db_quote(date('Y-m-d H:i:s')) . ' AND ';
}
break;
case 'pages':
if (zp_loggedin(MANAGE_ALL_PAGES_RIGHTS)) {
$show = '';
} else {
$show = "`show` = 1 AND ";
}
$sql .= '`title`, `titlelink` ';
if (empty($sorttype)) {
$key = '`date` DESC';
} else {
$key = trim($sorttype . $sortdirection);
}
if ($show) {
$show .= '`date`<=' . db_quote(date('Y-m-d H:i:s')) . ' AND ';
}
break;
case 'albums':
if ($this->search_unpublished || zp_loggedin()) {
$show = '';
} else {
$show = "`show` = 1 AND ";
}
$sql .= "`folder`, `title` ";
if (is_null($sorttype)) {
if (empty($this->album)) {
list($key, $sortdirection) = $this->sortKey($_zp_gallery->getSortType(), $sortdirection, 'title', 'albums');
if ($_zp_gallery->getSortDirection()) {
$key .= " DESC";
}
} else {
$key = $this->album->getAlbumSortKey();
if ($key != '`sort_order`' && $key != 'RAND()') {
if ($this->album->getSortDirection('album')) {
$key .= " DESC";
}
}
}
} else {
list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'albums');
$key = trim($key . ' ' . $sortdirection);
}
break;
default:
if ($this->search_unpublished || zp_loggedin()) {
$show = '';
} else {
$show = "`show` = 1 AND ";
}
$sql .= "`albumid`, `filename`, `title` ";
if (is_null($sorttype)) {
if (empty($this->album)) {
list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images');
if ($sortdirection) {
$key .= " DESC";
}
} else {
$key = $this->album->getImageSortKey();
if ($key != '`sort_order`') {
if ($this->album->getSortDirection('image')) {
$key .= " DESC";
}
}
}
} else {
list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images');
$key = trim($key . ' ' . $sortdirection);
}
break;
}
$sql .= "FROM " . prefix($tbl) . " WHERE " . $show;
$sql .= '(' . self::compressedIDList($idlist) . ')';
$sql .= " ORDER BY " . $key;
return array($sql, $weights);
}
return array(false, array());
}
private function getSearchAlbums($sorttype, $sortdirection, $mine = NULL) {
if (getOption('search_no_albums') || $this->search_no_albums) {
return array();
}
list($sorttype, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'albums');
$albums = array();
$searchstring = $this->getSearchString();
if (empty($searchstring)) {
return array();
}
$criteria = $this->getCacheTag('albums', serialize($searchstring), $sorttype . ' ' . $sortdirection . ' '. $mine);
if ($this->albums && $criteria == $this->searches['albums']) {
return $this->albums;
}
$albums = $this->getCachedSearch($criteria);
if (is_null($albums)) {
if (is_null($mine) && zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
$mine = true;
}
$result = $albums = array();
list ($search_query, $weights) = $this->searchFieldsAndTags($searchstring, 'albums', $sorttype, $sortdirection);
if (!empty($search_query)) {
$search_result = query($search_query);
if ($search_result) {
while ($row = db_fetch_assoc($search_result)) {
$albumname = $row['folder'];
if ($albumname != $this->dynalbumname) {
if (file_exists(ALBUM_FOLDER_SERVERPATH . internalToFilesystem($albumname))) {
$album = newAlbum($albumname);
$uralbum = getUrAlbum($album);
$viewUnpublished = ($this->search_unpublished || zp_loggedin() && $uralbum->albumSubRights() & (MANAGED_OBJECT_RIGHTS_EDIT | MANAGED_OBJECT_RIGHTS_VIEW));
switch (themeObject::checkScheduledPublishing($row)) {
case 1:
$album->setShow(0);
$album->save();
case 2:
$row['show'] = 0;
}
if ($mine || (is_null($mine) && $album->isMyItem(LIST_RIGHTS)) || (checkAlbumPassword($albumname) && (($album->checkAccess() && $album->isPublic()) || $viewUnpublished))) {
if ((empty($this->album_list) || in_array($albumname, $this->album_list)) && !$this->excludeAlbum($albumname)) {
$result[] = array('title' => $row['title'], 'name' => $albumname, 'weight' => $weights[$row['id']]);
}
}
}
}
}
db_free_result($search_result);
$sortdir = self::getSortdirBool($sortdirection);
if (is_null($sorttype)) {
$result = sortMultiArray($result, 'weight', $sortdir, true, false, false, array('weight'));
}
if ($sorttype == '`title`') {
$result = sortByMultilingual($result, 'title', $sortdir);
}
foreach ($result as $album) {
$albums[] = $album['name'];
}
}
}
zp_apply_filter('search_statistics', $searchstring, 'albums', !empty($albums), $this->dynalbumname, $this->iteration++);
$this->cacheSearch($criteria, $albums);
}
$this->albums = $albums;
$this->searches['albums'] = $criteria;
return $albums;
}
function getAlbums($page = 0, $sorttype = NULL, $sortdirection = NULL, $care = true, $mine = NULL) {
$this->albums = $this->getSearchAlbums($sorttype, $sortdirection, $mine);
if ($page == 0) {
return $this->albums;
} else {
$albums_per_page = max(1, getOption('albums_per_page'));
return array_slice($this->albums, $albums_per_page * ($page - 1), $albums_per_page);
}
}
function excludeAlbum($albumname) {
$exclude = false;
if (!is_null($this->album_list_exclude)) {
if (in_array($albumname, $this->album_list_exclude)) {
return true;
} else {
foreach ($this->album_list_exclude as $excludealbum) {
if (strpos($albumname, $excludealbum) === 0) {
return true;
}
}
}
}
return $exclude;
}
function getAlbumIndex($curalbum) {
$albums = $this->getAlbums(0);
return array_search($curalbum, $albums);
}
function getNextAlbum($curalbum) {
global $_zp_gallery;
$albums = $this->getAlbums(0);
$inx = array_search($curalbum, $albums) + 1;
if ($inx >= 0 && $inx < count($albums)) {
return newAlbum($albums[$inx]);
}
return null;
}
function getPrevAlbum($curalbum) {
global $_zp_gallery;
$albums = $this->getAlbums(0);
$inx = array_search($curalbum, $albums) - 1;
if ($inx >= 0 && $inx < count($albums)) {
return newAlbum($albums[$inx]);
}
return null;
}
function getNumImages() {
if (is_null($this->images)) {
$this->getImages(0);
}
return count($this->images);
}
private function getSearchImages($sorttype, $sortdirection, $mine = NULL) {
if (getOption('search_no_images') || $this->search_no_images) {
return array();
}
list($sorttype, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images');
if (is_null($mine) && zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
$mine = true;
}
$searchstring = $this->getSearchString();
$searchdate = $this->dates;
if (empty($searchstring) && empty($searchdate)) {
return array();
}
$criteria = $this->getCacheTag('images', serialize($searchstring) . ' ' . $searchdate, $sorttype . ' ' . $sortdirection . ' '.$mine);
if ($criteria == $this->searches['images']) {
return $this->images;
}
$images = $this->getCachedSearch($criteria);
if (is_null($images)) {
if (empty($searchdate)) {
list ($search_query, $weights) = $this->searchFieldsAndTags($searchstring, 'images', $sorttype, $sortdirection);
} else {
$search_query = $this->searchDate($searchstring, $searchdate, 'images', $sorttype, $sortdirection);
}
if (empty($search_query)) {
$search_result = false;
} else {
$search_result = query($search_query);
}
$albums_seen = $images = array();
if ($search_result) {
while ($row = db_fetch_assoc($search_result)) {
$albumid = $row['albumid'];
if (array_key_exists($albumid, $albums_seen)) {
$albumrow = $albums_seen[$albumid];
} else {
$query = "SELECT folder, `show` FROM " . prefix('albums') . " WHERE id = $albumid";
$row2 = query_single_row($query);
if ($row2) {
$albumname = $row2['folder'];
$allow = false;
$album = newAlbum($albumname);
$uralbum = getUrAlbum($album);
$viewUnpublished = ($this->search_unpublished || zp_loggedin() && $uralbum->albumSubRights() & (MANAGED_OBJECT_RIGHTS_EDIT | MANAGED_OBJECT_RIGHTS_VIEW));
switch (themeObject::checkScheduledPublishing($row)) {
case 1:
$imageobj = newImage($album, $row['filename']);
$imageobj->setShow(0);
$imageobj->save();
case 2:
$row['show'] = 0;
break;
}
$viewUnpublished = ($mine || is_null($mine)) && ($album->isMyItem(LIST_RIGHTS) || checkAlbumPassword($albumname) && ($album->isPublic() || $viewUnpublished));
if ($viewUnpublished) {
$allow = (empty($this->album_list) || in_array($albumname, $this->album_list)) && !$this->excludeAlbum($albumname);
}
$albums_seen[$albumid] = $albumrow = array('allow' => $allow, 'viewUnpublished' => $viewUnpublished, 'folder' => $albumname, 'localpath' => ALBUM_FOLDER_SERVERPATH . internalToFilesystem($albumname) . '/');
} else {
$albums_seen[$albumid] = $albumrow = array('allow' => false, 'viewUnpublished' => false, 'folder' => '', 'localpath' => '');
}
}
if ($albumrow['allow'] && ($row['show'] || $albumrow['viewUnpublished'])) {
if (file_exists($albumrow['localpath'] . internalToFilesystem($row['filename']))) {
$data = array('title' => $row['title'], 'filename' => $row['filename'], 'folder' => $albumrow['folder']);
if (isset($weights)) {
$data['weight'] = $weights[$row['id']];
}
$images[] = $data;
}
}
}
db_free_result($search_result);
$sortdir = self::getSortdirBool($sortdirection);
if (is_null($sorttype) && isset($weights)) {
$images = sortMultiArray($images, 'weight', $sortdir, true, false, false, array('weight'));
}
if ($sorttype == '`title`') {
$images = sortByMultilingual($images, 'title', $sortdir);
}
}
if (empty($searchdate)) {
zp_apply_filter('search_statistics', $searchstring, 'images', !empty($images), $this->dynalbumname, $this->iteration++);
}
$this->cacheSearch($criteria, $images);
}
$this->searches['images'] = $criteria;
return $images;
}
function getImages($page = 0, $firstPageCount = 0, $sorttype = NULL, $sortdirection = NULL, $care = true, $mine = NULL) {
$this->images = $this->getSearchImages($sorttype, $sortdirection, $mine);
if ($page == 0) {
return $this->images;
} else {
if (empty($this->images)) {
return array();
}
if (($page == 1) && ($firstPageCount > 0)) {
$pageStart = 0;
$images_per_page = $firstPageCount;
} else {
if ($firstPageCount > 0) {
$fetchPage = $page - 2;
} else {
$fetchPage = $page - 1;
}
$images_per_page = max(1, getOption('images_per_page'));
$pageStart = $firstPageCount + $images_per_page * $fetchPage;
}
$slice = array_slice($this->images, $pageStart, $images_per_page);
return $slice;
}
}
function getImageIndex($album, $filename) {
$images = $this->getImages();
$c = 0;
foreach ($images as $image) {
if (($album == $image['folder']) && ($filename == $image['filename'])) {
return $c;
}
$c++;
}
return false;
}
function getImage($index) {
global $_zp_gallery;
if (!is_null($this->images)) {
$this->getImages();
}
if ($index >= 0 && $index < $this->getNumImages()) {
$img = $this->images[$index];
return newImage(newAlbum($img['folder']), $img['filename']);
}
return false;
}
function getDynamicAlbum() {
return $this->album;
}
function getAlbumList() {
return $this->album_list;
}
function getCategoryList() {
return $this->category_list;
}
function getPages($published = NULL, $toplevel = false, $number = NULL, $sorttype = NULL, $sortdirection = NULL) {
return $this->getSearchPages($sorttype, $sortdirection);
}
private function getSearchPages($sorttype, $sortdirection) {
if (!extensionEnabled('zenpage') || getOption('search_no_pages') || $this->search_no_pages)
return array();
list($sorttype, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'pages');
$searchstring = $this->getSearchString();
$searchdate = $this->dates;
if (empty($searchstring) && empty($searchdate)) {
return array();
}
$criteria = $this->getCacheTag('pages', serialize($searchstring) . ' ' . $searchdate, $sorttype . ' ' . $sortdirection);
if ($this->pages && $criteria == $this->searches['pages']) {
return $this->pages;
}
$pages = $this->getCachedSearch($criteria);
if (is_null($pages)) {
$pages = $result = array();
if (empty($searchdate)) {
list ($search_query, $weights) = $this->searchFieldsAndTags($searchstring, 'pages', $sorttype, $sortdirection);
if (empty($search_query)) {
$search_result = false;
} else {
$search_result = query($search_query);
}
zp_apply_filter('search_statistics', $searchstring, 'pages', !$search_result, false, $this->iteration++);
} else {
$search_query = $this->searchDate($searchstring, $searchdate, 'pages', NULL, NULL);
$search_result = query($search_query);
}
if ($search_result) {
while ($row = db_fetch_assoc($search_result)) {
$pageobj = new ZenpagePage($row['titlelink']);
if((zp_loggedin() && $pageobj->isMyItem(LIST_RIGHTS)) || ($pageobj->isPublic() || $this->search_unpublished)) {
$data = array('title' => $row['title'], 'titlelink' => $row['titlelink']);
if (isset($weights)) {
$data['weight'] = $weights[$row['id']];
}
$result[] = $data;
}
}
db_free_result($search_result);
}
$sortdir = self::getSortdirBool($sortdirection);
if (is_null($sorttype) && isset($weights)) {
$result = sortMultiArray($result, 'weight', $sortdir, true, false, false, array('weight'));
}
if ($sorttype == '`title`') {
$result = sortByMultilingual($result, 'title', $sortdir);
}
foreach ($result as $page) {
$pages[] = $page['titlelink'];
}
$this->cacheSearch($criteria, $pages);
}
$this->pages = $pages;
$this->searches['pages'] = $criteria;
return $this->pages;
}
function getArticles($articles_per_page = 0, $published = NULL, $ignorepagination = false, $sorttype = NULL, $sortdirection = NULL) {
$articles = $this->getSearchArticles($sorttype, $sortdirection);
if (empty($articles)) {
return array();
} else {
if ($ignorepagination || !$articles_per_page) {
return $articles;
}
return array_slice($articles, Zenpage::getOffset($articles_per_page), $articles_per_page);
}
}
private function getSearchArticles($sorttype, $sortdirection) {
if (!extensionEnabled('zenpage') || getOption('search_no_news') || $this->search_no_news) {
return array();
}
list($sorttype, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'news');
$searchstring = $this->getSearchString();
$searchdate = $this->dates;
if (empty($searchstring) && empty($searchdate)) {
return array();
}
$criteria = $this->getCacheTag('news', serialize($searchstring) . ' ' . $searchdate, $sorttype . ' ' . $sortdirection);
if ($this->articles && $criteria == $this->searches['news']) {
return $this->articles;
}
$result = $this->getCachedSearch($criteria);
if (is_null($result)) {
$result = array();
if (empty($searchdate)) {
list ($search_query, $weights) = $this->searchFieldsAndTags($searchstring, 'news', $sorttype, $sortdirection);
} else {
$search_query = $this->searchDate($searchstring, $searchdate, 'news', $sorttype, $sortdirection, $this->whichdates);
}
if (empty($search_query)) {
$search_result = false;
} else {
$search_result = query($search_query);
}
zp_apply_filter('search_statistics', $searchstring, 'news', !empty($search_result), false, $this->iteration++);
if ($search_result) {
while ($row = db_fetch_assoc($search_result)) {
$articleobj = new ZenpageNews($row['titlelink']);
if((zp_loggedin() && $articleobj->isMyItem(LIST_RIGHTS)) || ($articleobj->isPublic() || $this->search_unpublished)) {
$data = array('title' => $row['title'], 'titlelink' => $row['titlelink']);
if (isset($weights)) {
$data['weight'] = $weights[$row['id']];
}
$result[] = $data;
}
}
db_free_result($search_result);
}
$sortdir = self::getSortdirBool($sortdirection);
if (is_null($sorttype) && isset($weights)) {
$result = sortMultiArray($result, 'weight', $sortdir, true, false, false, array('weight'));
}
if ($sorttype == '`title`') {
$result = sortByMultilingual($result, 'title', $sortdir);
}
$this->cacheSearch($criteria, $result);
}
$this->articles = $result;
$this->searches['news'] = $criteria;
return $this->articles;
}
function clearSearchWords() {
$this->processed_search = '';
$this->words = '';
}
protected function getCacheTag($table, $search, $sort) {
$user = 'guest';
$authCookies = Zenphoto_Authority::getAuthCookies();
if (!empty($authCookies)) {
$user = getUserIP();
}
$array = array('item' => $table, 'fields' => implode(', ', $this->fieldList), 'search' => $search, 'sort' => $sort, 'user' => $user);
$dynalbum = $this->getDynamicAlbum();
if($dynalbum) {
$array['dynalbum'] = $dynalbum->name;
}
return $array;
}
private function cacheSearch($criteria, $found) {
if (SEARCH_CACHE_DURATION) {
$criteria = serialize($criteria);
$sql = 'SELECT `id`, `data`, `date` FROM ' . prefix('search_cache') . ' WHERE `criteria` = ' . db_quote($criteria);
$result = query_single_row($sql);
if ($result) {
$sql = 'UPDATE ' . prefix('search_cache') . ' SET `data` = ' . db_quote(serialize($found)) . ', `date` = ' . db_quote(date('Y-m-d H:m:s')) . ' WHERE `id` = ' . $result['id'];
query($sql);
} else {
$sql = 'INSERT INTO ' . prefix('search_cache') . ' (criteria, data, date) VALUES (' . db_quote($criteria) . ', ' . db_quote(serialize($found)) . ', ' . db_quote(date('Y-m-d H:m:s')) . ')';
query($sql);
}
}
}
private function getCachedSearch($criteria) {
if (SEARCH_CACHE_DURATION) {
$sql = 'SELECT `id`, `date`, `data` FROM ' . prefix('search_cache') . ' WHERE `criteria` = ' . db_quote(serialize($criteria));
$result = query_single_row($sql);
if ($result) {
if ((time() - strtotime($result['date'])) > SEARCH_CACHE_DURATION * 60) {
query('DELETE FROM ' . prefix('search_cache') . ' WHERE `id` = ' . $result['id']);
} else {
if ($result = getSerializedArray($result['data'])) {
return $result;
}
}
}
}
return NULL;
}
static function clearSearchCache() {
$check = query_single_row('SELECT id FROM ' . prefix('search_cache'). ' LIMIT 1');
if($check) {
query('TRUNCATE TABLE ' . prefix('search_cache'));
}
}
static function getSortdirBool($sortdirection = 'asc') {
$dir = false;
if (strtolower($sortdirection) == 'desc') {
$dir = true;
}
return $dir;
}
}
function search_quote($word) {
if (is_numeric($word) || preg_match("/[ &|!'\"`,()]/", $word)) {
$word = '"' . str_replace("\\'", "'", addslashes($word)) . '"';
}
return $word;
}
?>