aboutsummaryrefslogtreecommitdiff
path: root/www/inc/photo.class.inc.php
blob: 1a55e59879c8dca8b1c0631ed89d9f5ecc57911d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/* Photo class for dealing with individual images

*/

class C_photo {
	var $id;
	var $preview;
	var $previewsize;
	var $mq;
	var $hq;
	var $name;
	var $caption;
	var $file;
	var $number;
	var $counter;
	var $album;
	var $comments; //rendered string

	function C_photo($file, $number) {
		global $root, $gallery_dir, $galerie, $db;
		
		$this->file = $file;
		$this->number = $number;
		$this->album = $galerie;
		//init from filesystem
		//preview
		$this->preview = "$gallery_dir/$galerie/lq/img-" . $this->number . ".jpg";
		$this->previewsize = getimagesize($this->preview);
		//MQ
		if (file_exists("$root/$gallery_dir/$galerie/mq/img-" . $this->number . ".jpg")) {
			$this->mq = "$gallery_dir/$galerie/mq/img-" . $this->number . ".jpg";
		}
		//HQ
		if (file_exists("$root/$gallery_dir/$galerie/hq/img-" . $this->number . ".jpg")) {
			$this->hq = "$gallery_dir/$galerie/hq/img-" . $this->number . ".jpg";
		}
		if ($GLOBALS['have_sqlite']) { //query just once
			require_once("$root/inc/db.class.inc.php");
			$sql = "select * from photo where ";
			$sql .= "number=" . $this->number . " and ";
			$sql .= "album='" . $this->album . "'";
			$db->query($sql);
		}
		$this->readCaption();
		$this->readCounter(); //reads access log number
		if ($GLOBALS['have_sqlite']) { //need to get photo id first
			if (!$db->count()) {//no record for this photo, let's update the record
				//FIXME - if no photo data in db, create a unique index for it
				//and add number, album, caption and views.
				$sql = "insert into photo (name, caption, counter, number, album)";
				$sql .= " values (";
				$sql .= "\"" . sqlite_escape_string($this->name) . "\", ";
				$sql .= "\"" . sqlite_escape_string(strtr($this->caption,"\"","'")) . "\", ";
				$sql .= $this->counter . ", ";
				$sql .= $this->number . ", ";
				$sql .= "\"" . $this->album . "\"";
				$sql .= ")";
				$db->query($sql);
				print "\n\n<!-- We've moved the data to the database.-->";
				//now we still need to query for the id
				$sql = "select id from photo where ";
				$sql .= "number=" . $this->number . " and ";
				$sql .= "album='" . $this->album . "'";
				$db->query($sql);
			}
			$db->rewind();
			$resultarray = sqlite_fetch_array($db->result);
			$this->id = $resultarray["id"];
			print "\n\n<!-- image id: " . $this->id . " -->\n";
		}
		$this->readComments();
	}

	function readCaption() {
		global $have_sqlite, $root, $gallery_dir, $galerie, $db;
		
		/* reads name and caption of a photo
		   - either from sqlite database or filesystem
		 */
		 if ($have_sqlite) {
				//try reading from sqlite
				if ($db->count()) {
					$result = sqlite_fetch_array($db->result);
					$this->name = $result["name"];
					$this->caption = $result["caption"];
					return; //no need to fallback anymore
				}
		 } 
		 
		 //we falback to filesystem
		  $buffer = "";
			$captionfile = "$root/$gallery_dir/$galerie/comments/" . $this->number . ".txt";
			$fh = @fopen($captionfile, "r");
			if ($fh) {
				 while (!feof($fh)) {
						 $buffer .= fgets($fh, 4096);
				 }
				 fclose($fh);
			} else { // no caption file
				$this->name = __("Photo ") . $this->number;
				return;
			}
			//parse buffer
			if(preg_match("|^<span>(.*)</span>( - )?(.*)|i", $buffer, $x)) {
				$this->name = $x[1]; //mostly "Photo"
				$this->caption = chop($x[3]);
			} else {
				$this->caption = $buffer;
			}
	}
	
	function readCounter() {
		global $log_access, $root, $gallery_dir, $galerie, $db;

		if ($GLOBALS['have_sqlite']) {
			//try reading from sqlite
			if ($db->count()) {
				$db->rewind();
				$result = sqlite_fetch_array($db->result);
				$this->counter = $result["counter"];
				return; //no need to fallback anymore
			}
		} 
		//we fallback to filesystem :/
		 if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
			 $log = "$root/$gallery_dir/$galerie/comments/log_" . $this->number . ".txt";
			 if (file_exists($log)){
				 $fh = @fopen($log, "r");
				 $this->counter = rtrim(fgets($fh));
				 fclose($fh);
			 } else {
				 $this->counter = 0;
			 }
		 } else {
			 //doesn't do anything if no perms
			 print "<!-- ". __('WARNING: comment dir not writable') . "-->\n";
			 return 0; //failure
		 }
		 return 1; //success
	}

	function readComments() {
		global $root, $gallery_dir, $galerie, $db;
		
		if ($GLOBALS['have_sqlite']) {
			//we have and will use SQLite
			//FIXME
			print "\n<!--SQLITE comments FIXME-->\n\n";
			return 1;
		} else {
			//filesystem
		 	$comments = "$root/$gallery_dir/$galerie/comments/user_" . $this->number . ".txt";
			if (file_exists($comments)){
				$buffer = "";
				$fh = @fopen($comments, "r");
				if ($fh) {
					 while (!feof($fh)) {
							 $buffer .= fgets($fh, 4096);
					 }
					 $this->comments = $buffer;
					 fclose($fh);
				}
			}
		}
	}
	
	function renderCounter() {
		
		 print "\n<div id=\"log\">\n";
		 print __('This image has been viewed') . " ";
		 print "<strong>" . $this->counter . "</strong>". " " . __('times') . ".";
		 print "</div>\n\n";
		 $this->writeCounter(); //save state

	}

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

		$this->counter++; //we add to counter
		if ($GLOBALS['have_sqlite']) {
			//we have SQLite
			$sql = "update photo set counter=" . $this->counter;
			$sql .= " where id=" . $this->id;
			$db->query($sql);
			return; //no need to fallback anymore
		} 
		 //fallback to filesystem
		 if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
			 $log = "$root/$gallery_dir/$galerie/comments/log_". $this->number .".txt";
			 if (file_exists($log) && !is_writable($log)) {
				 print "\n\n\n<!-- cannot open $log. Check permissions.";
				 print "\nAborting counter write -->\n";
				 return 0;
			 }
			 $fh = fopen($log,"w");
			 if (!fwrite($fh, $this->counter . "\n")) {
					$page->error( __('Could not write to') . $log . "!");
					$page->footer();
					exit; //stop everything
			 }
			 fclose($fh);
		 }
	}

	function renderBigSize() {

   if ($this->mq || $this->hq) {
		 print "<div id=\"mqhq\">";
		 if ($this->mq) {
				print "<a href=\"" . $this->mq . "\">". __('MQ') . "</a> ";
		 }
		 if ($this->hq) {
				print "<a href=\"" . $this->hq . "\">" . __('HQ') . "</a>";
		 }
		 print "</div>\n";
	 }
	}

	function renderPreview() {

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

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

	function renderCaption() {
	
		print "<div class=\"comment\">";
		print "<span>" . $this->name . "</span>";
		if ($this->caption) {
			print " &ndash; ";
			print $this->caption;
			print "</div>";
		}
	}

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

		if ($GLOBALS['have_sqlite']) {
			//sqlite
			print "\n<!--SQLITE comments addition FIXME-->\n\n";
		} else {
				//filesystem
				if (is_writable("$root/$gallery_dir/$galerie/comments")) { // needs perms
					$comment = "$root/$gallery_dir/$galerie/comments/user_";
					$comment .= $this->number . ".txt";
					if (file_exists($comment) && !is_writable($comment)) {
							$page->error("Permission Denied", __('Could not write to')  . $comment . 
								"!\n Check permissions.\n");
							$page->footer();
							exit; //stop everything
					}

					$fh = fopen("$comment", "a");
					if (!$comment_name) {
							$comment_name = __('Anonymous');
					}
					if (!fwrite($fh, "<div class=\"commententry\">\n")) {
							$page->error("Write Failed",  __('Could not write to')  . $comment . "!" );
							$page->footer();
							exit; //stop everything
					}
					fwrite($fh, "   <div class=\"name\">" . __('Comment from') . "<em>$comment_name</em></div>\n",90);
					fwrite($fh, "   <div class=\"commentdata\">$comment_data</div>\n",280);
					fwrite($fh, "</div>\n");
					
					fclose($fh);
				}
		}
	}
}
?>