OssnFile.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 class OssnFile extends OssnEntities {
12  /**
13  * DeleteDir
14  * Delete the directories including files
15  *
16  * @param string $path path of directory
17  *
18  * @return boolean
19  */
20  public static function DeleteDir($path) {
21  if(is_dir($path)) {
22  $files = array_diff(scandir($path), array(
23  '.',
24  '..'
25  ));
26  foreach($files as $file) {
27  if(is_dir("{$path}/{$file}")) {
28  self::DeleteDir("{$path}/{$file}");
29  } else {
30  unlink("{$path}/{$file}");
31  }
32  }
33  }
34  return rmdir($path);
35  }
36 
37  /**
38  * MaxSize
39  * Get server post max size
40  *
41  * @return float;
42  */
43  public function MaxSize() {
44  $val = ini_get('post_max_size');
45  $val = trim($val);
46  $last = strtolower($val[strlen($val) - 1]);
47  switch($last) {
48  case 'g':
49  $val *= 1024;
50  case 'm':
51  $val *= 1024;
52  case 'k':
53  $val *= 1024;
54  }
55  return $val;
56  }
57 
58  /**
59  * setFile
60  * Set a required file in memory
61  *
62  * @param string $name
63  * @return void
64  */
65  public function setFile($name) {
66  $this->showFileUploadError();
67  if(isset($_FILES[$name]['type'])) {
68  $file = $_FILES[$name];
69  $this->file = $file;
70  }
71  }
72 
73  /**
74  * Set a path for file where it need to upload
75  *
76  * @param string $path Path where file need to save
77  * @return void
78  */
79  public function setPath($path) {
80  $this->path = $path;
81  }
82  /**
83  * Get file extension from its name
84  *
85  * @param string $file Full file name
86  * @return string|false
87  */
88  public function getFileExtension($file) {
89  if(isset($file)) {
90  $extension = pathinfo($file, PATHINFO_EXTENSION);
91  if($extension) {
92  return $extension;
93  }
94  }
95  return false;
96  }
97  /**
98  * Allowed file extensions
99  * Validate file extension before save
100  *
101  * @return boolean
102  */
103  public function allowedFileExtensions() {
104  $types = array(
105  'zip',
106  'doc',
107  'docx',
108  'mp4',
109  'mp3',
110  'pdf',
111  'zip',
112  'jpg',
113  'png',
114  'gif',
115  'jpeg'
116  );
117  return ossn_call_hook('file', 'allowed:extensions', null, $types);
118  }
119  /**
120  * getFileUploadError
121  * Print user friendly file upload error
122  *
123  * @param integer $code Error code
124  *
125  * @return string
126  */
127  public function getFileUploadError($code) {
128  switch($code) {
129  case UPLOAD_ERR_OK:
130  return '';
131 
132  case UPLOAD_ERR_INI_SIZE:
133  $key = 'ini_size';
134  break;
135 
136  case UPLOAD_ERR_FORM_SIZE:
137  $key = 'form_size';
138  break;
139 
140  case UPLOAD_ERR_PARTIAL:
141  $key = 'partial';
142  break;
143 
144  case UPLOAD_ERR_NO_FILE:
145  $key = 'no_file';
146  break;
147 
148  case UPLOAD_ERR_NO_TMP_DIR:
149  $key = 'no_tmp_dir';
150  break;
151 
152  case UPLOAD_ERR_CANT_WRITE:
153  $key = 'cant_write';
154  break;
155 
156  case UPLOAD_ERR_EXTENSION:
157  $key = 'extension';
158  break;
159 
160  default:
161  $key = 'unknown';
162  break;
163  }
164  return ossn_print("upload:file:error:$key");
165  }
166  /**
167  * showFileUploadError
168  * Show file upload errors
169  *
170  * @return void
171  */
172  public function showFileUploadError() {
173  if(empty($this->redirect)) {
174  $this->redirect = REF;
175  }
176  if(isset($this->file) && $this->file['error'] !== UPLOAD_ERR_OK) {
177  ossn_trigger_message($this->getFileUploadError($this->file['error']), 'error');
178  redirect($this->redirect);
179  }
180 
181  }
182  /**
183  * addFile
184  * Add file to database
185  *
186  * @param integer $object->owner_guid Guid of owner , the file belongs to
187  * @param string $object->type Owner type,
188  * @param string $object->subtype file type
189  *
190  * @return boolean
191  */
192  public function addFile() {
193  if(isset($this->file) && !empty($this->file) && !empty($this->owner_guid) && !empty($this->subtype) && !empty($this->type)) {
194 
195  $this->extensions = $this->allowedFileExtensions();
196  $this->extension = $this->getFileExtension($this->file['name']);
197  //change user file extension to lower case #153
198  $this->extension = strtolower($this->extension);
199 
200  if($this->typeAllowed()) {
201 
202  $this->newfilename = md5($this->file['name'] . rand() . time()) . '.' . $this->extension;
203  $this->newfile = $this->path . $this->newfilename;
204 
205  $this->dir = ossn_get_userdata("{$this->type}/{$this->owner_guid}/{$this->path}");
206  if(!is_dir(ossn_get_userdata($this->dir))) {
207  mkdir($this->dir, 0755, true);
208  }
209 
210  $this->subtype = "file:{$this->subtype}";
211  $this->value = $this->newfile;
212 
213  if($this->add()) {
214  $filecontents = file_get_contents($this->file['tmp_name']);
215  if(preg_match('/image/i', $this->file['type'])) {
216  //allow devs to change default size , see #528
217  $image_res = array(
218  'width' => 2048,
219  'height' => 2048
220  );
221  $image_res = ossn_call_hook('file', 'image:resolution', $this, $image_res);
222 
223  //compress image before save
224  $filecontents = ossn_resize_image($this->file['tmp_name'], $image_res['width'], $image_res['height']);
225  }
226  file_put_contents("{$this->dir}{$this->newfilename}", $filecontents);
227  return true;
228  }
229  }
230  }
231  return false;
232  }
233  /**
234  * getFiles
235  * Get owner files
236  *
237  * @param integer $object->owner_guid Guid of owner , the file belongs to
238  * @param string $object->type Owner type
239  * @param string $object->subtype File type
240  *
241  * @return object
242  */
243  public function getFiles() {
244  if(!empty($this->type) && !empty($this->owner_guid) && !empty($this->subtype)) {
245  $this->filetype = "file:{$this->subtype}";
246  $this->subtype = preg_replace('/file:file:/i', 'file:', $this->filetype);
247  $this->order_by = 'guid DESC';
248  return arrayObject($this->get_entities(), get_class($this));
249  }
250  }
251 
252  /**
253  * Get file by id
254  *
255  * @param integer $object->file_id guid of file in database
256  * @param string $object->type Owner type
257  * @param string $object->subtype file type
258  *
259  * @return object
260  */
261  public function fetchFile() {
262  $this->entity_guid = $this->file_id;
263  $file = $this->get_entity();
264  return $file;
265  }
266  /**
267  * Set required file extension
268  *
269  * @param array $extension Uploaded file extension can be jpg, jpeg
270  *
271  * @return void
272  */
273  public function setExtension(array $extension = array()) {
274  $this->fileExtension = $extension;
275  }
276  /**
277  * Set required file mimetype
278  * If not set, it will try to match mimetype with pre-defined mime type
279  *
280  * @param array $mimtypes Mimetypes that are allowed
281  *
282  * @return void
283  */
284  public function setMimeTypes(array $mimtypes = array()) {
285  $this->fileMimeTypes = $mimtypes;
286  }
287  /**
288  * Validate a uploaded file
289  *
290  * Make sure the file extension match also check mimetype
291  *
292  * @return boolean
293  */
294  public function typeAllowed() {
295  if(!empty($this->file) && !empty($this->fileExtension)) {
296 
297  $this->extensions = $this->allowedFileExtensions();
298  $this->extension = $this->getFileExtension($this->file['name']);
299  $this->extension = strtolower($this->extension);
300  if(in_array($this->extension, $this->fileExtension)) {
301  $mimetypes = $this->mimeTypes();
302  if(!empty($this->fileMimeTypes) && is_array($this->fileMimeTypes)) {
303  $mimetypes = array();
304  $mimetypes[$this->extension] = $this->fileMimeTypes;
305  }
306  if(isset($mimetypes[$this->extension]) && in_array($this->file['type'], $mimetypes[$this->extension])) {
307  return true;
308  }
309  }
310  }
311  return false;
312  }
313  /**
314  * moveFiles
315  * Move files from one directory to another
316  *
317  * @param string $from Complete directory path from where you want to move files.
318  * @param string $to Complete directory path where you want to move files.
319  *
320  * @return boolean
321  */
322  public static function moveFiles($from, $to) {
323  if(!is_dir($from)) {
324  return false;
325  }
326  if(!is_dir($to)) {
327  mkdir($to, 0755, true);
328  }
329  if(!is_dir($to)) {
330  return false;
331  }
332  $files = scandir($from);
333  foreach($files as $fname) {
334  if($fname != '.' && $fname != '..') {
335  rename($from . $fname, $to . $fname);
336  }
337  }
338  self::DeleteDir($from);
339  return true;
340  }
341  /**
342  * MIME types.
343  *
344  * This function contains most commonly used MIME types in Ossn
345  *
346  * You can find mimtypes on the url below:
347  * http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
348  *
349  * Extra mimetypes has been removed in Ossn v3.1. You can add a hook to extends mimetypes
350  *
351  * @return array
352  */
353  public static function mimeTypes() {
354  $mimetypes = array(
355  'doc' => array(
356  'application/msword'
357  ),
358  'docx' => array(
359  'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
360  ),
361  'gif' => array(
362  'image/gif'
363  ),
364  'jpeg' => array(
365  'image/jpeg'
366  ),
367  'jpg' => array(
368  'image/jpeg'
369  ),
370  'mp3' => array(
371  'audio/mpeg'
372  ),
373  'mp4' => array(
374  'video/mp4'
375  ),
376  'pdf' => array(
377  'application/pdf'
378  ),
379  'png' => array(
380  'image/png'
381  ),
382  'zip' => array(
383  'application/zip'
384  )
385 
386  );
387  return ossn_call_hook('file', 'mimetypes', false, $mimetypes);
388  }
389 } //class