aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lib.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib.php')
-rw-r--r--lib/lib.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/lib.php b/lib/lib.php
new file mode 100644
index 0000000..1258bfa
--- /dev/null
+++ b/lib/lib.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Various imported functions, under: CC-By-SA, CC-By.
+ * See each function comments for details.
+*/
+
+/**
+ * Convert size in bytes in human readable format.
+ *
+ * Taken from http://stackoverflow.com/a/2510459/204910
+ * licensed / CC-By-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/),
+ * (c) Mef and various contributors,
+ *
+ * itself sourced from http://php.net/manual/en/function.filesize.php
+ * so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/),
+ * (c) the PHP Documentation Group
+ *
+ * @param integer $bytes size in bytes
+ * @param integer $precision
+ *
+ * @return string
+*/
+function formatBytes($bytes, $precision = 2) {
+ $units = array('B', 'KB', 'MB', 'GB', 'TB');
+
+ $bytes = max($bytes, 0);
+ $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
+ $pow = min($pow, count($units) - 1);
+
+ // Uncomment one of the following alternatives
+ // $bytes /= pow(1024, $pow);
+ $bytes /= (1 << (10 * $pow));
+
+ return round($bytes, $precision) . ' ' . $units[$pow];
+}
+
+
+/**
+ * Properly dump $assoc_arr into a .ini file, with or without sections.
+ *
+ * Taken from http://php.net/manual/en/function.parse-ini-file.php
+ * so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/),
+ * (c) the PHP Documentation Group
+ *
+ * @param array $assoc_arr data to dump
+ * @param string $path where to write the data
+ * @param boolean $has_sections
+ *
+ * @return boolean
+*/
+function write_ini_file($assoc_arr, $path, $has_sections = FALSE)
+{
+ $content = '';
+ if ($has_sections) {
+ foreach ($assoc_arr as $key => $elem) {
+ $content .= "\n;\n[" . $key . "]\n";
+ foreach ($elem as $key2 => $elem2) {
+ if (is_array($elem2)) {
+ for ($i = 0; $i < count($elem2); $i++)
+ $content .= $key2 . '[] = "' . $elem2[$i] . "\"\n";
+ }
+ elseif ( $elem2 == '')
+ $content .= $key2 ." = \n";
+ else
+ $content .= $key2 . ' = "' . $elem2 . "\"\n";
+ }
+ }
+ } else {
+ foreach ($assoc_arr as $key => $elem) {
+ if (is_array($elem)) {
+ for ($i = 0; $i < count($elem); $i++)
+ $content .= $key2."[] = \"".$elem[$i]."\"\n";
+ }
+ elseif ($elem == '')
+ $content .= $key2." = \n";
+ else
+ $content .= $key2." = \"".$elem."\"\n";
+ }
+ }
+
+ if (!$handle = fopen($path, 'w')) {
+ return false;
+ }
+ if (!fwrite($handle, $content)) {
+ return false;
+ }
+ fclose($handle);
+
+ return true;
+}
+
+