예제 #1
0
 function apiUpdateJson($data)
 {
     //configuration parameters:
     $config_params = Config::getConfigParams();
     $hostname = $config_params['hostname'];
     $tweet_size_max = $config_params['tweet_size_max'];
     $script_path = __FUNCTION__;
     $json_array = array();
     $error = false;
     $twextra_key = $data['twextra_key'];
     $twitter_access_token = $data['twitter_access_token'];
     $json_array['twextra_key'] = $twextra_key;
     //optional
     $tweet = $data['editor'];
     $tweet_len = trim(strlen($tweet));
     //generate an error if the message length exceeds max size, or is empty
     if ($tweet_len > $tweet_size_max) {
         //generate error message;
         $json_array['error']['message_len'] = 'Message exceeds max size';
         $error = true;
     } else {
         if ($tweet_len == 0) {
             //generate error message;
             $json_array['error']['message_len'] = 'Message is empty';
             $error = true;
         } else {
             $json_array['message_len'] = $tweet_len;
         }
     }
     //authenticate based on the key and check daily limit
     if (!$error) {
         logger($script_path . "  twextra_key: ", $twextra_key);
         $model = new TwextraModel();
         $key_match = $model->apiKeyMatchUpdate($twextra_key);
         //
         if ($key_match == 'miss') {
             //
             $json_array['error']['key_match_daily_limit'] = 'Error: Key did not match or the daily limit exceeded.';
             $error = true;
         }
     }
     //generate a random url
     if (!$error) {
         $message_id = $this->random_message_id();
         $url = $hostname . "/tweet_display.php?message_id={$message_id}&mthd=displayTweet";
         $tweet = stripslashes($tweet);
         //save in database, and increment daily usage count
         $model = new TwextraModel();
         $saveTweet = $model->saveTweet($tweet, $message_id);
         //$incCount = $model->apiIncCount ( $key ); //
         if (!$saveTweet) {
             $json_array['system_error'] = true;
             $json_array['url'] = '';
             $json_array['created'] = '';
         } else {
             $json_array['url'] = $url;
             $json_array['created'] = date("Y-m-d H:i:s");
         }
     }
     $json_string = json_encode($json_array);
     $json_string_stripped = stripslashes($json_string);
     //echo stripslashes ( $json_string );
     if ($error == false) {
         return $json_string_stripped;
     } else {
         return $error;
     }
 }