コード例 #1
0
ファイル: gplus.php プロジェクト: jay2413/wpgplus
function wpgplus_update_profile_status($post_id)
{
    $wpgplus_debug_file = WP_PLUGIN_DIR . '/wpgplus/debug.txt';
    /* Set up the post */
    global $more;
    $more = 0;
    //only the post teaser please
    $my_post = get_post($post_id);
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Inside update_profile_states with post_id " . $post_id . " \n");
    if (!empty($my_post->post_password)) {
        // post is password protected, don't post
        wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Post is password protected\n");
        return;
    }
    if (get_post_type($my_post->ID) != 'post') {
        // only do this for posts
        wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Post type is " . get_post_type($my_post->ID) . "\n");
        return;
    }
    $my_post_text = get_post_meta($post_id, 'wpgplus_message', true);
    // if we have a post_message
    if ($my_post_text == '') {
        if ($my_post->post_excerpt && $my_post->post_excerpt != '') {
            $my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content', $my_post->post_excerpt)));
        } else {
            $my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content', $my_post->post_content)));
        }
    }
    if (strlen($my_post_text) >= 995) {
        $space_index = strrpos(substr($my_post_text, 0, 995), ' ');
        $short_desc = substr($my_post_text, 0, $space_index);
        $short_desc .= '...';
        $my_post_text = $short_desc;
    }
    /* Now let's go get the form */
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Getting form for posting\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text is ". $my_post_text ."\n");
    // These are the cookies I know of - not sure all are needed
    $cookies = array();
    $my_cookies = array('GAPS', 'GALX', 'NID', 'SID', 'LSID', 'HSID', 'SSID', 'APISID', 'SAPISID');
    foreach ($my_cookies as $name) {
        $new_cookie = wpgplus_get_cookie($name);
        if ($new_cookie) {
            $cookies[] = wpgplus_get_cookie($name);
        }
    }
    //wpgplus_debug("\nAbout to get form, cookies are ". print_r($cookies,true) ."\n");
    $my_args = array('method' => 'GET', 'timeout' => '45', 'redirection' => 0, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'cookies' => $cookies);
    // Need to get from this URL and follow redirects
    $buf = wp_remote_request('https://plus.google.com/app/basic/share', $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    // need to check to see what cookies are new or updated
    // $new_cookies = array of cookies returned by post
    // $cookies = array of existing cookies sent
    $new_cookies = $buf['cookies'];
    for ($x = count($cookies); $x > 0; $x--) {
        // if cookie is already in $cookies array, we remove old version
        foreach ($new_cookies as $new_cookie) {
            if ($new_cookie->name == $cookies[$x]->name) {
                //wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
                unset($cookies[$x]);
            }
        }
    }
    // now that existing cookies are out of the array, add all back in
    foreach ($new_cookies as $cookie) {
        wpgplus_set_cookie($cookie);
        $cookies[] = $cookie;
    }
    // in case form gets redirected to a new base url
    $my_redirect = $buf['headers']['location'];
    if (substr($my_redirect, 0, 1) == '/') {
        $my_redirect = 'http://plus.google.com' . $my_redirect;
    }
    if ($my_redirect) {
        wpgplus_debug("\nShould be past redirect for form, response was " . print_r($buf, true) . "\n");
        wpgplus_debug("\nLine 430, My Redirect was " . $my_redirect . "\n");
        wpgplus_debug("\nGetting form at redirected url, cookies are " . print_r($cookies, true) . "\n");
        // need to reget the form at the new url
        $my_args = array('method' => 'GET', 'timeout' => '45', 'redirection' => 0, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'cookies' => $cookies);
        $buf = wp_remote_request($my_redirect, $my_args);
        if (is_wp_error($buf)) {
            wp_die($buf);
        }
        $new_cookies = $buf['cookies'];
        for ($x = count($cookies); $x > 0; $x--) {
            // if cookie is already in $cookies array, we remove old version
            foreach ($new_cookies as $new_cookie) {
                if ($new_cookie->name == $cookies[$x]->name) {
                    //wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
                    unset($cookies[$x]);
                }
            }
        }
        // now that existing cookies are out of the array, add all back in
        foreach ($new_cookies as $cookie) {
            wpgplus_set_cookie($cookie);
            $cookies[] = $cookie;
        }
        wpgplus_debug("\nGot new form at new url, cookies are now " . print_r($cookies, true) . "\n");
    }
    // now we get the form inputs, including hidden ones
    wpgplus_debug("\nForm being parsed is this page: " . $buf['body'] . "\n");
    $params = array();
    $doc = new DOMDocument();
    $doc->loadHTML($buf['body']);
    $inputs = $doc->getElementsByTagName('input');
    foreach ($inputs as $input) {
        if ($input->getAttribute('type') == 'hidden') {
            $params[$input->getAttribute('name')] = $input->getAttribute('value');
        }
    }
    if (function_exists('get_the_post_thumbnail') && has_post_thumbnail($my_post->ID)) {
        $my_thumb_id = get_post_thumbnail_id($my_post->ID);
        $my_thumb_array = wp_get_attachment_image_src($my_thumb_id);
        $my_image = $my_thumb_array[0];
        // this should be the url
    } else {
        $my_image = '';
    }
    /* first, lets submit the current form using the 'add image' button */
    $params['cpPostMsg'] = '';
    $params['currentPage'] = '1';
    $params['buttonPressed'] = '2';
    // pushign the second button should open the add image dialog
    $params['cpPhotoTitle'] = '';
    foreach ($params as $key => $value) {
        $post_items[] = $key . '=' . urlencode($value);
    }
    $params = implode('&', $post_items);
    // need to determine form url from the form action
    sleep(6);
    $forms = $doc->getElementsByTagName('form');
    $baseurl = 'https://plus.google.com' . $forms->item(0)->getAttribute('action');
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Posting share form, to get image form\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : and base url is ". $baseurl ."\n");
    // This last post should NOT follow redirects
    $my_args = array('method' => 'POST', 'timeout' => 45, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'redirection' => 0, 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'body' => $params, 'cookies' => $cookies, 'headers' => array('Referer' => 'https://plus.google.com/app/basic/share', 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => strlen($params)));
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : About to post form, my_args are ". print_r($my_args,true). "\n");
    $buf = wp_remote_post($baseurl, $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    $header = $buf['headers'];
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Requested form, status was ". $buf['response']['code']	. "\n");
    /* now we need to parse that form, to figure out how to send the images */
    wpgplus_debug("\nForm being parsed for image posting \n");
    $params = array();
    $doc = new DOMDocument();
    $doc->loadHTML($buf['body']);
    $inputs = $doc->getElementsByTagName('input');
    foreach ($inputs as $input) {
        if ($input->getAttribute('type') == 'hidden') {
            $params[$input->getAttribute('name')] = $input->getAttribute('value');
        }
    }
    $forms = $doc->getElementsByTagName('form');
    /* the image posting form is the third form on the page */
    $image_file = get_attached_file($my_thumb_id);
    $referer = $baseurl;
    $baseurl = 'https://plus.google.com' . $forms->item(2)->getAttribute('action');
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Image file is " . $image_file . "\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Going to post image upload form, ");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : and base url is " . $baseurl . "\n");
    $boundary = md5(time());
    $payload = '';
    // First, add the standard POST fields:
    foreach ($params as $key => $value) {
        $payload .= '--' . $boundary;
        $payload .= "\r\n";
        $payload .= 'Content-Disposition: form-data; name="' . $key . '"' . "\r\n\r\n";
        $payload .= $value;
        $payload .= "\r\n";
    }
    // Add the file
    $payload .= '--' . $boundary;
    $payload .= "\r\n";
    $payload .= 'Content-Disposition: form-data; name="photo_upload_file_name"; filename="' . basename($image_file) . '"' . "\r\n";
    $payload .= 'Content-Type: image/png' . "\r\n";
    // If you	know the mime-type
    $payload .= 'Content-Transfer-Encoding: binary' . "\r\n";
    $payload .= "\r\n";
    $payload .= file_get_contents($image_file);
    $payload .= "\r\n";
    $payload .= '--' . $boundary . '--';
    $payload .= "\r\n\r\n";
    $my_args = array('method' => 'POST', 'timeout' => 45, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'redirection' => 0, 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'body' => $payload, 'cookies' => $cookies, 'headers' => array('Referer' => $referer, 'content-type' => 'multipart/form-data; boundary=' . $boundary, 'Content-Length' => strlen($payload)));
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : About to post form for uploading image, payload is " . $payload . "\n");
    $buf = wp_remote_post($baseurl, $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    $header = $buf['headers'];
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Posted form (image upload), status was " . $buf['response']['code'] . "\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text was ". $my_post_text ."\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Header of response was ". print_r($header,true) ."\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Response was " . $buf['body'] . "\n");
    /* then we're ready to post the whole thing, including image references */
    /* really at this point we need to go get the whole original form perhaps, or at least reset the action */
    $params['cpPostMsg'] = $my_post_text . ' ' . get_permalink($my_post);
    $params['currentPage'] = '1';
    $params['buttonPressed'] = '1';
    $params['cpPhotoTitle'] = '';
    foreach ($params as $key => $value) {
        $post_items[] = $key . '=' . urlencode($value);
    }
    $params = implode('&', $post_items);
    // need to determine form url from the form action
    sleep(6);
    $forms = $doc->getElementsByTagName('form');
    $baseurl = 'https://plus.google.com' . $forms->item(0)->getAttribute('action');
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Going to publish, params is " . print_r($params, true) . "\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : and base url is " . $baseurl . "\n");
    // This last post should NOT follow redirects
    $my_args = array('method' => 'POST', 'timeout' => 45, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'redirection' => 0, 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'body' => $params, 'cookies' => $cookies, 'headers' => array('Referer' => 'https://plus.google.com/app/basic/share', 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => strlen($params)));
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : About to post form, my_args are " . print_r($my_args, true) . "\n");
    $buf = wp_remote_post($baseurl, $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    $header = $buf['headers'];
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Posted form, status was " . $buf['response']['code'] . "\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text was ". $my_post_text ."\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Header of response was ". print_r($header,true) ."\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Body was " . $buf['body'] . "\n");
    // seems like the item still isn't posted at this point?
}
コード例 #2
0
ファイル: gplus.php プロジェクト: juslee/e27
function wpgplus_update_profile_status($post_id)
{
    $wpgplus_debug_file = WP_PLUGIN_DIR . '/wpgplus/debug.txt';
    /* Set up the post */
    global $more;
    $more = 0;
    //only the post teaser please
    $my_post = get_post($post_id);
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Inside update_profile_states with post_id " . $post_id . " \n");
    if (!empty($my_post->post_password)) {
        // post is password protected, don't post
        wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Post is password protected\n");
        return;
    }
    if (get_post_type($my_post->ID) != 'post') {
        // only do this for posts
        wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Post type is " . get_post_type($my_post->ID) . "\n");
        return;
    }
    $my_post_text = get_post_meta($post_id, 'wpgplus_message', true);
    // if we have a post_message
    if ($my_post_text == '') {
        if ($my_post->post_excerpt && $my_post->post_excerpt != '') {
            $my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content', $my_post->post_excerpt)));
        } else {
            $my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content', $my_post->post_content)));
        }
    }
    if (strlen($my_post_text) >= 995) {
        $space_index = strrpos(substr($my_post_text, 0, 995), ' ');
        $short_desc = substr($my_post_text, 0, $space_index);
        $short_desc .= '...';
        $my_post_text = $short_desc;
    }
    /* Now let's go get the form */
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Getting form for posting\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text is ". $my_post_text ."\n");
    // These are the cookies I know of - not sure all are needed
    $cookies = array();
    $my_cookies = array('GAPS', 'GALX', 'NID', 'SID', 'LSID', 'HSID', 'SSID', 'APISID', 'SAPISID');
    foreach ($my_cookies as $name) {
        $new_cookie = wpgplus_get_cookie($name);
        if ($new_cookie) {
            $cookies[] = wpgplus_get_cookie($name);
        }
    }
    //wpgplus_debug("\nAbout to get form, cookies are ". print_r($cookies,true) ."\n");
    $my_args = array('method' => 'GET', 'timeout' => '45', 'redirection' => 0, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'cookies' => $cookies);
    // Need to get from this URL and follow redirects
    $buf = wp_remote_request('https://m.google.com/app/plus/x/?v=compose&group=b0&hideloc=1', $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    // need to check to see what cookies are new or updated
    // $new_cookies = array of cookies returned by post
    // $cookies = array of existing cookies sent
    $new_cookies = $buf['cookies'];
    for ($x = count($cookies); $x > 0; $x--) {
        // if cookie is already in $cookies array, we remove old version
        foreach ($new_cookies as $new_cookie) {
            if ($new_cookie->name == $cookies[$x]->name) {
                //wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
                unset($cookies[$x]);
            }
        }
    }
    // now that existing cookies are out of the array, add all back in
    foreach ($new_cookies as $cookie) {
        wpgplus_set_cookie($cookie);
        $cookies[] = $cookie;
    }
    // form gets redirected to a new base url
    $my_redirect = $buf['headers']['location'];
    //wpgplus_debug("\nShould be past redirect for form, response was ". print_r($buf,true) ."\n");
    wpgplus_debug("\nLine 415, My Redirect was " . $my_redirect . "\n");
    //wpgplus_debug("\nGetting form at redirected url, cookies are ". print_r($cookies,true) ."\n");
    // need to reget the form at the new url
    $my_args = array('method' => 'GET', 'timeout' => '45', 'redirection' => 0, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'cookies' => $cookies);
    $buf = wp_remote_request($my_redirect, $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    $new_cookies = $buf['cookies'];
    for ($x = count($cookies); $x > 0; $x--) {
        // if cookie is already in $cookies array, we remove old version
        foreach ($new_cookies as $new_cookie) {
            if ($new_cookie->name == $cookies[$x]->name) {
                //wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
                unset($cookies[$x]);
            }
        }
    }
    // now that existing cookies are out of the array, add all back in
    foreach ($new_cookies as $cookie) {
        wpgplus_set_cookie($cookie);
        $cookies[] = $cookie;
    }
    //wpgplus_debug("\nGot new form at new url, cookies are now ". print_r($cookies,true) ."\n");
    //wpgplus_debug("\nForm being parsed is in ". $buf['body'] ."\n");
    // now we get the form inputs, including hidden ones
    $params = array();
    $doc = new DOMDocument();
    $doc->loadHTML($buf['body']);
    $inputs = $doc->getElementsByTagName('input');
    foreach ($inputs as $input) {
        if ($input->getAttribute('type') == 'hidden') {
            $params[$input->getAttribute('name')] = $input->getAttribute('value');
        }
    }
    $params['newcontent'] = $my_post_text . ' ' . get_permalink($my_post);
    $params['post'] = ' Post ';
    // input type="submit" important too?
    // need to determine baseul from the last get, then add the right query string
    sleep(6);
    $baseurl = $my_redirect;
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Going to publish, params is " . print_r($params, true) . "\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : and base url is " . $baseurl . "\n");
    // This last post should NOT follow redirects
    $my_args = array('method' => 'POST', 'timeout' => 45, 'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)', 'redirection' => 0, 'blocking' => true, 'compress' => false, 'decompress' => true, 'ssl-verify' => false, 'body' => $params, 'cookies' => $cookies, 'headers' => array('Referer' => $baseurl));
    $buf = wp_remote_post($baseurl . '&a=post', $my_args);
    if (is_wp_error($buf)) {
        wp_die($buf);
    }
    $header = $buf['headers'];
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Posted form, status was " . $buf['response']['code'] . "\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text was ". $my_post_text ."\n");
    //wpgplus_debug(date("Y-m-d H:i:s",time())." : Header of response was ". print_r($header,true) ."\n");
    wpgplus_debug(date("Y-m-d H:i:s", time()) . " : Body was " . $buf['body'] . "\n");
    // seems like the item still isn't posted at this point?
}