From: toyomoyo Date: Mon, 20 Nov 2006 08:31:48 +0000 (+0000) Subject: adding attachment for blogs X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=7d0e5a95c3bce8115534b11f1690d8c45c50aa3e;p=moodle.git adding attachment for blogs --- diff --git a/blog/edit.html b/blog/edit.html index 9890eab424..a91e26a8df 100755 --- a/blog/edit.html +++ b/blog/edit.html @@ -20,7 +20,7 @@ } ?> -
+ @@ -146,6 +146,17 @@
+ + :
(
+ + maxbytes); ?> + + " /> + + + : diff --git a/blog/edit.php b/blog/edit.php index 66a4d4285e..5c1639f716 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -190,10 +190,14 @@ function do_add(&$post, &$errors) { $post->userid = $USER->id; $post->lastmodified = time(); $post->created = time(); - + // Insert the new blog entry. if ($id = insert_record('post', $post)) { $post->id = $id; + // add blog attachment + if ($post->attachment = blog_add_attachment($post, 'attachment',$message)) { + set_field("post", "attachment", $post->attachment, "id", $post->id); + } add_tags_info($post->id); add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$posz->id, $post->subject); @@ -234,13 +238,20 @@ function do_edit(&$post, &$errors) { } $post->lastmodified = time(); - + + if ($newfilename = blog_add_attachment($post, 'attachment',$message)) { + $post->attachment = $newfilename; + } else { + unset($post->attachment); + } // update record if (update_record('post', $post)) { // delete all tags associated with this entry delete_records('blog_tag_instance', 'entryid', $post->id); // add them back add_tags_info($post->id); + + add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject); } else { diff --git a/blog/lib.php b/blog/lib.php index 9162d2d6cb..55fbe95392 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -198,6 +198,14 @@ /// Actual content echo ''."\n"; + + if ($blogEntry->attachment) { + echo '
'; + $attachedimages = blog_print_attachments($blogEntry); + echo '
'; + } else { + $attachedimages = ''; + } switch ($template['publishstate']) { case 'draft': @@ -219,7 +227,9 @@ // Print whole message echo format_text($template['body']); - + + /// Print attachments + echo $attachedimages; /// Links to tags if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti @@ -251,7 +261,91 @@ echo ''."\n\n"; } + + function blog_file_area_name($blogentry) { + // Creates a directory file name, suitable for make_upload_directory() + global $CFG; + // $CFG->dataroot/blog/attachments/xxxx/file.jpg + return "blog/attachments/$blogentry->id"; + } + + function blog_file_area($blogentry) { + return make_upload_directory( blog_file_area_name($blogentry) ); + } + + + function blog_print_attachments($blogentry, $return=NULL) { + // if return=html, then return a html string. + // if return=text, then return a text-only string. + // otherwise, print HTML for non-images, and return image HTML + + global $CFG; + + $filearea = blog_file_area_name($blogentry); + + $imagereturn = ""; + $output = ""; + + if ($basedir = blog_file_area($blogentry)) { + if ($files = get_directory_list($basedir)) { + $strattachment = get_string("attachment", "forum"); + foreach ($files as $file) { + include_once($CFG->libdir.'/filelib.php'); + $icon = mimeinfo("icon", $file); + if ($CFG->slasharguments) { + $ffurl = "$CFG->wwwroot/file.php/$filearea/$file"; + } else { + $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file"; + } + $image = "pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"\" />"; + + if ($return == "html") { + $output .= "$image "; + $output .= "$file
"; + + } else if ($return == "text") { + $output .= "$strattachment $file:\n$ffurl\n"; + + } else { + if ($icon == "image.gif") { // Image attachments don't get printed as links + $imagereturn .= "
\"\""; + } else { + echo "$image "; + echo filter_text("$file
"); + } + } + } + } + } + + if ($return) { + return $output; + } + + return $imagereturn; + } + + /** + * If successful, this function returns the name of the file + * @param $post is a full post record, including course and forum + * @param $newfile is a full upload array from $_FILES + * @param $message is a string to hold the messages. + */ + function blog_add_attachment($blogentry, $inputname, &$message) { + + global $CFG; + + require_once($CFG->dirroot.'/lib/uploadlib.php'); + $um = new upload_manager($inputname,true,false,null,false,$CFG->maxbytes,true,true); + $dir = blog_file_area_name($blogentry); + if ($um->process_file_uploads($dir)) { + $message .= $um->get_errors(); + return $um->get_new_filename(); + } + $message .= $um->get_errors(); + echo $message; + } /** * Use this function to retrieve a list of publish states available for diff --git a/file.php b/file.php index e6bb63af2b..6c78f789f9 100644 --- a/file.php +++ b/file.php @@ -35,14 +35,16 @@ if (count($args) == 0) { // always at least courseid, may search for index.html in course root error('No valid arguments supplied'); } - + // security: limit access to existing course subdirectories - if (!$course = get_record_sql("SELECT * FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'")) { + // hack for blogs, needs proper security check too + if ((!$course = get_record_sql("SELECT * FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'")) && $args[0]!='blog') { error('Invalid course ID'); } // security: prevent access to "000" or "1 something" directories - if ($args[0] != $course->id) { + // hack for blogs, needs proper security check too + if ($args[0] != $course->id && $args[0]!='blog') { error('Invalid course ID'); } diff --git a/lib/db/install.xml b/lib/db/install.xml index 75ac4af4f4..0ba18ccce3 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -782,8 +782,9 @@ - - + + + @@ -1031,4 +1032,4 @@ - + \ No newline at end of file diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 5646699ec4..a4823f05c7 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -82,6 +82,16 @@ function xmldb_main_upgrade($oldversion=0) { $result = $result && add_field($table, $field5); } + if ($result && $oldversion < 2006112000) { + + /// Define field attachment to be added to post + $table = new XMLDBTable('post'); + $field = new XMLDBField('attachment'); + $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'format'); + + /// Launch add field attachment + $result = $result && add_field($table, $field); + } return $result; diff --git a/version.php b/version.php index bf5283d619..5bc6592021 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2006102600; // YYYYMMDD = date + $version = 2006112000; // YYYYMMDD = date // XY = increments within a single day $release = '1.8 dev'; // Human-friendly version name