getFromDBbyContent() public méthode

find a document with a file attached
public getFromDBbyContent ( $entity, $path ) : boolean
$entity of the document
$path of the searched file
Résultat boolean
 /**
  * add files (from $this->input['_filename']) to an ITIL object
  * create document if needed
  * create link from document to ITIL object
  *
  * @param $donotif         Boolean  if we want to raise notification (default 1)
  * @param $disablenotif             (default 0)
  *
  * @return array of doc added name
  **/
 function addFiles($donotif = 1, $disablenotif = 0)
 {
     global $CFG_GLPI;
     if (!isset($this->input['_filename']) || count($this->input['_filename']) == 0) {
         return array();
     }
     $docadded = array();
     foreach ($this->input['_filename'] as $key => $file) {
         $doc = new Document();
         $docitem = new Document_Item();
         $docID = 0;
         $filename = GLPI_TMP_DIR . "/" . $file;
         $input2 = array();
         // Crop/Resize image file if needed
         if (isset($this->input['_coordinates']) && !empty($this->input['_coordinates'][$key])) {
             $image_coordinates = json_decode(urldecode($this->input['_coordinates'][$key]), true);
             Toolbox::resizePicture($filename, $filename, $image_coordinates['img_w'], $image_coordinates['img_h'], $image_coordinates['img_y'], $image_coordinates['img_x'], $image_coordinates['img_w'], $image_coordinates['img_h'], 0);
         } else {
             Toolbox::resizePicture($filename, $filename, 0, 0, 0, 0, 0, 0, 0);
         }
         //If file tag is present
         if (isset($this->input['_tag_filename']) && !empty($this->input['_tag_filename'][$key])) {
             $this->input['_tag'][$key] = $this->input['_tag_filename'][$key];
         }
         // Check for duplicate
         if ($doc->getFromDBbyContent($this->fields["entities_id"], $filename)) {
             if (!$doc->fields['is_blacklisted']) {
                 $docID = $doc->fields["id"];
             }
             // File already exist, we replace the tag by the existing one
             if (isset($this->input['_tag'][$key]) && $docID > 0 && isset($this->input['content'])) {
                 $this->input['content'] = preg_replace('/' . Document::getImageTag($this->input['_tag'][$key]) . '/', Document::getImageTag($doc->fields["tag"]), $this->input['content']);
                 $docadded[$docID]['tag'] = $doc->fields["tag"];
             }
         } else {
             //TRANS: Default document to files attached to tickets : %d is the ticket id
             $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID()));
             if ($this->getType() == 'Ticket') {
                 $input2["tickets_id"] = $this->getID();
                 // Insert image tag
                 if (isset($this->input['_tag'][$key])) {
                     $input2["tag"] = $this->input['_tag'][$key];
                 }
             }
             $input2["entities_id"] = $this->fields["entities_id"];
             $input2["documentcategories_id"] = $CFG_GLPI["documentcategories_id_forticket"];
             $input2["_only_if_upload_succeed"] = 1;
             $input2["entities_id"] = $this->fields["entities_id"];
             $input2["_filename"] = array($file);
             $docID = $doc->add($input2);
         }
         if ($docID > 0) {
             if ($docitem->add(array('documents_id' => $docID, '_do_notif' => $donotif, '_disablenotif' => $disablenotif, 'itemtype' => $this->getType(), 'items_id' => $this->getID()))) {
                 $docadded[$docID]['data'] = sprintf(__('%1$s - %2$s'), stripslashes($doc->fields["name"]), stripslashes($doc->fields["filename"]));
                 if (isset($input2["tag"])) {
                     $docadded[$docID]['tag'] = $input2["tag"];
                     unset($this->input['_filename'][$key]);
                     unset($this->input['_tag'][$key]);
                 }
                 if (isset($this->input['_coordinates'][$key])) {
                     unset($this->input['_coordinates'][$key]);
                 }
             }
         }
         // Only notification for the first New doc
         $donotif = 0;
     }
     // Ticket update
     if (isset($this->input['content'])) {
         if ($CFG_GLPI["use_rich_text"]) {
             $this->input['content'] = $this->convertTagToImage($this->input['content'], true, $docadded);
             $this->input['_forcenotif'] = true;
         } else {
             $this->fields['content'] = $this->setSimpleTextContent($this->input['content']);
             $this->updateInDB(array('content'));
         }
     }
     return $docadded;
 }
 /**
  * add files (from $_FILES) to a ticket
  * create document if needed
  * create link from document to ticket
  *
  * @param $id of the ticket
  *
  * @return array of doc added name
  **/
 function addFiles($id)
 {
     global $LANG, $CFG_GLPI;
     if (!isset($_FILES)) {
         return array();
     }
     $docadded = array();
     $doc = new Document();
     $docitem = new Document_Item();
     // add Document if exists
     if (isset($_FILES['multiple'])) {
         unset($_FILES['multiple']);
         $TMPFILE = $_FILES;
     } else {
         $TMPFILE = array($_FILES);
     }
     foreach ($TMPFILE as $_FILES) {
         if (isset($_FILES['filename']) && count($_FILES['filename']) > 0 && $_FILES['filename']["size"] > 0) {
             // Check for duplicate
             if ($doc->getFromDBbyContent($this->fields["entities_id"], $_FILES['filename']['tmp_name'])) {
                 $docID = $doc->fields["id"];
             } else {
                 $input2 = array();
                 $input2["name"] = addslashes($LANG['tracking'][24] . " {$id}");
                 $input2["tickets_id"] = $id;
                 $input2["entities_id"] = $this->fields["entities_id"];
                 $input2["documentcategories_id"] = $CFG_GLPI["documentcategories_id_forticket"];
                 $input2["_only_if_upload_succeed"] = 1;
                 $input2["entities_id"] = $this->fields["entities_id"];
                 $docID = $doc->add($input2);
             }
             if ($docID > 0) {
                 if ($docitem->add(array('documents_id' => $docID, 'itemtype' => $this->getType(), 'items_id' => $id))) {
                     $docadded[] = stripslashes($doc->fields["name"] . " - " . $doc->fields["filename"]);
                 }
             }
         } else {
             if (!empty($_FILES['filename']['name']) && isset($_FILES['filename']['error']) && $_FILES['filename']['error']) {
                 addMessageAfterRedirect($LANG['document'][46], false, ERROR);
             }
         }
     }
     unset($_FILES);
     return $docadded;
 }
 /**
  * add files (from $_FILES) to an ITIL object
  * create document if needed
  * create link from document to ITIL object
  *
  * @param $id        Integer  ID of the ITIL object
  * @param $donotif   Boolean  if we want to raise notification (default 1)
  *
  * @return array of doc added name
  **/
 function addFiles($id, $donotif = 1)
 {
     global $CFG_GLPI;
     if (!isset($_FILES) || !isset($_FILES['filename'])) {
         return array();
     }
     $docadded = array();
     $doc = new Document();
     $docitem = new Document_Item();
     // if multiple files are uploaded
     $TMPFILE = array();
     if (is_array($_FILES['filename']['name'])) {
         foreach ($_FILES['filename']['name'] as $key => $filename) {
             if (!empty($filename)) {
                 $TMPFILE[$key]['filename']['name'] = $filename;
                 $TMPFILE[$key]['filename']['type'] = $_FILES['filename']['type'][$key];
                 $TMPFILE[$key]['filename']['tmp_name'] = $_FILES['filename']['tmp_name'][$key];
                 $TMPFILE[$key]['filename']['error'] = $_FILES['filename']['error'][$key];
                 $TMPFILE[$key]['filename']['size'] = $_FILES['filename']['size'][$key];
             }
         }
     } else {
         $TMPFILE = array($_FILES);
     }
     foreach ($TMPFILE as $_FILES) {
         if (isset($_FILES['filename']) && count($_FILES['filename']) > 0 && $_FILES['filename']["size"] > 0) {
             // Check for duplicate
             if ($doc->getFromDBbyContent($this->fields["entities_id"], $_FILES['filename']['tmp_name'])) {
                 $docID = $doc->fields["id"];
             } else {
                 $input2 = array();
                 //TRANS: Default document to files attached to tickets : %d is the ticket id
                 $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $id));
                 if ($this->getType() == 'Ticket') {
                     $input2["tickets_id"] = $id;
                 }
                 $input2["entities_id"] = $this->fields["entities_id"];
                 $input2["documentcategories_id"] = $CFG_GLPI["documentcategories_id_forticket"];
                 $input2["_only_if_upload_succeed"] = 1;
                 $input2["entities_id"] = $this->fields["entities_id"];
                 $docID = $doc->add($input2);
             }
             if ($docID > 0) {
                 if ($docitem->add(array('documents_id' => $docID, '_do_notif' => $donotif, 'itemtype' => $this->getType(), 'items_id' => $id))) {
                     $docadded[] = sprintf(__('%1$s - %2$s'), stripslashes($doc->fields["name"]), stripslashes($doc->fields["filename"]));
                 }
             }
         } else {
             if (!empty($_FILES['filename']['name']) && isset($_FILES['filename']['error']) && $_FILES['filename']['error']) {
                 Session::addMessageAfterRedirect(__('Failed to send the file (probably too large)'), false, ERROR);
             }
         }
         // Only notification for the first New doc
         $donotif = 0;
     }
     unset($_FILES);
     return $docadded;
 }
 /**
  * Add a document to a existing ticket
  * for an authenticated user
  *
  * @param $params array of options (ticket, uri, name, base64, comment)
  *        only one of uri and base64 must be set
  *        name is mandatory when base64 set, for extension check (filename)
  * @param $protocol     the communication protocol used
  *
  * @return array of hashtable
  **/
 static function methodAddTicketDocument($params, $protocol)
 {
     global $DB, $CFG_GLPI;
     if (isset($params['help'])) {
         return array('ticket' => 'integer,mandatory', 'uri' => 'string,optional', 'base64' => 'string,optional', 'content' => 'string,optional', 'close' => 'bool,optional', 'reopen' => 'bool,optional', 'source' => 'string,optional', 'private' => 'bool,optional', 'help' => 'bool,optional');
     }
     if (!Session::getLoginUserID()) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     $ticket = new Ticket();
     if (!isset($params['ticket'])) {
         return self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'ticket');
     }
     if (!is_numeric($params['ticket'])) {
         return self::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '', 'ticket');
     }
     if (!$ticket->can($params['ticket'], 'r')) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTFOUND);
     }
     if (in_array($ticket->fields["status"], $ticket->getClosedStatusArray())) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', 'closed ticket');
     }
     if (!$ticket->canAddFollowups()) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', 'access denied');
     }
     if (isset($params['name']) && !empty($params['name'])) {
         $document_name = addslashes($params['name']);
     } else {
         $document_name = addslashes(sprintf(__('%1$s %2$s'), _x('phone', 'Number'), $ticket->fields['id']));
     }
     $filename = tempnam(GLPI_DOC_DIR . '/_tmp', 'PWS');
     $response = parent::uploadDocument($params, $protocol, $filename, $document_name);
     //An error occured during document upload
     if (parent::isError($protocol, $response)) {
         return $response;
     }
     $doc = new Document();
     $documentitem = new Document_Item();
     $docid = $doc->getFromDBbyContent($ticket->fields["entities_id"], $filename);
     if ($docid) {
         $input = array('itemtype' => $ticket->getType(), 'items_id' => $ticket->getID(), 'documents_id' => $doc->getID());
         if ($DB->request('glpi_documents_items', $input)->numrows()) {
             return self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', 'document already associated to this ticket');
         }
         $new = $documentitem->add($input);
     } else {
         $input = array('itemtype' => $ticket->getType(), 'items_id' => $ticket->getID(), 'tickets_id' => $ticket->getID(), 'entities_id' => $ticket->getEntityID(), 'is_recursive' => $ticket->isRecursive(), 'documentcategories_id' => $CFG_GLPI["documentcategories_id_forticket"]);
         $new = $doc->add($input);
     }
     // to not add it twice during followup
     unset($_FILES['filename']);
     if (!$new) {
         return self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', self::getDisplayError());
     }
     if (isset($params['comment']) && !empty($params['comment'])) {
         $params['content'] = $params['comment'];
         unset($params['comment']);
     }
     if (isset($params['content']) && !empty($params['content'])) {
         return self::methodAddTicketFollowup($params, $protocol);
     }
     return self::methodGetTicket(array('ticket' => $params['ticket']), $protocol);
 }
--------------------------------------------------------------------------
*/
// ----------------------------------------------------------------------
// Original Author of file: Julien Dombre
// Purpose of file:
// ----------------------------------------------------------------------
define('GLPI_ROOT', '..');
include GLPI_ROOT . "/inc/includes.php";
if (!isset($_GET["id"])) {
    $_GET["id"] = -1;
}
$doc = new Document();
$documentitem = new Document_Item();
if (isset($_POST["add"])) {
    $doc->check(-1, 'w', $_POST);
    if (isset($_POST['itemtype']) && isset($_POST['items_id']) && isset($_FILES['filename']['tmp_name']) && $doc->getFromDBbyContent($_POST["entities_id"], $_FILES['filename']['tmp_name'])) {
        $documentitem->add(array('documents_id' => $doc->fields['id'], 'itemtype' => $_POST['itemtype'], 'items_id' => $_POST['items_id']));
    } else {
        $newID = $doc->add($_POST);
        $name = "";
        if (isset($_POST["name"])) {
            $name = $_POST["name"];
        } else {
            if (isset($_FILES['filename']) && isset($_FILES['filename']['name'])) {
                $name = $_FILES['filename']['name'];
            }
        }
        Event::log($newID, "documents", 4, "document", $_SESSION["glpiname"] . " " . $LANG['log'][20] . " " . $name . ".");
    }
    glpi_header($_SERVER['HTTP_REFERER']);
} else {
 function addFilesTickets($id, $question_name, $entities_id)
 {
     global $LANG, $CFG_GLPI;
     $docadded = array();
     $doc = new Document();
     $docitem = new Document_Item();
     // if multiple files are uploaded
     $TMPFILE = array();
     if (is_array($_FILES[$question_name]['name'])) {
         $_FILES['filename'] = $_FILES[$question_name];
         foreach ($_FILES[$question_name]['name'] as $key => $filename) {
             if (!empty($filename)) {
                 $TMPFILE[$key]['filename']['name'] = $filename;
                 $TMPFILE[$key]['filename']['type'] = $_FILES['filename']['type'][$key];
                 $TMPFILE[$key]['filename']['tmp_name'] = $_FILES['filename']['tmp_name'][$key];
                 $TMPFILE[$key]['filename']['error'] = $_FILES['filename']['error'][$key];
                 $TMPFILE[$key]['filename']['size'] = $_FILES['filename']['size'][$key];
             }
         }
     } else {
         $TMPFILE = array($_FILES);
     }
     foreach ($TMPFILE as $_FILES) {
         if (isset($_FILES[$question_name]) && count($_FILES[$question_name]) > 0 && $_FILES[$question_name]["size"] > 0) {
             $_FILES['filename'] = $_FILES[$question_name];
             // Check for duplicate
             if ($doc->getFromDBbyContent($entities_id, $_FILES['filename']['tmp_name'])) {
                 $docID = $doc->fields["id"];
             } else {
                 $input2 = array();
                 $input2["name"] = addslashes(__('Document Ticket') . " {$id}");
                 $input2["tickets_id"] = $id;
                 $input2["entities_id"] = $entities_id;
                 $input2["documentcategories_id"] = $CFG_GLPI["documentcategories_id_forticket"];
                 $input2["_only_if_upload_succeed"] = 1;
                 $input2["entities_id"] = $entities_id;
                 $docID = $doc->add($input2);
                 if ($docID > 0) {
                     if ($docitem->add(array('documents_id' => $docID, 'itemtype' => 'Ticket', 'items_id' => $id))) {
                         $docadded[] = stripslashes($doc->fields["name"] . " - " . $doc->fields["filename"]);
                     }
                 }
             }
         }
     }
     unset($_FILES['filename']);
     return true;
 }