comparison common-functions.inc @ 45:6c2c6acba30c

Support for hardcoding parameters.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Mon, 08 Oct 2012 02:02:09 +0200
parents fbbb82ced6de
children 15879e2aab65
comparison
equal deleted inserted replaced
44:79f708a48a7c 45:6c2c6acba30c
42 header(StatusCodes::httpHeaderFor($errorCode)); 42 header(StatusCodes::httpHeaderFor($errorCode));
43 print "<div id=\"page\"><h1>${errorText}</h1></div>"; 43 print "<div id=\"page\"><h1>${errorText}</h1></div>";
44 exit; 44 exit;
45 } 45 }
46 46
47 function genUrl($urlParams, $keys = array()) { 47 function genUrl($urlParams, $keys = array(), $nonQueryParams = array()) {
48 $out = "?"; 48 $out = '';
49 $first = 1; 49 $first = 1;
50 $new_params = $urlParams; 50 $new_params = $urlParams;
51 foreach($keys as $param => $val) { 51 foreach($keys as $param => $val) {
52 $new_params[$param] = $val; 52 $new_params[$param] = $val;
53 } 53 }
54
55 foreach($nonQueryParams as $nqp) {
56 if (array_key_exists($nqp, $new_params)) {
57 $val = $new_params[$nqp];
58 if ($val)
59 $out .= "/${val}";
60 unset($new_params[$nqp]);
61 }
62 }
63
54 foreach($new_params as $param => $val) { 64 foreach($new_params as $param => $val) {
55 if($first) 65 if ($val) {
56 $first = 0; 66 if($first) {
57 else 67 $first = 0;
58 $out .= "&amp;"; 68 $out .= "?";
59 $out .= urlencode($param) . '=' . urlencode($val); 69 }
70 else
71 $out .= "&amp;";
72 $out .= urlencode($param) . '=' . urlencode($val);
73 }
60 } 74 }
75
61 return $out; 76 return $out;
62 } 77 }
63 78
64 function getElementByTagName($obj, $name) { 79 function getElementByTagName($obj, $name) {
65 $elems = $obj->getElementsByTagName($name); 80 $elems = $obj->getElementsByTagName($name);
79 $text = preg_replace($pattern, '' , $text); 94 $text = preg_replace($pattern, '' , $text);
80 95
81 return $text; 96 return $text;
82 } 97 }
83 98
99 function startswith($haystack, $needle)
100 {
101 return strpos($haystack, $needle) === 0;
102 }
103
104 function endsWith($haystack, $needle)
105 {
106 $l = strlen($haystack) - strlen($needle);
107 return strrpos($haystack, $needle) === $l;
108 }
109
110 function getHeaders($url)
111 {
112 $response = http_head($url, array("timeout"=>1), $info);
113 $headers = array();
114 $str = explode("\n", $response);
115 foreach($str as $kv) {
116 $p = strpos($kv, ":");
117 if ($p) {
118 $key = substr($kv, 0, $p);
119 $value = trim(substr($kv, $p + 1));
120 $headers[$key] = $value;
121 }
122 }
123 return $headers;
124 }
125
126 function opttostring($opts)
127 {
128 $str = '';
129 foreach (array_keys($opts) as $key) {
130 $value = $opts[$key];
131 if ($str) {
132 $str .= "&${key}=${value}";
133 }
134 else {
135 $str = "?${key}=${value}";
136 }
137 }
138 return $str;
139 }
84 ?> 140 ?>