예제 #1
0
 public function send($item, $config)
 {
     $sql = "SELECT  `facebook`\n                FROM    `posts`\n                WHERE   `link` = '" . mysql_real_escape_string($item['link']) . "'";
     $status = mysql_fetch_assoc(mysql_query($sql));
     if (!isset($status['facebook'])) {
         $count = 0;
     } else {
         $count = 1;
     }
     if ($count == 0 or $status['facebook'] == 0) {
         preg_match_all('/(img src=")(.*)(")([\\s]+)(border)/Usmi', $item['description'], $matches);
         $tmp['image'] = $matches[2][0];
         $tmp['link_photo'] = str_replace('s320', 's1600', $tmp['image']);
         $filename = tempnam(ini_get('upload_tmp_dir'), 'cron');
         $fh = fopen($filename, 'a');
         fwrite($fh, file_get_contents($tmp['link_photo'], FILE_BINARY));
         $args = array('method' => 'photos.upload', 'v' => '1.0', 'api_key' => $config['apikey'], 'uid' => $config['uid'], 'call_id' => microtime(true), 'format' => 'XML', 'caption' => $item['title']);
         FaceBook::signRequest($args, $config['apisec']);
         $args[basename($filename)] = '@' . $filename;
         $ch = curl_init();
         $url = 'http://api.facebook.com/restserver.php?method=photos.upload';
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_HEADER, false);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
         $result = curl_exec($ch);
         fclose($fh);
         if (strpos($result, 'created')) {
             if ($count == 0) {
                 mysql_query("\n                                    INSERT INTO `posts`\n                                        (`link`, `facebook`)\n                                    VALUES\n                                        ('" . mysql_real_escape_string($item['link']) . "', '1');\n                                ");
             } else {
                 mysql_query("\n                                    UPDATE `posts`\n                                    SET `facebook` = '1'\n                                    WHERE `link` = '" . mysql_real_escape_string($item['link']) . "'\n                                ");
             }
         }
     }
 }
 public function indexAction(Request $request, $provider)
 {
     try {
         // get previous url
         $session = $request->getSession();
         $loginUrl = $this->getRequest()->headers->get('referer');
         // make sure we find the configuration
         if ($this->container->hasParameter($provider) && !$this->getUser()) {
             // get current configuration for provider
             $configs = $this->container->getParameter($provider);
             if ($provider == 'twitter') {
                 define('TWITTER_CONSUMER_KEY', $configs['api_key']);
                 define('TWITTER_CONSUMER_SECRET', $configs['api_secret']);
                 define('TWITTER_OAUTH_CALLBACK', $this->getRequest()->getUri());
                 if (!$session->get('oauth_token')) {
                     $connection = new \TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
                     $request_token = $connection->getRequestToken(TWITTER_OAUTH_CALLBACK);
                     $session->set('oauth_token', $request_token['oauth_token']);
                     $session->set('oauth_token_secret', $request_token['oauth_token_secret']);
                     switch ($connection->http_code) {
                         case 200:
                             return $this->redirect($connection->getAuthorizeURL($request_token['oauth_token']));
                             break;
                         default:
                             throw new \Exception('Could not connect to Twitter. Refresh the page or try again later.');
                             break;
                     }
                 } else {
                     $connection = new \TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $session->get('oauth_token'), $session->get('oauth_token_secret'));
                     $access_token = $connection->getAccessToken($request->get('oauth_verifier'));
                     $session->set('access_token', $access_token);
                     $content = $connection->get('account/verify_credentials');
                     $this->findUser($provider, array('id' => (string) $content->id, 'first_name' => (string) $content->name, 'last_name' => (string) $content->name, 'email' => (string) $content->id . '@twitter.com'));
                 }
             } elseif ($provider == 'google') {
                 // include google library
                 include dirname(__DIR__) . '/Vendor/Google/autoload.php';
                 $client = new \Google_Client();
                 $client->addScope("email");
                 $client->addScope("profile");
                 $client->setClientId($configs['api_key']);
                 $client->setClientSecret($configs['api_secret']);
                 $client->setRedirectUri($this->get_clean_url($this->getRequest()->getUri()));
                 if ($request->get('code', false)) {
                     $client->authenticate($request->get('code'));
                     $service = new \Google_Service_Oauth2($client);
                     $user = $service->userinfo->get();
                     //get user info
                     $this->findUser($provider, array('id' => (string) $user->id, 'first_name' => (string) $user->givenName, 'last_name' => (string) $user->familyName, 'email' => (string) $user->email));
                 } else {
                     return $this->redirect($client->createAuthUrl());
                 }
             } elseif ($provider == 'facebook') {
                 $facebook = new \FaceBook($configs['api_key'], $configs['api_secret'], $this->getRequest()->getUri());
                 if (!$facebook->is_authenticated()) {
                     return $this->redirect($facebook->get_redirect_url());
                 } else {
                     $this->findUser($provider, $facebook->get_user($session));
                 }
             } elseif ($provider == 'linkedin') {
                 $linkedin = new \LinkedIn($configs['api_key'], $configs['api_secret'], $this->getRequest()->getUri());
                 if (!$linkedin->is_authenticated()) {
                     return $this->redirect($linkedin->get_redirect_url());
                 } else {
                     $this->findUser($provider, $linkedin->get_user($session));
                 }
             }
         }
     } catch (\Exception $e) {
         $this->get('session')->getFlashBag()->add('error', $e->getMessage());
     }
     return $this->redirect($this->generateUrl($this->container->getParameter('login_path')));
 }