示例#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;
 }
示例#2
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;
 }
示例#3
0
文件: file.php 项目: just-paja/fudjan
 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;
 }
示例#4
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);
 }
示例#5
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);
 }
示例#6
0
文件: attr.php 项目: just-paja/fudjan
 /**
  * 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;
 }
示例#7
0
文件: form.php 项目: just-paja/fudjan
 /** 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;
 }
示例#8
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);
 }
示例#9
0
文件: file.php 项目: just-paja/fudjan
 /** Create instance from JSON
  * @param string $json
  */
 public static function from_json($json)
 {
     $model = get_called_class();
     return new $model(\System\Json::decode($json));
 }