PHP Agent (HTTP) - Complete Guide

PHP Agent enables file operations over HTTP when SSH access is unavailable. Ideal for shared hosting environments.

Overview

What is PHP Agent?

Capabilities

Deployment Guide

Step 1: Configure Plugin

  1. Open connection dialog
  2. Select transfer mode: PHP Agent (HTTP)
  3. Enter URL: https://example.com/sftp.php
  4. Enter username (optional, for identification)
  5. Enter password (will be used as PSK - Pre-Shared Key)
  6. Save session

Step 2: Generate sftp.php

  1. After saving password, plugin generates sftp.php
  2. Located in plugin runtime directory (next to plugin DLL): ...\\plugins\\wfx\\sftpplug\\sftp.php (build also copies it to build\\bin\\sftp.php)
  3. Contains derived authentication constants
  4. Ready for upload to server

Step 3: Upload to Server

  1. Use FTP, control panel, or other method
  2. Upload sftp.php to web-accessible location
  3. Note the full URL for plugin configuration
  4. Ensure file permissions allow execution (755 typical)

Step 4: Test Connection

  1. Enter URL in plugin: https://example.com/sftp.php
  2. Click Connect
  3. Plugin probes endpoint for capabilities
  4. On success, ready for file operations

Authentication

PSK (Pre-Shared Key)

Three configuration methods (in order of preference):

  1. Environment Variable (Recommended):
    AGENT_PSK=your_secret_key_here
    Environment keys checked: TC_SFTP_AGENT_PSK, SFTP_AGENT_PSK, AGENT_PSK
  2. Constant in sftp.php:
    const AGENT_PSK = 'your_secret_key_here';
  3. Salted Hash (for deployment):
    const AGENT_PSK_SALT = 'random_salt';
    const AGENT_PSK_SHA256 = 'sha256(salt:psk)';
    Allows upload-only deployment without knowing plain PSK

Signed Requests (TCSFTP-HMAC-V2)

The password never travels. Every request except PROBE carries:

X-SFTP-TS:             Unix time of the request
X-SFTP-NONCE:          random, 8-128 characters [A-Za-z0-9._-], never reused
X-SFTP-CONTENT-SHA256: hex SHA-256 of the request body (of "" when there is none)
X-SFTP-SIGNATURE-V2:   hex HMAC-SHA256(key, "TCSFTP-HMAC-V2" \n METHOD \n OPERATION \n
                       canonical query \n content SHA-256 \n timestamp \n nonce)

The PSK in the clear (X-SFTP-AUTH) and the partial signature of plugins before 10.3.1 are refused unless const AGENT_ACCEPT_LEGACY_AUTH = true; is set in sftp.php — only while such a plugin still has to connect. A plugin from 10.3.1 on refuses an agent that does not sign and says so; it never falls back to sending the password.

The signing key is in sftp.php as the hash: keep the file readable by the web server only. Signing protects the password and every request on plain HTTP too, but not the files themselves — use HTTPS so that transfers stay private.

Security Features

The shell is not confined. SHELL_EXEC runs any command as the web server user, outside AGENT_ROOT too. Whoever holds the password has that access. Set const AGENT_ALLOW_SHELL = false; in sftp.php when the shell is not needed.

URL Format

Basic URL

https://example.com/sftp.php
https://example.com/path/to/sftp.php
https://example.com/sftp.php?path=/subdir

With Port

https://example.com:8443/sftp.php

Path Parameter

Can specify path in URL or via parameter:

https://example.com/sftp.php?path=files
https://example.com/sftp.php?path=%2Fvar%2Fwww

Operations Reference

PROBE - Capability Detection

Automatic on first connection:

GET https://example.com/sftp.php?op=PROBE

Response to an unsigned PROBE:
{
  "ok": true,
  "agent": { "name": "TC PHP Agent", "version": "1.1.0" },
  "auth": { "scheme": "TCSFTP-HMAC-V2", "salt": "3f9c..." },
  "capabilities": { "recommended_chunk_size": 1048576 }
}

Response to a signed PROBE:
{
  "ok": true,
  "agent": { "name": "TC PHP Agent", "version": "1.1.0" },
  "capabilities": { "recommended_chunk_size": 1048576, "operations": [...], ... },
  "runtime": { "php_version": "8.2", "sapi": "apache2handler" },
  "ini": { "upload_max_filesize": "256M", ... }
}

Upload Chunk Policy

LIST - Directory Listing

GET https://example.com/sftp.php?op=LIST&path=/subdir

Response (JSON):
{
  "ok": true,
  "path": "subdir",
  "items": [
    { "name": "file.txt", "is_dir": false, "size": 1234, "mtime": 1234567890, "perms": "0644" }
  ]
}

Response (Plain):
F\t1234\t1234567890\tbase64(filename)
D\t0\t1234567890\tbase64(dirname)

GET - Download File

GET https://example.com/sftp.php?op=GET&path=file.txt&offset=0&length=1024

Headers:
Content-Type: application/octet-stream
Content-Length: 1024
X-SFTP-File-Size: 5000
X-SFTP-Offset: 0
Accept-Ranges: bytes

PUT - Upload File

POST https://example.com/sftp.php?op=PUT&path=file.txt&offset=0

Headers:
Content-Type: multipart/form-data or raw POST data
X-SFTP-Content-SHA256: sha256hash (optional verification)

Response:
{
  "ok": true,
  "path": "file.txt",
  "written": 1024,
  "size": 5000,
  "sha256": "abc123...",
  "part": true
}

FINALIZE - Complete Upload

POST https://example.com/sftp.php?op=FINALIZE&path=file.txt&overwrite=1

Response:
{
  "ok": true,
  "path": "file.txt",
  "size": 5000,
  "finalized": true
}

DELETE - Remove File

POST https://example.com/sftp.php?op=DELETE&path=file.txt

Response:
{ "ok": true, "deleted": true, "path": "file.txt" }

MKDIR - Create Directory

POST https://example.com/sftp.php?op=MKDIR&path=newdir&recursive=1&mode=0775

Response:
{ "ok": true, "created": true, "path": "newdir" }

mode is three octal digits, optionally with a leading zero; anything else is refused with INVALID_MODE.

RMDIR - Remove Directory

POST https://example.com/sftp.php?op=RMDIR&path=olddir&recursive=0

Response:
{ "ok": true, "removed": true, "path": "olddir", "recursive": false }

The agent root is refused with ROOT_PROTECTED. A link to a directory is removed as a link; the directory it points to stays. The same holds for DELETE and RENAME: they act on the link, not on its target.

RENAME - Move/Rename

POST https://example.com/sftp.php?op=RENAME&from=old.txt&to=new.txt&overwrite=1

Response:
{ "ok": true, "renamed": true, "from": "old.txt", "to": "new.txt" }

HASH - Compute File Hash

GET https://example.com/sftp.php?op=HASH&path=file.txt&algo=sha256

Response:
{ "ok": true, "path": "file.txt", "algo": "sha256", "hash": "abc123..." }

TAR_STREAM - Stream Entire Directory as TAR

GET https://example.com/sftp.php?op=TAR_STREAM&path=/subdir

Response: raw ustar TAR stream (application/x-tar)

The server streams the entire directory tree as a ustar TAR archive. The plugin parses the stream on the fly: directories are created immediately, files are written as their data arrives. GNU long-name extension records (././@LongLink, typeflag L) are handled for paths longer than 99 characters. The stream ends with two consecutive 512-byte zero blocks (standard TAR end-of-archive marker). Used when downloading an entire directory.

Enabled when the TAR stream checkbox is checked and a single directory is being downloaded.

TAR_PACK - Batch Download Selected Files as TAR

POST https://example.com/sftp.php?op=TAR_PACK
Content-Type: text/plain

relative/path/file1.txt
relative/path/file2.bin
subdir/file3.jpg

Response: raw ustar TAR stream (application/x-tar)

The plugin sends a list of relative remote paths (one per line, newline-separated) in the POST body. The server packs exactly those paths into a ustar TAR and streams the result directly to the response — no temporary file, no memory buffer, no Content-Length header. The plugin parses the TAR on the fly and writes each file to the local destination as it arrives.

Directories listed are included as directory entries (typeflag 5). Files larger than 8 589 934 591 bytes are skipped (POSIX ustar octal limit) without corrupting the rest of the archive. GNU LongLink is used for paths over 99 characters.

Enabled when the TAR stream checkbox is checked and multiple files are selected for download (TC GET_MULTI batch operation).

TAR_EXTRACT - Upload Files as TAR

POST https://example.com/sftp.php?op=TAR_EXTRACT&path=/subdir
Content-Type: application/x-tar
Content-Length: <exact 64-bit size>
[ustar TAR body]

Response:
{ "ok": true, "extracted": N }

The plugin packs all queued files into a single ustar TAR stream (with GNU long-name entries where needed) and sends it in one POST. The server extracts it under path. Files are queued during the TC copy operation and sent all at once when TC signals end-of-batch via FsStatusInfo. Content-Length is sent as a proper 64-bit value — no overflow for archives exceeding 4 GB.

Enabled when the TAR stream checkbox is checked and files are being uploaded.

SHELL_EXEC - Execute Command

POST https://example.com/sftp.php?op=SHELL_EXEC&cmd=ls+-la&cwd=/var/www

Response:
{
  "ok": true,
  "cwd": ".",
  "cwd_abs": "/var/www",
  "method": "shell_exec",
  "exit_code": 0,
  "stdout_b64": "base64_encoded_output",
  "stderr_b64": "base64_encoded_errors"
}

With AGENT_ALLOW_SHELL = false the agent answers HTTP 403 SHELL_DISABLED.

PHP Shell Console

Overview

Pseudo-terminal interface for command execution:

Opening Shell

  1. Connect with PHP Agent or PHP Shell mode
  2. Click Shell... button
  3. Plugin validates authentication first
  4. Console opens with prompt showing host and path

Shell Features

Limitations

Configuration Options

PHP Server Requirements

PHP INI Limits

Agent Constants

AGENT_NAME = 'TC PHP Agent'
AGENT_VERSION = '1.0.2'
AGENT_PSK = 'CHANGE_ME_TO_LONG_RANDOM_SECRET'
AGENT_PSK_SALT = ''
AGENT_PSK_SHA256 = ''
AGENT_ROOT = __DIR__  (or custom path)
AGENT_ALLOW_SHELL = true  (false refuses SHELL_EXEC)
AGENT_NONCE_TTL = 300
AGENT_DEFAULT_CHUNK = 262144
AGENT_MAX_LIST_ITEMS = 20000

Troubleshooting

HTTP 401 - Authentication Failed

HTTP 403 - Path Escape Attempt

HTTP 404 - Not Found

HTTP 503 - Service Unavailable

Upload Fails

Download Returns Empty File

Security Warning: Always use HTTPS for PHP Agent in production. Requests are signed and the password never travels, but over plain HTTP the files and listings you transfer are readable by anyone on the path.
Tip: Use environment variables for PSK in production. Never commit sftp.php with real secrets to version control.

Back to index