/** * return the list of directory * * @param string $folder directory path * * @return array * @throws CMbException */ private function _getListDirectory($folder = ".") { if (!$this->connexion) { throw new CMbException("CSourceSFTP-connexion-failed", $this->hostname); } /** * group by directory * size - uid -gid - permissions - atime - mtime - type */ if (!($files = $this->connexion->rawlist($folder))) { throw new CMbException("CSourceSFTP-getlistfiles-failed", $this->hostname); } CMbArray::extract($files, "."); CMbArray::extract($files, ".."); $list = array(); foreach ($files as $key => $_file) { if ($_file["type"] !== 2) { continue; } $list[$key] = $_file; } return $list; }
public static function send($settings = array(), $files = array(), $send_id = '') { global $pb_backupbuddy_destination_errors; if ('1' == $settings['disabled']) { $pb_backupbuddy_destination_errors[] = __('Error #48933: This destination is currently disabled. Enable it under this destination\'s Advanced Settings.', 'it-l10n-backupbuddy'); return false; } if (!is_array($files)) { $files = array($files); } pb_backupbuddy::status('details', 'FTP class send() function started.'); self::_init(); // Connect to server. $server = $settings['address']; $port = '22'; // Default sFTP port. if (strstr($server, ':')) { // Handle custom sFTP port. $server_params = explode(':', $server); $server = $server_params[0]; $port = $server_params[1]; } pb_backupbuddy::status('details', 'Connecting to sFTP server...'); $sftp = new Net_SFTP($server, $port); if (!$sftp->login($settings['username'], $settings['password'])) { pb_backupbuddy::status('error', 'Connection to sFTP server FAILED.'); pb_backupbuddy::status('details', 'sFTP log (if available & enabled via full logging mode): `' . $sftp->getSFTPLog() . '`.'); return false; } else { pb_backupbuddy::status('details', 'Success connecting to sFTP server.'); } pb_backupbuddy::status('details', 'Attempting to create path (if it does not exist)...'); if (true === $sftp->mkdir($settings['path'])) { // Try to make directory. pb_backupbuddy::status('details', 'Directory created.'); } else { pb_backupbuddy::status('details', 'Directory not created.'); } // Change to directory. pb_backupbuddy::status('details', 'Attempting to change into directory...'); if (true === $sftp->chdir($settings['path'])) { pb_backupbuddy::status('details', 'Changed into directory `' . $settings['path'] . '`. All uploads will be relative to this.'); } else { pb_backupbuddy::status('error', 'Unable to change into specified path. Verify the path is correct with valid permissions.'); pb_backupbuddy::status('details', 'sFTP log (if available & enabled via full logging mode): `' . $sftp->getSFTPLog() . '`.'); return false; } // Upload files. $total_transfer_size = 0; $total_transfer_time = 0; foreach ($files as $file) { if (!file_exists($file)) { pb_backupbuddy::status('error', 'Error #859485495. Could not upload local file `' . $file . '` to send to sFTP as it does not exist. Verify the file exists, permissions of file, parent directory, and that ownership is correct. You may need suphp installed on the server.'); } if (!is_readable($file)) { pb_backupbuddy::status('error', 'Error #8594846548. Could not read local file `' . $file . '` to send to sFTP as it is not readable. Verify permissions of file, parent directory, and that ownership is correct. You may need suphp installed on the server.'); } $filesize = filesize($file); $total_transfer_size += $filesize; $destination_file = basename($file); pb_backupbuddy::status('details', 'About to put to sFTP local file `' . $file . '` of size `' . pb_backupbuddy::$format->file_size($filesize) . '` to remote file `' . $destination_file . '`.'); $send_time = -microtime(true); $upload = $sftp->put($destination_file, $file, NET_SFTP_LOCAL_FILE); $send_time += microtime(true); $total_transfer_time += $send_time; if ($upload === false) { // Failed sending. $error_message = 'ERROR #9012b ( http://ithemes.com/codex/page/BackupBuddy:_Error_Codes#9012 ). sFTP file upload failed. Check file permissions & disk quota.'; pb_backupbuddy::status('error', $error_message); backupbuddy_core::mail_error($error_message); pb_backupbuddy::status('details', 'sFTP log (if available & enabled via full logging mode): `' . $sftp->getSFTPLog() . '`.'); return false; } else { // Success sending. pb_backupbuddy::status('details', 'Success completely sending `' . basename($file) . '` to destination.'); // Start remote backup limit if ($settings['archive_limit'] > 0) { pb_backupbuddy::status('details', 'Archive limit enabled. Getting contents of backup directory.'); $contents = $sftp->rawlist($settings['path']); // already in destination directory/path. // Create array of backups $bkupprefix = backupbuddy_core::backup_prefix(); $backups = array(); foreach ($contents as $filename => $backup) { // check if file is backup $pos = strpos($filename, 'backup-' . $bkupprefix . '-'); if ($pos !== FALSE) { $backups[] = array('file' => $filename, 'modified' => $backup['mtime']); } } function backupbuddy_number_sort($a, $b) { return $a['modified'] < $b['modified']; } // Sort by modified using custom sort function above. usort($backups, 'backupbuddy_number_sort'); if (count($backups) > $settings['archive_limit']) { pb_backupbuddy::status('details', 'More backups found (' . count($backups) . ') than limit permits (' . $settings['archive_limit'] . ').' . print_r($backups, true)); $delete_fail_count = 0; $i = 0; foreach ($backups as $backup) { $i++; if ($i > $settings['archive_limit']) { if (false === $sftp->delete($settings['path'] . '/' . $backup['file'])) { pb_backupbuddy::status('details', 'Unable to delete excess sFTP file `' . $backup['file'] . '` in path `' . $settings['path'] . '`.'); $delete_fail_count++; } else { pb_backupbuddy::status('details', 'Deleted excess sFTP file `' . $backup['file'] . '` in path `' . $settings['path'] . '`.'); } } } if ($delete_fail_count != 0) { backupbuddy_core::mail_error(sprintf(__('sFTP remote limit could not delete %s backups. Please check and verify file permissions.', 'it-l10n-backupbuddy'), $delete_fail_count)); pb_backupbuddy::status('error', 'Unable to delete one or more excess backup archives. File storage limit may be exceeded. Manually clean up backups and check permissions.'); } else { pb_backupbuddy::status('details', 'No problems encountered deleting excess backups.'); } } else { pb_backupbuddy::status('details', 'Not enough backups found to exceed limit. Skipping limit enforcement.'); } } else { pb_backupbuddy::status('details', 'No sFTP archive file limit to enforce.'); } // End remote backup limit } } // end $files loop. // Load destination fileoptions. pb_backupbuddy::status('details', 'About to load fileoptions data.'); require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php'; pb_backupbuddy::status('details', 'Fileoptions instance #6.'); $fileoptions_obj = new pb_backupbuddy_fileoptions(backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false); if (true !== ($result = $fileoptions_obj->is_ok())) { pb_backupbuddy::status('error', __('Fatal Error #9034.843498. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $result); return false; } pb_backupbuddy::status('details', 'Fileoptions data loaded.'); $fileoptions =& $fileoptions_obj->options; // Save stats. $fileoptions['write_speed'] = $total_transfer_size / $total_transfer_time; $fileoptions_obj->save(); unset($fileoptions_obj); return true; }
public function rawls() { $list = $this->_connection->rawlist(); return $list; }
$sftp = new Net_SFTP($server, $port); if (!$sftp->login($destination['username'], $destination['password'])) { pb_backupbuddy::status('error', 'Connection to sFTP server FAILED.'); return false; } else { pb_backupbuddy::status('details', 'Success connecting to sFTP server.'); } pb_backupbuddy::status('details', 'Attempting to create path (if it does not exist)...'); if (true === $sftp->mkdir($destination['path'])) { // Try to make directory. pb_backupbuddy::status('details', 'Directory created.'); } else { pb_backupbuddy::status('details', 'Directory not created.'); } // List files. $files = $sftp->rawlist($destination['path']); $backups = array(); $backup_list_temp = array(); foreach ($files as $filename => $file) { if (false === stristr($filename, 'backup')) { // only show backup files continue; } if (stristr($filename, '-db-') !== false) { $backup_type = 'Database'; } elseif (stristr($filename, '-full-') !== false) { $backup_type = 'Full'; } else { $backup_type = 'Unknown'; } $last_modified = $file['mtime'];
public function file_list($srvid, $dir, $tpl_browse = false) { if (empty($srvid)) { return 'No server ID given'; } // Get network ID (if browsing for template, use given ID as it's already a network ID if ($tpl_browse) { $this_netid = $srvid; } else { $result_nid = @mysql_query("SELECT netid FROM servers WHERE id = '{$srvid}' LIMIT 1"); $row_nid = mysql_fetch_row($result_nid); $this_netid = $row_nid[0]; } if (empty($this_netid)) { return 'Failed to get network ID!'; } require DOCROOT . '/includes/classes/network.php'; $Network = new Network(); $netinfo = $Network->netinfo($this_netid); $net_local = $netinfo['is_local']; #$net_game_ip = $netinfo['game_ip']; $ssh_ip = $netinfo['ssh_ip']; $ssh_port = $netinfo['ssh_port']; $ssh_user = $netinfo['ssh_user']; $ssh_pass = $netinfo['ssh_pass']; $ssh_homedir = $netinfo['ssh_homedir']; $net_local = $netinfo['is_local']; // Get real server info if (!class_exists('Servers')) { require DOCROOT . '/includes/classes/servers.php'; } $Servers = new Servers(); $srvinfo = $Servers->getinfo($srvid); $net_game_ip = $srvinfo[0]['ip']; $net_gameuser = $srvinfo[0]['username']; $net_game_port = $srvinfo[0]['port']; // Cant read any files with . in them (includes .filename and ../) if (preg_match('/^\\./', $dir)) { $dir = stripslashes($dir); $dir = strip_tags($dir); return 'Invalid directory provided (' . $dir . ')!'; } ################################################################ // Local Listing if ($net_local) { $local_dir = DOCROOT . '/_SERVERS/'; // Browsing for templates. Start at homedir if ($tpl_browse) { $game_dir = $local_dir; } else { $game_dir = $local_dir . '/accounts/' . $net_gameuser . '/' . $net_game_ip . '.' . $net_game_port; } // Append new directory to basedir if ($dir) { $game_dir .= '/' . $dir; } ########################################################################################## // File Contents if (preg_match('/\\.(txt|cfg|rc|log|ini|inf|vdf|yml|properties|json|conf)$/i', $dir)) { if (!file_exists($game_dir)) { return 'That file doesnt exist or the webserver cannot view it.'; } // Read file $fh = fopen($game_dir, "rb"); $file_content = fread($fh, filesize($game_dir)); fclose($fh); // Return remote file content return $file_content; } ########################################################################################## // Continue with directory listing if (GPXDEBUG) { $add_path = '(' . $game_dir . ') '; } else { $add_path = ''; } if (!is_dir($game_dir)) { die('Sorry, that game directory ' . $add_path . 'does not exist!'); } $dir = dir($game_dir); $dir_arr = array(); $count_dir = 0; // Permission errors etc if (!opendir($game_dir)) { return 'err_opendir'; } // Loop over files, no . or .. dirs while (($file = $dir->read()) !== false) { if (!preg_match('/^\\.+/', $file)) { $full_path = $game_dir . '/' . $file; if (is_dir($full_path)) { $thetype = '2'; } else { $thetype = '1'; } #$dir_arr[$count_dir]['name'] = $file; #$dir_arr[$count_dir]['size'] = filesize($full_path); #$dir_arr[$count_dir]['mtime'] = filemtime($full_path); #$dir_arr[$count_dir]['atime'] = fileatime($full_path); #$dir_arr[$count_dir]['perms'] = fileperms($full_path); #$dir_arr[$count_dir]['type'] = $thetype; $dir_arr[$file]['size'] = filesize($full_path); $dir_arr[$file]['mtime'] = filemtime($full_path); $dir_arr[$file]['atime'] = fileatime($full_path); $dir_arr[$file]['uid'] = fileowner($full_path); $dir_arr[$file]['gid'] = filegroup($full_path); $dir_arr[$file]['permissions'] = fileperms($full_path); $dir_arr[$file]['type'] = $thetype; } $count_dir++; } $dir->close(); return $dir_arr; } else { // Not browsing for templates if (!$tpl_browse) { // Use real SSO user's login, not gpx login $sso_info = $Network->sso_info($srvid); $ssh_user = $sso_info['sso_user']; $ssh_pass = $sso_info['sso_pass']; $sso_username = $sso_info['username']; $sso_gamedir = $sso_info['game_path']; // Set game dir $game_dir = $sso_gamedir; if (GPXDEBUG) { echo "DEBUG: Gamedir: {$sso_gamedir}, SSO User: {$ssh_user}, Username: {$sso_username}<br>"; } // File Contents if (preg_match('/\\.(txt|cfg|rc|log|ini|inf|vdf|yml|properties|json|conf)$/i', $dir)) { if ($tpl_browse) { return 'Sorry, you cannot edit files while browsing for templates!'; } $net_cmd = 'FileContent -f ' . $game_dir . '/' . $dir; $file_content = $Network->runcmd($this_netid, $netinfo, $net_cmd, true, $srvid); // Return remote file content return $file_content; } } else { // Use normal SSH homedir $game_dir = $ssh_homedir; } ########################################################################################## // PHPSecLib (Pure-PHP SSH Implementation) require DOCROOT . '/includes/SSH/Net/SFTP.php'; // Setup Connection $sftp = new Net_SFTP($ssh_ip, $ssh_port, 12); // Test login if (!$sftp->login($ssh_user, $ssh_pass)) { return 'ERROR: Failed to login to the remote server'; } // Append new directory to basedir if ($dir) { $game_dir .= '/' . $dir; } // Get raw file list $file_list = $sftp->rawlist($game_dir); return $file_list; } }