Example #1
0
 public function pre_menu()
 {
     if (self::is_plugin_enabled()) {
         if (module_security::has_feature_access(array('name' => 'Settings', 'module' => 'config', 'category' => 'Config', 'view' => 1, 'description' => 'view'))) {
             $this->links[] = array("name" => "Social", "p" => "social_settings", "args" => array('social_id' => false), 'holder_module' => 'config', 'holder_module_page' => 'config_admin', 'menu_include_parent' => 0);
         }
         if ($this->can_i('view', 'Social') && class_exists('ucm_twitter', false) && class_exists('ucm_facebook', false)) {
             $twitter = new ucm_twitter();
             $facebook = new ucm_facebook();
             $unread = $facebook->get_unread_count() + $twitter->get_unread_count();
             $this->links['social'] = array("name" => _l('Social') . ($unread > 0 ? " <span class='menu_label'>" . $unread . "</span>" : ''), "p" => "social_admin", 'icon_name' => 'comment-o');
         }
     }
 }
Example #2
0
    public function process()
    {
        if ("save_facebook" == $_REQUEST['_process']) {
            $social_facebook_id = isset($_REQUEST['social_facebook_id']) ? (int) $_REQUEST['social_facebook_id'] : 0;
            $facebook = new ucm_facebook_account($social_facebook_id);
            if (isset($_POST['butt_del']) && module_social::can_i('delete', 'Facebook', 'Social', 'social')) {
                if (module_form::confirm_delete('social_facebook_id', "Really delete this Facebook account from the system? All messages will be lost.", self::link_open($_REQUEST['social_facebook_id']))) {
                    $facebook->delete();
                    set_message("Facebook account deleted successfully");
                    redirect_browser(self::link_open(false));
                }
            }
            $facebook->save_data($_POST);
            $social_facebook_id = $facebook->get('social_facebook_id');
            if (isset($_POST['butt_save_connect'])) {
                $redirect = $this->link_open($social_facebook_id, false, false, 'facebook_account_connect');
            } else {
                set_message('Facebook account saved successfully');
                $redirect = $this->link_open($social_facebook_id);
            }
            redirect_browser($redirect);
            exit;
        } else {
            if ("send_facebook_message" == $_REQUEST['_process']) {
                if (module_form::check_secure_key()) {
                    $social_facebook_id = isset($_REQUEST['social_facebook_id']) ? (int) $_REQUEST['social_facebook_id'] : 0;
                    $facebook = new ucm_facebook_account($social_facebook_id);
                    if ($social_facebook_id && $facebook->get('social_facebook_id') == $social_facebook_id) {
                        // queue the message into the facebook_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;
                        /* @var $available_pages ucm_facebook_page[] */
                        $available_pages = $facebook->get('pages');
                        $send_pages = isset($_POST['compose_page_id']) && is_array($_POST['compose_page_id']) ? $_POST['compose_page_id'] : array();
                        $page_count = 0;
                        if ($send_pages) {
                            foreach ($send_pages as $facebook_page_id => $tf) {
                                if (!$tf) {
                                    continue;
                                }
                                // see if this is an available page.
                                if (isset($available_pages[$facebook_page_id])) {
                                    // push to db! then send.
                                    $facebook_message = new ucm_facebook_message($facebook, $available_pages[$facebook_page_id], false);
                                    $facebook_message->create_new();
                                    $facebook_message->update('social_facebook_page_id', $available_pages[$facebook_page_id]->get('social_facebook_page_id'));
                                    $facebook_message->update('social_facebook_id', $facebook->get('social_facebook_id'));
                                    $facebook_message->update('summary', isset($_POST['message']) ? $_POST['message'] : '');
                                    $facebook_message->update('type', 'pending');
                                    $facebook_message->update('link', isset($_POST['link']) ? $_POST['link'] : '');
                                    $facebook_message->update('data', json_encode($_POST));
                                    $facebook_message->update('user_id', module_security::get_loggedin_id());
                                    // do we send this one now? or schedule it later.
                                    $facebook_message->update('status', _SOCIAL_MESSAGE_STATUS_PENDINGSEND);
                                    if ($send_time) {
                                        // schedule for sending at a different time (now or in the past)
                                        $facebook_message->update('last_active', $send_time);
                                    } else {
                                        // send it now.
                                        $facebook_message->update('last_active', 0);
                                    }
                                    if (isset($_FILES['picture']['tmp_name']) && is_uploaded_file($_FILES['picture']['tmp_name'])) {
                                        $facebook_message->add_attachment($_FILES['picture']['tmp_name']);
                                    }
                                    $facebook_message->send_queued(isset($_POST['debug']));
                                    $page_count++;
                                } else {
                                    // log error?
                                }
                            }
                        }
                        set_message(_l('Message delivered successfully to %s Facebook pages', $page_count));
                        $redirect = $this->link_open_message_view($social_facebook_id);
                        redirect_browser($redirect);
                    }
                }
            } else {
                if ("ajax_facebook_url_info" == $_REQUEST['_process']) {
                    header('Content-type: text/javascript');
                    $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : false;
                    if (strlen($url) > 4 && preg_match('#https?://#', $url)) {
                        // pass this into graph api debugger to get some information back about the URL
                        $facebook = new ucm_facebook();
                        $data = $facebook->get_url_info($url);
                        // return the data formatted in json ready to be added into the relevant input boxes.
                        $data['link_picture'] = isset($data['image'][0]['url']) ? $data['image'][0]['url'] : '';
                        $data['link_name'] = isset($data['title']) ? $data['title'] : '';
                        $data['link_caption'] = isset($data['caption']) ? $data['caption'] : '';
                        $data['link_description'] = isset($data['description']) ? $data['description'] : '';
                        echo json_encode($data);
                    }
                    exit;
                } else {
                    if ("ajax_social_facebook" == $_REQUEST['_process']) {
                        // ajax functions from wdsocial. copied from the datafeed.php sample files.
                        header('Content-type: text/javascript');
                        if (module_form::check_secure_key()) {
                            // todo: check user has access to this message.
                            $social_facebook_id = isset($_REQUEST['social_facebook_id']) ? (int) $_REQUEST['social_facebook_id'] : 0;
                            //$facebook = new ucm_facebook_account($social_facebook_id);
                            //if($social_facebook_id && $facebook->get('social_facebook_id') == $social_facebook_id){
                            $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : false;
                            $message_id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
                            $facebook_message = new ucm_facebook_message();
                            $facebook_message->load($message_id);
                            //if($facebook_message->get('social_facebook_id') == $social_facebook_id){
                            switch ($action) {
                                case "send-message-reply":
                                    if (module_social::can_i('create', 'Facebook Comments', 'Social', 'social')) {
                                        $return = array();
                                        $message = isset($_POST['message']) && $_POST['message'] ? $_POST['message'] : '';
                                        $facebook_id = isset($_POST['facebook_id']) && $_POST['facebook_id'] ? $_POST['facebook_id'] : false;
                                        $debug = isset($_POST['debug']) && $_POST['debug'] ? $_POST['debug'] : false;
                                        if ($message) {
                                            if ($debug) {
                                                ob_start();
                                            }
                                            $facebook_message->send_reply($facebook_id, $message, $debug);
                                            if ($debug) {
                                                $return['message'] = ob_get_clean();
                                            } else {
                                                // todo - option to ask the user if they want to archive a message during the send.
                                                set_message(_l('Message sent and conversation archived.'));
                                                if ($social_facebook_id) {
                                                    $return['redirect'] = module_social_facebook::link_open_message_view($social_facebook_id);
                                                } else {
                                                    // return to the 'combined' view:
                                                }
                                            }
                                        }
                                        echo json_encode($return);
                                    }
                                    break;
                                case "set-answered":
                                    if (module_social::can_i('edit', 'Facebook Comments', 'Social', 'social')) {
                                        $facebook_message->update('status', _SOCIAL_MESSAGE_STATUS_ANSWERED);
                                        ?>

									$('.facebook_message_row[data-id=<?php 
                                        echo $message_id;
                                        ?>
]').hide();
									<?php 
                                    }
                                    break;
                                case "set-unanswered":
                                    if (module_social::can_i('edit', 'Facebook Comments', 'Social', 'social')) {
                                        $facebook_message->update('status', _SOCIAL_MESSAGE_STATUS_UNANSWERED);
                                        ?>

					                $('.facebook_message_row[data-id=<?php 
                                        echo $message_id;
                                        ?>
]').hide();
					                <?php 
                                    }
                                    break;
                            }
                            //echo 'The status is '.$facebook_message->get('status');
                            //}
                        }
                        // }
                        exit;
                    }
                }
            }
        }
    }
Example #3
0
 public function send_reply($facebook_id, $message, $debug = false)
 {
     if ($this->facebook_account && $this->facebook_page && $this->social_facebook_message_id) {
         $access_token = $this->facebook_page->get('facebook_token');
         // get the machine id from the parent facebook_account
         $machine_id = $this->facebook_account->get('machine_id');
         if (!$facebook_id) {
             $facebook_id = $this->get('facebook_id');
         }
         if ($debug) {
             echo "Sending a reply to facebook ID: {$facebook_id} <br>\n";
         }
         if ($debug) {
             echo "Type: " . $this->get('type') . " <br>\n";
         }
         $result = false;
         switch ($this->get('type')) {
             case 'conversation':
                 // do we reply to the previous comment?
                 // nah for now we just add it to the bottom of the list.
                 $facebook = new ucm_facebook();
                 $result = $facebook->graph_post('' . $facebook_id . '/messages', array('message' => $message, 'access_token' => $access_token, 'machine_id' => $machine_id));
                 if ($debug) {
                     echo "Graph Post Result: <br>\n" . var_export($result, true) . " <br>\n";
                 }
                 // reload this message and comments from the graph api.
                 $this->load_by_facebook_id($this->get('facebook_id'), false, $this->get('type'), $debug);
                 break;
                 break;
             default:
                 // do we reply to the previous comment?
                 // nah for now we just add it to the bottom of the list.
                 $facebook = new ucm_facebook();
                 $result = $facebook->graph_post('' . $facebook_id . '/comments', array('message' => $message, 'access_token' => $access_token, 'machine_id' => $machine_id));
                 if ($debug) {
                     echo "Graph Post Result: <br>\n" . var_export($result, true) . " <br>\n";
                 }
                 // reload this message and comments from the graph api.
                 $this->load_by_facebook_id($this->get('facebook_id'), false, $this->get('type'), $debug);
                 break;
         }
         // hack to add the 'user_id' of who created this reply to the db for logging.
         if ($result && isset($result['id'])) {
             // find this comment id in our facebook comment database.
             $exists = get_single('social_facebook_message_comment', array('facebook_id', 'social_facebook_message_id'), array($result['id'], $this->social_facebook_message_id));
             if ($exists && $exists['social_facebook_message_comment_id']) {
                 // it really should exist after we've done the 'load_by_facebook_id' above. however with a post with lots of comments it may not appear without graph api pagination.
                 // todo - pagination!
                 update_insert('social_facebook_message_comment_id', $exists['social_facebook_message_comment_id'], 'social_facebook_message_comment', array('user_id' => module_security::get_loggedin_id()));
             }
         }
     }
 }