1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * This is the model class for table "x2_temp_files".
39: *
40: * @package application.models
41: * @property integer $id
42: * @property string $folder
43: * @property string $name
44: * @property integer createDate
45: */
46: class TempFile extends CActiveRecord {
47: /**
48: * Returns the static model of the specified AR class.
49: * @return Media the static model class
50: */
51: public static function model($className=__CLASS__) {
52: return parent::model($className);
53: }
54:
55: /**
56: * @return string the associated database table name
57: */
58: public function tableName() {
59: return 'x2_temp_files';
60: }
61:
62: /**
63: * @return array validation rules for model attributes.
64: */
65: public function rules() {
66: // NOTE: you should only define rules for those attributes that
67: // will receive user inputs.
68: return array(
69: );
70: }
71:
72: public function behaviors() {
73: return array(
74: );
75: }
76:
77: /**
78: * @return array relational rules.
79: */
80: public function relations() {
81: // NOTE: you may need to adjust the relation name and the related
82: // class name for the relations automatically generated below.
83: return array(
84: );
85: }
86:
87: /**
88: * @return array customized attribute labels (name=>label)
89: */
90: public function attributeLabels() {
91: return array(
92: );
93: }
94:
95: /*
96: * Create a temp folder to save a temp file in.
97: * Create an entry in x2_temp_files to track the file
98: * Delete any old temp files
99: *
100: * return TempFile
101: * or false if failed to create temp folder
102: */
103: public static function createTempFile($name) {
104:
105: // delete old temp files if they exist
106: $old = time() - (86400); // 1 day old
107: $oldTempFiles = TempFile::model()->findAll("createDate < $old");
108: foreach($oldTempFiles as $oldTempFile) {
109: $oldFolder = $oldTempFile->folder;
110: $oldName = $oldTempFile->name;
111: if(file_exists('uploads/protected/media/temp/'. $oldFolder .'/'. $oldName))
112: unlink('uploads/protected/media/temp/'. $oldFolder .'/'. $oldName); // delete file
113: if(file_exists('uploads/protected/media/temp/'. $oldFolder))
114: rmdir('uploads/protected/media/temp/'. $oldFolder); // delete folder
115: $oldTempFile->delete(); // delete database entry tracking temp file
116: }
117:
118: // generate temp folder name
119: $folder = substr(md5(rand()), 0, 10);
120:
121: // try to create temp folder
122: if(!@mkdir('uploads/protected/media/temp/'. $folder, 0777, true))
123: return false; // couldn't create temp folder
124:
125: $tempFile = new TempFile; // track temp file in database
126: $tempFile->folder = $folder;
127: $tempFile->name = $name;
128: $tempFile->createDate = time();
129: if($tempFile->save())
130: return $tempFile; // TempFile
131: else
132: return false;
133: }
134:
135:
136: /*
137: * Get the full path including the file name
138: */
139: public function fullpath() {
140: return 'uploads/protected/media/temp/'. $this->folder .'/'. $this->name;
141: }
142:
143: public function convertToMedia (array $attributes=array ()) {
144: $username = Yii::app()->user->getName();
145: $name = $this->name;
146: $tempFilename = 'uploads/protected/media/temp/'.$this->folder.'/'.$this->name;
147: if (FileUtil::ccopy($tempFilename, "uploads/protected/media/$username/$name")) {
148: $model = new Media;
149: $model->name = $model->fileName = $name;
150: $model->uploadedBy = $username;
151: $model->createDate = time ();
152: $model->lastUpdated = time ();
153: $model->resolveType ();
154: $model->resolveSize ();
155: $model->setAttributes ($attributes, false);
156: if ($model->save ()) return $model;
157: }
158: return false;
159: }
160: }
161: