Source code of file oscpmwin_v0.4.1.683/oscpm1_upload.txt from the
osCommerce Product Manager for Windows.


0000:   <?php
0001:   // osCommerce Product Manager for Windows (oscpmwin).
0002:   // Copyright �2003-2007 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_upload_perms = "0644";
0034:   $opm_timezone_shift = 0;
0035:   $opm_allow_compression = true;
0036:   $opm_compress_level = 1;
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:   $opm_tag_comment = "#";                     // #
0054:   $opm_tag_stats = "ST";                      // ST
0055:   $opm_tag_fieldlist = "FL";                  // FL
0056:   
0057:   $opm_session_string = "OSCPMCHECKER";
0058:   $opm_max_failedlogins = 8;                  // Max logins before blocking the client.
0059:   $opm_max_blocktime = 3600;                  // Time a client is blocked after too many failures.
0060:   $opm_max_tracktime = 900;                   // Time to store the login story of each client.
0061:   $opm_sleep_afterfail = 15;                  // Delay when login fails.
0062:   
0063:   
0064:   $opm_script_version = "0.8.9";
0065:   error_reporting(E_ALL & ~E_NOTICE);
0066:   
0067:   $opm_realpath = str_replace (basename (__FILE__), "", __FILE__) . $opm_images_directory;
0068:   $opm_tmpsoftwarename = explode (" ", $HTTP_SERVER_VARS["SERVER_SOFTWARE"]);
0069:   $opm_tmposname = explode (" ", php_uname ());
0070:   $opm_useragent = "User-Agent: OSCPMWin/$opm_script_version " . $opm_tmpsoftwarename[0] . " MySQL/unknown " . $opm_tmposname[0];
0071:   
0072:   @set_time_limit (300);
0073:   
0074:   if (!$opm_upload_perms) {
0075:     $opm_upload_perms = "0600";
0076:   }
0077:   
0078:   $opm_ip_address = $_SERVER["REMOTE_ADDR"];
0079:   if (!$opm_ip_address) {
0080:     $opm_ip_address = $HTTP_SERVER_VARS["REMOTE_ADDR"];
0081:     if (!$opm_ip_address) {
0082:       $opm_ip_address = $_ENV["REMOTE_ADDR"];
0083:       if (!$opm_ip_address) {
0084:         $opm_ip_address = getenv("REMOTE_ADDR");
0085:       }
0086:       else $opm_ip_address = "";
0087:     }
0088:   }
0089:   
0090:   
0091:   
0092:   // Get all parameters about the request.
0093:   if (!$opm_browser_debug) {
0094:     $opm_passwordhash = strtoupper (trim ($_POST["Pw"]));
0095:     $opm_operation = strtolower (trim ($_POST["Op"]));
0096:     $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["Fn"])))));
0097:     $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_POST["SD"])))));
0098:     $opm_query = base64_decode (trim ($_POST["Qy"]));
0099:     $opm_reqversion = trim ($_POST["Vn"]);
0100:     $opm_logintimestamp = trim ($_POST["TS"]);
0101:     $opm_compress = abs ($_POST["Gz"]);
0102:   }
0103:   else {
0104:     $opm_passwordhash = strtoupper (trim ($_GET["Pw"]));
0105:     $opm_operation = strtolower (trim ($_GET["Op"]));
0106:     $opm_filename = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["Fn"])))));
0107:     $opm_subdir = str_replace (" ", "_", str_replace ("..", "", str_replace ("\\", "", str_replace ("//", "/", trim ($_GET["SD"])))));
0108:     $opm_query = trim ($_GET["Qy"]);
0109:     $opm_reqversion = trim ($_GET["Vn"]);
0110:     $opm_logintimestamp = trim ($_GET["TS"]);
0111:     $opm_compress = abs ($_GET["Gz"]);
0112:   }
0113:   if (($opm_filename{0} == "/") || ($opm_filename{0} == ".")) {
0114:     $opm_filename = substr ($opm_filename, 1, strlen ($opm_filename) - 1);
0115:   }
0116:   $opm_compress = ($opm_compress && $opm_allow_compression);
0117:   /*
0118:   Note on parameters:
0119:   When the operation needs a parameter, it shold be send using
0120:   either FN or QY (Filename or Query). The difference is that
0121:   FN is cleaned (deleting slashes, dots and spaces) while QY
0122:   is not cleaned. However, QY is always Base64-encoded.
0123:   */
0124:   
0125:   
0126:   // Log basic information about the request.
0127:   if ($opm_enable_extralog) {
0128:     if (!$opm_browser_debug) {
0129:       opm_FNDebug_Log (print_r ($_POST, true));
0130:     }
0131:     else {
0132:       opm_FNDebug_Log (print_r ($_GET, true));
0133:     }
0134:   }
0135:   else {
0136:     opm_FNDebug_Log ("");
0137:   }
0138:   opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0139:   opm_FNDebug_Log ("PASSWORDHASH=" . $opm_passwordhash);
0140:   opm_FNDebug_Log ("OPERATION=" . $opm_operation);
0141:   opm_FNDebug_Log ("FILENAME=" . $opm_filename);
0142:   opm_FNDebug_Log ("SUBDIR=" . $opm_subdir);
0143:   opm_FNDebug_Log ("QUERY=" . $opm_query);
0144:   opm_FNDebug_Log ("REQVERSION=" . $opm_reqversion);
0145:   opm_FNDebug_Log ("LOGINTIMESTAMP=" . $opm_logintimestamp);
0146:   opm_FNDebug_Log ("COMPRESS=" . $opm_compress);
0147:   
0148:   
0149:   // Include osCommerce database configuration and functions.
0150:   require ("includes/configure.php");
0151:   require (DIR_WS_INCLUDES . "database_tables.php");
0152:   require (DIR_WS_FUNCTIONS . "database.php");
0153:   
0154:   
0155:   if ($opm_browser_debug) { echo "<pre>"; }
0156:   
0157:   // Check if script version is the expected by client.
0158:   if (($opm_script_version != $opm_reqversion) && ($opm_operation)) {
0159:     opm_FNDebug_Log ("ERROR 1000 wrong script version. Expected $opm_reqversion, but we are $opm_script_version.");
0160:     echo "ERROR 1000 WRONG SCRIPT VERSION";
0161:     return;
0162:   }
0163:   
0164:   // Check if the image directory is valid.
0165:   if (!is_dir ($opm_realpath) && ($opm_operation)) {
0166:     opm_FNDebug_Log ("ERROR 1010 cannot find the images directory. Expecting $opm_realpath exist.");
0167:     echo "ERROR 1010 IMAGE DIRECTORY NOT FOUND";
0168:     return;
0169:   }
0170:   
0171:   
0172:   // Connect to database.
0173:   if (!tep_db_connect ()) {
0174:     echo "ERROR 1031 NO DATABASE CONNECTION";
0175:     return;
0176:   }
0177:   
0178:   
0179:   // Get osCommerce DB password and compare it with client password.
0180:   // Unless no operation is requested.
0181:   if ($opm_operation) {
0182:     if ($opm_enable_auth) {
0183:       if (defined ("DB_SERVER_PASSWORD") || ($opm_password_override != "")) {
0184:         // Check if the client is known...
0185:         $opm_failed_attempts = 0;
0186:         $opm_failed_time = time ();
0187:         $opm_sql_result = tep_db_query ("select customer_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url from " . TABLE_WHOS_ONLINE . " where customer_id=0 and session_id='$opm_session_string' and ip_address='$opm_ip_address'");
0188:         if ($opm_sql_record = tep_db_fetch_array ($opm_sql_result)) {
0189:           // It is known...
0190:           if (round (abs ($opm_sql_record["time_last_click"])) > ($opm_failed_time - $opm_max_tracktime)) {
0191:             $opm_failed_attempts = round (abs ($opm_sql_record["last_page_url"]));
0192:             if ($opm_failed_attempts > $opm_max_failedlogins) {
0193:               // It is blocked...
0194:               $opm_failed_time = time () + $opm_max_blocktime;
0195:             }
0196:           }
0197:           else {
0198:             // We are not tracking it...
0199:             $opm_failed_attempts = 0;
0200:           }
0201:           // Updates the tracking information...
0202:           $opm_sql_result = tep_db_query ("update " . TABLE_WHOS_ONLINE . " set time_last_click='$opm_failed_time' where customer_id=0 and session_id='$opm_session_string' and ip_address='$opm_ip_address'");
0203:         }
0204:         else {
0205:           // Creates a tracking record...
0206:           $opm_sql_result = tep_db_query ("insert into " . TABLE_WHOS_ONLINE . " (customer_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values (0, '$opm_session_string', '$opm_session_string', '$opm_ip_address', '$opm_failed_time', '$opm_failed_time', '0')");
0207:         }
0208:         if ($opm_failed_attempts > $opm_max_failedlogins) {
0209:           // The client is blocked, show error...
0210:           opm_FNDebug_Log ("ERROR 1024 the client ($opm_ip_address) has been blocked, too many failed logins ($opm_failed_attempts).");
0211:           sleep ($opm_sleep_afterfail);
0212:           echo "ERROR 1024 BLOCKED";
0213:           return;
0214:         }
0215:         else {
0216:           // The client is not blocked, continue...
0217:           if ($opm_password_override != "") {
0218:             opm_FNDebug_Log ("Password override is enabled.");
0219:             $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . $opm_password_override));
0220:           }
0221:           else {
0222:             $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . DB_SERVER_PASSWORD));
0223:           }
0224:           opm_FNDebug_Log ("SERVERPASSWORDHASH=" . $opm_serverpassword);
0225:           if ($opm_serverpassword != $opm_passwordhash) {
0226:             // Password mismatch...
0227:             opm_FNDebug_Log ("ERROR 1020 the server and client passwords do not match.");
0228:             $opm_failed_attempts++;
0229:             opm_FNDebug_Log ("Failed logins from $opm_ip_address = $opm_failed_attempts");
0230:             // Update tracking data about this...
0231:             $opm_sql_result = tep_db_query ("update " . TABLE_WHOS_ONLINE . " set time_last_click='$opm_failed_time', last_page_url='$opm_failed_attempts' where customer_id=0 and session_id='$opm_session_string' and ip_address='$opm_ip_address'");
0232:             if ($opm_failed_attempts > 1) {
0233:               // This is not the first failure, delay a bit...
0234:               sleep ($opm_sleep_afterfail);
0235:             }
0236:             echo "ERROR 1020 PASSWORD MISMATCH";
0237:             return;
0238:           }
0239:           else {
0240:             // Password match, reset failures count in tracking data...
0241:             $opm_sql_result = tep_db_query ("update " . TABLE_WHOS_ONLINE . " set time_last_click='$opm_failed_time', last_page_url='0' where customer_id=0 and session_id='$opm_session_string' and ip_address='$opm_ip_address'");
0242:           }
0243:         }
0244:       }
0245:       else {
0246:         opm_FNDebug_Log ("ERROR 1021 the server password was not found.");
0247:         echo "ERROR 1021 CANNOT FIND SERVER PASSWORD";
0248:         return;
0249:       }
0250:     }
0251:     else {
0252:       opm_FNDebug_Log ("Password authentication is disabled.");
0253:     }
0254:   }
0255:   else {
0256:     $opm_simpletest_dbok = false;
0257:     $opm_simpletest_imgok = false;
0258:     echo "<html><head><title>osCommerce Product Manager (OSCPMWin).</title>\n";
0259:     echo "<style>\nBODY { font-family: sans-serif; background-color: #FFFFFF; }\n";
0260:     echo "LI { padding: 5px; }\n";
0261:     echo "H3 { padding: 5px 5px 5px 20px; }\n";
0262:     echo "A { color: #0000FF; text-decoration: none; }\n";
0263:     echo "A:hover { color: #FF0000; text-decoration: underline; }\n";
0264:     echo "DIV { border: solid 2px #000000; margin: 10px 20px 10px 20px; }";
0265:     echo ".ok { background-color: #CCFFCC; }\n";
0266:     echo ".notok { background-color: #FFCCCC; }\n";
0267:     echo ".info { background-color: #EEEEFF; }\n";
0268:     echo ".links { background-color: #EEEEEE; }\n";
0269:     echo "</style>\n";
0270:     echo "</head><body>\n";
0271:     echo "<h3>osCommerce Product Manager (OSCPMWin),<br>server-side script $opm_script_version</h3>\n";
0272:     $opm_simpletest_dbok = (boolean) (tep_db_connect ());
0273:     $opm_simpletest_imgok = (boolean) (file_exists ($opm_realpath) && is_readable ($opm_realpath) && is_writable ($opm_realpath) && is_dir ($opm_realpath));
0274:     if ($opm_simpletest_dbok && $opm_simpletest_imgok) {
0275:       echo "<div class=\"ok\">";
0276:     }
0277:     else {
0278:       echo "<div class=\"notok\">";
0279:     }
0280:     echo "<ul>\n";
0281:     if ($opm_simpletest_dbok) {
0282:       echo "<li><strong>Database connection seems OK.</strong></li>\n";
0283:     }
0284:     else {
0285:       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";
0286:     }
0287:     if ($opm_simpletest_imgok) {
0288:       echo "<li><strong>Image directory seems OK.</strong></li>\n";
0289:     }
0290:     else {
0291:       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";
0292:     }
0293:     echo "</ul>";
0294:     if ($opm_simpletest_dbok && $opm_simpletest_imgok) {
0295:       echo "<h3><strong>Everything seems OK. &nbsp; &nbsp; &nbsp; :)</strong></h3>\n";
0296:     }
0297:     else {
0298:       echo "<h3><strong>There seems to be a problem. &nbsp; &nbsp; &nbsp; :(</strong></h3>\n";
0299:     }
0300:     echo "</div>";
0301:     echo "<div class=\"info\"><ul>";
0302:     if ($opm_browser_debug) {
0303:       echo "<li>Browser-debug mode is enabled.";
0304:     }
0305:     else {
0306:       echo "<li>Browser-debug mode is disabled";
0307:     }
0308:     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";
0309:   
0310:     if ($opm_enable_logfile) {
0311:       echo "<li>Logging file is enabled.";
0312:     }
0313:     else {
0314:       echo "<li>Logging file is disabled";
0315:     }
0316:     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";
0317:   
0318:     if ($opm_cfg_useproxy) {
0319:       echo "<li>Proxy-usage is enabled.";
0320:     }
0321:     else {
0322:       echo "<li>Proxy-usage is disabled";
0323:     }
0324:     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";
0325:   
0326:     if ($opm_allow_compression) {
0327:       echo "<li>Compression is enabled.";
0328:     }
0329:     else {
0330:       echo "<li>Compression is disabled";
0331:     }
0332:     echo "<br><small>Compression of data traffic improves the speed of the data transfer. The compression level is set to $opm_compress_level. This setting is not enforced unless the client application request a compressed data stream too.</small></li>\n";
0333:   
0334:     echo "<li>The time zone shift is set to $opm_timezone_shift.<br><small>To be used with some timestamps</small>.</li>\n";
0335:     echo "</ul></div>";
0336:     echo "<div class=\"links\"><ul>";
0337:     echo "<li><a href=\"http://www.mariovaldez.net/software/oscpmwin/\">OSCPMWin homepage</a>.</li>\n";
0338:     echo "<li><a href=\"http://www.mariovaldez.net/webapps/forums/index.php?c=9\">OSCPMWin web forums</a>.</li>\n";
0339:     echo "<li><a href=\"http://www.oscommerce.org/\">osCommerce homepage</a>.</li>\n";
0340:     echo "</ul></div>";
0341:     echo "<p><small>&copy;2003-2007 by Mario A. Valdez-Ramirez.<br>\n";
0342:     echo "This program is free software; you can redistribute it and/or modify\n";
0343:     echo "it under the terms of the GNU General Public License as published by\n";
0344:     echo "the Free Software Foundation; either version 2 of the License, or (at\n";
0345:     echo "your option) any later version.</small></p>\n";
0346:     echo "</body></html>";
0347:     return;
0348:   }
0349:   
0350:   
0351:   
0352:   // ==================================
0353:   // Do nothing.
0354:   // NOOP do nothing.
0355:   // ==================================
0356:   if ($opm_operation == "noop") {
0357:     echo "OK NOOP START\n";
0358:     opm_FNDebug_Log ("OK NOOP requested.");
0359:     echo "OK NOOP END\n";
0360:   }
0361:   
0362:   // ==================================
0363:   // Returns a PHPINFO dump.
0364:   // PHPINFO returns a dump of PHPINFO function.
0365:   // ==================================
0366:   if ($opm_operation == "phpinfo") {
0367:     echo "OK PHPINFO START\n";
0368:     ob_start ();
0369:     phpinfo ();
0370:     $opm_phpinfo = ob_get_contents ();
0371:     ob_end_clean ();
0372:     $opm_phpinfo = str_replace ("<h2>", str_repeat ("=", 40) . "\n* ", $opm_phpinfo);
0373:     $opm_phpinfo = str_replace ("</h2>", "\n" . str_repeat ("=", 40) . "\n", $opm_phpinfo);
0374:     $opm_phpinfo = str_replace ("<th>", " | ", $opm_phpinfo);
0375:     $opm_phpinfo = str_replace ("</th>", " | ", $opm_phpinfo);
0376:     $opm_phpinfo = str_replace ("<td class=\"v\">", " | ", $opm_phpinfo);
0377:     $opm_phpinfo = strip_tags ($opm_phpinfo);
0378:     $opm_phpinfo_lines = explode ("\n", $opm_phpinfo);
0379:     $opm_phpinfo = "";
0380:     foreach ($opm_phpinfo_lines as $opm_phpinfo_curline) {
0381:       $opm_phpinfo_curline = trim ($opm_phpinfo_curline);
0382:       if ($opm_phpinfo_curline) {
0383:         $opm_phpinfo .= $opm_phpinfo_curline . "\n";
0384:       }
0385:     }
0386:     echo $opm_phpinfo;
0387:     echo "OK PHPINFO END\n";
0388:   }
0389:   
0390:   // ==================================
0391:   // CHECK if a file exists.
0392:   // CHECK requires the full path of the image.
0393:   // ==================================
0394:   if ($opm_operation == "check") {
0395:     if ($opm_filename) {
0396:       if (file_exists ($opm_realpath . $opm_filename)) {
0397:         opm_FNDebug_Log ("OK file found $opm_filename.");
0398:         echo "OK FILE EXISTS";
0399:       }
0400:       else {
0401:         opm_FNDebug_Log ("ERROR 1110 file not found $opm_filename.");
0402:         echo "ERROR 1110 FILE NOT FOUND";
0403:       }
0404:     }
0405:   }
0406:   
0407:   // ==================================
0408:   // LIST all files recursively.
0409:   // LIST returns the filenames with paths.
0410:   // ==================================
0411:   if ($opm_operation == "list") {
0412:     echo "OK LIST START\n";
0413:     opm_FNList_Files ($opm_realpath, "", false);
0414:     echo "OK LIST END\n";
0415:   }
0416:   
0417:   // ==================================
0418:   // LISTDIRS only dirs recursively.
0419:   // LISTDIRS returns no filenames.
0420:   // ==================================
0421:   if ($opm_operation == "listdirs") {
0422:     echo "OK LISTDIRS START\n";
0423:     opm_FNList_Files ($opm_realpath, "", true);
0424:     echo "OK LISTDIRS END\n";
0425:   }
0426:   
0427:   // ==================================
0428:   // DELETE an image.
0429:   // DELETE requires the filename of the image with path.
0430:   // DELETE1 deletes the image if less than one record references it.
0431:   // DELETE2 deletes the image if less than two records reference it.
0432:   // ==================================
0433:   if (($opm_operation == "delete1") || ($opm_operation == "delete2")) {
0434:     if ($opm_operation == "delete1") {
0435:       $allowed_owners = 1;
0436:     }
0437:     else {
0438:       $allowed_owners = 2;
0439:     }
0440:     if ($opm_filename) {
0441:       // Check for More Pics 6 extension...
0442:       $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6"));
0443:       if ($opm_sqlresult[0]) {
0444:         $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'"));
0445:       }
0446:       else {
0447:         $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select count(products_id) from " . TABLE_PRODUCTS . " where products_image='$opm_filename'"));
0448:       }
0449:       if ($opm_sqlresult[0] < $allowed_owners) {
0450:         if (file_exists ($opm_realpath . $opm_filename)) {
0451:           if (@unlink ($opm_realpath . $opm_filename)) {
0452:             if (!file_exists ($opm_realpath . $opm_filename)) {
0453:               opm_FNDebug_Log ("OK file deleted $opm_filename.");
0454:               echo "OK DELETE";
0455:             }
0456:             else {
0457:               opm_FNDebug_Log ("ERROR 1120 file cannot be deleted $opm_filename.");
0458:               echo "ERROR 1120 DELETE FILE REMAINS";
0459:             }
0460:           }
0461:           else {
0462:             opm_FNDebug_Log ("ERROR 1121 when deleting $opm_filename.");
0463:             echo "ERROR 1121 DELETE UNKNOWN";
0464:           }
0465:         }
0466:         else {
0467:           opm_FNDebug_Log ("OK file not found when deleting $opm_filename.");
0468:           echo "OK DELETE FILE NOT FOUND";
0469:         }
0470:       }
0471:       else {
0472:         opm_FNDebug_Log ("OK file is used by other record (" . ($opm_sqlresult[0] - 1) . ").");
0473:         echo "OK DELETE FILE IN USE";
0474:       }
0475:     }
0476:     else {
0477:       opm_FNDebug_Log ("OK filename empty.");
0478:       echo "OK DELETE FILENAME EMPTY";
0479:     }
0480:   }
0481:   
0482:   // ==================================
0483:   // UPLOAD an image.
0484:   // UPLOAD requires the path, without filename, where the image will be stored.
0485:   // UPLOAD returns the image filename with the path.
0486:   // ==================================
0487:   if ($opm_operation == "upload") {
0488:     if ($_FILES["Fl"]["name"] && $_FILES["Fl"]["size"]) {
0489:       if (is_uploaded_file ($_FILES["Fl"]["tmp_name"])) {
0490:         if ($opm_filename) {
0491:           $newfn = $opm_filename;
0492:         }
0493:         else {
0494:           $newfn = trim ($_FILES["Fl"]["name"]);
0495:         }
0496:         $newfn = str_replace (" ", "_", $newfn);
0497:         $newfn = str_replace ("/", "", $newfn);
0498:         $newfn = str_replace ("\\", "", $newfn);
0499:         $newfn = ereg_replace("[^[:alnum:]\._]", "", $newfn);
0500:         $newfn = strtolower ($newfn);
0501:   
0502:         if ($opm_subdir) {
0503:           if ((!file_exists ($opm_realpath . $opm_subdir)) || (!is_dir ($opm_realpath . $opm_subdir))) {
0504:             opm_FNDebug_Log ("will create directory=" . $opm_realpath . $opm_subdir);
0505:             opm_FNmkdirs ($opm_realpath . $opm_subdir, 0777);
0506:           }
0507:           if (file_exists ($opm_realpath . $opm_subdir) && (is_dir ($opm_realpath . $opm_subdir))) {
0508:             opm_FNDebug_Log ("directory exists=" . $opm_realpath . $opm_subdir);
0509:             $newfn = $opm_subdir . "/" . $newfn;
0510:           }
0511:           else {
0512:             opm_FNDebug_Log ("directory does not exist, will ignore=" . $opm_realpath . $opm_subdir);
0513:           }
0514:         }
0515:         opm_FNDebug_Log ("wanted filename=" . $newfn);
0516:         $newfn = opm_FNSmart_Rename ($newfn, 100);
0517:         opm_FNDebug_Log ("smart filename=" . $newfn);
0518:         opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
0519:         if (!file_exists ($opm_realpath . $newfn)) {
0520:           opm_FNDebug_Log ("file not exist=" . $opm_realpath . $newfn);
0521:           if (@move_uploaded_file ($_FILES["Fl"]["tmp_name"], $opm_realpath . $newfn)) {
0522:             opm_FNDebug_Log ("moving file to=" . $opm_realpath . $newfn);
0523:             if (file_exists ($opm_realpath . $newfn)) {
0524:               opm_FNDebug_Log ("upload complete=" . $opm_realpath . $newfn);
0525:               @chmod ($opm_realpath . $newfn, octdec ($opm_upload_perms));
0526:               echo "OK UPLOAD [" . $newfn . "]";
0527:             }
0528:             else {
0529:               opm_FNDebug_Log ("ERROR 1130 file not copied=" . $opm_realpath . $newfn);
0530:               echo "ERROR 1130 UPLOAD FILE NOT FOUND";
0531:             }
0532:           }
0533:           else {
0534:             opm_FNDebug_Log ("ERROR 1131 file not copied=" . $opm_realpath . $newfn);
0535:             echo "ERROR 1131 UPLOAD CANNOT MOVE";
0536:           }
0537:         }
0538:         else {
0539:           opm_FNDebug_Log ("ERROR 1133 file exist=" . $opm_realpath . $newfn);
0540:           echo "ERROR 1133 UPLOAD EXIST [" . $newfn . "]";
0541:         }
0542:       }
0543:       else {
0544:         opm_FNDebug_Log ("ERROR 1134 cannot find uploaded file!");
0545:         echo "ERROR 1134 UPLOAD";
0546:       }
0547:     }
0548:   }
0549:   
0550:   // ==================================
0551:   // CAPABILITY listing.
0552:   // CAPABILITY returns a list of identified modules.
0553:   // ==================================
0554:   if ($opm_operation == "capa") {
0555:     opm_FNDebug_Log ("OK Capability search started.");
0556:     echo "OK CAPABILITY START\n";
0557:   
0558:     // Credit Card Payment...
0559:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_CC_STATUS'"));
0560:     if ($opm_sqlresult[0]) {
0561:       opm_FNDebug_Log ("OK PAY_CC capcbility found.");
0562:       echo "PAY_CC\n";
0563:     }
0564:   
0565:     // Money Order Payment...
0566:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_MONEYORDER_STATUS'"));
0567:     if ($opm_sqlresult[0]) {
0568:       opm_FNDebug_Log ("OK PAY_ORDER capcbility found.");
0569:       echo "PAY_ORDER\n";
0570:     }
0571:   
0572:     // COD Payment...
0573:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COD_STATUS'"));
0574:     if ($opm_sqlresult[0]) {
0575:       opm_FNDebug_Log ("OK PAY_CC capcbility found.");
0576:       echo "PAY_CC\n";
0577:     }
0578:   
0579:     // PayPal Payment...
0580:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPAL_STATUS'"));
0581:     if ($opm_sqlresult[0]) {
0582:       opm_FNDebug_Log ("OK PAY_PP capcbility found.");
0583:       echo "PAY_PP\n";
0584:     }
0585:   
0586:     // Flat Rate Shipping...
0587:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_FLAT_STATUS'"));
0588:     if ($opm_sqlresult[0]) {
0589:       opm_FNDebug_Log ("OK SHIP_FLAT capcbility found.");
0590:       echo "SHIP_FLAT\n";
0591:     }
0592:   
0593:     // Table Shipping...
0594:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'"));
0595:     if ($opm_sqlresult[0]) {
0596:       opm_FNDebug_Log ("OK SHIP_TBL capcbility found.");
0597:       echo "SHIP_TBL\n";
0598:     }
0599:   
0600:     // Store PickUp Shipping...
0601:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SPU_STATUS'"));
0602:     if ($opm_sqlresult[0]) {
0603:       opm_FNDebug_Log ("OK SHIP_SPU capcbility found.");
0604:       echo "SHIP_SPU\n";
0605:     }
0606:   
0607:     // Multi Pickup Shipping...
0608:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'stores'"));
0609:     if ($opm_sqlresult[0]) {
0610:       opm_FNDebug_Log ("OK SHIP_MPU capcbility found.");
0611:       echo "SHIP_MPU\n";
0612:     }
0613:   
0614:     // More Pics 6...
0615:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6"));
0616:     if ($opm_sqlresult[0]) {
0617:       opm_FNDebug_Log ("OK MOREPICS6 capcbility found.");
0618:       echo "MOREPICS6\n";
0619:     }
0620:   
0621:     // Total B2B...
0622:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_CUSTOMERS . " customers_groups_id"));
0623:     if ($opm_sqlresult[0]) {
0624:       opm_FNDebug_Log ("OK TOTALB2B capcbility found.");
0625:       echo "TOTALB2B\n";
0626:     }
0627:   
0628:     // Poll Booth...
0629:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'phesis_poll_config'"));
0630:     if ($opm_sqlresult[0]) {
0631:       opm_FNDebug_Log ("OK POLLBOOTH capcbility found.");
0632:       echo "POLLBOOTH\n";
0633:     }
0634:   
0635:     // Header Tags Controller...
0636:     $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS_DESCRIPTION . " products_head_keywords_tag"));
0637:     if ($opm_sqlresult[0]) {
0638:       opm_FNDebug_Log ("OK HEADERTAGS capcbility found.");
0639:       echo "HEADERTAGS\n";
0640:     }
0641:   
0642:     opm_FNDebug_Log ("OK Capability search finished.");
0643:     echo "OK CAPABILITY END\n";
0644:   }
0645:   
0646:   // ==================================
0647:   // BACKUP database.
0648:   // BACKUP returns a SQL dump.
0649:   // ==================================
0650:   if ($opm_operation == "backup") {
0651:     opm_FNDebug_Log ("OK Backup of database requested.");
0652:     $opm_bak_filename = date ("Ymd_His", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y"))) . ".sql";
0653:     opm_FNBackup ($opm_realpath . $opm_bak_filename);
0654:     if (file_exists ($opm_realpath . $opm_bak_filename)) {
0655:       opm_FNgzip_file ($opm_realpath . $opm_bak_filename, $opm_realpath . $opm_bak_filename . ".gz");
0656:       if (file_exists ($opm_realpath . $opm_bak_filename . ".gz")) {
0657:         $opm_bak_filesize = filesize ($opm_realpath . $opm_bak_filename . ".gz");
0658:         header ("Content-type: application/octet-stream");
0659:         header ("Content-disposition: attachement; filename=" . $opm_bak_filename . ".gz");
0660:         header("Content-Length: " . $opm_bak_filesize);
0661:         header ("Pragma: no-store");
0662:         readfile ($opm_realpath . $opm_bak_filename . ".gz");
0663:         @unlink ($opm_realpath . $opm_bak_filename . ".gz");
0664:         opm_FNDebug_Log ("OK Deleting compressed backup file.");
0665:       }
0666:       @unlink ($opm_realpath . $opm_bak_filename);
0667:       opm_FNDebug_Log ("OK Deleting uncompressed backup file.");
0668:     }
0669:     else {
0670:       opm_FNDebug_Log ("ERROR 1140 Backup file not found.");
0671:     }
0672:     opm_FNDebug_Log ("OK Backup operation finished.");
0673:   }
0674:   
0675:   
0676:   // ==================================
0677:   // Get XCHGRATES.
0678:   // XCHGRATES returns the exchange rates of currencies.
0679:   // ==================================
0680:   if ($opm_operation == "xchgrates") {
0681:     opm_FNDebug_Log ("OK Starting XCHGRATES operation.");
0682:     if ($opm_filename) {
0683:       $opm_filename = strtoupper ($opm_filename);
0684:       $opm_main_currency = substr ($opm_filename, 0, 3);
0685:       $opm_xchange_currencies = str_replace (",", "_", substr ($opm_filename, 4));
0686:       if (($opm_main_currency) && ($opm_xchange_currencies)) {
0687:         $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";
0688:         $opm_page_data = opm_FNget_webpage ($opm_xchange_page);
0689:         if ($opm_page_data) {
0690:           $opm_page_data = strip_tags ($opm_page_data);
0691:           $opm_page_data = preg_replace ("/[\r\n]+[\s\t]*[\r\n]+/", "", $opm_page_data);
0692:           $opm_xr_matches = array ();
0693:           preg_match_all ("/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i", $opm_page_data, $opm_xr_matches);
0694:           echo "OK XCHGRATES START\n";
0695:           foreach ($opm_xr_matches[2] as $key => $value) {
0696:             echo $opm_xr_matches[2][$key] . "," . $opm_xr_matches[4][$key] . "\n";
0697:           }
0698:           echo "OK XCHGRATES END\n";
0699:         }
0700:         else {
0701:           opm_FNDebug_Log ("ERROR 1150 The remote server answer is empty or invalid.");
0702:           echo "ERROR 1150 XCHGRATES REMOTE SERVER ERROR";
0703:         }
0704:       }
0705:       else {
0706:         opm_FNDebug_Log ("ERROR 1151 The specified currency codes are invalid.");
0707:         echo "ERROR 1151 XCHGRATES INVALID CURRENCY";
0708:       }
0709:     }
0710:     else {
0711:       opm_FNDebug_Log ("ERROR 1152 The no currency codes were specified.");
0712:       echo "ERROR 1152 XCHGRATES EMPTY CURRENCY";
0713:     }
0714:   }
0715:   
0716:   
0717:   
0718:   // ==================================
0719:   // Execute DBQUERY.
0720:   // DBQUERY returns ...
0721:   // ==================================
0722:   if ($opm_operation == "dbquery") {
0723:     opm_FNDebug_Log ("OK Starting DBQUERY operation.");
0724:     if ($opm_query) {
0725:       echo "OK DBQUERY START\n";
0726:       echo opm_FNExec_Query ($opm_query, false, true);
0727:       echo "OK DBQUERY END\n";
0728:     }
0729:     else {
0730:       opm_FNDebug_Log ("ERROR 1171 The database query is empty.");
0731:       echo "ERROR 1171 DBQUERY EMPTY DATABASE QUERY";
0732:     }
0733:     opm_FNDebug_Log ("OK Finishing DBQUERY operation.");
0734:   }
0735:   
0736:   
0737:   
0738:   if ($opm_browser_debug) { echo "</pre>"; }
0739:   return;
0740:   
0741:   
0742:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0743:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0744:   function opm_FNList_Files ($dirname, $parentdir, $onlydirs) {
0745:   	$dirhandle = opendir ($dirname);
0746:     while (($file = readdir ($dirhandle)) != false)
0747:       $sorteddir[count ($sorteddir)] = $file;
0748:   	closedir ($dirhandle);
0749:     natcasesort ($sorteddir);
0750:     if (!$onlydirs) {
0751:     	foreach ($sorteddir as $file) {
0752:         if (($file != ".") && ($file != "..")) {
0753:           if (!is_dir ($dirname . $file)) {
0754:             if ($parentdir) {
0755:               echo $parentdir . "/" . $file . "\n";
0756:             }
0757:             else {
0758:               echo $file . "\n";
0759:             }
0760:           }
0761:         }
0762:       }
0763:       foreach ($sorteddir as $file) {
0764:         if (($file != ".") && ($file != "..")) {
0765:           if (is_dir ($dirname . $file)) {
0766:             if ($parentdir) {
0767:               opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0768:             }
0769:             else {
0770:               opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0771:             }
0772:           }
0773:         }
0774:       }
0775:     }
0776:     else {
0777:       foreach ($sorteddir as $file) {
0778:         if (($file != ".") && ($file != "..")) {
0779:           if (is_dir ($dirname . $file)) {
0780:             if ($parentdir) {
0781:               echo $parentdir . "/" . $file . "\n";
0782:               opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs);
0783:             }
0784:             else {
0785:               echo $file . "\n";
0786:               opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs);
0787:             }
0788:           }
0789:         }
0790:       }
0791:     }
0792:   }
0793:   
0794:   
0795:   
0796:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0797:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0798:   function opm_FNSmart_Rename ($wantedname, $tries) {
0799:   global $opm_realpath;
0800:     $newname = $wantedname;
0801:     $newext = '.' . array_pop (explode ('.', $wantedname));
0802:     for ($trycounter = 1; $trycounter <= $tries; $trycounter++) {
0803:       if (!file_exists ($opm_realpath . $newname)) {
0804:         return ($newname);
0805:       }
0806:       else {
0807:         $newdirname = dirname ($wantedname);
0808:         if ($newdirname != ".") {
0809:           $newname = dirname ($wantedname) . "/" . basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0810:         }
0811:         else {
0812:           $newname = basename ($wantedname, $newext) . "_" . $trycounter . $newext;
0813:         }
0814:       }
0815:     }
0816:     return ($wantedname);
0817:   }
0818:   
0819:   
0820:   
0821:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0822:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0823:   function opm_FNDebug_Log ($logline) {
0824:   global $opm_enable_logfile, $opm_debug_file, $opm_timezone_shift;
0825:     if ($opm_enable_logfile) {
0826:       if (!(file_exists ($opm_debug_file) && !is_writable ($opm_debug_file))) {
0827:         $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")));
0828:         $opm_lfilef = @fopen ($opm_debug_file, 'a');
0829:         if ($opm_lfilef) {
0830:           if ($logline) {
0831:             @fwrite ($opm_lfilef, $opm_timestamp . ": " . $logline . "\n");
0832:           }
0833:           else {
0834:             @fwrite ($opm_lfilef, "\n");
0835:           }
0836:           @fclose ($opm_lfilef);
0837:         }
0838:       }
0839:     }
0840:   }
0841:   
0842:   
0843:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0844:   // Based on code posted on php.net by saint@corenova.com and bart@cdasites.com
0845:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0846:   function opm_FNmkdirs ($dirname) {
0847:     if (is_dir ($dirname) || empty ($dirname)) return 1;
0848:     if (file_exists ($dirname) && !is_dir ($dirname)) return 0;
0849:     if (opm_FNmkdirs (substr ($dirname, 0, strrpos ($dirname, '/')))) {
0850:       if (!file_exists($dirname)) {
0851:         return @mkdir ($dirname, 0777);
0852:       }
0853:     }
0854:     return 0;
0855:   }
0856:   
0857:   
0858:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0859:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0860:   function opm_FNExec_Query ($sql_query, $rawdata = true, $encodeddata = false) {
0861:   global $opm_tag_recordbegin, $opm_tag_recordend, $opm_tag_field, $opm_tag_cfield, $opm_tag_fieldlist;
0862:   global $opm_tag_databegin, $opm_tag_dataend, $opm_tag_comment, $opm_tag_stats;
0863:   global $opm_compress, $opm_compress_level;
0864:     $result_string = "";
0865:     $record_count = 0;
0866:     $query_type = strtoupper (substr ($sql_query, 0, strpos ($sql_query, " ")));
0867:     opm_FNDebug_Log ("SQLQUERY=" . $sql_query);
0868:     $sql_result = tep_db_query ($sql_query);
0869:     if (($query_type == "SELECT") || ($query_type == "SHOW") || ($query_type == "DESCRIBE")) {
0870:       while ($sql_record = tep_db_fetch_array ($sql_result)) {
0871:         $record_count++;
0872:         $sql_fullrec = "";
0873:         if ($rawdata) {
0874:           foreach ($sql_record as $key => $value) {
0875:             $result_string .= $value . "\n";
0876:           }
0877:         }
0878:         else {
0879:           $result_string .= "$opm_tag_recordbegin\n";
0880:           foreach ($sql_record as $key => $value) {
0881:             if ($encodeddata) {
0882:               $encodedfield = base64_encode ($value);
0883:             }
0884:             else {
0885:               $encodedfield = htmlentities ($value);
0886:             }
0887:             $result_string .= "$opm_tag_field " . $opm_tag_databegin . $encodedfield . $opm_tag_dataend . "\n";
0888:             $sql_fullrec .= $encodedfield;
0889:           }
0890:           $result_string .= "$opm_tag_recordend " . abs (crc32 ($sql_fullrec)) . "\n";
0891:         }
0892:       }
0893:     }
0894:     elseif ($query_type == "INSERT") {
0895:       $sql_result = tep_db_insert_id ();
0896:       $record_count = 1;
0897:       $sql_fullrec = "";
0898:       if ($rawdata) {
0899:         $result_string .= $sql_result . "\n";
0900:       }
0901:       else {
0902:         $result_string .= "$opm_tag_recordbegin\n";
0903:         if ($encodeddata) {
0904:           $encodedfield = base64_encode ($sql_result);
0905:         }
0906:         else {
0907:           $encodedfield = htmlentities ($sql_result);
0908:         }
0909:         $result_string .= "$opm_tag_field " . $opm_tag_databegin . $encodedfield . $opm_tag_dataend . "\n";
0910:         $sql_fullrec .= $encodedfield;
0911:         $result_string .= "$opm_tag_recordend " . abs (crc32 ($sql_fullrec)) . "\n";
0912:       }
0913:     }
0914:     if (!$rawdata) {
0915:       if ($opm_compress) {
0916:         opm_FNDebug_Log ("RECORDSET SIZE (UNCOMPRESSED)=" . strlen ($result_string));
0917:         $result_string = base64_encode (gzcompress ($result_string, $opm_compress_level)) . "\n";
0918:       }
0919:     }
0920:     opm_FNDebug_Log ("RECORDSET SIZE=" . strlen ($result_string));
0921:     return $result_string;
0922:   }
0923:   
0924:   
0925:   
0926:   
0927:   
0928:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0929:   // Based on code from osCommerce (/admin/backup.php).
0930:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0931:   function opm_FNBackup ($dump_file) {
0932:   global $opm_script_version;
0933:     if ($dump_file) {
0934:       $bakfilef = fopen ($dump_file, 'w');
0935:       $bakline  = "# OSCPMWin (server-side $opm_script_version)\n";
0936:       $bakline .= "# Database: " . DB_DATABASE . "@" . DB_SERVER . "\n";
0937:       $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";
0938:       fwrite ($bakfilef, $bakline);
0939:       $tables_query = tep_db_query ("show tables");
0940:       while ($tables = tep_db_fetch_array ($tables_query)) {
0941:         list (, $table) = each ($tables);
0942:         $bakline = "drop table if exists $table;\ncreate table $table (\n";
0943:         $table_list = array ();
0944:         $fields_query = tep_db_query ("show fields from $table");
0945:         while ($fields = tep_db_fetch_array($fields_query)) {
0946:           $table_list[] = $fields["Field"];
0947:           $bakline .= "  " . $fields["Field"] . " " . $fields["Type"];
0948:           if (strlen ($fields["Default"]) > 0) $bakline .= " default '" . $fields["Default"] . "'";
0949:           if (strtoupper ($fields["Null"]) != "YES") $bakline .= " not null";
0950:           if (isset ($fields["Extra"])) $bakline .= " " . $fields["Extra"];
0951:           $bakline .= ",\n";
0952:         }
0953:         $bakline = ereg_replace(",\n$", '', $bakline);
0954:         $index = array ();
0955:         $keys_query = tep_db_query ("show keys from $table");
0956:         while ($keys = tep_db_fetch_array ($keys_query)) {
0957:           $kname = $keys["Key_name"];
0958:           if (!isset ($index[$kname])) {
0959:             $index[$kname] = array ("unique" => !$keys["Non_unique"], "columns" => array());
0960:           }
0961:           $index[$kname]["columns"][] = $keys["Column_name"];
0962:         }
0963:         while (list($kname, $info) = each($index)) {
0964:           $bakline .= ",\n";
0965:           $columns = implode($info["columns"], ", ");
0966:           if ($kname == "PRIMARY") {
0967:             $bakline .= "  PRIMARY KEY ($columns)";
0968:           } elseif ($info["unique"]) {
0969:             $bakline .= "  UNIQUE $kname ($columns)";
0970:           } else {
0971:             $bakline .= "  KEY $kname ($columns)";
0972:           }
0973:         }
0974:         $bakline .= "\n);\n\n";
0975:         fwrite ($bakfilef, $bakline);
0976:         $rows_query = tep_db_query ("select " . implode (",", $table_list) . " from $table");
0977:         while ($rows = tep_db_fetch_array ($rows_query)) {
0978:           $bakline = "insert into $table (" . implode (", ", $table_list) . ") values (";
0979:           reset ($table_list);
0980:           while (list (, $i) = each ($table_list)) {
0981:             if (!isset ($rows[$i])) {
0982:               $bakline .= "NULL, ";
0983:             } elseif (($rows[$i] != "") && (strtoupper ($rows[$i]) != "NULL")) {
0984:               $row = addslashes ($rows[$i]);
0985:               $row = ereg_replace("\n#", "\n".'\#', $row);
0986:               $bakline .= "'$row', ";
0987:             } else {
0988:               $bakline .= "'', ";
0989:             }
0990:           }
0991:           $bakline = ereg_replace (", $", "", $bakline) . ");\n";
0992:           fwrite ($bakfilef, $bakline);
0993:         }
0994:       }
0995:       fclose ($bakfilef);
0996:     }
0997:   }
0998:   
0999:   
1000:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001:   // Compress a file with the gzip algorithm.
1002:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003:   function opm_FNgzip_file ($opm_gzsource, $opm_gztarget) {
1004:     if (function_exists ("gzwrite") && file_exists ($opm_gzsource)) {
1005:       $opm_ungzfp = fopen($opm_gzsource, "rb");
1006:       $opm_gzfp = gzopen($opm_gztarget, "wb9");
1007:       if ($opm_gzfp && $opm_ungzfp) {
1008:         while (!feof ($opm_ungzfp)) {
1009:           gzwrite ($opm_gzfp, fread ($opm_ungzfp, 65535));
1010:         }
1011:         @fclose ($opm_ungzfp);
1012:         @gzclose ($opm_gzfp);
1013:         return (true);
1014:       }
1015:       else {
1016:         @fclose ($opm_ungzfp);
1017:         @gzclose ($opm_gzfp);
1018:         return (false);
1019:       }
1020:     }
1021:     else {
1022:       return (false);
1023:     }
1024:   }
1025:   
1026:   
1027:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028:   // Request a page from a web server.
1029:   // If using a proxy server, redirect the call thru the proxy.
1030:   // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1031:   function opm_FNget_webpage ($target_url) {
1032:     global $opm_useragent, $opm_httpconn_timeout, $opm_cfg_useproxy, $opm_cfg_proxyaddress, $opm_cfg_proxyport;
1033:     $opm_http_content = "";
1034:     $errno = 0;
1035:     $errstr = "";
1036:     $urlparts = parse_url ($target_url);
1037:     if ($urlparts["host"]) {
1038:       @ignore_user_abort (true);
1039:       if ($opm_cfg_useproxy) {
1040:         $opm_sockconn = @fsockopen ($opm_cfg_proxyaddress, $opm_cfg_proxyport, $errno, $errstr, $opm_httpconn_timeout);
1041:       }
1042:       else {
1043:         $opm_sockconn = @fsockopen ($urlparts["host"], (empty($urlparts["port"]) ? "80" : $urlparts["port"]), $errno, $errstr, $opm_httpconn_timeout);
1044:       }
1045:       if ($opm_sockconn) {
1046:         if ($opm_cfg_useproxy) {
1047:           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");
1048:         }
1049:         else {
1050:           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");
1051:         }
1052:         while ((!feof($opm_sockconn)) && (!is_readable ($opm_skfilename))) {
1053:           $opm_http_content .= fgets ($opm_sockconn, 10240);
1054:         }
1055:         @fclose ($opm_sockconn);
1056:         if (eregi ("HTTP.*200 OK", $opm_http_content)) {
1057:           $opm_http_content = str_replace ("\r", "", $opm_http_content);
1058:           $opm_http_content = substr ($opm_http_content, strpos ($opm_http_content, "\n\n") + 2);
1059:           return ($opm_http_content);
1060:         }
1061:       }
1062:     }
1063:     return ("");
1064:   }
1065:   
1066:   
1067:   
1068:   
1069:   ?>
 
 
NA fum/lmd: 2007.07.15
Copyright ©1994-2024 by Mario A. Valdez-Ramírez.
no siga este enlace / do not follow this link