0) { $opm_smartrename = 1; } else { $opm_smartrename = 0; } // Log basic information about the request. if ($opm_enable_extralog) { if (!$opm_browser_debug) { opm_FNDebug_Log (print_r ($_POST, true)); } else { opm_FNDebug_Log (print_r ($_GET, true)); } } else { opm_FNDebug_Log (""); } opm_FNDebug_Log ("REALPATH=" . $opm_realpath); opm_FNDebug_Log ("PASSWORDHASH=" . $opm_passwordhash); opm_FNDebug_Log ("OPERATION=" . $opm_operation); opm_FNDebug_Log ("FILENAME=" . $opm_filename); opm_FNDebug_Log ("SUBDIR=" . $opm_subdir); opm_FNDebug_Log ("SMARTRENAME=" . $opm_smartrename); opm_FNDebug_Log ("REQVERSION=" . $opm_reqversion); opm_FNDebug_Log ("LOGINTIMESTAMP=" . $opm_logintimestamp); // Include OSCommerce database configuration and functions. require ("includes/configure.php"); require (DIR_WS_INCLUDES . "database_tables.php"); require (DIR_WS_FUNCTIONS . "database.php"); if ($opm_browser_debug) { echo "
"; }

// Check if script version is the expected by client.
if (($opm_script_version != $opm_reqversion) && ($opm_operation)) {
  opm_FNDebug_Log ("ERROR wrong script version. Expected $opm_reqversion, but we are $opm_script_version.");
  echo "ERROR WRONG SCRIPT VERSION";
  return;
}

// Check if the image directory is valid.
if (!is_dir ($opm_realpath)) {
  opm_FNDebug_Log ("ERROR cannot find the images directory. Expecting $opm_realpath exist.");
  echo "ERROR IMAGE DIRECTORY NOT FOUND";
  return;
}

// Get OSCommerce DB password and compare it with client password.
if ($opm_enable_auth) {
  if (defined ("DB_SERVER_PASSWORD") || ($opm_password_override != "")) {
    if ($opm_password_override != "") {
      opm_FNDebug_Log ("Password override is enabled.");
      $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . $opm_password_override));
    }
    else {
      $opm_serverpassword = strtoupper (md5 ($opm_logintimestamp . DB_SERVER_PASSWORD));
    }
    opm_FNDebug_Log ("SERVERPASSWORDHASH=" . $opm_serverpassword);
    if ($opm_serverpassword != $opm_passwordhash) {
      opm_FNDebug_Log ("ERROR the server and client passwords do not match.");
      echo "ERROR PASSWORD MISMATCH";
      if ($opm_operation) { return; }
    }
  }
  else {
    $opm_operation = "";
    opm_FNDebug_Log ("ERROR the server password was not found.");
    echo "ERROR CANNOT FIND SERVER PASSWORD";
  }
}
else {
  opm_FNDebug_Log ("Password authentication is disabled.");
}


// Connect to database.
if (($opm_operation == "delete") || ($opm_operation == "capa") || ($opm_operation == "backup")) {
  if (!tep_db_connect ()) {
    echo "ERROR NO DATABASE CONNECTION";
    if ($opm_operation) { return; }
  }
}


// ==================================
// Do nothing.
// NOOP do nothing.
// ==================================
if ($opm_operation == "noop") {
  echo "OK NOOP START\n";
  opm_FNDebug_Log ("OK NOOP requested.");
  echo "OK NOOP END\n";
  return;
}
// ==================================
// CHECK if a file exists.
// CHECK requires the full path of the image.
// ==================================
if ($opm_operation == "check") {
  if ($opm_filename) {
    if (file_exists ($opm_realpath . $opm_filename)) {
      opm_FNDebug_Log ("OK file found $opm_filename.");
      echo "OK FILE EXISTS";
      return;
    }
    else {
      opm_FNDebug_Log ("ERROR file not found $opm_filename.");
      echo "ERROR FILE NOT FOUND";
      return;
    }
  }
}
// ==================================
// LIST all files recursively.
// LIST returns the filenames with paths.
// ==================================
if ($opm_operation == "list") {
  echo "OK LIST START\n";
  opm_FNList_Files ($opm_realpath, "", false);
  echo "OK LIST END\n";
  return;
}
// ==================================
// LISTDIRS only dirs recursively.
// LISTDIRS returns no filenames.
// ==================================
if ($opm_operation == "listdirs") {
  echo "OK LISTDIRS START\n";
  opm_FNList_Files ($opm_realpath, "", true);
  echo "OK LISTDIRS END\n";
  return;
}
// ==================================
// DELETE an image.
// DELETE requires the filename of the image with path.
// ==================================
if ($opm_operation == "delete") {
  if ($opm_filename) {
    $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select count(products_id) from " . TABLE_PRODUCTS . " where products_image='$opm_filename'", $opm_raw_records));
    if ($opm_sqlresult[0] < 2) {
      if (file_exists ($opm_realpath . $opm_filename)) {
        if (@unlink ($opm_realpath . $opm_filename)) {
          if (!file_exists ($opm_realpath . $opm_filename)) {
            opm_FNDebug_Log ("OK file deleted $opm_filename.");
            echo "OK DELETE";
            return;
          }
          else {
            opm_FNDebug_Log ("ERROR file cannot be deleted $opm_filename.");
            echo "ERROR DELETE FILE REMAINS";
            return;
          }
        }
        else {
          opm_FNDebug_Log ("ERROR when deleting $opm_filename.");
          echo "ERROR DELETE UNKNOWN";
          return;
        }
      }
      else {
        opm_FNDebug_Log ("OK file not found when deleting $opm_filename.");
        echo "OK DELETE FILE NOT FOUND";
        return;
      }
    }
    else {
      opm_FNDebug_Log ("OK file is used by other record (" . ($opm_sqlresult[0] - 1) . ").");
      echo "OK DELETE FILE IN USE";
      return;
    }
  }
  else {
    opm_FNDebug_Log ("OK filename empty.");
    echo "OK DELETE FILENAME EMPTY";
    return;
  }
}
// ==================================
// UPLOAD an image.
// UPLOAD requires the path, without filename, where the image will be stored.
// UPLOAD returns the image filename with the path.
// ==================================
if ($opm_operation == "upload") {
  if ($_FILES["Fl"]["name"] && $_FILES["Fl"]["size"]) {
    if (is_uploaded_file ($_FILES["Fl"]["tmp_name"])) {
      if ($opm_filename) {
        $newfn = $opm_filename;
      }
      else {
        $newfn = trim ($_FILES["Fl"]["name"]);
      }
      $newfn = str_replace (" ", "_", $newfn);
      $newfn = str_replace ("/", "", $newfn);
      $newfn = str_replace ("\\", "", $newfn);
      $newfn = ereg_replace("[^[:alnum:]\._]", "", $newfn);
      $newfn = strtolower ($newfn);

      if ($opm_subdir) {
        if ((!file_exists ($opm_realpath . $opm_subdir)) || (!is_dir ($opm_realpath . $opm_subdir))) {
          opm_FNDebug_Log ("will create directory=" . $opm_realpath . $opm_subdir);
          opm_FNmkdirs ($opm_realpath . $opm_subdir, 0777);
        }
        if (file_exists ($opm_realpath . $opm_subdir) && (is_dir ($opm_realpath . $opm_subdir))) {
          opm_FNDebug_Log ("directory exists=" . $opm_realpath . $opm_subdir);
          $newfn = $opm_subdir . "/" . $newfn;
        }
        else {
          opm_FNDebug_Log ("directory does not exist, will ignore=" . $opm_realpath . $opm_subdir);
        }

      }
      opm_FNDebug_Log ("wanted filename=" . $newfn);
      if ($opm_smartrename == 1) {
        $newfn = opm_FNSmart_Rename ($newfn, 100);
      }
      opm_FNDebug_Log ("smart filename=" . $newfn);
      opm_FNDebug_Log ("REALPATH=" . $opm_realpath);
      if (!file_exists ($opm_realpath . $newfn)) {
        opm_FNDebug_Log ("file not exist=" . $opm_realpath . $newfn);
        if (@move_uploaded_file ($_FILES["Fl"]["tmp_name"], $opm_realpath . $newfn)) {
          opm_FNDebug_Log ("moving file to=" . $opm_realpath . $newfn);
          if (file_exists ($opm_realpath . $newfn)) {
            opm_FNDebug_Log ("upload complete=" . $opm_realpath . $newfn);
            @chmod ($opm_realpath . $newfn, octdec ($opm_upload_perms));
            echo "OK UPLOAD [" . $newfn . "]";
            return;
          }
          else {
            opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
            echo "ERROR UPLOAD FILE NOT FOUND";
            return;
          }
        }
        else {
          opm_FNDebug_Log ("ERROR file not copied=" . $opm_realpath . $newfn);
          echo "ERROR UPLOAD CANNOT MOVE";
          return;
        }
      }
      else {
        opm_FNDebug_Log ("ERROR file exist=" . $opm_realpath . $newfn);
        echo "ERROR UPLOAD EXIST [" . $newfn . "]";
        return;
      }
    }
    else {
      opm_FNDebug_Log ("ERROR cannot find uploaded file!");
      echo "ERROR UPLOAD";
      return;
    }
  }
}
// ==================================
// CAPABILITY listing.
// CAPABILITY returns a list of identified modules.
// ==================================
if ($opm_operation == "capa") {
  echo "OK CAPABILITY START\n";

  // Credit Card Payment...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_CC_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "PAY_CC\n";
  }

  // Money Order Payment...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_MONEYORDER_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "PAY_ORDER\n";
  }

  // COD Payment...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COD_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "PAY_CC\n";
  }

  // PayPal Payment...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPAL_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "PAY_PP\n";
  }

  // Flat Rate Shipping...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_FLAT_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "SHIP_FLAT\n";
  }

  // Table Shipping...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "SHIP_TBL\n";
  }

  // Store PickUp Shipping...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_SPU_STATUS'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "SHIP_SPU\n";
  }

  // Multi Pickup Shipping...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'stores'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "SHIP_MPU\n";
  }

  // More Pics 6...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS . " products_subimage6", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "MOREPICS6\n";
  }

  // Total B2B...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_CUSTOMERS . " customers_groups_id", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "TOTALB2B\n";
  }

  // Poll Booth...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("show tables like 'phesis_poll_config'", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "POLLBOOTH\n";
  }

  // Header Tags Controller...
  $opm_sqlresult = explode ("\n", opm_FNExec_Query ("describe " . TABLE_PRODUCTS_DESCRIPTION . " products_head_keywords_tag", $opm_raw_records));
  if ($opm_sqlresult[0]) {
    echo "HEADERTAGS\n";
  }

  echo "OK CAPABILITY END\n";
  return;
}
// ==================================
// BACKUP database.
// BACKUP returns a SQL dump.
// ==================================
if ($opm_operation == "backup") {
  $opm_bak_filename = "osc_" . date ("Ymd_His", mktime(date("H")+$opm_timezone_shift,date("i"),date("s"),date("m"),date("d"),date("Y"))) . ".sql";
  opm_FNBackup ($opm_realpath . $opm_bak_filename);
  if (file_exists ($opm_realpath . $opm_bak_filename)) {
    opm_FNgzip_file ($opm_realpath . $opm_bak_filename, $opm_realpath . $opm_bak_filename . ".gz");
    if (file_exists ($opm_realpath . $opm_bak_filename . ".gz")) {
      header ("Content-type: application/octet-stream");
      header ("Content-disposition: attachement; filename=" . $opm_bak_filename . ".gz");
      header ("Pragma: no-store");
      readfile ($opm_realpath . $opm_bak_filename . ".gz");
      @unlink ($opm_realpath . $opm_bak_filename . ".gz");
    }
  @unlink ($opm_realpath . $opm_bak_filename);
  }
  return;
}


// ==================================
// If no command is requested, just display the version.
// ==================================
echo "

OSCommerce Product Manager, server-side script " . $opm_script_version . ".

"; if ($opm_browser_debug) { echo "

BROWSER DEBUG MODE ENABLED!

"; } if ($opm_enable_logfile) { echo "

LOGFILE ENABLED!

"; } return; // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNList_Files ($dirname, $parentdir, $onlydirs) { $dirhandle = opendir ($dirname); while (($file = readdir ($dirhandle)) != false) $sorteddir[count ($sorteddir)] = $file; closedir ($dirhandle); natcasesort ($sorteddir); if (!$onlydirs) { foreach ($sorteddir as $file) { if (($file != ".") && ($file != "..")) { if (!is_dir ($dirname . $file)) { if ($parentdir) { echo $parentdir . "/" . $file . "\n"; } else { echo $file . "\n"; } } } } foreach ($sorteddir as $file) { if (($file != ".") && ($file != "..")) { if (is_dir ($dirname . $file)) { if ($parentdir) { opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs); } else { opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs); } } } } } else { foreach ($sorteddir as $file) { if (($file != ".") && ($file != "..")) { if (is_dir ($dirname . $file)) { if ($parentdir) { echo $parentdir . "/" . $file . "\n"; opm_FNList_Files ($dirname . $file . "/", $parentdir . "/" . $file, $onlydirs); } else { echo $file . "\n"; opm_FNList_Files ($dirname . $file . "/", $file, $onlydirs); } } } } } } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNSmart_Rename ($wantedname, $tries) { global $opm_realpath; $newname = $wantedname; $newext = '.' . array_pop (explode ('.', $wantedname)); for ($trycounter = 1; $trycounter <= $tries; $trycounter++) { if (!file_exists ($opm_realpath . $newname)) { return ($newname); } else { $newdirname = dirname ($wantedname); if ($newdirname != ".") { $newname = dirname ($wantedname) . "/" . basename ($wantedname, $newext) . "_" . $trycounter . $newext; } else { $newname = basename ($wantedname, $newext) . "_" . $trycounter . $newext; } } } return ($wantedname); } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNDebug_Log ($logline) { global $opm_enable_logfile, $opm_debug_file, $opm_timezone_shift; if ($opm_enable_logfile) { if (!(file_exists ($opm_debug_file) && !is_writable ($opm_debug_file))) { $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"))); $opm_lfilef = @fopen ($opm_debug_file, 'a'); if ($opm_lfilef) { if ($logline) { @fwrite ($opm_lfilef, $opm_timestamp . ": " . $logline . "\n"); } else { @fwrite ($opm_lfilef, "\n"); } @fclose ($opm_lfilef); } } } } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Based on code posted on php.net by saint@corenova.com and bart@cdasites.com // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNmkdirs ($dirname) { if (is_dir ($dirname) || empty ($dirname)) return 1; if (file_exists ($dirname) && !is_dir ($dirname)) return 0; if (opm_FNmkdirs (substr ($dirname, 0, strrpos ($dirname, '/')))) { if (!file_exists($dirname)) { return @mkdir ($dirname); } } return 0; } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNExec_Query ($sql_query, $rawdata) { global $opm_tag_recordbegin, $opm_tag_recordend, $opm_tag_field, $opm_tag_databegin, $opm_tag_dataend, $opm_encoded_data; $result_string = ""; opm_FNDebug_Log ("SQLQUERY=" . $sql_query); $sql_result = tep_db_query ($sql_query); while ($sql_record = tep_db_fetch_array ($sql_result)) { $sql_fullrec = ""; if ($rawdata) { foreach ($sql_record as $key => $value) { $result_string .= $value . "\n"; } } else { $result_string .= "$opm_tag_recordbegin\n"; foreach ($sql_record as $key => $value) { if ($opm_encoded_data) { $encodedfield = base64_encode ($value); } else { $encodedfield = htmlentities ($value); } $result_string .= $opm_tag_field . $opm_tag_databegin . $encodedfield . $opm_tag_dataend . "\n"; $sql_fullrec .= $encodedfield; } $result_string .= "$opm_tag_recordend " . abs (crc32 ($sql_fullrec)) . "\n"; } } return $result_string; } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Based on code from osCommerce (/admin/backup.php). // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNBackup ($dump_file) { global $opm_script_version; if ($dump_file) { $bakfilef = fopen ($dump_file, 'w'); $bakline = "# OSCPMWin (server-side $opm_script_version)\n"; $bakline .= "# Database: " . DB_DATABASE . "@" . DB_SERVER . "\n"; $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"; fwrite ($bakfilef, $bakline); $tables_query = tep_db_query ("show tables"); while ($tables = tep_db_fetch_array ($tables_query)) { list (, $table) = each ($tables); $bakline = "drop table if exists $table;\ncreate table $table (\n"; $table_list = array (); $fields_query = tep_db_query ("show fields from $table"); while ($fields = tep_db_fetch_array($fields_query)) { $table_list[] = $fields["Field"]; $bakline .= " " . $fields["Field"] . " " . $fields["Type"]; if (strlen ($fields["Default"]) > 0) $bakline .= " default '" . $fields["Default"] . "'"; if (strtoupper ($fields["Null"]) != "YES") $bakline .= " not null"; if (isset ($fields["Extra"])) $bakline .= " " . $fields["Extra"]; $bakline .= ",\n"; } $bakline = ereg_replace(",\n$", '', $bakline); $index = array (); $keys_query = tep_db_query ("show keys from $table"); while ($keys = tep_db_fetch_array ($keys_query)) { $kname = $keys["Key_name"]; if (!isset ($index[$kname])) { $index[$kname] = array ("unique" => !$keys["Non_unique"], "columns" => array()); } $index[$kname]["columns"][] = $keys["Column_name"]; } while (list($kname, $info) = each($index)) { $bakline .= ",\n"; $columns = implode($info["columns"], ", "); if ($kname == "PRIMARY") { $bakline .= " PRIMARY KEY ($columns)"; } elseif ($info["unique"]) { $bakline .= " UNIQUE $kname ($columns)"; } else { $bakline .= " KEY $kname ($columns)"; } } $bakline .= "\n);\n\n"; fwrite ($bakfilef, $bakline); $rows_query = tep_db_query ("select " . implode (",", $table_list) . " from $table"); while ($rows = tep_db_fetch_array ($rows_query)) { $bakline = "insert into $table (" . implode (", ", $table_list) . ") values ("; reset ($table_list); while (list (, $i) = each ($table_list)) { if (!isset ($rows[$i])) { $bakline .= "NULL, "; } elseif (($rows[$i] != "") && (strtoupper ($rows[$i]) != "NULL")) { $row = addslashes ($rows[$i]); $row = ereg_replace("\n#", "\n".'\#', $row); $bakline .= "'$row', "; } else { $bakline .= "'', "; } } $bakline = ereg_replace (", $", "", $bakline) . ");\n"; fwrite ($bakfilef, $bakline); } } fclose ($bakfilef); } } // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Compress a file with the gzip algorithm. // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function opm_FNgzip_file ($opm_gzsource, $opm_gztarget) { if (function_exists ("gzwrite") && file_exists ($opm_gzsource)) { $opm_ungzfp = fopen($opm_gzsource, "rb"); $opm_gzfp = gzopen($opm_gztarget, "wb9"); if ($opm_gzfp && $opm_ungzfp) { while (!feof ($opm_ungzfp)) { gzwrite ($opm_gzfp, fread ($opm_ungzfp, 65535)); } @fclose ($opm_ungzfp); @gzclose ($opm_gzfp); return (true); } else { @fclose ($opm_ungzfp); @gzclose ($opm_gzfp); return (false); } } else { return (false); } } ?>