| | 1 | <?php |
|---|
| | 2 | /** |
|---|
| | 3 | * @package jelix |
|---|
| | 4 | * @subpackage coord_plugin |
|---|
| | 5 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
|---|
| | 6 | * @copyright 2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau, 2007 Frédéric Guillot, 2007 Antoine Detante |
|---|
| | 7 | * @copyright 2008 Hadrien.eu |
|---|
| | 8 | * |
|---|
| | 9 | * This class was get originally from an experimental branch of the Copix project |
|---|
| | 10 | * (PluginAuth, Copix 2.3dev20050901, http://www.copix.org) |
|---|
| | 11 | * Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence). |
|---|
| | 12 | * Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau, |
|---|
| | 13 | * and this class was adapted for Jelix by Laurent Jouanneau |
|---|
| | 14 | * |
|---|
| | 15 | * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file |
|---|
| | 16 | */ |
|---|
| | 17 | class CacheCoordPlugin implements jICoordPlugin |
|---|
| | 18 | { |
|---|
| | 19 | /** |
|---|
| | 20 | * Activate cache |
|---|
| | 21 | * |
|---|
| | 22 | * @var boolean |
|---|
| | 23 | **/ |
|---|
| | 24 | public $activated = false; |
|---|
| | 25 | /** |
|---|
| | 26 | * time expiration (in milliseconds) |
|---|
| | 27 | * |
|---|
| | 28 | * @var Integer |
|---|
| | 29 | **/ |
|---|
| | 30 | private $_expires; |
|---|
| | 31 | |
|---|
| | 32 | /** |
|---|
| | 33 | * Cache filename |
|---|
| | 34 | * |
|---|
| | 35 | * @var string |
|---|
| | 36 | **/ |
|---|
| | 37 | private $_fileKey; |
|---|
| | 38 | |
|---|
| | 39 | /** |
|---|
| | 40 | * Cache folder path |
|---|
| | 41 | * |
|---|
| | 42 | * @var string |
|---|
| | 43 | **/ |
|---|
| | 44 | public $path; |
|---|
| | 45 | |
|---|
| | 46 | /** |
|---|
| | 47 | * path into cache folder |
|---|
| | 48 | * |
|---|
| | 49 | * @var string |
|---|
| | 50 | **/ |
|---|
| | 51 | private $_localPath; |
|---|
| | 52 | |
|---|
| | 53 | |
|---|
| | 54 | public function __construct($config) |
|---|
| | 55 | { |
|---|
| | 56 | if (isset($config['cache']) and |
|---|
| | 57 | $config['cache'] == 1) |
|---|
| | 58 | { |
|---|
| | 59 | $this->activated = true; |
|---|
| | 60 | |
|---|
| | 61 | if (isset($config['path'])) |
|---|
| | 62 | { |
|---|
| | 63 | $this->path = JELIX_APP_TEMP_PATH . |
|---|
| | 64 | $config['path'] . |
|---|
| | 65 | DIRECTORY_SEPARATOR; |
|---|
| | 66 | } |
|---|
| | 67 | else |
|---|
| | 68 | { |
|---|
| | 69 | $this->path = JELIX_APP_TEMP_PATH . 'cache/'; |
|---|
| | 70 | } |
|---|
| | 71 | } |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | /** |
|---|
| | 75 | * Get request params and check file cached |
|---|
| | 76 | * @param {array} $params Params array |
|---|
| | 77 | * @return void |
|---|
| | 78 | **/ |
|---|
| | 79 | public function beforeAction($params) |
|---|
| | 80 | { |
|---|
| | 81 | // Check if cache is activated and if this request ask for cache |
|---|
| | 82 | if ($this->activated and |
|---|
| | 83 | isset($params['cache.expires'])) |
|---|
| | 84 | { |
|---|
| | 85 | global $gJCoord; |
|---|
| | 86 | // create a path from module/action |
|---|
| | 87 | $this->_localPath .= |
|---|
| | 88 | $gJCoord->moduleName . '/' . |
|---|
| | 89 | str_replace( |
|---|
| | 90 | ':', |
|---|
| | 91 | '_', |
|---|
| | 92 | $gJCoord->actionName |
|---|
| | 93 | ) . '/'; |
|---|
| | 94 | |
|---|
| | 95 | jFile::createDir( |
|---|
| | 96 | $this->path . |
|---|
| | 97 | $this->_localPath |
|---|
| | 98 | ); |
|---|
| | 99 | |
|---|
| | 100 | // get md5 key from get params |
|---|
| | 101 | $this->_fileKey = md5( |
|---|
| | 102 | serialize( |
|---|
| | 103 | $gJCoord->request->params |
|---|
| | 104 | ) |
|---|
| | 105 | ); |
|---|
| | 106 | |
|---|
| | 107 | // Test file time |
|---|
| | 108 | $this->_expires = $params['cache.expires']; |
|---|
| | 109 | if (@filemtime( |
|---|
| | 110 | $this->path . |
|---|
| | 111 | $this->_localPath . |
|---|
| | 112 | $this->_fileKey |
|---|
| | 113 | ) < time() - $this->_expires) |
|---|
| | 114 | { |
|---|
| | 115 | // Start cache |
|---|
| | 116 | ob_start(); |
|---|
| | 117 | } |
|---|
| | 118 | else |
|---|
| | 119 | { |
|---|
| | 120 | // Include cached file |
|---|
| | 121 | include( |
|---|
| | 122 | $this->path . |
|---|
| | 123 | $this->_localPath . |
|---|
| | 124 | $this->_fileKey |
|---|
| | 125 | ); |
|---|
| | 126 | exit(); |
|---|
| | 127 | } |
|---|
| | 128 | } |
|---|
| | 129 | } |
|---|
| | 130 | public function beforeOutput() |
|---|
| | 131 | { |
|---|
| | 132 | |
|---|
| | 133 | } |
|---|
| | 134 | public function afterProcess() |
|---|
| | 135 | { |
|---|
| | 136 | if ($this->activated and |
|---|
| | 137 | $this->_expires) |
|---|
| | 138 | { |
|---|
| | 139 | // end cache |
|---|
| | 140 | $cache = ob_get_contents(); |
|---|
| | 141 | ob_end_flush(); |
|---|
| | 142 | |
|---|
| | 143 | // Write in file |
|---|
| | 144 | jFile::write( |
|---|
| | 145 | $this->path . |
|---|
| | 146 | $this->_localPath . |
|---|
| | 147 | $this->_fileKey, |
|---|
| | 148 | $cache |
|---|
| | 149 | ); |
|---|
| | 150 | } |
|---|
| | 151 | } |
|---|
| | 152 | } |