Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wpebooks site replicator wordpress plugin hangs windows using exec()

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

We purchased a site replicator plugin, only to find out it doesn't work on windows and support told me to fix it myself.

I have replaced the following line..

Code:
				// copy media
				$cp_base = ABSPATH . UPLOADBLOGSDIR . '/';
				$cp_cmd = str_replace( '//', '/', 'cp -r ' . $cp_base . $copy_id . ' ' . $cp_base . $new_id );

				exec($cp_cmd);

with...

Code:
				// copy media
				$cp_base = ABSPATH . UPLOADBLOGSDIR . '\\';
				$cp_cmd = str_replace( '/', '\\', ' xcopy ' . $cp_base . $copy_id . ' ' . $cp_base . $new_id );
				$cp_cmd .= ' /E /I /Q >NUL ';  
				
				exec($cp_cmd);

But the plugin just hangs. I beleive it is something to do with 'exec()' which causes problems with windows but as I don't do PHP I have no idea what the problem is or how to fix it.

I've read the PHP exec() info and all the threads on suposed fixes, but so far can't find one that works.

As you can see I've supressed all prompts from the command including output of copied files and even sent the 'xx files copied' final response from the command to NUL , I've also tried to capture any response via
Code:
$tmp = exec($cp_cmd);
but nothing seems to work and the plugin just hangs?

Any ideas how I fix this?

Thanks,
1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
easiest way to think this problem through is

1. echo $cp_cmd without execing it.
2. copy it and try running from the command line. you may have to prefix it with "cmd " but i'm not a windows guy.
3. once fixed on the command line, reverse engineer the changes back to the exec command remembering to use escape functions on each argument.

but i suspect it is better to use the copy function within php instead.

Code:
$cp_base = realpath(ABSPATH). DIRECTORY_SEPARATOR . UPLOADBLOGSDIR . DIRECTORY_SEPARATOR;
copy($cp_base . $copy_id, $cp_base . $new_id );
 
Hi jpadie,

I had already done the copy and run command in cmd.exe prompt, it works fine in a DOS prompt, infact the command works fine in PHP, it's just hangs the browser and the final 'copy complete' message is never received with the browser permenanly egg timing.

Does the PHP copy command copy directories as well as the files with in them?

Cheers,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
@1DMF
the short answer is that I don't know whether copy would work on windows for directories. I suspect that it would. However, it would not copy recursively.

there are examples of recursive copy code in the php manual here

I am surprised that the exec would hang the system. that is suggestive that the actual command is not being completed at the OS level; or perhaps that you are using a version of php with bugs in it. do you get the same hang if you run the php script from the command line rather than the browser? I don't know about piping to NULL in windows either, I'm afraid. try without the pipe.

and for exec use this
Code:
exec($cmd, $output, $return);
echo '<pre>' . print_r($output, true) . "\nReturn value: " . $return . "\n</pre>";
this captures that actual output, rather than just the last line.
 
I've tried your example, same thing happens, I don't get the ouput regarding the $output / $return and the browser just egg times.

The images are copied and the site replicated, however, the browser never completes the task nor displays any further output.

phpinfo() tells me : PHP Version 5.3.13 - Build Date :- May 8 2012 18:41:52 , if that help?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
And does the script hang if run from the cli?
 
provide the script with some known parameters and then just call it via the cli. like you would do for debugging.
 

here is the code in the plugin for the form, perhaps you could explain what I'd put as arguments to the command line?

Code:
<form method="POST">
			<input type="hidden" name="action" value="copy_blog" />
<?php		if( $src_blog ) {
			echo '<h4>' . esc_html( sprintf( __( 'Replicate %s (%s)' ), $src_blog->blogname, $src_blog->siteurl ) ) . '</h4>';
			echo '<input type="hidden" name="source_blog" value="' . $replicate_id . '" />';
		} else { ?>
			<h4 class="submit">New Registrations:&nbsp;
				<input class='button' type='submit' name="default_blog" value='Replicate the selected site' />&nbsp;
				<input class='button' type='submit' name="clear_default" value='Replicate WordPress default site' />
			</h4>
<?php		} ?>
				<table class="form-table">
<?php		if( !$src_blog ) { ?>
					<tr class="form-field form-required">
						<th style="text-align:center;" scope='row'><?php _e('Choose Source Site to Replicate'); ?></th>
						<td>
							<select name="source_blog" style="min-width:300px;">
<?php			foreach( $blogs as $blog ) { ?>
				<option value="<?php echo $blog->blog_id; ?>" <?php selected( $blog->blog_id, $src_id ); ?>><?php echo substr($blog->domain_path, 0, -1); ?></option>
<?php			} ?>
						</select></td>
					</tr>
<?php		} ?>
					<tr class="form-field form-required">
						<th style="text-align:center;" scope='row'><?php _e('New Site Address') ?></th>
						<td>
						<?php if( is_subdomain_install() ) { ?>
							<input name="blog[domain]" type="text" title="<?php _e('Domain') ?>"/>.<?php echo $current_site->domain;?>
						<?php } else {
							echo $current_site->domain . $current_site->path ?><input name="blog[domain]" type="text" title="<?php _e('Domain') ?>"/>
						<?php } ?>
						</td>
					</tr>
					<tr class="form-field form-required">
						<th style="text-align:center;" scope='row'><?php _e('New Site Title') ?></th>
						<td><input name="blog[title]" type="text" size="20" title="<?php _e('Title') ?>"/></td>
					</tr>
					<tr class="form-field form-required">
						<th style="text-align:center;" scope='row'><?php _e('New Site Admin Email') ?></th>
						<td><input name="blog[email]" type="text" size="20" title="<?php _e('Email') ?>"/></td>
					</tr>
					<tr class="form-field">
						<td colspan='2'><?php _e('A new user will be created if the above email address is not in the database.') ?><br /><?php _e('The username and password will be mailed to this email address.') ?></td>
					</tr>
				</table><?php
				wp_nonce_field( 'copy_blog' ); ?>
			<p class="submit"><input class='button' type='submit' value='Replicate Now' /></p>
		</form>

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
you wouldn't. you'd hard code the incoming parameters for testing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top