PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php-getid3-2.0.0b5/sample_apps/morg/morg/application.php

#
PHP | 518 lines | 226 code | 163 blank | 129 comment | 33 complexity | 9e01409ac4f2cf4b995da83486fae6c2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5.1.4 |
  4. // +----------------------------------------------------------------------+
  5. // | Placed in public domain by Allan Hansen, 2003-2006. |
  6. // | Share and enjoy! |
  7. // +----------------------------------------------------------------------+
  8. // | Updates and other scripts by Allan Hansen here: |
  9. // | http://www.artemis.dk/php/ |
  10. // +----------------------------------------------------------------------+
  11. // | application.php |
  12. // | Web Application Framework |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: Allan Hansen <ah@artemis.dk> |
  15. // +----------------------------------------------------------------------+
  16. //
  17. // $Id: application.php,v 1.4 2007/01/03 16:15:59 ah Exp $
  18. /**
  19. * Web Application Framework (superclass).
  20. */
  21. class application
  22. {
  23. // HTML header values
  24. protected $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  25. protected $charset = "ISO-8859-1";
  26. protected $css = "/main.css"; // array also supported
  27. protected $meta = array (); // array of array (type, key, value) - type is "http-equiv" or "name"
  28. protected $add_head; // additional html for <head>...</head>
  29. protected $body_id; // id of <body>
  30. // Database parameters
  31. protected $db_handler = "mysql"; // mysql, postgresql or firebird
  32. protected $db_server = "localhost";
  33. protected $db_database;
  34. protected $db_username;
  35. protected $db_password;
  36. protected $db_persistent = true;
  37. // Error handling
  38. protected $error;
  39. // Public database handle
  40. public $dbh;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. // Initialise
  47. error_reporting(E_ALL);
  48. ignore_user_abort(true);
  49. set_magic_quotes_runtime(0);
  50. // Load dependecies
  51. require_once 'abstraction.php';
  52. // Default meta tags
  53. $this->meta[] = array ('http-equiv', 'Content-Style-Type', 'text/css');
  54. }
  55. /**
  56. * HTML Header.
  57. *
  58. * @param string <title>...</title>
  59. * @return string
  60. */
  61. protected function head($title)
  62. {
  63. // Set charset
  64. header("Content-Type: text/html; charset=$this->charset");
  65. // Support both $this->css as string and array
  66. if (is_string($this->css)) {
  67. $this->css = array ($this->css);
  68. }
  69. // Output HTML header
  70. $result = $this->doctype ."\n<html>\n\n<head>\n<title>$title</title>";
  71. // Output meta tags
  72. foreach ($this->meta as $meta) {
  73. list($type, $key, $value) = $meta;
  74. $result .= "\n<meta $type='$key' content='$value' />";
  75. }
  76. // Output links to style sheets
  77. foreach ($this->css as $css) {
  78. $result .= "\n<link href='$css' type='text/css' rel='stylesheet' />";
  79. }
  80. $result .= "\n" . $this->add_head . "\n</head>\n\n<body" . ($this->body_id ? "id='$this->body_id'" : '') . ">";
  81. return $result;
  82. }
  83. /**
  84. * HTML Footer.
  85. *
  86. * @return string
  87. */
  88. protected function foot()
  89. {
  90. return "\n\n</body>\n</html>";
  91. }
  92. /**
  93. * Read language file and define constants.
  94. *
  95. * File is a tab seperated csv file with no quotes. Header row should contain
  96. * LABEL<tab>EN<tab>DA<ta>LANG3
  97. * Empty lines are allowed
  98. *
  99. * @param string filename Filename
  100. * @param string language Language column to use
  101. */
  102. protected function process_language_file($filename, $language)
  103. {
  104. // Read file into array
  105. $constants = file($filename);
  106. // Remove first array entry - contains languages
  107. $header = explode("\t", trim(array_shift($constants)));
  108. $offset = array_search($language, $header);
  109. // Loop thru array and define labels
  110. foreach ($constants as $line) {
  111. // Trim - remove possible \r\n only
  112. $line = trim($line, "\r\n");
  113. // Skip empty lines and comments
  114. if (trim($line) && $line{0} != '#') {
  115. // Extact values
  116. $values = explode("\t", $line);
  117. // Die if file is broken - better nok be broken
  118. if (sizeof($values) <= $offset) {
  119. die("Error in language file, $filename - $line");
  120. }
  121. // Define constant
  122. define($values[0], $values[$offset]);
  123. }
  124. }
  125. }
  126. /**
  127. * Read constants file and define.
  128. *
  129. * File is a tab seperated csv file with no quotes. Header row should contain
  130. * Empty lines and comments starting with # are allowed
  131. *
  132. * @param string filename Filename
  133. */
  134. protected function process_constants_file($filename)
  135. {
  136. // Read file into array
  137. $constants = file($filename);
  138. // Loop thru array and define labels
  139. foreach ($constants as $line) {
  140. // Trim - remove possible \r\n only
  141. $line = trim($line, "\r\n");
  142. // Skip empty lines and comments
  143. if (trim($line) && $line{0} != '#') {
  144. // Extact values
  145. $values = explode("\t", $line);
  146. // Die if file is broken - better not be broken!
  147. if (sizeof($values) != 2) {
  148. die("Error in constantse file, $filename - $line");
  149. }
  150. // Define constant
  151. define($values[0], $values[1]);
  152. }
  153. }
  154. }
  155. /**
  156. * Format message - typically defined by process_language_file()
  157. *
  158. * @param string string String to format. Contains %Code%.
  159. * @param array values Array ("Code" => $value, "Code2" => $value2)
  160. * @return string
  161. */
  162. public static function format_string($string, $values)
  163. {
  164. foreach ($values as $code => $value) {
  165. $string = str_replace("%$code%", $value, $string);
  166. }
  167. return $string;
  168. }
  169. /**
  170. * Redirect to new url.
  171. *
  172. * @param string url New url to redirect to.
  173. */
  174. public static function redirect($url)
  175. {
  176. // Replace relative urls with absolute to adhere to standard.
  177. if ($url[0] == "/") {
  178. if ($_SERVER["SERVER_PORT"] == 443) {
  179. $url= "https://$_SERVER[HTTP_HOST]$url";
  180. }
  181. else {
  182. $url= "http://$_SERVER[HTTP_HOST]$url";
  183. }
  184. }
  185. // Send redirect header
  186. header("Location: $url");
  187. // Close connection on HTTP 1.1 to avoid browser hang.
  188. if ($_SERVER["SERVER_PROTOCOL"] == "HTTP/1.1") {
  189. header("Connection: close");
  190. }
  191. // Stop script
  192. die();
  193. }
  194. // Global Error handler - adds error message from string or object to internal list of errors
  195. protected function error($error)
  196. {
  197. $this->rollback();
  198. if (empty($this->error)) {
  199. $this->error = array ();
  200. }
  201. if (is_object($error) && isset($error->error)) {
  202. $this->error($error->error);
  203. }
  204. if (is_string($error)) {
  205. $this->error[] = $error;
  206. return;
  207. }
  208. if (is_array($error)) {
  209. $this->error = array_merge($this->error, $error);
  210. }
  211. }
  212. /**
  213. * Error handler, internal errors.
  214. */
  215. public function internal_error()
  216. {
  217. @$this->rollback();
  218. header("HTTP/1.0 500 Internal Error");
  219. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/500.php")) {
  220. die();
  221. }
  222. echo $this->head("500 Internal Error");
  223. echo $this->foot();
  224. die();
  225. }
  226. /**
  227. * Error handler, data errors.
  228. */
  229. public function bad_request()
  230. {
  231. @$this->rollback();
  232. header("HTTP/1.0 400 Bad Request");
  233. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/400.php")) {
  234. die();
  235. }
  236. echo $this->head("400 Bad Request");
  237. echo $this->foot();
  238. die();
  239. }
  240. /**
  241. * Error handler, authentication errors.
  242. */
  243. public function unauthorized()
  244. {
  245. @$this->rollback();
  246. header("HTTP/1.0 401 Unauthorized");
  247. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/401.php")) {
  248. die();
  249. }
  250. echo $this->head("401 Unauthorized");
  251. echo $this->foot();
  252. die();
  253. }
  254. /**
  255. * Error handler, payment required errors.
  256. */
  257. public function payment_required()
  258. {
  259. @$this->rollback();
  260. header("HTTP/1.0 402 Payment Required");
  261. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/402.php")) {
  262. die();
  263. }
  264. echo $this->head("402 Payment Required");
  265. echo $this->foot();
  266. die();
  267. }
  268. /**
  269. * Error handler, forbidden errors.
  270. */
  271. public function forbidden()
  272. {
  273. $this->rollback();
  274. header("HTTP/1.0 403 Forbidden");
  275. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/403.php")) {
  276. die();
  277. }
  278. echo $this->head("403 Forbidden");
  279. echo $this->foot();
  280. die();
  281. }
  282. /**
  283. * Error handler, not_found errors.
  284. */
  285. public function not_found()
  286. {
  287. @$this->rollback();
  288. header("HTTP/1.0 404 Forbidden");
  289. if (@include($_SERVER["DOCUMENT_ROOT"] . "/templates/404.php")) {
  290. die();
  291. }
  292. echo $this->head("404 Not Found");
  293. echo $this->foot();
  294. die();
  295. }
  296. /**
  297. * Generate random numbers
  298. */
  299. public static function random($min, $max)
  300. {
  301. if (!isset($this->random_generator_initialsed)) {
  302. srand((double)microtime()*1000000);
  303. $this->random_generator_initialsed = true;
  304. }
  305. return ceil(rand() / getrandmax() * (1 + $max - $min)) - 1 + $min;
  306. }
  307. /**
  308. * Connect to database.
  309. */
  310. protected function db_connect()
  311. {
  312. // Load dependency
  313. require_once $this->db_handler.".php";
  314. // Connect to database
  315. if (!isset($this->dbh)) {
  316. $this->dbh = new $this->db_handler;
  317. $this->dbh->database = $this->db_database;
  318. $this->dbh->host = $this->db_server;
  319. $this->dbh->username = $this->db_username;
  320. $this->dbh->password = $this->db_password;
  321. return $this->dbh->connect();
  322. }
  323. }
  324. /**
  325. * Spawn a new $dbObject properly connected to database.
  326. *
  327. * @access private (protected)
  328. */
  329. protected function dbObject_spawn($dbObject)
  330. {
  331. if (!isset($this->dbh)) {
  332. $this->db_connect();
  333. }
  334. return new $dbObject($this);
  335. }
  336. // Transaction control and logging
  337. protected function begin()
  338. {
  339. $this->db_connect();
  340. return $this->dbh->begin();
  341. }
  342. // Transaction control and logging - commit refused if error() has been called!!
  343. protected function commit()
  344. {
  345. return empty($this->error) && $this->dbh->commit();
  346. }
  347. // Transaction control and logging
  348. protected function rollback()
  349. {
  350. if ($this->dbh) {
  351. return $this->dbh->rollback();
  352. }
  353. }
  354. }
  355. ?>