File Manager

Starting from Plesk 11.5, you can create extensions that add functionality to Plesk File Manager and manage domain files by themselves.

File Manager's Context Menu

To add an item to the context menu of the File Manager:

  1. Create the file /plib/library/FileManager/Action.php in your extension directory.
  2. Create the class Modules_MyModule_FileManager_Action that extends the pm_FileManager_Action class in this file. MyModule here is your extension's ID.

    An example of such class in provided below:

    class Modules_CodeEditor_FileManager_Action extends pm_FileManager_Action
    {
    	/**
    	* Retrieve title for action
    	*/
    	public function getTitle()
    	{
    		pm_Context::init('code-editor');
     
    		return $this->lmsg('actionTitle');
    	}
     
    	/**
    	* Retrieve name for action
    	* by default name is camelCase module ID
    	*/
    	public function getName()
    	{
    		return parent::getName();
    	}
     
		    /**
    	* Retrieve URL for action
    	*/
    	public function getHref()
    	{
    		pm_Context::init('code-editor');
     
    		$params = array(
    		'currentDir=' . urlencode($this->_item['currentDir']),
    		'file=' . urlencode($this->_item['name']),
    		);
    		return pm_Context::getBaseUrl() . '?' . implode('&', $params);
    	}
     
    	/**
    	* Checks if action is available for a file
    	*/
    	public function isActive()
    	{
    		if ($this->_item['isDirectory']) {
    			return false;
    		}
     
    		return true;
    	}
     
    	/**
    	* Checks if action is default for a file
    	*/
    	public function isDefault()
    	{
    		return false;
    	}
    }

The variable $this->_item contains an array with the information about the file or a directory on which the menu is shown:

array (
    'name' => 'hello.txt',
    'modificationTimestamp' => '1380276799',
    'modificationDate' => 'Sep 27, 2013 05:13 PM',
    'size' => '8192',
    'formatedSize' => '8.0 KB',
    'user' => 'user_ifvbjsgcdk',
    'group' => 'psacln',
    'filePerms' => 'rw- r-- r-- ',
    'isDirectory' => false,
    'icon' => '/theme/icons/16/plesk/file-txt.png',
    'isReadonly' => false,
    'currentDir' => '/httpdocs',
)

The result of adding such an extension is shown below:

Managing Webspace Files and Directories

To manage files and directories within a domain, use the pm_FileManager class. This class lets you create and remove files and directories, check for the existence of files and directories, read and write data to files, and also change file and directory permissions (only on Linux).

Important: For security considerations, we strongly recommend that you do not use system calls such as fopen or file_get_contents in the code of your extension. When you use such calls, the extension works with files on behalf of the psaadm user. In turn, if you use the pm_FileManager class, the manipulations are performed on behalf of the domain owner's system user.

Below is an example of working with files by means of the pm_FileManager class.

    $fileManager = new pm_FileManager($domainId);
    $filePath = $fileManager->getFilePath("$currentDir/$file");
    if ($fileManager->fileExists($filePath)) {
    	$fileData = $fileManager->fileGetContents($filePath);
    	// Some manipulation with file data...
    	$fileData = str_replace('foo', 'bar', $fileData);
  	   
    	$fileManager->filePutContents($filePath, $fileData);
    }

The $filePath path here is an absolute path. It is formed from the relative path within the domain directory $currentDir by the call of the getFilePath method.

Managing Server Files and Directories

To manage files and directories anywhere on the server, use the pm_ServerFileManager class. This class enables you to perform the same operations as the pm_FileManager class.

Below is an example of working with files by means of the pm_ServerFileManager class.

	$fileManager = new pm_ServerFileManager();
	$configPath = PRODUCT_ROOT . '/admin/conf/panel.ini';
	if ($fileManager->fileExists($configPath)) {
		$fileData = $fileManager->fileGetContents($filePath);
		// Some manipulation with file data...
		$fileData = str_replace('foo', 'bar', $fileData);

		$fileManager->filePutContents($filePath, $fileData);
	}