<?php
class RCCWP_Post
{	
	function save_files()
	{


### IF NOT CALLED FROM A CWP PAGE, IGNORE IT ###

		if( ! isset( $_REQUEST['rc_cwp_meta_files'] ) )
		{
			return;
		}



		### IF NO NEW FILES ARE GIVEN, USE THE OLD FILES ###

		if( count( $_FILES ) < 1 && isset( $_REQUEST['rc_cwp_meta_files'] ) )
		{
			foreach( $_REQUEST['rc_cwp_meta_files'] as $meta_name )
			{
				$_POST[$meta_name] = $_REQUEST[ $meta_name . '_last' ];
			}
		}

		
		### DEAL WITH SPECIFIED FILES ###

		

		foreach( $_FILES as $meta_name => $file )
		{
			$upload_filepath = $_POST[$meta_name . '_filepath']; 
			if ($upload_filepath != ""){
				$oldfilename = $upload_filepath; 
				$path = get_bloginfo('url') . '/wp-content/plugins/fresh-page/files/';

				$filename = time().substr($oldfilename, 10);
				//rename the file to avoid image caching
				rename(dirname(__FILE__) . '/files/'.$oldfilename, dirname(__FILE__) . '/files/'.$filename);
				$_POST[$meta_name] = $path . $filename;

				
			}
			else{
				$_POST[$meta_name] = $_POST[$meta_name . '_last'];
				continue;
			}

			/*if( file_exists( $file['tmp_name'] ) )
			{
				 //Old Upload code:
				$filename = time() . $file['name'];

				$path = get_bloginfo('url') . '/wp-content/plugins/fresh-page/files/';

				$_POST[$meta_name] = $path . $filename;

				move_uploaded_file( $file['tmp_name'], dirname(__FILE__) . '/files/' . $filename );
				*/
			
			/*else // try to get web file
			{
				$url = $_REQUEST[$meta_name . '_url'];
				

				if ($fp_source = @fopen($url, 'rb')) {
					//Get target filename
					$exploded_url = explode( '.', $url );
	
					$ext = array_pop( $exploded_url );
	
					$filename = time() . '_' . str_replace( 'rc_cwp_meta_', '', $meta_name ) . '.' . $ext;
					
					$path = get_bloginfo('url') . '/wp-content/plugins/fresh-page/files/';
		
					$_POST[$meta_name] = $path . $filename;
	
					$directory = dirname(__FILE__) . '/files/';

					$fp_dest = @fopen($directory . $filename,"wb");
					if ($fp_dest == false) return;

					while(!feof($fp_source)) {
						set_time_limit(30);

						if (connection_status()!=0) return;

						$readData = fread($fp_source, 1024*8);
						if ($readData == false) return;
						
						if (fwrite($fp_dest,$readData) == false) return;
						
					}
					if (fclose($fp_source) == false) return;
					if (fclose($fp_dest) == false) return;
				}
				else{
					$_POST[$meta_name] = $_POST[$meta_name . '_last'];
					continue;
				}

				

				
				if( ! $file_string = file_get_contents( $url ) ) // if we fail to get web file, use old value
				{
					$_POST[$meta_name] = $_POST[$meta_name . '_last'];

					continue;
				}

				$exploded_url = explode( '.', $url );

				$ext = array_pop( $exploded_url );

				$filename = time() . '_' . str_replace( 'rc_cwp_meta_', '', $meta_name ) . '.' . $ext;
				
				$path = get_bloginfo('url') . '/wp-content/plugins/fresh-page/files/';
	
				$_POST[$meta_name] = $path . $filename;

				$directory = dirname(__FILE__) . '/files/';

				file_put_contents( $directory . $filename, $file_string );
			}*/
		}



		### ADD THUMBNAILER PATH AND PARAMS TO THE PHOTO FILE NAME ###

		if( isset( $_REQUEST['rc_cwp_meta_photos'] ) )
		{
			foreach( $_REQUEST['rc_cwp_meta_photos'] as $meta_name )
			{
				// if photo is new, add thumbnailer path

				if( $_POST[ $meta_name ] != $_POST[ $meta_name . '_last' ] )
				{
					$path = get_bloginfo('url') . '/wp-content/plugins/fresh-page/phpThumb.php?src=';
					$_POST[ $meta_name ] = $path . $_POST[ $meta_name ];
				}
				

				// if photo is new, add params

				if( isset( $_REQUEST[ $meta_name . '_params' ] ) && $_REQUEST[ $meta_name . '_params' ] )
				{
					if( ! strpos( $_POST[$meta_name], $_REQUEST[ $meta_name . '_params' ] ) )
					{
						$_POST[$meta_name] .= $_REQUEST[$meta_name . '_params'];
					}
				}
			}
		}
	}


	function GetCustomWritePanel()
	{
		if (isset($_GET['post']))
		{

			$customWritePanelId = get_post_meta((int)$_GET['post'], RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true);
			
			if (empty($customWritePanelId))
			{
				$customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
			}
		}
		else if (isset($_REQUEST['custom-write-panel-id']))
		{
			$customWritePanelId = (int)$_REQUEST['custom-write-panel-id'];
		}
		
		if (isset($customWritePanelId))
		{
			include_once('RCCWP_Application.php');
			$customWritePanel = RCCWP_Application::GetCustomWritePanels($customWritePanelId);
		}
		return $customWritePanel;
	}
	
	function SetCustomWritePanel($postId)
	{
		if(!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel'))
			return $postId;
        		
		if (!current_user_can('edit_post', $postId))
			return $postId;
		
		$customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
		if (isset($customWritePanelId))
		{
			if (!empty($customWritePanelId))
			{
				if (!update_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId))
				{
					add_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId);
				}
			}
			else
			{
				delete_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY);
			}
		}
	}
	
	function SetMetaValue($postId)
	{
		global $wpdb;

		if(!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel'))
			return $postId;
		
		if (!current_user_can('edit_post', $postId))
			return $postId;
		
		$customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
		if (isset($customWritePanelId))
		{
			if (!empty($customWritePanelId))
			{
				$customFieldCount = count($customFieldKeys);
				$customFieldKeys = (array)$_POST['rc_cwp_meta_keys'];
				foreach ($customFieldKeys as $key)
				{
					$rawCustomFieldName = $key;
					
					if (isset($rawCustomFieldName))
					{
						if (!empty($rawCustomFieldName))
						{
							$customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($rawCustomFieldName))));

							$customFieldValue = $_POST[$rawCustomFieldName];
							$audioExt = substr($customFieldValue, -4);

							/*if($audioExt == '.mp3' || $audioExt == '.MP3' || $audioExt == '.avg' || $audioExt == '.AVG' || $audioExt == 'mpeg' || $audioExt == 'MPEG')
							{
								$customFieldValue = "\<div style=\'padding-top:3px;\'\>\<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase='\http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\' width=\'95%\' height=\'20\' wmode=\'transparent\' \>\<param name=\'movie\' value=\'http://www.freshoutmedia.com/singlemp3player.swf?file=".$customFieldValue."\' wmode=\'transparent\' /\>\<param name=\'quality\' value=\'high\' wmode=\'transparent\' /\>\<embed src=\'http://www.freshoutmedia.com/singlemp3player.swf?file=".$customFieldValue."' width=\'50\%\' height=\'20\' quality=\'high\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\' type=\'application/x-shockwave-flash\' wmode=\'transparent\' \>\</embed\>\</object\>\</div\>";
							}

							if(!(strpos($customFieldValue, 'phpThumb.php') === FALSE))
							{
								$_POST['rc_cwp_meta_Image_url'];

								if((strpos($customFieldValue, 'img src') === FALSE))
								{
									$cssClass = $wpdb->get_results("SELECT CSS FROM wp_rc_cwp_panel_custom_field WHERE panel_id='".$_POST['rc-cwp-custom-write-panel-id']."' and name='".$customFieldName."'");

									$customFieldValue = "\<img src=\'".$customFieldValue."\' class=\"".$cssClass[0]->CSS."\" \/\>";
									//$customFieldValue = "\<img src=\'".$customFieldValue."\' \/\>";
								}
							}*/

							delete_post_meta($postId, $customFieldName);
							if (is_array($customFieldValue))
							{
								foreach ($customFieldValue as $value)
								{
									$value = stripslashes(trim($value));
									add_post_meta($postId, $customFieldName, $value);
								}
							}
							else
							{
								$value = stripslashes(trim($customFieldValue));
								add_post_meta($postId, $customFieldName, $value);
							}
						}
						else
						{
							delete_post_meta($postId, $customFieldName);
						}
					}
				}
			}
			else
			{
				
			}
		}
	}
}
?>
