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 * __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 of the item.
105 * @param string Item filetype.
106 * @param string Item filesize.
107 * @param binary file to store.
108 * @param string Item description.
109 * @param array Array of data to change submitter and time of submit like: array('user' => 127, 'time' => 1234556789)
110 * @return id on success / false on failure.
112 function create($filename, $filetype, $filesize, $file, $description='None', $importData = array()) {
113 // Some browsers don't supply mime type if they don't know it
115 // Let's be on safe side?
116 $filetype = 'application/octet-stream';
122 if (!$filename || !$filetype || !$filesize || !$file) {
123 //echo '<p>|'.$filename.'|'.$filetype.'|'.$filesize.'|'.$file.'|';
124 $this->setError(_('ArtifactFile: File, name, type, size are required'));
128 if (array_key_exists('user', $importData)){
129 $userid = $importData['user'];
131 if (session_loggedin()) {
132 $userid=user_getid();
138 if (array_key_exists('time',$importData)){
139 $time = $importData['time'];
145 // If $filetype is "text/plain", $bin_data convert UTF-8 encoding.
146 if (strcasecmp($filetype,"text/plain") === 0 &&
147 function_exists('mb_convert_encoding') &&
148 function_exists('mb_detect_encoding')) {
149 $bin_data = mb_convert_encoding($bin_data,'UTF-8',mb_detect_encoding($bin_data, "auto"));
150 $filesize = strlen($bin_data);
155 $res = db_query_params ('INSERT INTO artifact_file
156 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
157 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
158 array ($this->Artifact->getID(),
167 $id=db_insertid($res,'artifact_file','id');
169 ArtifactStorage::instance()->store($id, $file);
173 ArtifactStorage::instance()->rollback();
174 $this->setError('ArtifactFile: '.db_error());
178 ArtifactStorage::instance()->commit();
181 // Now set up our internal data structures
183 if (!$this->fetchData($id)) {
187 // If time is set, no need to add to history, will be done in batch
188 if (!array_key_exists('time', $importData)){
189 $this->Artifact->addHistory('File Added',$id.': '.$filename);
191 $this->Artifact->UpdateLastModifiedDate();
198 * delete - delete this artifact file from the db.
200 * @return boolean success.
203 if (!forge_check_perm ('tracker', $this->Artifact->ArtifactType->getID(), 'tech')) {
204 $this->setPermissionDeniedError();
207 $res = db_query_params ('DELETE FROM artifact_file WHERE id=$1',
208 array ($this->getID())) ;
209 if (!$res || db_affected_rows($res) < 1) {
210 $this->setError('ArtifactFile: Unable to Delete');
213 ArtifactStorage::instance()->delete($this->getID())->commit();
215 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
221 * fetchData - re-fetch the data for this ArtifactFile from the database.
223 * @param int The file_id.
224 * @return boolean success.
226 function fetchData($id) {
227 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
229 if (!$res || db_numrows($res) < 1) {
230 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
233 $this->data_array = db_fetch_array($res);
234 db_free_result($res);
239 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
241 * @return object Artifact.
243 function &getArtifact() {
244 return $this->Artifact;
248 * getID - get this ArtifactFile's ID.
250 * @return int The id #.
253 return $this->data_array['id'];
257 * getName - get the filename.
259 * @return string filename.
262 return $this->data_array['filename'];
266 * getType - get the type.
268 * @return string type.
271 return $this->data_array['filetype'];
275 * getData - return the content of the attached file.
277 * @return string content of file.
280 return file_get_contents($this->getFile());
284 * getFile - get the file.
286 * @return string full pathname of file in storage.
289 return ArtifactStorage::instance()->get($this->getID());
293 * getSize - get the size.
298 return $this->data_array['filesize'];
302 * getDescription - get the description.
304 * @return string description.
306 function getDescription() {
307 return $this->data_array['description'];
311 * getDate - get the date file was added.
313 * @return int unix time.
316 return $this->data_array['adddate'];
320 * getSubmittedBy - get the user_id of the submitter.
322 * @return int user_id.
324 function getSubmittedBy() {
325 return $this->data_array['submitted_by'];
329 * getSubmittedRealName - get the real name of the submitter.
331 * @return string name.
333 function getSubmittedRealName() {
334 return $this->data_array['realname'];
338 * getSubmittedUnixName - get the unix name of the submitter.
340 * @return string unixname.
342 function getSubmittedUnixName() {
343 return $this->data_array['user_name'];
350 // c-file-style: "bsd"