Source code of file oscpmwin_v0.1.1.652/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: // ==================================
0039: // End of configuration options...
0040: // ==================================
0041:
0042:
0043: $opm_tag_recordbegin = "BR"; // BR
0044: $opm_tag_recordend = "ER"; // ER
0045: $opm_tag_field = "DF"; // DF
0046: $opm_tag_databegin = "["; // [
0047: $opm_tag_dataend = "]"; // ]
0048:
0049:
0050: $opm_script_version = "0.1.9";
0051: error_reporting(E_ALL & ~E_NOTICE);
0052:
0053: $opm_realpath = str_replace (basename (__FILE__), "", __FILE__) . $opm_images_directory;
0054:
0055: @set_time_limit (300);
0056:
0057: if (!$opm_upload_perms) {
0058: $opm_upload_perms = "0600";
0059: }
0060: // Get all parameters about the request.
0061: if (!$opm_browser_debug) {
0062: $opm_passwordhash = strtoupper (trim ($_POST["Pw"]));
0063: $opm_operation = strtolower (trim ($_POST["Op"]));
0064: $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["Fn"])))));
0065: $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["SD"])))));
0066: $opm_smartrename = abs (trim ($_POST["SR"]));
0067: $opm_reqversion = trim ($_POST["Vn"]);
0068: $opm_logintimestamp = trim ($_POST["TS"]);
0069: }
0070: else {
0071: $opm_passwordhash = strtoupper (trim ($_GET["Pw"]));
0072: $opm_operation = strtolower (trim ($_GET["Op"]));
0073: $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["Fn"])))));
0074: $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["SD"])))));
0075: $opm_smartrename = abs (trim ($_GET["SR"]));
0076: $opm_reqversion = trim ($_GET["Vn"]);
0077: $opm_logintimestamp = trim ($_GET["TS"]);
0078: }
0079: if (($opm_filename{0} == "/") || ($opm_filename{0} == ".")) {
0080: $opm_filename = substr ($opm_filename, 1, strlen ($opm_filename) - 1);
0081: }
0082: if ($opm_smartrename > 0) {
0083: $opm_smartrename = 1;
0084: }
0085: else {
0086: $opm_smartrename = 0;
0087: }
0088:
0089: // Log basic information about the request.
0090: if ($opm_enable_extralog) {
0091: if (!$opm_browser_debug) {
0092: opm_FNDebug_Log (print_r ($_POST, true));
0093: }
0094: else {
0095: opm_FNDebug_Log (print_r ($_GET, true));
0096: }
0097: }
0098: else {
0099: opm_FNDebug_Log ("");
0100: }
0101: opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0102: opm_FNDebug_Log ("PASSWORDHASH=" . $opm_passwordhash);
0103: opm_FNDebug_Log ("OPERATION=" . $opm_operation);
0104: opm_FNDebug_Log ("FILENAME=" . $opm_filename);
0105: opm_FNDebug_Log ("SUBDIR=" . $opm_subdir);
0106: opm_FNDebug_Log ("SMARTRENAME=" . $opm_smartrename);
0107: opm_FNDebug_Log ("REQVERSION=" . $opm_reqversion);
0108: opm_FNDebug_Log ("LOGINTIMESTAMP=" . $opm_logintimestamp);
0109:
0110:
0111: // Include OSCommerce database configuration and functions.
0112: require ("includes/configure.php");
0113: require (DIR_WS_INCLUDES . "database_tables.php");
0114: require (DIR_WS_FUNCTIONS . "database.php");
0115:
0116:
0117: if ($opm_browser_debug) { echo "<pre>"; }
0118:
0119: // Check if script version is the expected by client.
0120: if (($opm_script_version != $opm_reqversion) && ($opm_operation)) {
0121: opm_FNDebug_Log ("ERROR wrong script version. Expected $opm_reqversion, but we are $opm_script_version.");
0122: echo "ERROR WRONG SCRIPT VERSION";
0123: return;
0124: }
0125:
0126: // Check if the image directory is valid.
0127: if (!is_dir ($opm_realpath)) {
0128: opm_FNDebug_Log ("ERROR cannot find the images directory. Expecting $opm_realpath exist.");
0129: echo "ERROR IMAGE DIRECTORY NOT FOUND";
0130: return;
0131: }
0132:
0133: // Get OSCommerce DB password and compare it with client password.
0134: if ($opm_enable_auth) {
0135: if (defined ("DB_SERVER_PASSWORD") || ($opm_password_override != "")) {
0136: if ($opm_password_override != "") {
0137: opm_FNDebug_Log ("Password override is enabled.");
0138: $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . $opm_password_override));
0139: }
0140: else {
0141: $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . DB_SERVER_PASSWORD));
0142: }
0143: opm_FNDebug_Log ("SERVERPASSWORDHASH=" . $opm_serverpassword);
0144: if ($opm_serverpassword != $opm_passwordhash) {
0145: opm_FNDebug_Log ("ERROR the server and client passwords do not match.");
0146: echo "ERROR PASSWORD MISMATCH";
0147: if ($opm_operation) { return; }
0148: }
0149: }
0150: else {
0151: $opm_operation = "";
0152: opm_FNDebug_Log ("ERROR the server password was not found.");
0153: echo "ERROR CANNOT FIND SERVER PASSWORD";
0154: }
0155: }
0156: else {
0157: opm_FNDebug_Log ("Password authentication is disabled.");
0158: }
0159:
0160:
0161: // Connect to database.
0162: if (($opm_operation == "delete") || ($opm_operation == "capa") || ($opm_operation == "backup")) {
0163: if (!tep_db_connect ()) {
0164: echo "ERROR NO DATABASE CONNECTION";
0165: if ($opm_operation) { return; }
0166: }
0167: }
0168:
0169:
0170: // ==================================
0171: // Do nothing.
0172: // NOOP do nothing.
0173: // ==================================
0174: if ($opm_operation == "noop") {
0175: echo "OK NOOP START\n";
0176: opm_FNDebug_Log ("OK NOOP requested.");
0177: echo "OK NOOP END\n";
0178: return;
0179: }
0180: // ==================================
0181: // CHECK if a file exists.
0182: // CHECK requires the full path of the image.
0183: // ==================================
0184: if ($opm_operation == "check") {
0185: if ($opm_filename) {
0186: if (file_exists ($opm_realpath . $opm_filename)) {
0187: opm_FNDebug_Log ("OK file found $opm_filename.");
0188: echo "OK FILE EXISTS";
0189: return;
0190: }
0191: else {
0192: opm_FNDebug_Log ("ERROR file not found $opm_filename.");
0193: echo "ERROR FILE NOT FOUND";
0194: return;
0195: }
0196: }
0197: }
0198: // ==================================
0199: // LIST all files recursively.
0200: // LIST returns the filenames with paths.
0201: // ==================================
0202: if ($opm_operation == "list") {
0203: echo "OK LIST START\n";
0204: opm_FNList_Files ($opm_realpath, "", false);
0205: echo "OK LIST END\n";
0206: return;
0207: }
0208: // ==================================
0209: // LISTDIRS only dirs recursively.
0210: // LISTDIRS returns no filenames.
0211: // ==================================
0212: if ($opm_operation == "listdirs") {
0213: echo "OK LISTDIRS START\n";
0214: opm_FNList_Files ($opm_realpath, "", true);
0215: echo "OK LISTDIRS END\n";
0216: return;
0217: }
0218: // ==================================
0219: // DELETE an image.
0220: // DELETE requires the filename of the image with path.
0221: // ==================================
0222: if ($opm_operation == "delete") {
0223: if ($opm_filename) {
0224: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select count(products_id) from " . TABLE_PRODUCTS . " where products_image='$opm_filename'", $opm_raw_records));
0225: if ($opm_sqlresult[0] < 2) {
0226: if (file_exists ($opm_realpath . $opm_filename)) {
0227: if (@unlink ($opm_realpath . $opm_filename)) {
0228: if (!file_exists ($opm_realpath . $opm_filename)) {
0229: opm_FNDebug_Log ("OK file deleted $opm_filename.");
0230: echo "OK DELETE";
0231: return;
0232: }
0233: else {
0234: opm_FNDebug_Log ("ERROR file cannot be deleted $opm_filename.");
0235: echo "ERROR DELETE FILE REMAINS";
0236: return;
0237: }
0238: }
0239: else {
0240: opm_FNDebug_Log ("ERROR when deleting $opm_filename.");
0241: echo "ERROR DELETE UNKNOWN";
0242: return;
0243: }
0244: }
0245: else {
0246: opm_FNDebug_Log ("OK file not found when deleting $opm_filename.");
0247: echo "OK DELETE FILE NOT FOUND";
0248: return;
0249: }
0250: }
0251: else {
0252: opm_FNDebug_Log ("OK file is used by other record (" . ($opm_sqlresult[0] - 1) . ").");
0253: echo "OK DELETE FILE IN USE";
0254: return;
0255: }
0256: }
0257: else {
0258: opm_FNDebug_Log ("OK filename empty.");
0259: echo "OK DELETE FILENAME EMPTY";
0260: return;
0261: }
0262: }
0263: // ==================================
0264: // UPLOAD an image.
0265: // UPLOAD requires the path, without filename, where the image will be stored.
0266: // UPLOAD returns the image filename with the path.
0267: // ==================================
0268: if ($opm_operation == "upload") {
0269: if ($_FILES["Fl"]["name"] && $_FILES["Fl"]["size"]) {
0270: if (is_uploaded_file ($_FILES["Fl"]["tmp_name"])) {
0271: if ($opm_filename) {
0272: $newfn = $opm_filename;
0273: }
0274: else {
0275: $newfn = trim ($_FILES["Fl"]["name"]);
0276: }
0277: $newfn = str_replace (" ", "_", $newfn);
0278: $newfn = str_replace ("/", "", $newfn);
0279: $newfn = str_replace ("\\", "", $newfn);
0280: $newfn = ereg_replace("[^[:alnum:]\._]", "", $newfn);
0281: $newfn = strtolower ($newfn);
0282:
0283: if ($opm_subdir) {
0284: if ((!file_exists ($opm_realpath . $opm_subdir)) || (!is_dir ($opm_realpath . $opm_subdir))) {
0285: opm_FNDebug_Log ("will create directory=" . $opm_realpath . $opm_subdir);
0286: opm_FNmkdirs ($opm_realpath . $opm_subdir, 0777);
0287: }
0288: if (file_exists ($opm_realpath . $opm_subdir) && (is_dir ($opm_realpath . $opm_subdir))) {
0289: opm_FNDebug_Log ("directory exists=" . $opm_realpath . $opm_subdir);
0290: $newfn = $opm_subdir . "/" . $newfn;
0291: }
0292: else {
0293: opm_FNDebug_Log ("directory does not exist, will ignore=" . $opm_realpath . $opm_subdir);
0294: }
0295:
0296: }
0297: opm_FNDebug_Log ("wanted filename=" . $newfn);
0298: if ($opm_smartrename == 1) {
0299: $newfn = opm_FNSmart_Rename ($newfn, 100);
0300: }
0301: opm_FNDebug_Log ("smart filename=" . $newfn);
0302: opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0303: if (!file_exists ($opm_realpath . $newfn)) {
0304: opm_FNDebug_Log ("file not exist=" . $opm_realpath . $newfn);
0305: if (@move_uploaded_file ($_FILES["Fl"]["tmp_name"], $opm_realpath . $newfn)) {
0306: opm_FNDebug_Log ("moving file to=" . $opm_realpath . $newfn);
0307: if (file_exists ($opm_realpath . $newfn)) {
0308: opm_FNDebug_Log ("upload complete=" . $opm_realpath . $newfn);
0309: @chmod ($opm_realpath . $newfn, octdec ($opm_upload_perms));
0310: echo "OK UPLOAD [" . $newfn . "]";
0311: return;
0312: }
0313: else {
0314: opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
0315: echo "ERROR UPLOAD FILE NOT FOUND";
0316: return;
0317: }
0318: }
0319: else {
0320: opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
0321: echo "ERROR UPLOAD CANNOT MOVE";
0322: return;
0323: }
0324: }
0325: else {
0326: opm_FNDebug_Log ("ERROR file exist=" . $opm_realpath . $newfn);
0327: echo "ERROR UPLOAD EXIST [" . $newfn . "]";
0328: return;
0329: }
0330: }
0331: else {
0332: opm_FNDebug_Log ("ERROR cannot find uploaded file!");
0333: echo "ERROR UPLOAD";
0334: return;
0335: }
0336: }
0337: }
0338: // ==================================
0339: // CAPABILITY listing.
0340: // CAPABILITY returns a list of identified modules.
0341: // ==================================
0342: if ($opm_operation == "capa") {
0343: echo "OK CAPABILITY START\n";
0344:
0345: // Credit Card Payment...
0346: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_CC_STATUS'", $opm_raw_records));
0347: if ($opm_sqlresult[0]) {
0348: echo "PAY_CC\n";
0349: }
0350:
0351: // Money Order Payment...
0352: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_MONEYORDER_STATUS'", $opm_raw_records));
0353: if ($opm_sqlresult[0]) {
0354: echo "PAY_ORDER\n";
0355: }
0356:
0357: // COD Payment...
0358: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COD_STATUS'", $opm_raw_records));
0359: if ($opm_sqlresult[0]) {
0360: echo "PAY_CC\n";
0361: }
0362:
0363: // PayPal Payment...
0364: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPAL_STATUS'", $opm_raw_records));
0365: if ($opm_sqlresult[0]) {
0366: echo "PAY_PP\n";
0367: }
0368:
0369: // Flat Rate Shipping...
0370: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_FLAT_STATUS'", $opm_raw_records));
0371: if ($opm_sqlresult[0]) {
0372: echo "SHIP_FLAT\n";
0373: }
0374:
0375: // Table Shipping...
0376: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'", $opm_raw_records));
0377: if ($opm_sqlresult[0]) {
0378: echo "SHIP_TBL\n";
0379: }
0380:
0381: // Store PickUp Shipping...
0382: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SPU_STATUS'", $opm_raw_records));
0383: if ($opm_sqlresult[0]) {
0384: echo "SHIP_SPU\n";
0385: }
0386:
0387: // Multi Pickup Shipping...
0388: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'stores'", $opm_raw_records));
0389: if ($opm_sqlresult[0]) {
0390: echo "SHIP_MPU\n";
0391: }
0392:
0393: // More Pics 6...
0394: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6", $opm_raw_records));
0395: if ($opm_sqlresult[0]) {
0396: echo "MOREPICS6\n";
0397: }
0398:
0399: // Total B2B...
0400: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_CUSTOMERS . " customers_groups_id", $opm_raw_records));
0401: if ($opm_sqlresult[0]) {
0402: echo "TOTALB2B\n";
0403: }
0404:
0405: // Poll Booth...
0406: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'phesis_poll_config'", $opm_raw_records));
0407: if ($opm_sqlresult[0]) {
0408: echo "POLLBOOTH\n";
0409: }
0410:
0411: // Header Tags Controller...
0412: $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS_DESCRIPTION . " products_head_keywords_tag", $opm_raw_records));
0413: if ($opm_sqlresult[0]) {
0414: echo "HEADERTAGS\n";
0415: }
0416:
0417: echo "OK CAPABILITY END\n";
0418: return;
0419: }
0420: // ==================================
0421: // BACKUP database.
0422: // BACKUP returns a SQL dump.
0423: // ==================================
0424: if ($opm_operation == "backup") {
0425: $opm_bak_filename = "osc_" . date ("Ymd_His", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y"))) . ".sql";
0426: opm_FNBackup ($opm_realpath . $opm_bak_filename);
0427: if (file_exists ($opm_realpath . $opm_bak_filename)) {
0428: opm_FNgzip_file ($opm_realpath . $opm_bak_filename, $opm_realpath . $opm_bak_filename . ".gz");
0429: if (file_exists ($opm_realpath . $opm_bak_filename . ".gz")) {
0430: header ("Content-type: application/octet-stream");
0431: header ("Content-disposition: attachement; filename=" . $opm_bak_filename . ".gz");
0432: header ("Pragma: no-store");
0433: readfile ($opm_realpath . $opm_bak_filename . ".gz");
0434: @unlink ($opm_realpath . $opm_bak_filename . ".gz");
0435: }
0436: @unlink ($opm_realpath . $opm_bak_filename);
0437: }
0438: return;
0439: }
0440:
0441:
0442: // ==================================
0443: // If no command is requested, just display the version.
0444: // ==================================
0445: echo "<p>OSCommerce Product Manager, server-side script " . $opm_script_version . ".</p>";
0446: if ($opm_browser_debug) {
0447: echo "<p>BROWSER DEBUG MODE ENABLED!</p>";
0448: }
0449: if ($opm_enable_logfile) {
0450: echo "<p>LOGFILE ENABLED!</p>";
0451: }
0452: return;
0453:
0454:
0455: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0456: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0457: function opm_FNList_Files ($dirname, $parentdir, $onlydirs) {
0458: $dirhandle = opendir ($dirname);
0459: while (($file = readdir ($dirhandle)) != false)
0460: $sorteddir[count ($sorteddir)] = $file;
0461: closedir ($dirhandle);
0462: natcasesort ($sorteddir);
0463: if (!$onlydirs) {
0464: foreach ($sorteddir as $file) {
0465: if (($file != ".") && ($file != "..")) {
0466: if (!is_dir ($dirname . $file)) {
0467: if ($parentdir) {
0468: echo $parentdir . "/" . $file . "\n";
0469: }
0470: else {
0471: echo $file . "\n";
0472: }
0473: }
0474: }
0475: }
0476: foreach ($sorteddir as $file) {
0477: if (($file != ".") && ($file != "..")) {
0478: if (is_dir ($dirname . $file)) {
0479: if ($parentdir) {
0480: opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0481: }
0482: else {
0483: opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0484: }
0485: }
0486: }
0487: }
0488: }
0489: else {
0490: foreach ($sorteddir as $file) {
0491: if (($file != ".") && ($file != "..")) {
0492: if (is_dir ($dirname . $file)) {
0493: if ($parentdir) {
0494: echo $parentdir . "/" . $file . "\n";
0495: opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0496: }
0497: else {
0498: echo $file . "\n";
0499: opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0500: }
0501: }
0502: }
0503: }
0504: }
0505: }
0506:
0507:
0508:
0509: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0510: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0511: function opm_FNSmart_Rename ($wantedname, $tries) {
0512: global $opm_realpath;
0513: $newname = $wantedname;
0514: $newext = '.' . array_pop (explode ('.', $wantedname));
0515: for ($trycounter = 1; $trycounter <= $tries; $trycounter++) {
0516: if (!file_exists ($opm_realpath . $newname)) {
0517: return ($newname);
0518: }
0519: else {
0520: $newdirname = dirname ($wantedname);
0521: if ($newdirname != ".") {
0522: $newname = dirname ($wantedname) . "/" . basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0523: }
0524: else {
0525: $newname = basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0526: }
0527: }
0528: }
0529: return ($wantedname);
0530: }
0531:
0532:
0533:
0534: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0535: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0536: function opm_FNDebug_Log ($logline) {
0537: global $opm_enable_logfile, $opm_debug_file, $opm_timezone_shift;
0538: if ($opm_enable_logfile) {
0539: if (!(file_exists ($opm_debug_file) && !is_writable ($opm_debug_file))) {
0540: $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")));
0541: $opm_lfilef = @fopen ($opm_debug_file, 'a');
0542: if ($opm_lfilef) {
0543: if ($logline) {
0544: @fwrite ($opm_lfilef, $opm_timestamp . ": " . $logline . "\n");
0545: }
0546: else {
0547: @fwrite ($opm_lfilef, "\n");
0548: }
0549: @fclose ($opm_lfilef);
0550: }
0551: }
0552: }
0553: }
0554:
0555:
0556: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0557: // Based on code posted on php.net by saint@corenova.com and bart@cdasites.com
0558: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0559: function opm_FNmkdirs ($dirname) {
0560: if (is_dir ($dirname) || empty ($dirname)) return 1;
0561: if (file_exists ($dirname) && !is_dir ($dirname)) return 0;
0562: if (opm_FNmkdirs (substr ($dirname, 0, strrpos ($dirname, '/')))) {
0563: if (!file_exists($dirname)) {
0564: return @mkdir ($dirname);
0565: }
0566: }
0567: return 0;
0568: }
0569:
0570:
0571: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0572: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0573: function opm_FNExec_Query ($sql_query, $rawdata) {
0574: global $opm_tag_recordbegin, $opm_tag_recordend, $opm_tag_field, $opm_tag_databegin, $opm_tag_dataend, $opm_encoded_data;
0575: $result_string = "";
0576: opm_FNDebug_Log ("SQLQUERY=" . $sql_query);
0577: $sql_result = tep_db_query ($sql_query);
0578: while ($sql_record = tep_db_fetch_array ($sql_result)) {
0579: $sql_fullrec = "";
0580: if ($rawdata) {
0581: foreach ($sql_record as $key => $value) {
0582: $result_string .= $value . "\n";
0583: }
0584: }
0585: else {
0586: $result_string .= "$opm_tag_recordbegin\n";
0587: foreach ($sql_record as $key => $value) {
0588: if ($opm_encoded_data) {
0589: $encodedfield = base64_encode ($value);
0590: }
0591: else {
0592: $encodedfield = htmlentities ($value);
0593: }
0594: $result_string .= $opm_tag_field . $opm_tag_databegin . $encodedfield . $opm_tag_dataend . "\n";
0595: $sql_fullrec .= $encodedfield;
0596: }
0597: $result_string .= "$opm_tag_recordend " . abs (crc32 ($sql_fullrec)) . "\n";
0598: }
0599: }
0600: return $result_string;
0601: }
0602:
0603:
0604:
0605:
0606:
0607: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0608: // Based on code from osCommerce (/admin/backup.php).
0609: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0610: function opm_FNBackup ($dump_file) {
0611: global $opm_script_version;
0612: if ($dump_file) {
0613: $bakfilef = fopen ($dump_file, 'w');
0614: $bakline = "# OSCPMWin (server-side $opm_script_version)\n";
0615: $bakline .= "# Database: " . DB_DATABASE . "@" . DB_SERVER . "\n";
0616: $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";
0617: fwrite ($bakfilef, $bakline);
0618: $tables_query = tep_db_query ("show tables");
0619: while ($tables = tep_db_fetch_array ($tables_query)) {
0620: list (, $table) = each ($tables);
0621: $bakline = "drop table if exists $table;\ncreate table $table (\n";
0622: $table_list = array ();
0623: $fields_query = tep_db_query ("show fields from $table");
0624: while ($fields = tep_db_fetch_array($fields_query)) {
0625: $table_list[] = $fields["Field"];
0626: $bakline .= " " . $fields["Field"] . " " . $fields["Type"];
0627: if (strlen ($fields["Default"]) > 0) $bakline .= " default '" . $fields["Default"] . "'";
0628: if (strtoupper ($fields["Null"]) != "YES") $bakline .= " not null";
0629: if (isset ($fields["Extra"])) $bakline .= " " . $fields["Extra"];
0630: $bakline .= ",\n";
0631: }
0632: $bakline = ereg_replace(",\n$", '', $bakline);
0633: $index = array ();
0634: $keys_query = tep_db_query ("show keys from $table");
0635: while ($keys = tep_db_fetch_array ($keys_query)) {
0636: $kname = $keys["Key_name"];
0637: if (!isset ($index[$kname])) {
0638: $index[$kname] = array ("unique" => !$keys["Non_unique"], "columns" => array());
0639: }
0640: $index[$kname]["columns"][] = $keys["Column_name"];
0641: }
0642: while (list($kname, $info) = each($index)) {
0643: $bakline .= ",\n";
0644: $columns = implode($info["columns"], ", ");
0645: if ($kname == "PRIMARY") {
0646: $bakline .= " PRIMARY KEY ($columns)";
0647: } elseif ($info["unique"]) {
0648: $bakline .= " UNIQUE $kname ($columns)";
0649: } else {
0650: $bakline .= " KEY $kname ($columns)";
0651: }
0652: }
0653: $bakline .= "\n);\n\n";
0654: fwrite ($bakfilef, $bakline);
0655: $rows_query = tep_db_query ("select " . implode (",", $table_list) . " from $table");
0656: while ($rows = tep_db_fetch_array ($rows_query)) {
0657: $bakline = "insert into $table (" . implode (", ", $table_list) . ") values (";
0658: reset ($table_list);
0659: while (list (, $i) = each ($table_list)) {
0660: if (!isset ($rows[$i])) {
0661: $bakline .= "NULL, ";
0662: } elseif (($rows[$i] != "") && (strtoupper ($rows[$i]) != "NULL")) {
0663: $row = addslashes ($rows[$i]);
0664: $row = ereg_replace("\n#", "\n".'\#', $row);
0665: $bakline .= "'$row', ";
0666: } else {
0667: $bakline .= "'', ";
0668: }
0669: }
0670: $bakline = ereg_replace (", $", "", $bakline) . ");\n";
0671: fwrite ($bakfilef, $bakline);
0672: }
0673: }
0674: fclose ($bakfilef);
0675: }
0676: }
0677:
0678:
0679: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0680: // Compress a file with the gzip algorithm.
0681: // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0682: function opm_FNgzip_file ($opm_gzsource, $opm_gztarget) {
0683: if (function_exists ("gzwrite") && file_exists ($opm_gzsource)) {
0684: $opm_ungzfp = fopen($opm_gzsource, "rb");
0685: $opm_gzfp = gzopen($opm_gztarget, "wb9");
0686: if ($opm_gzfp && $opm_ungzfp) {
0687: while (!feof ($opm_ungzfp)) {
0688: gzwrite ($opm_gzfp, fread ($opm_ungzfp, 65535));
0689: }
0690: @fclose ($opm_ungzfp);
0691: @gzclose ($opm_gzfp);
0692: return (true);
0693: }
0694: else {
0695: @fclose ($opm_ungzfp);
0696: @gzclose ($opm_gzfp);
0697: return (false);
0698: }
0699: }
0700: else {
0701: return (false);
0702: }
0703: }
0704:
0705:
0706:
0707:
0708:
0709:
0710: ?>