5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'tracker/ArtifactStorage.class.php';
25 require_once $gfcommon.'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_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
41 array ($artifact_file_id)) ;
42 if (db_numrows($res) <1 ) {
43 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]=false;
46 $data = db_fetch_array($res);
48 $Artifact =& artifact_get_object($data["artifact_id"]);
49 $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"]= new ArtifactFile($Artifact,$data);
51 return $ARTIFACTFILE_OBJ["_".$artifact_file_id."_"];
55 class ArtifactFile extends Error {
58 * The artifact object.
60 * @var object $Artifact.
62 var $Artifact; //object
67 * @var array $data_array
72 * ArtifactFile - constructor.
74 * @param object The Artifact object.
75 * @param array (all fields from artifact_file_user_vw) OR id from database.
76 * @return boolean success.
78 function __construct(&$Artifact, $data=false) {
82 if (!$Artifact || !is_object($Artifact)) {
83 $this->setError('ArtifactFile: No Valid Artifact');
86 //did ArtifactType have an error?
87 if ($Artifact->isError()) {
88 $this->setError('ArtifactFile: '.$Artifact->getErrorMessage());
91 $this->Artifact =& $Artifact;
94 if (is_array($data)) {
95 $this->data_array =& $data;
98 if (!$this->fetchData($data)) {
108 * create - create a new item in the database.
110 * @param string Filename of the item.
111 * @param string Item filetype.
112 * @param string Item filesize.
113 * @param binary file to store.
114 * @param string Item description.
115 * @param array Array of data to change submitter and time of submit like: array('user' => 127, 'time' => 1234556789)
116 * @return id on success / false on failure.
118 function create($filename, $filetype, $filesize, $file, $description='None', $importData = array()) {
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 || !$file) {
129 //echo '<p>|'.$filename.'|'.$filetype.'|'.$filesize.'|'.$file.'|';
130 $this->setError(_('ArtifactFile: File, name, type, size are required'));
134 if (array_key_exists('user', $importData)){
135 $userid = $importData['user'];
137 if (session_loggedin()) {
138 $userid=user_getid();
144 if (array_key_exists('time',$importData)){
145 $time = $importData['time'];
151 // If $filetype is "text/plain", $bin_data convert UTF-8 encoding.
152 if (strcasecmp($filetype,"text/plain") === 0 &&
153 function_exists('mb_convert_encoding') &&
154 function_exists('mb_detect_encoding')) {
155 $bin_data = mb_convert_encoding($bin_data,'UTF-8',mb_detect_encoding($bin_data, "auto"));
156 $filesize = strlen($bin_data);
161 $res = db_query_params ('INSERT INTO artifact_file
162 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
163 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
164 array ($this->Artifact->getID(),
173 $id=db_insertid($res,'artifact_file','id');
175 ArtifactStorage::instance()->store($id, $file);
179 ArtifactStorage::instance()->rollback();
180 $this->setError('ArtifactFile: '.db_error());
184 ArtifactStorage::instance()->commit();
187 // Now set up our internal data structures
189 if (!$this->fetchData($id)) {
193 // If time is set, no need to add to history, will be done in batch
194 if (!array_key_exists('time', $importData)){
195 $this->Artifact->addHistory('File Added',$id.': '.$filename);
197 $this->Artifact->UpdateLastModifiedDate();
204 * delete - delete this artifact file from the db.
206 * @return boolean success.
209 if (!forge_check_perm ('tracker', $this->Artifact->ArtifactType->getID(), 'tech')) {
210 $this->setPermissionDeniedError();
213 $res = db_query_params ('DELETE FROM artifact_file WHERE id=$1',
214 array ($this->getID())) ;
215 if (!$res || db_affected_rows($res) < 1) {
216 $this->setError('ArtifactFile: Unable to Delete');
219 ArtifactStorage::instance()->delete($this->getID())->commit();
221 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
227 * fetchData - re-fetch the data for this ArtifactFile from the database.
229 * @param int The file_id.
230 * @return boolean success.
232 function fetchData($id) {
233 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
235 if (!$res || db_numrows($res) < 1) {
236 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
239 $this->data_array = db_fetch_array($res);
240 db_free_result($res);
245 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
247 * @return object Artifact.
249 function &getArtifact() {
250 return $this->Artifact;
254 * getID - get this ArtifactFile's ID.
256 * @return int The id #.
259 return $this->data_array['id'];
263 * getName - get the filename.
265 * @return string filename.
268 return $this->data_array['filename'];
272 * getType - get the type.
274 * @return string type.
277 return $this->data_array['filetype'];
281 * getData - return the content of the attached file.
283 * @return string content of file.
286 return file_get_contents($this->getFile());
290 * getFile - get the file.
292 * @return string full pathname of file in storage.
295 return ArtifactStorage::instance()->get($this->getID());
299 * getSize - get the size.
304 return $this->data_array['filesize'];
308 * getDescription - get the description.
310 * @return string description.
312 function getDescription() {
313 return $this->data_array['description'];
317 * getDate - get the date file was added.
319 * @return int unix time.
322 return $this->data_array['adddate'];
326 * getSubmittedBy - get the user_id of the submitter.
328 * @return int user_id.
330 function getSubmittedBy() {
331 return $this->data_array['submitted_by'];
335 * getSubmittedRealName - get the real name of the submitter.
337 * @return string name.
339 function getSubmittedRealName() {
340 return $this->data_array['realname'];
344 * getSubmittedUnixName - get the unix name of the submitter.
346 * @return string unixname.
348 function getSubmittedUnixName() {
349 return $this->data_array['user_name'];
356 // c-file-style: "bsd"