예제 #1
0
/** 
 * Copyright: dtbaker 2012
 * Licence: Please check CodeCanyon.net for licence details. 
 * More licence clarification available here:  http://codecanyon.net/wiki/support/legal-terms/licensing-terms/ 
 * Deploy: 9809 f200f46c2a19bb98d112f2d32a8de0c4
 * Envato: 4ffca17e-861e-4921-86c3-8931978c40ca
 * Package Date: 2015-11-25 02:55:20 
 * IP Address: 67.79.165.254
 */
$social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
$social_twitter_message_id = isset($_REQUEST['social_twitter_message_id']) ? (int) $_REQUEST['social_twitter_message_id'] : 0;
if ($social_twitter_id && $social_twitter_message_id && module_social::can_i('view', 'Twitter Comments', 'Social', 'social')) {
    $twitter = new ucm_twitter_account($social_twitter_id);
    if ($social_twitter_id && $twitter->get('social_twitter_id') == $social_twitter_id) {
        $twitter_message = new ucm_twitter_message($twitter, $social_twitter_message_id);
        if ($social_twitter_message_id && $twitter_message->get('social_twitter_message_id') == $social_twitter_message_id && $twitter_message->get('social_twitter_id') == $social_twitter_id) {
            $module->page_title = $twitter->get('twitter_name');
            $twitter_message->mark_as_read();
            ?>
			<form action="" method="post" id="twitter_edit_form">
				<div id="twitter_message_header">
					<div style="float:right; text-align: right; margin-top:-4px;">
						<small><?php 
            echo print_date($twitter_message->get('message_time'), true);
            ?>
 </small><br/>
						<?php 
            if (module_social::can_i('edit', 'Twitter Comments', 'Social', 'social')) {
                ?>
						    <?php 
예제 #2
0
 public function send_queued($debug = false)
 {
     if ($this->twitter_account && $this->social_twitter_message_id) {
         // send this message out to twitter.
         // this is run when user is composing a new message from the UI,
         if ($this->get('status') == _SOCIAL_MESSAGE_STATUS_SENDING) {
             return;
         }
         // dont double up on cron.
         $this->update('status', _SOCIAL_MESSAGE_STATUS_SENDING);
         $user_post_data = @json_decode($this->get('data'), true);
         if ($debug) {
             echo "Sending a new message to twitter account ID: " . $this->twitter_account->get('twitter_name') . " <br>\n";
         }
         $result = false;
         $tmhOAuth = $this->twitter_account->get_api();
         $post_data = array('status' => $this->get('summary'));
         $reply_message = false;
         if ($this->get('reply_to_id')) {
             $reply_message = new ucm_twitter_message(false, $this->get('reply_to_id'));
             if ($reply_message && $reply_message->get('twitter_message_id')) {
                 $post_data['in_reply_to_status_id'] = $reply_message->get('twitter_message_id');
             } else {
                 $reply_message = false;
             }
         }
         // todo: message or link are required.
         $now = time();
         $send_time = $this->get('message_time');
         if ($reply_message && $reply_message->get('type') == _TWITTER_MESSAGE_TYPE_DIRECT) {
             // send a direct reply, not a public tweet
             // a hack for DM, we dont reply to the most recent message (because we could reply to ourselves) we reply to the most recent message with a different author.
             $send_dm_to_id = $reply_message->get('twitter_from_id');
             if (!$send_dm_to_id) {
                 echo "Failed, no DM reply ID ";
                 $this->delete();
                 return false;
             }
             $our_twitter_id = preg_replace('#[^0-9]#', '', $this->twitter_account->get('twitter_id'));
             if ($our_twitter_id && $send_dm_to_id == $our_twitter_id) {
                 // dont reply to ourselves!
                 $to = preg_replace('#[^0-9]#', '', $reply_message->get('twitter_to_id'));
                 if ($our_twitter_id != $to) {
                     $send_dm_to_id = $to;
                 }
             }
             if ($debug) {
                 echo "Posting to 1.1/direct_messagse/new to user id " . $send_dm_to_id . "   <br>";
             }
             $this->update('type', _TWITTER_MESSAGE_TYPE_DIRECT);
             $twitter_code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/direct_messages/new'), 'params' => array('user_id' => $send_dm_to_id, 'text' => $this->get('summary'))));
             if ($twitter_code == 200) {
                 $result = json_decode($tmhOAuth->response['response'], true);
             } else {
                 $result = false;
             }
         } else {
             //if($post_data['in_reply_to_status_id']){
             $this->update('type', _TWITTER_MESSAGE_TYPE_MYTWEET);
             //}
             if (isset($user_post_data['twitter_post_type']) && $user_post_data['twitter_post_type'] == 'picture' && !empty($this->attachment_name) && is_file($this->attachment_name)) {
                 // we're posting a photo! change the post source from /feed to /photos
                 //$post_data['source'] = new CURLFile($this->attachment_name, 'image/jpg'); //'@'.$this->attachment_name;
                 $post_data['media[]'] = file_get_contents($this->attachment_name);
                 if ($debug) {
                     echo "Posting to 1.1/statuses/update_with_media with data: <br>";
                     print_r($post_data);
                 }
                 $twitter_code = $tmhOAuth->user_request(array('method' => 'POST', 'multipart' => true, 'url' => $tmhOAuth->url('1.1/statuses/update_with_media'), 'params' => $post_data));
                 if ($twitter_code == 200) {
                     $result = json_decode($tmhOAuth->response['response'], true);
                 } else {
                     $result = false;
                 }
             } else {
                 if ($debug) {
                     echo "Posting to 1.1/statuses/update with data: <br>";
                     print_r($post_data);
                 }
                 $twitter_code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => $post_data));
                 if ($twitter_code == 200) {
                     $result = json_decode($tmhOAuth->response['response'], true);
                 } else {
                     $result = false;
                 }
             }
         }
         if ($debug) {
             echo "API Post Result: <br>\n" . var_export($result, true) . " <br>\n";
         }
         if ($result && isset($result['id_str'])) {
             $this->update('twitter_message_id', $result['id_str']);
             // reload this message and comments from the graph api.
             $this->load_by_twitter_id($this->get('twitter_message_id'), false, $this->get('type') == _TWITTER_MESSAGE_TYPE_DIRECT ? _TWITTER_MESSAGE_TYPE_DIRECT : false, $debug, true);
         } else {
             echo 'Failed to send message. Error was: ' . var_export($tmhOAuth->response['response'], true);
             // remove from database.
             $this->delete();
             return false;
         }
         // successfully sent, mark is as answered.
         $this->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
         if ($reply_message) {
             //archive the message we replied to as well
             $reply_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
         }
         return true;
     }
     return false;
 }
예제 #3
0
    public function process()
    {
        if ("save_twitter" == $_REQUEST['_process']) {
            $social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
            $twitter = new ucm_twitter_account($social_twitter_id);
            if (isset($_POST['butt_del']) && module_social::can_i('delete', 'Twitter', 'Social', 'social')) {
                if (module_form::confirm_delete('social_twitter_id', "Really delete this Twitter account from the system? All messages will be lost.", self::link_open($_REQUEST['social_twitter_id']))) {
                    $twitter->delete();
                    set_message("Twitter account deleted successfully");
                    redirect_browser(self::link_open(false));
                }
            }
            $twitter->save_data($_POST);
            $social_twitter_id = $twitter->get('social_twitter_id');
            if (isset($_POST['butt_save_connect'])) {
                $redirect = $this->link_open($social_twitter_id, false, false, 'twitter_account_connect');
            } else {
                set_message('Twitter account saved successfully');
                $redirect = $this->link_open($social_twitter_id);
            }
            redirect_browser($redirect);
            exit;
        } else {
            if ("send_twitter_message" == $_REQUEST['_process']) {
                if (module_form::check_secure_key()) {
                    // queue the message into the twitter_message table
                    // if there's a scheduled date in the past we send it in the past, no date we send straight away, date in the future we leave it in the db table for the cron job to pick up.
                    //print_r($_POST);exit;
                    $send_time = false;
                    // default: now
                    if (isset($_POST['schedule_date']) && isset($_POST['schedule_time']) && !empty($_POST['schedule_date']) && !empty($_POST['schedule_time'])) {
                        $date = $_POST['schedule_date'];
                        $time_hack = $_POST['schedule_time'];
                        $time_hack = str_ireplace('am', '', $time_hack);
                        $time_hack = str_ireplace('pm', '', $time_hack);
                        $bits = explode(':', $time_hack);
                        if (strpos($_POST['schedule_time'], 'pm')) {
                            $bits[0] += 12;
                        }
                        // add the time if it exists
                        $date .= ' ' . implode(':', $bits) . ':00';
                        $send_time = strtotime(input_date($date, true));
                    } else {
                        if (isset($_POST['schedule_date']) && !empty($_POST['schedule_date'])) {
                            $send_time = strtotime(input_date($_POST['schedule_date'], true));
                        }
                    }
                    //echo print_date($send_time,true);
                    //echo '<br>';
                    //echo date('c',$send_time);
                    //exit;
                    $send_accounts = isset($_POST['compose_account_id']) && is_array($_POST['compose_account_id']) ? $_POST['compose_account_id'] : array();
                    $page_count = 0;
                    $last_twitter_account_id = false;
                    if ($send_accounts) {
                        foreach ($send_accounts as $twitter_account_id => $tf) {
                            if (!$tf) {
                                continue;
                            }
                            // see if this is an available account.
                            $twitter_account = new ucm_twitter_account($twitter_account_id);
                            //todo: check permissiont o access thi saccount
                            if ($twitter_account->get('social_twitter_id') == $twitter_account_id) {
                                // push to db! then send.
                                $last_twitter_account_id = $twitter_account_id;
                                $twitter_message = new ucm_twitter_message($twitter_account, false);
                                $twitter_message->create_new();
                                $twitter_message->update('social_twitter_id', $twitter_account->get('social_twitter_id'));
                                $twitter_message->update('summary', isset($_POST['message']) ? $_POST['message'] : '');
                                $twitter_message->update('type', 'pending');
                                $twitter_message->update('data', json_encode($_POST));
                                $twitter_message->update('user_id', module_security::get_loggedin_id());
                                // do we send this one now? or schedule it later.
                                $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                if ($send_time) {
                                    // schedule for sending at a different time (now or in the past)
                                    $twitter_message->update('message_time', $send_time);
                                } else {
                                    // send it now.
                                    $twitter_message->update('message_time', 0);
                                }
                                if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                    $twitter_message->add_attachment($_FILES['picture']['tmp_name']);
                                }
                                $twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug']);
                                $page_count++;
                            } else {
                                // log error?
                            }
                        }
                    }
                    set_message(_l('Message delivered successfully to %s Twitter accounts', $page_count));
                    $redirect = $this->link_open_message_view($last_twitter_account_id);
                    redirect_browser($redirect);
                }
                exit;
            } else {
                if ("ajax_social_twitter" == $_REQUEST['_process']) {
                    // ajax functions from wdsocial. copied from the datafeed.php sample files.
                    header('Content-type: text/javascript');
                    if (module_form::check_secure_key()) {
                        $social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
                        $twitter = new ucm_twitter_account($social_twitter_id);
                        if ($social_twitter_id && $twitter->get('social_twitter_id') == $social_twitter_id) {
                            $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : false;
                            $message_id = isset($_REQUEST['social_twitter_message_id']) ? (int) $_REQUEST['social_twitter_message_id'] : 0;
                            $twitter_message = new ucm_twitter_message();
                            $twitter_message->load($message_id);
                            if ($twitter_message->get('social_twitter_id') == $social_twitter_id && $twitter_message->get('social_twitter_message_id') == $message_id) {
                                switch ($action) {
                                    case "send-message-reply":
                                        if (module_social::can_i('create', 'Twitter Comments', 'Social', 'social')) {
                                            $return = array();
                                            $message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : '';
                                            $debug = isset($_POST['debug']) && $_POST['debug'] ? $_POST['debug'] : false;
                                            if ($message) {
                                                ob_start();
                                                //$twitter_message->send_reply( $message, $debug );
                                                $new_twitter_message = new ucm_twitter_message($twitter, false);
                                                $new_twitter_message->create_new();
                                                $new_twitter_message->update('reply_to_id', $twitter_message->get('social_twitter_message_id'));
                                                $new_twitter_message->update('social_twitter_id', $twitter->get('social_twitter_id'));
                                                $new_twitter_message->update('summary', $message);
                                                //$new_twitter_message->update('type','pending');
                                                $new_twitter_message->update('data', json_encode($_POST));
                                                $new_twitter_message->update('user_id', module_security::get_loggedin_id());
                                                // do we send this one now? or schedule it later.
                                                $new_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                                if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                                    $new_twitter_message->add_attachment($_FILES['picture']['tmp_name']);
                                                }
                                                $worked = $new_twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug']);
                                                $return['message'] = ob_get_clean();
                                                if ($debug) {
                                                    // just return message
                                                } else {
                                                    if ($worked) {
                                                        // success, redicet!
                                                        set_message(_l('Message sent and conversation archived.'));
                                                        $return['redirect'] = module_social_twitter::link_open_message_view($social_twitter_id);
                                                    } else {
                                                        // failed, no debug, force debug and show error.
                                                    }
                                                }
                                            }
                                            echo json_encode($return);
                                        }
                                        break;
                                    case "set-answered":
                                        if (module_social::can_i('edit', 'Twitter Comments', 'Social', 'social')) {
                                            $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                            ?>

									$('.twitter_message_row[data-id=<?php 
                                            echo $message_id;
                                            ?>
]').hide();
									<?php 
                                            // if this is a direct message, we also archive all other messages in it.
                                            if ($twitter_message->get('type') == _TWITTER_MESSAGE_TYPE_DIRECT) {
                                                $from = preg_replace('#[^0-9]#', '', $twitter_message->get('twitter_from_id'));
                                                $to = preg_replace('#[^0-9]#', '', $twitter_message->get('twitter_to_id'));
                                                if ($from && $to) {
                                                    $sql = "SELECT * FROM `" . _DB_PREFIX . "social_twitter_message` WHERE `type` = " . _TWITTER_MESSAGE_TYPE_DIRECT . " AND `status` = " . (int) _SOCIAL_MESSAGE_STATUS_UNANSWERED . " AND social_twitter_id = " . (int) $twitter_message->get('twitter_account')->get('social_twitter_id') . " AND ( (`twitter_from_id` = '{$from}' AND `twitter_to_id` = '{$to}') OR (`twitter_from_id` = '{$to}' AND `twitter_to_id` = '{$from}') ) ";
                                                    $others = qa($sql);
                                                    if (count($others)) {
                                                        foreach ($others as $other_message) {
                                                            $ucm_twitter_message = new ucm_twitter_message(false, $other_message['social_twitter_message_id']);
                                                            if ($ucm_twitter_message->get('social_twitter_message_id') == $other_message['social_twitter_message_id']) {
                                                                $ucm_twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                                                ?>

														$('.twitter_message_row[data-id=<?php 
                                                                echo $ucm_twitter_message->get('social_twitter_message_id');
                                                                ?>
]').hide();
													<?php 
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                    case "set-unanswered":
                                        if (module_social::can_i('edit', 'Twitter Comments', 'Social', 'social')) {
                                            $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_UNANSWERED);
                                            ?>

					                $('.twitter_message_row[data-id=<?php 
                                            echo $message_id;
                                            ?>
]').hide();
					                <?php 
                                        }
                                        break;
                                }
                                //echo 'The status is '.$twitter_message->get('status');
                            }
                        }
                    }
                    exit;
                }
            }
        }
    }