htaccess.php
4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
// namespace administrator\components\com_jmap\models;
/**
* @package JMAP::HTACCESS::administrator::components::com_jmap
* @subpackage models
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined ( '_JEXEC' ) or die ( 'Restricted access' );
jimport('joomla.filesystem.file');
/**
* Htaccess model concrete implementation
*
* @package JMAP::HTACCESS::administrator::components::com_jmap
* @subpackage models
* @since 3.0
*/
class JMapModelHtaccess extends JMapModel {
/**
* Load entity from ORM table
*
* @access public
* @param int $id
* @return Object&
*/
public function loadEntity($id) {
try {
// Load htaccess file, finding it
$targetHtaccess = null;
// Try to check for an active htaccess file
if(JFile::exists(JPATH_ROOT . '/.htaccess')) {
$targetHtaccess = JPATH_ROOT . '/.htaccess';
} elseif (JFile::exists(JPATH_ROOT . '/htaccess.txt')) { // Fallback on txt dummy version
$targetHtaccess = JPATH_ROOT . '/htaccess.txt';
$this->setState('htaccess_version', 'textual');
} else {
throw new JMapException(JText::_('COM_JMAP_HTACCESS_NOTFOUND'), 'error');
}
// htaccess found!
if($targetHtaccess !== false) {
// If file permissions ko
if(!$htaccessContents = JFile::read($targetHtaccess)) {
throw new JMapException(JText::_('COM_JMAP_ERROR_READING_HTACCESS'), 'error');
}
}
} catch(JMapException $e) {
$this->setError($e);
return false;
} catch(Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
return $htaccessContents;
}
/**
* Storing entity by ORM table
*
* @access public
* @return boolean
*/
public function storeEntity($buffer = null) {
try {
// Data posted required, otherwise avoid write anything
if(!$buffer) {
throw new JMapException(JText::_('COM_JMAP_HTACCESS_NO_DATA'), 'error');
}
$targetHtaccess = null;
// Find htaccess file
if(JFile::exists(JPATH_ROOT . '/.htaccess')) {
$targetHtaccess = JPATH_ROOT . '/.htaccess';
} elseif (JFile::exists(JPATH_ROOT . '/htaccess.txt')) { // Fallback on txt dummy version
$targetHtaccess = JPATH_ROOT . '/htaccess.txt';
$this->setState('htaccess_version', 'textual');
} else {
throw new JMapException(JText::_('COM_JMAP_HTACCESS_NOTFOUND'), 'error');
}
// If file permissions ko on rewrite updated contents
$originalPermissions = null;
if(!is_writable($targetHtaccess)) {
$originalPermissions = intval(substr(sprintf('%o', fileperms($targetHtaccess)), -4), 8);
@chmod($targetHtaccess, 0755);
}
if(@!JFile::write($targetHtaccess, $buffer)) {
throw new JMapException(JText::_('COM_JMAP_ERROR_WRITING_HTACCESS'), 'error');
}
// Check if permissions has been changed and recover the original in that case
if($originalPermissions) {
@chmod($targetHtaccess, $originalPermissions);
}
} catch(JMapException $e) {
$this->setError($e);
return false;
} catch(Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
return true;
}
/**
* Change the status of the htaccess from inactive to active
*
* @access public
* @return boolean
*/
public function activateHtaccessEntity() {
try {
$targetHtaccess = null;
// Find htaccess file
if(JFile::exists(JPATH_ROOT . '/.htaccess')) {
throw new JMapException(JText::_('COM_JMAP_HTACCESS_ACTIVE_ALREADYFOUND'), 'error');
} elseif (JFile::exists(JPATH_ROOT . '/htaccess.txt')) { // Fallback on txt dummy version
$targetHtaccess = JPATH_ROOT . '/htaccess.txt';
} else {
throw new JMapException(JText::_('COM_JMAP_HTACCESS_NOTFOUND'), 'error');
}
// If file permissions ko on rewrite updated contents
$originalPermissions = null;
if(!is_writable($targetHtaccess)) {
$originalPermissions = intval(substr(sprintf('%o', fileperms($targetHtaccess)), -4), 8);
@chmod($targetHtaccess, 0755);
}
if(@!rename($targetHtaccess, JPATH_ROOT . '/.htaccess')) {
throw new JMapException(JText::_('COM_JMAP_ERROR_RENAMING_HTACCESS'), 'error');
}
// Check if permissions has been changed and recover the original in that case
if($originalPermissions) {
@chmod($targetHtaccess, $originalPermissions);
}
} catch(JMapException $e) {
$this->setError($e);
return false;
} catch(Exception $e) {
$jmapException = new JMapException($e->getMessage(), 'error');
$this->setError($jmapException);
return false;
}
return true;
}
}