summaryrefslogtreecommitdiffstats
path: root/inc/util.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2014-06-27 21:15:35 +0200
committerSimon Rettberg2014-06-27 21:15:35 +0200
commit16aacada0f64240b7ec35026f0e207b7d0fd37df (patch)
tree26f4b434cd8eb7d3cd0c8eaba6cff6d096213761 /inc/util.inc.php
parentAdded doxygen comments to Taskmanager class (diff)
downloadslx-admin-16aacada0f64240b7ec35026f0e207b7d0fd37df.tar.gz
slx-admin-16aacada0f64240b7ec35026f0e207b7d0fd37df.tar.xz
slx-admin-16aacada0f64240b7ec35026f0e207b7d0fd37df.zip
New SysConfig module for adding a logo
Diffstat (limited to 'inc/util.inc.php')
-rw-r--r--inc/util.inc.php99
1 files changed, 17 insertions, 82 deletions
diff --git a/inc/util.inc.php b/inc/util.inc.php
index ea28b4fa..45a6b684 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -79,88 +79,6 @@ class Util
}
/**
- * Common initialization for download and downloadToFile
- * Return file handle to header file
- */
- private static function initCurl($url, $timeout, &$head)
- {
- $ch = curl_init();
- if ($ch === false)
- Util::traceError('Could not initialize cURL');
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, ceil($timeout / 2));
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_AUTOREFERER, true);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 6);
- $tmpfile = '/tmp/' . mt_rand() . '-' . time();
- $head = fopen($tmpfile, 'w+b');
- if ($head === false)
- Util::traceError("Could not open temporary head file $tmpfile for writing.");
- curl_setopt($ch, CURLOPT_WRITEHEADER, $head);
- return $ch;
- }
-
- /**
- * Read 10kb from the given file handle, seek to 0 first,
- * close the file after reading. Returns data read
- */
- private static function getContents($fh)
- {
- fseek($fh, 0, SEEK_SET);
- $data = fread($fh, 10000);
- fclose($fh);
- return $data;
- }
-
- /**
- * Download file, obey given timeout in seconds
- * Return data on success, false on failure
- */
- public static function download($url, $timeout, &$code)
- {
- $ch = self::initCurl($url, $timeout, $head);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = curl_exec($ch);
- $head = self::getContents($head);
- if (preg_match('#^HTTP/\d+\.\d+ (\d+) #', $head, $out)) {
- $code = (int) $out[1];
- } else {
- $code = 999;
- }
- curl_close($ch);
- return $data;
- }
-
- /**
- * Download file, obey given timeout in seconds
- * Return true on success, false on failure
- */
- public static function downloadToFile($target, $url, $timeout, &$code)
- {
- $fh = fopen($target, 'wb');
- if ($fh === false)
- Util::traceError("Could not open $target for writing.");
- $ch = self::initCurl($url, $timeout, $head);
- curl_setopt($ch, CURLOPT_FILE, $fh);
- $res = curl_exec($ch);
- $head = self::getContents($head);
- curl_close($ch);
- fclose($fh);
- if ($res === false) {
- @unlink($target);
- return false;
- }
- if (preg_match('#^HTTP/\d+\.\d+ (\d+) #', $head, $out)) {
- $code = (int) $out[1];
- } else {
- $code = '999 ' . curl_error($ch);
- }
- return true;
- }
-
- /**
* Convert given number to human readable file size string.
* Will append Bytes, KiB, etc. depending on magnitude of number.
*
@@ -249,5 +167,22 @@ class Util
return true;
}
+
+ /**
+ * Return contents of given file as string, but only read up to maxBytes bytes.
+ *
+ * @param string $file file to read
+ * @param int $maxBytes maximum length to read
+ * @return boolean success or failure
+ */
+ public static function readFile($file, $maxBytes = 1000)
+ {
+ $fh = @fopen($file, 'rb');
+ if ($fh === false)
+ return false;
+ $data = fread($fh, $maxBytes);
+ fclose($fh);
+ return $data;
+ }
}