PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htaccess_index.php

https://github.com/oilcf/agilebill
PHP | 266 lines | 186 code | 39 blank | 41 comment | 33 complexity | a30491f14f22f5dc98476f2766019ac6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, LGPL-2.0
  1. <?php
  2. ob_start();
  3. # Define the file types
  4. $_IncludeFiles = Array ( 'htm', 'html', 'php', 'php3', 'php4', 'phtml', 'inc', 'phps' );
  5. $_VirtualFiles = Array ( 'cgi', 'shtml', 'pl' );
  6. $_PassFiles = Array
  7. (
  8. Array ('name' => 'jpg', 'type' => 'image/jpeg', 'disposition' => 'inline'),
  9. Array ('name' => 'jpeg', 'type' => 'image/jpeg', 'disposition' => 'inline'),
  10. Array ('name' => 'jpe', 'type' => 'image/jpeg', 'disposition' => 'inline'),
  11. Array ('name' => 'gif', 'type' => 'image/gif', 'disposition' => 'inline'),
  12. Array ('name' => 'bmp', 'type' => 'image/bmp', 'disposition' => 'inline'),
  13. Array ('name' => 'tif', 'type' => 'image/tif', 'disposition' => 'inline'),
  14. Array ('name' => 'png', 'type' => 'image/png', 'disposition' => 'inline'),
  15. Array ('name' => 'wbmp', 'type' => 'image/vnd.wap.wbmp', 'disposition' => 'inline'),
  16. Array ('name' => 'pdf', 'type' => 'application/pdf', 'disposition' => 'inline'),
  17. Array ('name' => 'exe', 'type' => 'application/octet-stream', 'disposition'=> 'attatchment'),
  18. Array ('name' => 'zip', 'type' => 'application/x-zip', 'disposition' => 'attatchment'),
  19. Array ('name' => 'gzip', 'type' => 'application/gzip', 'disposition' => 'attatchment'),
  20. Array ('name' => 'tgz', 'type' => 'application/tgz', 'disposition' => 'attatchment'),
  21. Array ('name' => 'gz', 'type' => 'application/gz', 'disposition' => 'attatchment'),
  22. Array ('name' => 'doc', 'type' => 'application/ms-word', 'disposition' => 'inline'),
  23. Array ('name' => 'xls', 'type' => 'application/ms-excel', 'disposition' => 'inline'),
  24. Array ('name' => 'csv', 'type' => 'application/ms-excel', 'disposition' => 'inline'),
  25. Array ('name' => 'swf', 'type' => 'application/x-shockwave-flash', 'disposition' => 'inline'),
  26. Array ('name' => 'txt', 'type' => 'text/plain', 'disposition' => 'inline'),
  27. Array ('name' => 'text', 'type' => 'text/plain', 'disposition' => 'inline'),
  28. Array ('name' => 'rtf', 'type' => 'text/richtext', 'disposition' => 'inline'),
  29. Array ('name' => 'xml', 'type' => 'text/xml', 'disposition' => 'inline'),
  30. Array ('name' => 'css', 'type' => 'text/css', 'disposition' => 'inline'),
  31. Array ('name' => 'js', 'type' => 'text/plain', 'disposition' => 'inline'),
  32. Array ('name' => 'wml', 'type' => 'text/vnd.wap.wml', 'disposition' => 'inline'),
  33. Array ('name' => 'avi', 'type' => 'video/avi', 'disposition' => 'attatchment'),
  34. Array ('name' => 'mpg', 'type' => 'video/mpeg', 'disposition' => 'attatchment'),
  35. Array ('name' => 'mpeg', 'type' => 'video/mpeg', 'disposition' => 'attatchment'),
  36. Array ('name' => 'mpe', 'type' => 'video/mpeg', 'disposition' => 'attatchment'),
  37. Array ('name' => 'wmv', 'type' => 'video/x-ms-wmv', 'disposition' => 'attatchment'),
  38. Array ('name' => 'asf', 'type' => 'video/x-ms-asf', 'disposition' => 'attatchment')
  39. );
  40. # Load the config file:
  41. require_once('config.inc.php');
  42. # Require the needed files...
  43. require_once(PATH_ADODB . 'adodb.inc.php');
  44. require_once(PATH_CORE . 'auth.inc.php');
  45. require_once(PATH_CORE . 'database.inc.php');
  46. require_once(PATH_CORE . 'method.inc.php');
  47. require_once(PATH_CORE . 'session.inc.php');
  48. require_once(PATH_CORE . 'translate.inc.php');
  49. require_once(PATH_CORE . 'setup.inc.php');
  50. require_once(PATH_CORE . 'vars.inc.php');
  51. require_once(PATH_CORE . 'xml.inc.php');
  52. ## Path to the error file
  53. define ( 'ERROR_GIF', PATH_THEMES.DEF_THEME_N.'/images/htaccess_error.gif' );
  54. # start the debugger
  55. $C_debug = new CORE_debugger;
  56. # initialize the GET/POST vars
  57. $C_vars = new CORE_vars;
  58. $VAR = $C_vars->f;
  59. # initialize the site setup
  60. $C_setup = new CORE_setup;
  61. # initialize the session handler
  62. $C_sess = new CORE_session;
  63. # define the other session variables as constants
  64. $C_sess->session_constant();
  65. # initialize the translation handler
  66. $C_translate = new CORE_translate;
  67. # update the session constants
  68. $C_sess->session_constant_log();
  69. # initialze the authentication handler
  70. $force = false;
  71. $C_auth = new CORE_auth ($force);
  72. ########################################################################
  73. # Verify the User's Access
  74. $authorized = false;
  75. if(defined("SESS_LOGGED"))
  76. if(SESS_LOGGED == "1" && check_auth($VAR['_HTACCESS_ID']))
  77. $authorized = true;
  78. ############################################################################
  79. ## If this was a GET:
  80. if ( isset($REQUEST_URI ) )
  81. {
  82. $ARRAY = explode ( '?', $REQUEST_URI);
  83. $REQUEST_URI = $ARRAY[0] ;
  84. }
  85. ## Define global system vars...
  86. if(!isset($DOCUMENT_ROOT)) $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
  87. if(!isset($REQUEST_URI)) $REQUEST_URI = $_SERVER["REQUEST_URI"];
  88. if(!isset($SCRIPT_FILENAME)) $SCRIPT_FILENAME = $_SERVER["SCRIPT_FILENAME"];
  89. ############################################################################
  90. ### Check if File Exists:
  91. if (file_exists($DOCUMENT_ROOT.$REQUEST_URI) &&
  92. ($SCRIPT_FILENAME != $DOCUMENT_ROOT.$REQUEST_URI) &&
  93. ($REQUEST_URI != "/") &&
  94. (!ereg( '[////]{2,}$', $REQUEST_URI ) ) )
  95. {
  96. $url = $REQUEST_URI;
  97. ########################################################################
  98. # Check Passthu File Types:
  99. for ($i=0; $i<count($_PassFiles); $i++)
  100. {
  101. $ext = substr (strrchr ($DOCUMENT_ROOT.$url, "."), 1);
  102. if ( strtolower ( $ext ) == $_PassFiles[$i]["name"] )
  103. {
  104. if ($authorized)
  105. {
  106. # determine the filename:
  107. $ext1 = $_PassFiles[$i]['name'];
  108. @$arr2 = explode('/', $REQUEST_URI);
  109. $file_name = 'download.'.$ext1;
  110. for($ii=0; $ii<count($arr2); $ii++)
  111. $file_name = $arr2[$ii];
  112. # Set the correct header info:
  113. header("Content-type: " . $_PassFiles[$i]['type']);
  114. header("Content-Disposition: " . $_PassFiles[$i]['disposition'] . ";filename=$file_name");
  115. header("Cache-Control: no-store, no-cache, must-revalidate");
  116. header("Cache-Control: post-check=0, pre-check=0", false);
  117. header("Pragma: no-cache");
  118. @readfile ($DOCUMENT_ROOT.$url, "r");
  119. exit();
  120. }
  121. else
  122. {
  123. # Display the error gif:
  124. header("Content-type: image/gif");
  125. header("Content-Disposition: inline;filename=error.gif");
  126. header("Cache-Control: no-store, no-cache, must-revalidate");
  127. header("Cache-Control: post-check=0, pre-check=0", false);
  128. header("Pragma: no-cache");
  129. @readfile (ERROR_GIF, "r");
  130. exit();
  131. }
  132. }
  133. }
  134. ########################################################################
  135. # Check Include File Types:
  136. for ($i=0; $i<count($_IncludeFiles); $i++)
  137. {
  138. $ext = substr (strrchr ($DOCUMENT_ROOT.$url, "."), 1);
  139. if ( strtolower ( $ext ) == $_IncludeFiles[$i] )
  140. {
  141. if ($authorized)
  142. {
  143. ## run:
  144. include_once ( $DOCUMENT_ROOT.$url );
  145. exit();
  146. }
  147. else
  148. {
  149. ## forward to login page:
  150. header("Location: ".URL."?_page=account:login_htaccess&_htaccess_id=" . $VAR['_HTACCESS_ID'] . '&_htaccess_dir_id=' . $VAR['_HTACCESS_DIR_ID']);
  151. }
  152. }
  153. }
  154. ########################################################################
  155. # Check Virtual File Types:
  156. for ($i=0; $i<count($_VirtualFiles); $i++)
  157. {
  158. $ext = substr (strrchr ($DOCUMENT_ROOT.$url, "."), 1);
  159. if ( strtolower ( $ext ) == $_VirtualFiles[$i] )
  160. {
  161. virtual ( $DOCUMENT_ROOT.$url . "?" . $variables); // < needs some work!
  162. exit();
  163. }
  164. }
  165. }
  166. ########################################################################
  167. ### Load the index file:
  168. $url=strip_tags($REQUEST_URI);
  169. $url_array=explode("/",$url);
  170. array_shift($url_array);
  171. if ( $authorized ) {
  172. if(!empty($url_array) && file_exists($DOCUMENT_ROOT.$url.INDEX_FILE) ) {
  173. include(INDEX_FILE);
  174. exit();
  175. } else {
  176. ## Locate the index file, if any
  177. for($i=0; $i<count($_IncludeFiles); $i++) {
  178. if(file_exists($DOCUMENT_ROOT.$url.'index.'.$_IncludeFiles[$i])) {
  179. include($DOCUMENT_ROOT.$url.'index.'.$_IncludeFiles[$i]);
  180. exit();
  181. }
  182. }
  183. ## No index located!
  184. echo "<BR><BR><B><CENTER>PAGE NOT FOUND</CENTER></B>";
  185. exit();
  186. }
  187. } else {
  188. ## forward to login page:
  189. header("Location: ".URL."?_page=account:login_htaccess&_htaccess_id=" . $VAR['_HTACCESS_ID'] . '&_htaccess_dir_id=' . $VAR['_HTACCESS_DIR_ID']);
  190. exit();
  191. }
  192. ########################################################################
  193. # Filetype not defined, force download:
  194. header("Pragma: public");
  195. header("Expires: 0");
  196. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  197. header("Content-Type: application/force-download");
  198. header("Content-Type: application/octet-stream");
  199. header("Content-Type: application/download");
  200. header("Content-Disposition: attachment; filename=".@basename($DOCUMENT_ROOT.$url).";");
  201. header("Content-Transfer-Encoding: binary");
  202. header("Content-Length: ".@filesize($DOCUMENT_ROOT.$url));
  203. @readfile("$DOCUMENT_ROOT.$url");
  204. exit();
  205. ##############################
  206. ## Check Authentication ##
  207. ##############################
  208. function check_auth($id)
  209. {
  210. ### Check if user is a member of one of the authorized groups:
  211. $db = &DB();
  212. $sql = 'SELECT status,group_avail FROM ' . AGILE_DB_PREFIX . 'htaccess WHERE
  213. site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
  214. status = ' . $db->qstr('1') . ' AND
  215. id = ' . $db->qstr($id);
  216. $result = $db->Execute($sql);
  217. if($result->RecordCount() > 0) {
  218. global $C_auth;
  219. @$arr = unserialize($result->fields['group_avail']);
  220. for($i=0; $i<count($arr); $i++)
  221. if($C_auth->auth_group_by_id($arr[$i]))
  222. return true;
  223. }
  224. return false;
  225. }
  226. ob_end_flush();
  227. ?>