Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-webphotoalbum-git / www / inc / photo.class.inc.php @ 67466a8d

Historique | Voir | Annoter | Télécharger (8 ko)

1
<?php
2
/* Photo class for dealing with individual images
3

4
*/
5

    
6
class C_photo {
7
        var $id;
8
        var $preview;
9
        var $previewsize;
10
        var $mq;
11
        var $hq;
12
        var $name;
13
        var $caption;
14
        var $file;
15
        var $number;
16
        var $counter;
17
        var $album;
18
        var $comments; //rendered string
19

    
20
        function C_photo($file, $number) {
21
                global $root, $gallery_dir, $galerie, $db;
22
                
23
                $this->file = $file;
24
                $this->number = $number;
25
                $this->album = $galerie;
26
                //init from filesystem
27
                //preview
28
                $this->preview = "$gallery_dir/$galerie/lq/img-" . $this->number . ".jpg";
29
                $this->previewsize = getimagesize($this->preview);
30
                //MQ
31
                if (file_exists("$root/$gallery_dir/$galerie/mq/img-" . $this->number . ".jpg")) {
32
                        $this->mq = "$gallery_dir/$galerie/mq/img-" . $this->number . ".jpg";
33
                }
34
                //HQ
35
                if (file_exists("$root/$gallery_dir/$galerie/hq/img-" . $this->number . ".jpg")) {
36
                        $this->hq = "$gallery_dir/$galerie/hq/img-" . $this->number . ".jpg";
37
                }
38
                if ($GLOBALS['have_sqlite']) { //query just once
39
                        require_once("$root/inc/db.class.inc.php");
40
                        $sql = "select * from photo where ";
41
                        $sql .= "number=" . $this->number . " and ";
42
                        $sql .= "album='" . $this->album . "'";
43
                        $db->query($sql);
44
                }
45
                $this->readCaption();
46
                $this->readCounter(); //reads access log number
47
                if ($GLOBALS['have_sqlite']) { //need to get photo id first
48
                        if (!$db->count()) {//no record for this photo, let's update the record
49
                                //FIXME - if no photo data in db, create a unique index for it
50
                                //and add number, album, caption and views.
51
                                $sql = "insert into photo (name, caption, counter, number, album)";
52
                                $sql .= " values (";
53
                                $sql .= "\"" . sqlite_escape_string($this->name) . "\", ";
54
                                $sql .= "\"" . sqlite_escape_string(strtr($this->caption,"\"","'")) . "\", ";
55
                                $sql .= $this->counter . ", ";
56
                                $sql .= $this->number . ", ";
57
                                $sql .= "\"" . $this->album . "\"";
58
                                $sql .= ")";
59
                                $db->query($sql);
60
                                print "\n\n<!-- We've moved the data to the database.-->";
61
                                //now we still need to query for the id
62
                                $sql = "select id from photo where ";
63
                                $sql .= "number=" . $this->number . " and ";
64
                                $sql .= "album='" . $this->album . "'";
65
                                $db->query($sql);
66
                        }
67
                        $db->rewind();
68
                        $resultarray = sqlite_fetch_array($db->result);
69
                        $this->id = $resultarray["id"];
70
                        print "\n\n<!-- image id: " . $this->id . " -->\n";
71
                }
72
                $this->readComments();
73
        }
74

    
75
        function readCaption() {
76
                global $have_sqlite, $root, $gallery_dir, $galerie, $db;
77
                
78
                /* reads name and caption of a photo
79
                   - either from sqlite database or filesystem
80
                 */
81
                 if ($have_sqlite) {
82
                                //try reading from sqlite
83
                                if ($db->count()) {
84
                                        $result = sqlite_fetch_array($db->result);
85
                                        $this->name = $result["name"];
86
                                        $this->caption = $result["caption"];
87
                                        return; //no need to fallback anymore
88
                                }
89
                 } 
90
                 
91
                 //we falback to filesystem
92
                  $buffer = "";
93
                        $captionfile = "$root/$gallery_dir/$galerie/comments/" . $this->number . ".txt";
94
                        $fh = @fopen($captionfile, "r");
95
                        if ($fh) {
96
                                 while (!feof($fh)) {
97
                                                 $buffer .= fgets($fh, 4096);
98
                                 }
99
                                 fclose($fh);
100
                        } else { // no caption file
101
                                $this->name = __("Photo ") . $this->number;
102
                                return;
103
                        }
104
                        //parse buffer
105
                        if(eregi("^<span>(.*)</span>( - )?(.*)", $buffer, $x)) {
106
                                $this->name = $x[1]; //mostly "Photo"
107
                                $this->caption = chop($x[3]);
108
                        } else {
109
                                $this->caption = $buffer;
110
                        }
111
        }
112
        
113
        function readCounter() {
114
                global $log_access, $root, $gallery_dir, $galerie, $db;
115

    
116
                if ($GLOBALS['have_sqlite']) {
117
                        //try reading from sqlite
118
                        if ($db->count()) {
119
                                $db->rewind();
120
                                $result = sqlite_fetch_array($db->result);
121
                                $this->counter = $result["counter"];
122
                                return; //no need to fallback anymore
123
                        }
124
                } 
125
                //we fallback to filesystem :/
126
                 if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
127
                         $log = "$root/$gallery_dir/$galerie/comments/log_" . $this->number . ".txt";
128
                         if (file_exists($log)){
129
                                 $fh = @fopen($log, "r");
130
                                 $this->counter = rtrim(fgets($fh));
131
                                 fclose($fh);
132
                         } else {
133
                                 $this->counter = 0;
134
                         }
135
                 } else {
136
                         //doesn't do anything if no perms
137
                         print "<!-- ". __('WARNING: comment dir not writable') . "-->\n";
138
                         return 0; //failure
139
                 }
140
                 return 1; //success
141
        }
142

    
143
        function readComments() {
144
                global $root, $gallery_dir, $galerie, $db;
145
                
146
                if ($GLOBALS['have_sqlite']) {
147
                        //we have and will use SQLite
148
                        //FIXME
149
                        print "\n<!--SQLITE comments FIXME-->\n\n";
150
                        return 1;
151
                } else {
152
                        //filesystem
153
                         $comments = "$root/$gallery_dir/$galerie/comments/user_" . $this->number . ".txt";
154
                        if (file_exists($comments)){
155
                                $buffer = "";
156
                                $fh = @fopen($comments, "r");
157
                                if ($fh) {
158
                                         while (!feof($fh)) {
159
                                                         $buffer .= fgets($fh, 4096);
160
                                         }
161
                                         $this->comments = $buffer;
162
                                         fclose($fh);
163
                                }
164
                        }
165
                }
166
        }
167
        
168
        function renderCounter() {
169
                
170
                 print "\n<div id=\"log\">\n";
171
                 print __('This image has been viewed') . " ";
172
                 print "<strong>" . $this->counter . "</strong>". " " . __('times') . ".";
173
                 print "</div>\n\n";
174
                 $this->writeCounter(); //save state
175

    
176
        }
177

    
178
        function writeCounter() {
179
                global $log_access, $root, $gallery_dir, $galerie, $page, $db;
180

    
181
                $this->counter++; //we add to counter
182
                if ($GLOBALS['have_sqlite']) {
183
                        //we have SQLite
184
                        $sql = "update photo set counter=" . $this->counter;
185
                        $sql .= " where id=" . $this->id;
186
                        $db->query($sql);
187
                        return; //no need to fallback anymore
188
                } 
189
                 //fallback to filesystem
190
                 if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
191
                         $log = "$root/$gallery_dir/$galerie/comments/log_". $this->number .".txt";
192
                         if (file_exists($log) && !is_writable($log)) {
193
                                 print "\n\n\n<!-- cannot open $log. Check permissions.";
194
                                 print "\nAborting counter write -->\n";
195
                                 return 0;
196
                         }
197
                         $fh = fopen($log,"w");
198
                         if (!fwrite($fh, $this->counter . "\n")) {
199
                                        $page->error( __('Could not write to') . $log . "!");
200
                                        $page->footer();
201
                                        exit; //stop everything
202
                         }
203
                         fclose($fh);
204
                 }
205
        }
206

    
207
        function renderBigSize() {
208

    
209
   if ($this->mq || $this->hq) {
210
                 print "<div id=\"mqhq\">";
211
                 if ($this->mq) {
212
                                print "<a href=\"" . $this->mq . "\">". __('MQ') . "</a> ";
213
                 }
214
                 if ($this->hq) {
215
                                print "<a href=\"" . $this->hq . "\">" . __('HQ') . "</a>";
216
                 }
217
                 print "</div>\n";
218
         }
219
        }
220

    
221
        function renderPreview() {
222

    
223
   $divheight = $this->previewsize[1] + 10;
224
   print "<div id=\"image\" style=\"height: ${divheight}px\">\n"; // extra kludge 
225
                                                                 // because of tall 
226
                                                                 // images
227

    
228
   print "<img id=\"preview\" " . $this->previewsize[3] . " src=\"". $this->file;
229
         print "\" alt=\"$snimek\" />\n";
230
        }
231

    
232
        function renderCaption() {
233
        
234
                print "<div class=\"comment\">";
235
                print "<span>" . $this->name . "</span>";
236
                if ($this->caption) {
237
                        print " &ndash; ";
238
                        print $this->caption;
239
                        print "</div>";
240
                }
241
        }
242

    
243
        function addComment($comment_name, $comment_data) { //adds comment to file or database
244
                global $log_access, $root, $gallery_dir, $galerie, $page;
245

    
246
                if ($GLOBALS['have_sqlite']) {
247
                        //sqlite
248
                        print "\n<!--SQLITE comments addition FIXME-->\n\n";
249
                } else {
250
                                //filesystem
251
                                if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
252
                                        $comment = "$root/$gallery_dir/$galerie/comments/user_";
253
                                        $comment .= $this->number . ".txt";
254
                                        if (file_exists($comment) && !is_writable($comment)) {
255
                                                        $page->error("Permission Denied", __('Could not write to')  . $comment . 
256
                                                                "!\n Check permissions.\n");
257
                                                        $page->footer();
258
                                                        exit; //stop everything
259
                                        }
260

    
261
                                        $fh = fopen("$comment", "a");
262
                                        if (!$comment_name) {
263
                                                        $comment_name = __('Anonymous');
264
                                        }
265
                                        if (!fwrite($fh, "<div class=\"commententry\">\n")) {
266
                                                        $page->error("Write Failed",  __('Could not write to')  . $comment . "!" );
267
                                                        $page->footer();
268
                                                        exit; //stop everything
269
                                        }
270
                                        fwrite($fh, "   <div class=\"name\">" . __('Comment from') . "<em>$comment_name</em></div>\n",90);
271
                                        fwrite($fh, "   <div class=\"commentdata\">$comment_data</div>\n",280);
272
                                        fwrite($fh, "</div>\n");
273
                                        
274
                                        fclose($fh);
275
                                }
276
                }
277
        }
278
}
279
?>
Redmine Appliance - Powered by TurnKey Linux