[Typo3-dev] permission problems? Use ftp.

Piebe de Vries onzin at piebe.nl
Wed Dec 15 11:35:53 CET 2004


I could not get typo3 to work on my webaccount due various restrictions 
enforced by my ISP.  I browsed through the mailinglist archive and could 
not find a working solution so I made one myself. I replaced all 
file/directory related functions in class.t3lib_div.php with similar 
functions that use ftp instead of directly working on the filesystem. 
This solved my problems. I have been using it for the last three months 
and it works fine. I planned to cleanup the code and use some typo3 
configuration parameters to make using ftp optional etc.etc. but it 
seems I will not find time for this in the near future, so I just drop 
it in the mailing list in its current state. I hope it might be useful 
for some people.

Piebe


- Following functions replace the original functions with the same name 
in class.t3lib_div.php.
- configuration:
     in connecftp(): $ftp_server (name of the ftp server), $user (name 
of the ftp user), $pass (password of the ftp user)
    in various places: $htmlRoot (the root of the html tree) $ftpRoot 
(the root of the ftp tree)
--------------------------------

function writeFile($file,$content)      {
        return t3lib_div::ftpwriteFile($file,$content);
}

function unlink($file) {  // typo3 uses the php version unlink, thus in 
order to make this work all unlink() in the typo3 code should be 
replaced by t3lib_div::unlink()
        return t3lib_div::ftpunlink($file);
}

function mkdir($theNewFolder)   {
        $theNewFolder = ereg_replace('\/$','',$theNewFolder);
         return t3lib_div::ftpmkdir($theNewFolder) ;
}

function  connectftp() {
    global $connection;
    if (isset($connection) && $connection) //reuse old connection if it 
exists
        return $connection;
       $ftp_server='the ftp server'; // ftp server

       // login to ftp server
       $user = "the user name";
       $pass = "password of ftp user";
      if ($connection = ftp_connect($ftp_server)){  // connection
            $result = ftp_login($connection, $user, $pass);
            if (!$result) {// check if connection was made
                disconnectftp();
                return false;
            }
    } else
        return false;
    return $connection;
}

function disconnectftp($persistent = true) {
    global $connection;
    if (!$persistent && isset($connection) && $connection) {
        ftp_close($connection); // close connection
        $connection = null;
    }   
}

function ftpunlink($file) {
        $htmlRoot = "/home/.sites/133/site55/web/"; // use if html root 
and ftp root differ
        $ftpRoot = "/web/";

       $orgfile = $file;
       $file = str_replace($htmlRoot, $ftpRoot, $file);

       if (!($connection = t3lib_div::connectftp()))  // connection
                 return false;
        ftp_delete($connection, $file);
        t3lib_div::disconnectftp();
        return true;
}

function ftpwritefile($file, $content) {
        $htmlRoot = "/home/.sites/133/site55/web/"; // use if html root 
and ftp root differ
        $ftpRoot = "/web/";

        $orgfile = $file;
           $file = str_replace($htmlRoot, $ftpRoot, $file);

        if (!($connection = t3lib_div::connectftp()))  // connection
                 return false;
   
        //first write to temporary file
        $tempfp = tmpfile();
        fwrite($tempfp, $content);
        rewind($tempfp);
        // now use ftp to put file at right position with right ownership
        if (!ftp_fput($connection, $file, $tempfp, FTP_BINARY)) { 
//failed to write file assuming this is because file already exist (ftp 
can not be used to overwrite a file) thus first delete the file
            ftp_rename($connection, $file, $file . ".tmp");
            // renamed file try to save again
            rewind($tempfp);
            if (!ftp_fput($connection, $file, $tempfp, FTP_BINARY)) { 
//failed to write file, put back original file
                    ftp_rename($connection, $file . ".tmp", $file);
                    t3lib_div::disconnectftp();
                    fclose($tempfp);
                    return false;
            }
            // file has been placed, ensure proper rights:
            ftp_site($connection, "CHMOD 666 " . $file);
            //ftp_site($connection, "CHMOD " . 
octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask']) . " " . $file);
            // finally delete temp file and close connection:
            ftp_delete($connection, $file . ".tmp");
            t3lib_div::disconnectftp();
       }
       fclose($tempfp);
        // there seems to be a delay between putting the file on the 
server and becoming visible for php thus wait untill visible:
        // sleep(1); // but first sleep to prevent timeout of script (in 
safe mode the excecution time can not be enlarged, but sleep is not counted)
        while(!is_file($orgfile) )
            clearstatcache();
       
        return true;
}

// create directory through FTP connection
function ftpmkdir( $newDir) {
        $htmlRoot = "/home/.sites/133/site55/web/"; // use if html root 
and ftp root differ
        $ftpRoot = "/web/";

       $newDir = str_replace($htmlRoot, $ftpRoot, $newDir);

      if (!($connection = t3lib_div::connectftp()))  // connection
                 return false;
        // ftp_chdir($connection, $path); // go to destination dir
       if(ftp_mkdir($connection,$newDir)) { // create directory
          if (ftp_site($connection, "CHMOD 777 $newDir"))
                $ret= $newDir;
         else
           $ret= false;
       } else
           $ret= false;
        t3lib_div::disconnectftp();
        return $ret;
}






More information about the TYPO3-dev mailing list