/**
 * do frequently backup
 *
 * @param null
 * @return null
 */
function incoming_mail_handle_on_frequently()
{
    set_time_limit(0);
    require_once INCOMING_MAIL_MODULE_PATH . '/models/IncomingMailImporter.class.php';
    require_once ANGIE_PATH . '/classes/UTF8Converter/init.php';
    require_once ANGIE_PATH . '/classes/mailboxmanager/init.php';
    $mailboxes = IncomingMailboxes::findAllActive();
    IncomingMailImporter::importEmails($mailboxes, 20);
}
 /**
  * Conflict incoming mail
  * 
  * @param void
  * @return void
  */
 function conflict()
 {
     if ($this->active_mail->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     require_once INCOMING_MAIL_MODULE_PATH . '/models/IncomingMailImporter.class.php';
     $mail_data = $this->request->post('mail');
     if (!is_foreachable($mail_data)) {
         flash_error(incoming_mail_module_get_status_description($this->active_mail->getState()));
         $mail_data = array('subject' => $this->active_mail->getSubject(), 'body' => $this->active_mail->getBody(), 'created_by_id' => $this->active_mail->getCreatedById(), 'project_id' => $this->active_mail->getProjectId());
     }
     // if
     if ($this->request->isSubmitted()) {
         $this->active_mail->setSubject(array_var($mail_data, 'subject'));
         $this->active_mail->setBody(array_var($mail_data, 'body'));
         $creator_id = array_var($mail_data, 'created_by_id');
         if ($creator_id && $creator_id != 'original_author') {
             $creator = Users::findById($creator_id);
             if (instance_of($creator, 'User')) {
                 $this->active_mail->setCreatedBy($creator);
             }
             // if
         }
         // if
         $this->active_mail->setCreatedById(array_var($mail_data, 'created_by_id'));
         $this->active_mail->setObjectType(array_var($mail_data, 'object_type'));
         if (array_var($mail_data, 'object_type') == 'comment') {
             $this->active_mail->setParentId(array_var($mail_data, 'parent_id'));
         }
         // if
         // import email
         if (instance_of($importing_result = IncomingMailImporter::importPendingEmail($this->active_mail, $creator_id == 'original_author'), 'ProjectObject')) {
             // we have successfully imported email
             $this->active_mail->delete();
             if ($this->request->isAsyncCall()) {
                 $this->renderText(lang('<p>Conflict Solved Successfully!</p><p>View created <a href=":url">:object</a>.</p>', array('object' => $this->active_mail->getObjectType(), 'url' => $importing_result->getViewUrl())));
             } else {
                 flash_success('Conflict Solved Successfully!');
                 $this->redirectTo('incoming_mail');
             }
             // if
         } else {
             if ($this->request->isAsyncCall()) {
                 $this->httpError(HTTP_ERR_INVALID_PROPERTIES, null, false, 2);
             } else {
                 flash_error($importing_result->getMessage());
             }
             // if
         }
         // if
     }
     // if
     $user = $this->active_mail->getCreatedBy();
     if (instance_of($user, 'User')) {
         $this->smarty->assign('object_user', $user);
     } else {
         $this->smarty->assign('object_user', $this->logged_user);
     }
     // if
     $this->smarty->assign(array('async' => $this->request->isAsyncCall(), 'form_url' => $this->active_mail->getImportUrl() . ($this->request->isAsyncCall() ? '?skip_layout=1&async=1' : ''), 'status_message' => incoming_mail_module_get_status_description($this->active_mail->getState()), 'mail_data' => $mail_data, 'project' => $this->active_mail->getProject()));
     $flash =& Flash::instance();
     $flash->init();
     js_assign('additional_fields_url', assemble_url('incoming_mail_additional_form_fields'));
 }
 function importPendingEmailAsComment(&$incoming_mail, &$project, &$user, &$mailbox, $page_id = '')
 {
     $parent = ProjectObjects::findById(!empty($page_id) ? $page_id : $incoming_mail->getParentId());
     //EOF:mod 20120820
     if (!instance_of($parent, 'ProjectObject')) {
         // parent object does not exists
         $incoming_mail->setState(INCOMING_MAIL_STATUS_PARENT_NOT_EXISTS);
         $incoming_mail_save = $incoming_mail->save();
         return new Error(incoming_mail_module_get_status_description(INCOMING_MAIL_STATUS_PARENT_NOT_EXISTS));
     }
     // if
     if (!$mailbox->getAcceptAllRegistered() && instance_of($user, 'User') && !$parent->canComment($user)) {
         // user cannot create comments to parent object
         $incoming_mail->setState(INCOMING_MAIL_STATUS_USER_CANNOT_CREATE_COMMENT);
         $incoming_mail_save = $incoming_mail->save();
         return new Error(incoming_mail_module_get_status_description(INCOMING_MAIL_STATUS_USER_CANNOT_CREATE_COMMENT));
     } else {
         if (!$parent->can_have_comments || $parent->getIsLocked() || $parent->getState() < STATE_VISIBLE) {
             // parent object can't have comments
             $incoming_mail->setState(INCOMING_MAIL_STATUS_USER_CANNOT_CREATE_COMMENT);
             $incoming_mail_save = $incoming_mail->save();
             return new Error(incoming_mail_module_get_status_description(INCOMING_MAIL_STATUS_USER_CANNOT_CREATE_COMMENT));
         }
         // if
     }
     // if
     $comment = new Comment();
     $comment->log_activities = false;
     $comment->setCreatedBy($user);
     $comment->setCreatedOn($incoming_mail->getCreatedOn());
     $comment->setProjectId($parent->getProjectId());
     $comment->setState(STATE_VISIBLE);
     $comment->setSource(OBJECT_SOURCE_EMAIL);
     $comment->setVisibility($parent->getVisibility());
     $comment->setParent($parent);
     $body_content = '';
     if (stripos($incoming_mail->getBody(), '-- REPLY ABOVE THIS LINE --') !== false) {
         $body_content = substr($incoming_mail->getBody(), 0, strpos($incoming_mail->getBody(), '-- REPLY ABOVE THIS LINE --'));
     } else {
         $body_content = $incoming_mail->getBody();
     }
     $comment->setBody($body_content);
     IncomingMailImporter::attachFilesToProjectObject($incoming_mail, $comment);
     //$save = $comment->save();
     $save = $comment->save(true);
     if ($save && !is_error($save)) {
         $activity = new NewCommentActivityLog();
         $activity->log($comment, $user);
         if (instance_of($user, 'User')) {
             $parent->subscribe($user);
         }
         // if
         $comment->ready();
         //BOF:mod 20111110 #493
         preg_match("/\\[CID(.*?)\\](.*)/is", $incoming_mail->getSubject(), $results);
         if (count($results) > 0) {
             $project = new Project($parent->getProjectId());
             $variables = array('owner_company_name' => get_owner_company(), 'project_name' => $project->getName(), 'project_url' => $project->getOverviewUrl(), 'object_type' => $comment->getVerboseType(), 'object_name' => $comment->getName(), 'object_body' => $comment->getFormattedBody(), 'object_url' => $comment->getViewUrl(), 'comment_body' => $comment->getFormattedBody(), 'comment_url' => $comment->getViewUrl(), 'created_by_url' => $user->getViewUrl(), 'created_by_name' => $user->getDisplayName(), 'details_body' => '', 'comment_id' => $comment->getId());
             $emailed_comment_id = $results[1];
             $emailed_comment = new Comment($emailed_comment_id);
             $emailed_comment_creator_id = $emailed_comment->getCreatedById();
             $email_to = array();
             $temp_user_id = $user->getId();
             $temp_comment_id = $comment->getId();
             $rows = db_execute_all("select user_id from " . TABLE_PREFIX . "assignments_action_request where comment_id='" . $emailed_comment_id . "' and marked_for_email='1'");
             foreach ($rows as $row) {
                 if ($row['user_id'] != $temp_user_id) {
                     $email_to[] = new User($row['user_id']);
                     db_execute("insert into " . TABLE_PREFIX . "assignments_action_request (user_id, marked_for_email, selected_by_user_id, comment_id, date_added) values ('" . $row['user_id'] . "', '1', '" . $temp_user_id . "', '" . $temp_comment_id . "', now())");
                 }
             }
             $row = db_execute_one("select a.selected_by_user_id from " . TABLE_PREFIX . "assignments_action_request a where a.comment_id='" . $emailed_comment_id . "' and a.marked_for_email='1' and a.selected_by_user_id not in (select b.user_id from " . TABLE_PREFIX . "assignments_action_request b where b.comment_id='" . $emailed_comment_id . "' and b.marked_for_email='1') limit 0, 1");
             if (!empty($row['selected_by_user_id'])) {
                 if ($row['selected_by_user_id'] != $temp_user_id) {
                     $email_to[] = new User($row['selected_by_user_id']);
                     db_execute("insert into " . TABLE_PREFIX . "assignments_action_request (user_id, marked_for_email, selected_by_user_id, comment_id, date_added) values ('" . $row['selected_by_user_id'] . "', '1', '" . $temp_user_id . "', '" . $temp_comment_id . "', now())");
                 }
             }
             //ApplicationMailer::send(array(new User($emailed_comment_creator_id)), 'resources/new_comment', $variables, $parent);
             $attachments = null;
             $object_attachments = $comment->getAttachments();
             if ($object_attachments) {
                 $attachments = array();
                 foreach ($object_attachments as $object_attachment) {
                     $attachments[] = array('path' => $object_attachment->getFilePath(), 'name' => $object_attachment->getName(), 'mime_type' => $object_attachment->getMimeType());
                 }
             }
             ApplicationMailer::send($email_to, 'resources/new_comment', $variables, $parent, $attachments);
         }
         //EOF:mod 20111110 #493
         if (!empty($page_id)) {
             //$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
             //mysql_select_db(DB_NAME, $link);
             //mysql_query("insert into testing (date_added, content) values (now(), 'Page_id: " . $page_id . "')");
             //mysql_close($link);
             $task =& IncomingMailImporter::importPendingEmailToTaskList($incoming_mail, $project, $user, $page_id, $comment);
             return $task;
         } else {
             return $comment;
         }
     }
     // if
     return $save;
 }