Example #1
0
function initialize_page()
{
    $post_action = "";
    if (isset($_POST['submit'])) {
        $post_action = $_POST['submit'];
    }
    if ($post_action == "Add Document" || $post_action == "Add and Return to List") {
        $name = $_POST['name'];
        $file_type = getFileExtension($_FILES['file']['name']);
        $filename = slug(getFileName($_FILES['file']['name']));
        $filename_string = $filename . "." . $file_type;
        // Check to make sure there isn't already a file with that name
        $possibledoc = Documents::FindByFilename($filename_string);
        if (is_object($possibledoc)) {
            setFlash("<h3>Failure: Document filename already exists!</h3>");
            redirect("admin/add_document");
        }
        $target_path = SERVER_DOCUMENTS_ROOT . $filename_string;
        if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
            $new_doc = MyActiveRecord::Create('Documents', array('name' => $name, 'filename' => $filename_string, 'file_type' => $file_type));
            $new_doc->save();
            if (!chmod($target_path, 0644)) {
                setFlash("<h3>Warning: Document Permissions not set; this file may not display properly</h3>");
            }
            setFlash("<h3>Document uploaded</h3>");
        } else {
            setFlash("<h3>Failure: Document could not be uploaded</h3>");
        }
        if ($post_action == "Add and Return to List") {
            redirect("admin/list_documents");
        }
    }
}
Example #2
0
function document_display($content_to_display)
{
    $documentPattern = "/{{2}(document:[A-Za-z0-9\\-\\_ \\.\\(\\)'\"]+){{2}/";
    $documentIds = getFilterIds($content_to_display, $documentPattern);
    $documents = array();
    foreach ($documentIds as $documentId) {
        $filename = end(explode(":", $documentId));
        $documents[] = Documents::FindByFilename($filename);
    }
    foreach ($documents as $document) {
        if (is_object($document)) {
            $replacement = "<a class=\"hcd-document " . getFileExtension($document->filename) . "\" href=\"{$document->getPublicUrl()}\">" . cleanupSpecialChars($document->name) . "</a> (" . getFileExtension($document->filename) . ")";
            $content_to_display = updateContent($content_to_display, "/{{2}document:" . str_replace(")", "\\)", str_replace("(", "\\(", $document->filename)) . "{{2}/", $replacement);
        } else {
            $content_to_display = "<span class=\"database_error\">HCd&gt;CMS Warning: Document &ldquo;{$filename}&rdquo; not found!</span> " . $content_to_display;
        }
    }
    return $content_to_display;
}