Beispiel #1
0
 public function defaultRun($request)
 {
     $this->data['env'] = $this->config['env'];
     $this->data['domain'] = $this->config['domain'];
     $this->data['mcookie'] = $_COOKIE['m'];
     $this->data['is_mobile'] = $this->is_mobile;
     $this->data['web_root'] = $this->config['web_root'];
     $this->data['token'] = $this->getCsrfToken();
     $this->data['request'] = $request;
     $this->data['menu'] = new Menu();
     $this->data['action'] = $this->action;
     $this->data['preload_action'] = isset($request['preload_action']) ? $request['preload_action'] : "index";
     if ($this->action == 'index') {
         $this->action = "welcome";
         $this->data['action'] = 'welcome';
     }
     if ($this->config['env'] == 'prod') {
         require_once $this->config['zombie_root'] . "/config/version.php";
         $this->data['version'] = version();
     } else {
         $this->data['console'] = new Console();
     }
     $preload_class = underscoreToClass($this->action);
     if (class_exists($preload_class)) {
         $this->data['preload'] = new $preload_class();
     } else {
         $this->view = '404';
     }
 }
 function prepare()
 {
     $class = underscoreToClass($this->app);
     $options = array('template' => $this->template, 'app' => $this->app, 'create_app' => true, 'create_model' => true, 'views' => array('index'), 'slug' => $this->app, 'class' => $class, 'model_class' => $class . "Model");
     $this->options = array_merge($this->options, $options);
     $this->replace = array('SLUG' => $this->app, 'CLASS_NAME' => $class);
     if (isset($this->options['table'])) {
         $this->replace['MODEL_CLASS_NAME'] = underscoreToClass($this->options['table'] . "_model");
     }
     $this->files = array();
     array_push($this->files, new TemplateFile($this->config['zombie_root'] . "/apps/" . $this->app . "/" . $this->app . ".php", $this->config['zombie_root'] . $this->base_dir . "/" . $this->template . "/app/app.php"));
 }
Beispiel #3
0
function cliMain($argv)
{
    $argc = count($argv);
    if ($argc < 2) {
        die("Usage: zombie.php <action> <option=value> ...\n" . "Availabe actions:\n" . "\tcompile\n" . "\tcreate-app\n");
    }
    $action = $argv[1];
    $options = array();
    for ($i = 2; $i < $argc; ++$i) {
        $opt = explode("=", $argv[$i], 2);
        if (count($opt) == 2) {
            $options[$opt[0]] = $opt[1];
        } else {
            $options[$opt[0]] = true;
        }
    }
    if ($action == "generate-app") {
        if (!isset($options['app'])) {
            die("Usage: zombie.php generate-app app=<app name> [template=<template_name>] [option=<value>] ...\n");
        }
        $template = isset($options['template']) ? $options['template'] : 'basic';
        $base_dir = "/config/generator";
        $template_file = realpath(__DIR__ . "/../config/generator/" . $template) . "/template.php";
        if (!file_exists($template_file)) {
            $base_dir = "/zombie-core/generator/";
            $template_file = __DIR__ . "/generator/" . $template . "/template.php";
            if (!file_exists($template_file)) {
                die("unknown template: " . $template . "\n");
            }
        }
        $app = $options['app'];
        require_once __DIR__ . "/generator/ZombieTemplate.php";
        require $template_file;
        $template_class = underscoreToClass($template . "_template");
        $template = new $template_class($template, $app, $base_dir, $options);
        $template->run();
    } else {
        if ($action == "compile") {
            require __DIR__ . "/util/compile/compile.php";
            compile($options);
        } else {
            if ($action == "migrate") {
                require __DIR__ . "/util/migrate/migrate.php";
                migrate($options);
            } else {
                if ($action == "deploy") {
                    require __DIR__ . "/util/deploy.php";
                    deploy();
                } else {
                    if ($action == "install-package") {
                        if (!isset($options['name'])) {
                            die("Usage: zombie.php install-package name=<package name>\n");
                        }
                        require __DIR__ . "/packages/installer.php";
                        installPackage($options['name']);
                    } else {
                        if ($action == "kachow") {
                            echo "kachow!\n";
                        } else {
                            echo "Error: unknown action '" . $action . "'.\n";
                        }
                    }
                }
            }
        }
    }
}
Beispiel #4
0
 public function templatePrepare()
 {
     if (!isset($this->options['table'])) {
         die("table option required:\nzombie.php generate-app app=<app> template=mysql_crud table=<table>\n");
     }
     $config = getZombieConfig();
     $this->addView('index');
     $this->addView('edit');
     $this->addScript('main');
     $this->addModel();
     $this->replace['AJAX_COMMA_SEP_FIELDS'] = '';
     $this->replace['AJAX_COMMA_SEP_FIELDS_WID'] = '';
     $this->replace['ENUM_OPTIONS'] = '';
     $this->replace['FIELD_NAME'] = '';
     $this->replace['FIELD_NAME_NICE'] = '';
     $this->replace['HTML_EDIT_FIELDS'] = '';
     $this->replace['HTML_EDIT_FIELDS'] = '';
     $this->replace['HTML_FIELDS_TD'] = '';
     $this->replace['HTML_FIELDS_TH'] = '';
     $this->replace['INSERT_FIELDS_COMMA_SEP'] = '';
     $this->replace['INSERT_DOLLAR_PARAMS'] = '';
     $this->replace['INSERT_FUNC_PARAMS_APP'] = '';
     $this->replace['INSERT_FUNC_PARAMS_MODEL'] = '';
     $this->replace['INSERT_REQUEST_PARAMS'] = '';
     $this->replace['INSERT_REQUEST_PARAMS'] = '';
     $this->replace['JOIN_FIELD'] = '';
     $this->replace['MODEL_GET_ALL'] = '';
     $this->replace['MYSQL_JOINS'] = '';
     $this->replace['MYSQL_ADD_PARAMS'] = '';
     $this->replace['QUERY_INSERT_ADD_PARAMS'] = '';
     $this->replace['REQUIRED_CLASS'] = '';
     $this->replace['SET_FIELDS_COMMA_SEP'] = '';
     $this->replace['SQL_FIELDS_COMMA_SEP'] = '';
     $this->replace['SQL_JOINS'] = '';
     $this->replace['TABLE_NAME'] = $this->options['table'];
     $this->replace['UPDATE_REQUEST_PARAMS'] = '';
     $query = new MysqlQuery();
     $table_desc = $query->describe($this->options['table']);
     $i = 0;
     $field_templates = array();
     foreach ($table_desc as $sql_field) {
         ++$i;
         $validators = array();
         $field_name = $sql_field['Field'];
         $field_name_nice = ucwords(str_replace("_", " ", $field_name));
         $field_type = $sql_field['Type'];
         $is_join = false;
         if ($sql_field['Null'] == 'NO') {
             array_push($validators, "required");
         }
         if ($field_type == "text") {
             $html_type = "textarea";
             $field_template = $this->getField("textarea");
         } else {
             if (preg_match('/_id$/', $field_name)) {
                 $html_type = "select";
                 $is_join = true;
                 $field_template = $this->getField("table_select");
             } else {
                 if (strpos($field_type, 'enum') === 0) {
                     $html_type = "select";
                     $field_template = $this->getField("enum_select");
                 } else {
                     $html_type = "input";
                     $field_template = $this->getField("textbox");
                     $matches = array();
                     if (preg_match('/char\\((\\d+)\\)$/', $field_type, $matches)) {
                         $len = $matches[1];
                         $v = "maxlen=" . $len;
                         array_push($validators, $v);
                     } else {
                         if (preg_match('/^int/', $field_type, $matches)) {
                             array_push($validators, "int");
                         } else {
                             if (preg_match('/^decimal|^float|^double/', $field_type, $matches)) {
                                 array_push($validators, "number");
                             }
                         }
                     }
                 }
             }
         }
         if (count($validators) > 0) {
             $this->replace['VALIDATE'] = "validate=\"" . implode(",", $validators) . "\" ";
         } else {
             $this->replace['VALIDATE'] = "";
         }
         $this->replace['INSERT_FIELDS_COMMA_SEP'] .= $sql_field['Field'] . "\n                      , ";
         $this->replace['UPDATE_REQUEST_PARAMS'] .= "\$request['{$field_name}'],\n                      ";
         $this->replace['SQL_FIELDS_COMMA_SEP'] .= $this->options['table'] . "." . $sql_field['Field'] . "\n                     , ";
         $this->replace['AJAX_COMMA_SEP_FIELDS_WID'] .= '                      "' . $field_name . '":form.find("' . $html_type . '[name=' . $field_name . "]\").val(),\n";
         if ($field_name != 'id') {
             $this->replace['AJAX_COMMA_SEP_FIELDS'] .= '                      "' . $field_name . '":form.find("' . $html_type . '[name=' . $field_name . "]\").val(),\n";
             if (!$is_join) {
                 $this->replace['HTML_FIELDS_TD'] .= "      <td><?= \$row['{$field_name}'] ?></td>\n";
                 $this->replace['HTML_FIELDS_TH'] .= "      <th>{$field_name_nice}</th>\n";
             }
             $this->replace['INSERT_FUNC_PARAMS_APP'] .= '$request[\'' . $field_name . "'],\n" . str_repeat(" ", 32 + strlen($this->replace['TABLE_NAME']));
             $this->replace['INSERT_FUNC_PARAMS_MODEL'] .= '$' . $field_name . ",\n                          ";
             $this->replace['MYSQL_ADD_PARAMS'] .= "\n             ->addParam(\$" . $field_name . ")";
             $this->replace['QUERY_INSERT_ADD_PARAMS'] .= '      $query->addParam($' . $field_name . ');' . "\n";
             $this->replace['QUERY_INSERT_ADD_PARAMS'] .= '      $query->addParam($' . $field_name . ');' . "\n";
             $this->replace['SET_FIELDS_COMMA_SEP'] .= $sql_field['Field'] . " = \${$i} \n                  , ";
             $this->replace['INSERT_REQUEST_PARAMS'] .= "\$request['{$field_name}'],\n                      ";
             $this->replace['INSERT_DOLLAR_PARAMS'] .= "\$" . ($i - 1) . ", ";
             $this->replace['FIELD_NAME'] = $field_name;
             $this->replace['FIELD_NAME_NICE'] = $field_name_nice;
             if ($field_type == "text") {
                 $field_template->replace($this->replace);
                 $this->replace['HTML_EDIT_FIELDS'] .= $field_template->getContents();
             } else {
                 if ($is_join) {
                     $other_table = substr($field_name, 0, strlen($field_name) - 3);
                     $other_table_model_class = underscoreToClass($other_table . '_' . 'model');
                     $this->replace['MODEL_GET_ALL'] .= "      \${$other_table}_model = new {$other_table_model_class}();\n" . "      \$this->data['{$other_table}'] = \${$other_table}_model->getAll();\n";
                     $join_field = $this->getTableJoinField($other_table);
                     $this->replace['JOIN_FIELD'] = $join_field;
                     if (strlen($join_field) > 0) {
                         $this->replace['OTHER_TABLE_NAME'] = $other_table;
                         $this->replace['MYSQL_JOINS'] .= "\n             ->leftJoin('{$other_table} ON {$other_table}.id = {$other_table}_id')";
                         $this->replace['SQL_JOINS'] .= "         LEFT JOIN {$other_table} ON {$other_table}.id = {$other_table}_id\n";
                         $this->replace['SQL_FIELDS_COMMA_SEP'] .= "{$other_table}.{$join_field} {$other_table}_{$join_field}\n                     , ";
                         $this->replace['HTML_FIELDS_TD'] .= "      <td><?= \$row['{$other_table}_{$join_field}'] ?></td>\n";
                         $field_name_nice = ucwords(str_replace("_", " ", $other_table . " " . $join_field));
                         $this->replace['FIELD_NAME_NICE'] = $field_name_nice;
                         $this->replace['HTML_FIELDS_TH'] .= "      <th>{$field_name_nice}</th>\n";
                         $field_template->replace($this->replace);
                         $this->replace['HTML_EDIT_FIELDS'] .= $field_template->getContents();
                     }
                 } else {
                     if (strpos($field_type, 'enum') === 0) {
                         $enums = explode(",", str_replace("'", "", substr(substr($field_type, 5), 0, -1)));
                         $this->replace['ENUM_OPTIONS'] = "";
                         foreach ($enums as $enum) {
                             $this->replace['ENUM_OPTIONS'] .= "            <option value=\"{$enum}\">{$enum}</option>\n";
                         }
                         $field_template->replace($this->replace);
                         $this->replace['HTML_EDIT_FIELDS'] .= $field_template->getContents();
                     } else {
                         $field_template->replace($this->replace);
                         $this->replace['HTML_EDIT_FIELDS'] .= $field_template->getContents();
                     }
                 }
             }
         }
     }
     $this->replace['AJAX_COMMA_SEP_FIELDS'] = rtrim($this->replace['AJAX_COMMA_SEP_FIELDS'], "\n");
     $this->replace['AJAX_COMMA_SEP_FIELDS_WID'] = rtrim($this->replace['AJAX_COMMA_SEP_FIELDS_WID'], "\n");
     $this->replace['HTML_EDIT_FIELDS'] = rtrim($this->replace['HTML_EDIT_FIELDS'], "\n");
     $this->replace['MYSQL_JOINS'] = rtrim($this->replace['MYSQL_JOINS'], " \n");
     $this->replace['MYSQL_ADD_PARAMS'] = rtrim($this->replace['MYSQL_ADD_PARAMS'], " \n");
     $this->replace['SQL_JOINS'] = rtrim($this->replace['SQL_JOINS'], "\n");
     $this->replace['HTML_FIELDS_TH'] = rtrim($this->replace['HTML_FIELDS_TH'], "\n");
     $this->replace['HTML_FIELDS_TD'] = rtrim($this->replace['HTML_FIELDS_TD'], "\n");
     $this->replace['SQL_FIELDS_COMMA_SEP'] = rtrim($this->replace['SQL_FIELDS_COMMA_SEP'], " ,\n");
     $this->replace['SET_FIELDS_COMMA_SEP'] = rtrim($this->replace['SET_FIELDS_COMMA_SEP'], " ,\n");
     $this->replace['INSERT_DOLLAR_PARAMS'] = rtrim($this->replace['INSERT_DOLLAR_PARAMS'], " ,");
     $this->replace['INSERT_FIELDS_COMMA_SEP'] = rtrim($this->replace['INSERT_FIELDS_COMMA_SEP'], " ,\n");
     $this->replace['INSERT_REQUEST_PARAMS'] = rtrim($this->replace['INSERT_REQUEST_PARAMS'], " ,\n");
     $this->replace['UPDATE_REQUEST_PARAMS'] = rtrim($this->replace['UPDATE_REQUEST_PARAMS'], " ,\n");
     $this->replace['INSERT_FUNC_PARAMS_APP'] = rtrim($this->replace['INSERT_FUNC_PARAMS_APP'], " ,\n");
     $this->replace['INSERT_FUNC_PARAMS_MODEL'] = rtrim($this->replace['INSERT_FUNC_PARAMS_MODEL'], " ,\n");
 }
Beispiel #5
0
    class Tmp extends Controller
    {
        public function init()
        {
            $this->allowFormat("json");
            $this->allowFormat("xml");
            $this->allowFormat("serial");
        }
    }
    $c = new Tmp();
    $c->run();
}
if (isset($_GET['app'])) {
    // sanitize the app name: only letters, numbers, and underscores
    $app = preg_replace('/[^0-9a-zA-Z_]/', '', $_GET['app']);
    $app_file = "../apps/{$app}/{$app}.php";
    if (file_exists($app_file)) {
        require_once $app_file;
        $app_class = underscoreToClass($app);
        if (class_exists($app_class)) {
            $app = new $app_class();
            $app->run();
        } else {
            notFound();
        }
    } else {
        notFound();
    }
} else {
    notFound();
}
Beispiel #6
0
 /**
  * If no format is specified in the url, tell zombie which to use.
  * Provided formats include html, page, json, serial, and xml.
  * @param string $format
  */
 public function defaultFormat($format)
 {
     $this->allowFormat($format);
     $format = underscoreToClass($format);
     if ($this->use_default_renderer) {
         $this->format = $format;
         $this->renderer = $format;
     }
 }