/**
  * Sets the text of the last error and logs it to the history logger
  *
  * @param  int               $log_priority      Priority of message (UNIX-type): 0: Emergency, 1: Alert, 2: Critical, 3: Error, 4: Warning, 5: Notice, 6: Info, 7: Debug
  * @param  cbpaidTable|null  $object            Object stored in database, so that table name of table and id of key can be stored with the error
  * @param  string            $logMessagePrefix  Error message prefix for the logged message (simple non-html text only): will be prepended with ': '
  * @param  string            $userMessage       Error message for user (simple non-html text only)
  */
 public static function setLogErrorMSG($log_priority, $object, $logMessagePrefix, $userMessage)
 {
     global $_CB_database;
     $logObject = new cbpaidHistory($_CB_database);
     $logText = $logMessagePrefix ? $logMessagePrefix . ($userMessage ? ': ' . $userMessage : '') : $userMessage;
     $logObject->logError($log_priority, $logText, $object);
     if ($userMessage) {
         cbpaidApp::getBaseClass()->_setErrorMSG($userMessage);
     }
 }
 /**
  * Sets the text of the last error and logs it to the history logger
  * @access private
  *
  * @param  int               $log_priority      Priority of message (UNIX-type): 0: Emergency, 1: Alert, 2: Critical, 3: Error, 4: Warning, 5: Notice, 6: Info, 7: Debug
  * @param  cbpaidTable|null  $object            Object stored in database, so that table name of table and id of key can be stored with the error
  * @param  string            $logMessagePrefix  Error message prefix for the logged message (simple non-html text only): will be prepended with ': '
  * @param  string            $userMessage       Error message for user (simple non-html text only)
  */
 protected function _setLogErrorMSG($log_priority, $object, $logMessagePrefix, $userMessage)
 {
     global $_CB_database;
     $logObject = new cbpaidHistory($_CB_database);
     $logText = $logMessagePrefix ? $logMessagePrefix . ($userMessage ? ': ' . $userMessage : '') : $userMessage;
     $logObject->logError($log_priority, $logText, $object);
     if ($userMessage) {
         $this->_setErrorMSG($userMessage);
     }
 }
 /**
  * Error Handling function of CBSubs to give as argument for set_error_handler
  * @deprecated : Use cbpaidErrorHandler::init() to set it.
  *
  * @param $errno
  * @param string $errstr
  * @param string $errfile
  * @param string $errline
  * @return bool
  */
 public static function _error_handler_callable($errno, $errstr = '', $errfile = '', $errline = '')
 {
     if (self::$handlerOff || defined('E_STRICT') && $errno == constant('E_STRICT')) {
         return false;
     }
     global $_CB_framework, $_CB_database;
     $cfg['adminEmail'] = null;
     // if error has been supressed with an @
     if (error_reporting() == 0) {
         return false;
     }
     // check if function has been called by an exception
     if (func_num_args() == 5) {
         // called by trigger_error()
         list($errno, $errstr, $errfile, $errline) = func_get_args();
         $backtrace = debug_backtrace();
     } else {
         // caught exception
         /** @var $exc Exception */
         $exc = func_get_arg(0);
         $errno = $exc->getCode();
         $errstr = $exc->getMessage();
         $errfile = $exc->getFile();
         $errline = $exc->getLine();
         $backtrace = array_reverse($exc->getTrace());
     }
     $errorType = array(E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'PARSING ERROR', E_NOTICE => 'NOTICE', E_CORE_ERROR => 'CORE ERROR', E_CORE_WARNING => 'CORE WARNING', E_COMPILE_ERROR => 'COMPILE ERROR', E_COMPILE_WARNING => 'COMPILE WARNING', E_USER_ERROR => 'USER ERROR', E_USER_WARNING => 'USER WARNING', E_USER_NOTICE => 'USER NOTICE');
     if (defined('E_STRICT')) {
         // php 5
         $errorType[E_STRICT] = 'STRICT NOTICE';
     }
     if (defined('E_RECOVERABLE_ERROR')) {
         // php 5.1.6 + 5.2.x
         $errorType[E_RECOVERABLE_ERROR] = 'E_RECOVERABLE_ERROR';
     }
     $errorPriority = array(E_ERROR => 1, E_WARNING => 4, E_PARSE => 1, E_NOTICE => 5, E_CORE_ERROR => 1, E_CORE_WARNING => 4, E_COMPILE_ERROR => 1, E_COMPILE_WARNING => 4, E_USER_ERROR => 1, E_USER_WARNING => 4, E_USER_NOTICE => 5);
     if (defined('E_STRICT')) {
         $errorPriority[E_STRICT] = 6;
     }
     if (defined('E_RECOVERABLE_ERROR')) {
         $errorPriority[E_RECOVERABLE_ERROR] = 6;
     }
     // create error message
     if (array_key_exists($errno, $errorType)) {
         $err = $errorType[$errno];
     } else {
         $err = 'CAUGHT EXCEPTION';
     }
     $errMsg = $err . ': ' . $errstr . ' in ' . $errfile . ' on line ' . $errline;
     // start backtrace:
     $trace = '';
     foreach ($backtrace as $v) {
         if (isset($v['class'])) {
             $trace .= 'called in class ' . $v['class'] . '::' . $v['function'] . '(';
             if (isset($v['args'])) {
                 $separator = '';
                 foreach ($v['args'] as $arg) {
                     $trace .= $separator . self::cbpaidGetArgument($arg);
                     $separator = ', ';
                 }
             }
             $trace .= ')';
             if (isset($v['line'])) {
                 $trace .= ' on line ' . $v['line'];
             }
             if (isset($v['file'])) {
                 $trace .= ' in file ' . substr(strrchr($v['file'], '/'), 1);
             }
         } elseif (isset($v['function'])) {
             if (strtolower($v['function']) != strtolower(__FUNCTION__)) {
                 $trace .= 'called in function ' . $v['function'] . '(';
                 if (!empty($v['args'])) {
                     $separator = '';
                     foreach ($v['args'] as $arg) {
                         $trace .= $separator . self::cbpaidGetArgument($arg);
                         $separator = ', ';
                     }
                 }
                 $trace .= ')';
                 if (isset($v['line'])) {
                     $trace .= ' on line ' . $v['line'];
                 }
                 if (isset($v['file'])) {
                     $trace .= ' in file ' . substr(strrchr($v['file'], '/'), 1);
                 }
             }
         } else {
             $trace .= '????';
             $trace .= '::::::' . var_export($v, true);
         }
         $trace .= "\n";
     }
     $trace .= '$_GET = ' . var_export($_GET, true) . "\n";
     $trace .= '$_POST = ' . var_export($_POST, true) . "\n";
     $errorText = $errMsg . "\n" . 'Trace:' . $trace . "\n";
     // display error msg, if debug is enabled
     if ($_CB_framework->getCfg('debug')) {
         if (defined('E_STRICT') && $errno != constant('E_STRICT')) {
             echo '<h2>CBPaid Debug Message</h2>' . nl2br($errMsg) . '<br />
 	        Trace:' . nl2br($trace) . '<br />';
         }
     }
     // what to do
     switch ($errno) {
         //    	case E_STRICT:	break;		// only if it's defined (php 4 compatibility)
         //        case E_NOTICE:
         //        case E_USER_NOTICE:
         //        	break;
         default:
             if (array_key_exists($errno, $errorPriority)) {
                 $priority = $errorPriority[$errno];
             } else {
                 $priority = 7;
             }
             $log = new cbpaidHistory($_CB_database);
             $errorTextForMe = strpos($errorText, 'cbpaidsubscriptions') !== false && strpos($errorText, 'parainvite') === false;
             if ($errorTextForMe) {
                 $log->logError($priority, $errorText, null);
             }
             if (TRUE || !$_CB_framework->getCfg('debug')) {
                 // send email to admin
                 if ($errorTextForMe && !empty($cfg['adminEmail'])) {
                     $params = cbpaidApp::settingsParams();
                     $licensee_name = $params->get('licensee_name');
                     @mail($cfg['adminEmail'], cbpaidApp::version() . ' error on ' . $_SERVER['HTTP_HOST'] . ' customer:' . $licensee_name, 'CBPaid Debug Message: ' . $errorText, 'From: error_handler');
                 }
                 if ($priority == 1) {
                     // end and display error msg
                     exit(self::cbDisplayClientMessage());
                 }
             } else {
                 exit('<p>aborting.</p>');
             }
             break;
     }
     return false;
 }
	/**
	* Saves the registration tab/area postdata into the tab's permanent storage
	*
	* @param  UserTable  $row        reflecting the user being registered
	* @param  UserTable  $rowExtras  old duplicate
	* @return boolean                true if ok, false if ErrorMSG generated
	*/
	public function onBeforeUserRegistration( &$row, &$rowExtras ) {
		global $_CB_database, $_POST, $_PLUGINS, $ueConfig;
		cbpaidErrorHandler::on();

		$params									=	$this->params;
		$registrationPlansEnabled				=	$params->get( 'registrationPlansEnabled', 0 );
		$enableFreeRegisteredUser				=	$params->get( 'enableFreeRegisteredUser', 1 );
		
		$result = true;
		if ( $registrationPlansEnabled ) {
			$chosenPlans						=	$this->_getRegistrationChoosenPlans( $row );		// keep chosen plans including _options for onAfterUserRegistration
			if ( is_array( $chosenPlans ) ) {									// no more exclusive-only plans: && ( count( $chosenPlans ) > 0 ) ) {
				if ( ( count( $chosenPlans ) > 0 ) || ( $enableFreeRegisteredUser ) ) {
					$approvedOverride			=	null;
					$confirmedOverride			=	null;
					$free						=	false;
					$this->_checkRegistrationOverridesFree( $chosenPlans, $free, $approvedOverride, $confirmedOverride );
					if ( $approvedOverride !== null ) {
						$rowExtras->approved	=	$approvedOverride;
						$ueConfig['reg_admin_approval']	=	( $approvedOverride ? '0' : '1' );
					}
					if ( $confirmedOverride !== null ) {
						$rowExtras->confirmed	=	$confirmedOverride;
						$ueConfig['reg_confirmation']		=	( $confirmedOverride ? '0' : '1' );
					}
					if ( ! $free ) {
						// non-free plan: force workflow to auto-confirm + auto-approve, but block until payment completed:
						$row->block				=	( ( $enableFreeRegisteredUser && $rowExtras->approved && $rowExtras->confirmed ) ? '0' : '1' );
					}
				} else {
					$errorMsg					=	CBPTXT::T("Registration is enabled, free registrations are not allowed, but no subscription plan is available for registration");
					$_PLUGINS->raiseError(0);
					$_PLUGINS->_setErrorMSG( sprintf( CBPTXT::T("Sorry, %s. Please contact site administrator."), $errorMsg ) );
					$log						=	new cbpaidHistory( $_CB_database );
					$null						=	null;
					$log->logError( 3, CBPTXT::T("User error") .' : ' . CBPTXT::T("Configuration does not make sense") . ': ' . $errorMsg, $null );
				}
			} else {
				$_PLUGINS->raiseError(0);
				if ( is_array( $chosenPlans ) ) {
					$subTxt						=	CBPTXT::T( $params->get( 'subscription_name', 'subscription' ) );
					$_PLUGINS->_setErrorMSG( sprintf( CBPTXT::T("Please choose your %s plan."), $subTxt ) );
				} else {
					$_PLUGINS->_setErrorMSG( $chosenPlans );
				}
				$result  = false;
			}
		}
		cbpaidErrorHandler::off();
		return $result;
	}