PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/icwp-feature-master.php

https://github.com/stackgrinder/wp-simple-firewall
PHP | 337 lines | 217 code | 45 blank | 75 comment | 46 complexity | 8725f1eeda9fa2f8ea6e5f1cbe022c92 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2014 iControlWP <support@icontrolwp.com>
  4. * All rights reserved.
  5. *
  6. * This is
  7. * distributed under the GNU General Public License, Version 2,
  8. * June 1991. Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin
  9. * St, Fifth Floor, Boston, MA 02110, USA
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. require_once( dirname(__FILE__).'/icwp-pure-base.php' );
  23. if ( !class_exists('ICWP_Feature_Master') ):
  24. class ICWP_Feature_Master extends ICWP_Pure_Base_V5 {
  25. /**
  26. *@var array
  27. */
  28. protected $aFeatures;
  29. /**
  30. *@var array
  31. */
  32. protected $m_aOptionsHandlers;
  33. /**
  34. * @var ICWP_OptionsHandler_Wpsf
  35. */
  36. protected $m_oPluginMainOptions;
  37. protected $fHasFtpOverride = false;
  38. public function __construct( ICWP_Wordpress_Simple_Firewall_Plugin $oPluginVo, $aFeatures, $inaOptions ) {
  39. parent::__construct( $oPluginVo );
  40. $this->aFeatures = $aFeatures;
  41. $this->m_aOptionsHandlers = $inaOptions;
  42. }
  43. /**
  44. * Based on the existence of files placed within the plugin directory, will enable or disable
  45. * all registered features and return the value of the override setting that was put in place.
  46. *
  47. * @return string - override settings (empty string if none).
  48. */
  49. protected function override() {
  50. if ( $this->m_oWpFs->exists( path_join($this->sPluginRootDir, 'forceOff') ) ) {
  51. $fHasFtpOverride = true;
  52. $sSetting = 'N';
  53. }
  54. else if ( $this->m_oWpFs->exists( path_join($this->sPluginRootDir, 'forceOn') ) ) {
  55. $fHasFtpOverride = true;
  56. $sSetting = 'Y';
  57. }
  58. else {
  59. $sSetting = '';
  60. }
  61. if ( $sSetting == '' ) {
  62. return $sSetting;
  63. }
  64. $aFeatures = $this->getFeaturesMap();
  65. foreach( $aFeatures as $sFeature => $sName ) {
  66. $this->setSharedOption( 'enable_'.$sFeature, $sSetting );
  67. }
  68. return $sSetting;
  69. }
  70. /**
  71. * @return array
  72. */
  73. protected function getFeaturesMap() {
  74. return $this->aFeatures;
  75. }
  76. /**
  77. * Given a certain feature 'slug' will return true if this is a particular supported feature of this plugin.
  78. *
  79. * @param string $insFeature
  80. * @return boolean
  81. */
  82. public function getIsFeature( $insFeature ) {
  83. return array_key_exists( $insFeature, $this->getFeaturesMap() ) || in_array( $insFeature, $this->getFeaturesMap() );
  84. }
  85. /**
  86. * @param string $insFeature - firewall, login_protect, comments_filter, lockdown
  87. * @return boolean
  88. */
  89. public function getIsMainFeatureEnabled( $insFeature ) {
  90. if ( $this->m_oWpFs->exists( $this->sPluginRootDir . 'forceOff' ) ) {
  91. return false;
  92. }
  93. else if ( $this->m_oWpFs->exists( $this->sPluginRootDir . 'forceOn' ) ) {
  94. return true;
  95. }
  96. $aFeatures = $this->getFeaturesMap();
  97. if ( array_key_exists( $insFeature, $aFeatures ) ) {
  98. $fEnabled = $this->m_oPluginMainOptions->getOpt( 'enable_'.$insFeature ) == 'Y';
  99. }
  100. else {
  101. $fEnabled = false;
  102. }
  103. return $fEnabled;
  104. }
  105. /**
  106. * This is necessary because we store these values in several places and we need to always keep it in sync.
  107. *
  108. * @param string $insOption
  109. * @param mixed $inmValue
  110. * @return boolean
  111. */
  112. public function setSharedOption( $insOption, $inmValue ) {
  113. $aFeatures = $this->getFeaturesMap();
  114. $sFeature = str_replace( 'enable_', '', $insOption );
  115. if ( !array_key_exists( $sFeature, $aFeatures ) ) {
  116. return;
  117. }
  118. $this->loadOptionsHandler( $aFeatures[$sFeature] );
  119. $sOptions = 'm_o'.$aFeatures[$sFeature].'Options';// e.g. m_oFirewallOptions
  120. $this->{$sOptions}->setOpt( $insOption, $inmValue );
  121. $this->m_oPluginMainOptions->setOpt( $insOption, $inmValue );
  122. }
  123. protected function loadOptionsHandler( $insOptionHandler = 'PluginMain', $infRecreate = false, $infFullBuild = false ) {
  124. $aAllHandlers = array_values( $this->getFeaturesMap() );
  125. $aAllHandlers[] = 'PluginMain';
  126. // special case
  127. if ( $insOptionHandler == 'all' ) {
  128. foreach( $aAllHandlers as $sHandler ) {
  129. $fSuccess = $this->loadOptionsHandler( $sHandler, $infRecreate, $infFullBuild );
  130. }
  131. return $fSuccess;
  132. }
  133. if ( !in_array( $insOptionHandler, $aAllHandlers ) ) {
  134. return false;
  135. }
  136. $sOptionsVarName = 'm_o'.$insOptionHandler.'Options'; // e.g. m_oPluginMainOptions
  137. if ( $insOptionHandler == 'PluginMain' ) {
  138. $sSourceFile = dirname(__FILE__).'/icwp-optionshandler-'.$this->sPluginSlug.'.php'; // e.g. icwp-optionshandler-wpsf.php
  139. $sClassName = 'ICWP_OptionsHandler_'.ucfirst( $this->sPluginSlug ); // e.g. ICWP_OptionsHandler_Wpsf
  140. }
  141. else {
  142. $sSourceFile = dirname(__FILE__).'/icwp-optionshandler-'.strtolower($insOptionHandler).'.php'; // e.g. icwp-optionshandler-wpsf.php
  143. $sClassName = 'ICWP_OptionsHandler_'.$insOptionHandler; // e.g. ICWP_OptionsHandler_Wpsf
  144. }
  145. require_once( $sSourceFile );
  146. if ( $infRecreate || !isset( $this->{$sOptionsVarName} ) ) {
  147. $this->{$sOptionsVarName} = new $sClassName( $this->oPluginVo );
  148. }
  149. if ( $infFullBuild ) {
  150. $this->{$sOptionsVarName}->buildOptions();
  151. }
  152. return true;
  153. }
  154. /**
  155. * Given a feature/processor name will load the variable for it, including the appropriate source file.
  156. *
  157. * @param string $insProcessorName
  158. * @param boolean $infRebuild
  159. * @return ICWP_OptionsHandler_Base_Wpsf
  160. */
  161. protected function loadProcessor( $insProcessorName, $infRebuild = false ) {
  162. $aAllProcessors = $this->getFeaturesMap();
  163. if ( !in_array( $insProcessorName, array_values($aAllProcessors) ) ) {
  164. $this->doWpDie( sprintf('Processor %s is not permitted here.', $insProcessorName) );
  165. }
  166. $sProcessorVarName = 'm_o'.$insProcessorName.'Processor'; // e.g. m_oFirewallProcessor
  167. $sSourceFile = dirname(__FILE__).'/icwp-processor-'.strtolower($insProcessorName).'.php'; // e.g. icwp-optionshandler-wpsf.php
  168. $sClassName = 'ICWP_'.strtoupper( $this->sPluginSlug ).'_'.$insProcessorName.'Processor'; // e.g. ICWP_WPSF_FirewallProcessor
  169. $sStorageKey = array_search($insProcessorName, $aAllProcessors).'_processor'; // e.g. firewall_processor
  170. $sOptionsHandlerVarName = 'm_o'.$insProcessorName.'Options'; // e.g. m_oFirewallOptions
  171. require_once( $sSourceFile );
  172. if ( $infRebuild || empty( $this->{$sProcessorVarName} ) ) {
  173. $oTemp = $this->getOption( $sStorageKey );
  174. if ( !$infRebuild && is_object( $oTemp ) && ( $oTemp instanceof $sClassName ) ) {
  175. $oTemp->reset();
  176. }
  177. else {
  178. $oTemp = new $sClassName( $this->oPluginVo, self::$sOptionPrefix );
  179. }
  180. $this->{$sProcessorVarName} = $oTemp;
  181. }
  182. if ( $this->loadOptionsHandler( $insProcessorName ) ) {
  183. $aOptionsValues = $this->{$sOptionsHandlerVarName}->getPluginOptionsValues();
  184. $this->{$sProcessorVarName}->setOptions( $aOptionsValues );
  185. }
  186. return $this->{$sProcessorVarName};
  187. }
  188. protected function resetProcessor( $insProcessorName ) {
  189. if ( !$this->getIsFeature( $insProcessorName ) ) {
  190. $this->doWpDie('Not a processor: '.$insProcessorName);
  191. return;
  192. }
  193. $this->loadProcessor( $insProcessorName );
  194. return;
  195. }
  196. protected function resetOptionHandler( $insOptionName ) {
  197. if ( !$this->getIsFeature( $insOptionName ) ) {
  198. $this->doWpDie('Not a feature: '.$insOptionName);
  199. return;
  200. }
  201. $this->loadOptionsHandler( $insOptionName );
  202. return;
  203. }
  204. public function clearCaches() {
  205. $aFeatures = $this->getFeaturesMap();
  206. foreach( $aFeatures as $sFeature ) {
  207. $this->resetOptionHandler( $sFeature );
  208. $this->resetProcessor( $sFeature );
  209. }
  210. }
  211. protected function getAllOptionsHandlers() {
  212. $this->loadOptionsHandler('all');
  213. $aOptions = array();
  214. foreach( $this->m_aOptionsHandlers as $sName ) {
  215. if ( isset( $this->{$sName} ) ) {
  216. $aOptions[] = &$this->{$sName};
  217. }
  218. }
  219. return $aOptions;
  220. }
  221. /**
  222. * Makes sure and cache the processors after all is said and done.
  223. */
  224. public function saveProcessors() {
  225. $aFeatures = $this->getFeaturesMap();
  226. foreach( $aFeatures as $sSlug => $sProcessorName ) {
  227. $oProcessor = $this->getProcessorVar( $sProcessorName );
  228. if ( !is_null($oProcessor) && is_object($oProcessor) ) {
  229. $oProcessor->store();
  230. }
  231. }
  232. }
  233. /**
  234. * Makes sure and cache the processors after all is said and done.
  235. */
  236. public function saveOptions() {
  237. $aOptions = $this->getAllOptionsHandlers();
  238. foreach( $aOptions as &$oOption ) {
  239. if ( isset( $oOption ) ) {
  240. $oOption->savePluginOptions();
  241. }
  242. }
  243. }
  244. /**
  245. *
  246. * @param string $insProcessorName
  247. * @param bool $infLoad
  248. * @return null|ICWP_WPSF_BaseProcessor
  249. */
  250. protected function getProcessorVar( $insProcessorName, $infLoad = false ) {
  251. if ( !$this->getIsFeature( $insProcessorName ) ) {
  252. return null;
  253. }
  254. $sProcessorVariable = 'm_o'.$insProcessorName.'Processor';
  255. if ( $infLoad || !isset( $this->{$sProcessorVariable} ) ) {
  256. $this->loadProcessor( $insProcessorName );
  257. }
  258. $sProcessorVariable = 'm_o'.$insProcessorName.'Processor';
  259. return $this->{$sProcessorVariable};
  260. }
  261. protected function shutdown() {
  262. parent::shutdown();
  263. $this->saveOptions();
  264. $this->saveProcessors();
  265. }
  266. protected function deleteAllPluginDbOptions() {
  267. if ( !current_user_can( 'manage_options' ) ) {
  268. return;
  269. }
  270. $aOptions = $this->getAllOptionsHandlers();
  271. foreach( $aOptions as &$oOption ) {
  272. $oOption->deletePluginOptions();
  273. }
  274. $aFeatures = $this->getFeaturesMap();
  275. foreach( $aFeatures as $sSlug => $sProcessorName ) {
  276. $oProcessor = $this->getProcessorVar( $sProcessorName, true );
  277. if ( !is_null($oProcessor) && is_object($oProcessor) ) {
  278. $oProcessor->deleteAndCleanUp();
  279. }
  280. }
  281. remove_action( 'shutdown', array( $this, 'onWpShutdown' ) );
  282. }
  283. public function onWpActivatePlugin() {
  284. $this->loadOptionsHandler( 'all', true, true );
  285. }
  286. public function onWpDeactivatePlugin() {
  287. if ( $this->m_oPluginMainOptions->getOpt( 'delete_on_deactivate' ) == 'Y' ) {
  288. $this->deleteAllPluginDbOptions();
  289. }
  290. }
  291. }
  292. endif;