+ }
+ } else {
+ $sThreadResponse = "";
+ }
+ unset($sRead);
+
+ if ($response !== 'OK') {
+ return false;
+ }
+
+ /* Example response
+ * S: * THREAD (2)(3 6 (4 23)(44 7 96))
+ * -- 2
+ *
+ * -- 3
+ * \-- 6
+ * |-- 4
+ * | \-- 23
+ * |
+ * \-- 44
+ * \-- 7
+ * \-- 96
+ */
+/*
+ * Notes for future work:
+ * indent_array should contain: indent_level, parent and flags,
+ * sibling nodes ..
+ * To achieve that we need to define the following flags:
+ * 0: hasnochildren
+ * 1: haschildren
+ * 2: is first
+ * 4: is last
+ * a node has sibling nodes if it's not the last node
+ * a node has no sibling nodes if it's the last node
+ * By using binary comparations we can store the flag in one var
+ *
+ * example:
+ * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
+ * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst)
+ * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast)
+ * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast)
+ * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast)
+ */
+
+ $j = 0;
+ $k = 0;
+ $l = 0;
+ $aUidThread = array();
+ $aIndent = array();
+ $aUidSubThread = array();
+ $aDepthStack = array();
+ $sUid = '';
+
+ if ($sThreadResponse) {
+ for ($i=0,$iCnt = strlen($sThreadResponse);$i<$iCnt;++$i) {
+ $cChar = $sThreadResponse{$i};
+ switch ($cChar) {
+ case '(': // new sub thread
+ // correction for a subthread of a thread with no parents in thread
+ if (!count($aUidSubThread) && $j > 0) {
+ --$l;
+ }
+ $aDepthStack[$j] = $l;
+ ++$j;
+ break;
+ case ')': // close sub thread
+ if($sUid !== '') {
+ $aUidSubThread[] = $sUid;
+ $aIndent[$sUid] = $j + $l - 1;
+ ++$l;
+ $sUid = '';
+ }
+ --$j;
+ if ($j === 0) {
+ // show message that starts the thread first.
+ $aUidSubThread = array_reverse($aUidSubThread);
+ // do not use array_merge because it's extremely slow and is causing timeouts
+ foreach ($aUidSubThread as $iUid) {
+ $aUidThread[] = $iUid;
+ }
+ $aUidSubThread = array();
+ $l = 0;
+ $aDepthStack = array();
+ } else {
+ $l = $aDepthStack[$j];