/**
  * check requirements
  *
  * @param array $data return theme variables
  * @return bool requirements met
  */
 private function _check_requirements(&$data)
 {
     // proceed variable check if all requirements are true. If any of them is false, proceed is set false.
     $bProceed = true;
     //lets be optimistic!
     /**
      * check image HTML template
      *
      * @param bool $result
      */
     function check_HTML_image($result)
     {
         $label = array('wrong', 'right');
         return sprintf('<img src="%s/installer/images/tick-%s.png" alt="Found" />', Yii::app()->baseUrl, $label[$result]);
     }
     function is_writable_recursive($dir)
     {
         $folder = opendir($dir);
         while ($file = readdir($folder)) {
             if ($file != '.' && $file != '..' && (!is_writable($dir . "/" . $file) || is_dir($dir . "/" . $file) && !is_writable_recursive($dir . "/" . $file))) {
                 closedir($folder);
                 return false;
             }
         }
         closedir($folder);
         return true;
     }
     /**
      * check for a specific PHPFunction, return HTML image
      *
      * @param string $function
      * @param string $image return
      * @return bool result
      */
     function check_PHPFunction($function, &$image)
     {
         $result = function_exists($function);
         $image = check_HTML_image($result);
         return $result;
     }
     /**
      * check if file or directory exists and is writeable, returns via parameters by reference
      *
      * @param string $path file or directory to check
      * @param int $type 0:undefined (invalid), 1:file, 2:directory
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_PathWriteable($path, $type, &$data, $base, $keyError, $bRecursive = false)
     {
         $result = false;
         $data[$base . 'Present'] = 'Not Found';
         $data[$base . 'Writable'] = '';
         switch ($type) {
             case 1:
                 $exists = is_file($path);
                 break;
             case 2:
                 $exists = is_dir($path);
                 break;
             default:
                 throw new Exception('Invalid type given.');
         }
         if ($exists) {
             $data[$base . 'Present'] = 'Found';
             if (!$bRecursive && is_writable($path) || $bRecursive && is_writable_recursive($path)) {
                 $data[$base . 'Writable'] = 'Writable';
                 $result = true;
             } else {
                 $data[$base . 'Writable'] = 'Unwritable';
             }
         }
         $result || ($data[$keyError] = true);
         return $result;
     }
     /**
      * check if file exists and is writeable, returns via parameters by reference
      *
      * @param string $file to check
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_FileWriteable($file, &$data, $base, $keyError)
     {
         return check_PathWriteable($file, 1, $data, $base, $keyError);
     }
     /**
      * check if directory exists and is writeable, returns via parameters by reference
      *
      * @param string $directory to check
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_DirectoryWriteable($directory, &$data, $base, $keyError, $bRecursive = false)
     {
         return check_PathWriteable($directory, 2, $data, $base, $keyError, $bRecursive);
     }
     //  version check
     if (version_compare(PHP_VERSION, '5.1.6', '<')) {
         $bProceed = !($data['verror'] = true);
     }
     // mbstring library check
     if (!check_PHPFunction('mb_convert_encoding', $data['mbstringPresent'])) {
         $bProceed = false;
     }
     // JSON library check
     if (!check_PHPFunction('json_encode', $data['bJSONPresent'])) {
         $bProceed = false;
     }
     // ** file and directory permissions checking **
     // config directory
     if (!check_DirectoryWriteable(Yii::app()->getConfig('rootdir') . '/application/config', $data, 'config', 'derror')) {
         $bProceed = false;
     }
     // templates directory check
     if (!check_DirectoryWriteable(Yii::app()->getConfig('tempdir') . '/', $data, 'tmpdir', 'tperror', true)) {
         $bProceed = false;
     }
     //upload directory check
     if (!check_DirectoryWriteable(Yii::app()->getConfig('uploaddir') . '/', $data, 'uploaddir', 'uerror', true)) {
         $bProceed = false;
     }
     // Session writable check
     $session = Yii::app()->session;
     /* @var $session CHttpSession */
     $sessionWritable = $session->get('saveCheck', null) === 'save';
     $data['sessionWritable'] = $sessionWritable;
     $data['sessionWritableImg'] = check_HTML_image($sessionWritable);
     if (!$sessionWritable) {
         // For recheck, try to set the value again
         $session['saveCheck'] = 'save';
         $bProceed = false;
     }
     // ** optional settings check **
     // gd library check
     if (function_exists('gd_info')) {
         $data['gdPresent'] = check_HTML_image(array_key_exists('FreeType Support', gd_info()));
     } else {
         $data['gdPresent'] = check_HTML_image(false);
     }
     // ldap library check
     check_PHPFunction('ldap_connect', $data['ldapPresent']);
     // php zip library check
     check_PHPFunction('zip_open', $data['zipPresent']);
     // zlib php library check
     check_PHPFunction('zlib_get_coding_type', $data['zlibPresent']);
     // imap php library check
     check_PHPFunction('imap_open', $data['bIMAPPresent']);
     return $bProceed;
 }
 /**
  * check requirements
  *
  * @param array $data return theme variables
  * @return bool requirements met
  */
 private function _check_requirements(&$aData)
 {
     // proceed variable check if all requirements are true. If any of them is false, proceed is set false.
     $bProceed = true;
     //lets be optimistic!
     /**
      * check image HTML template
      *
      * @param bool $result
      * @return string Span with check if $result is true; otherwise a span with warning
      */
     function check_HTML_image($result)
     {
         if ($result) {
             return "<span class='fa fa-check text-success' alt='right'></span>";
         } else {
             return "<span class='fa fa-exclamation-triangle text-danger' alt='wrong'></span>";
         }
     }
     function is_writable_recursive($sDirectory)
     {
         $sFolder = opendir($sDirectory);
         while ($sFile = readdir($sFolder)) {
             if ($sFile != '.' && $sFile != '..' && (!is_writable($sDirectory . "/" . $sFile) || is_dir($sDirectory . "/" . $sFile) && !is_writable_recursive($sDirectory . "/" . $sFile))) {
                 closedir($sFolder);
                 return false;
             }
         }
         closedir($sFolder);
         return true;
     }
     /**
      * check for a specific PHPFunction, return HTML image
      *
      * @param string $function
      * @param string $image return
      * @return bool result
      */
     function check_PHPFunction($sFunctionName, &$sImage)
     {
         $bExists = function_exists($sFunctionName);
         $sImage = check_HTML_image($bExists);
         return $bExists;
     }
     /**
      * check if file or directory exists and is writeable, returns via parameters by reference
      *
      * @param string $path file or directory to check
      * @param int $type 0:undefined (invalid), 1:file, 2:directory
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_PathWriteable($path, $type, &$aData, $base, $keyError, $bRecursive = false)
     {
         $bResult = false;
         $aData[$base . 'Present'] = 'Not Found';
         $aData[$base . 'Writable'] = '';
         switch ($type) {
             case 1:
                 $exists = is_file($path);
                 break;
             case 2:
                 $exists = is_dir($path);
                 break;
             default:
                 throw new Exception('Invalid type given.');
         }
         if ($exists) {
             $aData[$base . 'Present'] = 'Found';
             if (!$bRecursive && is_writable($path) || $bRecursive && is_writable_recursive($path)) {
                 $aData[$base . 'Writable'] = 'Writable';
                 $bResult = true;
             } else {
                 $aData[$base . 'Writable'] = 'Unwritable';
             }
         }
         $bResult || ($aData[$keyError] = true);
         return $bResult;
     }
     /**
      * check if file exists and is writeable, returns via parameters by reference
      *
      * @param string $file to check
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_FileWriteable($file, &$data, $base, $keyError)
     {
         return check_PathWriteable($file, 1, $data, $base, $keyError);
     }
     /**
      * check if directory exists and is writeable, returns via parameters by reference
      *
      * @param string $directory to check
      * @param string $data to manipulate
      * @param string $base key for data manipulation
      * @param string $keyError key for error data
      * @return bool result of check (that it is writeable which implies existance)
      */
     function check_DirectoryWriteable($directory, &$data, $base, $keyError, $bRecursive = false)
     {
         return check_PathWriteable($directory, 2, $data, $base, $keyError, $bRecursive);
     }
     //  version check
     if (version_compare(PHP_VERSION, '5.3.0', '<')) {
         $bProceed = !($aData['verror'] = true);
     }
     if (convertPHPSizeToBytes(ini_get('memory_limit')) / 1024 / 1024 < 64 && ini_get('memory_limit') != -1) {
         $bProceed = !($aData['bMemoryError'] = true);
     }
     // mbstring library check
     if (!check_PHPFunction('mb_convert_encoding', $aData['mbstringPresent'])) {
         $bProceed = false;
     }
     // JSON library check
     if (!check_PHPFunction('json_encode', $aData['bJSONPresent'])) {
         $bProceed = false;
     }
     // ** file and directory permissions checking **
     // config directory
     if (!check_DirectoryWriteable(Yii::app()->getConfig('rootdir') . '/application/config', $aData, 'config', 'derror')) {
         $bProceed = false;
     }
     // templates directory check
     if (!check_DirectoryWriteable(Yii::app()->getConfig('tempdir') . '/', $aData, 'tmpdir', 'tperror', true)) {
         $bProceed = false;
     }
     //upload directory check
     if (!check_DirectoryWriteable(Yii::app()->getConfig('uploaddir') . '/', $aData, 'uploaddir', 'uerror', true)) {
         $bProceed = false;
     }
     // Session writable check
     $session = Yii::app()->session;
     /* @var $session CHttpSession */
     $sessionWritable = $session->get('saveCheck', null) === 'save';
     $aData['sessionWritable'] = $sessionWritable;
     $aData['sessionWritableImg'] = check_HTML_image($sessionWritable);
     if (!$sessionWritable) {
         // For recheck, try to set the value again
         $session['saveCheck'] = 'save';
         $bProceed = false;
     }
     // ** optional settings check **
     // gd library check
     if (function_exists('gd_info')) {
         $aData['gdPresent'] = check_HTML_image(array_key_exists('FreeType Support', gd_info()));
     } else {
         $aData['gdPresent'] = check_HTML_image(false);
     }
     // ldap library check
     check_PHPFunction('ldap_connect', $aData['ldapPresent']);
     // php zip library check
     check_PHPFunction('zip_open', $aData['zipPresent']);
     // zlib php library check
     check_PHPFunction('zlib_get_coding_type', $aData['zlibPresent']);
     // imap php library check
     check_PHPFunction('imap_open', $aData['bIMAPPresent']);
     return $bProceed;
 }