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: 
<?php
require_once(dirname(__FILE__) . '/global-definitions.php');
require_once(dirname(__FILE__) . '/functions-common.php');
global $_zp_conf_vars;
$const_webpath = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
$const_serverpath = str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME']));
if (!preg_match('~(.*)/(' . ZENFOLDER . ')~', $const_webpath, $matches)) {
    preg_match('~(.*)/(' . USER_PLUGIN_FOLDER . '|' . THEMEFOLDER . ')~', $const_webpath, $matches);
}
if ($matches) {
    $const_webpath = $matches[1];
    $const_serverpath = substr($const_serverpath, 0, strrpos($const_serverpath, '/' . $matches[2]));
    if (!defined('OFFSET_PATH')) {
        switch ($matches[2]) {
            case ZENFOLDER:
                define('OFFSET_PATH', 1);
                break;
            case USER_PLUGIN_FOLDER:
                define('OFFSET_PATH', 3);
                break;
            case THEMEFOLDER:
                define('OFFSET_PATH', 4);
                break;
        }
    }
    unset($matches);
} else {
    if (!defined('OFFSET_PATH')) {
        define('OFFSET_PATH', 0);
    }
}
if ($const_webpath == '/' || $const_webpath == '.') {
    $const_webpath = '';
}
if (defined('SERVERPATH')) {
    $const_serverpath = SERVERPATH;
}
define("ZP_INDEX", 1);
define("ZP_ALBUM", 2);
define("ZP_IMAGE", 4);
define("ZP_COMMENT", 8);
define("ZP_SEARCH", 16);
define("ZP_SEARCH_LINKED", 32);
define("ZP_ALBUM_LINKED", 64);
define('ZP_IMAGE_LINKED', 128);
define('ZP_ZENPAGE_NEWS_PAGE', 256);
define('ZP_ZENPAGE_NEWS_ARTICLE', 512);
define('ZP_ZENPAGE_NEWS_CATEGORY', 1024);
define('ZP_ZENPAGE_NEWS_DATE', 2048);
define('ZP_ZENPAGE_PAGE', 4096);
define('ZP_ZENPAGE_SINGLE', 8192);
switch (PHP_MAJOR_VERSION) {
    case 5:
        switch (PHP_MINOR_VERSION) {
            case 0:
            case 1:
            case 2:
                define('ENT_FLAGS', ENT_QUOTES);
                break;
            case 3:
                define('ENT_FLAGS', ENT_QUOTES | ENT_IGNORE);
                break;
            default: 
                define('ENT_FLAGS', ENT_QUOTES | ENT_SUBSTITUTE);
                break;
        }
        break;
    default: 
        define('ENT_FLAGS', ENT_QUOTES | ENT_SUBSTITUTE);
        break;
}
@ini_set('display_errors', '0'); 
if (TEST_RELEASE) {
    error_reporting(E_ALL | E_STRICT);
    @ini_set('display_errors', '1');
} 
set_error_handler("zpErrorHandler");
set_exception_handler("zpErrorHandler");
$_configMutex = new zpMutex('cF');
if (!defined('WEBPATH')) {
    define('WEBPATH', $const_webpath);
}
unset($const_webpath);
if (!defined('SERVERPATH')) {
    define('SERVERPATH', $const_serverpath);
}
unset($const_serverpath);
if (OFFSET_PATH != 2 && !file_exists(SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE)) {
    require_once(dirname(__FILE__) . '/reconfigure.php');
    reconfigureAction(1);
} else {
    eval('?>' . file_get_contents(SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE));
}
if (!isset($_zp_conf_vars['server_protocol'])) {
    $_zp_conf_vars['server_protocol'] = 'http';
}
if (isset($_zp_conf_vars['server_protocol'])) {
    define('SERVER_PROTOCOL', $_zp_conf_vars['server_protocol']);
} else {
    define('SERVER_PROTOCOL', 'http');
}
switch (SERVER_PROTOCOL) {
    case 'https':
        define('PROTOCOL', 'https');
        break;
    default:
        if (secureServer()) {
            define('PROTOCOL', 'https');
        } else {
            define('PROTOCOL', 'http');
        }
        break;
}
if (!isset($_zp_conf_vars['special_pages'])) {
    $_zp_conf_vars['special_pages'] = getDefaultRewriteTokens(null);
} else {
    addMissingDefaultRewriteTokens();
}
define('DATABASE_PREFIX', $_zp_conf_vars['mysql_prefix']);
$_zp_mutex = new zpMutex();
if (OFFSET_PATH != 2 && empty($_zp_conf_vars['mysql_database'])) {
    require_once(dirname(__FILE__) . '/reconfigure.php');
    reconfigureAction(2);
}
require_once(dirname(__FILE__) . '/lib-utf8.php');
if (!defined('CHMOD_VALUE')) {
    define('CHMOD_VALUE', fileperms(dirname(__FILE__)) & 0666);
}
define('FOLDER_MOD', CHMOD_VALUE | 0311);
define('FILE_MOD', CHMOD_VALUE & 0666);
define('DATA_MOD', fileperms(SERVERPATH . '/' . DATA_FOLDER . '/' . CONFIGFILE) & 0777);
if(file_exists(SERVERPATH . '/' . DATA_FOLDER . '/setup.log')) {
    define('LOGS_MOD', fileperms(SERVERPATH . '/' . DATA_FOLDER . '/setup.log') & 0600);
} else {
    define('LOGS_MOD', DATA_MOD);
}
if (!defined('DATABASE_SOFTWARE') && extension_loaded(strtolower(@$_zp_conf_vars['db_software']))) {
    require_once(dirname(__FILE__) . '/functions-db-' . $_zp_conf_vars['db_software'] . '.php');
    $dbconfig_defaults = array(
            'db_software' => $_zp_conf_vars['db_software'],
            'mysql_user' => null,
            'mysql_pass' => null,
            'mysql_host' => 'localhost',
            'mysql_database' => null,
            'mysql_port' => 3306,
            'mysql_prefix' => '',
            'UTF-8' => true);
    foreach($dbconfig_defaults as $key => $value) {
        if (!isset($_zp_conf_vars[$key]) || ($key != 'mysql_prefix' && isset($_zp_conf_vars[$key]) && empty($_zp_conf_vars[$key]))) {
            $_zp_conf_vars[$key] = $value;
        }
    }
    $data = db_connect($_zp_conf_vars, false);
} else {
    $data = false;
}
if (!$data && OFFSET_PATH != 2) {
    require_once(dirname(__FILE__) . '/reconfigure.php');
    reconfigureAction(3);
}
if (!defined('FILESYSTEM_CHARSET')) {
    if (isset($_zp_conf_vars['FILESYSTEM_CHARSET']) && $_zp_conf_vars['FILESYSTEM_CHARSET'] != 'unknown') {
        define('FILESYSTEM_CHARSET', $_zp_conf_vars['FILESYSTEM_CHARSET']);
    } else {
        $data = getOption('filesystem_charset');
        if(!$data) {
            $data = 'UTF-8';
        }
        define('FILESYSTEM_CHARSET', $data);
    }
}
$data = getOption('charset');
if (!$data) {
    $data = 'UTF-8';
}
define('LOCAL_CHARSET', $data);
$data = getOption('gallery_data');
if ($data) {
    $data = getSerializedArray($data);
} else {
    $data = array();
}
define('GALLERY_SESSION', @$data['album_session']);
define('GALLERY_SECURITY', @$data['gallery_security']);
unset($data);
if (function_exists('date_default_timezone_set')) {
    $level = error_reporting(0);
    $_zp_server_timezone = date_default_timezone_get();
    date_default_timezone_set($_zp_server_timezone);
    @ini_set('date.timezone', $_zp_server_timezone);
    error_reporting($level);
}
if (ini_get('memory_limit') && parse_size(ini_get('memory_limit')) < 100663296) {
    @ini_set('memory_limit', '96M');
}
if (function_exists('mb_internal_encoding')) {
    @mb_internal_encoding(LOCAL_CHARSET);
}
$_zp_graphics_optionhandlers = array();
$try = array('lib-GD.php', 'lib-NoGraphics.php');
if (getOption('use_imagick')) {
    array_unshift($try, 'lib-Imagick.php');
}
while (!function_exists('zp_graphicsLibInfo')) {
    require_once(dirname(__FILE__) . '/' . array_shift($try));
}
$_zp_cachefileSuffix = zp_graphicsLibInfo();
define('GRAPHICS_LIBRARY', $_zp_cachefileSuffix['Library']);
unset($_zp_cachefileSuffix['Library']);
unset($_zp_cachefileSuffix['Library_desc']);
$_zp_supported_images = array();
foreach ($_zp_cachefileSuffix as $key => $type) {
    if ($type) {
        $_zp_supported_images[] = strtolower($key);
    }
}
require_once(dirname(__FILE__) . '/lib-encryption.php');
if (!defined('COOKIE_PERSISTENCE')) {
    $persistence = getOption('cookie_persistence');
    if (!$persistence)
        $persistence = 5184000;
    define('COOKIE_PERSISTENCE', $persistence);
    unset($persistence);
}
if ($c = getOption('zenphoto_cookie_path')) {
    define('COOKIE_PATH', $c);
} else {
    define('COOKIE_PATH', WEBPATH);
}
define('SERVER_HTTP_HOST', PROTOCOL . "://" . $_SERVER['HTTP_HOST']);
define('SAFE_MODE', false);
define('FULLWEBPATH', SERVER_HTTP_HOST . WEBPATH);
define('SAFE_MODE_ALBUM_SEP', '');
define('SERVERCACHE', SERVERPATH . '/' . CACHEFOLDER);
define('MOD_REWRITE', getOption('mod_rewrite'));
define('DEBUG_LOG_SIZE', getOption('debug_log_size'));
define('ALBUM_FOLDER_WEBPATH', getAlbumFolder(WEBPATH));
define('ALBUM_FOLDER_SERVERPATH', getAlbumFolder(SERVERPATH));
define('ALBUM_FOLDER_EMPTY', getAlbumFolder(''));
define('IMAGE_WATERMARK', getOption('fullimage_watermark'));
define('FULLIMAGE_WATERMARK', getOption('fullsizeimage_watermark'));
define('THUMB_WATERMARK', getOption('Image_watermark'));
define('OPEN_IMAGE_CACHE', !getOption('protected_image_cache'));
define('IMAGE_CACHE_SUFFIX', getOption('image_cache_suffix'));
define('DATE_FORMAT', getOption('date_format'));
define('IM_SUFFIX', getOption('mod_rewrite_image_suffix'));
define('UTF8_IMAGE_URI', getOption('UTF8_image_URI'));
define('MEMBERS_ONLY_COMMENTS', getOption('comment_form_members_only'));
define('HASH_SEED', getOption('extra_auth_hash_text'));
define("CACHE_HASH_LENGTH", strlen(sha1(HASH_SEED))); 
define('IP_TIED_COOKIES', getOption('IP_tied_cookies'));
define('MENU_TRUNCATE_STRING', getOption('menu_truncate_string'));
define('MENU_TRUNCATE_INDICATOR', getOption('menu_truncate_indicator'));
function js_encode($this_string) {
    global $_zp_UTF8;
    $this_string = preg_replace("/\r?\n/", "\\n", $this_string);
    $this_string = utf8::encode_javascript($this_string);
    return $this_string;
}
function getOption($key) {
    global $_zp_conf_vars, $_zp_options;
    $key = strtolower($key);
    if (is_null($_zp_options) && function_exists('query_full_array')) { 
        
        $sql = "SELECT `name`, `value` FROM " . prefix('options') . ' WHERE (`theme`="" OR `theme` IS NULL) AND `ownerid`=0';
        $optionlist = query_full_array($sql, false);
        if ($optionlist !== false) {
            $_zp_options = array();
            foreach ($optionlist as $option) {
                $_zp_options[strtolower($option['name'])] = $option['value'];
            }
        }
    }
    if (isset($_zp_options[$key])) {
        return $_zp_options[$key];
    } else {
        return NULL;
    }
}
function setOption($key, $value, $persistent = true, $creator = NULL) {
    global $_zp_options;
    if ($persistent) {
        $sql = 'INSERT INTO ' . prefix('options') . ' (`name`,`ownerid`,`theme`,`value`,`creator`) VALUES (' . db_quote($key) . ',0,"",';
        $sqlu = ' ON DUPLICATE KEY UPDATE `value`=';
        if (is_null($value)) {
            $sql .= 'NULL';
            $sqlu .= 'NULL';
        } else {
            $sql .= db_quote($value);
            $sqlu .= db_quote($value);
        }
  
  if (is_null($creator)) {
            $sql .= ',NULL';
        } else {
            $sql .= ','.db_quote($creator);
        }
  
        $sql .= ') ' . $sqlu;
        $result = query($sql, false);
    } else {
        $result = true;
    }
    if ($result) {
        $_zp_options[strtolower($key)] = $value;
        return true;
    } else {
        return false;
    }
}
function setOptionDefault($key, $default) {
    global $_zp_options;
    if (!is_null($default)) {
        $bt = debug_backtrace();
        $b = array_shift($bt);
        $serverpath = str_replace('\\', '/', dirname($b['file']));
        if (!preg_match('~(.*)/(' . ZENFOLDER . ')~', $serverpath, $matches)) {
            preg_match('~(.*)/(' . USER_PLUGIN_FOLDER . '|' . THEMEFOLDER . ')~', $serverpath, $matches);
        }
        if ($matches) {
            $creator = str_replace($matches[1] . '/', '', str_replace('\\', '/', $b['file']));
        } else {
            $creator = NULL;
        }
        $sql = 'INSERT INTO ' . prefix('options') . ' (`name`, `value`, `ownerid`, `theme`, `creator`) VALUES (' . db_quote($key) . ',';
        if (is_null($default)) {
            $sql .= 'NULL';
        } else {
            $sql .= db_quote($default);
        }
        $sql .= ',0,"",';
        if (is_null($creator)) {
            $sql .= 'NULL);';
        } else {
            $sql .= db_quote($creator) . ');';
        }
        if (query($sql, false)) {
            $_zp_options[strtolower($key)] = $default;
        }
    }
}
function loadLocalOptions($albumid, $theme) {
    global $_zp_options;
    
    $sql = "SELECT `name`, `value` FROM " . prefix('options') . ' WHERE `theme`=' . db_quote($theme) . ' AND `ownerid`=0';
    $optionlist = query_full_array($sql, false);
    if ($optionlist !== false) {
        foreach ($optionlist as $option) {
            $_zp_options[strtolower($option['name'])] = $option['value'];
        }
    }
    if ($albumid) {
        
        $sql = "SELECT `name`, `value` FROM " . prefix('options') . ' WHERE `theme`=' . db_quote($theme) . ' AND `ownerid`=' . $albumid;
        $optionlist = query_full_array($sql, false);
        if ($optionlist !== false) {
            foreach ($optionlist as $option) {
                $_zp_options[strtolower($option['name'])] = $option['value'];
            }
        }
    }
}
function renameOption($oldkey, $newkey) {
    $oldoption = getOption($oldkey);
    if ($oldoption) {
        setOptionDefault($newkey, $oldoption);
        purgeOption($oldkey);
    }
}
function purgeOption($key) {
    global $_zp_options;
    unset($_zp_options[strtolower($key)]);
    $sql = 'DELETE FROM ' . prefix('options') . ' WHERE `name`=' . db_quote($key);
    query($sql, false);
}
function getOptionList() {
    global $_zp_options;
    if (NULL == $_zp_options) {
        getOption('nil'); 
    }
    return $_zp_options;
}
function hasDynamicAlbumSuffix($path) {
    global $_zp_albumHandlers;
    return array_key_exists(getSuffix($path), $_zp_albumHandlers);
}
function isHandledAlbum($path) {
    global $_zp_albumHandlers;
    foreach (array_keys($_zp_albumHandlers) as $suffix) {
        if (file_exists($path . '.' . $suffix)) {
            
            return $suffix;
        }
    } return NULL;
}
function rewrite_get_album_image($albumvar, $imagevar) {
    global $_zp_rewritten, $_zp_albumHandlers;
    $ralbum = isset($_GET[$albumvar]) ? trim(sanitize_path($_GET[$albumvar]), '/') : NULL;
    $rimage = isset($_GET[$imagevar]) ? sanitize($_GET[$imagevar]) : NULL;
    
    if ($_zp_rewritten) {
        if (!empty($ralbum) && empty($rimage)) { 
            $path = internalToFilesystem(getAlbumFolder(SERVERPATH) . $ralbum);
            if (IM_SUFFIX) { 
                if (preg_match('|^(.*)' . preg_quote(IM_SUFFIX) . '$|', $ralbum, $matches)) {
                    
                    $rimage = basename($matches[1]);
                    $ralbum = trim(dirname($matches[1]), '/');
                    $path = internalToFilesystem(getAlbumFolder(SERVERPATH) . $ralbum);
                }
            } else { 
                if (Gallery::validImage($ralbum) || Gallery::validImageAlt($ralbum)) { 
                    $rimage = basename($ralbum);
                    $ralbum = trim(dirname($ralbum), '/');
                    $path = internalToFilesystem(getAlbumFolder(SERVERPATH) . $ralbum);
                }
            }
            if (!is_dir($path)) {
                if ($suffix = isHandledAlbum($path)) { 
                    $ralbum .= '.' . $suffix;
                }
            }
        }
        if (empty($ralbum)) {
            unset($_GET[$albumvar]);
        } else {
            $_GET[$albumvar] = $ralbum;
        }
        if (empty($rimage)) {
            unset($_GET[$imagevar]);
        } else {
            $_GET[$imagevar] = $rimage;
        }
    }
    return array($ralbum, $rimage);
}
function getImageCacheFilename($album8, $image8, $args) {
    global $_zp_supported_images, $_zp_cachefileSuffix;
    
    $album = internalToFilesystem($album8);
    if (is_array($image8)) {
        $image8 = $image8['name'];
    }
    if (IMAGE_CACHE_SUFFIX) {
        $suffix = IMAGE_CACHE_SUFFIX;
    } else {
        $suffix = @$_zp_cachefileSuffix[strtoupper(getSuffix($image8))];
        if (empty($suffix)) {
            $suffix = 'jpg';
        }
    }
    if (is_array($image8)) {
        $image = internalToFilesystem($image8['name']);
    } else {
        $image = stripSuffix(internalToFilesystem($image8));
    }
    
    $postfix = getImageCachePostfix($args);
    if (empty($album)) {
        $albumsep = '';
    } else {
        $albumsep = '/';
    }
    if (getOption('obfuscate_cache')) {
        $result = '/' . $album . $albumsep . sha1($image . HASH_SEED . $postfix) . '.' . $image . $postfix . '.' . $suffix;
    } else {
        $result = '/' . $album . $albumsep . $image . $postfix . '.' . $suffix;
    }
    return $result;
}
function makeSpecialImageName($image) {
    $filename = basename($image);
    $base = explode('/', str_replace(SERVERPATH . '/', '', dirname($image)));
    $sourceFolder = array_shift($base);
    $sourceSubfolder = implode('/', $base);
    return array('source' => $sourceFolder . '/' . $sourceSubfolder . '/' . $filename, 'name' => $sourceFolder . '_' . basename($sourceSubfolder) . '_' . $filename);
}
define('NO_WATERMARK', '!');
function getWatermarkParam($image, $use) {
    $watermark_use_image = $image->getWatermark();
    if (!empty($watermark_use_image) && ($image->getWMUse() & $use)) { 
        return $watermark_use_image;
    }
    $id = NULL;
    $album = $image->album;
    if ($use & (WATERMARK_FULL)) { 
        $watermark_use_image = getAlbumInherited($album->name, 'watermark', $id);
        if (empty($watermark_use_image)) {
            $watermark_use_image = FULLIMAGE_WATERMARK;
        }
    } else {
        if ($use & (WATERMARK_IMAGE)) { 
            $watermark_use_image = getAlbumInherited($album->name, 'watermark', $id);
            if (empty($watermark_use_image)) {
                $watermark_use_image = IMAGE_WATERMARK;
            }
        } else {
            if ($use & WATERMARK_THUMB) { 
                $watermark_use_image = getAlbumInherited($album->name, 'watermark_thumb', $id);
                if (empty($watermark_use_image)) {
                    $watermark_use_image = THUMB_WATERMARK;
                }
            }
        }
    }
    if (!empty($watermark_use_image)) {
        return $watermark_use_image;
    }
    return NO_WATERMARK; 
}
function getImageCachePostfix($args) {
    list($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbStandin, $passedWM, $adminrequest, $effects) = $args;
    $postfix_string = ($size ? "_$size" : "") .
                    ($width ? "_w$width" : "") .
                    ($height ? "_h$height" : "") .
                    ($cw ? "_cw$cw" : "") .
                    ($ch ? "_ch$ch" : "") .
                    (is_numeric($cx) ? "_cx$cx" : "") .
                    (is_numeric($cy) ? "_cy$cy" : "") .
                    ($thumb || $thumbStandin ? '_thumb' : '') .
                    ($adminrequest ? '_admin' : '') .
                    (($passedWM && $passedWM != NO_WATERMARK) ? '_' . $passedWM : '') .
                    ($effects ? '_' . $effects : '');
    return $postfix_string;
}
function getImageParameters($args, $album = NULL) {
    $thumb_crop = getOption('thumb_crop');
    $thumb_size = getOption('thumb_size');
    $thumb_crop_width = getOption('thumb_crop_width');
    $thumb_crop_height = getOption('thumb_crop_height');
    $thumb_quality = getOption('thumb_quality');
    $image_default_size = getOption('image_size');
    $quality = getOption('image_quality');
    
    $thumb = $crop = false;
    @list($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbstandin, $WM, $adminrequest, $effects) = $args;
    $thumb = $thumbstandin;
    switch ($size) {
        case 'thumb':
            $thumb = true;
            if ($thumb_crop) {
                $cw = $thumb_crop_width;
                $ch = $thumb_crop_height;
            }
            $size = round($thumb_size);
            break;
        case 'default':
            $size = $image_default_size;
            break;
        case 0:
        default:
            if (empty($size) || !is_numeric($size)) {
                $size = false; 
            } else {
                $size = round($size);
            }
            break;
    }
    
    
    list($width, $height, $cw, $ch, $quality) = array_map('sanitize_numeric', array($width, $height, $cw, $ch, $quality));
    if (!is_null($cx)) {
        $cx = sanitize_numeric($cx);
    }
    if (!is_null($cy)) {
        $cy = sanitize_numeric($cy);
    }
    if (!empty($cw) || !empty($ch)) {
        $crop = true;
    }
    if (is_null($effects)) {
        if ($thumb) {
            if (getOption('thumb_gray')) {
                $effects = 'gray';
            }
        } else {
            if (getOption('image_gray')) {
                $effects = 'gray';
            }
        }
    }
    if (empty($quality)) {
        if ($thumb) {
            $quality = round($thumb_quality);
        } else {
            $quality = getOption('image_quality');
        }
    }
    if (empty($WM)) {
        if (!$thumb) {
            if (!empty($album)) {
                $WM = getAlbumInherited($album, 'watermark', $id);
            }
            if (empty($WM)) {
                $WM = IMAGE_WATERMARK;
            }
        }
    }
    
    $args = array($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbstandin, $WM, $adminrequest, $effects);
    return $args;
}
function getImageProcessorURI($args, $album, $image) {
    list($size, $width, $height, $cw, $ch, $cx, $cy, $quality, $thumb, $crop, $thumbstandin, $passedWM, $adminrequest, $effects) = $args;
    $uri = WEBPATH . '/' . ZENFOLDER . '/i.php?a=' . $album;
    if (is_array($image)) {
        $uri .= '&i=' . $image['name'] . '&z=' . ($z = $image['source']);
    } else {
        $uri .= '&i=' . $image;
        $z = NULL;
    }
    if (empty($size)) {
        $args[0] = NULL;
    } else {
        $uri .= '&s=' . ($args[0] = (int) $size);
    }
    if ($width) {
        $uri .= '&w=' . ($args[1] = (int) $width);
    } else {
        $args[1] = NULL;
    }
    if ($height) {
        $uri .= '&h=' . ($args[2] = (int) $height);
    } else {
        $args[2] = NULL;
    }
    if (is_null($cw)) {
        $args[3] = NULL;
    } else {
        $uri .= '&cw=' . ($args[3] = (int) $cw);
    }
    if (is_null($ch)) {
        $args[4] = NULL;
    } else {
        $uri .= '&ch=' . ($args[4] = (int) $ch);
    }
    if (is_null($cx)) {
        $args[5] = NULL;
    } else {
        $uri .= '&cx=' . ($args[5] = (int) $cx);
    }
    if (is_null($cy)) {
        $args[6] = NULL;
    } else {
        $uri .= '&cy=' . ($args[6] = (int) $cy);
    }
    if ($quality) {
        $uri .= '&q=' . ($args[7] = (int) $quality);
    } else {
        $args[7] = NULL;
    }
    $args[8] = NULL;
    if ($crop) {
        $uri .= '&c=' . ($args[9] = 1);
    } else {
        $args[9] = NULL;
    }
    if ($thumb || $thumbstandin) {
        $uri .= '&t=' . ($args[10] = 1);
    } else {
        $args[10] = NULL;
    }
    if ($passedWM) {
        $uri .= '&wmk=' . $passedWM;
    } else {
        $args[11] = NULL;
    }
    if ($adminrequest) {
        $args[12] = true;
        $uri .= '&admin=1';
    } else {
        $args[12] = false;
    }
    if ($effects) {
        $uri .= '&effects=' . $effects;
    } else {
        $args[13] = NULL;
    }
    $args[14] = $z;
    $uri .= '&check=' . sha1(HASH_SEED . serialize($args));
    $uri = zp_apply_filter('image_processor_uri', $uri);
    return $uri;
}
define('MAX_SIZE', getOption('image_max_size'));
function getImageArgs($set) {
    $args = array(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    if (isset($set['s'])) { 
        if (is_numeric($s = $set['s'])) {
            if ($s) {
                $args[0] = (int) min(abs($s), MAX_SIZE);
            }
        } else {
            $args[0] = sanitize($set['s']);
        }
    } else {
        if (!isset($set['w']) && !isset($set['h'])) {
            $args[0] = MAX_SIZE;
        }
    }
    if (isset($set['w'])) { 
        $args[1] = (int) min(abs(sanitize_numeric($set['w'])), MAX_SIZE);
    }
    if (isset($set['h'])) { 
        $args[2] = (int) min(abs(sanitize_numeric($set['h'])), MAX_SIZE);
    }
    if (isset($set['cw'])) { 
        $args[3] = (int) sanitize_numeric(($set['cw']));
    }
    if (isset($set['ch'])) { 
        $args[4] = (int) sanitize_numeric($set['ch']);
    }
    if (isset($set['cx'])) { 
        $args[5] = (int) sanitize_numeric($set['cx']);
    }
    if (isset($set['cy'])) { 
        $args[6] = (int) sanitize_numeric($set['cy']);
    }
    if (isset($set['q'])) { 
        $args[7] = (int) sanitize_numeric($set['q']);
    }
    if (isset($set['c'])) {
        $args[9] = (int) sanitize($set['c']);
    }
    if (isset($set['t'])) { 
        $args[10] = (int) sanitize($set['t']);
    }
    if (isset($set['wmk']) && !isset($_GET['admin'])) { 
        $args[11] = sanitize($set['wmk']);
    }
    $args[12] = (bool) isset($_GET['admin']); 
    if (isset($set['effects'])) { 
        $args[13] = sanitize($set['effects']);
    }
    if (isset($set['z'])) { 
        $args[14] = sanitize($set['z']);
    }
    return $args;
}
function getImageProcessorURIFromCacheName($match, $watermarks) {
    $set = array();
    $done = false;
    $params = explode('_', stripSuffix($match));
    while (!$done && count($params) > 1) {
        $check = array_pop($params);
        if (is_numeric($check)) {
            $set['s'] = $check;
            break;
        }
        $c = substr($check, 0, 1);
        if ($c == 'w' || $c == 'h') {
            if (is_numeric($v = substr($check, 1))) {
                $set[$c] = (int) $v;
                continue;
            }
        }
        if ($c == 'c') {
            $c = substr($check, 0, 2);
            if (is_numeric($v = substr($check, 2))) {
                $set[$c] = (int) $v;
                continue;
            }
        }
        if (!isset($set['w']) && !isset($set['h']) && !isset($set['s'])) {
            if (!isset($set['wm']) && in_array($check, $watermarks)) {
                $set['wmk'] = $check;
            } else if ($check == 'thumb') {
                $set['t'] = true;
            } else {
                $set['effects'] = $check;
            }
        } else {
            array_push($params, $check);
            break;
        }
    }
    if (!isset($set['wmk'])) {
        $set['wmk'] = '!';
    }
    $image = preg_replace('~.*/' . CACHEFOLDER . '/~', '', implode('_', $params)) . '.' . getSuffix($match);
    
    $album = dirname($image);
    $image = preg_replace('~^[0-9a-f]{' . CACHE_HASH_LENGTH . '}\.~', '', basename($image));
    $image = $album . '/' . $image;
    return array($image, getImageArgs($set));
}
function getImageURI($args, $album, $image, $mtime) {
    $cachefilename = getImageCacheFilename($album, $image, $args);
    if (OPEN_IMAGE_CACHE && file_exists(SERVERCACHE . $cachefilename) && (!$mtime || filemtime(SERVERCACHE . $cachefilename) >= $mtime)) {
        return WEBPATH . '/' . CACHEFOLDER . imgSrcURI($cachefilename) . '?cached=' . filemtime(SERVERCACHE . $cachefilename);
    } else {
        return getImageProcessorURI($args, $album, $image);
    }
}
function getAllowedTags($which) {
    global $_user_tags, $_style_tags, $_default_tags;
    switch ($which) {
        case 'allowed_tags':
            if (is_null($_user_tags)) {
                $user_tags = "(" . getOption('allowed_tags') . ")";
                $allowed_tags = parseAllowedTags($user_tags);
                if ($allowed_tags === false) { 
                    $allowed_tags = array();
                }
                $_user_tags = $allowed_tags;
            }
            return $_user_tags;
            break;
        case 'style_tags':
            if (is_null($_style_tags)) {
                $style_tags = "(" . getOption('style_tags') . ")";
                $allowed_tags = parseAllowedTags($style_tags);
                if ($allowed_tags === false) { 
                    $allowed_tags = array();
                }
                $_style_tags = $allowed_tags;
            }
            return $_style_tags;
            break;
        case 'allowed_tags_default':
            if (is_null($_default_tags)) {
                $default_tags = "(" . getOption('allowed_tags_default') . ")";
                $allowed_tags = parseAllowedTags($default_tags);
                if ($allowed_tags === false) { 
                    $allowed_tags = array();
                }
                $_default_tags = $allowed_tags;
            }
            return $_default_tags;
            break;
    }
    return array();
}
function rewrite_path($rewrite, $plain, $webpath = NULL) {
    if (is_null($webpath)) {
        if (class_exists('seo_locale')) {
            $webpath = seo_locale::localePath();
        } else {
            $webpath = WEBPATH;
        }
    }
    if (MOD_REWRITE) {
        $path = $rewrite;
    } else {
        $path = $plain;
    }
    if ($path[0] == "/") {
        $path = substr($path, 1);
    }
    return $webpath . "/" . $path;
}
function parse_query($str) {
    $pairs = explode('&', $str);
    $params = array();
    foreach ($pairs as $pair) {
        if (strpos($pair, '=') === false) {
            $params[$pair] = NULL;
        } else {
            list($name, $value) = explode('=', $pair, 2);
            $params[$name] = $value;
        }
    }
    return $params;
}
function build_query($parts) {
    $q = '';
    foreach ($parts as $name => $value) {
        $q .= $name . '=' . $value . '&';
    }
    return substr($q, 0, -1);
}
function build_url($parts) {
    $u = '';
    if (isset($parts['scheme'])) {
        $u .= $parts['scheme'] . '://';
    }
    if (isset($parts['host'])) {
        $u .= $parts['host'];
    }
    if (isset($parts['port'])) {
        $u .= ':' . $parts['port'];
    }
    if (isset($parts['path'])) {
        if (empty($u)) {
            $u = $parts['path'];
        } else {
            $u .= '/' . ltrim($parts['path'], '/');
        }
    }
    if (isset($parts['query'])) {
        $u .= '?' . $parts['query'];
    }
    if (isset($parts['fragment '])) {
        $u .= '#' . $parts['fragment '];
    }
    return $u;
}
function pathurlencode($path) {
    $parts = parse_url($path);
    if (isset($parts['query'])) {
        
        $pairs = parse_query($parts['query']);
        if (preg_match('/^a=.*\&i=?/i', $parts['query'])) { 
            $index = 'a';
            foreach ($pairs as $p => $q) {
                switch ($p) {
                    case 'i':
                        $index = 'i';
                    case 'a':
                        break;
                    default:
                        if (is_null($q)) {
                            $pairs[$index] .= '&' . $p;
                        } else if (in_array($p, array('s', 'w', 'h', 'cw', 'ch', 'cx', 'cy', 'q', 'c', 't', 'wmk', 'admin', 'effects', 'z'))) { 
                            break 2;
                        } else {
                            $pairs[$index] .= '&' . $p . '=' . $q;
                        }
                        unset($pairs[$p]);
                        break;
                }
            }
        }
        foreach ($pairs as $name => $value) {
            if ($value) {
                $pairs[$name] = implode("/", array_map("rawurlencode", explode("/", $value)));
            }
        }
        $parts['query'] = build_query($pairs);
    }
    $parts['path'] = implode("/", array_map("rawurlencode", explode("/", $parts['path'])));
    return build_url($parts);
}
function getAlbumFolder($root = SERVERPATH) {
    global $_zp_album_folder, $_zp_conf_vars;
    if (is_null($_zp_album_folder)) {
        if (!isset($_zp_conf_vars['external_album_folder']) || empty($_zp_conf_vars['external_album_folder'])) {
            if (!isset($_zp_conf_vars['album_folder']) || empty($_zp_conf_vars['album_folder'])) {
                $_zp_album_folder = $_zp_conf_vars['album_folder'] = '/' . ALBUMFOLDER . '/';
            } else {
                $_zp_album_folder = str_replace('\\', '/', $_zp_conf_vars['album_folder']);
            }
        } else {
            $_zp_conf_vars['album_folder_class'] = 'external';
            $_zp_album_folder = str_replace('\\', '/', $_zp_conf_vars['external_album_folder']);
        }
        if (substr($_zp_album_folder, -1) != '/')
            $_zp_album_folder .= '/';
    }
    $root = str_replace('\\', '/', $root);
    switch (@$_zp_conf_vars['album_folder_class']) {
        default:
            $_zp_conf_vars['album_folder_class'] = 'std';
        case 'std':
            return $root . $_zp_album_folder;
        case 'in_webpath':
            if (WEBPATH) { 
                $pos = strrpos($root, WEBPATH);
                if ($pos !== false) {
                    $root = substr_replace($root, '', $pos, strlen(WEBPATH));
                }
                if ($root == '/') {
                    $root = '';
                }
            }
            return $root . $_zp_album_folder;
        case 'external':
            return $_zp_album_folder;
    }
}
function switchLog($log) {
    $dir = getcwd();
    chdir(SERVERPATH . '/' . DATA_FOLDER);
    $list = safe_glob($log . '-*.log');
    if (empty($list)) {
        $counter = 1;
    } else {
        sort($list);
        $last = array_pop($list);
        preg_match('|' . $log . '-(.*).log|', $last, $matches);
        $counter = $matches[1] + 1;
    }
    chdir($dir);
    @copy(SERVERPATH . '/' . DATA_FOLDER . '/' . $log . '.log', SERVERPATH . '/' . DATA_FOLDER . '/' . $log . '-' . $counter . '.log');
    if (getOption($log . '_log_mail')) {
        zp_mail(sprintf(gettext('%s log size limit exceeded'), $log), sprintf(gettext('The %1$s log has exceeded its size limit and has been renamed to %2$s.'), $log, $log . '-' . $counter . '.log'));
    }
}
function debugLog($message, $reset = false) {
    if (defined('SERVERPATH')) {
        global $_zp_mutex;
        $path = SERVERPATH . '/' . DATA_FOLDER . '/debug.log';
        $me = getmypid();
        if (is_object($_zp_mutex))
            $_zp_mutex->lock();
        if ($reset || ($size = @filesize($path)) == 0 || (defined('DEBUG_LOG_SIZE') && DEBUG_LOG_SIZE && $size > DEBUG_LOG_SIZE)) {
            if (!$reset && $size > 0) {
                switchLog('debug');
            }
            $f = fopen($path, 'w');
            if ($f) {
                if (!class_exists('zpFunctions') || hasPrimaryScripts()) {
                    $clone = '';
                } else {
                    $clone = ' ' . gettext('clone');
                }
                fwrite($f, '{' . $me . ':' . gmdate('D, d M Y H:i:s') . " GMT} Zenphoto v" . ZENPHOTO_VERSION . $clone . "\n");
            }
        } else {
            $f = fopen($path, 'a');
            if ($f) {
                fwrite($f, '{' . $me . ':' . gmdate('D, d M Y H:i:s') . " GMT}\n");
            }   
        }
        if ($f) {
            fwrite($f, "  " . $message . "\n");
            fclose($f);
            clearstatcache();
            if (defined('LOGS_MOD')) {
                @chmod($path, LOGS_MOD);
            }
        }
        if (is_object($_zp_mutex))
            $_zp_mutex->unlock();
    }
}
function instrument($point) {
    global $_zp_timer;
    $now = microtime(true);
    if (empty($_zp_timer)) {
        $delta = '';
    } else {
        $delta = ' (' . ($now - $_zp_timer) . ')';
    }
    $_zp_timer = microtime(true);
    debugLogBacktrace($point . ' ' . $now . $delta);
}
function parse_size($size) {
    $suffixes = array(
                    ''   => 1,
                    'k'  => 1024,
                    'm'  => 1048576, 
                    'g'  => 1073741824, 
    );
    if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) {
        return $match[1] * $suffixes[strtolower($match[2])];
    }
}
function getAlbumArray($albumstring, $includepaths = false) {
    if ($includepaths) {
        $array = array($albumstring);
        while ($slashpos = strrpos($albumstring, '/')) {
            $albumstring = substr($albumstring, 0, $slashpos);
            array_unshift($array, $albumstring);
        }
        return $array;
    } else {
        return explode('/', $albumstring);
    }
}
function imgSrcURI($uri) {
    if (UTF8_IMAGE_URI)
        return filesystemToInternal($uri);
    return $uri;
}
function getSuffix($filename) {
    return strtolower(substr(strrchr($filename, "."), 1));
}
function stripSuffix($filename) {
    return str_replace(strrchr($filename, "."), '', $filename);
}
function getAlbumInherited($folder, $field, &$id) {
    $folders = explode('/', filesystemToInternal($folder));
    $album = array_shift($folders);
    $like = ' LIKE ' . db_quote(db_LIKE_escape($album));
    while (count($folders) > 0) {
        $album .= '/' . array_shift($folders);
        $like .= ' OR `folder` LIKE ' . db_quote(db_LIKE_escape($album));
    }
    $sql = 'SELECT `id`, `' . $field . '` FROM ' . prefix('albums') . ' WHERE `folder`' . $like;
    $result = query_full_array($sql);
    if (!is_array($result))
        return '';
    while (count($result) > 0) {
        $try = array_pop($result);
        if (!empty($try[$field])) {
            $id = $try['id'];
            return $try[$field];
        }
    }
    return '';
}
function themeSetup($album) {
    
    $id = NULL;
    $theme = getAlbumInherited(filesystemToInternal($album), 'album_theme', $id);
    if (empty($theme)) {
        $galleryoptions = getSerializedArray(getOption('gallery_data'));
        $theme = @$galleryoptions['current_theme'];
    }
    loadLocalOptions($id, $theme);
    return $theme;
}
function accessAllAlbums($action) {
    global $_zp_admin_album_list, $_zp_loggedin;
    if (zp_loggedin(MANAGE_ALL_ALBUM_RIGHTS)) {
        if (zp_loggedin($action))
            return true;
    }
    if (zp_loggedin(ALL_ALBUMS_RIGHTS) && ($action == LIST_RIGHTS)) { 
        return $_zp_loggedin;
    }
    return false;
}
function getWatermarkPath($wm) {
    $path = SERVERPATH . '/' . ZENFOLDER . '/watermarks/' . internalToFilesystem($wm) . '.png';
    if (!file_exists($path)) {
        $path = SERVERPATH . '/' . USER_PLUGIN_FOLDER . '/watermarks/' . internalToFilesystem($wm) . '.png';
    }
    return $path;
}
function secureServer() {
    if (isset($_SERVER['HTTPS'])) {
        if ('on' == strtolower($_SERVER['HTTPS'])) {
            return true;
        }
        if ('1' == $_SERVER['HTTPS']) {
            return true;
        }
    } elseif (isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] )) {
        return true;
    } elseif (isset($_SERVER['HTTP_FORWARDED']) && preg_match("/^(.+[,;])?\s*proto=https\s*([,;].*)$/", strtolower($_SERVER['HTTP_FORWARDED']))) {
        return true;
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && ('https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']))) {
        return true;
    }
    return false;
}
function getRequestURI() {
    if (array_key_exists('REQUEST_URI', $_SERVER)) {
        $uri = sanitize($_SERVER['REQUEST_URI']);
        preg_match('|^(http[s]*\://[a-zA-Z0-9\-\.]+/?)*(.*)$|xis', $uri, $matches);
        $uri = $matches[2];
        if (!empty($matches[1])) {
            $uri = '/' . $uri;
        }
    } else {
        $uri = sanitize(@$_SERVER['SCRIPT_NAME']);
    }
    return urldecode(str_replace('\\', '/', $uri));
}
function safe_glob($pattern, $flags = 0) {
    $split = explode('/', $pattern);
    $match = '/^' . strtr(addcslashes(array_pop($split), '\\.+^$(){}=!<>|'), array('*' => '.*', '?' => '.?')) . '$/i';
    $path_return = $path = implode('/', $split);
    if (empty($path)) {
        $path = '.';
    } else {
        $path_return = $path_return . '/';
    }
    if (!is_dir($path))
        return array();
    if (($dir = opendir($path)) !== false) {
        $glob = array();
        while (($file = readdir($dir)) !== false) {
            if (@preg_match($match, $file) && $file[0] != '.') {
                if (is_dir("$path/$file")) {
                    if ($flags & GLOB_MARK)
                        $file.='/';
                    $glob[] = $path_return . $file;
                } else if (!is_dir("$path/$file") && !($flags & GLOB_ONLYDIR)) {
                    $glob[] = $path_return . $file;
                }
            }
        }
        closedir($dir);
        if (!($flags & GLOB_NOSORT))
            sort($glob);
        return $glob;
    } else {
        return array();
    }
}
function checkInstall() {
    if ($i = getOption('zenphoto_install')) {
        $install = getSerializedArray($i);
    } else {
        $install = array('ZENPHOTO' => '0.0.0');
    }
    if ($install['ZENPHOTO'] && $install['ZENPHOTO'] != ZENPHOTO_VERSION || ((time() & 7) == 0) && OFFSET_PATH != 2 && $i != serialize(installSignature())) {
        require_once(dirname(__FILE__) . '/reconfigure.php');
        reconfigureAction(0);
    }
}
function exitZP() {
    IF (function_exists('db_close'))
        db_close();
    exit();
}
function installSignature() {
    $all_algos = hash_algos();
    $algo = 'sha256';
    if(!in_array($algo, $all_algos)) { 
        $algo = 'sha1';
    }
    $testFiles = array('template-functions.php'  => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/template-functions.php'),
                    'functions-filter.php'       => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/functions-filter.php'),
                    'lib-auth.php'                       => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/lib-auth.php'),
                    'lib-utf8.php'                       => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/lib-utf8.php'),
                    'functions.php'                      => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/functions.php'),
                    'functions-basic.php'            => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/functions-basic.php'),
                    'functions-controller.php' => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/functions-controller.php'),
                    'functions-image.php'            => hash_file($algo, SERVERPATH . '/' . ZENFOLDER . '/functions-image.php'));
    if (isset($_SERVER['SERVER_SOFTWARE'])) {
        $s = $_SERVER['SERVER_SOFTWARE'];
    } else {
        $s = 'software unknown';
    }
    $dbs = db_software();
    $version = ZENPHOTO_VERSION;
    $i = strpos($version, '-');
    if ($i !== false) {
        $version = substr($version, 0, $i);
    }
    $signature_array = array_merge($testFiles, array(
            'SERVER_SOFTWARE' => $s,
            'ZENPHOTO' => $version,
            'FOLDER' => dirname(SERVERPATH . '/' . ZENFOLDER),
            'DATABASE' => $dbs['application'] . ' ' . $dbs['version']
                    )
    );
    $signature_array['SIGNATURE_HASH'] = hash($algo, implode(array_values($signature_array))); 
    return $signature_array;
}
function zp_session_start() {
    if (session_id() == '') {
        
        $cookieinfo = session_get_cookie_params();
        
        if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
            $cookieinfo['secure'] = secureServer();
            $cookieinfo['httponly'] = true;
            $cookieinfo['samesite'] = 'Lax';
            session_set_cookie_params($cookieinfo);
        } else {
            session_set_cookie_params($cookieinfo['lifetime'], $cookieinfo['path'], $cookieinfo['domain'], secureServer(), true);
        }
        session_start();
    }
}
function zp_session_destroy() {
    $CookieInfo = session_get_cookie_params();
    zp_setCookie(session_name(), '', time() - 42000, $CookieInfo['path'], secureServer(), true);
    if (session_id() != '') {
        $_SESSION = array();
        session_destroy();
    }
}
function getDefaultRewriteTokens($token = null) {
    global $_zp_default_rewritetokens; 
    if(!is_array($_zp_default_rewritetokens)) {
        $zp_cfg = file_get_contents(SERVERPATH . '/' . ZENFOLDER . '/zenphoto_cfg.txt');
        $i = strpos($zp_cfg, "\$conf['special_pages']");
        $j = strpos($zp_cfg, '//', $i);
        eval(substr($zp_cfg, $i, $j - $i));
        $_zp_default_rewritetokens = $conf['special_pages'];
        unset($conf);
    }
    if(isset($_zp_default_rewritetokens[$token])) {
        return $_zp_default_rewritetokens[$token];
    } else {
        return $_zp_default_rewritetokens;
    }
}
function addMissingDefaultRewriteTokens() {
    global $_zp_conf_vars;
    $tokens = array_keys(getDefaultRewriteTokens(null));
    foreach($tokens as $token) {
        if (!isset($_zp_conf_vars['special_pages'][$token])) {
            $_zp_conf_vars['special_pages'][$token] = getDefaultRewriteTokens($token);
        }
    }
}
function curlRequest($uri, $options = array()) {
    if (function_exists('curl_init')) {
        $defaultoptions = array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_TIMEOUT => 2000,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_MAXREDIRS => 3
        );
        if (empty($options) || !is_array($options)) {
            $options = $defaultoptions;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt_array($ch, $options);
        $curl_exec = curl_exec($ch);
        if ($curl_exec === false) {
            debugLog(gettext('ERROR: cURL request failed: ') . curl_error($ch));
            $result = false;
        } else if (trim($curl_exec) == false) {
            debugLogVar(gettext('NOTICE: cURL request not successful.'), curl_getinfo($ch));
            $result = false;
        } else {
            $result = $curl_exec;
        }
        curl_close($ch);
        return $result;
    }
    debugLog(gettext('ERROR: Your server does not support cURL.'));
    return false;
}
function generateImageCacheFile($imageuri) {
    $uri = $imageuri;
    if (strpos($imageuri, SERVER_HTTP_HOST) === false) {
        $uri = SERVER_HTTP_HOST . pathurlencode($uri) . '&returnmode';
    }
    return curlRequest($uri);
}
function httpsRedirect($type = 'backend') {
    $redirect_url = SERVER_HTTP_HOST . getRequestURI();
    switch ($type) {
        case 'backend':
            if (SERVER_PROTOCOL == 'https_admin' || SERVER_PROTOCOL == 'https') {
                
                if (!secureServer()) {
                    redirectURL($redirect_url);
                }
            }
            break;
        case 'frontend':
            if ((PROTOCOL == 'https' && !secureServer()) || (PROTOCOL == 'http' && secureServer())) {
                redirectURL($redirect_url, '301');
            }
            break;
    }
}
function redirectURL($url, $statuscode = null, $allowexternal = false) {
    $redirect_url = sanitize($url);
    if (!$allowexternal) {
        sanitizeRedirect($redirect_url);
    }
    switch ($statuscode) {
        case '200':
            header("HTTP/1.0 200 OK");
            header("Status: 200 OK");
            break;
        case '301':
            header("HTTP/1.1 301 Moved Permanently");
            header("Status: 301 Moved Permanently");
            break;
        case '302':
            header("HTTP/1.1 302 Found");
            header("Status: 302 Found");
            break;
        case '401':
            header("HTTP/1.1 401 Unauthorized");
            header("Status: 401 Unauthorized");
            break;
        case '403':
            header("HTTP/1.1 403 Forbidden");
            header("Status: 403 Forbidden");
            break;
        case '404':
            header("HTTP/1.1 404 Not found");
            header("Status: 404 Not found");
            break;
    }
    header('Location: ' . $redirect_url);
    exitZP();
}
function sanitizeRedirect($redirectTo) {
    $redirect = NULL;
    if ($redirectTo && $redir = parse_url($redirectTo)) {
        if (isset($redir['scheme']) && isset($redir['host'])) {
            $redirect = $redir['scheme'] . '://' . sanitize($redir['host']);
        }
        if (defined('SERVER_HTTP_HOST') && $redirect != SERVER_HTTP_HOST) {
            $redirect = SERVER_HTTP_HOST;
        }
        if (defined('WEBPATH') && !empty(WEBPATH) && strpos($redirectTo, WEBPATH) === false) {
            $redirect .= WEBPATH;
        } 
        if (isset($redir['path'])) {
            $path = urldecode(sanitize($redir['path']));
            
            if(substr($path , 0, 1) != '/') {
                $path = '/' . $path;
            } 
            $redirect .= $path;
        }
        if (isset($redir['query'])) {
            $redirect .= '?' . sanitize($redir['query']);
        }
        if (isset($redir['fragment'])) {
            $redirect .= '#' . sanitize($redir['fragment']);
        }
    }
    return $redirect;
}
class zpMutex {
    private $locked = NULL;
    private $ignoreUseAbort = NULL;
    private $mutex = NULL;
    private $lock = NULL;
    function __construct($lock = 'zP', $concurrent = NULL) {
        
        if (function_exists('flock') && defined('SERVERPATH')) {
            if ($concurrent) {
                If ($subLock = self::which_lock($lock, $concurrent)) {
                    $this->lock = $lock . '_' . $subLock;
                }
            } else {
                $this->lock = $lock;
            }
        }
        return $this->lock;
    }
    
    
    private static function which_lock($lock, $concurrent) {
        global $_zp_mutex;
        $counter_file = SERVERPATH . '/' . DATA_FOLDER . '/' . MUTEX_FOLDER . '/' . $lock . '_counter';
        $_zp_mutex->lock();
        
        if (@file_put_contents($counter_file, $count = (((int) @file_get_contents($counter_file)) + 1) % $concurrent)) {
            $count++;
        } else {
            $count = false;
        }
        $_zp_mutex->unlock();
        return $count;
    }
    function __destruct() {
        if ($this->locked) {
            $this->unlock();
        }
    }
    public function lock() {
        
        
        if (!$this->locked && $this->lock) {
            if ($this->mutex = @fopen(SERVERPATH . '/' . DATA_FOLDER . '/' . MUTEX_FOLDER . '/' . $this->lock, 'wb')) {
                if (flock($this->mutex, LOCK_EX)) {
                    $this->locked = true;
                    
                    
                    $this->ignoreUserAbort = ignore_user_abort(true);
                }
            }
        }
        return $this->locked;
    }
    
    public function unlock() {
        if ($this->locked) {
            
            $this->locked = false;
            ignore_user_abort($this->ignoreUserAbort); 
            flock($this->mutex, LOCK_UN);
            fclose($this->mutex);
            return true;
        }
        return false;
    }
}