PageRenderTime 473ms CodeModel.GetById 24ms RepoModel.GetById 4ms app.codeStats 0ms

/modules/htaccess_dir/htaccess_dir.inc.php

https://github.com/axxtel/agilebill
PHP | 472 lines | 336 code | 53 blank | 83 comment | 32 complexity | e52f6515f322eda185780f4d71725514 MD5 | raw file
  1. <?php
  2. /**
  3. * AgileBill - Open Billing Software
  4. *
  5. * This body of work is free software; you can redistribute it and/or
  6. * modify it under the terms of the Open AgileBill License
  7. * License as published at http://www.agileco.com/agilebill/license1-4.txt
  8. *
  9. * For questions, help, comments, discussion, etc., please join the
  10. * Agileco community forums at http://forum.agileco.com/
  11. *
  12. * @link http://www.agileco.com/
  13. * @copyright 2004-2008 Agileco, LLC.
  14. * @license http://www.agileco.com/agilebill/license1-4.txt
  15. * @author Tony Landis <tony@agileco.com>
  16. * @package AgileBill
  17. * @version 1.4.93
  18. */
  19. class htaccess_dir
  20. {
  21. # Open the constructor for this mod
  22. function htaccess_dir()
  23. {
  24. # name of this module:
  25. $this->module = "htaccess_dir";
  26. # location of the construct XML file:
  27. $this->xml_construct = PATH_MODULES . "" . $this->module . "/" . $this->module . "_construct.xml";
  28. # open the construct file for parsing
  29. $C_xml = new CORE_xml;
  30. $construct = $C_xml->xml_to_array($this->xml_construct);
  31. $this->method = $construct["construct"]["method"];
  32. $this->trigger = $construct["construct"]["trigger"];
  33. $this->field = $construct["construct"]["field"];
  34. $this->table = $construct["construct"]["table"];
  35. $this->module = $construct["construct"]["module"];
  36. $this->cache = $construct["construct"]["cache"];
  37. $this->order_by = $construct["construct"]["order_by"];
  38. $this->limit = $construct["construct"]["limit"];
  39. # add extra lines needed in the .htaccess files when added/updated:
  40. # Example:
  41. #$this->htaccess_extra = "Options +FollowSymlinks\n";
  42. $this->htaccess_extra = '';
  43. }
  44. ##############################
  45. ## ADD ##
  46. ##############################
  47. function add($VAR)
  48. {
  49. global $C_translate, $C_debug;
  50. $VAR['htaccess_dir_htaccess'] = '# Error!';
  51. $this->validated = true;
  52. ### Change the path...
  53. if ( isset ( $VAR['htaccess_dir_path'] ) && $VAR['htaccess_dir_path'] != '' )
  54. {
  55. # trim whitspaces
  56. $VAR['htaccess_dir_path'] = trim ( $VAR['htaccess_dir_path'] );
  57. # replace all forward slashes with back slashes
  58. $VAR['htaccess_dir_path'] = ereg_replace('\\\\', '/', $VAR['htaccess_dir_path']);
  59. # add the final trailing slash if missing
  60. if ( !ereg ('[/]$', $VAR['htaccess_dir_path'] ) )
  61. $VAR['htaccess_dir_path'] = $VAR['htaccess_dir_path'] . '/';
  62. }
  63. if( isset ( $VAR['htaccess_dir_path'] ) && $VAR['htaccess_dir_path'] != '' )
  64. {
  65. ################################################################
  66. ### VERIFY LOCAL PATH & WRITABILITY!
  67. @$filename = $VAR['htaccess_dir_path'] . '.htaccess';
  68. @$id = $VAR['htaccess_dir_htaccess_id'];
  69. $db = &DB();
  70. $sql = 'SELECT name FROM ' . AGILE_DB_PREFIX . 'htaccess WHERE
  71. site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
  72. id = ' . $db->qstr($id);
  73. $result = $db->Execute($sql);
  74. @$name = $result->fields['name'];
  75. ### Check path
  76. $path = $VAR['htaccess_dir_path'];
  77. if ( is_dir ( $path ) )
  78. {
  79. ### Check if is writable!
  80. if ( !is_writable ( $path ) )
  81. {
  82. ## Path not writable!
  83. $this->validated = false;
  84. $this->val_error[] = array(
  85. 'field' => 'none',
  86. 'field_trans' => $C_translate->translate('error', 'core', ""),
  87. 'error' => $C_translate->translate('path_auth', 'htaccess_dir', ""));
  88. }
  89. }
  90. else
  91. {
  92. ### Path broken!
  93. $this->validated = false;
  94. $this->val_error[] = array(
  95. 'field' => 'none',
  96. 'field_trans' => $C_translate->translate('error', 'core', ""),
  97. 'error' => $C_translate->translate('path_broke', 'htaccess_dir', ""));
  98. }
  99. }
  100. ####################################################################
  101. ### If validation was failed, skip the db insert &
  102. ### set the errors & origonal fields as Smarty objects,
  103. ### and change the page to be loaded.
  104. ####################################################################
  105. if(!$this->validated)
  106. {
  107. global $smarty;
  108. # set the errors as a Smarty Object
  109. $smarty->assign('form_validation', $this->val_error);
  110. # set the page to be loaded
  111. if(!defined("FORCE_PAGE"))
  112. {
  113. define('FORCE_PAGE', $VAR['_page_current']);
  114. }
  115. return;
  116. }
  117. ####################################################################
  118. ### Create the record/verify fields
  119. $type = "add";
  120. $this->method["$type"] = explode(",", $this->method["$type"]);
  121. $db = new CORE_database;
  122. $dir_id = $db->add($VAR, $this, $type);
  123. ####################################################################
  124. ### Create the .htaccess file
  125. if( isset ( $dir_id ) && $dir_id > 0 )
  126. {
  127. ### GENERATE THE EXCLUDE LIST
  128. $exclude_list = $this->exclude_list();
  129. ### GENERATE THE .HTACCESS FILE
  130. $nl = "\n";
  131. $data = $this->htaccess_extra . 'RewriteEngine on' . $nl;
  132. if(empty($VAR['htaccess_dir_recursive']))
  133. $data .= 'RewriteRule ^(.*)/.*$ - [L]' . $nl;
  134. $data .= 'RewriteRule ' . $exclude_list . '$ htaccess_index.php?_HTACCESS_ID='.$id.'&_HTACCESS_DIR_ID='.$dir_id;
  135. ### Update the db record
  136. $db = &DB();
  137. $sql = "UPDATE ".AGILE_DB_PREFIX."htaccess_dir SET
  138. htaccess = " . $db->qstr( $data ) . " WHERE
  139. id = " . $db->qstr( $dir_id ) . " AND
  140. site_id = " . $db->qstr( DEFAULT_SITE );
  141. $result = $db->Execute($sql);
  142. ### WRITE THE LOCAL .HTACCESS FILE
  143. $fp = fopen($filename, "w+");
  144. fwrite($fp,$data);
  145. fclose($fp);
  146. ### WRITE THE htaccess_index.php FILE
  147. $php_filename = $VAR['htaccess_dir_path'] . 'htaccess_index.php';
  148. $data = $this->create_php();
  149. $fp = fopen($php_filename, "w+");
  150. fwrite($fp,$data);
  151. fclose($fp);
  152. }
  153. }
  154. ##############################
  155. ## UPDATE ##
  156. ##############################
  157. function update($VAR)
  158. {
  159. global $C_translate, $C_debug;
  160. $this->validated = true;
  161. ### Change the path...
  162. if ( isset ( $VAR['htaccess_dir_path'] ) && $VAR['htaccess_dir_path'] != '' )
  163. {
  164. # trim whitspaces
  165. $VAR['htaccess_dir_path'] = trim ( $VAR['htaccess_dir_path'] );
  166. # replace all forward slashes with back slashes
  167. $VAR['htaccess_dir_path'] = ereg_replace('\\\\', '/', $VAR['htaccess_dir_path']);
  168. # add the final trailing slash if missing
  169. if ( !ereg ('[/]$', $VAR['htaccess_dir_path'] ) )
  170. $VAR['htaccess_dir_path'] = $VAR['htaccess_dir_path'] . '/';
  171. }
  172. ### Change the .htaccess data
  173. if( isset ( $VAR['htaccess_dir_path'] ) && $VAR['htaccess_dir_path'] != '' )
  174. {
  175. ################################################################
  176. ### VERIFY LOCAL PATH & WRITABILITY!
  177. @$filename = $VAR['htaccess_dir_path'] . '.htaccess';
  178. @$php_filename = $VAR['htaccess_dir_path'] . 'htaccess_index.php';
  179. @$id = $VAR['htaccess_dir_htaccess_id'];
  180. $db = &DB();
  181. $sql = 'SELECT name FROM ' . AGILE_DB_PREFIX . 'htaccess WHERE
  182. site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
  183. id = ' . $db->qstr($id);
  184. $result = $db->Execute($sql);
  185. @$name = $result->fields['name'];
  186. ### Check path
  187. $path = $VAR['htaccess_dir_path'];
  188. if ( is_dir ( $path ) )
  189. {
  190. ### Check if is writable!
  191. if ( is_writable ( $path ) )
  192. {
  193. ### GENERATE THE EXCLUDE LIST
  194. $exclude_list = $this->exclude_list();
  195. $nl = "\n";
  196. /*
  197. $data = $this->htaccess_extra .
  198. 'RewriteEngine on' . $nl .
  199. 'RewriteRule ^(.*)/.*$ - [L]' . $nl .
  200. 'RewriteRule ' .
  201. '' . $exclude_list . '$ ' .
  202. 'htaccess_index.php' .
  203. '?_HTACCESS_ID='.$id.'&_HTACCESS_DIR_ID='.$VAR["htaccess_dir_id"];
  204. */
  205. $data = $this->htaccess_extra . 'RewriteEngine on' . $nl;
  206. if(empty($VAR['htaccess_dir_recursive']))
  207. $data .= 'RewriteRule ^(.*)/.*$ - [L]' . $nl;
  208. $data .= 'RewriteRule ' . $exclude_list . '$ htaccess_index.php?_HTACCESS_ID='.$id.'&_HTACCESS_DIR_ID='.$VAR["htaccess_dir_id"];
  209. ### Set the .htaccess var for the db
  210. $VAR['htaccess_dir_htaccess'] = $data;
  211. }
  212. else
  213. {
  214. ## Path not writable!
  215. $this->validated = false;
  216. $this->val_error[] = array(
  217. 'field' => 'none',
  218. 'field_trans' => $C_translate->translate('error', 'core', ""),
  219. 'error' => $C_translate->translate('path_auth', 'htaccess_dir', ""));
  220. }
  221. }
  222. else
  223. {
  224. ### Path broken!
  225. $this->validated = false;
  226. $this->val_error[] = array(
  227. 'field' => 'none',
  228. 'field_trans' => $C_translate->translate('error', 'core', ""),
  229. 'error' => $C_translate->translate('path_broke', 'htaccess_dir', ""));
  230. }
  231. }
  232. ####################################################################
  233. ### If validation was failed, skip the db insert &
  234. ### set the errors & origonal fields as Smarty objects,
  235. ### and change the page to be loaded.
  236. ####################################################################
  237. if(!$this->validated)
  238. {
  239. global $smarty;
  240. # set the errors as a Smarty Object
  241. $smarty->assign('form_validation', $this->val_error);
  242. # set the page to be loaded
  243. if(!defined("FORCE_PAGE"))
  244. {
  245. define('FORCE_PAGE', $VAR['_page']);
  246. }
  247. return;
  248. }
  249. ### Update the db record
  250. $type = "update";
  251. $this->method["$type"] = explode(",", $this->method["$type"]);
  252. $db = new CORE_database;
  253. $dir = $db->update($VAR, $this, $type);
  254. if($dir != false)
  255. {
  256. ### UPDATE THE LOCAL .HTACCESS FILE
  257. $fp = fopen($filename, "w+");
  258. fwrite($fp,$data);
  259. fclose($fp);
  260. ### UPDATE THE LOCAL htaccess_index.php
  261. $data = $this->create_php();
  262. $fp = fopen($php_filename, "w+");
  263. fwrite($fp,$data);
  264. fclose($fp);
  265. }
  266. }
  267. ##############################
  268. ## DELETE ##
  269. ##############################
  270. function delete($VAR)
  271. {
  272. ### Get the array
  273. if(isset($VAR["delete_id"]))
  274. $id = explode(',', $VAR["delete_id"]);
  275. elseif (isset($VAR["id"]))
  276. $id = explode(',', $VAR["id"]);
  277. ### Loop:
  278. for($i=0; $i<count($id); $i++)
  279. {
  280. ### Delete the protection
  281. $this->delete_one($id[$i]);
  282. }
  283. }
  284. ##############################
  285. ## DELETE ONE ##
  286. ##############################
  287. function delete_one($id)
  288. {
  289. global $C_debug, $C_translate;
  290. if ($id == '') return false;
  291. ### Get the details of this directory record
  292. $db = &DB();
  293. $sql = "SELECT * FROM ".AGILE_DB_PREFIX."htaccess_dir WHERE
  294. site_id = ".$db->qstr(DEFAULT_SITE)." AND
  295. id = ".$db->qstr($id);
  296. $result = $db->Execute($sql);
  297. $type = $result->fields['type'];
  298. $path = $result->fields['path'];
  299. if( $result != false )
  300. {
  301. ### DELETE THE LOCAL .HTACCESS FILE
  302. $filename = $result->fields['path'] . '.htaccess';
  303. if ( @unlink ($filename) === false)
  304. {
  305. $C_translate->value['htaccess_dir']['dir'] = $result->fields['path'] . '.htaccess';
  306. $C_debug->alert($C_translate->translate('remove_fail','htaccess_dir',''));
  307. }
  308. ### DELETE THE LOCAL HTACCESS_ATILE.PHP FILE
  309. $filename = $result->fields['path'] . 'htaccess_index.php';
  310. @unlink ($filename);
  311. }
  312. ### Delete the Record:
  313. $db = &DB();
  314. $sql = "DELETE FROM ".AGILE_DB_PREFIX."htaccess_dir WHERE
  315. site_id = ".$db->qstr(DEFAULT_SITE)." AND
  316. id = ".$db->qstr($id);
  317. $resulta = $db->Execute($sql);
  318. ### Success message
  319. $C_translate->value['htaccess_dir']['dira'] = $path;
  320. $C_debug->alert($C_translate->translate('remove_success','htaccess_dir',''));
  321. return true;
  322. }
  323. ##############################
  324. ## VIEW ##
  325. ##############################
  326. function view($VAR)
  327. {
  328. $type = "view";
  329. $this->method["$type"] = explode(",", $this->method["$type"]);
  330. $db = new CORE_database;
  331. $db->view($VAR, $this, $type);
  332. }
  333. ##############################
  334. ## SEARCH ##
  335. ##############################
  336. function search($VAR)
  337. {
  338. $type = "search";
  339. $this->method["$type"] = explode(",", $this->method["$type"]);
  340. $db = new CORE_database;
  341. $db->search($VAR, $this, $type);
  342. }
  343. ##############################
  344. ## SEARCH SHOW ##
  345. ##############################
  346. function search_show($VAR)
  347. {
  348. $type = "search";
  349. $this->method["$type"] = explode(",", $this->method["$type"]);
  350. $db = new CORE_database;
  351. $db->search_show($VAR, $this, $type);
  352. }
  353. function exclude_list()
  354. {
  355. global $VAR;
  356. $list = '';
  357. @$Arr = $VAR['htaccess_dir_exclude'];
  358. if ( count($Arr) == 0) return '';
  359. $db = &DB();
  360. $sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'htaccess_exclude WHERE
  361. site_id = ' . $db->qstr(DEFAULT_SITE);
  362. $result = $db->Execute($sql);
  363. if($result->RecordCount() == 0) return '';
  364. while(!$result->EOF)
  365. {
  366. $id = $result->fields['id'];
  367. $ext= $result->fields['extension'];
  368. ### GENERATE THE EXCLUDE LIST
  369. for ($i=0; $i<count($Arr); $i++)
  370. {
  371. if ($id == $Arr[$i])
  372. {
  373. if ( $list == '')
  374. $list = $ext;
  375. else
  376. $list.= '|'.$ext;
  377. }
  378. }
  379. $result->MoveNext();
  380. }
  381. if ($list != '') $list = '!(\.+' . $list . ')';
  382. return $list;
  383. }
  384. function create_php()
  385. {
  386. $data = '<?php
  387. define ( "INDEX_FILE", "index.html" );
  388. require_once ("' . PATH_AGILE . 'htaccess_index.php"); ?>';
  389. return $data;
  390. }
  391. }
  392. ?>