protected function _processView(array $output) { $success = array(); $query = $this->_request->getQuery(); if (!isset($query['method'])) { throw new CM_Exception_Invalid('No method specified', CM_Exception::WARN); } if (!preg_match('/^[\\w_]+$/i', $query['method'])) { throw new CM_Exception_Invalid('Illegal method', CM_Exception::WARN, ['method' => $query['method']]); } if (!isset($query['params']) || !is_array($query['params'])) { throw new CM_Exception_Invalid('Illegal params', CM_Exception::WARN); } $view = $this->_getView(); if ($view instanceof CM_View_CheckAccessibleInterface) { $view->checkAccessible($this->getRender()->getEnvironment()); } $ajaxMethodName = 'ajax_' . $query['method']; $params = CM_Params::factory($query['params'], true); $componentHandler = new CM_Frontend_JavascriptContainer_View(); $this->_setStringRepresentation(get_class($view) . '::' . $ajaxMethodName); if (!method_exists($view, $ajaxMethodName)) { throw new CM_Exception_Invalid('Method not found', CM_Exception::WARN, ['method' => $ajaxMethodName]); } $data = $view->{$ajaxMethodName}($params, $componentHandler, $this); $success['data'] = CM_Params::encode($data); $frontend = $this->getRender()->getGlobalResponse(); $frontend->getOnloadReadyJs()->append($componentHandler->compile('this')); $jsCode = $frontend->getJs(); if (strlen($jsCode)) { $success['exec'] = $jsCode; } $output['success'] = $success; return $output; }
/** * @param CM_Params|array|null $params */ public function __construct($params = null) { if (!$params instanceof CM_Params) { $params = CM_Params::factory($params, false); } $this->_params = $params; }
function smarty_function_formField(array $params, Smarty_Internal_Template $template) { /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); $cssClasses = array(); if (isset($params['class'])) { $cssClasses[] = (string) $params['class']; unset($params['class']); } $label = null; if (isset($params['label'])) { $label = (string) $params['label']; } $input = null; $fieldName = null; if (isset($params['prepend'])) { $input .= (string) $params['prepend']; } /** @var CM_Frontend_ViewResponse|null $viewResponse */ $viewResponse = null; if (isset($params['name'])) { $fieldName = (string) $params['name']; $cssClasses[] = $fieldName; /** @var CM_Form_Abstract $form */ $form = $render->getGlobalResponse()->getClosestViewResponse('CM_Form_Abstract')->getView(); if (null === $form) { throw new CM_Exception_Invalid('Cannot find parent `CM_Form_Abstract` view response. Named {formField} can be only rendered within form view.'); } $formField = $form->getField($fieldName); $renderAdapter = new CM_RenderAdapter_FormField($render, $formField); $input .= $renderAdapter->fetch(CM_Params::factory($params, false), $viewResponse); if (null !== $formField->getValue()) { $cssClasses[] = 'prefilled'; } } if (isset($params['append'])) { $input .= (string) $params['append']; } $html = '<div class="formField clearfix ' . join(' ', $cssClasses) . '">'; if ($label) { $html .= '<label'; if ($viewResponse) { $html .= ' for="' . $viewResponse->getAutoIdTagged('input') . '"'; } $html .= '>' . $label . '</label>'; } if ($input) { $html .= '<div class="input">'; $html .= $input; $html .= '</div>'; } $html .= '</div>'; return $html; }
protected function _process() { $params = CM_Params::factory($this->_request->getQuery(), true); try { $user = $params->getUser('user'); $mailType = $params->getInt('mailType'); $action = new CM_Action_Email(CM_Action_Abstract::VIEW, $user, $mailType); $action->prepare(); $action->notify($user, $mailType); } catch (CM_Exception $e) { if (in_array(get_class($e), ['CM_Exception_Nonexistent', 'CM_Exception_InvalidParam'])) { $e->setSeverity(CM_Exception::WARN); } throw $e; } $this->setHeader('Content-Type', 'image/gif'); $this->_setContent(base64_decode('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==')); }
function smarty_function_input(array $params, Smarty_Internal_Template $template) { $params = CM_Params::factory($params, false); if (!$params->has('name')) { throw new CM_Exception_Invalid('Param `name` missing'); } /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); /** @var CM_Form_Abstract $form */ $form = $render->getGlobalResponse()->getClosestViewResponse('CM_Form_Abstract')->getView(); if (null === $form) { throw new CM_Exception_Invalid('Cannot find parent `CM_Form_Abstract` view response. {input} can be only rendered within form view.'); } $fieldName = $params->getString('name'); $field = $form->getField($fieldName); $renderAdapter = new CM_RenderAdapter_FormField($render, $field); return $renderAdapter->fetch($params); }
protected function _process() { $output = array(); $this->_runWithCatching(function () use(&$output) { $query = CM_Params::factory($this->_request->getQuery()); $method = $query->getString('method'); if (!preg_match('/^(?<class>[\\w_]+)\\.(?<function>[\\w_]+)$/', $method, $matches)) { throw new CM_Exception_InvalidParam('Illegal method: `' . $method . '`.'); } $class = $matches['class']; $function = $matches['function']; $params = $query->getArray('params'); $output['success'] = array('result' => call_user_func_array(array($class, 'rpc_' . $function), $params)); }, function (CM_Exception $e) use(&$output) { $output['error'] = array('type' => get_class($e), 'msg' => $e->getMessagePublic($this->getRender()), 'isPublic' => $e->isPublic()); }); $this->setHeader('Content-Type', 'application/json'); $this->_setContent(json_encode($output)); }
/** * @param string $serverKey * @param string $sessionData * @param string $channelKey * @param string $channelMediaId * @param string $channelData * @param string $streamKey * @param int $streamStart * @throws CM_Exception_AuthFailed * @throws CM_Exception_AuthRequired * @throws CM_Exception_Invalid * @throws CM_Exception_NotAllowed */ public static function rpc_subscribe($serverKey, $sessionData, $channelKey, $channelMediaId, $channelData, $streamKey, $streamStart) { $janus = CM_Service_Manager::getInstance()->getJanus('janus'); self::_authenticate($janus, $serverKey); $server = $janus->getServerList()->findByKey($serverKey); $sessionParams = CM_Params::factory(CM_Params::jsonDecode($sessionData), true); $session = $sessionParams->getSession('sessionId'); $user = $session->getUser(true); $channelKey = (string) $channelKey; $channelMediaId = (string) $channelMediaId; $channelParams = CM_Params::factory(CM_Params::jsonDecode($channelData), true); $channelType = $channelParams->getInt('streamChannelType'); $streamKey = (string) $streamKey; $streamStart = (int) $streamStart; $streamRepository = $janus->getStreamRepository(); /** @var CM_Model_StreamChannel_Media $streamChannel */ $streamChannel = $streamRepository->findStreamChannelByKey($channelKey); if (null === $streamChannel) { $streamChannel = $streamRepository->createStreamChannel($channelKey, $channelType, $server->getId(), $channelMediaId); } else { $channelServerData = ['type' => $streamChannel->getType(), 'mediaId' => $streamChannel->getMediaId(), 'serverId' => $streamChannel->getServerId()]; $channelRequestData = ['type' => $channelType, 'mediaId' => $channelMediaId, 'serverId' => $server->getId()]; if ($channelServerData !== $channelRequestData) { throw new CM_Exception_Invalid('Channel request data differs from server data', null, ['channelRequestData' => $channelRequestData, 'channelServerData' => $channelServerData]); } } try { $streamRepository->createStreamSubscribe($streamChannel, $user, $streamKey, $streamStart); } catch (CM_Exception_NotAllowed $exception) { if (!$streamChannel->hasStreams()) { $streamChannel->delete(); } throw new CM_Exception_NotAllowed('Cannot subscribe', $exception->getSeverity(), ['originalExceptionMessage' => $exception->getMessage()]); } catch (CM_Exception_Invalid $exception) { if (!$streamChannel->hasStreams()) { $streamChannel->delete(); } throw new CM_Exception_Invalid('Cannot subscribe', $exception->getSeverity(), ['originalExceptionMessage' => $exception->getMessage()]); } }
function smarty_block_form($params, $content, Smarty_Internal_Template $template, $open) { /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); $frontend = $render->getGlobalResponse(); if ($open) { $form = CM_Form_Abstract::factory($params['name'], $params); $viewResponse = new CM_Frontend_ViewResponse($form); $form->prepare($render->getEnvironment(), $viewResponse); $frontend->treeExpand($viewResponse); return ''; } else { $viewResponse = $frontend->getClosestViewResponse('CM_Form_Abstract'); if (null === $viewResponse) { throw new CM_Exception_Invalid('Cannot find `CM_Form_Abstract` within frontend tree.'); } /** @var CM_Form_Abstract $form */ $form = $viewResponse->getView(); $cssClasses = $viewResponse->getCssClasses(); $cssClasses[] = $form->getName(); $html = '<form id="' . $viewResponse->getAutoId() . '" class="' . implode(' ', $cssClasses) . '" method="post" action="" onsubmit="return false;" novalidate >'; if ($form->getAvoidPasswordManager()) { $html .= '<input style="display:none" type="text" name="fakeusernameremembered">'; $html .= '<input style="display:none" type="password" name="fakepasswordremembered">'; } $html .= $content; foreach ($form->getFields() as $field) { if ($field instanceof CM_FormField_Hidden) { $renderAdapter = new CM_RenderAdapter_FormField($render, $field); $html .= $renderAdapter->fetch(CM_Params::factory()); } } foreach ($form->getActions() as $actionName => $action) { $viewResponse->getJs()->append("this.registerAction('{$actionName}', {$action->js_presentation()});"); } $html .= '</form>'; $frontend->treeCollapse(); return $html; } }
/** * @param string $streamName * @param string $clientKey * @param int $start * @param string $data * @throws CM_Exception_NotAllowed */ public function subscribe($streamName, $clientKey, $start, $data) { $streamName = (string) $streamName; $clientKey = (string) $clientKey; $start = (int) $start; $data = (string) $data; $user = null; $params = CM_Params::factory(CM_Params::jsonDecode($data), true); if ($params->has('sessionId')) { if ($session = CM_Session::findById($params->getString('sessionId'))) { $user = $session->getUser(false); } } /** @var CM_Model_StreamChannel_Abstract $streamChannel */ $streamChannel = CM_Model_StreamChannel_Abstract::findByKeyAndAdapter($streamName, $this->getType()); if (!$streamChannel) { throw new CM_Exception_NotAllowed(); } try { CM_Model_Stream_Subscribe::createStatic(array('streamChannel' => $streamChannel, 'user' => $user, 'start' => $start, 'key' => $clientKey)); } catch (CM_Exception $ex) { throw new CM_Exception_NotAllowed('Cannot subscribe: ' . $ex->getMessage()); } }
/** * @param string|null $className * @return CM_View_Abstract */ protected function _getView($className = null) { if (null === $className) { $className = 'CM_View_Abstract'; } $viewInfo = $this->_getViewInfo($className); /** @var CM_View_Abstract $className */ return $className::factory($viewInfo['className'], CM_Params::factory($viewInfo['params'], true)); }
/** * @param string $key * @param CM_Params|null $default * @return CM_Params * @throws CM_Exception_Invalid */ public function getParams($key, CM_Params $default = null) { $param = $this->getObject($key, 'CM_Params', $default, function ($className, $param) { if (is_string($param)) { $json = (string) $param; try { $array = CM_Params::decode($json, true); } catch (CM_Exception_Invalid $e) { throw new CM_Exception_InvalidParam('Cannot decode input', null, ['message' => $e->getMessage()]); } } elseif (is_array($param)) { $array = $param; } else { throw new CM_Exception_InvalidParam('Unexpected type of arguments', null, ['type' => gettype($param)]); } return CM_Params::factory($array, false); }); if (!$param instanceof CM_Params) { throw new CM_Exception_Invalid('Not a CM_Params'); } return $param; }
/** * @param string $pageClass * @param CM_Model_User|null $viewer OPTIONAL * @param array $params OPTIONAL * @return CM_Page_Abstract */ public static function createPage($pageClass, CM_Model_User $viewer = null, $params = array()) { $request = new CM_Http_Request_Get('?' . http_build_query($params), array(), $viewer); return new $pageClass(CM_Params::factory($request->getQuery(), true), $request->getViewer()); }
/** * @param string $message * @throws CM_Exception_Invalid */ public function onRedisMessage($message) { $message = CM_Params::jsonDecode($message); $type = $message['type']; $data = $message['data']; switch ($type) { case 'subscribe': $channel = $data['channel']; $clientKey = $data['clientKey']; $start = time(); $data = CM_Params::factory((array) $data['data'], true); $user = null; if ($data->has('sessionId')) { if ($session = CM_Session::findById($data->getString('sessionId'))) { $user = $session->getUser(false); } } $this->_subscribe($channel, $clientKey, $start, $user); break; case 'unsubscribe': $channel = $data['channel']; $clientKey = $data['clientKey']; $this->_unsubscribe($channel, $clientKey); break; case 'message': break; default: throw new CM_Exception_Invalid('Invalid socket-redis event type'); } }
/** * @param CM_Http_Request_Abstract $request * @throws CM_Exception_Nonexistent * @throws CM_Exception * @throws CM_Exception_Nonexistent * @return string|null|boolean */ private function _processPage(CM_Http_Request_Abstract $request) { return $this->_runWithCatching(function () use($request) { $this->getSite()->rewrite($request); $pageParams = CM_Params::factory($request->getQuery(), true); try { $className = CM_Page_Abstract::getClassnameByPath($this->getRender(), $request->getPath()); $page = CM_Page_Abstract::factory($className, $pageParams); } catch (CM_Exception $ex) { throw new CM_Exception_Nonexistent('Cannot load page `' . $request->getPath() . '`: ' . $ex->getMessage()); } $this->_setStringRepresentation(get_class($page)); $page->prepareResponse($this->getRender()->getEnvironment(), $this); if ($this->getRedirectUrl()) { $request->setUri($this->getRedirectUrl()); return null; } $html = $this->_renderPage($page); $this->_page = $page; $this->_pageParams = $pageParams; return $html; }, function (CM_Exception $ex, array $errorOptions) use($request) { $this->getRender()->getGlobalResponse()->clear(); /** @var CM_Page_Abstract $errorPage */ $errorPage = $errorOptions['errorPage']; $request->setPath($errorPage::getPath()); $request->setQuery(array()); return false; }); }
/** * @param CM_FormField_Abstract $formField * @param array|null $renderParams * @param CM_Frontend_Render|null $render * @return CM_Dom_NodeList */ protected function _renderFormField(CM_FormField_Abstract $formField, array $renderParams = null, CM_Frontend_Render $render = null) { if (null === $render) { $render = new CM_Frontend_Render(); } $renderAdapter = new CM_RenderAdapter_FormField($render, $formField); $html = $renderAdapter->fetch(CM_Params::factory($renderParams, false)); return new CM_Dom_NodeList($html, true); }
function smarty_function_menu(array $params, Smarty_Internal_Template $template) { /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); $environment = $render->getEnvironment(); if (isset($params['params'])) { $params = array_merge($params['params'], $params); } $activePage = null; if (isset($params['activePage'])) { $activePage = $params['activePage']; } elseif ($pageViewResponse = $render->getGlobalResponse()->getClosestViewResponse('CM_Page_Abstract')) { $activePage = $pageViewResponse->getView(); } elseif ($layoutViewResponse = $render->getGlobalResponse()->getClosestViewResponse('CM_Layout_Abstract')) { $activePage = $layoutViewResponse->get('page'); } if ($activePage) { if (!$activePage instanceof CM_Page_Abstract) { throw new CM_Exception_Invalid('`$activePage` must be an instance of `CM_Page_Abstract`'); } $pageClassName = get_class($activePage); $activePath = $activePage::getPath(); $activeParams = $activePage->getParams(); } else { $pageClassName = null; $activePath = '/'; $activeParams = CM_Params::factory(); } $menu = null; $name = null; if (isset($params['name'])) { $name = $params['name']; $menuArr = $render->getSite()->getMenus(); if (isset($menuArr[$name])) { $menu = $menuArr[$name]; } } elseif (isset($params['data'])) { $menu = new CM_Menu($params['data']); $render->addMenu($menu); } if (!$menu) { return ''; } $menuEntries = null; $depth = isset($params['depth']) ? (int) $params['depth'] : null; if (!empty($params['breadcrumb'])) { $entry = $menu->findEntry($pageClassName, $activeParams, $depth, $depth); if ($entry) { $menuEntries = $entry->getParents(); // Also add current entry $menuEntries[] = $entry; } } elseif (!empty($params['submenu'])) { $entry = $menu->findEntry($pageClassName, $activeParams, $depth, $depth); if ($entry && $entry->hasChildren()) { $menu = $entry->getChildren(); $menuEntries = $menu->getEntries($environment); } } elseif (!is_null($depth)) { if ($entry = $menu->findEntry($pageClassName, $activeParams, $depth)) { $parents = $entry->getParents(); $parents[] = $entry; /** @var CM_MenuEntry $menuEntry */ $menuEntry = $parents[$depth]; $menuEntries = $menuEntry->getSiblings()->getEntries($environment); } } else { $menuEntries = $menu->getEntries($environment); } if (empty($menuEntries)) { return ''; } $class = 'menu'; if ($name) { $class .= ' ' . $name; } if (isset($params['class'])) { $class .= ' ' . $params['class']; } $tplName = 'default'; if (isset($params['template'])) { $tplName = $params['template']; } $tplPath = $render->getLayoutPath('menu/' . $tplName . '.tpl'); $assign = array('menu_entries' => $menuEntries, 'menu_class' => $class, 'activePath' => $activePath, 'activeParams' => $activeParams, 'name' => $name, 'originalParams' => $params); return $render->fetchTemplate($tplPath, $assign); }
/** * @param CM_Http_Request_Abstract $request * @param CM_Http_Response_Page_ProcessingResult $result * @throws CM_Exception * @throws Exception * @return boolean */ private function _processPage(CM_Http_Request_Abstract $request, CM_Http_Response_Page_ProcessingResult $result) { return $this->_runWithCatching(function () use($request, $result) { $path = CM_Util::link($request->getPath(), $request->getQuery()); $result->addPath($path); $this->getSite()->rewrite($request); $pageParams = CM_Params::factory($request->getQuery(), true); try { $className = CM_Page_Abstract::getClassnameByPath($this->getRender(), $request->getPath()); $page = CM_Page_Abstract::factory($className, $pageParams); } catch (CM_Exception $ex) { throw new CM_Exception_Nonexistent('Cannot load page `' . $request->getPath() . '`: ' . $ex->getMessage()); } $result->addPage($page); $environment = $this->getRender()->getEnvironment(); $page->prepareResponse($environment, $this); if ($this->getRedirectUrl()) { $request->setUri($this->getRedirectUrl()); return true; } if ($page->getCanTrackPageView()) { $this->getRender()->getServiceManager()->getTrackings()->trackPageView($environment, $result->getPathTracking()); } $result->setHtml($this->_renderPage($page)); return true; }, function (CM_Exception $ex, array $errorOptions) use($request) { $this->getRender()->getGlobalResponse()->clear(); /** @var CM_Page_Abstract $errorPage */ $errorPage = $errorOptions['errorPage']; $request->setPath($errorPage::getPath()); $request->setQuery(array()); return false; }); }
/** * @param array[] $paramsList * @return mixed[] */ protected function _runMultipleWithoutGearman(array $paramsList) { $resultList = array(); foreach ($paramsList as $params) { $resultList[] = $this->_executeJob(CM_Params::factory($params, true)); } return $resultList; }
/** * @param array $data * @param CM_Http_Response_View_Form $response * @param CM_Form_Abstract $form * @return mixed */ public final function process(array $data, CM_Http_Response_View_Form $response, CM_Form_Abstract $form) { return $this->_process(CM_Params::factory($data, false), $response, $form); }