WordPress.org

Ready to get started?Download WordPress

Forums

Really Simple CAPTCHA
Function cleanup : unlink permission denied (14 posts)

  1. soju22
    Member
    Posted 2 years ago #

    Hello

    Nice plugin, but with the latest version there is an error when cleanup files :

    unlink(/path/wp-content/uploads/wpcf7_captcha/1630659852.png) function.unlink : Permission denied in /path/wp-content/plugins/really-simple-captcha/really-simple-captcha.php on line 190

    This make sense since these files are chmod to 0444 or 0440
    Why not chmod to 0644 / 0640 ?

    http://wordpress.org/extend/plugins/really-simple-captcha/

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 2 years ago #

    What is current permission of the parent directory?

  3. soju22
    Member
    Posted 2 years ago #

    No specific perms since this bug appears only on windows.

  4. squeaky
    Member
    Posted 2 years ago #

    Any solution to this? I am experiencing the same problem on a windows server. IUSR has full access to the captcha directory.

  5. kate.ussr
    Member
    Posted 2 years ago #

    open really-simple-captcha/really-simple-captcha.php

    186 foreach ( $suffixes as $suffix ) {
    187 $filename = sanitize_file_name( $prefix . $suffix );
    188 $file = trailingslashit( $this->tmp_dir ) . $filename;
    189 if ( is_file( $file ) )
    190 unlink( $file );

    you have to replace "trailingslashit" to "untrailingslashit" on line 188.

  6. zempachi
    Member
    Posted 2 years ago #

    Hello,
    i have the same problem on Windows (Version 1.5) and it's clerary a problem of permission.
    The temp files (png and txt) are created with a chmod 0444 and 0440.
    Or this means no "write" permission. Then, it is impossible to delete those files (unlink).
    So the only solution is to set (really-simple-captcha.php)
    - "file_mode" = 0644 (line 70)
    - "answer_file_mode" = 0640 (line 73)

  7. fad115
    Member
    Posted 2 years ago #

    Registered just to say that, i was also having the unlink problem on ISS.
    Suggestion by kate.ussr, replacing "trailingslashit" to "untrailingslashit" on line 188, worked for me! So thanks a lot kate.ussr :)

  8. rossebow
    Member
    Posted 1 year ago #

    yup kate.ussr's suggestion worked for me too! thanks very much...

  9. mahendrakumbhar
    Member
    Posted 1 year ago #

    Thanks kate.ussr...your suggestion really did help...but now it is showing some different problem..... Failed to send your message. Please try later or contact the administrator by another method.

  10. Faramarz Salehpour
    Member
    Posted 1 year ago #

    First of all, thanks for awesome plugins Takayuki, great job!

    I also had this problem with my development machine (Windows + Apache + mod_FCGI + PHP5).

    The problem is with the permissions which, on windows, sets the files as read-only and makes PHP unable to delete the files.

    I tried to fix this through tweaking the permissions for Apache and PHP and the directory containing the files, but all failed.

    The solution turned out to be changing the permissions (chmod) of the files just before deleting (unlink).

    so, I added @chmod (@file, 0777); just before the unlink() and it works perfectly now.

    To do the same, you can edit really-simple-captcha.php and make the modifications on functions remove and cleanup as follows:

    /* Remove temporary files with $prefix */
    
    	function remove( $prefix ) {
    		$suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' );
    
    		foreach ( $suffixes as $suffix ) {
    			$filename = sanitize_file_name( $prefix . $suffix );
    			$file = trailingslashit( $this->tmp_dir ) . $filename;
    			if ( is_file( $file ) )
    			{
    				@chmod( $file, 0777 );
    				unlink( $file );
    			}
    		}
    	}
    
    	/* Clean up dead files older than $minutes in the tmp folder */
    
    	function cleanup( $minutes = 60 ) {
    		$dir = trailingslashit( $this->tmp_dir );
    
    		if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! is_writable( $dir ) )
    			return false;
    
    		$count = 0;
    
    		if ( $handle = @opendir( $dir ) ) {
    			while ( false !== ( $filename = readdir( $handle ) ) ) {
    				if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) )
    					continue;
    
    				$file = $dir . $filename;
    
    				$stat = @stat( $file );
    				if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
    					@chmod( $file, 0777 );
    					@unlink( $file );
    					$count += 1;
    				}
    			}
    
    			closedir( $handle );
    		}
    
    		return $count;
    	}

    By the way, the plugin works perfectly fine on Linux, so if your host is running Linux, you don't need to apply these changes there.

  11. Andy Schmidt
    Member
    Posted 1 year ago #

    Any plans to make this patch part of the official code, as it would make the plug-in platform independent!

    As has been reported by several users, the current code can eventually result in a "denial of service" scenario when tens of thousands of files start accumulating within a few weeks - and the server starts consuming extensive amount of time every 60 seconds, attempting to iterate through and to (always unsuccessfully) delete those tens of thousands of files individually.

    Assuming that the default file_modes were chosen for a good reason, then (at minimum) a chmod to 0777 is needed prior to any unlink for it to succeed. (Ideally, appropriate error-handling would also be addeded for unlink returning "false", to report that the necessary cleanup is failing!)

  12. Faramarz Salehpour
    Member
    Posted 1 year ago #

    Takayuki should decide about this.

    My suggestion is to host the code on github, so bug tracking and code contribution would become easier.

  13. tedinoz
    Member
    Posted 1 year ago #

    Thanks, Faramarz Salehpour. Worked for me - development machine=Windows + Apache + mod_FCGI + PHP5.

    I too hope this goes to the core code.

    Ted in Sydney

  14. buchannon
    Member
    Posted 1 year ago #

    Thanks kate.ussr!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic