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