Exemple #1
0
 public function handle_process($process, $options = array())
 {
     switch ($process) {
         case 'send_social_message':
             check_admin_referer('social_send-message');
             $message_count = 0;
             if (isset($options['social_message_id']) && (int) $options['social_message_id'] > 0 && isset($_POST['twitter_message']) && !empty($_POST['twitter_message'])) {
                 // we have a social message id, ready to send!
                 // which twitter accounts are we sending too?
                 $twitter_accounts = isset($_POST['compose_twitter_id']) && is_array($_POST['compose_twitter_id']) ? $_POST['compose_twitter_id'] : array();
                 foreach ($twitter_accounts as $twitter_account_id => $tf) {
                     if (!$tf) {
                         continue;
                     }
                     // shoulnd't happen, as checkbox shouldn't post.
                     $twitter_account = new ucm_twitter_account($twitter_account_id);
                     if ($twitter_account->get('social_twitter_id') == $twitter_account_id) {
                         // good to go! send us a message!
                         $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('social_message_id', $options['social_message_id']);
                         $twitter_message->update('summary', isset($_POST['twitter_message']) ? $_POST['twitter_message'] : '');
                         if (isset($_POST['track_links']) && $_POST['track_links']) {
                             $twitter_message->parse_links();
                         }
                         $twitter_message->update('type', 'pending');
                         $twitter_message->update('data', json_encode($_POST));
                         $twitter_message->update('user_id', get_current_user_id());
                         // do we send this one now? or schedule it later.
                         $twitter_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                         if (isset($options['send_time']) && !empty($options['send_time'])) {
                             // schedule for sending at a different time (now or in the past)
                             $twitter_message->update('message_time', $options['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']);
                         }
                         $now = time();
                         if (!$twitter_message->get('message_time') || $twitter_message->get('message_time') <= $now) {
                             // send now! otherwise we wait for cron job..
                             if ($twitter_message->send_queued(isset($_POST['debug']) && $_POST['debug'])) {
                                 $message_count++;
                             }
                         } else {
                             $message_count++;
                             if (isset($_POST['debug']) && $_POST['debug']) {
                                 echo "Message will be sent in cron job after " . print_date($twitter_message->get('message_time'), true);
                             }
                         }
                     }
                 }
             }
             return $message_count;
             break;
         case 'save_twitter_settings':
             check_admin_referer('save-twitter-settings');
             if (isset($_POST['twitter_app_api_key'])) {
                 $this->update('api_key', $_POST['twitter_app_api_key']);
             }
             if (isset($_POST['twitter_app_api_secret'])) {
                 $this->update('api_secret', $_POST['twitter_app_api_secret']);
             }
             break;
         case 'save_twitter':
             $social_twitter_id = isset($_REQUEST['social_twitter_id']) ? (int) $_REQUEST['social_twitter_id'] : 0;
             check_admin_referer('save-twitter' . $social_twitter_id);
             $twitter = new ucm_twitter_account($social_twitter_id);
             if (isset($_POST['butt_delete'])) {
                 $twitter->delete();
                 $redirect = 'admin.php?page=simple_social_inbox_twitter_settings';
             } else {
                 $twitter->save_data($_POST);
                 $social_twitter_id = $twitter->get('social_twitter_id');
                 if (isset($_POST['butt_save_reconnect'])) {
                     $redirect = $twitter->link_connect();
                 } else {
                     $redirect = $twitter->link_edit();
                 }
             }
             header("Location: {$redirect}");
             exit;
             break;
     }
 }
    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;
                }
            }
        }
    }