<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/>.
*/

/**
* alloc API
*
* A public interface for alloc code
* @author <NAME>
* @version 1.0
*/
class services {

  public function __construct($sessID="") {
    $current_user = $this->get_current_user($sessID);
    singleton("current_user",$current_user);
  }

  /**
  * Perform an authentication check, start a new session
  * @param string $username
  * @param string $password
  * @return string the session key
  */
  public function authenticate($username,$password) {
    $person = new person();
    $sess = new session();
    $row = $person->get_valid_login_row($username,$password); 
    if ($row) {
      $sess->Start($row,false);
      $sess->UseGet();
      $sess->Save();
      return $sess->GetKey();
    } else {
      die("Authentication Failed(1)."); 
    }
  }  

  private function get_current_user($sessID) {
    $sess = new session($sessID);
    if ($sess->Started()) {
      $person = new person();
      $person->load_current_user($sess->Get("personID"));
      // update session_started, which affects session lifetime
      $sess->Save();
      return $person;
    }
  }

  /**
  * Get all the commments on a task
  * @param string $taskID
  * @return array an array of comments
  */
  public function get_task_comments($taskID) {
    if ($taskID) {
      $task = new task();
      $task->set_id($taskID);
      $task->select();
      return $task->get_task_comments_array();
    }
  }

  /**
  * Convert a comma separated string of names, into an array with email addresses
  * @param string $people
  * @param string $entity the related entity that can assist in the look up
  * @param integer $entityID the id of the related entity
  * @return array an array of people, indexed by their email address
  */
  public function get_people($options=array(), $entity="", $entityID="") {
    $person_table =& get_cached_table("person");
    $people = $options;

    if ($entity && $entityID) {
      $e = new $entity;
      $e->set_id($entityID);
      $e->select();
      in_array("default",$people)  and $default_recipients  = $e->get_all_parties();
      in_array("internal",$people) and $internal_recipients = $e->get_all_parties();
    }

    // remove default and internal from the array
    $clean_people = array_diff($people, array("default", "internal"));

    if (is_object($e)) {
      $projectID = $e->get_project_id();
      $p = new project();
      $p->set_id($projectID);
      $p->select();
      $client = $p->get_foreign_object("client");
      $clientID = $client->get_id();
    }

    foreach ((array)$default_recipients as $email => $info) {
      if ($info["selected"]) {
        $rtn[$email] = $this->reduce_person_info($info);
      }
    }

    foreach ((array)$internal_recipients as $email => $info) {
      if ($info["selected"] && !$info["external"]) {
        $rtn[$email] = $this->reduce_person_info($info);
      }
    }

    foreach ((array)$clean_people as $person) {
      $bad_person = true;
      $person = trim($person);

      // personID
      if (is_numeric($person)) {
        if ($person_table[$person]["personActive"]) {
          $rtn[$person_table[$person]["emailAddress"]] = $person_table[$person];
          $bad_person = false;
          continue;
        }

      // email addresses
      } else if (in_str("@",$person)) {
        foreach ($person_table as $pid => $data) {
          if (same_email_address($person,$data["emailAddress"]) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }
        
        if ($ccID = clientContact::find_by_email($person)) {
          $cc = new clientContact();
          $cc->set_id($ccID);
          $cc->select();
          $rtn[$cc->get_value("clientContactEmail")] = $cc->row();
          $bad_person = false;
          continue;
        }

        // If we get here, then return the email address entered
        list($e, $n) = parse_email_address($person);
        $rtn[$e] = array("emailAddress"=>$e, "name"=>$n);
        $bad_person = false;
        continue;

      // usernames, partial and full names
      } else {

        // Note the third check against partial: firstname." ".surname. Because $data["name"] will default back to username
        foreach ($person_table as $pid => $data) {
          if ((strtolower($person) == strtolower($data["username"])
            || strtolower($person) == strtolower($data["name"])
            || strtolower($person) == strtolower(substr(strtolower($data["firstName"]." ".$data["surname"]),0,strlen($person)))
          ) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }

        if ($ccID = clientContact::find_by_nick($person,$clientID)) {
          $cc = new clientContact();
          $cc->set_id($ccID);
          $cc->select();<commit_msg>v187 22774 alloc-cli make --to search usernames and fullnames correctly.<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/>.
*/

/**
* alloc API
*
* A public interface for alloc code
* @author <NAME>
* @version 1.0
*/
class services {

  public function __construct($sessID="") {
    $current_user = $this->get_current_user($sessID);
    singleton("current_user",$current_user);
  }

  /**
  * Perform an authentication check, start a new session
  * @param string $username
  * @param string $password
  * @return string the session key
  */
  public function authenticate($username,$password) {
    $person = new person();
    $sess = new session();
    $row = $person->get_valid_login_row($username,$password); 
    if ($row) {
      $sess->Start($row,false);
      $sess->UseGet();
      $sess->Save();
      return $sess->GetKey();
    } else {
      die("Authentication Failed(1)."); 
    }
  }  

  private function get_current_user($sessID) {
    $sess = new session($sessID);
    if ($sess->Started()) {
      $person = new person();
      $person->load_current_user($sess->Get("personID"));
      // update session_started, which affects session lifetime
      $sess->Save();
      return $person;
    }
  }

  /**
  * Get all the commments on a task
  * @param string $taskID
  * @return array an array of comments
  */
  public function get_task_comments($taskID) {
    if ($taskID) {
      $task = new task();
      $task->set_id($taskID);
      $task->select();
      return $task->get_task_comments_array();
    }
  }

  /**
  * Convert a comma separated string of names, into an array with email addresses
  * @param string $people
  * @param string $entity the related entity that can assist in the look up
  * @param integer $entityID the id of the related entity
  * @return array an array of people, indexed by their email address
  */
  public function get_people($options=array(), $entity="", $entityID="") {
    $person_table =& get_cached_table("person");
    $people = $options;

    if ($entity && $entityID) {
      $e = new $entity;
      $e->set_id($entityID);
      $e->select();
      in_array("default",$people)  and $default_recipients  = $e->get_all_parties();
      in_array("internal",$people) and $internal_recipients = $e->get_all_parties();
    }

    // remove default and internal from the array
    $clean_people = array_diff($people, array("default", "internal"));

    if (is_object($e)) {
      $projectID = $e->get_project_id();
      $p = new project();
      $p->set_id($projectID);
      $p->select();
      $client = $p->get_foreign_object("client");
      $clientID = $client->get_id();
    }

    foreach ((array)$default_recipients as $email => $info) {
      if ($info["selected"]) {
        $rtn[$email] = $this->reduce_person_info($info);
      }
    }

    foreach ((array)$internal_recipients as $email => $info) {
      if ($info["selected"] && !$info["external"]) {
        $rtn[$email] = $this->reduce_person_info($info);
      }
    }

    foreach ((array)$clean_people as $person) {
      $bad_person = true;
      $person = trim($person);

      // personID
      if (is_numeric($person)) {
        if ($person_table[$person]["personActive"]) {
          $rtn[$person_table[$person]["emailAddress"]] = $person_table[$person];
          $bad_person = false;
          continue;
        }

      // email addresses
      } else if (in_str("@",$person)) {
        foreach ($person_table as $pid => $data) {
          if (same_email_address($person,$data["emailAddress"]) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }
        
        if ($ccID = clientContact::find_by_email($person)) {
          $cc = new clientContact();
          $cc->set_id($ccID);
          $cc->select();
          $rtn[$cc->get_value("clientContactEmail")] = $cc->row();
          $bad_person = false;
          continue;
        }

        // If we get here, then return the email address entered
        list($e, $n) = parse_email_address($person);
        $rtn[$e] = array("emailAddress"=>$e, "name"=>$n);
        $bad_person = false;
        continue;

      // usernames, partial and full names
      } else {

        foreach ($person_table as $pid => $data) {
          // If matches username
          if (strtolower($person) == strtolower($data["username"]) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }
        foreach ($person_table as $pid => $data) {
          // If matches name
          if (strtolower($person) == strtolower($data["firstName"]." ".$data["surname"]) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }
        foreach ($person_table as $pid => $data) {
          // If matches a section of name, eg: a search for "Ale" will match the full name "Alex Lance"
          if (strtolower($person) == strtolower(substr(strtolower($data["firstName"]." ".$data["surname"]),0,strlen($person))) && $data["personActive"]) {
            $rtn[$data["emailAddress"]] = $data;
            $bad_person = false;
            continue 2;
          }
        }

        if ($ccID = clientContact::find_by_nick($person,$clientID)) {
          $cc = new clientContact();
          $cc->set_id($ccID);
          $cc->select();
{'fraction_non_alphanumeric': 0.15066666666666667, 'fraction_numerical': 0.0033333333333333335, 'mean_word_length': 2.6903444034440342, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083270', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3580, 'n_words': 970}