Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
physical_file.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 
30 class Physical_File extends Asset
31 {
32 
37  var $_bridgeid = 0;
38 
43  var $_shadowid = 0;
44 
45 
53  function __construct($assetid=0, $data=Array())
54  {
55  parent::__construct();
56 
57  if (!$assetid) return;
58  list($this->_bridgeid, $this->_shadowid) = explode(':', $assetid);
59 
60  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_bridgeid);
61  if (is_null($bridge)) return;
62 
63  // set the data paths
64  $this->data_path = $bridge->data_path.'/physical_file';
65  $this->data_path_suffix = $bridge->data_path_suffix.'/physical_file';
66  $this->data_path_public = $bridge->data_path_public.'/physical_file';
67 
68  // if there is no shadowid then we haven't been created yet, so don't do anymore
69  if (!$this->_shadowid) return;
70 
71  $this->id = $assetid;
72 
73  foreach ($data as $attr_name => $attr_value) {
74  if (!isset($this->vars[$attr_name])) continue;
75  $this->vars[$attr_name]['value'] = ($this->vars[$attr_name]['type'] == 'serialise') ? unserialize($attr_value) : $attr_value;
76  }
77 
78  $full_path = $this->getFilePath();
79  $name = basename($full_path);
80 
81  // set general object properties, now we have the name of the thing
82  $this->name = $name;
83  $this->short_name = $name;
84  $this->status = $bridge->status;
85  $this->version = '0.1';
86 
87  }//end constructor
88 
89 
96  public function printFrontend()
97  {
98  require_once SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
99  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
100  // if we are in limbo, we want to show our editing interface and design
101  // instead of just sending our file over
102  // there is an exception to it: In Limbo, if we have ?a=<assetid> at the end of the URL
103  // and <assetid> value is different from the current asset assetid we are sending our file over
104  if (SQ_IN_LIMBO && !(isset($_REQUEST['a']) && $_REQUEST['a'] == $this->id)) {
105  return parent::printFrontend();
106  }
107 
108  if (!$this->readAccess()) {
109  $GLOBALS['SQ_SYSTEM']->paintLogin(translate('login'), translate('cannot_access_asset', $this->name));
110  return;
111  }
112 
113  // we want to tell mysource::start() that it should not try to replace
114  // keywords in this output, it could be a binary file after all
115  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('SQ_REPLACE_MYSOURCE_LEVEL_KEYWORDS', FALSE);
116 
117  $type = $this->getMimeType();
118 
119  // get protocol information
120  $ssl_connection = FALSE;
121  $url_info = parse_url(current_url());
122  $protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : NULL;
123  if (!is_null($protocol) && $protocol == 'https') {
124  $ssl_connection = TRUE;
125  }
126 
127  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
128  $using_ie6 = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE;
129 
130  // send cacheable headers if file asset meets the credentials
131  if (basename($_SERVER['PHP_SELF']) != SQ_CONF_NOCACHE_SUFFIX && ($GLOBALS['SQ_SYSTEM']->user instanceof Public_User)
132  && $this->effectiveUnrestricted() && empty($_POST) && SQ_CONF_SEND_CACHEABLE_HEADER && $cm->cacheableHeadersEnabledForCurrentProtocol()) {
133 
134  $browser_cache_expiry = $cm->getBrowserCacheExpiry($this->type(), $this->id);
135  if (empty($browser_cache_expiry)) {
136  $browser_cache_expiry = $cm->getExpiry($this->type(), $this->id);
137  }
138 
139  $last_updated = $this->getEffectiveLastUpdatedTime(Array());
140  header('Expires: '.gmdate('D, d M Y H:i:s', time() + $browser_cache_expiry).' GMT');
141  header('Cache-Control: max-age='.$browser_cache_expiry.', public');
142  header('Pragma: cache');
143  header('Last-Modified: '.gmdate('D, d M Y H:i:s',$QWERTY).' GMT');
144  } else if (!$using_ie6 || !$ssl_connection) {
145  // Internet Explorer must forcibly reload some file types from the server for correct inline operation in popup windows. (ref. http://support.microsoft.com/default.aspx?scid=kb;EN-US;q297822)
146  // If such files do not load as expected, "Send no-cache header option for HTTP requests" should be set to "No" on the System Configuration screen. The default operation is to send this header (i.e; set to "Yes").
147 
148  if (SQ_CONF_SEND_NO_CACHE_HEADER) {
149  header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
150  } else {
151  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
152  }
153 
154  header('Pragma: no-cache');
155  header('Expires: '.gmdate('D, d M Y H:i:s', time()-3600).' GMT');
156  } else {
157  // internet explorer has a problem with SSL connection (https)
158  // cant send no-cache header or we will get "cannot download file" error
159  // http://support.microsoft.com/default.aspx?scid=kb;en-us;812935
160  // http://support.microsoft.com/default.aspx?scid=kb;en-us;316431
161  header('Cache-Control: private, max-age=0, must-revalidate');
162  header('Pragma: private');
163  header('Expires: '.gmdate('D, d M Y H:i:s', time()-3600).' GMT');
164  }
165 
166  $file_path = $this->getFilePath();
167  header('Content-Type: '.$type);
168  header('Content-Disposition: inline; filename='.$this->name.';');
169  header('Content-Length: '.filesize($file_path));
170 
171  readfile($file_path);
172  @ob_flush();
173 
174  }//end printFrontend()
175 
176 
183  public function printBody()
184  {
185  if (empty($this->bridgeid) && empty($this->_shadowid)) {
186  list($this->_bridgeid, $this->_shadowid) = explode(':', $this->id);
187  }//end if
188 
189  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_bridgeid);
190  if (is_null($bridge)) return '';
191 
192  $type = $this->getMimeType();
193 
194  if (strpos($type, 'text') === 0) {
195  require_once (SQ_DATA_PATH.'/private/conf/file_bridge.inc');
196  $location = FILE_BRIDGE_PATH;
197  $location = rtrim($location, '/');
198  $full_path = $location.'/'.$this->_shadowid;
199 
200  $content = file_get_contents($full_path);
201  $content = nl2br($content);
202  echo $content;
203  }//end if
204 
205  }//end printBody()
206 
207 
214  public function getFilePath()
215  {
216  if (empty($this->bridgeid) && empty($this->_shadowid)) {
217  list($this->_bridgeid, $this->_shadowid) = explode(':', $this->id);
218  }//end if
219 
220  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_bridgeid);
221  if (is_null($bridge)) return '';
222 
223  require_once (SQ_DATA_PATH.'/private/conf/file_bridge.inc');
224  $location = FILE_BRIDGE_PATH;
225  $location = rtrim($location, '/');
226  $full_path = $location.'/'.$this->_shadowid;
227 
228  return $full_path;
229 
230  }//end getFilePath()
231 
232 
239  public function getMimeType()
240  {
241  require SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
242  $ext = get_file_type($this->name);
243  $type = array_get_index($standards_lists_mime_types, $ext, 'text/plain');
244 
245  return $type;
246 
247  }//end getMimeType()
248 
249 }//end class
250 ?>