+
+ /**
+ * Encodes raw data (almost any type) by "serializing" it and then pack it
+ * into a "binary format".
+ *
+ * @param $rawData Raw data (almost any type)
+ * @return $encoded Encoded data
+ */
+ protected function encodeData ($rawData) {
+ // Make sure no objects or resources pass through
+ assert(!is_object($rawData));
+ assert(!is_resource($rawData));
+
+ // First "serialize" it (json_encode() is faster than serialize())
+ $encoded = $this->packString(json_encode($rawData));
+
+ // And return it
+ return $encoded;
+ }
+
+ /**
+ * Pack a string into a "binary format". Please execuse me that this is
+ * widely undocumented. :-(
+ *
+ * @param $str Unpacked string
+ * @return $packed Packed string
+ * @todo Improve documentation
+ */
+ protected function packString ($str) {
+ // Debug message
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('str=' . $str . ' - CALLED!');
+
+ // First compress the string (gzcompress is okay)
+ $str = gzcompress($str);
+
+ // Init variable
+ $packed = '';
+
+ // And start the "encoding" loop
+ for ($idx = 0; $idx < strlen($str); $idx += $this->packingData[$this->archArrayElement]['step']) {
+ $big = 0;
+ for ($i = 0; $i < $this->packingData[$this->archArrayElement]['step']; $i++) {
+ $factor = ($this->packingData[$this->archArrayElement]['step'] - 1 - $i);
+
+ if (($idx + $i) <= strlen($str)) {
+ $ord = ord(substr($str, ($idx + $i), 1));
+
+ $add = $ord * pow(256, $factor);
+
+ $big += $add;
+
+ //print 'idx=' . $idx . ',i=' . $i . ',ord=' . $ord . ',factor=' . $factor . ',add=' . $add . ',big=' . $big . PHP_EOL;
+ } // END - if
+ } // END - for
+
+ $l = ($big & $this->packingData[$this->archArrayElement]['left']) >>$this->packingData[$this->archArrayElement]['factor'];
+ $r = $big & $this->packingData[$this->archArrayElement]['right'];
+
+ $chunk = str_pad(pack($this->packingData[$this->archArrayElement]['format'], $l, $r), 8, '0', STR_PAD_LEFT);
+ //* NOISY-DEBUG */ print 'big=' . $big . ',chunk('.strlen($chunk) . ')='.md5($chunk).PHP_EOL;
+
+ $packed .= $chunk;
+ } // END - for
+
+ // Return it
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('packed=' . $packed . ' - EXIT!');
+ return $packed;
+ }
+
+ /**
+ * Checks whether the given file/path is in open_basedir(). This does not
+ * gurantee that the file is actually readable and/or writeable. If you need
+ * such gurantee then please use isReadableFile() instead.
+ *
+ * @param $filePathName Name of the file/path to be checked
+ * @return $isReachable Whether it is within open_basedir()
+ */
+ public static function isReachableFilePath ($filePathName) {
+ // Is not reachable by default
+ $isReachable = FALSE;
+
+ // Get open_basedir parameter
+ $openBaseDir = ini_get('open_basedir');
+
+ // Is it set?
+ if (!empty($openBaseDir)) {
+ // Check all entries
+ foreach (explode(PATH_SEPARATOR, $openBaseDir) as $dir) {
+ // Check on existence
+ if (substr($filePathName, 0, strlen($dir)) == $dir) {
+ // Is reachable
+ $isReachable = TRUE;
+ } // END - if
+ } // END - foreach
+ } else {
+ // If open_basedir is not set, all is allowed
+ $isReachable = TRUE;
+ }
+
+ // Return status
+ return $isReachable;
+ }
+
+ /**
+ * Checks whether the give file is within open_basedir() (done by
+ * isReachableFilePath()), is actually a file and is readable.
+ *
+ * @param $fileName Name of the file to be checked
+ * @return $isReadable Whether the file is readable (and therefor exists)
+ */
+ public static function isReadableFile ($fileName) {
+ // Default is not readable
+ $isReadable = FALSE;
+
+ // Is within parameters, so check if it is a file and readable
+ $isReadable = ((self::isReachableFilePath($fileName)) && (file_exists($fileName)) && (is_file($fileName)) && (is_readable($fileName)));
+
+ // Return status
+ return $isReadable;
+ }