/**
	 * Returns an object that is a child of the FTPClient class or null on failure.
	 */
	public static function initializeFTP() {
		if (self::$ftp === null && Config::get('base.ftp.enabled') == true) {
			self::$ftp = FTPClient::getObject();
			$data = Config::get('base.ftp');
			if (self::$ftp !== null && self::$ftp->setServer($data['host'], $data['port']) !== true) {
				self::$debug->addText('Could not set ftp server to ...');
				self::$ftp = null;
			}
			if (self::$ftp !== null && self::$ftp->connect() !== true) {
				self::$debug->addText('Could not connect to ftp server');
				self::$ftp->quit();
				self::$ftp = null;
			}
			if (self::$ftp !== null && self::$ftp->login($data['username'], $data['password']) !== true) {
				self::$debug->addText('Could not login on ftp server with user data ...');
				self::$ftp->quit();
				self::$ftp = null;
			}
			if (self::$ftp !== null && self::$ftp->chdir($data['directory']) !== true) {
				self::$debug->addText('Could not change directory on ftp server to ...');
				self::$ftp->quit();
				self::$ftp = null;
			}
		}
		return self::$ftp;
	}
	public function __construct($verb = false, $le = false) {
		parent::__construct(true, $verb, $le);
	}
<?php

set_time_limit(0);
$debug = true;
// Login information
include 'ftp_login.php';
// Include the FTP class
include 'ftp_class.php';
// Create the FTP object
$ftpObj = new FTPClient();
// Connect
if ($ftpObj->connect(FTP_HOST, FTP_USER, FTP_PASS)) {
    $File = "logfile_" . date('Ymd') . ".txt";
    $handle = fopen($File, 'a');
    //fwrite($Handle, $Data);
    $starttime = "\n===============start time of script " . date("l F d, Y, h:i A") . "===================";
    $ftpObj->writeLog($handle, $starttime);
    //$today = date('Ymd',strtotime("200 minutes")); // date format for folder YYYYMMDD i.e. 20110617
    $today = date('Ymd');
    $yesterday = date('Ymd');
    // previous date YYYYMMDD i.e. 20110617
    $h = date('H');
    //$h = date('H',strtotime("270 minutes"));
    if ($h == "00" || $h == "01" || $h == "02" || $h == "03" || $h == "04" || $h == "05" || $h == "06" || $h == "07") {
        $hourwisedir = "08";
        $dir = $yesterday . "/" . $hourwisedir;
    }
    if ($h == "08" || $h == "09" || $h == "10" || $h == "11" || $h == "12" || $h == "13" || $h == "14" || $h == "15") {
        $hourwisedir = "16";
        $dir = $today . "/" . $hourwisedir;
    }
 /**
  * Installation, upgrade and initialization functions
  */
 function update()
 {
     global $Shopp, $wp_version;
     $db = DB::get();
     $log = array();
     if (!isset($_POST['update'])) {
         die("Update Failed: Update request is invalid.  No update specified.");
     }
     if (!isset($_POST['type'])) {
         die("Update Failed: Update request is invalid. Update type not specified");
     }
     if (!isset($_POST['password'])) {
         die("Update Failed: Update request is invalid. No FTP password provided.");
     }
     $updatekey = $this->Settings->get('updatekey');
     $credentials = $this->Settings->get('ftp_credentials');
     if (empty($credentials)) {
         // Try to load from WordPress settings
         $credentials = get_option('ftp_credentials');
         if (!$credentials) {
             $credentials = array();
         }
     }
     // Make sure we can connect to FTP
     $ftp = new FTPClient($credentials['hostname'], $credentials['username'], $_POST['password']);
     if (!$ftp->connected) {
         die("ftp-failed");
     } else {
         $log[] = "Connected with FTP successfully.";
     }
     // Get zip functions from WP Admin
     if (class_exists('PclZip')) {
         $log[] = "ZIP library available.";
     } else {
         @(require_once ABSPATH . 'wp-admin/includes/class-pclzip.php');
         $log[] = "ZIP library loaded.";
     }
     // Put site in maintenance mode
     if ($this->Settings->get('maintenance') != "on") {
         $this->Settings->save("maintenance", "on");
         $log[] = "Enabled maintenance mode.";
     }
     // Find our temporary filesystem workspace
     $tmpdir = defined('SHOPP_TEMP_PATH') ? SHOPP_TEMP_PATH : sys_get_temp_dir();
     $tmpdir = str_replace('\\', '/', $tmpdir);
     //Windows path sanitiation
     $log[] = "Found temp directory: {$tmpdir}";
     // Download the new version of Shopp
     $updatefile = tempnam($tmpdir, "shopp_update_");
     if (($download = fopen($updatefile, 'wb')) === false) {
         $log[] = "A temporary file could not be created under {$tmpdir}, trying WordPress upload directory instead.";
         $tmpdir = trailingslashit(WP_CONTENT_DIR . "/uploads");
         $updatefile = tempnam($tmpdir, "shopp_update_");
         $log[] = "Found temp directory: {$tmpdir}";
         if (($download = fopen($updatefile, 'wb')) === false) {
             die(join("\n\n", $log) . "\n\nUpdate Failed: Cannot save the Shopp update to the temporary workspace because of a write permission error.");
         }
     }
     $query = build_query_request(array("ShoppServerRequest" => "download-update", "ver" => "1.0"));
     $data = build_query_request(array("key" => $updatekey['key'], "core" => SHOPP_VERSION, "wp" => $wp_version, "site" => get_bloginfo('siteurl'), "update" => $_POST['update']));
     $connection = curl_init();
     curl_setopt($connection, CURLOPT_URL, SHOPP_HOME . "?" . $query);
     curl_setopt($connection, CURLOPT_USERAGENT, SHOPP_GATEWAY_USERAGENT);
     curl_setopt($connection, CURLOPT_HEADER, 0);
     curl_setopt($connection, CURLOPT_POST, 1);
     curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
     curl_setopt($connection, CURLOPT_TIMEOUT, 20);
     curl_setopt($connection, CURLOPT_FILE, $download);
     curl_exec($connection);
     curl_close($connection);
     fclose($download);
     $downloadsize = filesize($updatefile);
     // Report error message returned by the server request
     if (filesize($updatefile) < 256) {
         die(join("\n\n", $log) . "\nUpdate Failed: " . file_get_contents($updatefile));
     }
     // Nothing downloaded... couldn't reach the server?
     if (filesize($updatefile) == 0) {
         die(join("\n\n", $log) . "\n\\Update Failed: The download did not complete succesfully.");
     }
     // Download successful, log the size
     $log[] = "Downloaded update of " . number_format($downloadsize) . " bytes";
     // Extract data
     $log[] = "Unpacking updates...";
     $archive = new PclZip($updatefile);
     $files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING);
     if (!is_array($files)) {
         die(join("\n\n", $log) . "\n\nUpdate Failed: The downloaded update did not complete or is corrupted and cannot be used.");
     } else {
         unlink($updatefile);
     }
     $target = trailingslashit($tmpdir);
     // Move old updates that still exist in $tmpdir to a new location
     if (file_exists($target . $files[0]['filename']) && is_dir($target . $files[0]['filename'])) {
         rename($target . $files[0]['filename'], $updatefile . '_old_update');
     }
     // Create file structure in working path target
     foreach ($files as $file) {
         if (!$file['folder']) {
             if (file_put_contents($target . $file['filename'], $file['content'])) {
                 @chmod($target . $file['filename'], 0644);
             }
         } else {
             if (!is_dir($target . $file['filename'])) {
                 if (!@mkdir($target . $file['filename'], 0755, true)) {
                     die(join("\n\n", $log) . "\n\nUpdate Failed: Couldn't create directory {$target}{$file['filename']}");
                 }
             }
         }
     }
     $log[] = "Successfully unpacked the update.";
     // FTP files to make it "easier" than dealing with permissions
     $log[] = "Updating files via FTP connection";
     switch ($_POST['type']) {
         case "core":
             $results = $ftp->update($target . $files[0]['filename'], $Shopp->path);
             if (!empty($results)) {
                 die(join("\n\n", $log) . join("\n\n", $results) . "\n\nFTP transfer failed.");
             }
             break;
         case "Payment Gateway":
             $results = $ftp->update($target . $files[0]['filename'], $Shopp->path . DIRECTORY_SEPARATOR . "gateways" . DIRECTORY_SEPARATOR . $files[0]['filename']);
             if (!empty($results)) {
                 die(join("\n\n", $log) . join("\n\n", $results) . "\n\nFTP transfer failed.");
             }
             break;
         case "Shipping Module":
             $results = $ftp->update($target . $files[0]['filename'], $Shopp->path . DIRECTORY_SEPARATOR . "shipping" . DIRECTORY_SEPARATOR . $files[0]['filename']);
             if (!empty($results)) {
                 die(join("\n\n", $log) . join("\n\n", $results) . "\n\nFTP transfer failed.");
             }
             break;
     }
     echo "updated";
     // Report success!
     exit;
 }
Example #5
0
 protected function authenticateUser($username, $password)
 {
     $url = $_REQUEST['ftp_url'];
     if (!preg_match('|[a-zA-Z-_]{1,}\\.\\w{1,}|', $url)) {
         throw new Exception("Invalid URL address `{$url}'.");
     }
     $ftp = new FTPClient($url, $username, $password);
     if (!$ftp->authenticate()) {
         throw new Exception("Could not authenticate to FTP server at `{$url}'.");
     }
     $ticket = $this->generateRandomTicket();
     $this->storeSessionData($ticket, array($url, $username, $password));
     return $ticket;
 }