Beispiel #1
0
 public function __destruct()
 {
     /* On efface le model de la base tampon et on vide la base */
     $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'models' . DS . Inflector::lower($this->to->db) . DS . ucfirst(Inflector::lower($this->to->table)) . '.php';
     File::delete($modelFile);
     $this->to->drop();
 }
Beispiel #2
0
 public function __construct($db)
 {
     $this->orm = $db;
     $this->db = $db->db();
     $this->table = $db->table();
     $dir = Config::get('dir.blizz.store', session_save_path());
     if (!is_dir($dir)) {
         File::mkdir($dir);
     }
     $dir .= DS . Inflector::urlize(Inflector::uncamelize($this->db));
     if (!is_dir($dir)) {
         File::mkdir($dir);
     }
     $this->dir = $dir . DS . Inflector::urlize(Inflector::uncamelize($this->table));
     if (!is_dir($this->dir)) {
         File::mkdir($this->dir);
     }
     $file = $this->dir . DS . 'data.db';
     $new = false;
     if (!is_file($file)) {
         File::create($file);
         $new = true;
         File::put($this->dir . DS . 'age.blizz', '');
     }
     $link = new SQLite3($file);
     Now::set("blizz.link.{$this->db}.{$this->table}", $link);
     if ($new) {
         $this->init();
     }
 }
Beispiel #3
0
 public function model($data = [])
 {
     $view = false;
     $db = $this->db;
     $table = $this->table;
     $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql');
     }
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models');
     }
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db));
     }
     if (!File::exists($modelFile)) {
         File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel', File::read(__DIR__ . DS . 'dbModel.tpl')));
     }
     $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel';
     if (!class_exists($class)) {
         require_once $modelFile;
     }
     $model = $this;
     if (true === $view) {
         $model = self::instance($db, $table);
     }
     return new $class($model, $data);
 }
Beispiel #4
0
 public function background()
 {
     $file = realpath(APPLICATION_PATH . DS . '..' . DS . 'public' . DS . 'scripts' . DS . 'schedule.php');
     if (File::exists($file)) {
         $cmd = 'php ' . $file;
         lib('utils')->backgroundTask($cmd);
     }
 }
Beispiel #5
0
 public function __construct($ns = 'core.cache')
 {
     if (!is_dir(STORAGE_PATH . DS . 'kit')) {
         File::mkdir(STORAGE_PATH . DS . 'kit');
     }
     $file = STORAGE_PATH . DS . 'kit' . DS . $ns . '.db';
     $this->lite = new SQLite3($file);
     $q = "CREATE TABLE IF NOT EXISTS content (k VARCHAR PRIMARY KEY, v, ts);";
     $this->raw($q);
 }
Beispiel #6
0
 public function getOptionsFromMarket($market_id)
 {
     if (!is_integer($market_id)) {
         throw new Exception("market_id must be an integer id.");
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'options' . DS . $market_id . '.php';
     if (File::exists($file)) {
         $options = (include $file);
         return $options;
     }
     return [];
 }
Beispiel #7
0
 public function __construct($db, $table)
 {
     $file = Config::get('mlite.dir.' . $db, STORAGE_PATH . DS . $db . '.db');
     $new = !is_file($file);
     if (!is_file($file)) {
         File::create($file);
     }
     $link = new SQLite3($file);
     Now::set("lite.link.{$db}.{$table}", $link);
     if ($new) {
         $q = "CREATE TABLE IF NOT EXISTS infosdb (data_key VARCHAR PRIMARY KEY, data_value);";
         $res = $link->exec($q);
     }
     $this->table = $table;
     $this->database = $db;
 }
Beispiel #8
0
 /**
  *
  * @method __construct
  *
  * @param  string
  * @param  array
  */
 public function __construct(array $data = [])
 {
     $ns = $this->ns = $this->forever();
     $file = Config::get('cachemelite.dir.' . $ns, STORAGE_PATH . DS . $ns . '_cache.db');
     $new = !is_file($file);
     if (!is_file($file)) {
         File::create($file);
     }
     $db = new SQLite3($file);
     Now::set("cachemelite.link.{$ns}", $db);
     $q = "CREATE TABLE IF NOT EXISTS cachedb (data_key VARCHAR PRIMARY KEY, data_value);";
     $res = $this->db->exec($q);
     if (!empty($data)) {
         foreach ($data as $k => $v) {
             $this->set($k, $v);
         }
     }
 }
Beispiel #9
0
 public function model($data = [])
 {
     $db = $this->db;
     $table = $this->table;
     $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db));
     }
     if (!File::exists($modelFile)) {
         File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast', File::read(__DIR__ . DS . 'dbModel.tpl')));
     }
     $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast';
     if (!class_exists($class)) {
         require_once $modelFile;
     }
     $model = $this;
     return new $class($model, $data);
 }
Beispiel #10
0
 public function read($id, $default = null)
 {
     $key = $this->key . '::' . $id;
     try {
         $result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => $key));
     } catch (NoSuchKeyException $e) {
         if (!strstr($id, 'file::')) {
             $data = File::read($this->findFile($id));
         } else {
             $data = File::read(str_replace('thinseparator', '', str_replace('file::', '', $id)));
         }
         if (strlen($data)) {
             $this->write($id, $data);
             return $data;
         } else {
             return $default;
         }
     }
     return $result['Body'];
 }
Beispiel #11
0
 public static function dispatch()
 {
     static::$method = Request::method();
     $uri = substr(str_replace('/ws/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     if (count($tab) < 3) {
         Api::forbidden();
     }
     $namespace = current($tab);
     $controller = $tab[1];
     $action = $tab[2];
     $tab = array_slice($tab, 3);
     $count = count($tab);
     if (0 < $count && $count % 2 == 0) {
         for ($i = 0; $i < $count; $i += 2) {
             $_REQUEST[$tab[$i]] = $tab[$i + 1];
         }
     }
     $file = APPLICATION_PATH . DS . 'webservices' . DS . $namespace . DS . $controller . '.php';
     if (!File::exists($file)) {
         Api::NotFound();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Ws';
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         Api::NotFound();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }
Beispiel #12
0
 public function read($id, $default = null)
 {
     $key = $this->key . '::' . $id;
     $row = $this->collection->findOne(function ($query) use($key) {
         $query->where('key', $key);
     });
     if ($row) {
         return isAke($row, 'value', $default);
     } else {
         if (!strstr($id, 'file::')) {
             $data = File::read($this->findFile($id));
         } else {
             $data = File::read(str_replace('thinseparator', '', str_replace('file::', '', $id)));
         }
         if (strlen($data)) {
             $this->write($id, $data);
             return $data;
         } else {
             return $default;
         }
     }
     return $default;
 }
Beispiel #13
0
 public function csvByZone($address)
 {
     ini_set('memory_limit', '1024M');
     set_time_limit(0);
     $file = '/home/gerald/Bureau/' . Inflector::urlize($address) . '_' . date('dmYHis') . '.csv';
     touch($file);
     $zone = rdb('geo', 'zone')->where(['address', '=i', $address])->first(true);
     if ($zone) {
         File::append($file, implode(';', ['category', 'name', 'address', 'lat', 'lng', 'altitude', 'zip', 'city', 'phone', 'mail', 'url']) . "\n");
         $pivots = $zone->pivots(rdb('geo', 'etablissement')->model())->exec(true);
         foreach ($pivots as $pivot) {
             $etab = $pivot->etablissement(true);
             if ($zone) {
                 $relations = $etab->pivots(rdb('geo', 'service'))->exec(true);
                 foreach ($relations as $relation) {
                     $sp = $relation->service(true);
                     if ($sp) {
                         $item = [];
                         $item['category'] = str_replace([', ', ','], '-', $sp->label);
                         $item['name'] = str_replace([', ', ','], '-', $etab->name);
                         $item['address'] = str_replace([', ', ','], '-', $etab->address);
                         $item['lat'] = str_replace([', ', ','], '.', $etab->lat);
                         $item['lng'] = str_replace([', ', ','], '.', $etab->lng);
                         $item['altitude'] = str_replace([', ', ','], '.', $etab->altitude);
                         $item['zip'] = str_replace([', ', ','], '-', $etab->zip);
                         $item['city'] = str_replace([', ', ','], '-', $etab->city);
                         $item['phone'] = str_replace([', ', ','], '-', $etab->phone);
                         $item['mail'] = str_replace([', ', ','], '-', $etab->mail);
                         $item['url'] = str_replace([', ', ','], '-', $etab->url);
                         File::append($file, implode(';', $item) . "\n");
                     }
                 }
             }
         }
     }
 }
Beispiel #14
0
 private function parse($pageConfig, $htmlTemplate, $htmlPage)
 {
     eval($pageConfig);
     $code = str_replace('@@content', $htmlPage, $htmlTemplate);
     $code = str_replace(array('{{', '}}'), array('<?php', '?>'), $code);
     if (strstr($code, '@@partial')) {
         $partials = $this->getPartials($code);
         if (count($partials)) {
             foreach ($partials as $partial) {
                 $partialFile = $this->dir . DS . 'partials' . DS . $partial . '.html';
                 if (File::exists($partialFile)) {
                     $code = str_replace("@@partial({$partial})", fgc($partialFile), $code);
                 }
             }
         }
     }
     $vars = $this->getVars($code);
     $url = container()->getUrlsite();
     $assets = '/content/' . SITE_NAME . '/themes/' . $this->theme . '/assets';
     if (count($vars)) {
         foreach ($vars as $var) {
             if (!isset(${$var})) {
                 $value = CmsConfig::get($var, false);
                 if (false === $value) {
                     $value = isAke($_REQUEST, $var, false);
                     if (false === $value) {
                         $value = '';
                     }
                 }
             } else {
                 $value = ${$var};
             }
             $code = str_replace("%({$var})%", $value, $code);
         }
     }
     return $code;
 }
Beispiel #15
0
    private function export($type, $rows)
    {
        $fieldInfos = isAke($this->config, 'fields');
        $fields = $this->fields();
        if ('excel' == $type) {
            $excel = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns="http://www.w3.org/TR/REC-html40">

            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <meta name="ProgId" content="Excel.Sheet">
                <meta name="Generator" content="Microsoft Excel 11">
                <style id="Classeur1_17373_Styles">
                <!--table
                    {mso-displayed-decimal-separator:"\\,";
                    mso-displayed-thousand-separator:" ";}
                .xl1517373
                    {padding-top:1px;
                    padding-right:1px;
                    padding-left:1px;
                    mso-ignore:padding;
                    color:windowtext;
                    font-size:10.0pt;
                    font-weight:400;
                    font-style:normal;
                    text-decoration:none;
                    font-family:Arial;
                    mso-generic-font-family:auto;
                    mso-font-charset:0;
                    mso-number-format:General;
                    text-align:general;
                    vertical-align:bottom;
                    mso-background-source:auto;
                    mso-pattern:auto;
                    white-space:nowrap;}
                .xl2217373
                    {padding-top:1px;
                    padding-right:1px;
                    padding-left:1px;
                    mso-ignore:padding;
                    color:#FFFF99;
                    font-size:10.0pt;
                    font-weight:700;
                    font-style:normal;
                    text-decoration:none;
                    font-family:Arial, sans-serif;
                    mso-font-charset:0;
                    mso-number-format:General;
                    text-align:center;
                    vertical-align:bottom;
                    background:#003366;
                    mso-pattern:auto none;
                    white-space:nowrap;}
                -->
                </style>
            </head>

                <body>
                <!--[if !excel]>&nbsp;&nbsp;<![endif]-->

                <div id="Classeur1_17373" align="center" x:publishsource="Excel">

                <table x:str border="0" cellpadding="0" cellspacing="0" width=640 style="border-collapse:
                 collapse; table-layout: fixed; width: 480pt">
                 <col width="80" span=8 style="width: 60pt">
                 <tr height="17" style="height:12.75pt">
                  ##headers##
                 </tr>
                 ##content##
                </table>
                </div>
            </body>
        </html>';
            $tplHeader = '<td class="xl2217373">##value##</td>';
            $tplData = '<td>##value##</td>';
            $headers = [];
            foreach ($fields as $field) {
                $fieldSettings = isAke($fieldInfos, $field);
                $exportable = isAke($fieldSettings, 'is_exportable', true);
                $label = isAke($fieldSettings, 'label', ucfirst($field));
                if (true === $exportable) {
                    $headers[] = \Thin\Html\Helper::display($label);
                }
            }
            $xlsHeader = '';
            foreach ($headers as $header) {
                $xlsHeader .= str_replace('##value##', $header, $tplHeader);
            }
            $excel = str_replace('##headers##', $xlsHeader, $excel);
            $xlsContent = '';
            foreach ($rows as $item) {
                $xlsContent .= '<tr>';
                foreach ($fields as $field) {
                    $fieldSettings = isAke($fieldInfos, $field);
                    $exportable = isAke($fieldSettings, 'is_exportable', true);
                    if (true === $exportable) {
                        $value = isAke($item, $field, '&nbsp;');
                        if (Arrays::exists('content_list', $fieldSettings)) {
                            $closure = $fieldSettings['content_list'];
                            if (is_callable($closure)) {
                                $value = call_user_func_array($closure, array($item));
                            }
                        }
                        if (empty($value)) {
                            $value = '&nbsp;';
                        }
                        $xlsContent .= str_replace('##value##', \Thin\Html\Helper::display($value), $tplData);
                    }
                }
                $xlsContent .= '</tr>';
            }
            $excel = str_replace('##content##', $xlsContent, $excel);
            $name = 'extraction_' . $this->model->db . '_' . $this->model->table . '_' . date('d_m_Y_H_i_s') . '.xlsx';
            $file = TMP_PUBLIC_PATH . DS . $name;
            File::delete($file);
            File::put($file, $excel);
            Utils::go(URLSITE . '/tmp/' . $name);
        } elseif ('pdf' == $type) {
            $pdf = '<html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <link href="//fonts.googleapis.com/css?family=Abel" rel="stylesheet" type="text/css" />
                <title>Extraction ' . $this->model->db . ' - ' . $this->model->table . '</title>
                <style>
                    *
                    {
                        font-family: Abel, ubuntu, verdana, tahoma, arial, sans serif;
                        font-size: 11px;
                    }
                    h1
                    {
                        text-transform: uppercase;
                        font-size: 135%;
                    }
                    th
                    {
                        font-size: 120%;
                        color: #fff;
                        background-color: #394755;
                        text-transform: uppercase;
                    }
                    td
                    {
                        border: solid 1px #394755;
                    }

                    a, a:visited, a:hover
                    {
                        color: #000;
                        text-decoration: underline;
                    }
                </style>
            </head>
            <body>
                <center><h1>Extraction &laquo ' . $this->model->db . ' - ' . $this->model->table . ' &raquo;</h1></center>
                <p></p>
                <table width="100%" cellpadding="5" cellspacing="0" border="0">
                <tr>
                    ##headers##
                </tr>
                ##content##
                </table>
                <p>&copy; GP 1996 - ' . date('Y') . ' </p>
            </body>
            </html>';
            $tplHeader = '<th>##value##</th>';
            $tplData = '<td>##value##</td>';
            $headers = [];
            foreach ($fields as $field) {
                $fieldSettings = isAke($fieldInfos, $field, []);
                $exportable = isAke($fieldSettings, 'is_exportable', true);
                if (true === $exportable) {
                    $label = isAke($fieldSettings, 'label', ucfirst($field));
                    $headers[] = \Thin\Html\Helper::display($label);
                }
            }
            $pdfHeader = '';
            foreach ($headers as $header) {
                $pdfHeader .= str_replace('##value##', $header, $tplHeader);
            }
            $pdf = str_replace('##headers##', $pdfHeader, $pdf);
            $pdfContent = '';
            foreach ($rows as $item) {
                $pdfContent .= '<tr>';
                foreach ($fields as $field) {
                    $fieldSettings = isAke($fieldInfos, $field, []);
                    $exportable = isAke($fieldSettings, 'is_exportable', true);
                    if (true === $exportable) {
                        $value = isAke($item, $field, '&nbsp;');
                        if (Arrays::exists('content_list', $fieldSettings)) {
                            $closure = $fieldSettings['content_list'];
                            if (is_callable($closure)) {
                                $value = call_user_func_array($closure, array($item));
                            }
                        }
                        if (empty($value)) {
                            $value = '&nbsp;';
                        }
                        $pdfContent .= str_replace('##value##', \Thin\Html\Helper::display($value), $tplData);
                    }
                }
                $pdfContent .= '</tr>';
            }
            $pdf = str_replace('##content##', $pdfContent, $pdf);
            return \Thin\Pdf::make($pdf, "extraction_" . $this->model->db . "_" . $this->model->table . "_" . date('d_m_Y_H_i_s'), false);
        }
    }
Beispiel #16
0
 public static function run()
 {
     Request::$route = $route = Utils::get('appDispatch');
     container()->setRoute($route);
     $render = $route->getRender();
     $tplDir = $route->getTemplateDir();
     $module = $route->getModule();
     $controller = $route->getController();
     $action = $route->getAction();
     $alert = $route->getAlert();
     $page = container()->getPage();
     $isCms = !empty($page);
     if (!empty($render)) {
         $tplMotor = $route->getTemplateMotor();
         $tplDir = empty($tplDir) ? APPLICATION_PATH . DS . SITE_NAME . DS . 'app' . DS . 'views' : $tplDir;
         $tpl = $tplDir . DS . $render . '.phtml';
         if (File::exists($tpl)) {
             if ('Twig' == $tplMotor) {
                 if (!class_exists('Twig_Autoloader')) {
                     require_once 'Twig/Autoloader.php';
                 }
                 $tab = explode(DS, $tpl);
                 $file = Arrays::last($tab);
                 $path = repl(DS . $file, '', $tpl);
                 $loader = new \Twig_Loader_Filesystem($path);
                 $view = new \Twig_Environment($loader, array('cache' => CACHE_PATH, 'debug' => false, 'charset' => 'utf-8', 'strict_variables' => false));
                 container()->setView($view);
                 if ($action instanceof Closure) {
                     $action($view);
                 }
                 $params = null === container()->getViewParams() ? array() : container()->getViewParams();
                 echo $view->render($file, $params);
                 /* stats */
                 if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) {
                     echo View::showStats();
                 }
             } else {
                 $view = new View($tpl);
                 container()->setView($view);
                 if ($action instanceof Closure) {
                     $action($view);
                 }
                 $view->render();
                 /* stats */
                 if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) {
                     echo View::showStats();
                 }
             }
             return;
         }
     }
     $module = Inflector::lower($module);
     $controller = Inflector::lower($controller);
     $action = Inflector::lower($action);
     if (true === container()->getMultiSite()) {
         $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module;
     } else {
         $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module;
     }
     if (!is_dir($moduleDir)) {
         throw new Exception("The module '{$module}' does not exist.");
     }
     $controllerDir = $moduleDir . DS . 'controllers';
     if (!is_dir($controllerDir)) {
         throw new Exception("The controller '{$controller}' does not exist.");
     }
     $controllerFile = $controllerDir . DS . $controller . 'Controller.php';
     if (!File::exists($controllerFile)) {
         throw new Exception("The controller '{$controllerFile}' does not exist.");
     }
     require_once $controllerFile;
     $controllerClass = 'Thin\\' . $controller . 'Controller';
     $controller = new $controllerClass();
     $controller->view = new View($route->getView());
     if (null !== $alert) {
         $controller->view->alert($alert);
     }
     container()->setController($controller);
     $actions = get_class_methods($controllerClass);
     if (true === $isCms) {
         if (!Arrays::inArray($action, $actions)) {
             $action = 'page';
         }
     }
     container()->setAction($action);
     if (strstr($action, '-')) {
         $words = explode('-', $action);
         $newAction = '';
         for ($i = 0; $i < count($words); $i++) {
             $word = trim($words[$i]);
             if ($i > 0) {
                 $word = ucfirst($word);
             }
             $newAction .= $word;
         }
         $action = $newAction;
     }
     $actionName = $action . 'Action';
     if (Arrays::in('init', $actions)) {
         $controller->init();
     }
     if (Arrays::in('preDispatch', $actions)) {
         $controller->preDispatch();
     }
     if (!Arrays::in($actionName, $actions)) {
         throw new Exception("The action '{$actionName}' does not exist.");
     }
     $controller->{$actionName}();
     $controller->view->render();
     if (Arrays::inArray('postDispatch', $actions)) {
         $controller->postDispatch();
     }
     /* stats */
     if (null !== Utils::get("showStats")) {
         echo View::showStats();
     }
 }
Beispiel #17
0
 public function valid()
 {
     $file = $this->cursor . DS . $this->position . '.php';
     return File::exists($file);
 }
Beispiel #18
0
 public static function generate($model, $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'Crud' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = model($model);
         $crud = new Crud($db);
         File::delete($file);
         $tplModel = fgc(__DIR__ . DS . 'Model.tpl');
         $tplField = fgc(__DIR__ . DS . 'Field.tpl');
         $fields = $crud->fields();
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(array('##singular##', '##plural##', '##default_order##'), array($singular, $plural, $default_order), $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(array('##field##', '##label##'), array($field, $label), $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Beispiel #19
0
 /**
  * Destructor
  *
  * Delete all files created by syrup component run
  */
 function __destruct()
 {
     $preserveRunFolder = $this->preserveRunFolder;
     $fs = new Filesystem();
     foreach ($this->files as $file) {
         if ($file['preserve']) {
             $preserveRunFolder = true;
         }
         if (File::exists($file['file']) && File::is($file['file']) && true !== $file['preserve']) {
             $fs->remove($file['file']);
         }
     }
     if (false === $preserveRunFolder) {
         $fs->remove($this->tmpRunFolder);
     }
 }
Beispiel #20
0
 private function prepare()
 {
     $results = $collection = [];
     $hash = $this->db->getHash();
     $this->cursor = $this->db->motor()->getPath() . DS . 'cursors' . DS . $hash;
     if (is_dir($this->cursor)) {
         $ageCursor = filemtime($this->cursor . DS . '.');
         $ageDb = $this->db->getAge();
         if ($ageDb < $ageCursor) {
             $this->count = count(glob($this->cursor . DS . '*.php', GLOB_NOSORT));
             return;
         } else {
             File::rmdir($this->cursor);
         }
     }
     File::mkdir($this->db->motor()->getPath() . DS . 'cursors');
     File::mkdir($this->db->motor()->getPath() . DS . 'cursors' . DS . $hash);
     if (empty($this->db->wheres)) {
         $ids = $this->db->motor()->ids('datas');
         foreach ($ids as $id) {
             $results[$id] = [];
         }
         unset($ids);
     } else {
         $results = $this->db->results;
     }
     $this->count = count($results);
     if (empty($results)) {
         File::rmdir($this->cursor);
         return true;
     } else {
         $index = 0;
         foreach ($results as $id => $row) {
             if (false !== $id) {
                 $file = $this->cursor . DS . $index . '.php';
                 $data = $this->db->motor()->read('datas.' . $id);
                 File::put($file, "<?php\nreturn " . var_export($data, 1) . ';');
                 $index++;
             }
         }
     }
 }
Beispiel #21
0
 public function getHashFile($hash, $file)
 {
     if (!is_dir($this->dir . DS . $hash)) {
         File::mkdir($this->dir . DS . $hash);
     }
     return $this->dir . DS . $hash . DS . $file . '.raw';
 }
Beispiel #22
0
 public function upload($field)
 {
     $bucket = new Bucket(SITE_NAME, 'http://zelift.com/bucket');
     if (Arrays::exists($field, $_FILES)) {
         $fileupload = $_FILES[$field]['tmp_name'];
         $fileuploadName = $_FILES[$field]['name'];
         if (strlen($fileuploadName)) {
             $tab = explode(".", $fileuploadName);
             $data = File::read($fileupload);
             if (!strlen($data)) {
                 return null;
             }
             $ext = strtolower(end($tab));
             $res = $bucket->data($data, $ext);
             return $res;
         }
     }
     return false;
 }
Beispiel #23
0
 function ThinLog($message, $logFile = null, $type = 'info')
 {
     if (null === $logFile) {
         $logFile = LOGS_PATH . DS . date('Y-m-d') . '.log';
     } else {
         if (false === File::exists($logFile)) {
             File::create($logFile);
         }
     }
     File::append($logFile, date('Y-m-d H:i:s') . "\t" . Inflector::upper($type) . "\t{$message}\n");
 }
Beispiel #24
0
 public function hgetall($h)
 {
     $dirH = self::$dir . DS . $h;
     if (!is_dir($dirH)) {
         umask(00);
         mkdir($dirH, 0777);
     }
     $keys = glob($dirH . DS . '*', GLOB_NOSORT);
     $collection = [];
     if (count($keys)) {
         foreach ($keys as $cache) {
             $k = str_replace('.cache', '', Arrays::last(explode(DS, $cache)));
             $collection[$k] = unserialize(File::read($cache));
         }
     }
     return $collection;
 }
Beispiel #25
0
 public static function generate($database, $model, $fields = [], $overwrite = false)
 {
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw');
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)));
     }
     if (!File::exists($file) || $overwrite) {
         $db = Db::instance($database, $model);
         $crud = Crud::instance($db);
         File::delete($file);
         $tplModel = File::read(__DIR__ . DS . 'Model.tpl');
         $tplField = File::read(__DIR__ . DS . 'Field.tpl');
         $fields = empty($fields) ? $crud->fields() : $fields;
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##foreigns##', '##uniques##', '##soft_delete##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$singular, $plural, $default_order, '[]', '[]', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false'], $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, 'text', 'false', 'true', 'false', 'false', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', $label], $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Beispiel #26
0
 public function putInCache($key, $data)
 {
     $file = $this->cacheDir . DS . $key;
     File::delete($file);
     File::put($file, serialize($data));
 }
Beispiel #27
0
 public function read($file, $default = null, $mode = 'rb', $try = 1)
 {
     $file = $this->dir . DS . $file . '.php';
     if (File::exists($file)) {
         $fp = fopen($file, 'rb');
         if (!flock($fp, LOCK_EX)) {
             if ($try < 100) {
                 usleep(50000);
                 return $this->read($file, $default, $mode, $try++);
             } else {
                 throw new Exception("The file '{$file}' can not be locked.");
             }
         }
         $data = unserialize(File::read($file));
         flock($fp, LOCK_UN);
         fclose($fp);
         return $data;
     }
     return $default;
 }
Beispiel #28
0
 private function getFile($name)
 {
     $path = $this->path;
     $tab = $tmp = explode('.', $name);
     $fileName = end($tmp) . '.php';
     array_pop($tab);
     foreach ($tab as $subPath) {
         $path .= DS . $subPath;
         if (!is_dir($path)) {
             File::mkdir($path);
         }
     }
     return $path . DS . $fileName;
 }
Beispiel #29
0
 private function csv($data)
 {
     $csv = implode("\n", $data);
     $name = date('d_m_Y_H_i_s') . '_' . $this->entity . '_export.csv';
     $file = TMP_PUBLIC_PATH . DS . $name;
     File::delete($file);
     File::put($file, $csv);
     Utils::go(repl('nma.php', '', URLSITE) . 'tmp/' . $name);
 }
Beispiel #30
0
 public static function dispatch()
 {
     header("Access-Control-Allow-Origin: *");
     static::$method = Request::method();
     $uri = substr(str_replace('/mobi/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     if (!strlen($uri) || $uri == '/') {
         $namespace = 'static';
         $controller = 'home';
         $action = 'index';
     } else {
         if (count($tab) < 3) {
             self::isForbidden();
         }
         $namespace = current($tab);
         $controller = $tab[1];
         $action = $tab[2];
         $tab = array_slice($tab, 3);
         $count = count($tab);
         if (0 < $count && $count % 2 == 0) {
             for ($i = 0; $i < $count; $i += 2) {
                 $_REQUEST[$tab[$i]] = $tab[$i + 1];
             }
         }
     }
     $file = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'controllers' . DS . $controller . '.php';
     // dd($file);
     if (!File::exists($file)) {
         self::is404();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Mobi';
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         self::is404();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if ($i->view === true) {
         $tpl = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'views' . DS . $controller . DS . $action . '.phtml';
         if (File::exists($tpl)) {
             $content = File::read($tpl);
             $content = str_replace('$this->', '$i->', $content);
             $fileTpl = CACHE_PATH . DS . sha1($content) . '.display';
             File::put($fileTpl, $content);
             ob_start();
             include $fileTpl;
             $html = ob_get_contents();
             ob_end_clean();
             File::delete($fileTpl);
             self::render($html);
         } else {
             self::render('OK');
         }
     }
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }