Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
text_file.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/files/file/file.inc';
18 
27 class Text_File extends File
28 {
29 
30 
37  function __construct($assetid=0)
38  {
39  parent::__construct($assetid);
40 
41  }//end constructor
42 
43 
54  public function create(Array &$link, $info=Array())
55  {
56  $this->_tmp['file_create_data'] =& $info;
57 
58  if (empty($info)) {
59  // Try getting the uploaded file details
60  $info = get_file_upload_info($this->getPrefix());
61  }
62 
63  if ($info === FALSE || empty($info)) {
64  // See if they've chosen a pre-uploaded file
65  $edit = $this->getEditFns();
66  $info = $edit->getChosenFileInfo($this->getPrefix());
67  }
68 
69  $file_name = '';
70  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_DATA_VALIDATION)) {
71 
72  // When no file is supplied via uploaded, create a new file
73  if ($info === FALSE || empty($info)) {
74 
75  $file_title = trim($this->attr('title'));
76  $file_title = preg_replace('/\s/', '_', $file_title);
77  $file_title = preg_replace('/[^A-Za-z0-9_\-]/', '', $file_title);
78 
79  $extension = 'txt';
80  $file_name = $file_title . '.' . $extension;
81 
82  $create_location = SQ_SYSTEM_ROOT.'/'.'data/temp';
83  // Create an temporary empty file
84  if (!fopen($create_location.'/'.$file_name, 'w+')) {
85  trigger_localised_error('SYS0025', E_USER_WARNING, $file_name. " from given title ".$file_title);
86  return FALSE;
87  }
88 
89  // Create info array for the file just created
90 
91  $info = Array();
92 
93  $info['name'] = $file_name;
94  $info['tmp_name'] = $create_location;
95  $info['created_file'] = TRUE;
96  }
97  }
98 
99  // Make the info array look like a result from getExistingFile()
100  if (!isset($info['path'])) {
101  $info['path'] = array_get_index($info, 'tmp_name', '');
102  }
103  if (!isset($info['filename'])) {
104  $info['filename'] = array_get_index($info, 'name', '');
105  }
106 
107  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_DATA_VALIDATION)) {
108  if (!$this->validFile($info)) return FALSE;
109  }
110 
111  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_DATA_VALIDATION)) {
112  // Check that we are not going to have web path conflicts
113  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
114  $valid_names = make_valid_web_paths(Array($info['name']));
115  $name = array_shift($valid_names);
116 
117  // Make grep sure the new web path is not already is use
118  $bad_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($link['asset'], Array($name));
119  if (!empty($bad_paths)) {
120  trigger_localised_error('CORE0086', E_USER_WARNING, $name);
121  return FALSE;
122  }
123 
124  $this->setAttrValue('name', $name);
125  }//end if
126 
127  return parent::create($link, $info);
128 
129  }//end create()
130 
131 
138  function getContent()
139  {
140  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
141  $parse_file = $this->data_path.'/'.$this->name;
142  $content = file_to_string($parse_file);
143 
144  return trim($content);
145 
146  }//end getContent()
147 
148 
149 }//end class
150 ?>