Exemplo n.º 1
0
/**
 * Paste_from_file
 * Parses a named uploaded file of html or txt type
 * The function identifies title, head and body for html files,
 * or body for text files.
 * 
 * @return FileData object
 */
function paste_from_file()
{
    $fileData = new FileData();
    if ($_FILES['uploadedfile_paste']['name'] == '') {
        $fileData->setErrorMsg(_AT('TR_ERROR_FILE_NOT_SELECTED'));
    } elseif ($_FILES['uploadedfile_paste']['type'] == 'text/plain' || $_FILES['uploadedfile_paste']['type'] == 'text/html') {
        $path_parts = pathinfo($_FILES['uploadedfile_paste']['name']);
        $ext = strtolower($path_parts['extension']);
        if (in_array($ext, array('html', 'htm'))) {
            $contents = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
            /* get the <title></title> of this page             */
            $start_pos = strpos(strtolower($contents), '<title>');
            $end_pos = strpos(strtolower($contents), '</title>');
            if ($start_pos !== false && $end_pos !== false) {
                $start_pos += strlen('<title>');
                $fileData->setTitle(trim(substr($contents, $start_pos, $end_pos - $start_pos)));
            }
            unset($start_pos);
            unset($end_pos);
            $fileData->setHead(trim(get_html_head_by_tag($contents, array("link", "style", "script"))));
            $fileData->setBody(trim(get_html_body($contents)));
        } else {
            if ($ext == 'txt') {
                $fileData->setBody(trim(file_get_contents($_FILES['uploadedfile_paste']['tmp_name'])));
            }
        }
    } else {
        $fileData->setErrorMsg(_AT('TR_ERROR_BAD_FILE_TYPE'));
    }
    return $fileData;
}