* @package WB * @subpackage service */ WBClass::load('WBService' , 'WBHtml' , 'WBHtml_JS'); /** * Process JSONP request * * @version 0.2.0 * @package WB * @subpackage service */ class WBService_JSONP extends WBService { /** * decide whether to select locale settings from path * @var bool */ protected $useI18nFromPath = true; /** * Render Content and Add JavaScript Padding * * @todo Implement dynmaic JavaScript load support? * @param int $status * @param mixed $data to be send * @param string $checksum * @return bool true */ public function run(&$status, &$data, &$checksum = null) { if (count($this->path) < 2) { $status = 400; $data = 'Invalid path for JSONP request.'; return true; } $this->stripLanguageFromPath($this->path); $sel = array_shift($this->path); $config = array(); $log = array( 'service' => 'jsonp', 'action' => 'run', 'error' => 'none', 'sel' => $sel, 'processor' => $config['processor'], 'path' => implode('/', $this->path) ); $this->log->notice($log); $conf = WBClass::create('WBConfig'); $conf->load($this->part . '/jsonp/' . implode('/', $this->path)); $config = $conf->get('content'); $this->mergeRequestParams($config); $this->cont = WBClass::create('WBContent_' . $config['processor']); $this->cont->configure(implode('/', $this->path), $config['params'], array()); $config['params'] = $this->cont->run(); // get string from content, afterwards add JavaScript and other HTML gadgets $cnt = $this->cont->getString(); $search = array( 'WB.Ajax.update(', 'WB.Ajax.link(' ); $repl = array( sprintf('WB.Ajax.JSONP.update(\'%s\', ', $sel), sprintf('WB.Ajax.JSONP.link(\'%s\', ', $sel) ); $cnt = str_replace($search, $repl, $cnt); // pre included $js = array(); $inc = WBHtml_JS::getIncluded(true); if (!empty($inc)) { foreach ($inc as $k => $i) { $i = str_replace('/', '.', $i); $js[] = sprintf(WBHtml_JS::FMT_LOADED, $k); } $js = array(sprintf(WBHtml_JS::FMT_CODE, implode("\n", $js))); } // additional JavaScript $js[] = WBHtml::get(WBHtml::AREA_HEAD); $js[] = WBHtml::get(WBHtml::AREA_FOOT); $js[] = sprintf(WBHtml_JS::FMT_CODE, WBHtml::get(WBHtml::AREA_JS_ONLOAD)); $js = implode("\n", $js); // add padding $data = sprintf('WB.Ajax.JSONP.callback("%s", %s, %s);', $sel, json_encode($cnt), json_encode($js)); $this->res->addHeader('Content-Type', 'application/x-javascript; charset=utf-8;'); $checksum = md5($data); $status = 200; return true; } }