Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
HeadersHelper.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Class for helping in work with headers
11  */
13 {
14 
15  /**
16  * Build content headers for file content.
17  *
18  * @param string $path file path
19  * @param string $content file content
20  *
21  * @return array headers
22  */
23  public static function forFile($path, $content = null)
24  {
25  $headers = array();
26  $headers[] = "Content-Type: " . CFileHelper::getMimeTypeByExtension($path);
27  if ($content) {
28  $headers[] = "Content-Length: " . mb_strlen($content);
29  $contentPath = Yii::app()->getModule('contentSource')->getContentSource()->contentFile($path);
30  $headers[] = "Last-Modified: " . gmdate("D, d M Y H:i:s", filemtime($contentPath)) . " GMT";
31  }
32  return $headers;
33  }
34 
35  /**
36  * Build expires headers for file
37  *
38  * @param int $seconds seconds before content expires
39  *
40  * @return array headers
41  */
42  public static function expires($seconds)
43  {
44  $headers = array();
45  $headers[] = "Pragma: public";
46  $headers[] = "Expires: " . date("r", time() + $seconds);
47 
48  return $headers;
49  }
50 
51  /**
52  *
53  * Build cache control header for file
54  *
55  * @param string $filename
56  * @param int $seconds how long the file should be cached in browser
57  * @return array headers
58  */
59  public static function cache($seconds = null)
60  {
61  $headers = array();
62 
63  $cache_string = "Cache-Control: public";
64 
65  if(isset($seconds) && is_numeric($seconds) && $seconds > 0) {
66  $cache_string = "Cache-Control: max-age=".$seconds.", public";
67  }
68 
69  $headers[] = $cache_string;
70 
71  return $headers;
72  }
73 }