Esempio n. 1
0
 /**
  * @param mixed $handler_id The ID of the handler.
  * @param Array $args The argument list.
  * @param Array &$data The local request data.
  */
 public function _handler_import($handler_id, array $args, array &$data)
 {
     if (!$this->_config->get('api_email_enable')) {
         throw new midcom_error('Email API is disabled');
     }
     if ($handler_id === 'api-email-basicauth') {
         midcom::get('auth')->require_valid_user('basic');
     }
     //Content-Type
     midcom::get()->skip_page_style = true;
     midcom::get('cache')->content->content_type('text/plain');
     if (!isset($this->_request_data['schemadb'][$this->_config->get('api_email_schema')])) {
         throw new midcom_error('Schema "' . $this->_config->get('api_email_schema') . '" not found in schemadb "' . $this->_config->get('schemadb') . '"');
     }
     $schema_instance =& $this->_request_data['schemadb'][$this->_config->get('api_email_schema')];
     // Parse email
     $this->_decode_email();
     $this->_parse_email_persons();
     midcom::get('auth')->request_sudo('net.nehmer.blog');
     // Create article
     $this->_create_article($this->_decoder->subject);
     // Load the article to DM2
     $this->_load_datamanager();
     // Find image and tag fields in schema
     foreach ($schema_instance->fields as $name => $field) {
         if (is_a($this->_datamanager->types[$name], 'midcom_helper_datamanager2_type_image')) {
             $this->_request_data['image_field'] = $name;
             continue;
         }
         if (is_a($this->_datamanager->types[$name], 'midcom_helper_datamanager2_type_tags')) {
             $data['tags_field'] = $name;
             continue;
         }
     }
     // Try to find tags in email content
     $content = $this->_decoder->body;
     $content_tags = '';
     midcom::get('componentloader')->load_graceful('net.nemein.tag');
     if (class_exists('net_nemein_tag_handler')) {
         // unconditionally tag
         debug_add("content before machine tag separation\n===\n{$content}\n===\n");
         $content_tags = net_nemein_tag_handler::separate_machine_tags_in_content($content);
         if (!empty($content_tags)) {
             debug_add("found machine tags string: {$content_tags}");
             net_nemein_tag_handler::tag_object($this->_article, net_nemein_tag_handler::string2tag_array($content_tags));
         }
         debug_add("content AFTER machine tag separation\n===\n{$content}\n===\n");
     }
     // Populate rest of the data
     $this->_datamanager->types['content']->value = $content;
     if (!empty($data['tags_field'])) {
         // if we have tags field put content_tags value there as well or they will get deleted!
         $this->_datamanager->types[$data['tags_field']]->value = $content_tags;
     }
     $body_switched = false;
     foreach ($this->_decoder->attachments as $att) {
         debug_add("processing attachment {$att['name']}");
         switch (true) {
             case strpos($att['mimetype'], 'image/') !== false:
                 $this->_add_image($att);
                 break;
             case strtolower($att['mimetype']) == 'text/plain':
                 if (!$body_switched) {
                     // Use first text/plain part as the content
                     $this->_datamanager->types['content']->value = $att['content'];
                     $body_switched = true;
                     break;
                 }
                 // Fall-through if not switching
             // Fall-through if not switching
             default:
                 $this->_add_attachment($att);
         }
     }
     if (!$this->_datamanager->save()) {
         // Remove the article, but get errstr first
         $errstr = midcom_connection::get_error_string();
         $this->_article->delete();
         throw new midcom_error('DM2 failed to save the article. Last Midgard error was: ' . $errstr);
     }
     // Index the article
     $indexer = midcom::get('indexer');
     net_nehmer_blog_viewer::index($this->_datamanager, $indexer, $this->_content_topic);
     if ($this->_config->get('api_email_autoapprove')) {
         $metadata = midcom_helper_metadata::retrieve($this->_article);
         if (!$metadata->force_approve()) {
             // Remove the article, but get errstr first
             $errstr = midcom_connection::get_error_string();
             $this->_article->delete();
             throw new midcom_error('Failed to force approval on article. Last Midgard error was: ' . $errstr);
         }
     }
     midcom::get('auth')->drop_sudo();
 }