3 * ArtifactFile.class.php - Class to handle files within an artifact
5 * Copyright 1999-2001 (c) VA Linux Systems
9 * This file is part of GForge.
11 * GForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
25 require_once('common/include/Error.class.php');
28 * Factory method which creates an ArtifactFile from an artifactFile ID
30 * @param int The artifactFile ID
31 * @param array The result array, if it's passed in
32 * @return object Artifact object
34 function &artifactfile_get_object($artifact_file_id,$data=false) {
35 global $ARTIFACTFILE_OBJ;
36 if (!isset($ARTIFACTFILE_OBJ["_".$artifact_file_id."_"])) {
38 //the db result handle was passed in
40 $res=db_query("SELECT * FROM artifact_file_user_vw WHERE id='$artifact_file_id'");
41 if (db_numrows($res) <1 ) {
42 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]=false;
45 $data =& db_fetch_array($res);
47 $Artifact =& artifact_get_object($data["artifact_id"]);
48 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]= new ArtifactFile($Artifact,$data);
50 return $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"];
54 class ArtifactFile extends Error {
57 * The artifact object.
59 * @var object $Artifact.
61 var $Artifact; //object
66 * @var array $data_array
71 * ArtifactFile - constructor.
73 * @param object The Artifact object.
74 * @param array (all fields from artifact_file_user_vw) OR id from database.
75 * @return boolean success.
77 function ArtifactFile(&$Artifact, $data=false) {
81 if (!$Artifact || !is_object($Artifact)) {
82 $this->setError('ArtifactFile: No Valid Artifact');
85 //did ArtifactType have an error?
86 if ($Artifact->isError()) {
87 $this->setError('ArtifactFile: '.$Artifact->getErrorMessage());
90 $this->Artifact =& $Artifact;
93 if (is_array($data)) {
94 $this->data_array =& $data;
97 if (!$this->fetchData($data)) {
107 * create - create a new item in the database.
109 * @param string Filename of the item.
110 * @param string Item filetype.
111 * @param string Item filesize.
112 * @param binary Binary item data.
113 * @param string Item description.
114 * @return id on success / false on failure.
116 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("INSERT INTO artifact_file
151 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
153 ('".$this->Artifact->getID()."','$description','". base64_encode($bin_data) ."','$filename',
154 '$filesize','$filetype','". time() ."','$userid')");
156 $id=db_insertid($res,'artifact_file','id');
160 $this->setError('ArtifactFile: '.db_error());
165 // skip this unless we need it later - save a db query
168 // Now set up our internal data structures
170 if (!$this->fetchData($id)) {
176 $this->Artifact->addHistory('File Added',$id.': '.$filename);
183 * delete - delete this artifact file from the db.
185 * @return boolean success.
188 if (!$this->Artifact->ArtifactType->userIsTechnician()) {
189 $this->setPermissionDeniedError();
192 $res=db_query("DELETE FROM artifact_file WHERE id='". $this->getID() ."'");
193 if (!$res || db_affected_rows($res) < 1) {
194 $this->setError('ArtifactFile: Unable to Delete');
197 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
203 * fetchData - re-fetch the data for this ArtifactFile from the database.
205 * @param int The file_id.
206 * @return boolean success.
208 function fetchData($id) {
209 $res=db_query("SELECT * FROM artifact_file_user_vw WHERE id='$id'");
210 if (!$res || db_numrows($res) < 1) {
211 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
214 $this->data_array =& db_fetch_array($res);
215 db_free_result($res);
220 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
222 * @return object Artifact.
224 function &getArtifact() {
225 return $this->Artifact;
229 * getID - get this ArtifactFile's ID.
231 * @return int The id #.
234 return $this->data_array['id'];
238 * getName - get the filename.
240 * @return string filename.
243 return $this->data_array['filename'];
247 * getType - get the type.
249 * @return string type.
252 return $this->data_array['filetype'];
256 * getData - get the binary data from the db.
260 function &getData() {
261 return base64_decode($this->data_array['bin_data']);
265 * getSize - get the size.
270 return $this->data_array['filesize'];
274 * getDescription - get the description.
276 * @return string description.
278 function getDescription() {
279 return $this->data_array['description'];
283 * getDate - get the date file was added.
285 * @return int unix time.
288 return $this->data_array['adddate'];
292 * getSubmittedBy - get the user_id of the submitter.
294 * @return int user_id.
296 function getSubmittedBy() {
297 return $this->data_array['submitted_by'];
301 * getSubmittedRealName - get the real name of the submitter.
303 * @return string name.
305 function getSubmittedRealName() {
306 return $this->data_array['realname'];
310 * getSubmittedUnixName - get the unix name of the submitter.
312 * @return string unixname.
314 function getSubmittedUnixName() {
315 return $this->data_array['user_name'];