Exemplo n.º 1
0
 /**
  * Process posts
  */
 public function post_check(array $args)
 {
     $auth = null;
     $ocs = new com_meego_ocs_OCSWriter();
     if (!isset($_POST['login'])) {
         $ocs->writeError('The login argument is mandatory', 101);
         $ocs->endDocument();
         self::output_xml($ocs);
         return;
     }
     $tokens = com_meego_ocs_utils::prepare_tokens();
     switch (midgardmvc_core::get_instance()->configuration->ocs_authentication) {
         case 'LDAP':
             $info = com_meego_ocs_utils::ldap_check($tokens);
             $auth = com_meego_ocs_utils::authenticate();
             break;
         case 'basic':
         default:
             $info = new midgardmvc_core_services_authentication_basic();
             $e = new Exception("Requires HTTP Basic authentication");
             $info->handle_exception($e);
     }
     if (!$info || !$auth) {
         $ocs->writeError('Invalid account', 102);
         $ocs->endDocument();
         self::output_xml($ocs);
         return;
     }
     $ocs->writeMeta(null, null, 'Valid account', 'ok', 100);
     $ocs->writePersonCheck($info['username'], $info['email']);
     $ocs->endDocument();
     self::output_xml($ocs);
 }
Exemplo n.º 2
0
 /**
  * Process an attribute setting POST
  * args['context'] might be: installed
  * args['key'] holds the package ID
  * @param args see above
  */
 public function post_setattribute(array $args)
 {
     $summary = '';
     $success = true;
     if (!$this->user) {
         // this operation requires authentication
         $auth = com_meego_ocs_utils::authenticate($args);
         if (!$auth) {
             com_meego_ocs_utils::end_with_error('This interface is available for authenticated users only', 199);
         }
     }
     if (!isset($args['context'])) {
         com_meego_ocs_utils::end_with_error('Mandatory context missing (e.g. installed)', 102);
     }
     if (!isset($args['key'])) {
         com_meego_ocs_utils::end_with_error('Mandatory package ID missing', 103);
     }
     // check if the context is supported
     switch ($args['context']) {
         case 'save':
             $summary = 'User succesfully installed an application.';
             break;
         case 'unsave':
             $summary = 'User succesfully uninstalled an application.';
             break;
         case 'savefail':
             $summary = 'Application installation failed.';
             break;
         default:
             com_meego_ocs_utils::end_with_error('This context: ' . $args['context'] . ' is not supported', 104);
     }
     $package = new com_meego_package();
     try {
         $package->get_by_id((int) $args['key']);
     } catch (Exception $e) {
         $success = false;
         com_meego_ocs_utils::end_with_error('Package with id: ' . $args['key'] . ' not found', 105);
     }
     if ($success) {
         $res = midgardmvc_account_controllers_activity::create_activity($this->user->person, $args['context'], $package->guid, $summary, 'Apps');
         if (!$res) {
             com_meego_ocs_utils::end_with_error('Failed to create activity object.', 106);
         }
     }
     // everything went fine
     $ocs = new com_meego_ocs_OCSWriter();
     $ocs->writeMeta(null, null, 'Attribute setting succeded.', 'ok', 100);
     $ocs->endDocument();
     com_meego_ocs_utils::output_xml($ocs);
 }
Exemplo n.º 3
0
 /**
  * Process a comment post
  */
 public function post_add(array $args)
 {
     $success = true;
     if (!$this->user) {
         // Voting requires authentication
         $auth = com_meego_ocs_utils::authenticate($args);
         if (!$auth) {
             return null;
         }
     }
     $ocs = new com_meego_ocs_OCSWriter();
     if (!isset($_POST['content'])) {
         $ocs->writeError('Content must not be empty', 101);
         $ocs->endDocument();
         self::output_xml($ocs);
         return;
     }
     if (!(isset($_POST['message']) || isset($_POST['subject']))) {
         $ocs->writeError('Message or subject must not be empty', 102);
         $ocs->endDocument();
         self::output_xml($ocs);
         return;
     }
     if ($_POST['type'] != '1' && $_POST['type'] != '8') {
         $ocs->writeError('Content type: ' . $_POST['type'] . ' is not supported.', 104);
         $ocs->endDocument();
         self::output_xml($ocs);
         return;
     }
     $package = new com_meego_package();
     $package->get_by_id((int) $_POST['content']);
     if (!$package->guid) {
         $success = false;
         $this->mvc->log(__CLASS__, 'Package with id: ' . $_POST['content'] . ' not found.', 'error');
     }
     if ($success) {
         switch ($_POST['type']) {
             case 1:
                 $message = 'Rating via OCS failed. Could not create rating object for package ' . $package->name . '(id: ' . $package->id . ').';
                 $comment = new com_meego_comments_comment();
                 if (isset($_POST['parent']) && !empty($_POST['parent'])) {
                     $parent = new com_meego_comments_comment();
                     $parent->get_by_id((int) $_POST['parent']);
                     if ($parent->to != $package->guid) {
                         $success = false;
                         $this->mvc->log(__CLASS__, $message . ' Parent comment is not related to the content item', 'error');
                     }
                     $comment->up = $parent->id;
                 }
                 $comment->to = $package->guid;
                 $comment->content = $_POST['message'];
                 if (isset($_POST['subject']) && !empty($_POST['subject'])) {
                     $comment->title = $_POST['subject'];
                 }
                 $comment->create();
                 if ($comment->guid) {
                     $rating = new com_meego_ratings_rating();
                     $rating->to = $package->guid;
                     // for comments we have no votes
                     $rating->rating = 0;
                     $rating->comment = $comment->id;
                     $success = $rating->create();
                     if ($success) {
                         $message = 'Rating via OCS finished. New rating object is: ' . $rating->guid . '.';
                     }
                 }
                 break;
             case 8:
                 $name = substr($_POST['message'], 0, strpos($_POST['message'], ':'));
                 $workflows = $this->mvc->configuration->workflows;
                 if (array_key_exists($name, $workflows)) {
                     if (is_object($package)) {
                         $this->mvc->component->load_library('Workflow');
                         $workflow_definition = new $workflows[$name]['provider']();
                         $values = $workflow_definition->start($package);
                         if (array_key_exists('execution', $values)) {
                             // get the db form and fill in the fields
                             $form = new midgardmvc_ui_forms_form($values['review_form']);
                             $transaction = new midgard_transaction();
                             $transaction->begin();
                             $instance = new midgardmvc_ui_forms_form_instance();
                             $instance->form = $form->id;
                             $instance->relatedobject = $package->guid;
                             $instance->create();
                             if (isset($instance->guid)) {
                                 // give values to the db fields taken from the posted values and store each of them
                                 // use the form instance ID as "form" property of the fields
                                 $posted_values = explode(',', substr($_POST['message'], strpos($_POST['message'], ':') + 1));
                                 $db_fields = midgardmvc_ui_forms_generator::list_fields($form);
                                 $i = 0;
                                 foreach ($db_fields as $dbfield) {
                                     if (!$success) {
                                         // if 1 field creation failed then end this loop as fast as possible
                                         continue;
                                     }
                                     switch ($dbfield->widget) {
                                         case 'checkbox':
                                             $holder = "booleanvalue";
                                             $value = $posted_values[$i];
                                             break;
                                         default:
                                             $options = explode(',', $dbfield->options);
                                             $value = $options[(int) $posted_values[$i]];
                                             $holder = "stringvalue";
                                     }
                                     $field_instance = new midgardmvc_ui_forms_form_instance_field();
                                     $field_instance->form = $instance->id;
                                     $field_instance->field = $dbfield->guid;
                                     $field_instance->{$holder} = $value;
                                     if (!$field_instance->create()) {
                                         $success = false;
                                     }
                                     ++$i;
                                 }
                                 if ($success) {
                                     $message = 'QA via OCS by user ' . $this->user->login . ' for package: ' . $package->name . ' (id: ' . $package->id . ')';
                                     try {
                                         $workflow = $workflow_definition->get();
                                         $execution = new midgardmvc_helper_workflow_execution_interactive($workflow, $values['execution']);
                                     } catch (ezcWorkflowExecutionException $e) {
                                         $success = false;
                                         $this->mvc->log(__CLASS__, $message . ' failed. Workflow: ' . $values['workflow'] . ' not found. See error: ' . $e->getMessage(), 'error');
                                     }
                                     if ($success) {
                                         $args = array('review' => $instance->guid);
                                         try {
                                             $values = $workflow_definition->resume($execution->guid, $args);
                                         } catch (ezcWorkflowInvalidInputException $e) {
                                             $success = false;
                                             $this->mvc->log(__CLASS__, $message . ' failed. Maybe a quick re-submit? See error: ' . $e->getMessage(), 'error');
                                         }
                                         $transaction->commit();
                                         $this->mvc->log(__CLASS__, 'New QA form guid: ' . $instance->guid, 'info');
                                     }
                                 }
                             }
                             if (!$success) {
                                 $this->mvc->log(__CLASS__, $message . ' failed. Probably a form instance or a field creation failed.', 'info');
                                 $transaction->rollback();
                             }
                         }
                     }
                 }
                 break;
         }
         if ($success) {
             // POST went fine
             $ocs->writeMeta(null, null, 'Posting succeded.', 'ok', 100);
             $this->mvc->log(__CLASS__, $message, 'info');
             // create activity object
             $created = null;
             switch ($_POST['type']) {
                 case 1:
                     $verb = 'comment';
                     $summary = 'The user commented an application via OCS.';
                     $creator = $rating->metadata->creator;
                     $created = $rating->metadata->created;
                     $target = $rating->to;
                     break;
                 case 8:
                     $verb = 'review';
                     $summary = 'The user reviewed an application via OCS.';
                     $creator = $instance->metadata->creator;
                     $created = $instance->metadata->created;
                     $target = $instance->relatedobject;
                     break;
             }
             if ($created) {
                 $res = midgardmvc_account_controllers_activity::create_activity($creator, $verb, $target, $summary, 'Apps', $created);
             }
             unset($created, $creator, $target);
         }
     }
     if (!$success) {
         $ocs->writeError('Comment posting (type: ' . $_POST['type'] . ') failed.');
         $this->mvc->log(__CLASS__, $message . ' failed.', 'info');
     }
     $ocs->endDocument();
     self::output_xml($ocs);
 }
Exemplo n.º 4
0
 /**
  * End the run with an OCS error message
  */
 public static function end_with_error($message, $status)
 {
     $ocs = new com_meego_ocs_OCSWriter();
     $ocs->writeError($message, $status);
     $ocs->endDocument();
     self::output_xml($ocs);
 }
Exemplo n.º 5
0
 /**
  * Process vote posts
  */
 public function post_vote(array $args)
 {
     $auth = false;
     $ocs = new com_meego_ocs_OCSWriter();
     if ($this->user) {
         $auth = true;
     } else {
         // Voting requires authentication
         $auth = com_meego_ocs_utils::authenticate($args);
     }
     if ($auth) {
         $primary = new com_meego_package();
         $primary->get_by_id((int) $args['contentid']);
         if (!$primary->guid) {
             $this->mvc->log(__CLASS__, 'Package with id:  (with id:' . $args['contentid'] . ') can not be found', 'info');
             $ocs->writeError('Content not found', 101);
         } else {
             $voted = false;
             // the multiple voting is configurable, pls check the config file
             if (!$this->mvc->configuration->allow_multiple_voting) {
                 // if not allowed then check if the user has voted already
                 if (com_meego_ocs_utils::user_has_voted($primary->id, $this->user->person)) {
                     $this->mvc->log(__CLASS__, "{$this->user}->login has already voted for {$primary->name} (with id: {$primary->id}) and multiple votings are disabled", 'info');
                     $ocs->writeError('Multiple voting not allowed and user has already voted this object.', 103);
                 }
             }
             if (!$ocs->error) {
                 $rating = new com_meego_ratings_rating();
                 $rating->to = $primary->guid;
                 $vote = $_POST['vote'];
                 // incoming votes are ranging between 0 and 100
                 // our internal scale is different: 0 - 5
                 $vote = round($vote / 20);
                 if ($vote > $this->mvc->configuration->maxrate) {
                     $vote = $this->mvc->configuration->maxrate;
                 }
                 $rating->rating = $vote;
                 // for votes only we have no comments
                 $rating->comment = 0;
                 if (!$rating->create()) {
                     $this->mvc->log(__CLASS__, 'Failed to create rating object. User: '******', application: ' . $primary->name . ' (with id: ' . $primary->id . ')', 'info');
                     throw new midgardmvc_exception_notfound("Could not create rating object");
                 }
                 $args = array('to' => $rating->to);
                 com_meego_ratings_caching_controllers_rating::calculate_average($args);
                 $ocs->writeMeta(0);
                 $this->mvc->log(__CLASS__, 'Rating (' . $rating->rating . ') submitted by ' . $this->user->login . ' for ' . $primary->name . ' (with id: ' . $primary->id . ')', 'info');
                 // create activity object
                 $verb = 'rate';
                 $summary = 'The user rated an application via OCS.';
                 $res = midgardmvc_account_controllers_activity::create_activity($rating->metadata->creator, $verb, $rating->to, $summary, 'Apps', $rating->metadata->created);
             }
         }
     } else {
         // extend the OCS spec with a custom status code
         $this->mvc->log(__CLASS__, 'Attempt to vote by anonymous. No luck.', 'info');
         $ocs->writeError('Voting requires authentication. Please login first.', 102);
     }
     $ocs->endDocument();
     self::output_xml($ocs);
 }