public function index()
 {
     if (\Configer::get('maintain') == 'on') {
         return view('maintain');
     } else {
         return view('form', ['config' => \Configer::get(), 'alert' => \Configer::check()]);
     }
 }
 public function setting()
 {
     $setting = [];
     $result = \DB::table('setting')->get();
     foreach ($result as $item) {
         $setting[$item->name] = $item->value;
     }
     $langs = ['en' => 'English', 'zh-hant' => '正體中文', 'zh-hans' => '简体中文'];
     $themes = ["cerulean", "cosmo", "cyborg", "darkly", "flatly", "journal", "paper", "readable", "sandstone", "simplex", "slate", "united", "yeti", "lumen", "superhero", "spacelab"];
     $frequency = [60, 120, 180, 240, 300];
     return view('admin.setting', ['config' => \Configer::get(), 'setting' => $setting, 'langs' => $langs, 'themes' => $themes, 'frequency' => $frequency]);
 }
 public function check()
 {
     @($key = strip_tags(\Route::input('key')));
     if (empty($key)) {
         return redirect('/');
     } else {
         $result = \DB::table('post')->where('post_key', $key)->first();
         if (isset($result->id)) {
             if ($result->post_state == 1) {
                 $exp = explode('_', $result->facebook_url);
                 $url = 'https://facebook.com/' . $exp[0] . '/posts/' . $exp[1];
                 return redirect($url);
             } else {
                 if ($result->publish_time - time() >= 0) {
                     $state = '<div class="alert alert-danger text-center" role="alert">' . \Lang::get('check.do_not_close_window') . '</div><h4>' . \Lang::get('check.remaining_time') . ': <span id="countdown" data-timestamp="' . $result->publish_time . '"></span></h4><p>' . \Lang::get('check.auto_redirect_message') . '</p>';
                     $foot = '<script src="/js/check.js" type="text/javascript"></script>';
                 } elseif ($result->publish_time - time() < 0 && $result->post_state == 0) {
                     $last = \DB::table('post')->where('post_state', '1')->orderBy('id', 'desc')->first();
                     if (count($last) > 0) {
                         $query = 'UPDATE post SET post_state = 8 WHERE id < ' . $last->id . ' AND post_state = 0';
                         \DB::update(\DB::raw($query));
                     }
                     $this->publishPostToFacebook($result);
                     header("Refresh:0");
                     exit;
                 } elseif ($result->publish_time - time() < 0 && $result->post_state == 5) {
                     $state = '<h4>' . \Lang::get('check.pending') . '</h4><p>' . \Lang::get('check.pending_message') . '</p>';
                     $foot = '';
                 } else {
                     $state = '<h4>' . \Lang::get('check.oops') . '</h4><p>' . \Lang::get('check.oops_message') . '<br />' . \Lang::get('check.try_contact_with') . '<a href="http://facebook.com/' . \Configer::get('page_id') . '">' . \Lang::get('check.page_admin') . '</a></p>';
                     $foot = '';
                 }
                 return view('check', ['config' => \Configer::get(), 'state' => $state, 'foot' => $foot]);
             }
         } else {
             return redirect('/');
         }
     }
 }
 private function handleText($text)
 {
     if (empty($text)) {
         $text = \Configer::get('page_name');
     }
     if (mb_strlen($text, 'utf-8') > 140) {
         $text = mb_substr($text, 0, 140, 'utf-8');
     }
     $text = str_replace('\\n', "\n", $text);
     $text = $this->autoWrap($text);
     return $text;
 }
 public function unpublishPostFromFacebook($post)
 {
     $response = ['state' => 'error', 'message' => 'Something wrong.'];
     $fb = new Facebook(\Configer::get('fb_app_setting'));
     $fb->setDefaultAccessToken(\Configer::get('fb_page_token'));
     try {
         $callback = $fb->delete('/' . $post->facebook_url);
         $callback = $callback->getDecodedBody();
         if ($callback['success'] == 1) {
             \DB::table('post')->where('id', $post->id)->update(['post_state' => 2]);
             $response = ['state' => 'success', 'message' => 'Post unpublished.'];
         } else {
             $response = ['state' => 'failed', 'message' => 'Post unpublish failed.'];
         }
     } catch (Exception $e) {
         $response = ['state' => 'failed', 'message' => 'Post unpublish failed.'];
     }
     return $response;
 }