*** wordpress/xmlrpc.php	Fri May 21 17:17:52 2004
--- wordpress-local/xmlrpc.php	Fri Jul  9 22:30:23 2004
***************
*** 1310,1316 ****
  				$post_category[] = get_cat_ID($cat);
  			}
  		} else {
! 			$post_category[] = 1;
  		}
  		
  		// We've got all the data -- post it:
--- 1300,1306 ----
  				$post_category[] = get_cat_ID($cat);
  			}
  		} else {
! 			$post_category = 0;
  		}
  		
  		// We've got all the data -- post it:
***************
*** 1409,1419 ****
  
  		// Do some timestamp voodoo
  		$dateCreated = $contentstruct['dateCreated'];
! 		$dateCreated = $dateCreated ? iso8601_decode($dateCreated) : current_time('timestamp',1);
  		$post_date = date('Y-m-d H:i:s', $dateCreated);
  		$post_date_gmt = get_gmt_from_date($post_date);
  
- 
  		// We've got all the data -- post it:
  		$newpost = compact('ID','post_content','post_title','post_category','post_status','post_excerpt','comment_status','ping_status','post_date','post_date_gmt');
  
--- 1399,1408 ----
  
  		// Do some timestamp voodoo
  		$dateCreated = $contentstruct['dateCreated'];
! 		$dateCreated = $dateCreated ? iso8601_decode($dateCreated) : strtotime($postdata['post_date']);
  		$post_date = date('Y-m-d H:i:s', $dateCreated);
  		$post_date_gmt = get_gmt_from_date($post_date);
  
  		// We've got all the data -- post it:
  		$newpost = compact('ID','post_content','post_title','post_category','post_status','post_excerpt','comment_status','ping_status','post_date','post_date_gmt');
  
***************
*** 1429,1438 ****
  			sleep($sleep_after_edit);
  		}
  
! 		pingback($content, $post_ID);
! 		trackback_url_list($content_struct['mt_tb_ping_urls'],$post_ID);
  
! 		logIO("O","(MW) Edited ! ID: $post_ID");
  		$myResp = new xmlrpcval(true,"boolean");
  
  		return new xmlrpcresp($myResp);
--- 1418,1427 ----
  			sleep($sleep_after_edit);
  		}
  
! 		pingback($content, $ID);
! 		trackback_url_list(implode(",", $contentstruct['mt_tb_ping_urls']),$ID);
  
! 		logIO("O","(MW) Edited ! ID: $ID");
  		$myResp = new xmlrpcval(true,"boolean");
  
  		return new xmlrpcresp($myResp);
***************
*** 1476,1495 ****
  			$allow_comments = ('open' == $postdata['comment_status'])?1:0;
  			$allow_pings = ('open' == $postdata['ping_status'])?1:0;
  
  			$resp = array(
! 				'link' => new xmlrpcval(post_permalink($post_ID)),
  				'title' => new xmlrpcval($postdata["Title"]),
  				'description' => new xmlrpcval($post['main']),
  				'dateCreated' => new xmlrpcval($post_date,'dateTime.iso8601'),
  				'userid' => new xmlrpcval($postdata["Author_ID"]),
  				'postid' => new xmlrpcval($postdata["ID"]),
  				'content' => new xmlrpcval($postdata["Content"]),
! 				'permalink' => new xmlrpcval(post_permalink($post_ID)),
! 				'categories' => new xmlrpcval($catlist,'array'),
  				'mt_excerpt' => new xmlrpcval($postdata['Excerpt']),
  				'mt_allow_comments' => new xmlrpcval($allow_comments,'int'),
  				'mt_allow_pings' => new xmlrpcval($allow_pings,'int'),
! 				'mt_text_more' => new xmlrpcval($post['extended'])
  			);
  			
  			$resp = new xmlrpcval($resp,'struct');
--- 1465,1495 ----
  			$allow_comments = ('open' == $postdata['comment_status'])?1:0;
  			$allow_pings = ('open' == $postdata['ping_status'])?1:0;
  
+ 			// Retrun if Markdown is active or not. Useful for ecto.
+ 			// without this, ecto could apply "Convert linebreaks" sometimes.
+ 			$current_plugins = explode("\n", (get_settings('active_plugins')));
+ 			if (!empty($current_plugins) && in_array( "markdown.php", $current_plugins)) {
+ 				$mt_convert_breaks = 'markdown';
+ 			} else {
+ 				$mt_convert_breaks = '__default__';
+ 			}
+ 
  			$resp = array(
! 				'link' => new xmlrpcval(get_permalink($post_ID)),
  				'title' => new xmlrpcval($postdata["Title"]),
  				'description' => new xmlrpcval($post['main']),
  				'dateCreated' => new xmlrpcval($post_date,'dateTime.iso8601'),
  				'userid' => new xmlrpcval($postdata["Author_ID"]),
  				'postid' => new xmlrpcval($postdata["ID"]),
  				'content' => new xmlrpcval($postdata["Content"]),
! 				'permaLink' => new xmlrpcval(get_permalink($post_ID)),
! 				// Disable this field. use 'mt.getPostCategories instead
! 				//'categories' => new xmlrpcval($catlist,'array'), 
  				'mt_excerpt' => new xmlrpcval($postdata['Excerpt']),
  				'mt_allow_comments' => new xmlrpcval($allow_comments,'int'),
  				'mt_allow_pings' => new xmlrpcval($allow_pings,'int'),
! 				'mt_text_more' => new xmlrpcval($post['extended']),
! 				'mt_convert_breaks' => new xmlrpcval($mt_convert_breaks)
  			);
  			
  			$resp = new xmlrpcval($resp,'struct');
***************
*** 1539,1545 ****
  			$content = new xmlrpcval($entry['post_content']);
  			$excerpt = new xmlrpcval($entry['post_excerpt']);
  			
! 			$pcat = stripslashes(get_cat_name($entry['post_category']));
  			
  			// For multiple cats, we might do something like
  			// this in the future:
--- 1539,1545 ----
  			$content = new xmlrpcval($entry['post_content']);
  			$excerpt = new xmlrpcval($entry['post_excerpt']);
  			
! 			// $pcat = stripslashes(get_cat_name($entry['post_category']));
  			
  			// For multiple cats, we might do something like
  			// this in the future:
***************
*** 1549,1563 ****
  			//$catstruct['isPrimary'] = TRUE;
  			
  			//$catstruct2 = phpxmlrpc_encode($catstruct);
  			
! 			$categories = new xmlrpcval(array(new xmlrpcval($pcat)),'array');
  
  			$post = get_extended($entry['post_content']);
  
  			$postid = new xmlrpcval($entry['ID']);
  			$title = new xmlrpcval(stripslashes($entry['post_title']));
  			$description = new xmlrpcval(stripslashes($post['main']));
! 			$link = new xmlrpcval(post_permalink($entry['ID']));
  			$permalink = $link;
  
  			$extended = new xmlrpcval(stripslashes($post['extended']));
--- 1549,1570 ----
  			//$catstruct['isPrimary'] = TRUE;
  			
  			//$catstruct2 = phpxmlrpc_encode($catstruct);
+ 
+ 			$catids = wp_get_post_cats('1', $entry['ID']);
+ 		
+ 			// This should return multiple categories correctly
+ 			foreach($catids as $catid) {	
+ 				$catarray[] = new xmlrpcval(get_cat_name($catid),'string');
+ 			}
  			
! 			$categories = new xmlrpcval($catarray,'array');
  
  			$post = get_extended($entry['post_content']);
  
  			$postid = new xmlrpcval($entry['ID']);
  			$title = new xmlrpcval(stripslashes($entry['post_title']));
  			$description = new xmlrpcval(stripslashes($post['main']));
! 			$link = new xmlrpcval(get_permalink($entry['ID']));
  			$permalink = $link;
  
  			$extended = new xmlrpcval(stripslashes($post['extended']));
***************
*** 1573,1579 ****
  				'title' => $title,
  				'description' => $description,
  				'link' => $link,
! 				'permalink' => $permalink,
  				'mt_excerpt' => $excerpt,
  				'mt_allow_comments' => $allow_comments,
  				'mt_allow_pings' => $allow_pings,
--- 1580,1586 ----
  				'title' => $title,
  				'description' => $description,
  				'link' => $link,
! 				'permaLink' => $permalink,
  				'mt_excerpt' => $excerpt,
  				'mt_allow_comments' => $allow_comments,
  				'mt_allow_pings' => $allow_pings,
***************
*** 1624,1636 ****
  
  
  $mwnewmedia_sig =  array(array($xmlrpcStruct,$xmlrpcString,$xmlrpcString,$xmlrpcString,$xmlrpcStruct));
! $mwnewmedia_doc = 'Upload image or other binary data, MetaWeblog API-style (unimplemented)';
  
  function mwnewmedia($params) {	// ($blogid, $user, $pass, $struct) 
  	global $xmlrpcerruser;
  	
  	return new xmlrpcresp(0, $xmlrpcerruser+10, // user error 10
  	  'metaWeblog.newMediaObject not implemented (yet)');
  }
  
  
--- 1631,1748 ----
  
  
  $mwnewmedia_sig =  array(array($xmlrpcStruct,$xmlrpcString,$xmlrpcString,$xmlrpcString,$xmlrpcStruct));
! $mwnewmedia_doc = 'Upload image or other binary data, MetaWeblog API-style';
! 
! /*  File Upload in WordPress XML-RPC 
! 
! 
!  metaWeblog.newMediaObject (blogid, username, password, struct)
! 
!  The blogid, username and password params are as in the Blogger API. 
! 
!  The struct must contain at least three elements, name, type and bits.
! 
!  name is a string, it may be used to determine the name of the file 
!  that stores the object, or to display it in a list of objects. 
!  It determines how the weblog refers to the object. If the name is 
!  the same as an existing object stored in the weblog, it may replace 
!  the existing object.
! 
!  type is a string, it indicates the type of the object, it's a standard 
!  MIME type, like audio/mpeg or image/jpeg or video/quicktime. 
! 
!  bits is a base64-encoded binary value containing the content of the object.
! 
!  The struct may contain other elements, which may or may not be stored by 
!  the content management system.
! 
!  If newMediaObject fails, it throws an error. If it succeeds, it returns 
!  a struct, which must contain at least one element, url, which is the url 
!  through which the object can be accessed. It must be either an FTP or HTTP url.
! 
! */
  
  function mwnewmedia($params) {	// ($blogid, $user, $pass, $struct) 
  	global $xmlrpcerruser;
+ 
+ 	$xblogid = $params->getParam(0);
+ 	$xuser = $params->getParam(1);
+ 	$xpass = $params->getParam(2);
+ 	$xdata = $params->getParam(3);
+ 	
+ 	$blogid = $xblogid->scalarval();
+ 	$username = $xuser->scalarval();
+ 	$password = $xpass->scalarval();
+ 	$datastruct = phpxmlrpc_decode($xdata);
+ 
+ 	$name = $datastruct['name'];
+ 	$type = $datastruct['type'];
+ 	$bits = $datastruct['bits'];
+ 	
+ 	$file_realpath = get_settings('fileupload_realpath'); 
+ 	$file_url = get_settings('fileupload_url');
+ 
+ 	$userdata = get_userdatabylogin($username);
+ 	$userlevel = $userdata->user_level;
+ 
+ 	if (user_pass_ok($username,$password)) {
+ 		if( !get_settings('use_fileupload')) {
+ 			// Uploads not allowed
+ 			logIO("O","(MW) Uploads not allowed");
+ 			return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
+ 					   'No uploads allowed for this site');
+ 		} 
+ 		
+ 		if( get_settings('fileupload_minlevel') > $userlevel) {
+ 			// User has not enough privileges
+ 			logIO("O","(MW) Not enough privilege");
+ 			return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
+ 					   $username.' is not allowed to upload files to this site');
+ 		}
+ 
+ 		if( $file_realpath == "" || $file_url == "" ) {
+ 			// WordPress is not correctly configured
+ 			logIO("O","(MW) Bad configuration. Real/URL path not defined");
+ 			return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
+ 					   'Please configure WordPress with valid paths for file upload');
+ 		}
+ 		
+ 		$prefix = "/";
+ 		
+ 		if( !empty($name)) {
+ 			// Create the path
+ 			$localpath = $file_realpath.$prefix.$name;
+ 			$url = $file_url.$prefix.$name;
+ 
+ 			/* encode & write data (binary) */
+ 			$ifp = fopen( $localpath, "wb" );
+ 			$success = fwrite( $ifp, $bits );
+ 			fclose( $ifp );
+ 			chmod( $localpath, 0666 );
+ 
+ 			if( $success ) {
+ 				$resp = array(
+ 							'url' => new xmlrpcval( $url ),
+ 						);
+ 				
+ 				$resp = new xmlrpcval($resp,'struct');
+ 				return new xmlrpcresp($resp);
+ 			} else {
+ 				return new xmlrpcresp(0, $xmlrpcerruser+3, 
+ 	   			   			'Could not write file '.$name.' to '.$localpath );
+ 			}
+ 		}
  	
+ 	} else {
+ 		logIO("O","(MW) Wrong username/password combination <b>$username / $password</b>");
+ 		return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
+ 	   'Wrong username/password combination '.$username.' / '.starify($password));
+ 	}
+ 
+ 	/*	
  	return new xmlrpcresp(0, $xmlrpcerruser+10, // user error 10
  	  'metaWeblog.newMediaObject not implemented (yet)');
+ 	*/
  }
  
  
***************
*** 1815,1824 ****
  			$struct['postid'] = new xmlrpcval($post['ID'], 'string');
  			$struct['title'] = new xmlrpcval($post['post_title'], 'string');
  			
! 			$result[] = $struct;
  		}
  		
! 		return new xmlrpcresp(new xmlrpcval($results,'array'));
  
  	} else {
  		return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
--- 1927,1936 ----
  			$struct['postid'] = new xmlrpcval($post['ID'], 'string');
  			$struct['title'] = new xmlrpcval($post['post_title'], 'string');
  			
! 			$result[] = new xmlrpcval($struct,'struct');
  		}
  		
! 		return new xmlrpcresp(new xmlrpcval($result,'array'));
  
  	} else {
  		return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
***************
*** 1834,1840 ****
  	// This should probably check the status of the 'use_bbcode' 
  	// and 'use_gmcode' config options.
  	
! 	return new xmlrpcresp(new xmlrpcval(array(),'array'));
  }
  
  
--- 1946,1960 ----
  	// This should probably check the status of the 'use_bbcode' 
  	// and 'use_gmcode' config options.
  	
! 	// Archaic Markdown check
! 	$current_plugins = explode("\n", (get_settings('active_plugins')));
! 	if (!empty($current_plugins) && in_array( "markdown.php", $current_plugins)) {
! 		$struct['label'] = 'Markdown';
! 		$struct['key'] = 'markdown';
! 	}
! 	
! 	$xmlstruct = phpxmlrpc_encode($struct);
! 	return new xmlrpcresp(new xmlrpcval(array($xmlstruct),'array'));
  }
  
  
