<commit_before><?php

/*
 * Copyright (C) 2006-2011 <NAME>, <NAME>, Cyber IT Solutions
 * Pty. Ltd.
 * 
 * This file is part of the allocPSA application <<EMAIL>>.
 * 
 * allocPSA is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * allocPSA is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with allocPSA. If not, see <http://www.gnu.org/licenses/>.
*/

class command {

  var $commands;
  var $email_receive;

  function get_help($type) {
    $message = ucwords($type)." fields:";
    $fields = command::get_fields($type);
    foreach ((array)$fields as $k => $arr) {
      $message.= "\n      ".$k.":\t".$arr[1];
    }
    return $message;
  }

  function get_fields($type="all") {

    $comment = array(
      "key"   => array("","The comment thread key.")
     ,"ip"    => array("","Add to a comment's interested parties. Eg: jon,<EMAIL>,Hal Comp <<EMAIL>> ... can also remove eg: -jon")
     ,"quiet" => array("","Don't resend the email back out to anyone.")
    );

    $item = array(
      "tsid"      => array("timeSheetID",           "time sheet that this item belongs to")
     ,"date"      => array("dateTimeSheetItem",     "time sheet item's date")
     ,"duration"  => array("timeSheetItemDuration", "time sheet item's duration")
     ,"unit"      => array("timeSheetItemDurationUnitID", "time sheet item's unit of duration eg: 1=hours 2=days 3=week 4=month 5=fixed")
     ,"task"      => array("taskID",                "ID of the time sheet item's task")
     ,"rate"      => array("rate",                  "\$rate of the time sheet item's")
     ,"private"   => array("commentPrivate",        "privacy setting of the time sheet item's comment eg: 1=private 0=normal")
     ,"comment"   => array("comment",               "time sheet item comment")
     ,"multiplier"=> array("multiplier",            "time sheet item multiplier eg: 1=standard 1.5=time-and-a-half 2=double-time 3=triple-time 0=no-charge")
     ,"delete"    => array("",                      "set this to 1 to delete the time sheet item")
     ,"time"      => array("",                      "Add some time to a time sheet. Auto-creates time sheet if none exist.")
    );

    $task = array(
      "status"    => array("taskStatus",      "inprogress, notstarted, info, client, manager, invalid, duplicate, incomplete, complete; or: open, pending, closed")
     ,"name"      => array("taskName",        "task's title")
     ,"assign"    => array("personID",        "username of the person that the task is assigned to")
     ,"manage"    => array("managerID",       "username of the person that the task is managed by")
     ,"desc"      => array("taskDescription", "task's long description")
     ,"priority"  => array("priority",        "1, 2, 3, 4 or 5; or one of Wishlist, Minor, Normal, Important or Critical")
     ,"limit"     => array("timeLimit",       "limit in hours for effort spend on this task")
     ,"best"      => array("timeBest",        "shortest estimate of how many hours of effort this task will take")
     ,"likely"    => array("timeExpected",    "most likely amount of hours of effort this task will take")
     ,"worst"     => array("timeWorst",       "longest estimate of how many hours of effort this task will take")
     ,"project"   => array("projectID",       "task's project ID")
     ,"type"      => array("taskTypeID",      "Task, Fault, Message, Milestone or Parent")
     ,"dupe"      => array("duplicateTaskID", "If the task status is duplicate, then this should be set to the task ID of the related dupe")
     ,"task"      => array("",                "A task ID, or the word 'new' to create a new task.")
     ,"taskip"    => array("",                "Add some interested parties and send the desc to them.")
    );

    $types = array("all"=>array_merge($comment,$item,$task)
                  ,"comment"=>$comment
                  ,"item"=>$item
                  ,"task"=>$task);
    return $types[$type];
  }

  function run_commands($commands=array(), $email_receive=false) {
    global $current_user;
    $task_fields = $this->get_fields("task");
    $item_fields = $this->get_fields("item");

    // If there's Key in the email, then add a comment with the contents of the email.
    if ($commands["key"]) {
      $token = new token;
      if ($token->set_hash($commands["key"])) {

        global $guest_permission_cache, $current_user;
        // If sent from a client or someone we don't recognize, we need to imbue this process with guest perms
        $comment = $token->get_value("tokenEntity");
        $commentID = $token->get_value("tokenEntityID");
        $q = sprintf("SELECT commentMaster,commentMasterID FROM comment WHERE commentID = %d",$commentID);
        $db = new db_alloc();
        $r = $db->qr($q);
        $master = $r["commentMaster"];
        $masterID = $r["commentMasterID"];

        if (!$current_user) {
          // Used in db_entity::have_perm();
          global $guest_permission_cache;
          // Hard code some additional permissions for this guest user (just enough to create a comment)
          $guest_permission_cache[] = array("entity"=>$comment,"entityID"=>$commentID,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>$comment,"entityID"=>0,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>$master ,"entityID"=>$masterID,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>"indexQueue","entityID"=>0,"perms"=>15);
        }

        list($entity,$method) = $token->execute();
        if (is_object($entity) && $method == "add_comment_from_email") {

          $c = comment::add_comment_from_email($email_receive,$entity);

          if (is_object($c) && $c->get_id()) {
            $quiet = interestedParty::adjust_by_email_subject($email_receive,$entity);

            if ($commands["ip"]) {
              $rtn = interestedParty::add_remove_ips($commands["ip"],$entity->classname,$entity->get_id(),$entity->get_project_id());
            }

            if (!$quiet) {
              comment::send_comment($c->get_id(),array("interested"),$email_receive);
            }
          }
        }
      }
    // No key, then error
    } else if ($email_receive) {
      return array("status"=>"msg","message"=>"No key found. Unable to process email.");
    }


    // If there's a number/duration then add some time to a time sheet
    if ($commands["time"]) {

      // CLI passes time along as a string, email passes time along as an array
      if (!is_array($commands["time"])) {
        $t = $commands["time"];
        unset($commands["time"]);<commit_msg>20955 v184 Ensure emails with no key are forwarded to admin address.<commit_after><?php

/*
 * Copyright (C) 2006-2011 <NAME>, <NAME>, Cyber IT Solutions
 * Pty. Ltd.
 * 
 * This file is part of the allocPSA application <<EMAIL>>.
 * 
 * allocPSA is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * allocPSA is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with allocPSA. If not, see <http://www.gnu.org/licenses/>.
*/

class command {

  var $commands;
  var $email_receive;

  function get_help($type) {
    $message = ucwords($type)." fields:";
    $fields = command::get_fields($type);
    foreach ((array)$fields as $k => $arr) {
      $message.= "\n      ".$k.":\t".$arr[1];
    }
    return $message;
  }

  function get_fields($type="all") {

    $comment = array(
      "key"   => array("","The comment thread key.")
     ,"ip"    => array("","Add to a comment's interested parties. Eg: jon,<EMAIL>,<NAME> <<EMAIL>> ... can also remove eg: -jon")
     ,"quiet" => array("","Don't resend the email back out to anyone.")
    );

    $item = array(
      "tsid"      => array("timeSheetID",           "time sheet that this item belongs to")
     ,"date"      => array("dateTimeSheetItem",     "time sheet item's date")
     ,"duration"  => array("timeSheetItemDuration", "time sheet item's duration")
     ,"unit"      => array("timeSheetItemDurationUnitID", "time sheet item's unit of duration eg: 1=hours 2=days 3=week 4=month 5=fixed")
     ,"task"      => array("taskID",                "ID of the time sheet item's task")
     ,"rate"      => array("rate",                  "\$rate of the time sheet item's")
     ,"private"   => array("commentPrivate",        "privacy setting of the time sheet item's comment eg: 1=private 0=normal")
     ,"comment"   => array("comment",               "time sheet item comment")
     ,"multiplier"=> array("multiplier",            "time sheet item multiplier eg: 1=standard 1.5=time-and-a-half 2=double-time 3=triple-time 0=no-charge")
     ,"delete"    => array("",                      "set this to 1 to delete the time sheet item")
     ,"time"      => array("",                      "Add some time to a time sheet. Auto-creates time sheet if none exist.")
    );

    $task = array(
      "status"    => array("taskStatus",      "inprogress, notstarted, info, client, manager, invalid, duplicate, incomplete, complete; or: open, pending, closed")
     ,"name"      => array("taskName",        "task's title")
     ,"assign"    => array("personID",        "username of the person that the task is assigned to")
     ,"manage"    => array("managerID",       "username of the person that the task is managed by")
     ,"desc"      => array("taskDescription", "task's long description")
     ,"priority"  => array("priority",        "1, 2, 3, 4 or 5; or one of Wishlist, Minor, Normal, Important or Critical")
     ,"limit"     => array("timeLimit",       "limit in hours for effort spend on this task")
     ,"best"      => array("timeBest",        "shortest estimate of how many hours of effort this task will take")
     ,"likely"    => array("timeExpected",    "most likely amount of hours of effort this task will take")
     ,"worst"     => array("timeWorst",       "longest estimate of how many hours of effort this task will take")
     ,"project"   => array("projectID",       "task's project ID")
     ,"type"      => array("taskTypeID",      "Task, Fault, Message, Milestone or Parent")
     ,"dupe"      => array("duplicateTaskID", "If the task status is duplicate, then this should be set to the task ID of the related dupe")
     ,"task"      => array("",                "A task ID, or the word 'new' to create a new task.")
     ,"taskip"    => array("",                "Add some interested parties and send the desc to them.")
    );

    $types = array("all"=>array_merge($comment,$item,$task)
                  ,"comment"=>$comment
                  ,"item"=>$item
                  ,"task"=>$task);
    return $types[$type];
  }

  function run_commands($commands=array(), $email_receive=false) {
    global $current_user;
    $task_fields = $this->get_fields("task");
    $item_fields = $this->get_fields("item");

    // If there's Key in the email, then add a comment with the contents of the email.
    if ($commands["key"]) {
      $token = new token;
      if ($token->set_hash($commands["key"])) {

        global $guest_permission_cache, $current_user;
        // If sent from a client or someone we don't recognize, we need to imbue this process with guest perms
        $comment = $token->get_value("tokenEntity");
        $commentID = $token->get_value("tokenEntityID");
        $q = sprintf("SELECT commentMaster,commentMasterID FROM comment WHERE commentID = %d",$commentID);
        $db = new db_alloc();
        $r = $db->qr($q);
        $master = $r["commentMaster"];
        $masterID = $r["commentMasterID"];

        if (!$current_user) {
          // Used in db_entity::have_perm();
          global $guest_permission_cache;
          // Hard code some additional permissions for this guest user (just enough to create a comment)
          $guest_permission_cache[] = array("entity"=>$comment,"entityID"=>$commentID,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>$comment,"entityID"=>0,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>$master ,"entityID"=>$masterID,"perms"=>15);
          $guest_permission_cache[] = array("entity"=>"indexQueue","entityID"=>0,"perms"=>15);
        }

        list($entity,$method) = $token->execute();
        if (is_object($entity) && $method == "add_comment_from_email") {

          $c = comment::add_comment_from_email($email_receive,$entity);

          if (is_object($c) && $c->get_id()) {
            $quiet = interestedParty::adjust_by_email_subject($email_receive,$entity);

            if ($commands["ip"]) {
              $rtn = interestedParty::add_remove_ips($commands["ip"],$entity->classname,$entity->get_id(),$entity->get_project_id());
            }

            if (!$quiet) {
              comment::send_comment($c->get_id(),array("interested"),$email_receive);
            }
          }
        }
      }
    // No key, then error
    } else if ($email_receive) {
      return array("status"=>"err","message"=>"No key found. Unable to process email.");
    }


    // If there's a number/duration then add some time to a time sheet
    if ($commands["time"]) {

      // CLI passes time along as a string, email passes time along as an array
      if (!is_array($commands["time"])) {
        $t = $commands["time"];
        unset($commands["time"]);
{'fraction_non_alphanumeric': 0.14786294595549276, 'fraction_numerical': 0.006075591663723066, 'mean_word_length': 2.78705189941145, 'pattern_counts': {'":': 2, '<': 22, '<?xml version=': 0, '>': 140, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300336', 'n_tokens_mistral': 4183, 'n_tokens_neox': 3983, 'n_words': 1305}