Exemple #1
0
 public function post()
 {
     if (Input::has('api_key') && Input::has('content')) {
         $api_key = Input::get('api_key');
         $content = Input::get('content');
         $settings = Settings::where('api_key', '=', $api_key)->first();
         $user_id = $settings->user_id;
         $default_networks = json_decode($settings->default_networks, true);
         $schedule = Carbon::now();
         if (Input::has('queue')) {
             $schedule_id = $settings->schedule_id;
             $interval = Schedule::find($schedule_id);
             if ($interval->rule == 'add') {
                 $schedule = $current_datetime->modify('+ ' . $interval->period);
             } else {
                 if ($interval->rule == 'random') {
                     $current_day = date('d');
                     $from_datetime = Carbon::now();
                     $to_datetime = $from_datetime->copy()->modify('+ ' . $interval->period);
                     $days_to_add = $from_datetime->diffInDays($to_datetime);
                     $day = mt_rand($current_day, $current_day + $days_to_add);
                     $hour = mt_rand(1, 23);
                     $minute = mt_rand(0, 59);
                     $second = mt_rand(0, 59);
                     //year, month and timezone is null
                     $schedule = Carbon::create(null, null, $day, $hour, $minute, $second, null);
                 }
             }
             if (empty($schedule)) {
                 $schedule = $current_datetime->addHours(1);
             }
         }
         if (!empty($default_networks)) {
             $post = new Post();
             $post->user_id = $user_id;
             $post->content = $content;
             $post->date_time = $schedule;
             $post->save();
             $post_id = $post->id;
             foreach ($default_networks as $network_id) {
                 $post_network = new PostNetwork();
                 $post_network->user_id = $user_id;
                 $post_network->post_id = $post_id;
                 $post_network->network_id = $network_id;
                 $post_network->status = 1;
                 $post_network->save();
             }
             Queue::later($schedule, 'SendPost@fire', array('post_id' => $post_id));
             $response_data = array('type' => 'success', 'text' => 'Your post was scheduled! It will be published on ' . $schedule->format('l jS \\o\\f F \\a\\t h:i A'));
             return $response_data;
         }
     }
 }
Exemple #2
0
 public function updatePost()
 {
     $user_id = Auth::user()->id;
     $rules = array('content' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     $post_id = Input::get('id');
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator);
     }
     if (Input::has('network')) {
         $content = Input::get('content');
         $post = Post::where('user_id', '=', $user_id)->where('id', '=', $post_id)->first();
         $post->content = $content;
         $post->save();
         $networks = Input::get('network');
         $post_networks = PostNetwork::where('post_id', '=', $post_id)->lists('network_id');
         foreach ($post_networks as $network_id) {
             if (!in_array($network_id, $networks)) {
                 $post_network = PostNetwork::where('network_id', '=', $network_id)->where('post_id', '=', $post_id)->first();
                 if (!empty($post_network)) {
                     $post_network->status = 0;
                     $post_network->save();
                 }
             }
         }
         foreach ($networks as $network_id) {
             $post_network = PostNetwork::where('network_id', '=', $network_id)->where('post_id', '=', $post_id)->first();
             if (!empty($post_network)) {
                 $post_network->status = 1;
                 $post_network->save();
             } else {
                 $post_network = new PostNetwork();
                 $post_network->user_id = $user_id;
                 $post_network->post_id = $post_id;
                 $post_network->network_id = $network_id;
                 $post_network->status = 1;
                 $post_network->save();
             }
         }
     }
     $response_data = array('type' => 'success', 'text' => 'Post was updated!', 'post' => array('id' => $post_id, 'title' => substr($content, 0, 24), 'content' => $content));
     return $response_data;
 }
Exemple #3
0
 public function fire($job, $data)
 {
     $post_id = $data['post_id'];
     $post = Post::find($post_id);
     if (!empty($post)) {
         $user_id = $post->user_id;
         $post_url = '';
         preg_match_all('#[-a-zA-Z0-9@:%_\\+.~\\#?&//=]{2,256}\\.[a-z]{2,4}\\b(\\/[-a-zA-Z0-9@:%_\\+.~\\#?&//=]*)?#si', $post->content, $url_matches);
         if (!empty($url_matches)) {
             $all_urls = $url_matches[0];
             $post_url = !empty($all_urls[0]) ? $all_urls[0] : '';
         }
         $network_ids = PostNetwork::where('post_id', '=', $post_id)->lists('network_id');
         $network = Network::where('user_id', '=', $user_id)->select('user_token', 'user_secret', 'network')->whereIn('id', $network_ids)->get();
         $client = new GuzzleHttp\Client();
         if (!empty($network)) {
             foreach ($network as $s) {
                 if ($s->network == 'twitter') {
                     try {
                         Twitter::setOAuthToken($s->user_token);
                         Twitter::setOAuthTokenSecret($s->user_secret);
                         $twitter_response = Twitter::statusesUpdate($post->content);
                     } catch (Exception $e) {
                     }
                 } else {
                     if ($s->network == 'linkedin') {
                         if (!empty($post_url)) {
                             try {
                                 $post_data = array('comment' => $post->content, 'content' => array('description' => $post->content), 'visibility' => array('code' => 'anyone'));
                                 $post_data['content']['submittedUrl'] = $post_url;
                                 $request_body = $post_data;
                                 $linkedin_resource = '/v1/people/~/shares';
                                 $request_format = 'json';
                                 $linkedin_params = array('oauth2_access_token' => $s->user_token, 'format' => $request_format);
                                 $linkedinurl_info = parse_url('https://api.linkedin.com' . $linkedin_resource);
                                 if (isset($linkedinurl_info['query'])) {
                                     $query = parse_str($linkedinurl_info['query']);
                                     $linkedin_params = array_merge($linkedin_params, $query);
                                 }
                                 $request_url = 'https://api.linkedin.com' . $linkedinurl_info['path'] . '?' . http_build_query($linkedin_params);
                                 $request_body = json_encode($request_body);
                                 $linkedin_response = CurlRequester::requestCURL('POST', $request_url, $request_body, $request_format);
                             } catch (Exception $e) {
                             }
                         }
                     } else {
                         if ($s->network == 'facebook') {
                             try {
                                 $post_data = array('access_token' => $s->user_token, 'message' => $post->content);
                                 if (!empty($post_url)) {
                                     $post_data['link'] = $post_url;
                                 }
                                 $res = $client->post('https://graph.facebook.com/me/feed', array('query' => $post_data));
                                 $response_body = $res->getBody();
                                 $response_body = json_decode($response_body, true);
                             } catch (Exception $e) {
                             }
                         }
                     }
                 }
             }
         }
     }
     $post->published = 1;
     $post->save();
     $job->delete();
 }