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.'include/Error.class.php';
27 * Factory method which creates an ArtifactFile from an artifactFile ID
29 * @param int The artifactFile ID
30 * @param array The result array, if it's passed in
31 * @return object Artifact object
33 function &artifactfile_get_object($artifact_file_id,$data=false) {
34 global $ARTIFACTFILE_OBJ;
35 if (!isset($ARTIFACTFILE_OBJ["_".$artifact_file_id."_"])) {
37 //the db result handle was passed in
39 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
40 array ($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 * @param array Array of data to change submitter and time of submit like: array('user' => 127, 'time' => 1234556789)
115 * @return id on success / false on failure.
117 function create($filename, $filetype, $filesize, $bin_data, $description='None', $importData = array()) {
118 // Some browsers don't supply mime type if they don't know it
120 // Let's be on safe side?
121 $filetype = 'application/octet-stream';
127 if (!$filename || !$filetype || !$filesize || !$bin_data) {
128 //echo '<p>|'.$filename.'|'.$filetype.'|'.$filesize.'|'.$bin_data.'|';
129 $this->setError(_('ArtifactFile: File name, type, size, and data are required'));
133 if (array_key_exists('user', $importData)){
134 $userid = $importData['user'];
136 if (session_loggedin()) {
137 $userid=user_getid();
143 if (array_key_exists('time',$importData)){
144 $time = $importData['time'];
150 // If $filetype is "text/plain", $bin_data convert UTF-8 encoding.
151 if (strcasecmp($filetype,"text/plain") === 0 &&
152 function_exists('mb_convert_encoding') &&
153 function_exists('mb_detect_encoding')) {
154 $bin_data = mb_convert_encoding($bin_data,'UTF-8',mb_detect_encoding($bin_data, "auto"));
155 $filesize = strlen($bin_data);
160 $res = db_query_params ('INSERT INTO artifact_file
161 (artifact_id,description,bin_data,filename,filesize,filetype,adddate,submitted_by)
162 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
163 array ($this->Artifact->getID(),
165 base64_encode($bin_data),
172 $id=db_insertid($res,'artifact_file','id');
176 $this->setError('ArtifactFile: '.db_error());
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 $this->Artifact->addHistory('File Deleted',$this->getID().': '.$this->getName());
220 * fetchData - re-fetch the data for this ArtifactFile from the database.
222 * @param int The file_id.
223 * @return boolean success.
225 function fetchData($id) {
226 $res = db_query_params ('SELECT * FROM artifact_file_user_vw WHERE id=$1',
228 if (!$res || db_numrows($res) < 1) {
229 $this->setError('ArtifactFile: Invalid ArtifactFile ID');
232 $this->data_array = db_fetch_array($res);
233 db_free_result($res);
238 * getArtifact - get the Artifact Object this ArtifactFile is associated with.
240 * @return object Artifact.
242 function &getArtifact() {
243 return $this->Artifact;
247 * getID - get this ArtifactFile's ID.
249 * @return int The id #.
252 return $this->data_array['id'];
256 * getName - get the filename.
258 * @return string filename.
261 return $this->data_array['filename'];
265 * getType - get the type.
267 * @return string type.
270 return $this->data_array['filetype'];
274 * getData - get the binary data from the db.
279 return base64_decode($this->data_array['bin_data']);
283 * getSize - get the size.
288 return $this->data_array['filesize'];
292 * getDescription - get the description.
294 * @return string description.
296 function getDescription() {
297 return $this->data_array['description'];
301 * getDate - get the date file was added.
303 * @return int unix time.
306 return $this->data_array['adddate'];
310 * getSubmittedBy - get the user_id of the submitter.
312 * @return int user_id.
314 function getSubmittedBy() {
315 return $this->data_array['submitted_by'];
319 * getSubmittedRealName - get the real name of the submitter.
321 * @return string name.
323 function getSubmittedRealName() {
324 return $this->data_array['realname'];
328 * getSubmittedUnixName - get the unix name of the submitter.
330 * @return string unixname.
332 function getSubmittedUnixName() {
333 return $this->data_array['user_name'];
340 // c-file-style: "bsd"