getMime() static public method

Retrieve the mime type of a file
static public getMime ( $file, $type = false ) : string
$file string path of the file
$type string check if $file is the correct type (false by default)
return string (if $type not given) else boolean
Esempio n. 1
0
 /**
  * Convert tag to image
  *
  * @since version 0.85
  *
  * @param $content_text         text content of input
  * @param $force_update         force update of content in item (false by default
  * @param $doc_data       array of filenames and tags
  *
  * @return nothing
  **/
 function convertTagToImage($content_text, $force_update = false, $doc_data = array())
 {
     global $CFG_GLPI;
     $matches = array();
     // If no doc data available we match all tags in content
     if (!count($doc_data)) {
         $doc = new Document();
         preg_match_all('/' . Document::getImageTag('(([a-z0-9]+|[\\.\\-]?)+)') . '/', $content_text, $matches, PREG_PATTERN_ORDER);
         if (isset($matches[1]) && count($matches[1])) {
             $doc_data = $doc->find("`tag` IN('" . implode("','", array_unique($matches[1])) . "')");
         }
     }
     if (count($doc_data)) {
         foreach ($doc_data as $id => $image) {
             // Add only image files : try to detect mime type
             $ok = false;
             $mime = '';
             if (isset($image['filepath'])) {
                 $fullpath = GLPI_DOC_DIR . "/" . $image['filepath'];
                 $mime = Toolbox::getMime($fullpath);
                 $ok = Toolbox::getMime($fullpath, 'image');
             }
             if (isset($image['tag'])) {
                 if ($ok || empty($mime)) {
                     // Replace tags by image in textarea
                     $img = "<img alt='" . $image['tag'] . "' src='" . $CFG_GLPI['root_doc'] . "/front/document.send.php?docid=" . $id . "&tickets_id=" . $this->fields['id'] . "'/>";
                     // Replace tag by the image
                     $content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', Html::entities_deep($img), $content_text);
                     // Replace <br> TinyMce bug
                     $content_text = str_replace(array('&gt;rn&lt;', '&gt;\\r\\n&lt;', '&gt;\\r&lt;', '&gt;\\n&lt;'), '&gt;&lt;', $content_text);
                     // If the tag is from another ticket : link document to ticket
                     // TODO : comment maybe not used
                     // if($image['tickets_id'] != $this->fields['id']){
                     //    $docitem = new Document_Item();
                     //    $docitem->add(array('documents_id'  => $image['id'],
                     //                        '_do_notif'     => false,
                     //                        '_disablenotif' => true,
                     //                        'itemtype'      => $this->getType(),
                     //                        'items_id'      => $this->fields['id']));
                     // }
                 } else {
                     // Remove tag
                     $content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', '', $content_text);
                 }
             }
         }
     }
     if ($force_update) {
         $this->fields['content'] = $content_text;
         $this->updateInDB(array('content'));
     }
     return $content_text;
 }
Esempio n. 2
0
 /**
  * Move a document (files in GLPI_DOC_DIR."/_tmp" dir)
  *
  * @param $input     array of datas used in adding process (need current_filepath)
  * @param $filename        filename to move
  *
  * @return boolean for success / $input array is updated
  **/
 static function moveDocument(array &$input, $filename)
 {
     global $CFG_GLPI;
     $fullpath = GLPI_TMP_DIR . "/" . $filename;
     if (!is_dir(GLPI_TMP_DIR)) {
         Session::addMessageAfterRedirect(__("Temporary directory doesn't exist"), false, ERROR);
         return false;
     }
     if (!is_file($fullpath)) {
         Session::addMessageAfterRedirect(sprintf(__('File %s not found.'), $fullpath), false, ERROR);
         return false;
     }
     $sha1sum = sha1_file($fullpath);
     $dir = self::isValidDoc($filename);
     $new_path = self::getUploadFileValidLocationName($dir, $sha1sum);
     if (!$sha1sum || !$dir || !$new_path) {
         return false;
     }
     // Delete old file (if not used by another doc)
     if (isset($input['current_filepath']) && !empty($input['current_filepath']) && is_file(GLPI_DOC_DIR . "/" . $input['current_filepath']) && countElementsInTable('glpi_documents', "`sha1sum`='" . sha1_file(GLPI_DOC_DIR . "/" . $input['current_filepath']) . "'") <= 1) {
         if (unlink(GLPI_DOC_DIR . "/" . $input['current_filepath'])) {
             Session::addMessageAfterRedirect(sprintf(__('Succesful deletion of the file %s'), $input['current_filename']));
         } else {
             // TRANS: %1$s is the curent filename, %2$s is its directory
             Session::addMessageAfterRedirect(sprintf(__('Failed to delete the file %1$s (%2$s)'), $input['current_filename'], GLPI_DOC_DIR . "/" . $input['current_filepath']), false, ERROR);
         }
     }
     // Local file : try to detect mime type
     $input['mime'] = Toolbox::getMime($fullpath);
     if (is_writable(GLPI_TMP_DIR) && is_writable($fullpath)) {
         // Move if allowed
         if (self::renameForce($fullpath, GLPI_DOC_DIR . "/" . $new_path)) {
             Session::addMessageAfterRedirect(__('Document move succeeded.'));
         } else {
             Session::addMessageAfterRedirect(__('File move failed.'), false, ERROR);
             return false;
         }
     } else {
         // Copy (will overwrite dest file is present)
         if (copy($fullpath, GLPI_DOC_DIR . "/" . $new_path)) {
             Session::addMessageAfterRedirect(__('Document copy succeeded.'));
         } else {
             Session::addMessageAfterRedirect(__('File move failed'), false, ERROR);
             return false;
         }
     }
     // For display
     $input['filename'] = addslashes($filename);
     // Storage path
     $input['filepath'] = $new_path;
     // Checksum
     $input['sha1sum'] = $sha1sum;
     return true;
 }