Esempio n. 1
0
File: get.php Progetto: Borvik/Munla
 /**
  * Alias for get::array_default that is non-destructive.
  * 
  * @param array $array         The array to extract a value from.
  * @param mixed $key           The key to extract from the array.
  * @param mixed $default       The default value to use should the key not exist, or is invalid.
  * @param array $allowed       A list of values that this key is allowed to be.
  * 
  * @return Returns the value of the key, or the default value if the key is not found.
  */
 public static function array_def(array &$array, $key, $default = null, array $allowed = array())
 {
     return get::array_default($array, $key, $default, $allowed, true);
 }
Esempio n. 2
0
 /**
  * Opens a new form tag.
  * 
  * Has variable parameters.  May either pass an associative array with the parameters and HTML attributes mixed together
  * or the paramters may be passed in order, with an additional array parameter containing the HTML attributes.
  * 
  * @param callable $callback The callback function for handling the form.
  * @param bool $nocsrf       Indicator to use CSRF or not.
  * @param string $class      The HTML class attribute.
  * @param string $method     The HTML method attribute.
  * @param string $action     The HTML action attribute.
  * @param array $attributes  Other HTML attributes for the tag.
  * 
  * @return string|bool
  */
 public function open($arg)
 {
     if (isset($this->openForm)) {
         log::warning('Unable to open a new form - a form is already open.');
         return false;
     }
     $args = func_get_args();
     if (count($args) == 1 && is_array($arg) && !is_callable(get::form_callback($arg))) {
         $args = $arg;
     }
     if (count($args) > 0) {
         $n = array('callback' => null, 'nocsrf' => false, 'class' => null, 'method' => null, 'action' => null);
         $attributes = null;
         if (is_array(end($args))) {
             $eargs = array_pop($args);
             //could be params or only as callback
             if (is_callable(get::form_callback($eargs))) {
                 $n['callback'] = get::form_callback($eargs);
             } else {
                 $attributes = $eargs;
             }
         }
         $did = 0;
         //$dargs = array('callback', 'class', 'method', 'action', 'nocsrf');
         while (count($args) > 0) {
             $a = array_shift($args);
             if ($did < 2 && (is_string($a) || is_array($a)) && is_callable(get::form_callback($a))) {
                 $n['callback'] = get::form_callback($a);
                 $did++;
                 continue;
             }
             if ($did < 2 && is_bool($a)) {
                 $n['nocsrf'] = $a;
                 $did++;
                 continue;
             }
             if (is_string($a)) {
                 if ($did < 3) {
                     $n['class'] = $a;
                     $did = 3;
                     continue;
                 }
                 if ($did < 4) {
                     $n['method'] = $a;
                     $did = 4;
                     continue;
                 }
                 if ($did < 5) {
                     $n['action'] = $a;
                     $did = 5;
                     continue;
                 }
             }
         }
         if (is_array($attributes)) {
             foreach ($attributes as $ak => $av) {
                 $n[$ak] = $av;
             }
         }
         $args = $n;
     }
     set::array_default($args, 'method', 'post', array('get', 'post', 'GET', 'POST'));
     set::array_default($args, 'action', get::url());
     set::array_default($args, 'nocsrf', false, array(true, false));
     $formid = get::array_default($args, 'id', 'form_' . ++self::$nextFormIndex, array(), true);
     $callback = get::array_default($args, 'callback');
     if (!isset($callback) || !is_callable($callback)) {
         log::warning('Form callback is missing or invalid.  The form callback should be a static member of a class that extends formHandler.');
     }
     $html = '';
     $args = array_filter($args, function ($v) {
         return !is_null($v);
     });
     if (is::existset($args, 'nocsrf') && $args['nocsrf']) {
         $args['nocsrf'] = 'nocsrf';
         if (isset($callback) && is_callable($callback)) {
             $mnlAction = self::generateAction($callback);
             $html = sprintf('<form%s><input%s />', get::formattedAttributes($args), get::formattedAttributes(array('type' => 'hidden', 'name' => 'mnl_formaction', 'value' => $mnlAction)));
             $this->openForm = $callback;
         } else {
             $html = sprintf('<form%s>', get::formattedAttributes($args));
             $this->openForm = true;
         }
     } else {
         $csrfName = csrfHelper::generateName();
         if (!is::existset(munla::$session, 'forms')) {
             munla::$session['forms'] = array();
         }
         if (!is::existset(munla::$session['forms'], $csrfName)) {
             munla::$session['forms'][$csrfName] = array();
         }
         munla::$session['forms'][$csrfName][$formid] = array();
         $this->openForm =& munla::$session['forms'][$csrfName][$formid];
         $this->openForm['formid'] = $formid;
         $this->openForm['callback'] = $callback;
         $this->openForm['method'] = $args['method'];
         $this->openForm['fields'] = array();
         $this->openForm['nocsrf'] = $args['nocsrf'];
         unset($args['nocsrf']);
         $html = sprintf('<form%s><input%s />', get::formattedAttributes($args), get::formattedAttributes(array('type' => 'hidden', 'name' => 'mnl_formid', 'value' => $csrfName . '-' . $formid)));
     }
     formElement::$fieldNameArrays = array();
     if (!$this->echoOff) {
         echo $html;
     }
     return $html;
 }