/**
  * Instanciate the specified module
  */
 function &createInstanceOf($bundleId, $options = array(), $initArgs = array())
 {
     global $xoops;
     XOS::import('xoops_kernel_Module');
     // @TODO: This is lame and limited, whatever, should be arranged later
     if (!empty($bundleId)) {
         // Module ID specified: get its location from the registry
         if (!XOS::import($bundleId)) {
             trigger_error("Cannot instanciate unknown module " . $bundleId, E_USER_WARNING);
             return false;
         }
         $root = $xoops->path(XOS::classVar($bundleId, 'xoBundleRoot')) . '/';
     } else {
         $root = '';
     }
     $moduleInfo = @(include $root . 'xo-info.php');
     unset($moduleInfo['xoServices']);
     $options = array_merge($moduleInfo, $options);
     if (isset($moduleInfo['xoClassPath'])) {
         XOS::import($moduleInfo['xoBundleIdentifier']);
         $inst =& XOS::createInstanceOf($moduleInfo['xoBundleIdentifier'], $options);
     } else {
         $inst =& XOS::createInstanceOf('xoops_kernel_Module', $options);
     }
     $inst->xoBundleIdentifier = $moduleInfo['xoBundleIdentifier'];
     $inst->xoBundleRoot = XOS::classVar($inst->xoBundleIdentifier, 'xoBundleRoot');
     // If we are instanciating the "current" module, find the current location
     if ($inst && empty($bundleId)) {
         $moduleRoot = substr($inst->xoBundleRoot, 4);
         $scriptFile = substr(strstr($_SERVER['SCRIPT_NAME'], $moduleRoot), strlen($moduleRoot));
         $inst->currentLocation = $inst->findLocationName($scriptFile);
     }
     return $inst;
 }
 /**
  * Convert a XOOPS path to a physical one
  */
 function path($url, $virtual = false)
 {
     // If the URL begins with protocol:// then remove it
     if ($pos = strpos($url, '://')) {
         $url = substr($url, $pos + 3);
     }
     $parts = explode('#', $url);
     if (count($parts) == 1) {
         if ($parts[0][0] == '/') {
             $parts[0] = substr($parts[0], 1);
         }
         $parts = explode('/', $parts[0], 2);
         if (!$virtual) {
             return !isset($this->paths[$parts[0]]) ? '' : $this->paths[$parts[0]][0] . '/' . $parts[1];
         } else {
             if (!isset($this->paths[$parts[0]][1])) {
                 return false;
             }
             if (empty($this->paths[$parts[0]][1])) {
                 return $this->baseLocation . '/' . $parts[1];
             }
             return $this->baseLocation . '/' . $this->paths[$parts[0]][1] . '/' . $parts[1];
         }
     } else {
         $root = XOS::classVar($parts[0], 'xoBundleRoot');
         return $this->path($root . '/' . $parts[1]);
     }
 }
 function resourcePath($path, $fromDocRoot = true)
 {
     global $xoops;
     $parts = explode('#', $path, 2);
     if (count($parts) > 1) {
         list($bundleId, $resPath) = $parts;
         // This is component resource: modules are in 'modules', and components in 'components'
         $themedRoot = substr($parts[0], 0, 4) == 'mod_' ? 'modules' : 'components';
         if (file_exists("{$this->path}/{$themedRoot}/{$bundleId}/{$resPath}")) {
             return "themes/{$this->folderName}/{$themedRoot}/{$bundleId}/{$resPath}";
         } else {
             return XOS::classVar($bundleId, 'xoBundleRoot') . '/' . $resPath;
         }
     }
     if (substr($path, 0, 1) == '/') {
         $path = substr($path, 1);
         $fromDocRoot = false;
     }
     if (file_exists("{$this->path}/{$path}")) {
         return "themes/{$this->folderName}/{$path}";
     }
     if (!empty($this->parentTheme)) {
         if (!is_object($this->parentTheme)) {
             $this->parentTheme =& XOS::create('xoops_opal_Theme', array('folderName' => $this->parentTheme));
         }
         if (is_object($this->parentTheme)) {
             return $this->parentTheme->resourcePath($path, $fromDocRoot);
         }
     }
     return $fromDocRoot ? "www/{$path}" : "themes/{$this->folderName}/{$path}";
 }
 /**
  * Convert a XOOPS path to a physical one
  */
 function path($url, $virtual = false)
 {
     // If the URL begins with protocol:// then remove it
     if ($pos = strpos($url, '://')) {
         $url = substr($url, $pos + 3);
     }
     $parts = explode('#', $url);
     if (count($parts) == 1) {
         if (substr($parts[0], 0, 1) == '/') {
             $parts[0] = substr($parts[0], 1);
         }
         $parts = explode('/', $parts[0], 2);
         if (!$virtual) {
             return !isset($this->paths[$parts[0]]) ? '' : $this->paths[$parts[0]][0] . '/' . $parts[1];
         } else {
             if (!isset($this->paths[$parts[0]][1])) {
                 return false;
             }
             if (empty($this->paths[$parts[0]][1])) {
                 return $this->baseLocation . '/' . $parts[1];
             }
             return $this->baseLocation . '/' . $this->paths[$parts[0]][1] . '/' . $parts[1];
         }
     } else {
         $root = XOS::classVar($parts[0], 'xoBundleRoot');
         @(list($location, $query) = explode('?', $parts[1]));
         if (empty($location) || strpos($location, '/') !== false) {
             return $this->path($root . '/' . $location, $virtual);
         }
         if ($module = $this->loadModule($parts[0])) {
             $uri = $module->xoBundleRoot;
             if (isset($module->moduleLocations[$location])) {
                 $uri .= $module->moduleLocations[$location]['scriptFile'];
             } else {
                 trigger_error("Unknown location {$parts[0]}#{$location}", E_USER_WARNING);
                 return false;
             }
             if (isset($query)) {
                 $uri .= (strpos($uri, '?') ? '&' : '?') . $query;
             }
             return $this->path($uri, $virtual);
         }
     }
     return false;
 }
 /**
  * Generates widget insertion code
  *
  * This method generates the widget instanciation code to be inserted in the compiled template.
  * It is called for each <object> tag encountered during compilation of XML templates, or by the xoWidget
  * compiler plug-in.
  * 
  * @param array $args
  * @return string PHP code to be inserted in the compiled template
  */
 function insertWidget($args)
 {
     global $xoops;
     if (isset($args['assign'])) {
         $targetVar = $args['assign'];
         unset($args['assign']);
     } else {
         $targetVar = '';
     }
     $bundleId = $args['bundleId'];
     unset($args['bundleId']);
     $realBundleId = substr($bundleId, 1, strlen($bundleId) - 2);
     $code = "\n";
     // Get this widget stylesheet and javascript properties
     if ($css = XOS::classVar($realBundleId, 'stylesheet')) {
         $css = $realBundleId . '#' . $css;
         $attrs = array('type' => 'text/css', 'href' => $xoops->url($this->template_engine->currentTheme->resourcePath($css)));
         $code .= '$this->currentTheme->setMeta( "stylesheet", "' . $css . '", ' . var_export($attrs, true) . ");\n";
     }
     if ($js = XOS::classVar($realBundleId, 'javascript')) {
         $js = $realBundleId . '#' . $js;
         $attrs = array('type' => 'text/javascript', 'src' => $xoops->url($this->template_engine->currentTheme->resourcePath($js)));
         $code .= '$this->currentTheme->setMeta( "script", "' . $js . '", ' . var_export($attrs, true) . ");\n";
     }
     $create = 'XOS::create( ' . $bundleId;
     if (!empty($args)) {
         $create .= ", array(\n";
         foreach ($args as $prop => $value) {
             $create .= "\t'{$prop}' => " . $value . ",\n";
         }
         $create .= ')';
     }
     $create .= " )";
     if (!$targetVar) {
         $code .= "\$widget = {$create};\n";
         $code .= "echo ( \$widget ? \$widget->render() : \"Failed to instanciate {$bundleId} widget.\" );\n";
     } else {
         $code .= "\$this->_tpl_vars[{$targetVar}] = {$create};\n";
     }
     return $code;
 }