File:  [mozdev] / cdn / www / b2-include / lj_update.php
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Thu Mar 6 17:32:58 2003 UTC (16 years, 5 months ago) by cdn
Branches: MAIN
CVS tags: HEAD
m00

    1: <?php
    2: 
    3: // XMLRPC for PHP 1.02 from http://phpxmlrpc.sourceforge.net/
    4: // It's very basic, but it gets the job done
    5: // require_once("xmlrpc.inc");
    6: 
    7: function pdbg( $var ) {
    8: 	echo "<pre>";
    9: 	print_r( $var );
   10: 	echo "</pre>";
   11: }
   12: 
   13: // breaks a formatted time/date string into pieces and returns them as an array
   14: // "Y-m-d H:i:s"
   15: // I'm sure that there's a way to do this more efficiently with preg_split
   16: function lj_tmbr( $pdt ) {
   17: 	$z = explode( " ", $pdt );
   18: 	$z_d = explode( "-", $z[0] );
   19: 	$z_t = explode( ":", $z[1] );
   20: 	$zdt = array( "y" => $z_d[0], "m" => $z_d[1], "d" => $z_d[2], "h" => $z_t[0], "i" => $z_t[1] );
   21: 	return( $zdt );
   22: }
   23: 
   24: function get_ljuserdata($userid) {
   25: 	global $tableljusers,$querycount,$cache_ljuserdata,$use_cache;
   26: 	if ((empty($cache_ljuserdata[$userid])) OR (!$use_cache)) {
   27: 		$sql = "SELECT * FROM $tableljusers WHERE ID = '$userid'";
   28: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
   29: 		$myrow = mysql_fetch_array($result);
   30: 		$querycount++;
   31: 		$cache_ljuserdata[$userid] = $myrow;
   32: 	} else {
   33: 		$myrow = $cache_ljuserdata[$userid];
   34: 	}
   35: 	return($myrow);
   36: }
   37: 
   38: function get_ljuserdata2($userid) { // for team-listing
   39: 	global $tableljusers,$row;
   40: 	$ljuser_data['ID'] = $userid;
   41: 	$ljuser_data['lj_userid'] = $row->user_login;
   42: 	$ljuser_data['lj_passwd'] = $row->user_pass;
   43: 	return($ljuser_data);
   44: }
   45: 
   46: function get_ljpostdata( $post_ID ){
   47: 	global $tableljposts,$querycount,$cache_ljposts,$use_cache;
   48: 	if ((empty($cache_ljposts[$post_ID])) OR (!$use_cache)) {
   49: 		$sql = "SELECT * FROM $tableljposts WHERE ID = '$post_ID'";
   50: 		$result = mysql_query($sql) or die("Your SQL query: <br />$sql<br /><br />MySQL said:<br />".mysql_error());
   51: 		$myrow = mysql_fetch_array($result);
   52: 		$querycount++;
   53: 		$cache_ljposts[$post_ID] = $myrow;
   54: 	} else {
   55: 		$myrow = $cache_ljposts[$post_ID];
   56: 	}
   57: 	return($myrow);
   58: }
   59: 
   60: // not pleased with the assumption of index.php but i don't see any way to guess at the display name
   61: // this is based off of permalink_single - significant changes in that function should be reflected here
   62: function lj_permalink_single( $post_ID, $file='') {
   63: 	global $siteurl;
   64: 	global $querystring_start, $querystring_equal, $querystring_separator;
   65: 	if ($file=='')
   66: 		$file = 'blog.html';
   67: 	$ljpm = $siteurl.'/'.$file.$querystring_start.'p'.$querystring_equal.$post_ID.$querystring_separator.'more'.$querystring_equal.'1'.$querystring_separator.'c'.$querystring_equal.'1';
   68: 	return( $ljpm );
   69: }
   70: 
   71: function lj_update($post_ID, $user_ID, $pdt, $title, $content) {
   72: 	global $tableljposts, $blogname;
   73: 	global $lju_disablecomments, $lju_sourcefooter;
   74: 	// prep the LJClient for posting
   75: 	$ljuserdata = get_ljuserdata( $user_ID );
   76: 	// echo "ljc->U:".$ljuserdata['user_login']." ljc->P:".$ljuserdata['user_pass']."<br />";
   77: 	$ljc = new LJClient( $ljuserdata['user_login'], $ljuserdata['user_pass'] );
   78: 	// echo "ljc->U:".$ljc->lj_userid." ljc->P:".$ljc->lj_passwd."<br />";
   79: 	// and away we go... do some data prep
   80: 	$evt_time = lj_tmbr( $pdt );
   81: 	$content = stripslashes( stripslashes( $content ) );  // OUCH HACK!
   82: 	if ($lju_sourcefooter) {
   83: 		$content = $content.'<br /><br /><p>[cloned from <a href="'.lj_permalink_single( $post_ID ).'">'.$blogname.'</a>]</p>';
   84: 	}
   85: 	$response = $ljc->postevent( $title, $content,
   86: 									$evt_time['y'],	$evt_time['m'],	$evt_time['d'],
   87: 									$evt_time['h'],	$evt_time['i'],
   88: 									array( "opt_preformatted" => 1, "opt_nocomments" => $lju_disablecomments ) );
   89: 	$fc = $response->faultCode();
   90: 	if ( $fc == 0 ) {
   91: 		$ret_val = true;
   92: 		$rsp = xmlrpc_decode( $response->value() );
   93: 
   94: 		// pdbg( $rsp );
   95: 
   96: 		$query = "INSERT INTO $tableljposts (ID, LJID, post_author) VALUES ( $post_ID, ".$rsp['itemid'].", $user_ID )";
   97: 		$result = mysql_query($query);
   98: 	
   99: 		if (!$result)
  100: 		die ("Error updating B2-to-LJ Event Map... ".mysql_error()."<br />$query<br />contact the <a href=\"mailto:$admin_email\">webmaster</a>");
  101: 	} else {
  102: 		$ret_val = false;
  103: 	}
  104: 	return( $ret_val );
  105: }
  106: 
  107: function lj_edit($post_ID, $user_ID, $pdt, $title, $content) {
  108: 	global $blogname;
  109: 	global $lju_disablecomments, $lju_sourcefooter;
  110: 	$ljpostdata = get_ljpostdata( $post_ID );
  111: 	// $lj_eventid = lj_mappost( $post_ID );
  112: 	if ( !empty( $ljpostdata['LJID'] ) ) {
  113: 		// prep the LJClient for posting
  114: 		$ljuserdata = get_ljuserdata( $user_ID );
  115: 		// echo "ljc->U:".$ljuserdata['user_login']." ljc->P:".$ljuserdata['user_pass']."<br />";
  116: 		$ljc = new LJClient( $ljuserdata['user_login'], $ljuserdata['user_pass'] );
  117: 		// echo "ljc->U:".$ljc->lj_userid." ljc->P:".$ljc->lj_passwd."<br />";
  118: 		// and away we go... do some data prep
  119: 		$evt_time = lj_tmbr( $pdt );
  120: 		$content = stripslashes( stripslashes( $content ) );  // OUCH HACK!
  121: 		if ($lju_sourcefooter) {
  122: 			$content = $content.'<br /><br /><p>[cloned from <a href="'.lj_permalink_single( $post_ID ).'">'.$blogname.'</a>]</p>';
  123: 		}
  124: 		$response = $ljc->editevent( $ljpostdata['LJID'], $title, $content,
  125: 										$evt_time['y'],	$evt_time['m'],	$evt_time['d'],
  126: 										$evt_time['h'],	$evt_time['i'],
  127: 										array( "opt_preformatted" => 1, "opt_nocomments" => $lju_disablecomments ) );
  128: 		$fc = $response->faultCode();
  129: 		if ( $fc == 0 ) {
  130: 			$ret_val = true;
  131: 		} else {
  132: 			$ret_val = false;
  133: 			// die("XMLRPC Error:".$response->faultString());
  134: 		}
  135: 	}
  136: 	return( $ret_val );
  137: }
  138: 
  139: function lj_delete($post_ID, $user_ID) {
  140: 	global $tableljposts;
  141: 	$ljpostdata = get_ljpostdata( $post_ID );
  142: 	// $lj_eventid = lj_mappost( $post_ID );
  143: 	if ( !empty( $ljpostdata['LJID'] ) ) {
  144: 		// prep the LJClient for posting
  145: 		$ljuserdata = get_ljuserdata( $user_ID );
  146: 		// echo "ljc->U:".$ljuserdata['user_login']." ljc->P:".$ljuserdata['user_pass']."<br />";
  147: 		$ljc = new LJClient( $ljuserdata['user_login'], $ljuserdata['user_pass'] );
  148: 		// echo "ljc->U:".$ljc->lj_userid." ljc->P:".$ljc->lj_passwd."<br />";
  149: 		$response = $ljc->editevent( $ljpostdata['LJID'], "", "", "", "", "", "", "", "" );
  150: 		$fc = $response->faultCode();
  151: 		if ( $fc == 0 ) {
  152: 			$ret_val = true;
  153: 			$rsp = xmlrpc_decode( $response->value() );
  154: 
  155: 			// pdbg( $rsp );
  156: 
  157: 			$query = "DELETE FROM $tableljposts WHERE ID = $post_ID";
  158: 			$result = mysql_query($query);
  159: 		
  160: 			if (!$result)
  161: 			die ("Error updating B2-to-LJ Event Map... ".mysql_error()."<br />$query<br />contact the <a href=\"mailto:$admin_email\">webmaster</a>");
  162: 		} else {
  163: 			$ret_val = false;
  164: 			// die("XMLRPC Error:".$response->faultString());
  165: 		}
  166: 	}
  167: 	return( $ret_val );
  168: }
  169: 
  170: class JournalEvent
  171: {
  172: 	// this might have real functionality in the future
  173: 	// right now it exists to remind me to work on it :)
  174: 
  175: 	// ------------------------------------------------------------
  176: 	var $event_id;
  177: 	var $title;
  178: 	var $content;
  179: 	var $evt_year;
  180: 	var $evt_month;
  181: 	var $evt_day;
  182: 	var $evt_hour;
  183: 	var $evt_minute;
  184: 	var $meta_props;
  185: 	// ------------------------------------------------------------
  186: 
  187: }
  188: 
  189: class LJClient
  190: {
  191: 
  192: 	// see http://www.livejournal.com/doc/server/ljp.csp.xml-rpc.protocol.html
  193: 	// for documentation on the livejournal XML-RPC interface
  194: 
  195: 	// ------------------------------------------------------------
  196: 	var $clientid;
  197: 	var $protocol_version;
  198: 	var $lineendings;
  199: 	var $rpc_timeout;
  200: 
  201: 	var $lj_srvr;
  202: 	var $lj_port;
  203: 	var $lj_xmlrpcuri;
  204: 
  205: 	var $lj_userid;
  206: 	var $lj_passwd;
  207: 
  208: 	var $lj_loggedin;
  209: 	// ------------------------------------------------------------
  210: 
  211: 	function LJClient( $lj_userid = "", $lj_passwd = "" ){
  212: 		$this->clientid = "PHP-LJRPC/0.0.4";
  213: 		$this->protocol_version = 0;
  214: 		$this->lineendings = "unix";
  215: 		$this->rpc_timeout = 60;
  216: 
  217: 		$this->lj_srvr = "www.livejournal.com";
  218: 		$this->lj_port = "80";
  219: 		$this->lj_xmlrpcuri = "/interface/xmlrpc";
  220: 
  221: 		$this->lj_logged = false;
  222: 		$this->lj_userid = $lj_userid;
  223: 		$this->lj_passwd = md5( $lj_passwd );
  224: 		$this->client = new xmlrpc_client( $this->lj_xmlrpcuri, $this->lj_srvr, $this->lj_port );
  225: 	}
  226: 
  227: 	// ------------------------- API ------------------------------
  228: 
  229: 	function login(){
  230: 		$lj_method = "LJ.XMLRPC.login";
  231: 		$params = array( xmlrpc_encode( array( "username" => $this->lj_userid,
  232: 												"hpassword" => $this->lj_passwd,
  233: 												"ver" => $this->protocol_version,
  234: 												"clientversion" => $this->clientid ) ) );
  235: 		// print_r( xmlrpc_decode( $params[0] ) );
  236: 		$response = $this->do_the_thing( $lj_method, $params );
  237: 		$fc = $response->faultCode();
  238: 		if ( $fc == 0 ) {
  239: 			$this->loggedin = true;
  240: 			// if we want to do anything with the values from the server
  241: 			// this is where we'd do it
  242: 			// $rsp = xmlrpc_decode( $response->value() );
  243: 			return( true );
  244: 		}
  245: 		return( false );
  246: 	}
  247: 
  248: 	function postevent( $subject, $event, $evt_year, $evt_month, $evt_day, $evt_hour, $evt_minute, $meta_props ){
  249: 
  250: 		$this->chk_login();
  251: 
  252: 		$lj_method = "LJ.XMLRPC.postevent";
  253: 		$params = array( xmlrpc_encode( array( "username" => $this->lj_userid,
  254: 												"hpassword" => $this->lj_passwd,
  255: 												"ver" => $this->protocol_version,
  256: 												"lineendings" => $this->lineendings,
  257: 												"subject" => $subject,
  258: 												"event" => $event,
  259: 												"year" => $evt_year,
  260: 												"mon" => $evt_month,
  261: 												"day" => $evt_day,
  262: 												"hour" => $evt_hour,
  263: 												"min" => $evt_minute,
  264: 												"props" => $meta_props ) ) );
  265: 		// pdbg( xmlrpc_decode( $params[0] ) );
  266: 		$response = $this->do_the_thing( $lj_method, $params );
  267: 		return( $response );
  268: 	}
  269: 
  270: 	function editevent( $id, $subject, $event, $evt_year, $evt_month, $evt_day, $evt_hour, $evt_minute, $meta_props ){
  271: 
  272: 		$this->chk_login();
  273: 
  274: 		$lj_method = "LJ.XMLRPC.editevent";
  275: 		$params = array( xmlrpc_encode( array( "username" => $this->lj_userid,
  276: 												"hpassword" => $this->lj_passwd,
  277: 												"ver" => $this->protocol_version,
  278: 												"lineendings" => $this->lineendings,
  279: 												"itemid" => $id,
  280: 												"subject" => $subject,
  281: 												"event" => $event,
  282: 												"year" => $evt_year,
  283: 												"mon" => $evt_month,
  284: 												"day" => $evt_day,
  285: 												"hour" => $evt_hour,
  286: 												"min" => $evt_minute,
  287: 												"props" => $meta_props ) ) );
  288: 		// pdbg( xmlrpc_decode( $params[0] ) );
  289: 		$response = $this->do_the_thing( $lj_method, $params );
  290: 		return( $response );
  291: 	}
  292: 
  293: 	// ------------------------------------------------------------
  294: 
  295: 	// ------------------- Internal Functions ---------------------
  296: 
  297: 	function do_the_thing( $method, $params ){
  298: 		$xmlrpc_msg = new xmlrpcmsg( $method, $params );
  299: 		$xmlrpc_rsp = $this->client->send( $xmlrpc_msg, $this->rpc_timeout );
  300: 		return( $xmlrpc_rsp );
  301: 	}
  302: 
  303: 	function chk_login(){
  304: 		if ($this->loggedin == false){
  305: 			$this->login();
  306: 		}
  307: 	}
  308: 
  309: 	// ------------------------------------------------------------
  310: 
  311: }
  312: ?>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>