Re: Attachment Mod 2.0.1 by Frank H

no problems, always nice to be able to help smile

52 (edited by XuMiX 2005-04-17 04:27)

Re: Attachment Mod 2.0.1 by Frank H

My own solution for the problem of huge amount of queries( it was wrtitten at 5 AM, so dont be very cruel wink )
This will MOST LIKELY not work for you if you're going to use patch util
make all changes by hand

--- dev/viewtopic.php    (revision 177)
+++ dev/viewtopic.php    (revision 178)
@@ -211,8 +211,25 @@
 $post_count = 0;    // Keep track of post numbers
 
 // Retrieve the posts (and their respective poster/online status)
    
+$att_res = $db->query('select af.* from '.$db->prefix.'attach_2_files as af left join '.$db->prefix.'posts as p on af.post_id=p.id where p.topic_id='.$id);
+$att_arr = array();
+$attach_allow_download = false;
+
+while ($att_arr[] = $db->fetch_assoc($att_res)) {}
 
+if($pun_user['g_id']==PUN_ADMIN) $attach_allow_download = true;
+        else{         //fetch the rules of the forum, and check so that the user is allowed to download.
+            $result_attach_two = $db->query('SELECT ar.rules FROM '.$db->prefix.'attach_2_rules AS ar WHERE ar.group_id=\''.$pun_user['group_id'].'\' AND ar.forum_id='.$cur_topic['forum_id'].' LIMIT 1')or error('Unable to fetch rules for the attachments', __FILE__, __LINE__, $db->error());
+            if($db->num_rows($result_attach_two)==1){
+                list($attach_rules)=$db->fetch_row($result_attach_two);
+                $attach_allow_download = attach_rules($attach_rules,ATTACH_DOWNLOAD);
+            }
+        }
+
+        
+        
 while ($cur_post = $db->fetch_assoc($result))
 {
     $post_count++;
@@ -353,30 +370,23 @@
         }
     }
     // Attachment Mod Block Start
-    $attach_allow_download = false;
     $attach_output = '';
     $attach_num = 0;
+    
     // Check if this post has any attachments
-    $result_attach = $db->query('SELECT af.id, af.filename, af.size, af.downloads FROM '.$db->prefix.'attach_2_files AS af WHERE af.post_id='.$cur_post['id']) or error('Unable to fetch if there were any attachments to the post', __FILE__, __LINE__, $db->error());
-    $attach_num = $db->num_rows($result_attach);
-    if($attach_num > 0){
-        if($pun_user['g_id']==PUN_ADMIN)$attach_allow_download=true;
-        else{         //fetch the rules of the forum, and check so that the user is allowed to download.
-            $result_attach_two = $db->query('SELECT ar.rules FROM '.$db->prefix.'attach_2_rules AS ar WHERE ar.group_id=\''.$pun_user['group_id'].'\' AND ar.forum_id='.$cur_topic['forum_id'].' LIMIT 1')or error('Unable to fetch rules for the attachments', __FILE__, __LINE__, $db->error());
-            if($db->num_rows($result_attach_two)==1){
-                list($attach_rules)=$db->fetch_row($result_attach_two);
-                $attach_allow_download = attach_rules($attach_rules,ATTACH_DOWNLOAD);
-            }
-        }
+//    $result_attach = $db->query('SELECT af.id, af.filename, af.size, af.downloads FROM '.$db->prefix.'attach_2_files AS af WHERE af.post_id='.$cur_post['id']) or error('Unable to fetch if there were any attachments to the post', __FILE__, __LINE__, $db->error());
+//    $attach_num = $db->num_rows($result_attach);
+for ($i=0; $i < sizeof($att_arr)-1;$i++){
+    
+    if($att_arr[$i]['post_id'] == $cur_post['id']){
+        $attach_num++;
         if($attach_allow_download){//check if the user is allowed to download it.
             $attach_output .= $lang_attach['Attachments:'].' ';
-            while(list($attachment_id, $attachment_filename, $attachment_size, $attachment_downloads)=$db->fetch_row($result_attach)){
-                $attachment_extension=attach_get_extension($attachment_filename);
-                $attach_output .= '<br />'."\n\t\t\t\t\t\t".attach_icon($attachment_extension).' <a href="./attachment.php?item='.$attachment_id.'">'.$attachment_filename.'</a>, '.$lang_attach['Size:'].' '.number_format($attachment_size).' '.$lang_attach['bytes'].', '.$lang_attach['Downloads:'].' '.number_format($attachment_downloads);
-            }
+                $attachment_extension = attach_get_extension($att_arr[$i]['filename']);
+                $attach_output .= '<br />'."\n\t\t\t\t\t\t".attach_icon($att_arr[$i]['extension']).' <a href="./attachment.php?item='.$att_arr[$i]['id'].'">'.$att_arr[$i]['filename'].'</a>, '.$lang_attach['Size:'].' '.number_format($att_arr[$i]['size']).' '.$lang_attach['bytes'].', '.$lang_attach['Downloads:'].' '.number_format($att_arr[$i]['downloads']);
         }
     }
-    // Attachment Mod Block End
+}    // Attachment Mod Block End
 
 ?>
 <div id="p<?php echo $cur_post['id'] ?>" class="blockpost<?php echo $vtbg ?><?php if (($post_count + $start_from) == 1) echo ' firstpost'; ?>">
@@ -396,7 +406,7 @@
                 <div class="postmsg">
                     <?php echo $cur_post['message']."\n" ?>
 <?php if ($cur_post['edited'] != '') echo "\t\t\t\t\t".'<p class="postedit"><em>'.$lang_topic['Last edit'].' '.pun_htmlspecialchars($cur_post['edited_by']).' ('.format_time($cur_post['edited']).')</em></p>'."\n"; ?>
-<?php if ($attach_allow_download) echo "\t\t\t\t\t".'<div class="postsignature"><hr />'.$attach_output.'</div>'."\n"; ## Attachment Mod row ?>
+<?php if ($attach_allow_download && $attach_output != '') echo "\t\t\t\t\t".'<div class="postsignature"><hr />'.$attach_output.'</div>'."\n"; ## Attachment Mod row ?>
 <?php if ($signature != '') echo "\t\t\t\t".'<div class="postsignature"><hr />'.$signature.'</div>'."\n"; ?>
                 </div>
             </div>

53

Re: Attachment Mod 2.0.1 by Frank H

Has anyone been able to make the attachment mod work with sqlite? I get the following error message when I run the install_mod.php:

An error was encountered
File: /share/hdd/conf/opt/share/apache2/htdocs/forum/install_mod.php
Line: 40

PunBB reported: Unable to add table "attach_2_files" to database

Database reported: SQL logic error or missing database (Errno: 1)

Thanks in adavance for any help.

54

Re: Attachment Mod 2.0.1 by Frank H

Request for:
* Time of uploading of attachment (i.e. some later message was edited, attachment was deleted and re-uploaded.)

Re: Attachment Mod 2.0.1 by Frank H

Buzz wrote:

Has anyone been able to make the attachment mod work with sqlite? I get the following error message when I run the install_mod.php:

An error was encountered
File: /share/hdd/conf/opt/share/apache2/htdocs/forum/install_mod.php
Line: 40

PunBB reported: Unable to add table "attach_2_files" to database

Database reported: SQL logic error or missing database (Errno: 1)

Thanks in adavance for any help.

Hmm, no, totally untested.

Anyone with good knowledge how sqlite works, that want to give it a try?

Re: Attachment Mod 2.0.1 by Frank H

Dexus wrote:

Request for:
* Time of uploading of attachment (i.e. some later message was edited, attachment was deleted and re-uploaded.)

Nice one, never had thought of that, defenetly a good thing smile

57

Re: Attachment Mod 2.0.1 by Frank H

Sorry for my late answer, the problem was :  in the php.ini the maximun size of a file was 2 K, fixed this all goes great.

58

Re: Attachment Mod 2.0.1 by Frank H

Frank: post url to the old thread so people looking at it will see this has moved to a new thread.

Re: Attachment Mod 2.0.1 by Frank H

iku why would that help? it will just bump the old thread

60

Re: Attachment Mod 2.0.1 by Frank H

Connorhd: what do you mean?

It's not my loss but If someone for example uses google to find attachment mod for punbb it will show the old one too and that thread has nothing useful about 2.0. As last post there is only Frank's post saying he will publish new version after some testing.

Anyway I'm going to try out the new version now. Hopefully it will go nicely with my shared punbb-code for three vhosts smile

61

Re: Attachment Mod 2.0.1 by Frank H

Frank H
For needs of my users I was made a translation for the file attach.php to spanish, if somebody think this it can be useful can ask for it to gerardogomezvargas@gmail.com or if somebody can tell me where to upload it, thanks a lot

Great Work  Frank


Jerryg

62 (edited by Frank H 2005-04-20 14:31)

Re: Attachment Mod 2.0.1 by Frank H

jerryg wrote:

Frank H
For needs of my users I was made a translation for the file attach.php to spanish, if somebody think this it can be useful can ask for it to gerardogomezvargas@gmail.com or if somebody can tell me where to upload it, thanks a lot

Great Work  Frank


Jerryg

Nice to hear you got it working smile
If you want to, you can mail it to me (frank [dot} hagstrom [at} gmail [dot} com) and I'll put it up for download on my server.


@iku: ok, I'll edit the first message to say so wink
Edit: I coulnd't edit it as the thread is locked(by my request, as it's outdated) ... but the link goes to my page, where it clearly is visible there's a newer version, so I dont' htink it should be a problem...

63

Re: Attachment Mod 2.0.1 by Frank H

Phew. I got about everything to work. Same shared code for three vhosts now.

Frank: what about showing attached images directly in the post and not as url? I mean all of them by default.

Re: Attachment Mod 2.0.1 by Frank H

if you want to show an image in the post, just copy paste the download url into an [ img ] tag   (it's the same url as for viewing it, but with the added &download=1)

This mod is made for all type of files, not just images

65

Re: Attachment Mod 2.0.1 by Frank H

Frank: Yep I know that but I don't want it to work that way.

1) it will generate unnecessary http queries
2) it's slow when posting / editing a post
3) in my case I need this mod 99% for images only. some text files too but mainly images.
4) it needs to be as simple as possible because I'm not the only one posting images. Most people had problems posting images with the old version so that method would be more complex for them.
5) I would need to edit all of the older posts to reflect that change (img-bbcode)

I can edit the code like I did before, but such functionality might be good for the mod itself. I mean I don't want to re-invent the wheel in case you would be doing it because I'm as busy with my studies as you are  smile

btw, so far everything has been working well. Good mod, IMHO it was also easier to install than the previous version.

66 (edited by Dexus 2005-04-21 14:25)

Re: Attachment Mod 2.0.1 by Frank H

it's very annoying thing, to edit rules of many groups and many forums.... sad
is there any way to set "default" rules? or to multiset them???

67

Re: Attachment Mod 2.0.1 by Frank H

I thought it was pain in the ass too. Luckily I have to edit rules only once smile

68

Re: Attachment Mod 2.0.1 by Frank H

well, if you added forum or group - you have to edit all these rules again.

Re: Attachment Mod 2.0.1 by Frank H

When I am logged in as an administrator no attachments get uploaded.  But if I log in as a standard user then I can upload.  Basically in both modes it actually attempts to upload the file (can tell by the length of time it takes) but in admin mode the file does not seem to be added to the database or to the base of the admins posts.

70

Re: Attachment Mod 2.0.1 by Frank H

Great work! Can't wait to try it! big_smile

71 (edited by Dexus 2005-04-22 11:55)

Re: Attachment Mod 2.0.1 by Frank H

I was trying to implement "resume" of broken downloads of attachments, but faced some problems... and decided that it's nearly impossible due to "Range" parameter parsed by http server (apache).
After "Range: bytes=319488-" sent to server by client, server generates this responce before any scripting activity starts:

HTTP/1.1 416 Requested Range Not Satisfiable
Date: Fri, 22 Apr 2005 06:31:11 GMT
Server: Apache/2.0.53 (Win32) PHP/4.3.11
Connection: close
Content-Type: text/html; charset=iso-8859-1

addition:

    $fname=$pun_config['attach_basefolder'].$attach_location;
    $fp = fopen($fname, "rb");
    if(!$fp){
        message($lang_common['Bad request']);
    }else{
        ignore_user_abort(true);
        header('Content-Disposition: attachment; filename="'.$attach_filename.'"');
//        header("Connection: close");
//Gather relevent info about file
        if(strlen($attach_mime)!=0)
            $ctype=$attach_mime;
        else
            $ctype='application/octet-stream'; // a default mime is nothing is defined for the file
//Begin writing headers
//Use the switch-generated Content-Type
        header("Pragma: ");
        header("Cache-control: ");
        header("Content-Type: $ctype");

        header('Last-Modified: '.gmdate('D, d M Y H:i:s',$attach_uploadtime).' GMT');

        $ff=fopen("c:/!!!!".mktime(),'w'); //a sort of log for every session.
        $size=filesize($fname);
        fwrite($ff,"filename='".$fname."' size=".$size."\n\n");
//check if http_range is sent by browser (or download manager)

        if(isset($_ENV['HTTP_RANGE'])) {
            list($a, $rang)=explode("=",$_ENV['HTTP_RANGE']);
//if yes, download missing part
            list($a, $b)=explode("-", $rang);
            fwrite($ff,"rang='".$rang."' a='".$a."' b='".$b."'\n");
            if ($a=='') $a=0;
            if ($b=='') $b=$size-1;
            if ($a>0) fseek($fp,$a); 
            $new_length=$b-$a+1;
//            header("HTTP/1.1 206 Partial content");
            header("Content-Length: $new_length");
            header("Content-Range: bytes $a-$b/$size");
            fwrite($ff,"header: ".$a."-".$b."/".$size."\n");
            $size=new_length;
            fwrite($ff,"new length ".$size."\n");
//if not, download whole file
        } else {
            header("Accept-Ranges: bytes");
            header("Content-Length: ".$size);
            header("Connection: close");
        }

        flush();
        ob_flush();
//start buffered download
        set_time_limit(0);
        $chunksize=4024*1;
        while($size>=0) {
//reset time limit for big files
            if  (connection_status()!=0) break;
            $buf=fread($fp,$chunksize);
            print($buf);
            fwrite($ff,"pushed chunksize=".strlen($buf)." size=".$size." ob_len=".ob_get_length()."\n");
            $size-=$chunksize;
            ob_flush();
//            sleep(1);
        }
        fwrite($ff,"Connection lost status ".connection_status()."\n\n\n");
        if (feof($fp)) fwrite($ff,"EOF reached\n");
        if (feof($fp)) $result = $db->query('UPDATE '.$db->prefix.'attach_2_files SET downloads=downloads+1 WHERE id=\''.$attach_item.'\'')or error();
        fclose($fp); 
        fclose($ff);

it doesn't work well sad a dummie session is created that can't be stopped, and "pushing" file till it's end...
very odd.

Re: Attachment Mod 2.0.1 by Frank H

Dexus wrote:

it's very annoying thing, to edit rules of many groups and many forums.... sad
is there any way to set "default" rules? or to multiset them???

Default is to allow nothing ... I might add 'base rules upon' in future version, similar to what punbb already has...

iku wrote:

Frank: Yep I know that but I don't want it to work that way.

Well, I'm not forcing anyone to use it, I could add a zillion features, but then it would be very far from the idea of PunBB, right? ... and I wanted to keep this mod as simple as possible, othervise I would have done a temporary storage, javascripts doing this and that etc. But I made this mod for myself, and released it in public for anyone else that want to have attachments... feel free to write another mod that suits your needs (wasn't there another image upload mod? Perhaps that better suites to your needs?)

treffynnon wrote:

When I am logged in as an administrator no attachments get uploaded.  But if I log in as a standard user then I can upload.  Basically in both modes it actually attempts to upload the file (can tell by the length of time it takes) but in admin mode the file does not seem to be added to the database or to the base of the admins posts.

Administrators override security checks, so make sure all the code is in place, and that your administratorgroup has the right group id. (should be 1) ... you can also check so that you haven't set max upload size to something that php cannot handle. (Documentation chapter 1.6)

Dexus wrote:

I was trying to implement "resume" of broken downloads of attachments, but faced some problems... and decided that it's nearly impossible due to "Range" parameter parsed by http server (apache).

Ouch, ah well one cannot have everything in the world wink
And this isn't just a config issue/lack of info sent to apache? (seeing that you seem to have all the neccessary headers), as there seems to be people in the php documentation that has managed to send resumes ...

73

Re: Attachment Mod 2.0.1 by Frank H

theoretically resuming is possible, but actually there are some issues, makin it nearly impossible sad
but most of fileservers workds with resume.. how???

74 (edited by Dexus 2010-09-05 18:10)

Re: Attachment Mod 2.0.1 by Frank H

Okay, I've made a "rules wizard", using javascript for copy-pasting of single rules, and a row/col as groups/forums (i.e. copy from one user to other for all the forums, or from one forum to other rules of all usergroups).
I don't know how good it will display on narrow screens... Anyways, the view of rules is formatted as table, with usergroups as columns, and forums as rows.
You may try it from here [updated 2010-09-05].
And you may write url of this page directly, or add a kind of code

                            <tr>
                                <th scope="row"><div><a href='attach_rulwiz.php'>Rules Wizard</a></div></th>
                                <td>
                                    <span>As above, but with more friendly interface</span>
                                </td>
                            </tr>

into AP_Attachment_Mod.php about 1169 line after

                            <tr>
                                <th scope="row"><div><input type="submit" name="list_rules" value="List Rules" tabindex="6" /></div></th>
                                <td>
                                    <span>In here you can see what permissions tou have set for different groups, and you can assign groups access to attach files to their posts or change already existing rules. All settings are per group and per forum basis. No rules = no permissions.</span>
                                </td>
                            </tr>

* Little update: forum categories added.

* Note: when single rules or rows or columns are "copyed", only their position is stored, not content. Therefore if you change somehow what you just copyed, you'll past changed content. Also, you may copy rule only once, and paste it many times.

* Screenshot: http://volutar.eu5.org/rulwiz.png

75

Re: Attachment Mod 2.0.1 by Frank H

About resumed downloading and cacheble images i see only one reasonable way - is to store files with their normal names (and escaped), and if they have double - adding some postfix, to make unique filename. And pointing exactly on files.. Apache or other http server knows better how to send files...