WowzaMediaSystems /
wse-rest-library-php
| 1 | <?php |
||||
| 2 | // |
||||
| 3 | // This code and all components (c) Copyright 2006 - 2018, Wowza Media Systems, LLC. All rights reserved. |
||||
| 4 | // This code is licensed pursuant to the Wowza Public License version 1.0, available at www.wowza.com/legal. |
||||
| 5 | // |
||||
| 6 | |||||
| 7 | namespace Com\Wowza; |
||||
| 8 | |||||
| 9 | use Com\Wowza\Entities\Application\Helpers\Settings; |
||||
| 10 | |||||
| 11 | class Wowza |
||||
| 12 | { |
||||
| 13 | const VERB_POST = 'POST'; |
||||
| 14 | const VERB_GET = 'GET'; |
||||
| 15 | const VERB_DELETE = 'DELETE'; |
||||
| 16 | const VERB_PUT = 'PUT'; |
||||
| 17 | |||||
| 18 | protected $restURI = ''; |
||||
| 19 | private $_skip = []; |
||||
| 20 | private $_additional = []; |
||||
| 21 | |||||
| 22 | private $settings; |
||||
| 23 | |||||
| 24 | public function __construct(Settings $settings) |
||||
| 25 | { |
||||
| 26 | $this->settings = $settings; |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | protected function getHost() |
||||
| 30 | { |
||||
| 31 | return $this->settings->getHost(); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | protected function getServerInstance() |
||||
| 35 | { |
||||
| 36 | return $this->settings->getServerInstance(); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | protected function getVHostInstance() |
||||
| 40 | { |
||||
| 41 | return $this->settings->getVhostInstance(); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | protected function getEntites($args, $baseURI) |
||||
| 45 | { |
||||
| 46 | $entities = []; |
||||
| 47 | $argsCount = count($args); |
||||
| 48 | |||||
| 49 | for ($i = 0; $i < $argsCount; $i++) { |
||||
| 50 | $arg = $args[$i]; |
||||
| 51 | if (!is_null($arg)) { |
||||
| 52 | if (is_null($arg->restURI)) { |
||||
| 53 | if (is_null($baseURI)) { |
||||
| 54 | unset($arg->restURI); |
||||
| 55 | } else { |
||||
| 56 | call_user_func_array([ |
||||
| 57 | $arg, |
||||
| 58 | 'setURI', |
||||
| 59 | ], [ |
||||
| 60 | $baseURI, |
||||
| 61 | ]); |
||||
| 62 | } |
||||
| 63 | } |
||||
| 64 | $entities [] = $arg; |
||||
| 65 | } |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | return $entities; |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | protected function debug($str) |
||||
| 72 | { |
||||
| 73 | if ($this->settings->isDebug()) { |
||||
| 74 | if (!is_string($str)) { |
||||
| 75 | $str = json_encode($str); |
||||
| 76 | } |
||||
| 77 | echo $str . "\n"; |
||||
| 78 | } |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | protected function sendRequest($props, $entities, $verbType = self::VERB_POST, $queryParams = null) |
||||
| 82 | { |
||||
| 83 | if (isset($props->restURI) && !empty($props->restURI)) { |
||||
| 84 | $entityCount = count($entities); |
||||
| 85 | if ($entityCount > 0) { |
||||
| 86 | for ($i = 0; $i < $entityCount; $i++) { |
||||
| 87 | $entity = $entities[$i]; |
||||
| 88 | if (is_object($entity) && method_exists($entity, 'getEntityName')) { |
||||
| 89 | $name = $entity->getEntityName(); |
||||
| 90 | $props->$name = $entity; |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | } |
||||
| 94 | $json = json_encode($props); |
||||
| 95 | |||||
| 96 | $restURL = $props->restURI; |
||||
| 97 | if (null !== $queryParams) { |
||||
| 98 | $restURL .= '?' . $queryParams; |
||||
| 99 | } |
||||
| 100 | $this->debug("JSON REQUEST to {$restURL} with verb {$verbType}: " . $json); |
||||
| 101 | |||||
| 102 | $ch = curl_init($restURL); |
||||
| 103 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verbType); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 104 | curl_setopt($ch, CURLOPT_POSTFIELDS, $json); |
||||
| 105 | |||||
| 106 | if ($this->settings->isUseDigest()) { |
||||
| 107 | curl_setopt($ch, CURLOPT_USERPWD, |
||||
| 108 | $this->settings->getUsername() . ':' . $this->settings->getPassword()); |
||||
| 109 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
| 113 | curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
||||
| 114 | 'Accept:application/json; charset=utf-8', |
||||
| 115 | 'Content-type:application/json; charset=utf-8', |
||||
| 116 | 'Content-Length: ' . strlen($json), |
||||
| 117 | ]); |
||||
| 118 | $contents = curl_exec($ch); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 119 | curl_close($ch); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 120 | |||||
| 121 | $this->debug('RETURN: ' . $contents); |
||||
| 122 | |||||
| 123 | return json_decode($contents); |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | return false; |
||||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * @param $key |
||||
| 131 | * @param $value |
||||
| 132 | * |
||||
| 133 | * @return $this |
||||
| 134 | */ |
||||
| 135 | public function addAdditionalParameter($key, $value) |
||||
| 136 | { |
||||
| 137 | $this->_additional[$key] = $value; |
||||
| 138 | |||||
| 139 | return $this; |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | /** |
||||
| 143 | * @param $key |
||||
| 144 | * @param $value |
||||
| 145 | * |
||||
| 146 | * @return $this |
||||
| 147 | */ |
||||
| 148 | public function addSkipParameter($key, $value) |
||||
| 149 | { |
||||
| 150 | $this->_skip[$key] = $value; |
||||
| 151 | |||||
| 152 | return $this; |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | protected function preparePropertiesForRequest($class) |
||||
| 156 | { |
||||
| 157 | $classPropNames = get_class_vars($class); |
||||
| 158 | |||||
| 159 | $props = new \stdClass(); |
||||
| 160 | foreach ($classPropNames as $key => $val) { |
||||
| 161 | if (isset($this->$key)) { |
||||
| 162 | if (preg_match("/^(\_)/", $key)) { |
||||
| 163 | continue; |
||||
| 164 | } |
||||
| 165 | if (array_key_exists($key, $this->_skip)) { |
||||
| 166 | continue; |
||||
| 167 | } |
||||
| 168 | $props->$key = $this->$key; |
||||
| 169 | } |
||||
| 170 | } |
||||
| 171 | |||||
| 172 | if (count($this->_additional) > 0) { |
||||
| 173 | foreach ($this->_additional as $key => $val) { |
||||
| 174 | $props->$key = $val; |
||||
| 175 | } |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | return $props; |
||||
| 179 | } |
||||
| 180 | } |
||||
| 181 |