DIFF: blogcms\libs\MEDIA.php : nucleus\libs\MEDIA.php

1
<?php
2
/**
BLOG:CMS
  * BLOG:CMS: PHP/MySQL Personal Content Management System
BLOG:CMS
  * http://blogcms.com/
BLOG:CMS
  * http://forum.blogcms.com/
BLOG:CMS
  *
BLOG:CMS
  * 2003-2005, (c) Radek HULAN
BLOG:CMS
  * http://hulan.cz/
BLOG:CMS
  *
BLOG:CMS
  * Portions of this software are based on Nucleus CMS
BLOG:CMS
  * http://nucleuscms.org/
Nucleus
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
Nucleus
  * Copyright (C) 2002-2004 The Nucleus Group
12
  *
13
  * This program is free software; you can redistribute it and/or
14
  * modify it under the terms of the GNU General Public License
15
  * as published by the Free Software Foundation; either version 2
16
  * of the License, or (at your option) any later version.
BLOG:CMS
**/
Nucleus
  * (see nucleus/documentation/index.html#license for more info)
Nucleus
  *
Nucleus
  * Media classes for nucleus
Nucleus
  */
18
19
20
/**
21
  * Represents the media objects for a certain member
22
  */
23
class MEDIA {
24
24
var $private;
24
var $collection;
24
var $filename;
24
var $timestamp;
24
25
	/**
26
	  * Gets the list of collections available to the currently logged
27
	  * in member
28
	  *
29
	  * @returns array of dirname => display name
30
	  */
31
	function getCollectionList() {
32
		global $member, $DIR_MEDIA;
33
34
		$collections = array();
35
36
		// add private directory for member
37
		$collections[$member->getID()] = 'Private Collection';
38
39
		// add global collections
40
		if (!is_dir($DIR_MEDIA)) return $collections;
41
42
		$dirhandle = opendir($DIR_MEDIA);
43
		while ($dirname = readdir($dirhandle)) {
44
			// only add non-numeric (numeric=private) dirs
45
			if (@is_dir($DIR_MEDIA . $dirname) && ($dirname != '.') && ($dirname != '..') && ($dirname != 'CVS') && (!is_numeric($dirname)))  {
46
				$collections[$dirname] = $dirname;
47
			}
48
		}
49
		closedir($dirhandle);
50
51
		return $collections;
52
53
	}
54
55
	/**
56
	  * Returns an array of MEDIAOBJECT objects for a certain collection
57
	  *
58
	  * @param $collection
59
	  *		name of the collection
60
	  * @param $filter
61
	  *		filter on filename (defaults to none)
62
	  */
63
	function getMediaListByCollection($collection, $filter = '') {
64
		global $DIR_MEDIA;
65
66
		$filelist = array();
67
68
		// 1. go through all objects and add them to the filelist
69
70
		$mediadir = $DIR_MEDIA . $collection . '/';
71
72
		// return if dir does not exist
73
		if (!is_dir($mediadir)) return $filelist;
74
75
		$dirhandle = opendir($mediadir);
76
		while ($filename = readdir($dirhandle)) {
77
			// only add files that match the filter
78
			if (!@is_dir($filename) && MEDIA::checkFilter($filename, $filter))
79
				array_push($filelist, new MEDIAOBJECT($collection, $filename, filemtime($mediadir . $filename)));
80
		}
81
		closedir($dirhandle);
82
83
		// sort array so newer files are shown first
84
		usort($filelist, 'sort_media');
85
86
		return $filelist;
87
	}
88
89
	function checkFilter($strText, $strFilter) {
90
		if ($strFilter == '')
91
			return 1;
92
		else
93
			return is_integer(strpos(strtolower($strText), strtolower($strFilter)));
94
	}
95
96
	/**
97
	  * checks if a collection exists with the given name, and if it's
98
	  * allowed for the currently logged in member to upload files to it
99
	  */
100
	function isValidCollection($collectionName) {
101
		global $member, $DIR_MEDIA;
102
103
		// private collections only accept uploads from their owners
104
		if (is_numeric($collectionName))
105
			return ($member->getID() == $collectionName);
106
107
		// other collections should exists and be writable
108
		$collectionDir = $DIR_MEDIA . $collectionName;
109
		return (@is_dir($collectionDir) || @is_writable($collectionDir));
110
	}
111
112
	/**
113
	  * Adds an uploaded file to the media archive
114
	  *
115
	  * @param collection
116
	  *		collection
117
	  * @param uploadfile
BLOG:CMS
	  *		the array from the array
Nucleus
	  *		the postFileInfo(..) array
119
	  * @param filename
120
	  *		the filename that should be used to save the file as
121
	  *		(date prefix should be already added here)
122
	  */
123
	function addMediaObject($collection, $uploadfile, $filename) {
124
		global $DIR_MEDIA;
125
126
		// don't allow uploads to unknown or forbidden collections
127
		if (!MEDIA::isValidCollection($collection))
128
			return _ERROR_DISALLOWED;
129
130
		// check dir permissions (try to create dir if it does not exist)
131
		$mediadir = $DIR_MEDIA . $collection;
132
133
		// try to create new private media directories if needed
134
		if (!@is_dir($mediadir) && is_numeric($collection)) {
135
			$oldumask = umask(0000);
136
			if (!@mkdir($mediadir, 0777))
137
				return _ERROR_BADPERMISSIONS;
138
			umask($oldumask);
139
		}
140
141
		// if dir still not exists, the action is disallowed
142
		if (!@is_dir($mediadir))
143
			return _ERROR_DISALLOWED;
144
145
		if (!is_writeable($mediadir))
146
			return _ERROR_BADPERMISSIONS;
147
148
		// add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
149
		$mediadir .= '/';
150
151
		if (file_exists($mediadir . $filename))
152
			return _ERROR_UPLOADDUPLICATE;
153
154
		// move file to directory
155
		if (is_uploaded_file($uploadfile)) {
156
			if (!@move_uploaded_file($uploadfile, $mediadir . $filename))
157
				return _ERROR_UPLOADMOVE;
158
		} else {
159
			if (!copy($uploadfile, $mediadir . $filename))
160
				return _ERROR_UPLOADCOPY ;
161
		}
162
163
		// chmod uploaded file
164
		$oldumask = umask(0000);
165
		@chmod($mediadir . $filename, 0644);
166
		umask($oldumask);
167
168
		return '';
169
170
	}
171
172
	/**
173
	 * Adds an uploaded file to the media dir.
174
	 *
175
	 * @param $collection
176
	 *		collection to use
177
	 * @param $filename
178
	 *		the filename that should be used to save the file as
179
	 *		(date prefix should be already added here)
180
	 * @param &$data
181
	 *		File data (binary)
182
	 *
183
	 * NOTE: does not check if $collection is valid.
184
	 */
185
	function addMediaObjectRaw($collection, $filename, &$data) {
186
		global $DIR_MEDIA;
187
188
		// check dir permissions (try to create dir if it does not exist)
189
		$mediadir = $DIR_MEDIA . $collection;
190
191
		// try to create new private media directories if needed
192
		if (!@is_dir($mediadir) && is_numeric($collection)) {
193
			$oldumask = umask(0000);
194
			if (!@mkdir($mediadir, 0777))
195
				return _ERROR_BADPERMISSIONS;
196
			umask($oldumask);
197
		}
198
199
		// if dir still not exists, the action is disallowed
200
		if (!@is_dir($mediadir))
201
			return _ERROR_DISALLOWED;
202
203
		if (!is_writeable($mediadir))
204
			return _ERROR_BADPERMISSIONS;
205
206
		// add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)
207
		$mediadir .= '/';
208
209
		if (file_exists($mediadir . $filename))
210
			return _ERROR_UPLOADDUPLICATE;
211
212
		// create file
213
		$fh = @fopen($mediadir . $filename, 'wb');
214
		if (!$fh)
215
			return _ERROR_UPLOADFAILED;
216
		$ok = @fwrite($fh, $data);
217
		@fclose($fh);
218
		if (!$ok)
219
			return _ERROR_UPLOADFAILED;
220
221
		// chmod uploaded file
222
		$oldumask = umask(0000);
223
		@chmod($mediadir . $filename, 0644);
224
		umask($oldumask);
225
226
		return '';
227
228
	}
229
230
}
231
232
/**
233
  * Represents the characteristics of one single media-object
234
  *
235
  * Description of properties:
236
  *  - filename: filename, without paths
237
  *  - timestamp: last modification (unix timestamp)
238
  *  - collection: collection to which the file belongs (can also be a owner ID, for private collections)
239
  *  - private: true if the media belongs to a private member collection
240
  */
241
class MEDIAOBJECT {
242
242
	var $private;
242
	var $collection;
242
	var $filename;
242
	var $timestamp;
242
243
	function MEDIAOBJECT($collection, $filename, $timestamp) {
244
		$this->private = is_numeric($collection);
245
		$this->collection = $collection;
246
		$this->filename = $filename;
247
		$this->timestamp = $timestamp;
248
	}
249
250
}
251
252
/**
253
  * User-defined sort method to sort an array of MEDIAOBJECTS
254
  */
255
function sort_media($a, $b) {
256
    if ($a->timestamp == $b->timestamp) return 0;
257
    return ($a->timestamp > $b->timestamp) ? -1 : 1;
258
}
259
260
?>