Ejemplo n.º 1
0
 public function test_reversing()
 {
     $cfg = \System\Settings::get('domains');
     $list = array_keys($cfg);
     $host = $list[0];
     $this->assertEquals(\System\Router::get_url($host, 'system_resource', array('static', 'a', 'b')), '/static/a/b');
 }
Ejemplo n.º 2
0
 public function send_mail()
 {
     if (!$this->sent_report) {
         $users = \System\User\Group::find(self::ID_GROUP_REPORT)->users->fetch();
         $rcpt = array();
         foreach ($users as $user) {
             $contacts = $user->contacts->where(array('type' => \System\User\Contact::STD_EMAIL, 'spam' => true))->fetch();
             $mails = collect(array('attr', 'ident'), $contacts);
             $rcpt = array_merge($rcpt, $mails);
         }
         $data = $this->get_data();
         $locales = new \System\Locales();
         $subject = 'mail-subject-new-feedback';
         $body = 'mail-body-new-feedback';
         $from = $this->email;
         $mail = \Helper\Offcom\Mail::create($subject, $body);
         $locales->set_locale();
         $subject = stprintf($locales->trans($subject), $data);
         $body = stprintf($locales->trans($body), $data);
         // Prepend tag to subject of all alerts
         $subject = '[Impro][Feedback ' . $this->id . '] ' . $subject;
         $body .= "\n\nTento e-mail byl automaticky vygenerován uživatelskou interakcí webu Improligy. Pokud tento e-mail neměl přijít vám, obraťte se na kontakty Improligy.\nhttp://www.improliga.cz";
         $mail->rcpt = $rcpt;
         $this->sent_report = $mail->send();
         if ($from) {
             $mail->from = \System\Settings::get('offcom', 'default', 'sender');
             $mail->rcpt = array($from);
             $this->sent_copy = $mail->send();
         }
         $this->save();
     }
     return $this;
 }
Ejemplo n.º 3
0
 public function construct($attrs)
 {
     \System\Directory::check(BASE_DIR . self::DIR_CACHE);
     $cache = \System\Settings::getSafe(array('cache', 'templates'), true);
     if (class_exists('Jade\\Jade')) {
         $this->jade = new \Jade\Jade(array('cache' => $cache ? BASE_DIR . self::DIR_CACHE : null));
     } else {
         throw new \System\Error\MissingDependency('Could not find jade template compiler.', 'Please install ronan-gloo/jadephp');
     }
 }
Ejemplo n.º 4
0
 public function send_notif(\System\Http\Response $res)
 {
     $ren = \System\Template\Renderer\Txt::from_response($res);
     $ren->reset_layout();
     $ren->partial('mail/car/offer', array("admin" => $res->url_full('carshare.admin', array($this->ident))));
     $mail = new \Helper\Offcom\Mail(array('rcpt' => array($this->email), 'subject' => 'Improtřesk 2016 - Sdílení auta', 'reply_to' => \System\Settings::get('offcom', 'default', 'reply_to'), 'message' => $ren->render_content()));
     $mail->send();
     $this->sent_notif = true;
     $this->save();
 }
Ejemplo n.º 5
0
 public static function build_config()
 {
     self::$menu = \System\Settings::read(self::DIR_ADMIN);
     self::$routes = \System\Settings::read(self::DIR_ROUTES);
     self::$cfg = array();
     $prefix = cfg('godmode', 'path_prefix');
     // Add godmode path prefix
     foreach (self::$routes as &$route) {
         $route['url'] = $prefix . $route['url'];
     }
     self::save();
 }
Ejemplo n.º 6
0
 public static function query($query, $db_ident = null)
 {
     if (($db = self::get_db($db_ident)) !== null) {
         $start = microtime(true);
         $res = $db->query($query);
         self::$queries++;
         if (\System\Settings::get('dev', 'debug', 'backend')) {
             $trace = debug_backtrace();
             $tres = count($trace > 2) ? 2 : 1;
             self::$query_record[] = array("trace" => $trace[$tres], "time" => microtime(true) - $start, "query" => $query);
         }
         return $res;
     } else {
         throw new \System\Error\Database('Not connected to database "' . $db_ident . '"');
     }
 }
Ejemplo n.º 7
0
 /** Initialize all scripts for this request and domain
  * @return void
  */
 public function init()
 {
     if ($this->get('lang')) {
         $this->lang = $this->get('lang');
     } else {
         if ($this->cookie('lang')) {
             $this->lang = $this->cookie('lang');
         }
     }
     try {
         $login = \System\Settings::get('policies', 'auto_login');
     } catch (\System\Error\Config $e) {
         $login = true;
     }
     if ($login) {
         $this->relogin();
     }
     \System\Init::run($this->init, array("request" => $this));
     return $this;
 }
Ejemplo n.º 8
0
 public function cmd_pair()
 {
     \System\Init::full();
     $token = \System\Settings::get('bank', 'token');
     $from = \System\Settings::get('bank', 'from');
     $to = date('Y-m-d');
     $url = str_replace('{token}', $token, self::URL_TRANSACTIONS);
     $url = str_replace('{from}', $from, $url);
     $url = str_replace('{to}', $to, $url);
     $res = \Helper\Offcom\Request::get($url);
     if (!$res->ok()) {
         if ($res->status == 409) {
             \Helper\Cli::out('Please wait 30 seconds and try again.');
             exit(4);
         } else {
             \Helper\Cli::out('Unknown error during transaction scrape.');
             exit(5);
         }
     }
     $feed = \System\Json::decode($res->content);
     \Workshop\Payment::pair_with_feed($feed);
 }
Ejemplo n.º 9
0
 public function run()
 {
     $rq = $this->request;
     $res = $this->response;
     $ren = $res->renderer;
     $userClass = \System\Loader::get_class_from_model(\System\Settings::get('godmode', 'userClass'));
     if ($rq->logged_in()) {
         $this->flow->redirect($ren->url('god_home'));
     } else {
         $f = $res->form(array("id" => 'core-user-login'));
         $f->input(array('type' => 'text', 'name' => 'login', 'label' => $ren->trans("gm-login-name"), 'required' => true));
         $f->input(array('type' => 'password', 'name' => 'password', 'label' => $ren->trans("gm-password"), 'required' => true));
         $f->submit($ren->trans('Log in'));
         if ($f->passed()) {
             $p = $f->get_data();
             if ($userClass::startSession($rq, $p['login'], $p['password'])) {
                 $this->flow->redirect($ren->url('god_home'));
             }
         }
         $f->out($this);
     }
 }
Ejemplo n.º 10
0
 public function run()
 {
     $rq = $this->request;
     $res = $this->response;
     $page = 0;
     $per_page = 1;
     $model = $this->req('model');
     $cname = \System\Loader::get_class_from_model($model);
     $exists = class_exists($cname) && is_subclass_of($cname, '\\System\\Model\\Perm');
     $send = array('status' => 404, 'message' => 'schema-not-found');
     if ($exists) {
         try {
             $schema = $cname::get_visible_schema($rq->user);
         } catch (\System\Error\AccessDenied $e) {
             $send['status'] = 403;
             $send['message'] = 'access-denied';
         }
         if ($schema) {
             $send['status'] = 200;
             $send['message'] = 'ok';
             $send['data'] = $schema;
         }
     }
     try {
         $debug = \System\Settings::get('dev', 'debug', 'backend');
     } catch (\System\Error $e) {
         $debug = true;
     }
     if (!$debug) {
         $max_age = \System\Settings::get('cache', 'resource', 'max-age');
         $res->header('Pragma', 'public,max-age=' . $max_age);
         $res->header('Cache-Control', 'public');
         $res->header('Expires', date(\DateTime::RFC1123, time() + $max_age + rand(0, 60)));
         $res->header('Age', '0');
     }
     $this->partial(null, $send);
 }
Ejemplo n.º 11
0
 public static function cmd_database()
 {
     \System\Init::full();
     $db_list = \System\Settings::get('database', 'list');
     foreach ($db_list as $db_ident => $db_cfg) {
         $size = \System\Database::query("\n\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tsum(data_length + index_length) 'size',\n\t\t\t\t\t\t\tsum( data_free ) 'free'\n\t\t\t\t\t\tFROM information_schema.TABLES\n\t\t\t\t\t\tWHERE table_schema = '" . $db_cfg['database'] . "'\n\t\t\t\t\t\tGROUP BY table_schema;\n\t\t\t\t")->fetch();
         $mlast_date = false;
         $mcount = 0;
         try {
             $mig = \System\Database\Migration::get_new();
             $mcount = count($mig);
             $stat = "Ok";
             $mlast = \System\Database\Migration::get_first()->where(array("status" => 'ok'))->sort_by("updated_at DESC")->fetch();
             if ($mlast) {
                 $mlast_date = $mlast->updated_at;
             }
         } catch (System\Error $e) {
             $stat = "Migrating database is necessary.";
         }
         \Helper\Cli::out('Database ' . $db_ident);
         \Helper\Cli::sep();
         \Helper\Cli::out_flist(array("list" => array("Driver" => $db_cfg['driver'], "Host name" => $db_cfg['host'], "Database name" => $db_cfg['database'], "User" => $db_cfg['username'], "Used charset" => $db_cfg['charset'], "Lazy driver" => $db_cfg['lazy'] ? 'yes' : 'no', "Size" => \System\Template::convert_value('information', $size['size']), "Free space" => \System\Template::convert_value('information', $size['free']), "Structure" => $stat, "Last migrated" => $mlast_date ? $mlast_date->format('Y-m-d H:i:s') : 'never', "New migrations" => $mcount)));
     }
 }
Ejemplo n.º 12
0
 public function run()
 {
     $id = $this->id;
     $new = $this->new;
     def($id);
     def($new, false);
     $model = $this->req('model');
     $rq = $this->request;
     $cname = \System\Loader::get_class_from_model($model);
     $response = array('message' => 'not-found', 'status' => 404);
     if (class_exists($cname) && is_subclass_of($cname, '\\System\\Model\\Perm')) {
         if ($item = $new ? new $cname() : $cname::find($id)) {
             $data = $rq->post();
             foreach ($data as $attr_name => $val) {
                 if ($item::has_attr($attr_name)) {
                     $def = $cname::get_attr($attr_name);
                     if (is_string($val)) {
                         if (preg_match('/^[\\{\\[].*[\\}\\]]$/', $val)) {
                             $val = \System\Json::decode(html_entity_decode($val));
                         }
                     }
                     if (in_array($def['type'], array('file', 'image'))) {
                         $helper_cname = '\\System\\File';
                         if ($def['type'] == 'image') {
                             $helper_cname = '\\System\\Image';
                         }
                         if (is_array($val)) {
                             if (any($val['method']) && any($val[$val['method']])) {
                                 $data = $rq->post($val[$val['method']]);
                                 if ($data) {
                                     $item->{$attr_name} = $helper_cname::from_tmp($data['tmp_name'], $data['name']);
                                 }
                             }
                         }
                     } else {
                         if ($def['type'] == 'password') {
                             $item->{$attr_name} = hash_passwd($val);
                         } else {
                             if ($def['type'] == 'bool') {
                                 if ($val == 'false') {
                                     $val = false;
                                 }
                                 $item->{$attr_name} = $val;
                             } else {
                                 if ($def['type'] == 'date') {
                                     $date = \DateTime::createFromFormat('Y-m-d', $val);
                                     if ($date) {
                                         $tz = new \DateTimeZone(\System\Settings::get('locales', 'timezone'));
                                         $date->setTimeZone($tz);
                                     } else {
                                         $date = null;
                                     }
                                     $item->{$attr_name} = $date;
                                 } else {
                                     if ($def['type'] == 'datetime') {
                                         $date = \DateTime::createFromFormat('Y-m-d\\TH:i:sO', $val);
                                         if ($date) {
                                             $tz = new \DateTimeZone(\System\Settings::get('locales', 'timezone'));
                                             $date->setTimeZone($tz);
                                         } else {
                                             $date = null;
                                         }
                                         $item->{$attr_name} = $date;
                                     } else {
                                         $item->{$attr_name} = $val;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $item->request = $rq;
             if ($item::has_attr('author') && $rq->user) {
                 $item->author = $rq->user;
             }
             try {
                 $item->save();
             } catch (\System\Error $e) {
                 $response['status'] = 500;
                 $response['message'] = $e->get_explanation();
             }
             if ($response['status'] != 500) {
                 $response['message'] = $new ? 'created' : 'saved';
                 $response['status'] = 200;
             }
             $response['data'] = $item->to_object();
         }
     }
     $this->partial(null, $response);
 }
Ejemplo n.º 13
0
 public static function init()
 {
     $whoops = new \Whoops\Run();
     if (static::on_cli()) {
         $whoops->pushHandler(new \Whoops\Handler\PlainTextHandler());
     } else {
         $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
     }
     $whoops->pushHandler(function ($exc, $inspector, $run) {
         $debug = \System\Settings::getSafe(array('dev', 'debug', 'backend'), true);
         static::filterException($exc, $debug);
         static::report('error', static::getExceptionMessage($exc));
     });
     $whoops->register();
 }
Ejemplo n.º 14
0
        $static_domain = \System\Settings::get('resources', 'domain');
    } catch (\System\Error\Config $e) {
        $static_domain = null;
    }
    try {
        $locales_url = ($static_domain ? '//' . $static_domain : '') . $res->url("system_resource", array('static', 'locale', '{lang}.' . $serial . '.json'));
    } catch (\System\Error\NotFound $e) {
        $locales_url = '';
    }
    try {
        $autoload = \System\Settings::get('locales', 'autoload');
    } catch (\System\Error\Config $e) {
        $autoload = false;
    }
    try {
        $debug = \System\Settings::get('dev', 'debug');
    } catch (\System\Error\Config $e) {
        $debug = array('frontend' => true, 'backend' => true);
    }
    $now = new \DateTime();
    $tz = new \DateTimeZone(\System\Settings::get('locales', 'timezone'));
    $now->setTimeZone($tz);
    $cont = array("locales" => array("url" => $locales_url, "lang" => $res->locales->get_lang(), "autoload" => $autoload, "tz" => -$tz->getOffset($now) / 60, "now" => $now->format('c')), "comm" => array("blank" => '/share/html/blank.html'), "debug" => $debug, "proxy" => array('url' => '/proxy/head/?url={url}'));
    try {
        $frontend = \System\Settings::get('frontend');
    } catch (\System\Error $e) {
        $frontend = array();
    }
    $rq->fconfig = array_merge_recursive($cont, $frontend);
    return true;
};
Ejemplo n.º 15
0
 public function slot_check($name)
 {
     try {
         $debug = \System\Settings::get('dev', 'debug', 'backend');
     } catch (\System\Error $e) {
         $debug = true;
     }
     if (!isset($this->content['slots'][$name])) {
         $this->content['slots'][$name] = array();
         if ($debug && strpos($this->format, 'html') !== false && !\System\Status::on_cli()) {
             $this->content['slots'][$name][] = '<!-- Slot: "' . $name . '" -->';
         }
     }
 }
Ejemplo n.º 16
0
 public static function build_locales()
 {
     $cfg = \System\Settings::get('locales', 'allowed');
     $loc = new \System\Locales();
     foreach ($cfg as $lang) {
         $loc->load_messages($lang);
         \System\File::put(BASE_DIR . \System\Locales::DIR_CACHE . '/' . $lang . '.json', json_encode($loc->get_messages($lang)));
     }
 }
Ejemplo n.º 17
0
 /**
  * Find named route and translate it with args
  *
  * @param string $host
  * @param string $name
  * @param array  $args
  * @return string
  */
 public static function get_url($host, $name, array $args = array())
 {
     $route = self::get_route($host, $name);
     if ($route) {
         $attrs = self::get_pattern_attrs($route['url']);
         $c = count($attrs);
         if ($c != count($args)) {
             throw new \System\Error\Argument(sprintf("Named route called '%s' accepts %s arguments. %s were given.", $name, count($attrs), count($args)));
         }
         $str = self::get_pattern_simplified($route['url']);
         if ($c > 0) {
             $num = 0;
             foreach ($attrs as $num => $attr) {
                 $val = null;
                 if (isset($args[$attr['name']])) {
                     $val = $args[$attr['name']];
                 } else {
                     if (isset($args[$num])) {
                         $val = $args[$num];
                     }
                 }
                 if (is_object($val)) {
                     if ($val instanceof \System\Model\Database) {
                         $val = $val->get_seoname();
                     } else {
                         throw new \System\Error\Argument(sprintf("Argument '%s' passed to reverse build route '%s' must be string or instance of System::Model::Database", $num, $name), sprintf("Instance of '%s' was given.", get_class($arg)));
                     }
                 }
                 if (is_null($val) && $attr['required']) {
                     throw new \System\Error\Argument('Argument must be supplied to build route', $name, $attr);
                 }
                 $str = str_replace('{' . $attr['name'] . '}', $val, $str);
             }
         }
         if (self::is_domain($host)) {
             $domain = $host;
         } else {
             $domain = self::get_domain($host);
         }
         $dns = \System\Settings::get('domains', $domain);
         if (!array_key_exists('htaccess', $dns) || $dns['htaccess']) {
             return $str;
         }
         return '?path=' . urlencode($str);
     } else {
         throw new \System\Error\Config(sprintf("Named route called '%s' was not found for domain '%s'", $name, $host));
     }
     return false;
 }
Ejemplo n.º 18
0
 public function cmd_lostAndFound()
 {
     \System\Init::full();
     $users = \Workshop\SignUp::get_all()->where(array("solved" => true, "newsletter" => true, "sentLostAndFound" => false))->fetch();
     \Helper\Cli::do_over($users, function ($key, $user) {
         $ren = new \System\Template\Renderer\Txt();
         $ren->reset_layout();
         $ren->partial('mail/notif/lost-and-found', array("user" => $user, "symvar" => $user->check->symvar));
         $mail = new \Helper\Offcom\Mail(array('rcpt' => array($user->email), 'subject' => 'Improtřesk 2016 - Ztráty a nálezy, pozvánka', 'reply_to' => \System\Settings::get('offcom', 'default', 'reply_to'), 'message' => $ren->render_content()));
         $mail->send();
         $user->sentLostAndFound = true;
         $user->save();
     });
 }
Ejemplo n.º 19
0
 public function to_object()
 {
     if (!$this->mime) {
         $this->read_meta();
     }
     $path = str_replace(\System\File::DIR, '', $this->get_path_relative());
     $src = 'media';
     $cname = $this::RESOURCE_CNAME;
     $stat = $cname::DIR_STATIC;
     try {
         $cache = \System\Settings::get('cache', 'resources');
     } catch (\System\Error $e) {
         $cache = false;
     }
     if ($cache) {
         $dirs = array($stat);
     } else {
         $dirs = \System\Composer::list_dirs($stat);
         array_unshift($dirs, $stat);
     }
     foreach ($dirs as $dir) {
         if (strpos($path, $dir) === 0 || strpos($path, $dir = substr($dir, 1)) === 0) {
             $path = substr($path, strlen($dir));
             $src = 'static';
             break;
         }
     }
     $url = \System\Resource::get_url($src, self::RESOURCE_TYPE, preg_replace("/^\\//", '', $path));
     return array("url" => $url, "path" => $path, "name" => $this->name, "mime" => $this->mime, "size" => $this->size, "method" => $this->method ? $this->method : 'save');
 }
Ejemplo n.º 20
0
 public static function cmd_seed(array $params = array())
 {
     \System\Init::full();
     \Helper\Cli::out('Seeding initial system data ..');
     \System\Database::seed_initial_data();
     $data = \System\Settings::read(\System\Database::DIR_INITIAL_DATA, true);
     if ($data) {
         \Helper\Cli::out("Injecting initial data ..");
         foreach ($data as $data_set_name => $data_set_models) {
             self::seed_data($data_set_name, $data_set_models);
         }
     }
 }
Ejemplo n.º 21
0
 public function mail_payment_update()
 {
     $check = $this->check;
     if (!$check) {
         return;
     }
     $payments = $check->payments->fetch();
     $paid = 0;
     foreach ($payments as $p) {
         $paid += $p->amount;
     }
     $ren = new \System\Template\Renderer\Txt();
     $ren->reset_layout();
     $ren->partial('mail/signup/payment-update', array("item" => $this, "check" => $check, "paid" => $paid, "payments" => $payments));
     $mail = new \Helper\Offcom\Mail(array('rcpt' => array($this->email), 'subject' => 'Improtřesk 2016 - Přihláška, platební aktualizace', 'reply_to' => \System\Settings::getSafe(array('offcom', 'default', 'reply_to'), null), 'message' => $ren->render_content()));
     $mail->send();
     $this->sent_notif = true;
     $this->save();
 }
Ejemplo n.º 22
0
 /** Load all messages by language
  * @param string $lang
  * @return void
  */
 public function load_messages($locale = null)
 {
     if (is_null($locale)) {
         $locale = $this->locale;
     }
     if (!isset($this->messages[$locale])) {
         $list = $this::get_available();
         if (in_array($locale, $list)) {
             $file = BASE_DIR . self::DIR_CACHE . '/' . $locale . '.json';
             if (file_exists($file)) {
                 $this->messages[$locale] = \System\Json::read($file);
             } else {
                 $this->messages[$locale] = \System\Settings::read(self::DIR . '/' . $locale, false, $this->files);
             }
         } else {
             throw new \System\Error\Locales(sprintf('Unknown language %s', $locale));
         }
     }
     return $this;
 }
Ejemplo n.º 23
0
 /**
  * Prepare data of a kind to be saved, mostly conversions
  *
  * @param string $attr  Name of attribute
  * @param mixed  $val   Value to check and fix
  * @return mixed Fixed value
  */
 public static function convert_attr_val($attr, $val = null)
 {
     $attr_data = static::get_attr($attr);
     if (isset($attr_data['is_null']) && $attr_data['is_null'] && is_null($val)) {
         return $val = null;
     }
     switch ($attr_data['type']) {
         case 'int':
             $val = filter_var($val, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
             break;
         case 'float':
             $val = filter_var($val, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
             break;
         case 'bool':
             $val = is_null($val) ? false : !!$val;
             break;
         case 'password':
         case 'text':
         case 'time':
         case 'varchar':
             $val = mb_substr(strval($val), 0, isset($attr_data['length']) ? $attr_data['length'] : 255);
             break;
         case 'date':
         case 'datetime':
             $is_null = isset($attr_data['is_null']) && $attr_data['is_null'];
             if (!$val instanceof \DateTime) {
                 if (any($val)) {
                     if ($val == '0000-00-00 00:00:00') {
                         $val = null;
                     } else {
                         $val = \DateTime::createFromFormat('Y-m-d H:i:s', $val, new \DateTimeZone(\System\Settings::get('locales', 'timezone')));
                     }
                 }
                 if (!$is_null && !$val) {
                     $val = new \DateTime();
                 }
             }
             break;
         case 'image':
         case 'file':
             if ($attr_data['type'] == 'image') {
                 $cname = '\\System\\Image';
             } else {
                 $cname = '\\System\\File';
             }
             if (any($val)) {
                 if (is_object($val)) {
                     if (!$val instanceof $cname) {
                         throw new \System\Error\Model(sprintf('Value for attribute "%s" of model "%s" should be instance of "%s". Instance of "%s" was given.', $attr, get_called_class(), $cname, get_class($val)));
                     }
                 } elseif (is_array($val)) {
                     $val = new $cname($val);
                 } elseif (is_string($val)) {
                     $val_json = str_replace("\\", "", $val);
                     if ($j = \System\Json::decode($val_json, true)) {
                         $val = new $cname($j);
                     } else {
                         $val = $cname::from_path($val);
                     }
                 }
             } else {
                 $val = null;
             }
             break;
         case 'object':
             if ($val) {
                 if (isset($attr_data['model'])) {
                     if (!$val instanceof $attr_data['model']) {
                         throw new \System\Error\Argument(sprintf("Value must be instance of '%s'", $attr_data['model']), get_called_class(), $attr, is_object($val) ? get_class($val) : gettype($val));
                     }
                 } else {
                     throw new \System\Error\Argument(sprintf("Attribute '%s' of model '%s' must have model defined!", $attr, get_called_class()));
                 }
             }
             break;
         case 'json':
             if (any($val) && is_string($val)) {
                 $val = array_filter((array) \System\Json::decode($val));
             }
             break;
         case 'int_set':
             if (any($val)) {
                 if (is_array($val)) {
                     $val = array_map('intval', array_filter($val));
                 } else {
                     $val = array_map('intval', explode(',', $val));
                 }
             } else {
                 $val = array();
             }
             break;
         case 'point':
             if (any($val) && !$val instanceof \System\Gps) {
                 if (is_array($val)) {
                     $val = \System\Gps::from_array($val);
                 } elseif (strpos($val, 'POINT(') === 0) {
                     $val = \System\Gps::from_sql($val);
                 } else {
                     $val = new \System\Gps();
                 }
             }
             break;
         case 'video_youtube':
             if (any($val) && !$val instanceof \System\Video\Youtube) {
                 if (is_string($val)) {
                     ($vid = \System\Video\Youtube::from_url($val)) || ($vid = \System\Video\Youtube::from_id($val));
                     $val = $vid;
                 } else {
                     throw new \System\Error\Format('Cannot create Youtube video object from "' . gettype($val) . '".');
                 }
             }
         case 'list':
             if (!is_array($val)) {
                 $val = (array) $val;
             }
     }
     return $val;
 }
Ejemplo n.º 24
0
 /** Get list of available files from user input
  * @param string $type
  * @param list   $modules
  * @return array
  */
 public function get_file_list($modules)
 {
     $found = array();
     $missing = array();
     try {
         $use_cache = \System\Settings::get('cache', 'resources');
     } catch (\System\Error\Config $e) {
         $use_cache = false;
     }
     if ($use_cache) {
         $dirs = array(BASE_DIR . $this::DIR_CACHE);
     } else {
         if ($this->src == 'static') {
             $src = $this::DIR_STATIC;
         } else {
             $src = $this::DIR_MEDIA;
         }
         $dirs = \System\Composer::list_dirs($src);
     }
     foreach ($modules as $module) {
         $mod_found = false;
         foreach ($dirs as $dir) {
             foreach ($this::$postfixes as $postfix) {
                 $path = $dir . DIRECTORY_SEPARATOR . $module;
                 if (file_exists($p = $path . '.list')) {
                     $list = $this->get_file_list(array_map('trim', array_filter(explode("\n", \System\File::read($p)))));
                     $found = array_merge($found, $list[self::KEY_FOUND]);
                     $missing = array_merge($missing, $list[self::KEY_MISSING]);
                     $mod_found = true;
                     break;
                 } else {
                     if (is_file($p = $path) || is_file($p = $path . '.' . $postfix)) {
                         $found[] = $p;
                         $mod_found = true;
                     }
                 }
                 if (is_dir($path)) {
                     $json = null;
                     $meta = self::get_meta($path, 'bower.json');
                     if (!$meta) {
                         $meta = self::get_meta($path, 'package.json');
                     }
                     if ($meta) {
                         $files = array();
                         foreach ($meta as $file) {
                             $files[] = str_replace($dir . '/', '', $path . '/' . $file);
                         }
                         $list = $this->get_file_list($files);
                         $found = array_merge($found, $list[self::KEY_FOUND]);
                         $mod_found = true;
                         break;
                     } else {
                         $files = \System\Directory::find_all_files($path);
                         foreach ($files as $key => $tmp_file) {
                             $files[$key] = str_replace($dir . '/', '', $files[$key]);
                         }
                         $list = $this->get_file_list($files);
                         $found = array_merge($found, $list[self::KEY_FOUND]);
                         $missing = array_merge($missing, $list[self::KEY_MISSING]);
                         $mod_found = true;
                         break;
                     }
                 }
             }
             if ($mod_found) {
                 break;
             }
         }
         if (!$mod_found) {
             $missing[] = $module;
         }
     }
     $this->sum = self::get_module_sum_from_list($modules);
     return array(self::KEY_FOUND => array_unique(array_filter($found)), self::KEY_MISSING => array_unique(array_filter($missing)), self::KEY_SUM => $this->sum);
 }
Ejemplo n.º 25
0
 public function set_headers()
 {
     $headers = array();
     $res = $this->response;
     if ($this->mime) {
         $res->header('Content-Type', $this->mime);
     } else {
         if ($this::MIME_TYPE) {
             $res->header('Content-Type', $this::MIME_TYPE);
         }
     }
     $res->header('Content-Length', strlen($this->content));
     $res->header('Access-Control-Allow-Origin', '*');
     if (!$this->debug) {
         $max_age = \System\Settings::get('cache', 'resource', 'max-age');
         $res->header('Pragma', 'public, max-age=' . $max_age);
         $res->header('Cache-Control', 'public');
         $res->header('Expires', date(\DateTime::RFC1123, time() + $max_age + rand(0, 60)));
         $res->header('Age', '0');
     }
 }
Ejemplo n.º 26
0
 /** Send email message object
  * @return int
  */
 public function send()
 {
     try {
         $disable = \System\Settings::get('dev', 'disable', 'offcom');
     } catch (\System\Error\Config $e) {
         $disable = false;
     }
     try {
         $fallback_to = \System\Settings::get('offcom', 'mail_to');
     } catch (\System\Error\Config $e) {
         $fallback_to = null;
     }
     $this->validate();
     $body = array();
     $headers_str = array();
     if ($fallback_to) {
         $rcpt = $fallback_to;
     } else {
         $rcpt = implode(', ', $this->rcpt);
     }
     $headers = $this->get_default_headers();
     $headers['From'] = $this->get_sender();
     $headers['Subject'] = $this->get_encoded_subject();
     if ($this->reply_to) {
         if (self::is_addr_valid($this->reply_to)) {
             $headers['Reply-To'] = $this->reply_to;
         } else {
             throw new \System\Error\Format(sprintf('Reply-To "%s" is not formatted according to RFC 2822.', $this->reply_to));
         }
     }
     foreach ($headers as $header => $value) {
         $headers_str[] = ucfirsts($header, '-', '-') . ": " . $value;
     }
     $body[] = implode("\n", $headers_str) . "\n";
     $body[] = strip_tags($this->message);
     $body = implode("\n", $body);
     $this->status = self::STATUS_SENDING;
     if (!$disable) {
         if (mail($rcpt, $this->get_encoded_subject(), '', $body)) {
             $this->status = self::STATUS_SENT;
         } else {
             $this->status = self::STATUS_FAILED;
         }
     }
     return $this->status;
 }
Ejemplo n.º 27
0
 public function cmd_retire()
 {
     if (\System\Settings::get('dev', 'debug', 'backend') || \System\Settings::get('dev', 'disable', 'serial')) {
         return;
     }
     $serial = \System\Settings::get('cache', 'resource', 'serial');
     \System\Settings::set(array('cache', 'resource', 'serial'), $serial + 1);
     \System\Settings::save('cache');
 }
Ejemplo n.º 28
0
 /** Run low level debug - Include a PHP file just after init and before module flow
  * @return void
  */
 public function exec_lld()
 {
     if (!$this->no_debug && \System\Settings::get('dev', 'debug', 'backend')) {
         if (file_exists(ROOT . '/lib/include/devel.php')) {
             $response = $this;
             $request = $this->request;
             $renderer = $this->renderer;
             $locales = $this->locales;
             $flow = $this->flow;
             $ren =& $renderer;
             include ROOT . '/lib/include/devel.php';
         }
     }
     return $this;
 }