Ejemplo n.º 1
0
 public function request_decode_part($part_name)
 {
     $part = html_entity_decode($this->request->get($part_name));
     if ($part) {
         return \System\Json::decode($part);
     }
     return null;
 }
Ejemplo n.º 2
0
 public static function load_config()
 {
     if (self::check_cache()) {
         $data = \System\Json::read(BASE_DIR . self::FILE_ROUTES_CACHE);
         self::$routes = $data['routes'];
         self::$menu = $data['menu'];
         self::$cfg = $data['cfg'];
     } else {
         self::build_config();
     }
 }
Ejemplo n.º 3
0
 public function val_get()
 {
     $val = $this->form()->input_value($this->name);
     if ($val) {
         if (gettype($val) == 'string') {
             $val = \System\Json::decode($val);
         }
         if (!$val instanceof \System\Location) {
             $val = new \System\Location($val);
         }
     }
     return $val;
 }
Ejemplo n.º 4
0
 /** Print basic system info to STDOUT
  * @return void
  */
 public static function cmd_update()
 {
     \System\Init::basic();
     $deps = cfg('assets');
     $list = array();
     foreach ($deps as $dep_list) {
         $list = array_merge($list, $dep_list);
     }
     \System\Json::put(BASE_DIR . '/bower.json', array("name" => "pwf-generic", "dependencies" => $list));
     \System\Json::put(BASE_DIR . '/.bowerrc', array("directory" => "share/bower"));
     $found = exec('which bower');
     if ($found) {
         \Helper\Cli::out('Running bower update');
         passthru('cd ' . BASE_DIR . '; bower update');
     } else {
         \Helper\Cli::out('Please install node.js#bower first');
     }
     unlink(BASE_DIR . '/.bowerrc');
     unlink(BASE_DIR . '/bower.json');
 }
Ejemplo n.º 5
0
 public function val_get()
 {
     if ($this->resolved_value) {
         $val = $this->resolved_value;
     } else {
         $val = $this->form()->input_value($this->name);
     }
     if (gettype($val) == 'string') {
         $val = \System\Json::decode($val);
     }
     if (is_array($val)) {
         if ($val['method'] == 'url') {
             $val = \System\File::fetch($val['url'])->temp()->unload();
             $val->temp = false;
             $val->method = 'keep';
         } else {
             if ($val['method'] == 'upload') {
                 $file = $this->form()->request->post($val['upload']);
                 $file = \System\File::from_tmp($file['tmp_name'], $val['name']);
                 $file->temp();
                 $file->temp = false;
                 $file->method = 'keep';
                 $file->mime = $val['mime'];
                 $val = $file;
             } else {
                 if ($val['method'] == 'save') {
                     $file = \System\File::from_path($val['path']);
                     $file->read_meta();
                     $val = $file;
                 } else {
                     if ($val['method'] == 'drop') {
                         $val = null;
                     }
                 }
             }
         }
     }
     $this->resolved_value = $val;
     return $val;
 }
Ejemplo n.º 6
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.º 7
0
 /** Save module settings
  * @param string $module
  * @param string $env
  */
 public static function save($module, $env = null)
 {
     is_null($env) && ($env = self::$env);
     $path = BASE_DIR . self::DIR_CONF_DIST . '/' . $env . '/' . $module . ".json";
     $data = \System\Json::json_humanize(json_encode(self::get($module)));
     if (!($action = \System\File::put($path, $data))) {
         throw new \System\Error\Permissions(sprintf('Failed to write settings. Please check your permissions on directory \'%s\'', ROOT . self::DIR_CONF_DIST));
     }
     self::purge_cache();
     self::reload();
     return $action;
 }
Ejemplo n.º 8
0
 public static function get_meta($path, $file)
 {
     $json = null;
     $meta = null;
     if (file_exists($p = $path . '/' . $file)) {
         $json = \System\Json::read($p);
     }
     if ($json) {
         if (isset($json['include'])) {
             $meta = $json['include'];
         } else {
             if (isset($json['main'])) {
                 $meta = $json['main'];
                 if (!is_array($meta)) {
                     $meta = array($meta);
                 }
             }
         }
     }
     return $meta;
 }
Ejemplo n.º 9
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.º 10
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.º 11
0
 /** Lookup commited data in input class
  * @return void
  */
 protected function take_data_from_request()
 {
     $this->data_commited = $this->request->input_by_prefix($this->prefix, $this->method);
     if (isset($this->data_commited['data_hidden'])) {
         $this->data_hidden = \System\Json::decode(htmlspecialchars_decode($this->data_commited['data_hidden']));
         $tmp = array();
         if (is_array($this->data_hidden)) {
             foreach ($this->data_hidden as $key => $val) {
                 $tmp[$key] = $val;
             }
         }
         if (is_array($this->data_commited)) {
             foreach ($this->data_commited as $key => $val) {
                 $tmp[$key] = $val;
             }
         }
         $this->data_commited = $tmp;
         unset($this->data_commited['data_hidden']);
     }
     $this->submited = isset($this->data_commited['submited']) ? !!$this->data_commited['submited'] : false;
 }
Ejemplo n.º 12
0
 /** Make a request and return JSON decoded data
  * @param string $url Requested URL
  * @return mixed
  */
 static function json($url)
 {
     return \System\Json::decode(self::get($url), true);
 }
Ejemplo n.º 13
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.º 14
0
 /** Create instance from JSON
  * @param string $json
  */
 public static function from_json($json)
 {
     $model = get_called_class();
     return new $model(\System\Json::decode($json));
 }
Ejemplo n.º 15
0
 private function import_json($file)
 {
     $json = \System\Json::read($file);
     $this->seed_data($file, $json);
     return $this;
 }