function __construct($warningLevelFile = './tmp/errorLevel.csv', $mailSettings = false, $cronSettings = false, $libPath = './lib/')
 {
     //=== includes and parameter validation ===//
     if (@(include_once $libPath . 'class.fileManager.inc.php')) {
         $pathValidator = new fileManager();
         if (!$pathValidator->validPath($warningLevelFile, false)) {
             throw new Exception('Invalid constructor - first parameter must be a valid file path.');
         } else {
             $this->warningLevelFile = $warningLevelFile;
         }
     } else {
         trigger_error('Unable to include fileManager for path validation.', E_USER_WARNING);
         if (!is_string($warningLevelFile)) {
             throw new Exception('Invalid constructor - first parameter must be a string (file path expected).');
         } else {
             $this->warningLevelFile = $warningLevelFile;
         }
     }
     if ($mailSettings != false) {
         if (!isset($mailSettings['server']) || !isset($mailSettings['subject']) || !isset($mailSettings['title']) || !isset($mailSettings['from']) || !isset($mailSettings['recipientsByLevel'])) {
             return false;
         } else {
             $this->mailSettings = $mailSettings;
         }
     } else {
         trigger_error('No valid mail settings found.', E_USER_NOTICE);
         $this->mailSettings = false;
     }
     if ($cronSettings != false) {
         if (!isset($cronSettings['computer']) || !isset($cronSettings['user']) || !isset($cronSettings['password'])) {
             return false;
         }
         if (@(include_once $libPath . 'class.cronManager.inc.php')) {
             $isWin = strpos(PHP_OS, 'WIN') === 0;
             //0, not false!;
             if ($isWin) {
                 $this->cronManager = new cronManager($cronSettings['password'], $cronSettings['computer'], $cronSettings['user']);
             } else {
                 if (empty($this->cronSettings['tmpFile'])) {
                     $this->cronSettings['tmpFile'] = '/tmp/.crontabTemp.txt';
                 }
                 $this->cronManager = new cronManager($cronSettings['tmpFile']);
             }
         }
     } else {
         trigger_error('No valid cron settings found.', E_USER_WARNING);
         $this->cronManager = false;
     }
     //=== includes only ===//
     if (!@(include_once $libPath . 'class.sensor.inc.php')) {
         throw new Exception('Unable to include class sensor for sensorCarer.');
     }
     //=== variable initialisation ===//
     $this->values = array();
     $this->libPath = $libPath;
     $this->internalErrorIdentifier = 'internalError';
 }
 function __construct($sensorListPath, $phpLocation = 'php', $libPath = './lib/', $settings = array())
 {
     //=== include libraries and validate parameter ===//
     if (!@(include_once $libPath . 'class.csvManager.inc.php')) {
         throw new Exception('Unable to include csvNabager for sensorManager.');
     }
     $this->csvManager = new csvManager();
     if (@(include_once $libPath . 'class.fileManager.inc.php')) {
         $pathValidator = new fileManager();
         if (!$pathValidator->validPath($sensorListPath, false)) {
             throw new Exception('Invalid constructor - first parameter must be a valid file path.');
         } else {
             $this->sensorListPath = $sensorListPath;
         }
     } else {
         trigger_error('Unable to include fileManager for path validation.', E_USER_WARNING);
         if (!is_string($sensorListPath)) {
             throw new Exception('Invalid constructor - first parameter must be a string (file path expected).');
         }
     }
     $this->isWin = strpos(PHP_OS, 'WIN') === 0;
     //0, not false!
     if (@(include_once $libPath . 'class.cronManager.inc.php')) {
         if ($this->isWin) {
             $this->cronManager = new cronManager($settings['password'], $settings['computer'], $settings['user']);
         } else {
             if (empty($settings['tmpFile'])) {
                 $settings['tmpFile'] = '/tmp/.crontabTemp.txt';
             }
             $this->cronManager = new cronManager($settings['tmpFile']);
         }
     } else {
         throw new Exception('Unable to include cronManager for path validation.');
     }
     //=== define attributes ===//
     $this->mandatoryColumns = array('id', 'type', 'target', 'green-rec-min', 'orange-rec-min', 'yellow-trigger-ms', 'orange-trigger-ms');
     $this->optionalColumns = array('source', 'user', 'pass', 'subpath', 'yellow-rec-min', 'red-rec-min', 'red-trigger-min');
     $this->idCol = $this->mandatoryColumns[0];
     $this->phpLocation = $phpLocation;
 }
 function write($file, $values, $keyCol = false)
 {
     if (empty($file)) {
         return false;
     }
     if (empty($values)) {
         return false;
     }
     //--- calculate 3rd parameter ---//
     $assoc = $this->forceAssoc || count(array_filter(array_keys($values), 'is_string'));
     //--- get name of temporary copy ---//
     $newFile = '';
     if (is_string($this->tmpFile)) {
         include_once $this->fileManagerPath;
         if (class_exists('fileManager')) {
             if (fileManager::validPath($this->tmpFile, false)) {
                 $newFile = $this->tmpFile;
             }
         }
     }
     if (empty($newFile)) {
         $newFile = $file . $this->tmpFileExt;
     }
     //--- initialize variables ---//
     $anyChanges = false;
     //In this context file appends are no changes: "$anyChanges" will stay "false".
     $overwrite = 0;
     //overwrite in this context means not only a line will be overwritten, but the key value matched.
     $error = false;
     //--- copy first row ---//
     //... read first row ...//
     if (!file_exists($file)) {
         $rowAsIs = array();
         $oldFileHandle = false;
         $newFileHandle = fopen($file, 'w');
     } else {
         $oldFileHandle = fopen($file, 'r+');
         $newFileHandle = false;
         if ($oldFileHandle) {
             $rowAsIs = fgetcsv($oldFileHandle, $this->rowLength, $this->delimiter, $this->enclosure, $this->escape);
             if ($rowAsIs === false) {
                 $rowAsIs = array();
             }
         } else {
             return false;
         }
     }
     //... calculate and write first row ...//
     $rowAsIsCount = count($rowAsIs);
     if ($assoc) {
         // assoc //
         $columnsToAdd = array();
         foreach (array_keys($values) as $colFromValues) {
             if (!in_array($colFromValues, $rowAsIs)) {
                 $columnsToAdd[] = $colFromValues;
             }
         }
         $anyChanges = (bool) count($columnsToAdd);
         $oldHead = $rowAsIs;
         $columns = array_merge($rowAsIs, $columnsToAdd);
         //copy/write head:
         try {
             $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile);
             $error = !$this->writeBuffered($columns, $newFileHandle);
         } catch (Exception $e) {
             $error = true;
         }
     } else {
         // not assoc //
         $valueCount = count($values);
         ksort($values, SORT_NUMERIC);
         if ($rowAsIsCount !== 0) {
             $anyChanges = $valueCount >= $rowAsIsCount;
             $colCount = max($valueCount, $rowAsIsCount);
             $firstKey = key($values);
             end($values);
             $lastKey = key($values);
             if ($firstKey < 0 || $lastKey >= $colCount) {
                 $valuesWithInvalidKeys = $values;
                 $values = array();
                 foreach ($valuesWithInvalidKeys as $value) {
                     $values[] = $value;
                 }
                 unset($valuesWithInvalidKeys);
             }
             $oldHead = range(0, $rowAsIsCount - 1);
             $columns = range(0, $colCount - 1);
             $rowToBe = $rowAsIs;
             for ($i = 0; $i < $valueCount - $rowAsIsCount; $i++) {
                 if (!isset($rowToBe[$i])) {
                     $rowToBe[$i] = $this->emptyContent;
                 }
                 if ($keyCol !== false && $i === $keyCol) {
                     $overwrite = -1;
                     break;
                 }
             }
             if ($overwrite === -1) {
                 $rowToBe = $values;
                 $lineIsEmpty = true;
                 for ($i = 0; $i < $valueCount - $rowAsIsCount; $i++) {
                     if (!isset($rowToBe[$i])) {
                         $rowToBe[$i] = $this->emptyContent;
                     } else {
                         if ($keyCol === false || $i !== $keyCol) {
                             $lineIsEmpty = false;
                         }
                     }
                 }
             } else {
                 $lineIsEmpty = false;
             }
             //copy/write first row:
             if (!$lineIsEmpty) {
                 try {
                     $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile);
                     $error = !$this->writeBufferedAssoc($rowToBe, $columns, $newFileHandle);
                 } catch (Exception $e) {
                     $error = true;
                 }
             } else {
                 $error = false;
             }
             if ($overwrite === -1) {
                 if ($error) {
                     $overwrite = 0;
                 } else {
                     $overwrite = 1;
                 }
             }
         }
     }
     //--- Copy old lines ---//
     $colCount = count($columns);
     while ($rowAsIsCount != 0 && !$error) {
         //... read next line ...//
         if ($assoc) {
             $rowAsIs = $this->fgetcsv_assoc($oldFileHandle, $oldHead);
         } else {
             $rowAsIs = fgetcsv($oldFileHandle, $this->rowLength, $this->delimiter, $this->enclosure, $this->escape);
         }
         if (!is_array($rowAsIs)) {
             $rowAsIs = array();
             $rowAsIsCount = 0;
         } else {
             $rowAsIsCount = count($rowAsIs);
         }
         if ($rowAsIsCount !== 0) {
             //... write (copy) line ...//
             $lineIsEmpty = false;
             $rowToWrite =& $rowAsIs;
             if ($keyCol !== false && $overwrite != 1 && isset($rowAsIs[$keyCol]) && $rowAsIs[$keyCol] === $values[$keyCol]) {
                 $rowToWrite = $this->overwriteEmpty($values, $rowAsIs);
                 $overwrite = -1;
                 //Check if this overwrite makes any changes on the file:
                 $lineIsEmpty = true;
                 foreach ($values as $key => &$value) {
                     if ($key != $keyCol && isset($rowAsIs[$key])) {
                         $lineIsEmpty = false;
                     }
                     if (!isset($rowAsIs[$key]) || $rowAsIs[$key] != $value) {
                         $anyChanges = true;
                         break;
                     }
                 }
             }
             if (!$lineIsEmpty) {
                 try {
                     $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile);
                     $error = !$this->writeBufferedAssoc($rowToWrite, $columns, $newFileHandle);
                 } catch (Exception $e) {
                     $error = true;
                 }
             }
             if ($overwrite === -1) {
                 if ($error) {
                     $overwrite = 0;
                 } else {
                     $overwrite = 1;
                 }
             }
             $anyChanges = $anyChanges || $colCount != $rowAsIsCount || $lineIsEmpty && $overwrite;
         } else {
             break;
         }
     }
     //--- Attach values --- //
     if (!$error) {
         if ($anyChanges) {
             if (!$overwrite) {
                 //Attach line to new file:
                 try {
                     $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile);
                     $error = !$this->writeBufferedAssoc($values, $columns, $newFileHandle);
                 } catch (Exception $e) {
                     $error = true;
                 }
             }
             if (!$error) {
                 //Write buffer now:
                 try {
                     $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile, true);
                     //force to open handle
                     $error = !$this->writeBuffered(true, $newFileHandle);
                     //force to write
                 } catch (Exception $e) {
                     $error = true;
                 }
             }
         } else {
             if (!$overwrite) {
                 //Now "overwrite" means it is overwritten or in this case: It isn't.
                 if (count($values) != 0 + ($keyCol !== false)) {
                     // line is not empty
                     //Write buffer now:
                     try {
                         $newFileHandle = $this->openHandleOnLastChance($newFileHandle, $newFile, true);
                         //force to open handle
                         $error = !$this->writeBuffered(true, $newFileHandle);
                         //force to write
                     } catch (Exception $e) {
                         $error = true;
                     }
                     if (!$error) {
                         //Attach new line
                         $this->fputcsv_assoc($oldFileHandle ?: $newFileHandle, $columns, $values);
                     }
                 }
             }
         }
     }
     //--- close & clean ---//
     if ($oldFileHandle) {
         fclose($oldFileHandle);
         if ($newFileHandle) {
             fclose($newFileHandle);
             if ($anyChanges && !$error) {
                 unlink($file);
                 rename($newFile, $file);
             } else {
                 unlink($newFile);
             }
         }
     } else {
         if ($newFileHandle) {
             //newFileHandle points on primary file path
             fclose($newFileHandle);
         }
     }
     return !$error;
 }
 private function _linConstruct($tempCronTablePath)
 {
     if (empty($tempCronTablePath)) {
         throw new Exception('This is no windows server, parameter 1 must be a path for a valid file.');
     }
     if (!(include $this->csvManagerPath)) {
         throw new Exception('Unable to include csvManager. csvManager is required because it\'s no windows server.');
     }
     if (@(include $this->fileManagerPath)) {
         if (class_exists('fileManager')) {
             if (!fileManager::validPath($tempCronTablePath, false, false)) {
                 //if its no valid file path
                 if (!fileManager::validPath($tempCronTablePath, true, false)) {
                     //and no valid directory
                     throw new Exception('This is no windows server, parameter 1 must a valid path of the cron table.');
                 } else {
                     $tempCronTablePath .= 'cronTemp.txt';
                     if (!fileManager::validPath($tempCronTablePath, false, false)) {
                         //if still no valid file path
                         throw new Exception('This is no windows server, parameter 1 must a valid path. Found directory path but an error occured.');
                     }
                 }
             }
         }
     }
     $this->tempCronTablePath = $tempCronTablePath;
     $this->csvManager = new csvManager('#', chr(219));
     $this->cronTableDelimiter = '~~~cronId:';
 }
 function validPathArray($paths = './', &$errorMessage)
 {
     $validKeys = array('all', 'year', 'month', 'day');
     //--- include used methods ---//
     include_once $this->fileManagerPath;
     if (!class_exists('fileManager')) {
         $errorMessage = 'Unable to include "fileManager" (path="' . $this->fileManagerPath . '")';
         return false;
     }
     //--- check syntax and return true or false ---//
     if (is_array($paths)) {
         foreach ($paths as $name => &$path) {
             $name_ci = strtolower($name);
             if (in_array($name_ci, $validKeys)) {
                 if (fileManager::validPath($path, false, $this->dateDelimiter)) {
                     if (!(substr_count($path, $this->dateDelimiter) % 2)) {
                         if ($name_ci != $name && !isset($paths[$name_ci])) {
                             $paths[$name_ci] = $path;
                         }
                         continue;
                     } else {
                         $errorMessage = 'Invalid amount of date delimiter ("' . $this->dateDelimiter . '") found: ' . $name . ' => ' . $path;
                         return false;
                     }
                 } else {
                     $errorMessage = 'Invalid file path found: ' . $name . ' => ' . $path;
                     return false;
                 }
             }
         }
         return $paths;
     } else {
         if (is_string($paths)) {
             if (fileManager::validPath($paths, true, $this->dateDelimiter)) {
                 $handle = fopen($paths . 'logManagerTestFile.txt', 'x');
                 if ($handle) {
                     $message = 'This is a test file, created';
                     $message .= ' by the script ' . $_SERVER['PHP_SELF'];
                     $message .= ' with the file' . __FILE__;
                     $message .= ' at ' . date('Y-m-d H:i:s e');
                     $message .= '.' . "\n";
                     $message .= 'That this file still exists is caused by a failure on file deletion.';
                     $message .= ' You can delete it unconcerned.';
                     $written = fwrite($handle, $message);
                     fclose($handle);
                     unlink($paths . 'logManagerTestFile.txt');
                 } else {
                     $written = false;
                 }
                 if (!$written) {
                     $errorMessage = 'Unable to write into given directory ("' . $paths . '").';
                     return false;
                 } else {
                     $newPaths = array('all' => $paths . 'years.csv', 'year' => $paths . $this->dateDelimiter . 'Y' . $this->dateDelimiter . '.csv', 'month' => $paths . $this->dateDelimiter . 'Y-F' . $this->dateDelimiter . '.csv', 'day' => $paths . $this->dateDelimiter . 'Y-m-d' . $this->dateDelimiter . '.csv');
                     return $newPaths;
                 }
             } else {
                 $errorMessage = 'Path is no valid directory (syntax)';
                 return false;
             }
         } else {
             $errorMessage = 'Invalid parameter: String or array expected, ' . gettype($paths) . ' given.';
             return false;
         }
     }
 }