Exemple #1
0
 /**
  * Functional point that add a new contact to the external address book of the authenticated user
  *
  * @param   integer   $phone            Can be 'internal' or 'external'
  * @param   string    $first_name       (Optional) First name of the new contact
  * @param   string    $last_name        (Optional) Last name of the new contact
  * @param   string    $email            (Optional) Email of the new contact
  * @return  boolean   True if the contact was successfully added, or false if an error exists
  */
 function addAddressBookContact($phone, $first_name, $last_name, $email, $getIdInserted = FALSE, $address = NULL, $company = NULL, $notes = NULL, $status = NULL, $cell_phone = NULL, $home_phone = NULL, $fax1 = NULL, $fax2 = NULL, $province = NULL, $city = NULL, $company_contact = NULL, $contact_rol = NULL, $picture = NULL)
 {
     global $arrConf;
     if (!$this->_checkUserAuthorized('address_book')) {
         return false;
     }
     $dbAddressBook = $this->_getDB($arrConf['dsn_conn_database']);
     $addressBook = new paloAdressBook($dbAddressBook);
     // Obtener el ID del usuario logoneado
     $id_user = $this->_leerIdUser();
     if (is_null($id_user)) {
         return false;
     }
     // Validar que el teléfono está presente y es numérico
     if (!isset($phone) || !preg_match('/^\\d+$/', $phone)) {
         $this->errMsg["fc"] = 'PARAMERROR';
         $this->errMsg["fm"] = 'Invalid format';
         $this->errMsg["fd"] = 'Invalid phone, must be numeric string';
         $this->errMsg["cn"] = get_class($this);
         return false;
     }
     if (isset($cell_phone) && !preg_match('/^[\\*|#]*[[:digit:]]*$/', $cell_phone)) {
         $this->errMsg["fc"] = 'PARAMERROR';
         $this->errMsg["fm"] = 'Invalid format';
         $this->errMsg["fd"] = 'Invalid cell phone, it must be a numeric string and can only contain at the beginning * or #';
         $this->errMsg["cn"] = get_class($this);
         return false;
     }
     if (isset($home_phone) && !preg_match('/^[\\*|#]*[[:digit:]]*$/', $home_phone)) {
         $this->errMsg["fc"] = 'PARAMERROR';
         $this->errMsg["fm"] = 'Invalid format';
         $this->errMsg["fd"] = 'Invalid home phone, it must be a numeric string and can only contain at the beginning * or #';
         $this->errMsg["cn"] = get_class($this);
         return false;
     }
     if (isset($fax1) && !preg_match('/^[\\*|#]*[[:digit:]]*$/', $fax1)) {
         $this->errMsg["fc"] = 'PARAMERROR';
         $this->errMsg["fm"] = 'Invalid format';
         $this->errMsg["fd"] = 'Invalid fax1, it must be a numeric string and can only contain at the beginning * or #';
         $this->errMsg["cn"] = get_class($this);
         return false;
     }
     if (isset($fax2) && !preg_match('/^[\\*|#]*[[:digit:]]*$/', $fax2)) {
         $this->errMsg["fc"] = 'PARAMERROR';
         $this->errMsg["fm"] = 'Invalid format';
         $this->errMsg["fd"] = 'Invalid fax2, it must be a numeric string and can only contain at the beginning * or #';
         $this->errMsg["cn"] = get_class($this);
         return false;
     }
     $arrStatus = array("isPrivate", "isPublic");
     if (!in_array($status, $arrStatus)) {
         $status = "isPrivate";
     }
     $lastId = $addressBook->getLastContactInsertedId();
     $nextId = $lastId + 1;
     //TODO: Hay que tener en cuenta la posibilidad de una condición de carrera, es decir en caso de que existan dos peticiones hechas exactamente al mismo tiempo, con lo cual las dos obtendrían el mismo id y una de estas peticiones sobreescribirá la imagen enviada por la otra. (escenario muy complicado pero de todas formas posible)
     if (isset($picture) && $picture != "") {
         $picture = base64_decode($picture);
         $tmpname = "/tmp/image" . time();
         file_put_contents($tmpname, $picture);
         //localización temporal de la imagen
         $size = getimagesize($tmpname);
         if (!is_array($size)) {
             $this->errMsg["fc"] = 'PARAMERROR';
             $this->errMsg["fm"] = 'Invalid format';
             $this->errMsg["fd"] = 'Invalid picture, the format of the image is incorrect';
             $this->errMsg["cn"] = get_class($this);
             return false;
         }
         $destination_path = "/var/www/address_book_images";
         //Se procede a redimensionar la imagen para evitar inyección de código dentro de la imagen y luego se guarda
         $extension = $addressBook->saveResizeImage($tmpname, $size[0], $size[1], $size[0], $size[1], $size[2], $destination_path . "/{$nextId}");
         //Se procede a guardar la imagen en formato thumbnail
         $new_width = 48;
         $new_height = 48;
         $addressBook->saveResizeImage($tmpname, $size[0], $size[1], $new_width, $new_height, $size[2], $destination_path . "/{$nextId}_Thumbnail");
         $picture = $nextId . $extension;
         unlink($tmpname);
     }
     $arrStatus = array("isPrivate", "isPublic");
     if (!in_array($status, $arrStatus)) {
         $status = "isPrivate";
     }
     // Construir el arreglo de datos que hay que almacenar
     if (!isset($first_name)) {
         $first_name = NULL;
     }
     if (!isset($last_name)) {
         $last_name = NULL;
     }
     if (!isset($email)) {
         $email = NULL;
     }
     $data = array($first_name, $last_name, $phone, $cell_phone, $home_phone, $fax1, $fax2, $email, $id_user, $picture, $province, $city, $address, $company, $company_contact, $contact_rol, "external", $notes, $status, NULL, NULL);
     $result = $addressBook->addContact($data);
     if (!$result) {
         $this->errMsg["fc"] = 'DBERROR';
         $this->errMsg["fm"] = 'Database operation failed';
         $this->errMsg["fd"] = 'Unable to write data to external phonebook - ' . $addressBook->_DB->errMsg;
         $this->errMsg["cn"] = get_class($addressBook);
         return false;
     }
     if ($getIdInserted) {
         return $nextId;
     } else {
         return true;
     }
 }