Mercurial > SimpleWebPresenter
comparison common-functions.inc @ 33:511b6514823f
Remove more globals.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 04 Oct 2012 21:05:11 +0200 |
| parents | 9dab5b96b789 |
| children | ca76d31b7d48 |
comparison
equal
deleted
inserted
replaced
| 32:7b19be62ea94 | 33:511b6514823f |
|---|---|
| 1 <?php | 1 <?php |
| 2 include_once 'http-response-status-codes.inc'; | 2 include_once 'http-response-status-codes.inc'; |
| 3 | 3 |
| 4 function loadFile($sFilename, $sCharset = 'UTF-8') | 4 function loadFile($sFilename, $sCharset = 'UTF-8') |
| 5 { | 5 { |
| 6 if (floatval(phpversion()) >= 4.3) { | 6 if (floatval(phpversion()) >= 4.3) { |
| 7 if (!file_exists($sFilename)) return -3; | 7 if (!file_exists($sFilename)) |
| 8 $sData = file_get_contents($sFilename); | 8 return -3; |
| 9 } else { | 9 $sData = file_get_contents($sFilename); |
| 10 if (!file_exists($sFilename)) return -3; | 10 } |
| 11 $rHandle = fopen($sFilename, 'r'); | 11 else { |
| 12 if (!$rHandle) return -2; | 12 if (!file_exists($sFilename)) |
| 13 | 13 return -3; |
| 14 $sData = ''; | 14 $rHandle = fopen($sFilename, 'r'); |
| 15 while(!feof($rHandle)) | 15 if (!$rHandle) |
| 16 $sData .= fread($rHandle, filesize($sFilename)); | 16 return -2; |
| 17 fclose($rHandle); | 17 |
| 18 $sData = ''; | |
| 19 while(!feof($rHandle)) | |
| 20 $sData .= fread($rHandle, filesize($sFilename)); | |
| 21 fclose($rHandle); | |
| 22 } | |
| 23 if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) { | |
| 24 if ($sEncoding != 1) { | |
| 25 $sData = mb_convert_encoding($sData, $sCharset, $sEncoding); | |
| 18 } | 26 } |
| 19 if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) { | 27 } |
| 20 if ($sEncoding != 1) { | 28 return $sData; |
| 21 $sData = mb_convert_encoding($sData, $sCharset, $sEncoding); | |
| 22 } | |
| 23 } | |
| 24 return $sData; | |
| 25 } | 29 } |
| 26 | 30 |
| 27 function errorPage($errorText, $errorCode=403) | 31 function errorPage($errorText, $errorCode = 403) |
| 28 { | 32 { |
| 29 header(StatusCodes::httpHeaderFor($errorCode)); | 33 header(StatusCodes::httpHeaderFor($errorCode)); |
| 30 print "<div id=\"page\"><h1>${errorText}</h1></div>"; | 34 print "<div id=\"page\"><h1>${errorText}</h1></div>"; |
| 31 exit; | 35 exit; |
| 32 } | 36 } |
| 33 | 37 |
| 34 function genUrl($keys=array()) { | 38 function genUrl($urlParams, $keys = array()) { |
| 35 $out="?"; | 39 $out = "?"; |
| 36 $first=1; | 40 $first = 1; |
| 37 $URL_PARAMS=$GLOBALS['URL_PARAMS']; | 41 $new_params = $urlParams; |
| 38 $new_params=array(); | 42 foreach($keys as $param => $val) { |
| 39 foreach($URL_PARAMS as $param) { | 43 $new_params[$param] = $val; |
| 40 $new_params[$param]=$GLOBALS[$param]; | 44 } |
| 41 } | 45 foreach($new_params as $param => $val) { |
| 42 foreach($keys as $param => $val) { | 46 if($first) |
| 43 $new_params[$param]=$val; | 47 $first = 0; |
| 44 } | 48 else |
| 45 foreach($new_params as $param => $val) { | 49 $out .= "&"; |
| 46 if($first) | 50 $out .= urlencode($param) . '=' . urlencode($val); |
| 47 $first=0; | 51 } |
| 48 else | 52 return $out; |
| 49 $out.="&"; | |
| 50 $out.=urlencode($param).'='.urlencode($val); | |
| 51 } | |
| 52 return $out; | |
| 53 } | 53 } |
| 54 | 54 |
| 55 function getElementByTagName($obj, $name) { | 55 function getElementByTagName($obj, $name) { |
| 56 $elems=$obj->getElementsByTagName($name); | 56 $elems = $obj->getElementsByTagName($name); |
| 57 if ($elems->length != 1) { | 57 if ($elems->length != 1) { |
| 58 exit; | 58 exit; |
| 59 } | 59 } |
| 60 $elem=$elems->item(0); | 60 $elem = $elems->item(0); |
| 61 return $elem; | 61 return $elem; |
| 62 } | 62 } |
| 63 | 63 |
| 64 function getXmlContent($node) | 64 function getXmlContent($node) |
| 65 { | 65 { |
| 66 $text=$node->ownerDocument->saveXml($node); | 66 $text = $node->ownerDocument->saveXml($node); |
| 67 $pattern="/^<".$node->tagName."[^>]*>/is"; | 67 $pattern = "/^<" . $node->tagName."[^>]*>/is"; |
| 68 $text=preg_replace($pattern, '' , $text); | 68 $text = preg_replace($pattern, '' , $text); |
| 69 $pattern='/<\/'.$node->tagName.'[^>]*>$/is'; | 69 $pattern = '/<\/' . $node->tagName . '[^>]*>$/is'; |
| 70 $text=preg_replace($pattern, '' , $text); | 70 $text = preg_replace($pattern, '' , $text); |
| 71 | 71 |
| 72 return $text; | 72 return $text; |
| 73 } | 73 } |
| 74 | 74 |
| 75 ?> | 75 ?> |
