public function buildApp($hash) { LINB::checkArgs($hash, array('string' => array('pathName' => NULL, 'className' => NULL))); $io = LINB::SC('IO'); $prjpath = self::PROJECTS_PATH . DIRECTORY_SEPARATOR . $hash->pathName; //replace exists project file $path = $prjpath; if ($io->exists($path)) { throw new LINB_E("{$path} exists already!"); } //$io->delete($path); $io->dirMake($path, true); $template = $io->getString(self::TEMPLATE_HTML); //html page file file_put_contents($path . DIRECTORY_SEPARATOR . self::INDEX . self::FILE_HTML, LINB::parseTemplate($template, $hash)); $template = $io->getString(self::TEMPLATE_DEBUG); //html page file file_put_contents($path . DIRECTORY_SEPARATOR . self::DEBUG . self::FILE_HTML, LINB::parseTemplate($template, $hash)); //img path $io->dirMake($path . DIRECTORY_SEPARATOR . self::IMG_PATH, true); $rpath = $path; //base class path $path = $rpath . DIRECTORY_SEPARATOR . $hash->className; $io->dirMake($path, true); //js path $path = $path . DIRECTORY_SEPARATOR . self::JS_PATH; $io->dirMake($path, true); $template = $io->getString(self::TEMPLATE_JS); // js class file file_put_contents($path . DIRECTORY_SEPARATOR . self::INDEX . self::FILE_JS, LINB::parseTemplate($template, $hash)); $path = $rpath . DIRECTORY_SEPARATOR . self::LOCATE_PATH; //lang path $io->dirMake($path, true); $io->setString($path . DIRECTORY_SEPARATOR . self::EN_PATH, '{}'); unset($io); return $prjpath; }
/** * Parse a template to string * * @param string $template * tag: {tag} * or * tag pairs: {tag} string... {/tag} * @param array $data * array( * 'a' => 'a', * array( * array('i'=>'i','j1'=>'j1'), * array('i'=>'i','j2'=>'j2'), * array('i'=>'i','j3'=>'j3') * ) * ) * @return string */ public static function parseTemplate($template, $data, $tag_l = '{', $tag_r = '}') { $str = $template; if (is_object($data)) { $data = (array) $data; } foreach ($data as $key => $val) { if (!is_array($val)) { $str = str_replace($tag_l . $key . $tag_r, $val, $str); } else { if (preg_match("|" . $tag_l . $key . $tag_r . "(.+)" . $tag_l . '/' . $key . $tag_r . "|s", $template, $match)) { $str2 = ''; foreach ($val as $item) { $str2 .= LINB::parseTemplate($match['1'], $item); } $str = str_replace($match['0'], $str2, $str); } } } return $str; }
public function stimulate(&$hash) { LINB::checkArgs($hash, array('string' => array('action' => 'open', 'path' => 'linbApp', 'className' => 'App', 'content' => '', 'theme' => 'default', 'lang' => 'en'))); $io = LINB::SC('IO'); //only input relative path, and not ./ or ../ allowed switch ($hash->action) { case 'fetchwebfile': $content = file_get_contents($hash->path); if ($content !== false) { return $content; } else { throw new LINB_E("Error: Can\\'t get " . $hash->path); } break; case 'downloadjs': header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"jsLinb.Class.js\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($hash->content)); header("Pragma: public"); header("Expires: 0"); echo $hash->content; return; break; case 'downloadhtml': $template = $io->getString(self::TEMPLATE_SINHTML); $template = LINB::parseTemplate($template, array("libpath" => "http://jslinb.appspot.com/", "clsName" => $hash->clsName, "content" => $hash->content, "theme" => $hash->theme, "lang" => $hash->lang)); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"linbApp.html\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($template)); header("Pragma: public"); header("Expires: 0"); echo $template; return; break; case 'downloadzip2': $zip = new zip(); $fileName = 'jsLinbApp.zip'; $rootName = 'runtime'; $path = self::BASE_PATH; $path2 = 'index.html'; $template = $io->getString(self::TEMPLATE_SINHTML); $template = LINB::parseTemplate($template, array("libpath" => "", "clsName" => $hash->clsName, "content" => $hash->content, "theme" => $hash->theme, "lang" => $hash->lang)); $zip->addFile($template, $path2); $path2 = $rootName . DIRECTORY_SEPARATOR . 'loading.gif'; $f = file_get_contents($path . DIRECTORY_SEPARATOR . $path2); $zip->addFile($f, $path2); $path2 = $rootName . DIRECTORY_SEPARATOR . 'addBuilderLink.js'; $f = file_get_contents($path . DIRECTORY_SEPARATOR . $path2); $zip->addFile($f, $path2); $io->_zip($path, $rootName . DIRECTORY_SEPARATOR . 'jsLinb' . DIRECTORY_SEPARATOR . 'Locale', $zip); $io->_zip($path, $rootName . DIRECTORY_SEPARATOR . 'jsLinb' . DIRECTORY_SEPARATOR . 'appearance', $zip); $path2 = $rootName . DIRECTORY_SEPARATOR . 'jsLinb' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'linb-all.js'; $f = file_get_contents($path . DIRECTORY_SEPARATOR . $path2); $zip->addFile($f, $path2); $fd = fopen($fileName, "wb"); $out = fwrite($fd, $zip->getZippedfile()); fclose($fd); $zip->forceDownload($fileName); @unlink($fileName); return; break; case 'savetoserver': $io->setString($hash->path, $hash->content); return array('OK' => true); //throw new LINB_E("You cant save file to this server!"); break; case 'del': foreach ($hash->path as $v) { $io->delete($v); } return array('OK' => true); break; case 'add': $file = $hash->path; if ($hash->type == 'file') { $file = $hash->path . '/' . $hash->filename; } if ($io->exists($io->absPath($file))) { throw new LINB_E("'{$file}' exists!"); } if (!$io->exists($hash->path)) { $io->dirMake($hash->path, true); } if ($hash->type == 'file') { $template = " "; if (substr($file, -3, 3) == self::FILE_JS) { $className = "==>specify_class_name_here"; try { // Get js class name $farr = explode('/js/', $file); if (isset($farr[0])) { $farr1 = explode('/', $farr[0]); if (isset($farr[1])) { $farr2 = explode(".", $farr[1]); if (isset($farr2[0])) { $className = $farr1[sizeof($farr1) - 1] . '.' . implode('.', explode('/', $farr2[0])); } } } } catch (Exception $e) { } $template = $io->getString(self::TEMPLATE_JS); $template = LINB::parseTemplate($template, array("className" => $className)); } $io->setString($io->absPath($file), $template); } return array('OK' => true); break; case 'save': $io->setString($hash->path, $hash->content); return array('OK' => true); break; case 'getfile': return array('file' => $io->getString($hash->path)); break; case 'open': $prjpath = $hash->path; if ($prjpath[0] == '.') { throw new LINB_E("Error: Can\\'t handle parent path!"); } $prjpath = str_replace("/", "\\", $prjpath); //$b = $io->dirList($prjpath); $b = $io->search("[a-zA-Z0-9].*", $prjpath, isset($hash->type) ? $hash->type : -1, isset($hash->deep) ? $hash->deep : 0); $root = str_replace("\\", "/", realpath('.')) . '/'; //ensure to return relative url format: '/' foreach ($b as &$v) { $v['location'] = str_replace("\\", "/", $v['location']); $v['location'] = str_replace($root, "", $v['location']); } unset($io); return $b; break; case 'release': $arr = explode(DIRECTORY_SEPARATOR, $hash->path); $name = array_pop($arr); $io->zipDir4Download($hash->path, $name . '.zip'); return; break; case 'upload': LINB::checkArgs($hash, array('string' => array('path' => null))); $uploader = LINB::SC('Uploader'); $uploader->set_type('image'); $name = ""; foreach ($_FILES as $file) { if (!empty($file['name'])) { $name = $file['name']; $save_path = $hash->path . '/'; $uploader->save($file, $save_path); break; } } unset($uploader); return array('OK' => true, 'name' => $name); break; } }
<?php include_once '../phpLinb/linb.php'; $io = LINB::SC('IO'); $template = $io->getString("template/singledebug.html"); $clsName = $_POST['clsName']; $clsName = get_magic_quotes_gpc() ? stripslashes($clsName) : $clsName; $content = $_POST['content']; $content = get_magic_quotes_gpc() ? stripslashes($content) : $content; $theme = $_POST['theme']; $theme = get_magic_quotes_gpc() ? stripslashes($theme) : $theme; $lang = $_POST['lang']; $lang = get_magic_quotes_gpc() ? stripslashes($lang) : $lang; $template = LINB::parseTemplate($template, array("clsName" => $clsName, "content" => $content, "theme" => $theme, "lang" => $lang)); echo $template;