Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 44 |
| IMAP | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
342.00 | |
0.00% |
0 / 44 |
| __construct | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 10 |
|||
| __destruct | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 10 |
|||
| open | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| __call | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 19 |
|||
| <?php | |
| namespace Luxian\Fetch\IMAP; | |
| use ReflectionFunction; | |
| /** | |
| * OOP wrapper over PHP native imap_* function | |
| * | |
| * @package Luxian\webmail | |
| * | |
| * @method getMessageCount() | |
| * @method uid() | |
| * @method fetchheader() | |
| * @method rfc822_parse_headers() | |
| * @method fetchstructure() | |
| * @method list() | |
| * @method reopen() | |
| * | |
| * @deprecated | |
| * @codeCoverageIgnore | |
| */ | |
| class IMAP | |
| { | |
| /** | |
| * @var bool|null; | |
| * TRUE or FALSE depending if PHP imap library is present or not. | |
| * NULL means the check wasn't done yet | |
| */ | |
| protected static $php_imap_library_loaded; | |
| /** | |
| * @var string | |
| * Server specifications as described in imap_open() | |
| */ | |
| protected $server_ref; | |
| /** | |
| * @var resource | |
| * IMAP connection as returned by imap_open() | |
| */ | |
| protected $connection; | |
| /** | |
| * @var array | |
| * Define function renames | |
| */ | |
| protected static $renamedFunctions = [ | |
| 'getMessageCount' => 'imap_num_msg', | |
| ]; | |
| /** | |
| * Class constructor which sets up the server reference used by imap_open() | |
| * | |
| * @param string $server | |
| * @param null|int $port | |
| * @param bool $ssl | |
| * | |
| * @throws \Luxian\Fetch\IMAP\Exception\MissingPHPLibraryException | |
| * @SuppressWarnings(PHPMD.BooleanArgumentFlag) | |
| */ | |
| public function __construct($server, $port = null, $ssl = false) | |
| { | |
| if (self::$php_imap_library_loaded === null) { | |
| self::$php_imap_library_loaded = extension_loaded('imap'); | |
| } | |
| if (!self::$php_imap_library_loaded) { | |
| throw new Exception\MissingPHPLibraryException(); | |
| } | |
| $this->server_ref = '{'; | |
| $this->server_ref .= imap_utf7_encode($server); | |
| $this->server_ref .= isset($port) ? ":{$port}" : ''; | |
| $this->server_ref .= $ssl ? "/ssl" : ''; | |
| $this->server_ref .= '}'; | |
| } | |
| /** | |
| * Class destructor which prints all alerts and errors and then closes the | |
| * connection if opened | |
| */ | |
| public function __destruct() | |
| { | |
| // Clear error & alerts to avoid PHP errors when closing connection | |
| $imap_alerts = imap_alerts(); | |
| $imap_errors = imap_errors(); | |
| if (is_resource($this->connection)) { | |
| imap_close($this->connection); | |
| echo "Connection closed!\n"; | |
| } | |
| if ($imap_alerts !== false) { | |
| echo "Alerts:\n" . var_export($imap_alerts, true) . "\n"; | |
| } | |
| if ($imap_errors !== false) { | |
| echo "Errors:\n" . var_export($imap_errors, true) . "\n"; | |
| } | |
| } | |
| /** | |
| * Wrapper for imap_open() | |
| * | |
| * @param string $user | |
| * @param string $pass | |
| * | |
| * @throws \Luxian\Fetch\IMAP\Exception\ConnectionOpenException | |
| * @throws \Luxian\Fetch\IMAP\Exception\UndefinedMethodException | |
| * @throws \ReflectionException | |
| */ | |
| public function open($user, $pass) | |
| { | |
| $arguments = [$this->server_ref, $user, $pass]; | |
| $this->connection = $this->__call('open', $arguments); | |
| if (!is_resource($this->connection)) { | |
| throw new Exception\ConnectionOpenException(); | |
| } | |
| } | |
| /** | |
| * @param string $name | |
| * @param array $arguments | |
| * | |
| * @return mixed | |
| * @throws \Luxian\Fetch\IMAP\Exception\UndefinedMethodException | |
| * @throws \ReflectionException | |
| */ | |
| public function __call(string $name, array $arguments) | |
| { | |
| $function_name = self::$renamedFunctions[$name] ?? 'imap_' . $name; | |
| if (!function_exists($function_name)) { | |
| throw new Exception\UndefinedMethodException(); | |
| } | |
| // use reflection to see if first parameter needs to be connection | |
| // and add it to arguments automatically | |
| $function_reflection = new ReflectionFunction($function_name); | |
| $parameters = $function_reflection->getParameters(); | |
| if (count($parameters) > 0) { | |
| $prepended_arguments = []; | |
| $supported_args = [ | |
| [ | |
| 'type' => null, | |
| 'name' => 'stream_id', | |
| 'value' => $this->connection | |
| ], | |
| [ | |
| 'type' => null, | |
| 'name' => 'ref', | |
| 'value' => $this->server_ref | |
| ], | |
| ]; | |
| foreach ($parameters as $parameter) { | |
| foreach ($supported_args as $supported_arg) { | |
| if ($parameter->getType() === $supported_arg['type'] | |
| && $parameter->getName() === $supported_arg['name'] | |
| ) { | |
| $prepended_arguments[] = $supported_arg['value']; | |
| } | |
| } | |
| } | |
| $arguments = array_merge($prepended_arguments, $arguments); | |
| } | |
| return call_user_func_array($function_name, $arguments); | |
| } | |
| } |