| 1 |
<?php
|
| 2 |
|
| 3 |
function postalso_node_post($node_src) {
|
| 4 |
$dst_name = '';
|
| 5 |
$destinations = variable_get('postalso_destinations', array());
|
| 6 |
//print_r($destinations);
|
| 7 |
if (sizeof($destinations) == 0) {
|
| 8 |
drupal_set_message(t("Destination site not set 1"));
|
| 9 |
return FALSE;
|
| 10 |
}
|
| 11 |
else if (sizeof($destinations) > 1) {
|
| 12 |
if (!$dst_name) {
|
| 13 |
drupal_set_message(t("Destination site not set 2"));
|
| 14 |
return FALSE;
|
| 15 |
}
|
| 16 |
else {
|
| 17 |
//match the dst by name
|
| 18 |
$dst = $destinations[$dst_name];
|
| 19 |
}
|
| 20 |
}
|
| 21 |
else {
|
| 22 |
if (!$dst_name) {
|
| 23 |
$dst = array_pop($destinations);
|
| 24 |
}
|
| 25 |
else {
|
| 26 |
//match the dst by name
|
| 27 |
$dst = $destinations[$dst_name];
|
| 28 |
}
|
| 29 |
}
|
| 30 |
// print_r($destinations);
|
| 31 |
// print_r($dst);
|
| 32 |
|
| 33 |
if (!$dst) {
|
| 34 |
drupal_set_message(t("Destination site not found"));
|
| 35 |
return FALSE;
|
| 36 |
}
|
| 37 |
|
| 38 |
$dst['uid'] = 1; //TODO
|
| 39 |
$node = _postalso_format_xmlrpc_node_save($node_src, $dst);
|
| 40 |
|
| 41 |
// assume the remote site is a Drupal site, other types TODO
|
| 42 |
$result = xmlrpc($dst['url'], "user.login", ' ', $dst['user'], $dst['pass']);
|
| 43 |
if ($result) {
|
| 44 |
//print_nice($result);
|
| 45 |
if (is_array($result)) {
|
| 46 |
$sid = $result['sessid'];
|
| 47 |
}
|
| 48 |
//print $sid;
|
| 49 |
}
|
| 50 |
else {
|
| 51 |
print 'user error';
|
| 52 |
print $dst['url'] ."<br>\n";
|
| 53 |
print "user.login" ."<br>\n";
|
| 54 |
print $dst['user'] ."<br>\n";
|
| 55 |
print $dst['pass'] ."<br>\n";
|
| 56 |
print xmlrpc_errno() ."<br>\n";
|
| 57 |
print xmlrpc_error_msg() ."<br>\n";
|
| 58 |
return FALSE;
|
| 59 |
}
|
| 60 |
$result = xmlrpc($dst['url'], "user.login", ' ', $dst['user'], $dst['pass']);
|
| 61 |
if ($result) {
|
| 62 |
//print_nice($result);
|
| 63 |
if (is_array($result)) {
|
| 64 |
$sid = $result['sessid'];
|
| 65 |
}
|
| 66 |
//print $sid;
|
| 67 |
}
|
| 68 |
else {
|
| 69 |
print 'user error';
|
| 70 |
print $dst['url'] ."<br>\n";
|
| 71 |
print "user.login" ."<br>\n";
|
| 72 |
print $dst['user'] ."<br>\n";
|
| 73 |
print $dst['pass'] ."<br>\n";
|
| 74 |
print xmlrpc_errno() ."<br>\n";
|
| 75 |
print xmlrpc_error_msg() ."<br>\n";
|
| 76 |
return FALSE;
|
| 77 |
}
|
| 78 |
|
| 79 |
|
| 80 |
$result = xmlrpc($dst['url'], "node.save", $sid, $node);
|
| 81 |
if ($result) {
|
| 82 |
$nid_dst = $result;
|
| 83 |
// save to the postalso table, TODO
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
print 'node save error';
|
| 87 |
print xmlrpc_errno();
|
| 88 |
print xmlrpc_error_msg();
|
| 89 |
}
|
| 90 |
|
| 91 |
|
| 92 |
}
|
| 93 |
|
| 94 |
function _postalso_format_xmlrpc_node_save($node_src, $dst = array()) {
|
| 95 |
// print("<pre>$nid");print_r($node_src); die;
|
| 96 |
$node_type_dst = 'page'; // should make it setable, TODO
|
| 97 |
$node_dst = array(
|
| 98 |
"type" => $node_type_dst, //required
|
| 99 |
"uid" => $dst['uid'], //required both uid and name to associate to a user
|
| 100 |
"name" => $dst['user'], //required both uid and name to associate to a user
|
| 101 |
|
| 102 |
"title" => $node_src->title,
|
| 103 |
"body" => $node_src->body,
|
| 104 |
);
|
| 105 |
return $node_dst;
|
| 106 |
}
|