5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/Error.class.php';
29 * Factory method which creates an ArtifactFile from an artifactFile ID
31 * @param int The artifactFile ID
32 * @param array The result array, if it's passed in
33 * @return object Artifact object
35 function &artifactfile_get_object($artifact_file_id,$data=false) {
36 global $ARTIFACTFILE_OBJ;
37 if (!isset($ARTIFACTFILE_OBJ["_".$artifact_file_id."_"])) {
39 //the db result handle was passed in
41 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
42 array ($artifact_file_id)) ;
43 if (db_numrows($res) <1 ) {
44 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]=false;
47 $data =& db_fetch_array($res);
49 $Artifact =& artifact_get_object($data["artifact_id"]);
50 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]= new ArtifactFile($Artifact,$data);
52 return $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"];
56 class ArtifactFile extends Error {
59 * The artifact object.
61 * @var object $Artifact.
63 var $Artifact; //object
68 * @var array $data_array
73 * ArtifactFile - constructor.
75 * @param object The Artifact object.
76 * @param array (all fields from artifact_file_user_vw) OR id from database.
77 * @return boolean success.
79 function ArtifactFile(&$Artifact, $data=false) {
83 if (!$Artifact || !is_object($Artifact)) {
84 $this->setError('ArtifactFile: No Valid Artifact');
87 //did ArtifactType have an error?
88 if ($Artifact->isError()) {
89 $this->setError('ArtifactFile: '.$Artifact->getErrorMessage());
92 $this->Artifact =& $Artifact;
95 if (is_array($data)) {
96 $this->data_array =& $data;
99 if (!$this->fetchData($data)) {
109 * create - create a new item in the database.
111 * @param string Filename of the item.
112 * @param string Item filetype.
113 * @param string Item filesize.
114 * @param binary Binary item data.
115 * @param string Item description.
116 * @return id on success / false on failure.
118 function create($filename, $filetype, $filesize, $bin_data, $description='None') {
119 // Some browsers don't supply mime type if they don't know it
121 // Let's be on safe side?
122 $filetype = 'application/octet-stream';
128 if (!$filename || !$filetype || !$filesize || !$bin_data) {
129 //echo '<p>|'.$filename.'|'.$filetype.'|'.$filesize.'|'.$bin_data.'|';
130 $this->setError(_('ArtifactFile: File name, type, size, and data are required'));
134 if (session_loggedin()) {
135 $userid=user_getid();
140 // If $filetype is "text/plain", $bin_data convert UTF-8 encoding.
141 if (strcasecmp($filetype,"text/plain") === 0 &&
142 function_exists('mb_convert_encoding') &&
143 function_exists('mb_detect_encoding')) {
144 $bin_data = mb_convert_encoding($bin_data,'UTF-8',mb_detect_encoding($bin_data, "auto"));
145 $filesize = strlen($bin_data);
150 $res = db_query_params ('INSERT INTO artifact_file
151 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
152 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
153 array ($this->Artifact->getID(),
155 base64_encode($bin_data),
162 $id=db_insertid($res,'artifact_file','id');
166 $this->setError('ArtifactFile: '.db_error());
171 // skip this unless we need it later - save a db query
174 // Now set up our internal data structures
176 if (!$this->fetchData($id)) {
182 $this->Artifact->addHistory('File Added',$id.': '.$filename);
189 * delete - delete this artifact file from the db.
191 * @return boolean success.
194 if (!forge_check_perm ('tracker', $this->Artifact->ArtifactType->getID(), 'tech')) {
195 $this->setPermissionDeniedError();
198 $res = db_query_params ('DELETE FROM artifact_file WHERE id=$1',
199 array ($this->getID())) ;
200 if (!$res || db_affected_rows($res) < 1) {
201 $this->setError('ArtifactFile: Unable to Delete');
204 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
210 * fetchData - re-fetch the data for this ArtifactFile from the database.
212 * @param int The file_id.
213 * @return boolean success.
215 function fetchData($id) {
216 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
218 if (!$res || db_numrows($res) < 1) {
219 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
222 $this->data_array =& db_fetch_array($res);
223 db_free_result($res);
228 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
230 * @return object Artifact.
232 function &getArtifact() {
233 return $this->Artifact;
237 * getID - get this ArtifactFile's ID.
239 * @return int The id #.
242 return $this->data_array['id'];
246 * getName - get the filename.
248 * @return string filename.
251 return $this->data_array['filename'];
255 * getType - get the type.
257 * @return string type.
260 return $this->data_array['filetype'];
264 * getData - get the binary data from the db.
269 return base64_decode($this->data_array['bin_data']);
273 * getSize - get the size.
278 return $this->data_array['filesize'];
282 * getDescription - get the description.
284 * @return string description.
286 function getDescription() {
287 return $this->data_array['description'];
291 * getDate - get the date file was added.
293 * @return int unix time.
296 return $this->data_array['adddate'];
300 * getSubmittedBy - get the user_id of the submitter.
302 * @return int user_id.
304 function getSubmittedBy() {
305 return $this->data_array['submitted_by'];
309 * getSubmittedRealName - get the real name of the submitter.
311 * @return string name.
313 function getSubmittedRealName() {
314 return $this->data_array['realname'];
318 * getSubmittedUnixName - get the unix name of the submitter.
320 * @return string unixname.
322 function getSubmittedUnixName() {
323 return $this->data_array['user_name'];
330 // c-file-style: "bsd"