diff 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
line wrap: on
line diff
--- a/common-functions.inc	Mon Oct 08 02:01:04 2012 +0200
+++ b/common-functions.inc	Mon Oct 08 02:02:09 2012 +0200
@@ -44,20 +44,35 @@
   exit;
 }
 
-function genUrl($urlParams, $keys = array()) {
-  $out = "?";
+function genUrl($urlParams, $keys = array(), $nonQueryParams = array()) {
+  $out = '';
   $first = 1;
   $new_params = $urlParams;
   foreach($keys as $param => $val) {
     $new_params[$param] = $val;
   }
+
+  foreach($nonQueryParams as $nqp) {
+    if (array_key_exists($nqp, $new_params)) {
+      $val = $new_params[$nqp];
+      if ($val)
+	$out .= "/${val}";
+      unset($new_params[$nqp]);
+    }
+  }
+
   foreach($new_params as $param => $val) {
-    if($first)
-      $first = 0;
-    else
-      $out .= "&amp;";
-    $out .= urlencode($param) . '=' . urlencode($val);
+    if ($val) {
+      if($first) {
+	$first = 0;
+	$out .= "?";
+      }
+      else
+	$out .= "&amp;";
+      $out .= urlencode($param) . '=' . urlencode($val);
+    }
   }
+
   return $out;
 }
 
@@ -81,4 +96,45 @@
   return $text;
 }
 
+function startswith($haystack, $needle)
+{
+    return strpos($haystack, $needle) === 0;
+}
+
+function endsWith($haystack, $needle)
+{
+  $l = strlen($haystack) - strlen($needle);
+  return strrpos($haystack, $needle) === $l;
+}
+
+function getHeaders($url)
+{
+  $response = http_head($url, array("timeout"=>1), $info);
+  $headers = array();
+  $str = explode("\n", $response);
+  foreach($str as $kv) {
+    $p = strpos($kv, ":");
+    if ($p) {
+      $key = substr($kv, 0, $p);
+      $value = trim(substr($kv, $p + 1));
+      $headers[$key] = $value;
+    }
+  }
+  return $headers;
+}
+
+function opttostring($opts)
+{
+  $str = '';
+  foreach (array_keys($opts) as $key) {
+    $value = $opts[$key];
+    if ($str) {
+      $str .= "&${key}=${value}";
+    }
+    else {
+      $str = "?${key}=${value}";
+    }
+  }
+  return $str;
+}
 ?>
\ No newline at end of file