/** * Returns bit length of number $num * * @param gmp resource $num * @return int * @access public */ function bitLen($num) { $tmp = $this->int2bin($num); $bit_len = _byte_strlen($tmp) * 8; $tmp = _byte_ord($tmp[_byte_strlen($tmp) - 1]); if (!$tmp) { $bit_len -= 8; } else { while (!($tmp & 0x80)) { $bit_len--; $tmp <<= 1; } } return $bit_len; }
/** * quoteadPrintableEncode() * * Encodes data to quoted-printable standard. * * @param $input The data to encode * @param $line_max Optional max line length. Should * not be more than 76 chars * * @access private */ function _quotedPrintableEncode($input, $line_max = 76) { $lines = preg_split("/\r?\n/", $input); $eol = MAIL_MIMEPART_CRLF; $escape = '='; $output = ''; while (list(, $line) = each($lines)) { $linlen = _byte_strlen($line); $newline = ''; for ($i = 0; $i < $linlen; $i++) { $char = _byte_substr($line, $i, 1); $dec = _byte_ord($char); if ($dec == 32 and $i == $linlen - 1) { // convert space at eol only $char = '=20'; } elseif ($dec == 9) { // Do nothing if a tab. } elseif ($dec == 61 or $dec < 32 or $dec > 126) { $char = $escape . _ml_strtoupper(sprintf('%02s', dechex($dec))); } if (_byte_strlen($newline) + _byte_strlen($char) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay $newline = ''; } $newline .= $char; } // end of for $output .= $newline . $eol; } $output = _byte_substr($output, 0, -1 * _byte_strlen($eol)); // Don't want last crlf return $output; }
/** * Logical Right Shift * * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder. * * @param $x String * @param $shift Integer * @return String * @access private */ function _base256_rshift(&$x, $shift) { if ($shift == 0) { $x = ltrim($x, _byte_chr(0)); return ''; } $num_bytes = $shift >> 3; // eg. floor($shift/8) $shift &= 7; // eg. $shift % 8 $remainder = ''; if ($num_bytes) { $start = $num_bytes > _byte_strlen($x) ? -_byte_strlen($x) : -$num_bytes; $remainder = _byte_substr($x, $start); $x = _byte_substr($x, 0, -$num_bytes); } $carry = 0; $carry_shift = 8 - $shift; for ($i = 0; $i < _byte_strlen($x); $i++) { $temp = _byte_ord($x[$i]) >> $shift | $carry; $carry = _byte_ord($x[$i]) << $carry_shift & 0xff; $x[$i] = _byte_chr($temp); } $x = ltrim($x, _byte_chr(0)); $remainder = _byte_chr($carry >> $carry_shift) . $remainder; return ltrim($remainder, _byte_chr(0)); }
function onAction() { global $application; $default_lng = modApiFunc('MultiLang', 'getDefaultLanguage'); $application->enterCriticalSection('ML_UpdateLabelData'); // getting the request data $request =& $application->getInstance('Request'); $lng = $request->getValueByKey('lng'); $label_data = $request->getValueByKey('label_data'); // flag if there is an error $error = ''; // checking the language if ($lng != $default_lng && !modApiFunc('MultiLang', 'checkLanguage', $lng, false)) { $error = 'ML_ERROR_INVALID_LANGUAGE'; $lng = modApiFunc('MultiLang', 'getDefaultLanguage'); } // checking the label id // and setting the label name for future use if ($label_data['id']) { $tmp = modApiFunc('MultiLang', 'searchLabels', array('label_id' => $label_data['id'], 'lng' => $lng)); if (!$tmp) { // label_id is incorrect $label_data['id'] = 0; $error = 'ML_ERROR_INVALID_LABEL'; $label_data['label'] = 'CUSTOM_'; $label_data['prefix'] = 'CZ'; } else { $label_data['label'] = $tmp[0]['label']; $label_data['prefix'] = $tmp[0]['prefix']; } // checking if the label is custom // while trying to change its name if (isset($label_data['custom_label']) && _ml_substr($tmp[0]['label'], 0, 7) != 'CUSTOM_') { $error = 'ML_ERROR_NOT_CUSTOM_LABEL'; } } else { $label_data['label'] = 'CUSTOM_'; $label_data['prefix'] = 'CZ'; } // checking the label name for custom labels if (!$error && (!$label_data['id'] || isset($label_data['custom_label'])) && !$label_data['custom_label']) { $error = 'ML_ERROR_EMPTY_CUSTOM_LABEL_NAME'; } // ckecking if custom_name contain invalid symbols if (!$error && !$label_data['id']) { for ($i = 0; $i < _byte_strlen($label_data['custom_label']); $i++) { $tmp = _byte_ord($label_data['custom_label'][$i]); if ($tmp < 48 || $tmp > 57 && $tmp < 65 || $tmp > 90 && $tmp != 95) { $error = 'ML_ERROR_INVALID_CUSTOM_LABEL_NAME'; break; } } } // checking if the label name is unique if (!$error && (!$label_data['id'] || isset($label_data['custom_label']))) { $tmp = modApiFunc('MultiLang', 'searchLabels', array('label' => array('exactly' => 'Y', 'value' => 'CUSTOM_' . $label_data['custom_label']), 'type' => 'CZ_CUSTOM', 'lng' => $lng)); if ($tmp && $tmp[0]['id'] != $label_data['id']) { $error = 'ML_ERROR_LABEL_EXISTS'; } } if (!$error) { // we are ready to save the changes if (!$label_data['id']) { // inserting a new label modApiFunc('Resources', 'addLabelToDB', 'CUSTOM_' . $label_data['custom_label'], $label_data['def_value']); $label_data['id'] = $application->db->DB_Insert_Id(); // saving the result message modApiFunc('Session', 'set', 'ResultMessage', 'ML_SUCCESS_LABEL_ADDED'); } else { // updating the label modApiFunc('Resources', 'updateLabelText', $label_data['id'], $label_data['def_value'], isset($label_data['custom_label']) ? 'CUSTOM_' . $label_data['custom_label'] : ''); modApiFunc('Session', 'set', 'ResultMessage', 'ML_SUCCESS_LABEL_UPDATED'); } // saving the multilang data if needed if ($lng != modApiFunc('MultiLang', 'getDefaultLanguage')) { $ml_label = modApiFunc('MultiLang', 'mapMLField', 'resource_labels', 'res_text', 'Resources'); modApiFunc('MultiLang', 'setMLValue', $ml_label, $label_data['id'], $label_data['value'], $lng); } modApiFunc('Session', 'set', 'ML_ReloadParentWindow', 'Y'); } else { // form contain an error, saving it to session modApiFunc('Session', 'set', 'SavedLabelData', $label_data); // saving the result modApiFunc('Session', 'set', 'ResultMessage', $error); } $application->leaveCriticalSection(); $req_to_redirect = new Request(); $req_to_redirect->setView('PopupWindow'); $req_to_redirect->setKey('page_view', 'LabelData'); $req_to_redirect->setKey('label_id', $label_data['id']); $req_to_redirect->setKey('lng', $lng); $application->redirect($req_to_redirect); }
/** * Sets the secret key * The key must be non-zero, and less than or equal to * 56 characters (bytes) in length. * * If you are making use of the PHP mcrypt extension, you must call this * method before each encrypt() and decrypt() call. * * @param string $key * @param string $iv 8-char initialization vector (required for CBC mode) * @return boolean|PEAR_Error Returns TRUE on success, PEAR_Error on failure * @access public * @ Fix the caching of the key */ function setKey($key, $iv = null) { if (!is_string($key)) { return PEAR::raiseError('Key must be a string', 2); } $len = _byte_strlen($key); if ($len > $this->_key_size || $len == 0) { return PEAR::raiseError('Key must be less than ' . $this->_key_size . ' characters (bytes) and non-zero. Supplied key length: ' . $len, 3); } if ($this->_iv_required) { if (_byte_strlen($iv) != $this->_iv_size) { return PEAR::raiseError('IV must be ' . $this->_iv_size . '-character (byte) long. Supplied IV length: ' . _byte_strlen($iv), 7); } $this->_iv = $iv; } // If same key passed, no need to re-initialize internal arrays. // @ This needs to be worked out better... if ($this->_keyHash == md5($key)) { return true; } $this->_init(); $k = 0; $data = 0; $datal = 0; $datar = 0; for ($i = 0; $i < 18; $i++) { $data = 0; for ($j = 4; $j > 0; $j--) { $data = $data << 8 | _byte_ord($key[$k]); $k = ($k + 1) % $len; } $this->_P[$i] ^= $data; } for ($i = 0; $i <= 16; $i += 2) { $this->_encipher($datal, $datar); $this->_P[$i] = $datal; $this->_P[$i + 1] = $datar; } for ($i = 0; $i < 256; $i += 2) { $this->_encipher($datal, $datar); $this->_S[0][$i] = $datal; $this->_S[0][$i + 1] = $datar; } for ($i = 0; $i < 256; $i += 2) { $this->_encipher($datal, $datar); $this->_S[1][$i] = $datal; $this->_S[1][$i + 1] = $datar; } for ($i = 0; $i < 256; $i += 2) { $this->_encipher($datal, $datar); $this->_S[2][$i] = $datal; $this->_S[2][$i + 1] = $datar; } for ($i = 0; $i < 256; $i += 2) { $this->_encipher($datal, $datar); $this->_S[3][$i] = $datal; $this->_S[3][$i + 1] = $datar; } $this->_keyHash = md5($key); return true; }
/** * Decrypts $enc_data by the key $this->_dec_key or $key. * * @param string $enc_data encrypted data as binary string * @param object $key decryption key (object of RSA_Crypt_Key class) * @return mixed * decrypted data as string on success or false on error * * @access public */ function decryptBinary($enc_data, $key = null) { if (is_null($key)) { // use current decryption key $key = $this->_dec_key; } elseif (!Crypt_RSA_Key::isValid($key)) { $obj = PEAR::raiseError('invalid decryption key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY); $this->pushError($obj); return false; } $exp = $this->_math_obj->bin2int($key->getExponent()); $modulus = $this->_math_obj->bin2int($key->getModulus()); $data_len = _byte_strlen($enc_data); $chunk_len = $key->getKeyLength() - 1; $block_len = (int) ceil($chunk_len / 8); $curr_pos = 0; $bit_pos = 0; $plain_data = $this->_math_obj->bin2int(""); while ($curr_pos < $data_len) { $tmp = $this->_math_obj->bin2int(_byte_substr($enc_data, $curr_pos, $block_len)); $tmp = $this->_math_obj->powmod($tmp, $exp, $modulus); $plain_data = $this->_math_obj->bitOr($plain_data, $tmp, $bit_pos); $bit_pos += $chunk_len; $curr_pos += $block_len; } $result = $this->_math_obj->int2bin($plain_data); // delete tail, containing of \x01 $tail = _byte_ord($result[_byte_strlen($result) - 1]); if ($tail != 1) { $obj = PEAR::raiseError("Error tail of decrypted text = {$tail}. Expected 1", CRYPT_RSA_ERROR_WRONG_TAIL); $this->pushError($obj); return false; } return _byte_substr($result, 0, -1); }