File indexing completed on 2024-12-22 05:36:34

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Crypt
0017  * @subpackage Math
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Support for arbitrary precision mathematics in PHP.
0025  *
0026  * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp
0027  * and big_int. Since each offer similar functionality, but availability of
0028  * each differs across installations of PHP, this wrapper attempts to select
0029  * the fastest option available and encapsulate a subset of its functionality
0030  * which all extensions share in common.
0031  *
0032  * This class requires one of the three extensions to be available. BCMATH
0033  * while the slowest, is available by default under Windows, and under Unix
0034  * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp
0035  * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp"
0036  * flag. BIG_INT support is available from a big_int PHP library available from
0037  * only from PECL (a Windows port is not available).
0038  *
0039  * @category   Zend
0040  * @package    Zend_Crypt
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_Crypt_Math_BigInteger
0045 {
0046 
0047     /**
0048      * Holds an instance of one of the three arbitrary precision wrappers.
0049      *
0050      * @var Zend_Crypt_Math_BigInteger_Interface
0051      */
0052     protected $_math = null;
0053 
0054     /**
0055      * Constructor; a Factory which detects a suitable PHP extension for
0056      * arbitrary precision math and instantiates the suitable wrapper
0057      * object.
0058      *
0059      * @param string $extension
0060      * @throws  Zend_Crypt_Math_BigInteger_Exception
0061      */
0062     public function __construct($extension = null)
0063     {
0064         if ($extension !== null && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) {
0065             // require_once('Zend/Crypt/Math/BigInteger/Exception.php');
0066             throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint');
0067         }
0068         $this->_loadAdapter($extension);
0069     }
0070 
0071     /**
0072      * Redirect all public method calls to the wrapped extension object.
0073      *
0074      * @param  string $methodName
0075      * @param  array  $args
0076      * @return mixed
0077      * @throws Zend_Crypt_Math_BigInteger_Exception
0078      */
0079     public function __call($methodName, $args)
0080     {
0081         if(!method_exists($this->_math, $methodName)) {
0082             // require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
0083             throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
0084         }
0085         return call_user_func_array(array($this->_math, $methodName), $args);
0086     }
0087 
0088     /**
0089      * @param string $extension
0090      * @throws  Zend_Crypt_Math_BigInteger_Exception
0091      */
0092     protected function _loadAdapter($extension = null)
0093     {
0094         if ($extension === null) {
0095             if (extension_loaded('gmp')) {
0096                 $extension = 'gmp';
0097             //} elseif (extension_loaded('big_int')) {
0098             //    $extension = 'big_int';
0099             } else {
0100                 $extension = 'bcmath';
0101             }
0102         }
0103         if($extension == 'gmp' && extension_loaded('gmp')) {
0104             // require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
0105             $this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
0106         //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
0107         //    // require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
0108         //    $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
0109         } elseif ($extension == 'bcmath' && extension_loaded('bcmath')) {
0110             // require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
0111             $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
0112         } else {
0113             // require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
0114             throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');
0115         }
0116     }
0117 
0118 }