예제 #1
0
파일: fb.api.php 프로젝트: JalpMi/v2contact
 function doPostToNT($options, $message)
 {
     require_once 'apis/facebook.php';
     $badOut = array('pgID' => '', 'isPosted' => 0, 'pDate' => date('Y-m-d H:i:s'), 'Error' => '');
     // prr($message); prr($options);
     //## Check settings
     if (!is_array($options)) {
         $badOut['Error'] = 'No Options';
         return $badOut;
     }
     if (!isset($options['fbAppAuthToken']) || trim($options['fbAppAuthToken']) == '') {
         $badOut['Error'] = 'No Auth Token Found';
         return $badOut;
     }
     //## Make Post
     $facebook = new NXS_Facebook(array('appId' => $options['fbAppID'], 'secret' => $options['fbAppSec'], 'cookie' => true));
     if (!isset($options['fbAppPageAuthToken']) || trim($options['fbAppPageAuthToken']) == '') {
         $options['fbAppPageAuthToken'] = $options['fbAppAuthToken'];
     }
     //## Some OLD Format Conversion
     if (!isset($options['attachType']) && isset($options['fbAttch'])) {
         $options['attachType'] = $options['fbAttch'];
     }
     if (!isset($options['postType']) && isset($options['fbPostType'])) {
         $options['postType'] = $options['fbPostType'];
     }
     if (!isset($options['pgID']) && isset($options['fbPgID'])) {
         $options['pgID'] = $options['fbPgID'];
     }
     $msg = $message['message'];
     $imgURL = $message['imageURL'];
     $fbPostType = $options['postType'];
     $fbWhere = 'feed';
     $attachType = $options['attachType'];
     if ($attachType == '1') {
         $attachType = 'A';
     } else {
         $attachType = 'S';
     }
     if ($options['imgUpl'] != '2') {
         $options['imgUpl'] = 'T';
     } else {
         $options['imgUpl'] = 'A';
     }
     $page_id = $options['pgID'];
     $mssg = array('access_token' => $options['fbAppPageAuthToken'], 'message' => $msg);
     if ($fbPostType == 'I' && trim($imgURL) == '') {
         $fbPostType = 'T';
     }
     if ($fbPostType == 'A' || $fbPostType == '') {
         if ($attachType == 'A' || $attachType == 'S') {
             $attArr = array('name' => $message['title'], 'caption' => $message['siteName'], 'link' => $message['link'], 'description' => $message['description']);
             $mssg = array_merge($mssg, $attArr);
         }
         if ($attachType == 'A') {
             $mssg['actions'] = array(array('name' => $message['siteName'], 'link' => $message['link']));
         }
         if (trim($imgURL) != '') {
             $mssg['picture'] = $imgURL;
         }
         if (trim($message['videoURL']) != '') {
             $mssg['source'] = $message['videoURL'];
         }
     } elseif ($fbPostType == 'I') {
         $facebook->setFileUploadSupport(true);
         $fbWhere = 'photos';
         $mssg['url'] = $imgURL;
         if ($options['imgUpl'] == 'T') {
             //## Try to Post to TImeline
             $aacct = array('access_token' => $options['fbAppPageAuthToken']);
             $albums = $facebook->api("/{$page_id}/albums", "get", $aacct);
             foreach ($albums["data"] as $album) {
                 if ($album["type"] == "wall") {
                     $chosen_album = $album;
                     break;
                 }
             }
             if (isset($chosen_album) && isset($chosen_album["id"])) {
                 $page_id = $chosen_album["id"];
             }
         }
     }
     //prr($message); prr($mssg); prr($options); //die();
     try {
         $ret = $facebook->api("/{$page_id}/" . $fbWhere, "post", $mssg);
     } catch (NXS_FacebookApiException $e) {
         $badOut['Error'] = ' [ERROR]' . $e->getMessage() . "<br/>\n";
         if (stripos($e->getMessage(), 'This API call requires a valid app_id') !== false) {
             if (!is_numeric($page_id) && stripos($options['fbURL'], '/groups/') != false) {
                 $badOut['Error'] .= ' [ERROR] Unrecognized Facebook Group ID. Please use numeric ID.';
             } else {
                 $badOut['Error'] .= " [ERROR] (invalid app_id) Authorization Error. <br/>\r\n<br/>\r\n Possible Reasons: <br/>\r\n 1. Your app is not authorized. Please go to the Plugin Settings - Facebook and authorize it.<br/>\r\n 2. The current authorized user have no rights to post to the specified page. Please login to Facebook as the correct user and Re-Authorize the Plugin.<br/>\r\n 3. You clicked 'Skip' or unchecked the 'Manage Pages' or 'Post on your behalf' permissions when Authorization wizard asked you. Please Re-Authorize the Plugin<br/>\r\n";
             }
         }
     }
     if (isset($ret['id']) && $ret['id'] != '') {
         $pgID = isset($ret['post_id']) && strpos($ret['post_id'], '_') !== false ? $ret['post_id'] : $ret['id'];
         $pgg = explode('_', $pgID);
         $postID = $pgg[1];
         $pgURL = 'http://www.facebook.com/' . $options['pgID'] . '/posts/' . $postID;
         return array('isPosted' => '1', 'postID' => $pgID, 'postURL' => $pgURL, 'pDate' => date('Y-m-d H:i:s'));
     } else {
         return $badOut;
     }
 }
예제 #2
0
파일: fb.php 프로젝트: JalpMi/v2contact
        function showGenNTSettings($ntOpts)
        {
            global $nxs_snapThisPageUrl, $nxs_plurl;
            $ntInfo = array('code' => 'FB', 'lcode' => 'fb', 'name' => 'Facebook', 'defNName' => 'dlUName', 'tstReq' => true);
            if (isset($_GET['code']) && $_GET['code'] != '' && (isset($_GET['auth']) && $_GET['auth'] == 'fb') && (!isset($_GET['action']) || $_GET['action'] != 'gPlusAuth')) {
                $at = $_GET['code'];
                echo "-= This is normal technical authorization info that will dissapear (Unless you get some errors) =- <br/><br/><br/>";
                //$fbo = array('wfa'=> 1339160000); //foreach ($ntOpts as $two) { if (isset($two['wfa']) && $two['wfa']>$fbo['wfa']) $fbo =  $two; }
                $fbo = $ntOpts[$_GET['acc']];
                $wprg = array();
                $response = wp_remote_get('https://graph.facebook.com/nextscripts', $wprg);
                if (is_wp_error($response) && isset($response->errors['http_request_failed']) && stripos($response->errors['http_request_failed'][0], 'SSL') !== false) {
                    prr($response->errors);
                    $wprg['sslverify'] = false;
                }
                if (isset($fbo['fbPgID'])) {
                    echo "-=";
                    prr($fbo);
                    // die();
                    $response = wp_remote_get('https://graph.facebook.com/oauth/access_token?client_id=' . $fbo['fbAppID'] . '&redirect_uri=' . urlencode($nxs_snapThisPageUrl . '&auth=fb&acc=' . $_GET['acc']) . '&client_secret=' . $fbo['fbAppSec'] . '&code=' . $at, $wprg);
                    //prr('https://graph.facebook.com/oauth/access_token?client_id='.$fbo['fbAppID'].'&redirect_uri='.urlencode($nxs_snapThisPageUrl).'&client_secret='.$fbo['fbAppSec'].'&code='.$at);
                    if (is_object($response) && isset($response->errors) || is_array($response) && stripos($response['body'], '"error":') !== false) {
                        prr($response);
                        die;
                    }
                    parse_str($response['body'], $params);
                    $at = $params['access_token'];
                    prr($response);
                    prr($params);
                    $response = wp_remote_get('https://graph.facebook.com/oauth/access_token?client_secret=' . $fbo['fbAppSec'] . '&client_id=' . $fbo['fbAppID'] . '&grant_type=fb_exchange_token&fb_exchange_token=' . $at, $wprg);
                    if (is_object($response) && isset($response->errors)) {
                        prr($response);
                        die;
                    }
                    parse_str($response['body'], $params);
                    $at = $params['access_token'];
                    $fbo['fbAppAuthToken'] = $at;
                    require_once 'apis/facebook.php';
                    echo "-= Using API =-<br/>";
                    $facebook = new NXS_Facebook(array('appId' => $fbo['fbAppID'], 'secret' => $fbo['fbAppSec'], 'cookie' => true));
                    $facebook->setAccessToken($fbo['fbAppAuthToken']);
                    $user = $facebook->getUser();
                    echo "USER:"******"-= Authorizing Page =-";
                            if (!is_numeric($page_id) && stripos($fbo['fbURL'], '/groups/') != false) {
                                //$fbPgIDR = wp_remote_get('nxs.php?g='.$fbo['fbURL']); // TODO - how to replace
                                $fbPgIDR = trim($fbPgIDR['body']);
                                $page_id = $fbPgIDR != '' ? $fbPgIDR : $page_id;
                            }
                            $page_info = $facebook->api("/{$page_id}?fields=access_token");
                            if (!empty($page_info['access_token'])) {
                                $fbo['fbAppPageAuthToken'] = $page_info['access_token'];
                            }
                        } catch (NXS_FacebookApiException $e) {
                            $errMsg = $e->getMessage();
                            prr($errMsg);
                            if (stripos($errMsg, 'Unknown fields: access_token') !== false) {
                                $fbo['fbAppPageAuthToken'] = $fbo['fbAppAuthToken'];
                            } else {
                                if (stripos($errMsg, 'Unsupported get request') !== false) {
                                    echo "<b style='color:red;'>Error </b>: Your Facebook URL ( <i>" . $fbo['fbURL'] . "</i> ) is either incorrect or authorzing user don't have rights to post there.<br/>";
                                }
                                echo 'Reported Error: ', $errMsg, "\n";
                                die;
                            }
                        }
                    } else {
                        echo "Can't get Facebook User. Please login to Facebook.";
                    }
                    if ($user > 0) {
                        $fbo['fbAppAuthUser'] = $user;
                        $optionsG = get_option('NS_SNAutoPoster');
                        $optionsG['fb'][$_GET['acc']] = $fbo;
                        update_option('NS_SNAutoPoster', $optionsG);
                        ?>
<script type="text/javascript">window.location = "<?php 
                        echo $nxs_snapThisPageUrl;
                        ?>
"</script>      
      <?php 
                    }
                    die;
                }
            }
            ?>
    
    <div class="nxs_box">
      <div class="nxs_box_header"> 
        <div class="nsx_iconedTitle" style="margin-bottom:1px;background-image:url(<?php 
            echo $nxs_plurl;
            ?>
img/<?php 
            echo $ntInfo['lcode'];
            ?>
16.png);"><?php 
            echo $ntInfo['name'];
            ?>
          <?php 
            $cbo = count($ntOpts);
            ?>
 <?php 
            wp_nonce_field('ns' . $ntInfo['code'], 'ns' . $ntInfo['code'] . '_wpnonce');
            ?>
          <?php 
            if ($cbo > 1) {
                ?>
<div class="nsBigText"><?php 
                echo "(" . ($cbo == '0' ? 'No' : $cbo) . " ";
                _e('accounts', 'nxs_snap');
                echo ")";
                ?>
</div><?php 
            }
            ?>
        </div>
      </div>
      <div class="nxs_box_inside">
        <?php 
            foreach ($ntOpts as $indx => $pbo) {
                if (trim($pbo['nName'] == '')) {
                    $pbo['nName'] = str_ireplace('https://www.facebook.com', '', str_ireplace('http://www.facebook.com', '', $pbo['fbURL']));
                }
                if (!isset($pbo[$ntInfo['lcode'] . 'OK']) || $pbo[$ntInfo['lcode'] . 'OK'] == '') {
                    $pbo[$ntInfo['lcode'] . 'OK'] = isset($pbo['fbAppAuthUser']) && $pbo['fbAppAuthUser'] > 1 ? '1' : '';
                }
                ?>
          <p style="margin:0px;margin-left:5px;">
            <input value="1" name="<?php 
                echo $ntInfo['lcode'];
                ?>
[<?php 
                echo $indx;
                ?>
][apDo<?php 
                echo $ntInfo['code'];
                ?>
]" onchange="doShowHideBlocks('<?php 
                echo $ntInfo['code'];
                ?>
');" type="checkbox" <?php 
                if ((int) $pbo['do' . $ntInfo['code']] == 1) {
                    echo "checked";
                }
                ?>
 /> <?php 
                if ((int) $pbo['catSel'] == 1) {
                    ?>
   <span onmouseout="nxs_hidePopUpInfo('popOnlyCat');" onmouseover="nxs_showPopUpInfo('popOnlyCat', event);"><?php 
                    echo "*[" . (substr_count($pbo['catSelEd'], ",") + 1) . "]*";
                    ?>
</span><?php 
                }
                ?>
            <strong><?php 
                _e('Auto-publish to', 'nxs_snap');
                ?>
 <?php 
                echo $ntInfo['name'];
                ?>
 <i style="color: #005800;"><?php 
                if ($pbo['nName'] != '') {
                    echo "(" . $pbo['nName'] . ")";
                }
                ?>
</i></strong>
          &nbsp;&nbsp;<?php 
                if ($ntInfo['tstReq'] && (!isset($pbo[$ntInfo['lcode'] . 'OK']) || $pbo[$ntInfo['lcode'] . 'OK'] == '')) {
                    ?>
<b style="color: #800000"><?php 
                    _e('Attention requred. Unfinished setup', 'nxs_snap');
                    ?>
 ==&gt;</b><?php 
                }
                ?>
<a id="do<?php 
                echo $ntInfo['code'] . $indx;
                ?>
A" href="#" onclick="doShowHideBlocks2('<?php 
                echo $ntInfo['code'] . $indx;
                ?>
');return false;">[<?php 
                _e('Show Settings', 'nxs_snap');
                ?>
]</a>&nbsp;&nbsp;
          <a href="#" onclick="doDelAcct('<?php 
                echo $ntInfo['lcode'];
                ?>
', '<?php 
                echo $indx;
                ?>
', '<?php 
                if (isset($pbo['bgBlogID'])) {
                    echo $pbo['nName'];
                }
                ?>
');return false;">[<?php 
                _e('Remove Account', 'nxs_snap');
                ?>
]</a>
          </p><?php 
                $this->showNTSettings($indx, $pbo);
            }
            ?>
      </div>
    </div> <?php 
        }