Source code of file oscpmwin_v0.1.2.450/oscpm1_upload.txt from the
osCommerce Product Manager for Windows.
0000: <?php
0001: // osCommerce Product Manager for Windows (oscpmwin).
0002: // Copyright �2003,2004,2005 by Mario A. Valdez-Ramirez.
0003:
0004: // You can contact Mario A. Valdez-Ramirez
0005: // by email at mario@mariovaldez.org or paper mail at
0006: // Olmos 809, San Nicolas, NL. 66495, Mexico.
0007:
0008: // This program is free software; you can redistribute it and/or modify
0009: // it under the terms of the GNU General Public License as published by
0010: // the Free Software Foundation; either version 2 of the License, or (at
0011: // your option) any later version.
0012:
0013: // This program is distributed in the hope that it will be useful, but
0014: // WITHOUT ANY WARRANTY; without even the implied warranty of
0015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0016: // General Public License for more details.
0017:
0018: // You should have received a copy of the GNU General Public License
0019: // along with this program; if not, write to the Free Software
0020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0021:
0022:
0023: // ==================================
0024: // Start of configuration options...
0025: // ==================================
0026: $opm_images_directory = "images/";
0027: $opm_enable_logfile = false;
0028: $opm_enable_extralog = false;
0029: $opm_debug_file = "opm_debug.txt";
0030: $opm_browser_debug = false;
0031: $opm_enable_auth = true;
0032: $opm_password_override = "";
0033: $opm_encoded_data = false;
0034: $opm_upload_perms = "0644";
0035: $opm_raw_records = true;
0036: $opm_timezone_shift = 0;
0037:
0038: $opm_cfg_useproxy = false;
0039: $opm_cfg_proxyaddress = "192.168.0.1";
0040: $opm_cfg_proxyport = 3128;
0041:
0042: // ==================================
0043: // End of configuration options...
0044: // ==================================
0045:
0046:
0047: $opm_httpconn_timeout = 60; // 60 seconds
0048: $opm_tag_recordbegin = "BR"; // BR
0049: $opm_tag_recordend = "ER"; // ER
0050: $opm_tag_field = "DF"; // DF
0051: $opm_tag_databegin = "["; // [
0052: $opm_tag_dataend = "]"; // ]
0053:
0054:
0055: $opm_script_version = "0.1.15";
0056: error_reporting(E_ALL & ~E_NOTICE);
0057:
0058: $opm_realpath = str_replace (basename (__FILE__), "", __FILE__) . $opm_images_directory;
0059: $opm_tmpsoftwarename = explode (" ", $HTTP_SERVER_VARS["SERVER_SOFTWARE"]);
0060: $opm_tmposname = explode (" ", php_uname ());
0061: $opm_useragent = "User-Agent: OSCPMWin/$opm_script_version " . $opm_tmpsoftwarename[0] . " MySQL/unknown " . $opm_tmposname[0];
0062:
0063:
0064:
0065: @set_time_limit (300);
0066:
0067: if (!$opm_upload_perms) {
0068: $opm_upload_perms = "0600";
0069: }
0070: // Get all parameters about the request.
0071: if (!$opm_browser_debug) {
0072: $opm_passwordhash = strtoupper (trim ($_POST["Pw"]));
0073: $opm_operation = strtolower (trim ($_POST["Op"]));
0074: $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["Fn"])))));
0075: $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["SD"])))));
0076: $opm_reqversion = trim ($_POST["Vn"]);
0077: $opm_logintimestamp = trim ($_POST["TS"]);
0078: }
0079: else {
0080: $opm_passwordhash = strtoupper (trim ($_GET["Pw"]));
0081: $opm_operation = strtolower (trim ($_GET["Op"]));
0082: $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["Fn"])))));
0083: $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["SD"])))));
0084: $opm_reqversion = trim ($_GET["Vn"]);
0085: $opm_logintimestamp = trim ($_GET["TS"]);
0086: }
0087: if (($opm_filename{0} == "/") || ($opm_filename{0} == ".")) {
0088: $opm_filename = substr ($opm_filename, 1, strlen ($opm_filename) - 1);
0089: }
0090:
0091: // Log basic information about the request.
0092: if ($opm_enable_extralog) {
0093: if (!$opm_browser_debug) {
0094: opm_FNDebug_Log (print_r ($_POST, true));
0095: }
0096: else {
0097: opm_FNDebug_Log (print_r ($_GET, true));
0098: }
0099: }
0100: else {
0101: opm_FNDebug_Log ("");
0102: }
0103: opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0104: opm_FNDebug_Log ("PASSWORDHASH=" . $opm_passwordhash);
0105: opm_FNDebug_Log ("OPERATION=" . $opm_operation);
0106: opm_FNDebug_Log ("FILENAME=" . $opm_filename);
0107: opm_FNDebug_Log ("SUBDIR=" . $opm_subdir);
0108: opm_FNDebug_Log ("REQVERSION=" . $opm_reqversion);
0109: opm_FNDebug_Log ("LOGINTIMESTAMP=" . $opm_logintimestamp);
0110:
0111:
0112: // Include osCommerce database configuration and functions.
0113: require ("includes/configure.php");
0114: require (DIR_WS_INCLUDES . "database_tables.php");
0115: require (DIR_WS_FUNCTIONS . "database.php");
0116:
0117:
0118: if ($opm_browser_debug) { echo "<pre>"; }
0119:
0120: // Check if script version is the expected by client.
0121: if (($opm_script_version != $opm_reqversion) && ($opm_operation)) {
0122: opm_FNDebug_Log ("ERROR wrong script version. Expected $opm_reqversion, but we are $opm_script_version.");
0123: echo "ERROR WRONG SCRIPT VERSION";
0124: return;
0125: }
0126:
0127: // Check if the image directory is valid.
0128: if (!is_dir ($opm_realpath) && ($opm_operation)) {
0129: opm_FNDebug_Log ("ERROR cannot find the images directory. Expecting $opm_realpath exist.");
0130: echo "ERROR IMAGE DIRECTORY NOT FOUND";
0131: return;
0132: }
0133:
0134: // Get osCommerce DB password and compare it with client password.
0135: // Unless no operation is requested.
0136: if ($opm_operation) {
0137: if ($opm_enable_auth) {
0138: if (defined ("DB_SERVER_PASSWORD") || ($opm_password_override != "")) {
0139: if ($opm_password_override != "") {
0140: opm_FNDebug_Log ("Password override is enabled.");
0141: $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . $opm_password_override));
0142: }
0143: else {
0144: $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . DB_SERVER_PASSWORD));
0145: }
0146: opm_FNDebug_Log ("SERVERPASSWORDHASH=" . $opm_serverpassword);
0147: if ($opm_serverpassword != $opm_passwordhash) {
0148: opm_FNDebug_Log ("ERROR the server and client passwords do not match.");
0149: echo "ERROR PASSWORD MISMATCH";
0150: return;
0151: }
0152: }
0153: else {
0154: opm_FNDebug_Log ("ERROR the server password was not found.");
0155: echo "ERROR CANNOT FIND SERVER PASSWORD";
0156: return;
0157: }
0158: }
0159: else {
0160: opm_FNDebug_Log ("Password authentication is disabled.");
0161: }
0162: }
0163: else {
0164: $opm_simpletest_dbok = false;
0165: $opm_simpletest_imgok = false;
0166: echo "<html><head><title>osCommerce Product Manager (OSCPMWin).</title>\n";
0167: echo "<style>\nBODY { font-family: sans-serif; background-color: #FFFFFF; }\n";
0168: echo "LI { padding: 5px; }\n";
0169: echo "H3 { padding: 5px 5px 5px 20px; }\n";
0170: echo "A { color: #0000FF; text-decoration: none; }\n";
0171: echo "A:hover { color: #FF0000; text-decoration: underline; }\n";
0172: echo "DIV { border: solid 2px #000000; margin: 10px 20px 10px 20px; }";
0173: echo ".ok { background-color: #CCFFCC; }\n";
0174: echo ".notok { background-color: #FFCCCC; }\n";
0175: echo ".info { background-color: #EEEEFF; }\n";
0176: echo ".links { background-color: #EEEEEE; }\n";
0177: echo "</style>\n";
0178: echo "</head><body>\n";
0179: echo "<h3>osCommerce Product Manager (OSCPMWin),<br>server-side script $opm_script_version</h3>\n";
0180: $opm_simpletest_dbok = (boolean) (tep_db_connect ());
0181: $opm_simpletest_imgok = (boolean) (file_exists ($opm_realpath) && is_readable ($opm_realpath) && is_writable ($opm_realpath) && is_dir ($opm_realpath));
0182: if ($opm_simpletest_dbok && $opm_simpletest_imgok) {
0183: echo "<div class=\"ok\">";
0184: }
0185: else {
0186: echo "<div class=\"notok\">";
0187: }
0188: echo "<ul>\n";
0189: if ($opm_simpletest_dbok) {
0190: echo "<li><strong>Database connection seems OK.</strong></li>\n";
0191: }
0192: else {
0193: echo "<li><strong>Database connection failed.</strong><br><small>Check that the database server is working properly. This should not happen if the web store is working properly.</small></li>\n";
0194: }
0195: if ($opm_simpletest_imgok) {
0196: echo "<li><strong>Image directory seems OK.</strong></li>\n";
0197: }
0198: else {
0199: echo "<li><strong>Image directory not found, not readable or not writable.</strong><br><small>Check the opm_images_directory option in this script, and that this script has permissions to read and write in that directory.</small></li>\n";
0200: }
0201: echo "</ul>";
0202: if ($opm_simpletest_dbok && $opm_simpletest_imgok) {
0203: echo "<h3><strong>Everything seems OK. :)</strong></h3>\n";
0204: }
0205: else {
0206: echo "<h3><strong>There seems to be a problem. :(</strong></h3>\n";
0207: }
0208: echo "</div>";
0209: echo "<div class=\"info\"><ul>";
0210: if ($opm_browser_debug) {
0211: echo "<li>Browser-debug mode is enabled.";
0212: }
0213: else {
0214: echo "<li>Browser-debug mode is disabled";
0215: }
0216: echo "<br><small>This is for developers only. To be used only if you are debugging this script or the client application.</small></li>\n";
0217:
0218: if ($opm_enable_logfile) {
0219: echo "<li>Logging file is enabled.";
0220: }
0221: else {
0222: echo "<li>Logging file is disabled";
0223: }
0224: echo "<br><small>For debugging purposes only, not for production stores (because if enabled, slows the server down and consumes too much disk space).</small></li>\n";
0225:
0226: if ($opm_cfg_useproxy) {
0227: echo "<li>Proxy-usage is enabled.";
0228: }
0229: else {
0230: echo "<li>Proxy-usage is disabled";
0231: }
0232: echo "<br><small>To be used if the web server is behind a proxy so that all http connections are done thru the proxy.</small></li>\n";
0233: echo "<li>The time zone shift is set to $opm_timezone_shift.<br><small>To be used with some timestamps</small>.</li>\n";
0234: echo "</ul></div>";
0235: echo "<div class=\"links\"><ul>";
0236: echo "<li><a href=\"http://www.mariovaldez.net/software/oscpmwin/\">OSCPMWin homepage</a>.</li>\n";
0237: echo "<li><a href=\"http://www.mariovaldez.net/webapps/forums/index.php?c=9\">OSCPMWin web forums</a>.</li>\n";
0238: echo "<li><a href=\"http://www.oscommerce.org/\">osCommerce homepage</a>.</li>\n";
0239: echo "</ul></div>";
0240: echo "<p><small>©2002-2005 by Mario A. Valdez-Ramirez.<br>\n";
0241: echo "This program is free software; you can redistribute it and/or modify\n";
0242: echo "it under the terms of the GNU General Public License as published by\n";
0243: echo "the Free Software Foundation; either version 2 of the License, or (at\n";
0244: echo "your option) any later version.</small></p>\n";
0245: echo "</body></html>";
0246: return;
0247: }
0248:
0249:
0250: // Connect to database.
0251: if (($opm_operation == "delete1") || ($opm_operation == "delete2") || ($opm_operation == "capa") || ($opm_operation == "backup")) {
0252: if (!tep_db_connect ()) {
0253: echo "ERROR NO DATABASE CONNECTION";
0254: return;
0255: }
0256: }
0257:
0258:
0259: // ==================================
0260: // Do nothing.
0261: // NOOP do nothing.
0262: // ==================================
0263: if ($opm_operation == "noop") {
0264: echo "OK NOOP START\n";
0265: opm_FNDebug_Log ("OK NOOP requested.");
0266: echo "OK NOOP END\n";
0267: }
0268:
0269: // ==================================
0270: // CHECK if a file exists.
0271: // CHECK requires the full path of the image.
0272: // ==================================
0273: if ($opm_operation == "check") {
0274: if ($opm_filename) {
0275: if (file_exists ($opm_realpath . $opm_filename)) {
0276: opm_FNDebug_Log ("OK file found $opm_filename.");
0277: echo "OK FILE EXISTS";
0278: }
0279: else {
0280: opm_FNDebug_Log ("ERROR file not found $opm_filename.");
0281: echo "ERROR FILE NOT FOUND";
0282: }
0283: }
0284: }
0285:
0286: // ==================================
0287: // LIST all files recursively.
0288: // LIST returns the filenames with paths.
0289: // ==================================
0290: if ($opm_operation == "list") {
0291: echo "OK LIST START\n";
0292: opm_FNList_Files ($opm_realpath, "", false);
0293: echo "OK LIST END\n";
0294: }
0295:
0296: // ==================================
0297: // LISTDIRS only dirs recursively.
0298: // LISTDIRS returns no filenames.
0299: // ==================================
0300: if ($opm_operation == "listdirs") {
0301: echo "OK LISTDIRS START\n";
0302: opm_FNList_Files ($opm_realpath, "", true);
0303: echo "OK LISTDIRS END\n";
0304: }
0305:
0306: // ==================================
0307: // DELETE an image.
0308: // DELETE requires the filename of the image with path.
0309: // DELETE1 deletes the image if less than one record references it.
0310: // DELETE2 deletes the image if less than two records reference it.
0311: // ==================================
0312: if (($opm_operation == "delete1") || ($opm_operation == "delete2")) {
0313: if ($opm_operation == "delete1") {
0314: $allowed_owners = 1;
0315: }
0316: else {
0317: $allowed_owners = 2;
0318: }
0319: if ($opm_filename) {
0320: // Check for More Pics 6 extension...
0321: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6", $opm_raw_records));
0322: if ($opm_sqlresult[0]) {
0323: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select count(products_id) from " . TABLE_PRODUCTS . " where products_image='$opm_filename' or products_subimage1='$opm_filename' or products_subimage2='$opm_filename' or products_subimage3='$opm_filename' or products_subimage4='$opm_filename' or products_subimage5='$opm_filename' or products_subimage6='$opm_filename'", $opm_raw_records));
0324: }
0325: else {
0326: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select count(products_id) from " . TABLE_PRODUCTS . " where products_image='$opm_filename'", $opm_raw_records));
0327: }
0328: if ($opm_sqlresult[0] < $allowed_owners) {
0329: if (file_exists ($opm_realpath . $opm_filename)) {
0330: if (@unlink ($opm_realpath . $opm_filename)) {
0331: if (!file_exists ($opm_realpath . $opm_filename)) {
0332: opm_FNDebug_Log ("OK file deleted $opm_filename.");
0333: echo "OK DELETE";
0334: }
0335: else {
0336: opm_FNDebug_Log ("ERROR file cannot be deleted $opm_filename.");
0337: echo "ERROR DELETE FILE REMAINS";
0338: }
0339: }
0340: else {
0341: opm_FNDebug_Log ("ERROR when deleting $opm_filename.");
0342: echo "ERROR DELETE UNKNOWN";
0343: }
0344: }
0345: else {
0346: opm_FNDebug_Log ("OK file not found when deleting $opm_filename.");
0347: echo "OK DELETE FILE NOT FOUND";
0348: }
0349: }
0350: else {
0351: opm_FNDebug_Log ("OK file is used by other record (" . ($opm_sqlresult[0] - 1) . ").");
0352: echo "OK DELETE FILE IN USE";
0353: }
0354: }
0355: else {
0356: opm_FNDebug_Log ("OK filename empty.");
0357: echo "OK DELETE FILENAME EMPTY";
0358: }
0359: }
0360:
0361: // ==================================
0362: // UPLOAD an image.
0363: // UPLOAD requires the path, without filename, where the image will be stored.
0364: // UPLOAD returns the image filename with the path.
0365: // ==================================
0366: if ($opm_operation == "upload") {
0367: if ($_FILES["Fl"]["name"] && $_FILES["Fl"]["size"]) {
0368: if (is_uploaded_file ($_FILES["Fl"]["tmp_name"])) {
0369: if ($opm_filename) {
0370: $newfn = $opm_filename;
0371: }
0372: else {
0373: $newfn = trim ($_FILES["Fl"]["name"]);
0374: }
0375: $newfn = str_replace (" ", "_", $newfn);
0376: $newfn = str_replace ("/", "", $newfn);
0377: $newfn = str_replace ("\\", "", $newfn);
0378: $newfn = ereg_replace("[^[:alnum:]\._]", "", $newfn);
0379: $newfn = strtolower ($newfn);
0380:
0381: if ($opm_subdir) {
0382: if ((!file_exists ($opm_realpath . $opm_subdir)) || (!is_dir ($opm_realpath . $opm_subdir))) {
0383: opm_FNDebug_Log ("will create directory=" . $opm_realpath . $opm_subdir);
0384: opm_FNmkdirs ($opm_realpath . $opm_subdir, 0777);
0385: }
0386: if (file_exists ($opm_realpath . $opm_subdir) && (is_dir ($opm_realpath . $opm_subdir))) {
0387: opm_FNDebug_Log ("directory exists=" . $opm_realpath . $opm_subdir);
0388: $newfn = $opm_subdir . "/" . $newfn;
0389: }
0390: else {
0391: opm_FNDebug_Log ("directory does not exist, will ignore=" . $opm_realpath . $opm_subdir);
0392: }
0393:
0394: }
0395: opm_FNDebug_Log ("wanted filename=" . $newfn);
0396: $newfn = opm_FNSmart_Rename ($newfn, 100);
0397: opm_FNDebug_Log ("smart filename=" . $newfn);
0398: opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0399: if (!file_exists ($opm_realpath . $newfn)) {
0400: opm_FNDebug_Log ("file not exist=" . $opm_realpath . $newfn);
0401: if (@move_uploaded_file ($_FILES["Fl"]["tmp_name"], $opm_realpath . $newfn)) {
0402: opm_FNDebug_Log ("moving file to=" . $opm_realpath . $newfn);
0403: if (file_exists ($opm_realpath . $newfn)) {
0404: opm_FNDebug_Log ("upload complete=" . $opm_realpath . $newfn);
0405: @chmod ($opm_realpath . $newfn, octdec ($opm_upload_perms));
0406: echo "OK UPLOAD [" . $newfn . "]";
0407: }
0408: else {
0409: opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
0410: echo "ERROR UPLOAD FILE NOT FOUND";
0411: }
0412: }
0413: else {
0414: opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
0415: echo "ERROR UPLOAD CANNOT MOVE";
0416: }
0417: }
0418: else {
0419: opm_FNDebug_Log ("ERROR file exist=" . $opm_realpath . $newfn);
0420: echo "ERROR UPLOAD EXIST [" . $newfn . "]";
0421: }
0422: }
0423: else {
0424: opm_FNDebug_Log ("ERROR cannot find uploaded file!");
0425: echo "ERROR UPLOAD";
0426: }
0427: }
0428: }
0429:
0430: // ==================================
0431: // CAPABILITY listing.
0432: // CAPABILITY returns a list of identified modules.
0433: // ==================================
0434: if ($opm_operation == "capa") {
0435: opm_FNDebug_Log ("OK Capability search started.");
0436: echo "OK CAPABILITY START\n";
0437:
0438: // Credit Card Payment...
0439: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_CC_STATUS'", $opm_raw_records));
0440: if ($opm_sqlresult[0]) {
0441: opm_FNDebug_Log ("OK PAY_CC capcbility found.");
0442: echo "PAY_CC\n";
0443: }
0444:
0445: // Money Order Payment...
0446: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_MONEYORDER_STATUS'", $opm_raw_records));
0447: if ($opm_sqlresult[0]) {
0448: opm_FNDebug_Log ("OK PAY_ORDER capcbility found.");
0449: echo "PAY_ORDER\n";
0450: }
0451:
0452: // COD Payment...
0453: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COD_STATUS'", $opm_raw_records));
0454: if ($opm_sqlresult[0]) {
0455: opm_FNDebug_Log ("OK PAY_CC capcbility found.");
0456: echo "PAY_CC\n";
0457: }
0458:
0459: // PayPal Payment...
0460: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPAL_STATUS'", $opm_raw_records));
0461: if ($opm_sqlresult[0]) {
0462: opm_FNDebug_Log ("OK PAY_PP capcbility found.");
0463: echo "PAY_PP\n";
0464: }
0465:
0466: // Flat Rate Shipping...
0467: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_FLAT_STATUS'", $opm_raw_records));
0468: if ($opm_sqlresult[0]) {
0469: opm_FNDebug_Log ("OK SHIP_FLAT capcbility found.");
0470: echo "SHIP_FLAT\n";
0471: }
0472:
0473: // Table Shipping...
0474: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'", $opm_raw_records));
0475: if ($opm_sqlresult[0]) {
0476: opm_FNDebug_Log ("OK SHIP_TBL capcbility found.");
0477: echo "SHIP_TBL\n";
0478: }
0479:
0480: // Store PickUp Shipping...
0481: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SPU_STATUS'", $opm_raw_records));
0482: if ($opm_sqlresult[0]) {
0483: opm_FNDebug_Log ("OK SHIP_SPU capcbility found.");
0484: echo "SHIP_SPU\n";
0485: }
0486:
0487: // Multi Pickup Shipping...
0488: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'stores'", $opm_raw_records));
0489: if ($opm_sqlresult[0]) {
0490: opm_FNDebug_Log ("OK SHIP_MPU capcbility found.");
0491: echo "SHIP_MPU\n";
0492: }
0493:
0494: // More Pics 6...
0495: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6", $opm_raw_records));
0496: if ($opm_sqlresult[0]) {
0497: opm_FNDebug_Log ("OK MOREPICS6 capcbility found.");
0498: echo "MOREPICS6\n";
0499: }
0500:
0501: // Total B2B...
0502: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_CUSTOMERS . " customers_groups_id", $opm_raw_records));
0503: if ($opm_sqlresult[0]) {
0504: opm_FNDebug_Log ("OK TOTALB2B capcbility found.");
0505: echo "TOTALB2B\n";
0506: }
0507:
0508: // Poll Booth...
0509: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'phesis_poll_config'", $opm_raw_records));
0510: if ($opm_sqlresult[0]) {
0511: opm_FNDebug_Log ("OK POLLBOOTH capcbility found.");
0512: echo "POLLBOOTH\n";
0513: }
0514:
0515: // Header Tags Controller...
0516: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS_DESCRIPTION . " products_head_keywords_tag", $opm_raw_records));
0517: if ($opm_sqlresult[0]) {
0518: opm_FNDebug_Log ("OK HEADERTAGS capcbility found.");
0519: echo "HEADERTAGS\n";
0520: }
0521:
0522: opm_FNDebug_Log ("OK Capability search finished.");
0523: echo "OK CAPABILITY END\n";
0524: }
0525:
0526: // ==================================
0527: // BACKUP database.
0528: // BACKUP returns a SQL dump.
0529: // ==================================
0530: if ($opm_operation == "backup") {
0531: opm_FNDebug_Log ("OK Backup of database requested.");
0532: $opm_bak_filename = date ("Ymd_His", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y"))) . ".sql";
0533: opm_FNBackup ($opm_realpath . $opm_bak_filename);
0534: if (file_exists ($opm_realpath . $opm_bak_filename)) {
0535: opm_FNgzip_file ($opm_realpath . $opm_bak_filename, $opm_realpath . $opm_bak_filename . ".gz");
0536: if (file_exists ($opm_realpath . $opm_bak_filename . ".gz")) {
0537: $opm_bak_filesize = filesize ($opm_realpath . $opm_bak_filename . ".gz");
0538: header ("Content-type: application/octet-stream");
0539: header ("Content-disposition: attachement; filename=" . $opm_bak_filename . ".gz");
0540: header("Content-Length: " . $opm_bak_filesize);
0541: header ("Pragma: no-store");
0542: readfile ($opm_realpath . $opm_bak_filename . ".gz");
0543: @unlink ($opm_realpath . $opm_bak_filename . ".gz");
0544: opm_FNDebug_Log ("OK Deleting compressed backup file.");
0545: }
0546: @unlink ($opm_realpath . $opm_bak_filename);
0547: opm_FNDebug_Log ("OK Deleting uncompressed backup file.");
0548: }
0549: else {
0550: opm_FNDebug_Log ("ERROR Backup file not found.");
0551: }
0552: opm_FNDebug_Log ("OK Backup operation finished.");
0553: }
0554:
0555:
0556: // ==================================
0557: // Get XCHGRATES.
0558: // XCHGRATES returns the exchange rates of currencies.
0559: // ==================================
0560: if ($opm_operation == "xchgrates") {
0561: opm_FNDebug_Log ("OK Starting XCHGRATES operation.");
0562: if ($opm_filename) {
0563: $opm_filename = strtoupper ($opm_filename);
0564: $opm_main_currency = substr ($opm_filename, 0, 3);
0565: $opm_xchange_currencies = str_replace (",", "_", substr ($opm_filename, 4));
0566: if (($opm_main_currency) && ($opm_xchange_currencies)) {
0567: $opm_xchange_page = "http://www.oanda.com/convert/fxdaily?value=1&exch=$opm_main_currency&dest=Get+Table&sel_list=$opm_xchange_currencies&format=CSV&redirected=1";
0568: $opm_page_data = opm_FNget_webpage ($opm_xchange_page);
0569: if ($opm_page_data) {
0570: $opm_page_data = strip_tags ($opm_page_data);
0571: $opm_page_data = preg_replace ("/[\r\n]+[\s\t]*[\r\n]+/", "", $opm_page_data);
0572: $opm_xr_matches = array ();
0573: preg_match_all ("/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i", $opm_page_data, $opm_xr_matches);
0574: echo "OK XCHGRATES START\n";
0575: foreach ($opm_xr_matches[2] as $key => $value) {
0576: echo $opm_xr_matches[2][$key] . "," . $opm_xr_matches[4][$key] . "\n";
0577: }
0578: echo "OK XCHGRATES END\n";
0579: }
0580: else {
0581: opm_FNDebug_Log ("ERROR The remote server answer is empty or invalid.");
0582: echo "ERROR REMOTE SERVER ERROR";
0583: }
0584: }
0585: else {
0586: opm_FNDebug_Log ("ERROR The specified currency codes are invalid.");
0587: echo "ERROR INVALID CURRENCY";
0588: }
0589: }
0590: else {
0591: opm_FNDebug_Log ("ERROR The no currency codes were specified.");
0592: echo "ERROR EMPTY CURRENCY";
0593: }
0594: }
0595:
0596:
0597: if ($opm_browser_debug) { echo "</pre>"; }
0598: return;
0599:
0600:
0601: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0602: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0603: function opm_FNList_Files ($dirname, $parentdir, $onlydirs) {
0604: $dirhandle = opendir ($dirname);
0605: while (($file = readdir ($dirhandle)) != false)
0606: $sorteddir[count ($sorteddir)] = $file;
0607: closedir ($dirhandle);
0608: natcasesort ($sorteddir);
0609: if (!$onlydirs) {
0610: foreach ($sorteddir as $file) {
0611: if (($file != ".") && ($file != "..")) {
0612: if (!is_dir ($dirname . $file)) {
0613: if ($parentdir) {
0614: echo $parentdir . "/" . $file . "\n";
0615: }
0616: else {
0617: echo $file . "\n";
0618: }
0619: }
0620: }
0621: }
0622: foreach ($sorteddir as $file) {
0623: if (($file != ".") && ($file != "..")) {
0624: if (is_dir ($dirname . $file)) {
0625: if ($parentdir) {
0626: opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0627: }
0628: else {
0629: opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0630: }
0631: }
0632: }
0633: }
0634: }
0635: else {
0636: foreach ($sorteddir as $file) {
0637: if (($file != ".") && ($file != "..")) {
0638: if (is_dir ($dirname . $file)) {
0639: if ($parentdir) {
0640: echo $parentdir . "/" . $file . "\n";
0641: opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0642: }
0643: else {
0644: echo $file . "\n";
0645: opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0646: }
0647: }
0648: }
0649: }
0650: }
0651: }
0652:
0653:
0654:
0655: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0656: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0657: function opm_FNSmart_Rename ($wantedname, $tries) {
0658: global $opm_realpath;
0659: $newname = $wantedname;
0660: $newext = '.' . array_pop (explode ('.', $wantedname));
0661: for ($trycounter = 1; $trycounter <= $tries; $trycounter++) {
0662: if (!file_exists ($opm_realpath . $newname)) {
0663: return ($newname);
0664: }
0665: else {
0666: $newdirname = dirname ($wantedname);
0667: if ($newdirname != ".") {
0668: $newname = dirname ($wantedname) . "/" . basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0669: }
0670: else {
0671: $newname = basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0672: }
0673: }
0674: }
0675: return ($wantedname);
0676: }
0677:
0678:
0679:
0680: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0681: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0682: function opm_FNDebug_Log ($logline) {
0683: global $opm_enable_logfile, $opm_debug_file, $opm_timezone_shift;
0684: if ($opm_enable_logfile) {
0685: if (!(file_exists ($opm_debug_file) && !is_writable ($opm_debug_file))) {
0686: $opm_timestamp = date ("Y-m-d H:i:s", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y")));
0687: $opm_lfilef = @fopen ($opm_debug_file, 'a');
0688: if ($opm_lfilef) {
0689: if ($logline) {
0690: @fwrite ($opm_lfilef, $opm_timestamp . ": " . $logline . "\n");
0691: }
0692: else {
0693: @fwrite ($opm_lfilef, "\n");
0694: }
0695: @fclose ($opm_lfilef);
0696: }
0697: }
0698: }
0699: }
0700:
0701:
0702: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0703: // Based on code posted on php.net by saint@corenova.com and bart@cdasites.com
0704: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0705: function opm_FNmkdirs ($dirname) {
0706: if (is_dir ($dirname) || empty ($dirname)) return 1;
0707: if (file_exists ($dirname) && !is_dir ($dirname)) return 0;
0708: if (opm_FNmkdirs (substr ($dirname, 0, strrpos ($dirname, '/')))) {
0709: if (!file_exists($dirname)) {
0710: return @mkdir ($dirname);
0711: }
0712: }
0713: return 0;
0714: }
0715:
0716:
0717: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0718: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0719: function opm_FNExec_Query ($sql_query, $rawdata) {
0720: global $opm_tag_recordbegin, $opm_tag_recordend, $opm_tag_field, $opm_tag_databegin, $opm_tag_dataend, $opm_encoded_data;
0721: $result_string = "";
0722: opm_FNDebug_Log ("SQLQUERY=" . $sql_query);
0723: $sql_result = tep_db_query ($sql_query);
0724: while ($sql_record = tep_db_fetch_array ($sql_result)) {
0725: $sql_fullrec = "";
0726: if ($rawdata) {
0727: foreach ($sql_record as $key => $value) {
0728: $result_string .= $value . "\n";
0729: }
0730: }
0731: else {
0732: $result_string .= "$opm_tag_recordbegin\n";
0733: foreach ($sql_record as $key => $value) {
0734: if ($opm_encoded_data) {
0735: $encodedfield = base64_encode ($value);
0736: }
0737: else {
0738: $encodedfield = htmlentities ($value);
0739: }
0740: $result_string .= $opm_tag_field . $opm_tag_databegin . $encodedfield . $opm_tag_dataend . "\n";
0741: $sql_fullrec .= $encodedfield;
0742: }
0743: $result_string .= "$opm_tag_recordend " . abs (crc32 ($sql_fullrec)) . "\n";
0744: }
0745: }
0746: return $result_string;
0747: }
0748:
0749:
0750:
0751:
0752:
0753: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0754: // Based on code from osCommerce (/admin/backup.php).
0755: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0756: function opm_FNBackup ($dump_file) {
0757: global $opm_script_version;
0758: if ($dump_file) {
0759: $bakfilef = fopen ($dump_file, 'w');
0760: $bakline = "# OSCPMWin (server-side $opm_script_version)\n";
0761: $bakline .= "# Database: " . DB_DATABASE . "@" . DB_SERVER . "\n";
0762: $bakline .= "# Date: " . date ("Y-m-d H:i:s", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y"))) . "\n";
0763: fwrite ($bakfilef, $bakline);
0764: $tables_query = tep_db_query ("show tables");
0765: while ($tables = tep_db_fetch_array ($tables_query)) {
0766: list (, $table) = each ($tables);
0767: $bakline = "drop table if exists $table;\ncreate table $table (\n";
0768: $table_list = array ();
0769: $fields_query = tep_db_query ("show fields from $table");
0770: while ($fields = tep_db_fetch_array($fields_query)) {
0771: $table_list[] = $fields["Field"];
0772: $bakline .= " " . $fields["Field"] . " " . $fields["Type"];
0773: if (strlen ($fields["Default"]) > 0) $bakline .= " default '" . $fields["Default"] . "'";
0774: if (strtoupper ($fields["Null"]) != "YES") $bakline .= " not null";
0775: if (isset ($fields["Extra"])) $bakline .= " " . $fields["Extra"];
0776: $bakline .= ",\n";
0777: }
0778: $bakline = ereg_replace(",\n$", '', $bakline);
0779: $index = array ();
0780: $keys_query = tep_db_query ("show keys from $table");
0781: while ($keys = tep_db_fetch_array ($keys_query)) {
0782: $kname = $keys["Key_name"];
0783: if (!isset ($index[$kname])) {
0784: $index[$kname] = array ("unique" => !$keys["Non_unique"], "columns" => array());
0785: }
0786: $index[$kname]["columns"][] = $keys["Column_name"];
0787: }
0788: while (list($kname, $info) = each($index)) {
0789: $bakline .= ",\n";
0790: $columns = implode($info["columns"], ", ");
0791: if ($kname == "PRIMARY") {
0792: $bakline .= " PRIMARY KEY ($columns)";
0793: } elseif ($info["unique"]) {
0794: $bakline .= " UNIQUE $kname ($columns)";
0795: } else {
0796: $bakline .= " KEY $kname ($columns)";
0797: }
0798: }
0799: $bakline .= "\n);\n\n";
0800: fwrite ($bakfilef, $bakline);
0801: $rows_query = tep_db_query ("select " . implode (",", $table_list) . " from $table");
0802: while ($rows = tep_db_fetch_array ($rows_query)) {
0803: $bakline = "insert into $table (" . implode (", ", $table_list) . ") values (";
0804: reset ($table_list);
0805: while (list (, $i) = each ($table_list)) {
0806: if (!isset ($rows[$i])) {
0807: $bakline .= "NULL, ";
0808: } elseif (($rows[$i] != "") && (strtoupper ($rows[$i]) != "NULL")) {
0809: $row = addslashes ($rows[$i]);
0810: $row = ereg_replace("\n#", "\n".'\#', $row);
0811: $bakline .= "'$row', ";
0812: } else {
0813: $bakline .= "'', ";
0814: }
0815: }
0816: $bakline = ereg_replace (", $", "", $bakline) . ");\n";
0817: fwrite ($bakfilef, $bakline);
0818: }
0819: }
0820: fclose ($bakfilef);
0821: }
0822: }
0823:
0824:
0825: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0826: // Compress a file with the gzip algorithm.
0827: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0828: function opm_FNgzip_file ($opm_gzsource, $opm_gztarget) {
0829: if (function_exists ("gzwrite") && file_exists ($opm_gzsource)) {
0830: $opm_ungzfp = fopen($opm_gzsource, "rb");
0831: $opm_gzfp = gzopen($opm_gztarget, "wb9");
0832: if ($opm_gzfp && $opm_ungzfp) {
0833: while (!feof ($opm_ungzfp)) {
0834: gzwrite ($opm_gzfp, fread ($opm_ungzfp, 65535));
0835: }
0836: @fclose ($opm_ungzfp);
0837: @gzclose ($opm_gzfp);
0838: return (true);
0839: }
0840: else {
0841: @fclose ($opm_ungzfp);
0842: @gzclose ($opm_gzfp);
0843: return (false);
0844: }
0845: }
0846: else {
0847: return (false);
0848: }
0849: }
0850:
0851:
0852: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0853: // Request a page from a web server.
0854: // If using a proxy server, redirect the call thru the proxy.
0855: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0856: function opm_FNget_webpage ($target_url) {
0857: global $opm_useragent, $opm_httpconn_timeout, $opm_cfg_useproxy, $opm_cfg_proxyaddress, $opm_cfg_proxyport;
0858: $opm_http_content = "";
0859: $errno = 0;
0860: $errstr = "";
0861: $urlparts = parse_url ($target_url);
0862: if ($urlparts["host"]) {
0863: @ignore_user_abort (true);
0864: if ($opm_cfg_useproxy) {
0865: $opm_sockconn = @fsockopen ($opm_cfg_proxyaddress, $opm_cfg_proxyport, $errno, $errstr, $opm_httpconn_timeout);
0866: }
0867: else {
0868: $opm_sockconn = @fsockopen ($urlparts["host"], (empty($urlparts["port"]) ? "80" : $urlparts["port"]), $errno, $errstr, $opm_httpconn_timeout);
0869: }
0870: if ($opm_sockconn) {
0871: if ($opm_cfg_useproxy) {
0872: fputs ($opm_sockconn, "GET " . $urlparts["scheme"] . "://" . $urlparts["host"] . $urlparts["path"] . "?" . $urlparts["query"] . " HTTP/1.0\r\nHost: " . $opm_cfg_proxyaddress . "\r\nUser-Agent: $opm_useragent\r\nPragma: no-cache\r\nConnection: Close\r\n\r\n");
0873: }
0874: else {
0875: fputs ($opm_sockconn, "GET " . $urlparts["path"] . "?" . $urlparts["query"] . " HTTP/1.0\r\nHost: " . $urlparts["host"] . "\r\nUser-Agent: $opm_useragent\r\nConnection: Close\r\n\r\n");
0876: }
0877: while ((!feof($opm_sockconn)) && (!is_readable ($opm_skfilename))) {
0878: $opm_http_content .= fgets ($opm_sockconn, 10240);
0879: }
0880: @fclose ($opm_sockconn);
0881: if (eregi ("HTTP.*200 OK", $opm_http_content)) {
0882: $opm_http_content = str_replace ("\r", "", $opm_http_content);
0883: $opm_http_content = substr ($opm_http_content, strpos ($opm_http_content, "\n\n") + 2);
0884: return ($opm_http_content);
0885: }
0886: }
0887: }
0888: return ("");
0889: }
0890:
0891:
0892:
0893:
0894: ?>