Source of file IncomingMessage.php

Size: 7,882 Bytes - Last Modified: 2020-09-30T19:35:47+00:00

/data/development/sccp/sources/PAMI/src/PAMI/Message/IncomingMessage.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
<?php
/**
 * A generic incoming message.
 *
 * PHP Version 5
 *
 * @category Pami
 * @package  Message
 * @author   Marcelo Gornstein <marcelog@gmail.com>
 * @license  http://marcelog.github.com/PAMI/ Apache License 2.0
 * @version  SVN: $Id$
 * @link     http://marcelog.github.com/PAMI/
 *
 * Copyright 2011 Marcelo Gornstein <marcelog@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
namespace PAMI\Message;

/**
 * A generic incoming message.
 *
 * PHP Version 5
 *
 * @category Pami
 * @package  Message
 * @author   Marcelo Gornstein <marcelog@gmail.com>
 * @license  http://marcelog.github.com/PAMI/ Apache License 2.0
 * @link     http://marcelog.github.com/PAMI/
 */
abstract class IncomingMessage extends Message
{
    /**
     * Holds original message.
     * @var string
     */
    protected $rawContent;

    /**
     * Metadata. Specific channel variables.
     * @var string[]
     */
    protected $channelVariables;

    /**
     * Metadata. Specific channel variables.
     * @var string[]
     */
    protected $statusVariables;

    /**
     * Serialize function.
     *
     * @return string[]
     */
    public function __sleep()
    {
        $ret = parent::__sleep();
        $ret[] = 'rawContent';
        return $ret;
    }

    /**
     * Returns key 'EventList'. In respones, this will surely be a "start". In
     * events, should be a "complete".
     *
     * @return string
     */
    public function getEventList()
    {
        return $this->getKey('EventList');
    }

    /**
     * Returns the original message content without parsing.
     *
     * @return string
     */
    public function getRawContent()
    {
        return $this->rawContent;
    }

    /**
     * Returns the channel variables for all reported channels.
     * https://github.com/marcelog/PAMI/issues/85
     *
     * The channel names will be lowercased.
     *
     * @return array
     */
    public function getAllChannelVariables()
    {
        return $this->channelVariables;
    }

    /**
     * Returns the channel variables for the given channel.
     * https://github.com/marcelog/PAMI/issues/85
     *
     * @param string $channel Channel name. If not given, will return variables
     * for the "current" channel.
     *
     * @return array
     */
    public function getChannelVariables($channel = null)
    {
        if (is_null($channel)) {
            if (!isset($this->keys['channel'])) {
                return $this->getChannelVariables('default');
            } else {
                return $this->getChannelVariables($this->keys['channel']);
            }
        } else {
            $channel = strtolower($channel);
            if (!isset($this->channelVariables[$channel])) {
                return null;
            }
            return $this->channelVariables[$channel];
        }
    }

    /**
     * Returns the channel variables for all reported channels.
     * https://github.com/marcelog/PAMI/issues/85
     *
     * The channel names will be lowercased.
     *
     * @return array
     */
    public function getAllStatusVariables()
    {
        return $this->statusVariables;
    }

    /**
     * Returns the channel variables for the given channel.
     * https://github.com/marcelog/PAMI/issues/85
     *
     * @param string $channel Channel name. If not given, will return variables
     * for the "current" channel.
     *
     * @return array
     */
    public function getStatusVariables($channel = null)
    {
        if (is_null($channel)) {
            if (!isset($this->keys['channel'])) {
                return $this->getStatusVariables('default');
            } else {
                return $this->getStatusVariables($this->keys['channel']);
            }
        } else {
            $channel = strtolower($channel);
            if (!isset($this->statusVariables[$channel])) {
                return null;
            }
            return $this->statusVariables[$channel];
        }
    }

    /**
     * Constructor.
     *
     * @param string $rawContent Original message as received from ami.
     *
     * @return void
     */
    public function __construct($rawContent)
    {
        parent::__construct();
        $this->channelVariables = array('default' => array());
        $this->statusVariables = array('default' => array());
        $this->rawContent = $rawContent;
        $lines = explode(Message::EOL, $rawContent);
        foreach ($lines as $line) {
            $content = explode(':', $line);
            $name = strtolower(trim($content[0]));
            unset($content[0]);
            $value = isset($content[1]) ? trim(implode(':', $content)) : '';
            if (!strncmp($name, 'chanvariable', 12)) {
                // https://github.com/marcelog/PAMI/issues/85
                $matches = preg_match("/\(([^\)]*)\)/", $name, $captures);
                $chanName = 'default';
                if ($matches > 0) {
                    $chanName = $captures[1];
                }
                $content = explode('=', $value);
                $name = trim($content[0]);
                unset($content[0]);
                $value = isset($content[1]) ? trim(implode(':', $content)) : '';
                $this->channelVariables[$chanName][strtolower($name)] = $value;
                $this->setSanitizedKey('chanvariable', $name);
            } elseif (!strncmp($name, 'variable', 8)) {
                // https://github.com/marcelog/PAMI/issues/85
                $matches = preg_match("/\(([^\)]*)\)/", $name, $captures);
                $chanName = 'default';
                if ($matches > 0) {
                    $chanName = $captures[1];
                }
                $content = explode('=', $value);
                $name = trim($content[0]);
                unset($content[0]);
                $value = isset($content[1]) ? trim(implode(':', $content)) : '';
                $this->statusVariables[$chanName][strtolower($name)] = $value;
                $this->setSanitizedKey('variable', $name);
            }
            // Added ResponseFactory #d3b0ce8
            try {
                $this->setSanitizedKey($name, $value);
            } catch (PAMIException $e) {
                throw new PAMIException("Error: '" . $e . "'\n Dump RawContent:\n"  . $this->rawContent ."\n");
            }
        }
        // https://github.com/marcelog/PAMI/issues/85
        if (isset($this->keys['channel'])) {
            $channel = strtolower($this->keys['channel']);

            if (isset($this->channelVariables[$channel])) {
                $this->channelVariables[$channel] = array_merge(
                    $this->channelVariables[$channel],
                    $this->channelVariables['default']
                );
            } else {
                $this->channelVariables[$channel] = $this->channelVariables['default'];
            }
            unset($this->channelVariables['default']);

            if (isset($this->statusVariables[$channel])) {
                $this->statusVariables[$channel] = array_merge(
                    $this->statusVariables[$channel],
                    $this->statusVariables['default']
                );
            } else {
                $this->statusVariables[$channel] = $this->statusVariables['default'];
            }
            unset($this->statusVariables['default']);
        }
    }
}