/**
  * @dataProvider commandInterpreterCases
  */
 public function testCommandInterpreter($input, $expectedType, $comment = '')
 {
     $inter = new CommandInterpreter();
     $user = new User();
     // fake user
     $user->limit(1);
     $user->find();
     $cmd = $inter->handle_command($user, $input);
     $type = $cmd ? get_class($cmd) : null;
     $this->assertEquals(strtolower($expectedType), strtolower($type), $comment);
 }
Example #2
0
function interpretCommand($user, $body)
{
    $inter = new CommandInterpreter();
    $chan = new CLIChannel();
    $cmd = $inter->handle_command($user, $body);
    if ($cmd) {
        $cmd->execute($chan);
        return true;
    } else {
        $chan->error($user, "Not a valid command. Try 'help'?");
        return false;
    }
}
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         $msg = _('The server was unable to handle that much POST ' . 'data (%s bytes) due to its current configuration.');
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         return;
     }
     if (empty($this->status)) {
         $this->clientError('Client must provide a \'status\' parameter with a value.', 400, $this->format);
         return;
     }
     if (empty($this->auth_user)) {
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     $status_shortened = common_shorten_links($this->status);
     if (Notice::contentTooLong($status_shortened)) {
         // Note: Twitter truncates anything over 140, flags the status
         // as "truncated."
         $this->clientError(sprintf(_('That\'s too long. Max notice size is %d chars.'), Notice::maxContent()), 406, $this->format);
         return;
     }
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $status_shortened);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::staticGet($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 $this->clientError(_('Not found.'), $code = 404, $this->format);
                 return;
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->auth_user);
         } catch (ClientException $ce) {
             $this->clientError($ce->getMessage());
             return;
         }
         if (isset($upload)) {
             $status_shortened .= ' ' . $upload->shortUrl();
             if (Notice::contentTooLong($status_shortened)) {
                 $upload->delete();
                 $msg = _('Max notice size is %d chars, ' . 'including attachment URL.');
                 $this->clientError(sprintf($msg, Notice::maxContent()));
             }
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         if ($this->auth_user->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->auth_user->getProfile());
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->auth_user->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage());
             return;
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Example #4
0
 /**
  * Attempt to handle a message as a command
  * @param User $user user the message is from
  * @param string $body message text
  * @return boolean true if the message was a command and was executed, false if it was not a command
  */
 protected function handleCommand($user, $body)
 {
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $body);
     if ($cmd) {
         $chan = new IMChannel($this);
         $cmd->execute($chan);
         return true;
     }
     return false;
 }
Example #5
0
 function saveNewNotice()
 {
     $user = $this->flink->getUser();
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->showPage(_m('No notice content!'));
         return;
     } else {
         $content_shortened = common_shorten_links($content);
         if (Notice::contentTooLong($content_shortened)) {
             $this->showPage(sprintf(_m('That\'s too long. Max notice size is %d chars.'), Notice::maxContent()));
             return;
         }
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content_shortened);
     if ($cmd) {
         // XXX fix this
         $cmd->execute(new WebChannel());
         return;
     }
     $replyto = $this->trimmed('inreplyto');
     try {
         $notice = Notice::saveNew($user->id, $content, 'web', array('reply_to' => $replyto == 'false' ? null : $replyto));
     } catch (Exception $e) {
         $this->showPage($e->getMessage());
         return;
     }
 }
Example #6
0
 function handle_command($user, $body)
 {
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $body);
     if ($cmd) {
         $chan = new XMPPChannel($this->conn);
         $cmd->execute($chan);
         return true;
     } else {
         return false;
     }
 }
Example #7
0
 /**
  * Save a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 function saveNewNotice()
 {
     $user = common_current_user();
     assert($user);
     // XXX: maybe an error instead...
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->clientError(_('No content!'));
     } else {
         $content_shortened = common_shorten_links($content);
         if (mb_strlen($content_shortened) > 140) {
             $this->clientError(_('That\'s too long. ' . 'Max notice size is 140 chars.'));
         }
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content_shortened);
     if ($cmd) {
         if ($this->boolean('ajax')) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $replyto = $this->trimmed('inreplyto');
     #If an ID of 0 is wrongly passed here, it will cause a database error,
     #so override it...
     if ($replyto == 0) {
         $replyto = 'false';
     }
     $notice = Notice::saveNew($user->id, $content, 'web', 1, $replyto == 'false' ? null : $replyto);
     if (is_string($notice)) {
         $this->clientError($notice);
         return;
     }
     common_broadcast_notice($notice);
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         $this->element('title', null, _('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->showNotice($notice);
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $returnto = $this->trimmed('returnto');
         if ($returnto) {
             $url = common_local_url($returnto, array('nickname' => $user->nickname));
         } else {
             $url = common_local_url('shownotice', array('notice' => $notice->id));
         }
         common_redirect($url, 303);
     }
 }
 /**
  * Save a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 function saveNewNotice()
 {
     $user = common_current_user();
     assert($user);
     // XXX: maybe an error instead...
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->clientError(_('No content!'));
         return;
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if ($this->boolean('ajax')) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $content_shortened = common_shorten_links($content);
     if (Notice::contentTooLong($content_shortened)) {
         $this->clientError(sprintf(_('That\'s too long. ' . 'Max notice size is %d chars.'), Notice::maxContent()));
     }
     $replyto = $this->trimmed('inreplyto');
     #If an ID of 0 is wrongly passed here, it will cause a database error,
     #so override it...
     if ($replyto == 0) {
         $replyto = 'false';
     }
     $upload = null;
     $upload = MediaFile::fromUpload('attach');
     if (isset($upload)) {
         $content_shortened .= ' ' . $upload->shortUrl();
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), Notice::maxContent()));
         }
     }
     $options = array('reply_to' => $replyto == 'false' ? null : $replyto);
     if ($user->shareLocation()) {
         // use browser data if checked; otherwise profile data
         if ($this->arg('notice_data-geo')) {
             $locOptions = Notice::locationOptions($this->trimmed('lat'), $this->trimmed('lon'), $this->trimmed('location_id'), $this->trimmed('location_ns'), $user->getProfile());
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $user->getProfile());
         }
         $options = array_merge($options, $locOptions);
     }
     $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
     if (isset($upload)) {
         $upload->attachToNotice($notice);
     }
     if ($this->boolean('ajax')) {
         header('Content-Type: text/xml;charset=utf-8');
         $this->xw->startDocument('1.0', 'UTF-8');
         $this->elementStart('html');
         $this->elementStart('head');
         $this->element('title', null, _('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->showNotice($notice);
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $returnto = $this->trimmed('returnto');
         if ($returnto) {
             $url = common_local_url($returnto, array('nickname' => $user->nickname));
         } else {
             $url = common_local_url('shownotice', array('notice' => $notice->id));
         }
         common_redirect($url, 303);
     }
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
     }
     if (empty($this->status)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         $this->clientError(_('Client must provide a \'status\' parameter with a value.'));
     }
     if (is_null($this->scoped)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404);
     }
     /* Do not call shortenlinks until the whole notice has been build */
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $this->status);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::getKV($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 // TRANS: Client error displayed when replying to a non-existing notice.
                 $this->clientError(_('Parent notice not found.'), 404);
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->scoped);
         } catch (NoUploadedMediaException $e) {
             // There was no uploaded media for us today.
         }
         if (isset($upload)) {
             $this->status .= ' ' . $upload->shortUrl();
             /* Do not call shortenlinks until the whole notice has been build */
         }
         // in Qvitter we shorten _before_ posting, so disble shortening here
         $status_shortened = $this->status;
         if (Notice::contentTooLong($status_shortened)) {
             if ($upload instanceof MediaFile) {
                 $upload->delete();
             }
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum lenth for a notice.
             $msg = _m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent());
             /* Use HTTP 413 error code (Request Entity Too Large)
              * instead of basic 400 for better understanding
              */
             $this->clientError(sprintf($msg, Notice::maxContent()), 413);
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         // -------------------------------------------------------------
         // -------- Qvitter's post-to-the-right-group stuff! -----------
         // -------------------------------------------------------------
         // guess the groups by the content first, if we don't have group id:s as meta data
         $profile = Profile::getKV('id', $this->scoped->id);
         $guessed_groups = User_group::groupsFromText($content, $profile);
         // if the user has specified any group id:s, correct the guesswork
         if (strlen($this->post_to_groups) > 0) {
             // get the groups that the user wants to post to
             $group_ids = explode(':', $this->post_to_groups);
             $correct_groups = array();
             foreach ($group_ids as $group_id) {
                 $correct_groups[] = User_group::getKV('id', $group_id);
             }
             // correct the guesses
             $corrected_group_ids = array();
             foreach ($guessed_groups as $guessed_group) {
                 $id_to_keep = $guessed_group->id;
                 foreach ($correct_groups as $k => $correct_group) {
                     if ($correct_group->nickname == $guessed_group->nickname) {
                         $id_to_keep = $correct_group->id;
                         unset($correct_groups[$k]);
                         break;
                     }
                 }
                 $corrected_group_ids[$id_to_keep] = true;
             }
             // but we still want to post to all of the groups that the user specified by id
             // even if we couldn't use it to correct a bad guess
             foreach ($correct_groups as $correct_group) {
                 $corrected_group_ids[$correct_group->id] = true;
             }
             $options['groups'] = array_keys($corrected_group_ids);
         } else {
             $guessed_ids = array();
             foreach ($guessed_groups as $guessed_group) {
                 $guessed_ids[$guessed_group->id] = true;
             }
             $options['groups'] = array_keys($guessed_ids);
         }
         // -------------------------------------------------------------
         // ------ End of Qvitter's post-to-the-right-group stuff! ------
         // -------------------------------------------------------------
         if ($this->scoped->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->scoped);
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->scoped->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode());
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Example #10
0
 /**
  * Save a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 function saveNewNotice()
 {
     $user = common_current_user();
     assert($user);
     // XXX: maybe an error instead...
     $content = $this->trimmed('status_textarea');
     $options = array();
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (!$content) {
         $this->clientError(_('No content!'));
         return;
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if ($this->boolean('ajax')) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $content_shortened = $user->shortenLinks($content);
     if (Notice::contentTooLong($content_shortened)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $replyto = intval($this->trimmed('inreplyto'));
     if ($replyto) {
         $options['reply_to'] = $replyto;
     }
     $upload = null;
     $upload = MediaFile::fromUpload('attach');
     if (isset($upload)) {
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
             $content_shortened .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent()), Notice::maxContent()));
         }
     }
     if ($user->shareLocation()) {
         // use browser data if checked; otherwise profile data
         if ($this->arg('notice_data-geo')) {
             $locOptions = Notice::locationOptions($this->trimmed('lat'), $this->trimmed('lon'), $this->trimmed('location_id'), $this->trimmed('location_ns'), $user->getProfile());
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $user->getProfile());
         }
         $options = array_merge($options, $locOptions);
     }
     $author_id = $user->id;
     $text = $content_shortened;
     if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
         $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
         if (isset($upload)) {
             $upload->attachToNotice($notice);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $notice));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
     if ($this->boolean('ajax')) {
         header('Content-Type: text/xml;charset=utf-8');
         $this->xw->startDocument('1.0', 'UTF-8');
         $this->elementStart('html');
         $this->elementStart('head');
         $this->element('title', null, _('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->showNotice($notice);
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $returnto = $this->trimmed('returnto');
         if ($returnto) {
             $url = common_local_url($returnto, array('nickname' => $user->nickname));
         } else {
             $url = common_local_url('shownotice', array('notice' => $notice->id));
         }
         common_redirect($url, 303);
     }
 }
 /**
  * EndInterpretCommand will handle the 'd' and 'dm' commands.
  *
  * @param string  $cmd     Command being run
  * @param string  $arg     Rest of the message (including address)
  * @param User    $user    User sending the message
  * @param Command &$result The resulting command object to be run.
  *
  * @return boolean hook value
  */
 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
 {
     $dm_cmds = array('d', 'dm');
     if ($result === false && in_array($cmd, $dm_cmds)) {
         if (!empty($arg)) {
             list($other, $extra) = CommandInterpreter::split_arg($arg);
             if (!empty($extra)) {
                 $result = new MessageCommand($user, $other, $extra);
             }
         }
         return false;
     }
     return true;
 }
Example #12
0
 /**
  * EndInterpretCommand for RepeatPlugin will handle the 'repeat' command
  * using the class RepeatCommand.
  *
  * @param string  $cmd     Command being run
  * @param string  $arg     Rest of the message (including address)
  * @param User    $user    User sending the message
  * @param Command &$result The resulting command object to be run.
  *
  * @return boolean hook value
  */
 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
 {
     if ($result === false && in_array($cmd, array('repeat', 'rp', 'rt', 'rd'))) {
         if (empty($arg)) {
             $result = null;
         } else {
             list($other, $extra) = CommandInterpreter::split_arg($arg);
             if (!empty($extra)) {
                 $result = null;
             } else {
                 $result = new RepeatCommand($user, $other);
             }
         }
         return false;
     }
     return true;
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         return;
     }
     if (empty($this->status)) {
         $this->clientError(_('Client must provide a \'status\' parameter with a value.'), 400, $this->format);
         return;
     }
     if (empty($this->auth_user)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     $status_shortened = $this->auth_user->shortenlinks($this->status);
     if (Notice::contentTooLong($status_shortened)) {
         // Note: Twitter truncates anything over 140, flags the status
         // as "truncated."
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()), 406, $this->format);
         return;
     }
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $status_shortened);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::staticGet($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 $this->clientError(_('Parent notice not found.'), $code = 404, $this->format);
                 return;
             }
         }
         if (!empty($this->link)) {
             try {
                 $post_url_file = MediaFile::fromLink($this->link, $this->auth_user);
             } catch (Exception $e) {
                 $this->clientError($e->getMessage(), $e->getCode(), $this->format);
                 return;
             }
         }
         $upload2 = null;
         try {
             $upload2 = MediaFile::fromUpload('media2', $this->auth_user);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->auth_user);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         if (isset($upload)) {
             if (Notice::contentTooLong($status_shortened)) {
                 $upload->delete();
                 // TRANS: Client error displayed exceeding the maximum notice length.
                 // TRANS: %d is the maximum lenth for a notice.
                 $msg = _m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent());
                 $this->clientError(sprintf($msg, Notice::maxContent()), 400, $this->format);
             }
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         if ($this->auth_user->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->auth_user->getProfile());
             $options = array_merge($options, $locOptions);
         }
         //dyg add to response to group_id request
         ToSelector::fillOptions($this, $options);
         //end
         if (!empty($this->html_status)) {
             $options['rendered'] = $this->html_status;
         }
         $created = $this->trimmed('created');
         if (!empty($created)) {
             $options['created'] = $created;
         }
         try {
             $this->notice = Notice::saveNew($this->auth_user->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
         if (isset($upload2)) {
             $upload2->attachToNotice($this->notice);
         }
         if (isset($post_url_file)) {
             $post_url_file->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Example #14
0
 /**
  * This doPost saves a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 protected function doPost()
 {
     assert($this->scoped instanceof Profile);
     // XXX: maybe an error instead...
     $user = $this->scoped->getUser();
     $content = $this->trimmed('status_textarea');
     $options = array();
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (empty($content)) {
         // TRANS: Client error displayed trying to send a notice without content.
         $this->clientError(_('No content!'));
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if (GNUsocial::isAjax()) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $content_shortened = $user->shortenLinks($content);
     if (Notice::contentTooLong($content_shortened)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $replyto = $this->int('inreplyto');
     if ($replyto) {
         $options['reply_to'] = $replyto;
     }
     $upload = null;
     try {
         // throws exception on failure
         $upload = MediaFile::fromUpload('attach', $this->scoped);
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
             $content_shortened .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum length for a notice.
             $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent()), Notice::maxContent()));
         }
     } catch (NoUploadedMediaException $e) {
         // simply no attached media to the new notice
     }
     if ($this->scoped->shareLocation()) {
         // use browser data if checked; otherwise profile data
         if ($this->arg('notice_data-geo')) {
             $locOptions = Notice::locationOptions($this->trimmed('lat'), $this->trimmed('lon'), $this->trimmed('location_id'), $this->trimmed('location_ns'), $this->scoped);
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $this->scoped);
         }
         $options = array_merge($options, $locOptions);
     }
     $author_id = $this->scoped->id;
     $text = $content_shortened;
     // Does the heavy-lifting for getting "To:" information
     ToSelector::fillOptions($this, $options);
     if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
         $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
         if ($upload instanceof MediaFile) {
             $upload->attachToNotice($this->stored);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
     if (!GNUsocial::isAjax()) {
         $url = common_local_url('shownotice', array('notice' => $this->stored->id));
         common_redirect($url, 303);
     }
     return _('Saved the notice!');
 }
Example #15
0
 function saveNewNotice()
 {
     $user = $this->flink->getUser();
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->showPage(_('No notice content!'));
         return;
     } else {
         $content_shortened = common_shorten_links($content);
         if (mb_strlen($content_shortened) > 140) {
             $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
             return;
         }
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content_shortened);
     if ($cmd) {
         // XXX fix this
         $cmd->execute(new WebChannel());
         return;
     }
     $replyto = $this->trimmed('inreplyto');
     $notice = Notice::saveNew($user->id, $content, 'Facebook', 1, $replyto == 'false' ? null : $replyto);
     if (is_string($notice)) {
         $this->showPage($notice);
         return;
     }
     common_broadcast_notice($notice);
     // Also update the user's Facebook status
     $this->updateFacebookStatus($notice);
     $this->updateProfileBox($notice);
 }
 /**
  * EndInterpretCommand for FavoritePlugin will handle the 'fav' command
  * using the class FavCommand.
  *
  * @param string  $cmd     Command being run
  * @param string  $arg     Rest of the message (including address)
  * @param User    $user    User sending the message
  * @param Command &$result The resulting command object to be run.
  *
  * @return boolean hook value
  */
 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
 {
     if ($result === false && $cmd == 'fav') {
         if (empty($arg)) {
             $result = null;
         } else {
             list($other, $extra) = CommandInterpreter::split_arg($arg);
             if (!empty($extra)) {
                 $result = null;
             } else {
                 $result = new FavCommand($user, $other);
             }
         }
         return false;
     }
     return true;
 }
Example #17
0
 /**
  * This doPost saves a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 protected function doPost()
 {
     assert($this->scoped instanceof Profile);
     // XXX: maybe an error instead...
     $user = $this->scoped->getUser();
     $content = $this->trimmed('status_textarea');
     $options = array('source' => 'web');
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (empty($content)) {
         // TRANS: Client error displayed trying to send a notice without content.
         $this->clientError(_('No content!'));
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if (GNUsocial::isAjax()) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     if ($this->int('inreplyto')) {
         // Throws exception if the inreplyto Notice is given but not found.
         $parent = Notice::getByID($this->int('inreplyto'));
     } else {
         $parent = null;
     }
     $act = new Activity();
     $act->verb = ActivityVerb::POST;
     $act->time = time();
     $act->actor = $this->scoped->asActivityObject();
     $upload = null;
     try {
         // throws exception on failure
         $upload = MediaFile::fromUpload('attach', $this->scoped);
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
             $content .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));
         // We could check content length here if the URL was added, but I'll just let it slide for now...
         $act->enclosures[] = $upload->getEnclosure();
     } catch (NoUploadedMediaException $e) {
         // simply no attached media to the new notice
     }
     // Reject notice if it is too long (without the HTML)
     // This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
     if (Notice::contentTooLong($content)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         throw new ClientException(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $act->context = new ActivityContext();
     if ($parent instanceof Notice) {
         $act->context->replyToID = $parent->getUri();
         $act->context->replyToUrl = $parent->getUrl(true);
         // maybe we don't have to send true here to force a URL?
     }
     if ($this->scoped->shareLocation()) {
         // use browser data if checked; otherwise profile data
         if ($this->arg('notice_data-geo')) {
             $locOptions = Notice::locationOptions($this->trimmed('lat'), $this->trimmed('lon'), $this->trimmed('location_id'), $this->trimmed('location_ns'), $this->scoped);
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $this->scoped);
         }
         $act->context->location = Location::fromOptions($locOptions);
     }
     $content = $this->scoped->shortenLinks($content);
     // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
     if (Event::handle('StartNoticeSaveWeb', array($this, $this->scoped, &$content, &$options))) {
         // FIXME: We should be able to get the attentions from common_render_content!
         // and maybe even directly save whether they're local or not!
         $act->context->attention = common_get_attentions($content, $this->scoped, $parent);
         $actobj = new ActivityObject();
         $actobj->type = ActivityObject::NOTE;
         $actobj->content = common_render_content($content, $this->scoped, $parent);
         // Finally add the activity object to our activity
         $act->objects[] = $actobj;
         $this->stored = Notice::saveActivity($act, $this->scoped, $options);
         if ($upload instanceof MediaFile) {
             $upload->attachToNotice($this->stored);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (!GNUsocial::isAjax()) {
         $url = common_local_url('shownotice', array('notice' => $this->stored->id));
         common_redirect($url, 303);
     }
     return _('Saved the notice!');
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
     }
     if (empty($this->status)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         $this->clientError(_('Client must provide a \'status\' parameter with a value.'));
     }
     if (is_null($this->scoped)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404);
     }
     /* Do not call shortenLinks until the whole notice has been build */
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $this->status);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::getKV($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 // TRANS: Client error displayed when replying to a non-existing notice.
                 $this->clientError(_('Parent notice not found.'), 404);
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->scoped);
             $this->status .= ' ' . $upload->shortUrl();
             /* Do not call shortenLinks until the whole notice has been build */
         } catch (NoUploadedMediaException $e) {
             // There was no uploaded media for us today.
         }
         /* Do call shortenlinks here & check notice length since notice is about to be saved & sent */
         $status_shortened = $this->auth_user->shortenLinks($this->status);
         if (Notice::contentTooLong($status_shortened)) {
             if ($upload instanceof MediaFile) {
                 $upload->delete();
             }
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum lenth for a notice.
             $msg = _m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent());
             /* Use HTTP 413 error code (Request Entity Too Large)
              * instead of basic 400 for better understanding
              */
             $this->clientError(sprintf($msg, Notice::maxContent()), 413);
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         if ($this->scoped->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->scoped);
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->scoped->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode());
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Example #19
0
 function update($args, $apidata)
 {
     parent::handle($args);
     if (!in_array($apidata['content-type'], array('xml', 'json'))) {
         $this->clientError(_('API method not found!'), $code = 404);
         return;
     }
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
         return;
     }
     $this->auth_user = $apidata['user'];
     $user = $this->auth_user;
     $status = $this->trimmed('status');
     $source = $this->trimmed('source');
     $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
     $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
     if (!$source || in_array($source, $reserved_sources)) {
         $source = 'api';
     }
     if (!$status) {
         // XXX: Note: In this case, Twitter simply returns '200 OK'
         // No error is given, but the status is not posted to the
         // user's timeline.     Seems bad.     Shouldn't we throw an
         // errror? -- Zach
         return;
     } else {
         $status_shortened = common_shorten_links($status);
         if (mb_strlen($status_shortened) > 140) {
             // XXX: Twitter truncates anything over 140, flags the status
             // as "truncated." Sending this error may screw up some clients
             // that assume Twitter will truncate for them.    Should we just
             // truncate too? -- Zach
             $this->clientError(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
             return;
         }
     }
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $status_shortened);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $n = $user->getCurrentNotice();
         $apidata['api_arg'] = $n->id;
     } else {
         $reply_to = null;
         if ($in_reply_to_status_id) {
             // check whether notice actually exists
             $reply = Notice::staticGet($in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $in_reply_to_status_id;
             } else {
                 $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
                 return;
             }
         }
         $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'), $source, 1, $reply_to);
         if (is_string($notice)) {
             $this->serverError($notice);
             return;
         }
         common_broadcast_notice($notice);
         $apidata['api_arg'] = $notice->id;
     }
     $this->show($args, $apidata);
 }
Example #20
0
 function handle_command($user, $from, $msg)
 {
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $msg);
     if ($cmd) {
         $cmd->execute(new MailChannel($from));
         return true;
     }
     return false;
 }
Example #21
0
 /**
  * Attempt to handle a message from a channel as a command
  *
  * @param User $user User the message is from
  * @param string $channel Channel the message originated from
  * @param string $body Message text
  * @return boolean true if the message was a command and was executed, false if it was not a command
  */
 protected function handle_channel_command($user, $channel, $body)
 {
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $body);
     if ($cmd) {
         $chan = new ChannelResponseChannel($this, $channel);
         $cmd->execute($chan);
         return true;
     } else {
         return false;
     }
 }