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 $artifact_file_id The artifactFile ID
31 * @param array|bool $data The result array, if it's passed in
32 * @return 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 * __construct - ArtifactFile constructor.
74 * @param Artifact $Artifact The Artifact object.
75 * @param array|bool $data (all fields from artifact_file_user_vw) OR id from database.
77 function __construct(&$Artifact, $data=false) {
80 // Was Artifact legit?
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;
96 $this->fetchData($data);
102 * create - create a new item in the database.
104 * @param string $filename Filename of the item.
105 * @param string $filetype filetype.
106 * @param string $filesize filesize.
107 * @param string $file file to store.
108 * @param string $description Description.
109 * @param array $importData Array of data to change submitter and time of submit like:
110 * array('user' => 127, 'time' => 1234556789)
111 * @return int|bool Identifier on success / false on failure.
113 function create($filename, $filetype, $filesize, $file, $description='None', $importData = array()) {
114 // Some browsers don't supply mime type if they don't know it
116 // Let's be on safe side?
117 $filetype = 'application/octet-stream';
123 if (!$filename || !$filetype || !$filesize || !$file) {
124 //echo '<p>|'.$filename.'|'.$filetype.'|'.$filesize.'|'.$file.'|';
125 $this->setError(_('ArtifactFile: File, name, type, size are required'));
129 if (array_key_exists('user', $importData)){
130 $user_id = $importData['user'];
132 if (session_loggedin()) {
133 $user_id=user_getid();
139 if (array_key_exists('time',$importData)){
140 $time = $importData['time'];
146 // If $filetype is "text/plain", $bin_data convert UTF-8 encoding.
147 if (strcasecmp($filetype,"text/plain") === 0 &&
148 function_exists('mb_convert_encoding') &&
149 function_exists('mb_detect_encoding')) {
150 $bin_data = mb_convert_encoding($bin_data,'UTF-8',mb_detect_encoding($bin_data, "auto"));
151 $filesize = strlen($bin_data);
156 $res = db_query_params ('INSERT INTO artifact_file
157 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
158 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
159 array ($this->Artifact->getID(),
168 $id=db_insertid($res,'artifact_file','id');
170 ArtifactStorage::instance()->store($id, $file);
174 ArtifactStorage::instance()->rollback();
175 $this->setError('ArtifactFile: '.db_error());
179 ArtifactStorage::instance()->commit();
182 // Now set up our internal data structures
184 if (!$this->fetchData($id)) {
188 // If time is set, no need to add to history, will be done in batch
189 if (!array_key_exists('time', $importData)){
190 $this->Artifact->addHistory('File Added',$id.': '.$filename);
192 $this->Artifact->UpdateLastModifiedDate();
199 * delete - delete this artifact file from the db.
201 * @return boolean success.
204 if (!forge_check_perm ('tracker', $this->Artifact->ArtifactType->getID(), 'tech')) {
205 $this->setPermissionDeniedError();
208 $res = db_query_params ('DELETE FROM artifact_file WHERE id=$1',
209 array ($this->getID())) ;
210 if (!$res || db_affected_rows($res) < 1) {
211 $this->setError('ArtifactFile: Unable to Delete');
214 ArtifactStorage::instance()->delete($this->getID())->commit();
216 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
222 * fetchData - re-fetch the data for this ArtifactFile from the database.
224 * @param int $id The file_id.
225 * @return boolean success.
227 function fetchData($id) {
228 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
230 if (!$res || db_numrows($res) < 1) {
231 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
234 $this->data_array = db_fetch_array($res);
235 db_free_result($res);
240 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
242 * @return object Artifact.
244 function &getArtifact() {
245 return $this->Artifact;
249 * getID - get this ArtifactFile's ID.
251 * @return int The id #.
254 return $this->data_array['id'];
258 * getName - get the filename.
260 * @return string filename.
263 return $this->data_array['filename'];
267 * getType - get the type.
269 * @return string type.
272 return $this->data_array['filetype'];
276 * getData - return the content of the attached file.
278 * @return string content of file.
281 return file_get_contents($this->getFile());
285 * getFile - get the file.
287 * @return string full pathname of file in storage.
290 return ArtifactStorage::instance()->get($this->getID());
294 * getSize - get the size.
299 return $this->data_array['filesize'];
303 * getDescription - get the description.
305 * @return string description.
307 function getDescription() {
308 return $this->data_array['description'];
312 * getDate - get the date file was added.
314 * @return int unix time.
317 return $this->data_array['adddate'];
321 * getSubmittedBy - get the user_id of the submitter.
323 * @return int user_id.
325 function getSubmittedBy() {
326 return $this->data_array['submitted_by'];
330 * getSubmittedRealName - get the real name of the submitter.
332 * @return string name.
334 function getSubmittedRealName() {
335 return $this->data_array['realname'];
339 * getSubmittedUnixName - get the unix name of the submitter.
341 * @return string unixname.
343 function getSubmittedUnixName() {
344 return $this->data_array['user_name'];
351 // c-file-style: "bsd"