function respond_to_error($obj, $redirect_to_params, $options = array())
{
    $extra_api_params = isset($options['api']) ? $options['api'] : array();
    $status = isset($options['status']) ? $options['status'] : 500;
    if (is_object($obj) && is_subclass_of($obj, 'ActiveRecord')) {
        $obj = implode(', ', $obj->record_errors->full_messages());
        $status = 420;
    }
    if ($status == 420) {
        $status = '420 Invalid Record';
    } elseif ($status == 421) {
        $status = '421 User Throttled';
    } elseif ($status == 422) {
        $status = '422 Locked';
    } elseif ($status == 423) {
        $status = '423 Already Exists';
    } elseif ($status == 424) {
        $status = '424 Invalid Parameters';
    }
    switch (Request::$format) {
        case 'html':
            notice('Error: ' . $obj);
            check_array($redirect_to_params);
            call_user_func_array('redirect_to', $redirect_to_params);
        case 'json':
            render('json', to_json(array_merge($extra_api_params, array('success' => false, 'reason' => $obj))), array('status' => $status));
        case 'xml':
            // fmt.xml {render :xml => extra_api_params.merge(:success => false, :reason => obj).to_xml(:root => "response"), :status => status}
            break;
    }
}
Example #2
0
/**
 * Set variables in Request::$params automatically
 *
 * This simple checks if variables exist in the $params object,
 * and if they don't, they will be created automatically, avoiding
 * things like:
 * <input type="text" value="<?php echo !empty(Request::$params->query) ? Request::$params->query : null ?>" />
 * and just echo the parameter.
 * <input type="text" value="<?php echo Request::$params->query ?>" />
 *
 * By default the value is null. To give a custom value for a param,
 * make the param the key and the value its value.
 * 
 * Example:
 * auto_create_params(array('query', 'tags' => 'none', 'users->foo->id'));
 * $params->query will be null while $params->tags will be 'none' and
 * $params->users->foo->id will also be null.
 */
function auto_set_params($params, $default_value = null)
{
    check_array($params);
    foreach ($params as $key => $param) {
        if (is_int($key)) {
            $value = $default_value;
        } else {
            $value = $param;
            $param = $key;
        }
        if (property_exists(Request::$params, $param)) {
            continue;
        }
        if (is_int(strpos($param, '->'))) {
            $subs = explode('->', $param);
            $param_sub =& Request::$params;
            $i = 0;
            foreach ($subs as $sub) {
                if (!property_exists($param_sub, $sub)) {
                    if ($i == count($subs) - 1) {
                        $param_sub->{$sub} = $value;
                    } else {
                        $param_sub->{$sub} = new stdClass();
                    }
                }
                $param_sub =& $param_sub->{$sub};
                $i++;
            }
        } else {
            Request::$params->{$param} = $value;
        }
    }
}
Example #3
0
function render_partial($part, $locals = array())
{
    check_array($locals);
    # Will automatically make global vars with names of
    # the active controller and part.
    $locals[] = $part;
    $locals[] = Request::$controller;
    foreach ($locals as $key => $var) {
        if (is_int($key)) {
            global ${$var};
        } else {
            global ${$key};
            ${$key} = $var;
        }
    }
    if (is_int(strpos($part, '/'))) {
        $pos = strrpos($part, '/');
        $part = substr_replace($part, '/_', $pos, 1) . '.php';
        $file = VIEWPATH . $part;
    } else {
        $file = VIEWPATH . Request::$controller . '/_' . $part . '.php';
    }
    if (false === (include $file)) {
        echo "Partial ({$part}) could not be found.";
    }
}
Example #4
0
 static function to_aliased($tags)
 {
     check_array($tags);
     $aliased_tags = array();
     foreach ($tags as $tag_name) {
         $aliased_tags[] = self::to_aliased_helper($tag_name);
     }
     return $aliased_tags;
 }
Example #5
0
 /**
  * Check required parameters
  *
  * Checks if a parameter is missing in the $params object
  * and exits with a 400 status if it is missing.
  *
  * This is useful in post methods, if the user tries to post with empty
  * parameters.
  * 
  * Example: required_params(array('id', 'pool' => 'name'), 'only', 'post', 'put');
  * This will check for $params->id and for $params->pool->name.
  *
  * Created to be used at the first lines of an action file.
  */
 static function required_params($args)
 {
     $params = array_shift($args);
     check_array($params);
     if (!empty($args)) {
         $type = array_shift($args);
         $methods = $args;
         unset($args);
         if (($type == 'only' && in_array(strtolower(self::$method), $methods) || $type == 'except' && !in_array(strtolower(self::$method), $methods)) && !self::check_required_params($params)) {
             exit_with_status(400);
         }
     } else {
         if (!self::check_required_params($params)) {
             exit_with_status(400);
         }
     }
 }
function after_filter($functions, $filter = null, $actions = null)
{
    if ($filter) {
        $actions = explode(',', str_replace(' ', '', $actions));
        if (!filter_actions($filter, $actions)) {
            return;
        }
    }
    check_array($functions);
    foreach ($functions as $func => $params) {
        if (is_numeric($func)) {
            $func = $params;
            $params = array();
        } else {
            check_array($params);
        }
        ActionController::$after_filters[] = array($func, $params);
    }
}
Example #7
0
function check_array($array, $arr_key)
{
    global $config, $new_config, $level;
    global $check_good, $check_bad, $check_empty, $check_element;
    foreach ($array as $key => $value) {
        $check_b = "<font color='{$check_empty}'>";
        $check_e = "</font>";
        if (!is_array($value)) {
            if (!empty($arr_key)) {
                if (in_array($value, $config[$arr_key])) {
                    $check_b = "<font color='{$check_good}'>";
                    $check_e = "</font>";
                }
            } else {
                if (isset($config[$key])) {
                    if ($config[$key] === $new_config[$key]) {
                        $check_b = "<font color='{$check_good}'>";
                        $check_e = "</font>";
                    } else {
                        $check_b = "<font color='{$check_bad}'>";
                        $check_e = "</font>";
                    }
                }
            }
            for ($i = 0; $i < $level + 1; $i++) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            if ($level) {
                $ccf = '';
            } else {
                $ccf = "<font color='{$check_element}'>\$config</font>";
            }
            echo $ccf . "['<font color='{$check_element}'>" . $key . "</font>'] => " . $check_b . $value . "&nbsp" . $check_e . "<br />\n";
        } else {
            if (array_key_exists($key, $config)) {
                $check_b = "<font color='#00CC00'>";
                $check_e = "</font>";
            }
            for ($i = 0; $i < $level + 1; $i++) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            echo "<font color='{$check_element}'>\$config</font>['<font color='{$check_element}'>" . $key . "</font>'] => " . $check_b . "Array" . $check_e . "<br />\n";
            for ($i = 0; $i < $level + 1; $i++) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            echo "&nbsp;&nbsp;(<br />\n";
            $level++;
            check_array($value, $key);
            $level--;
            for ($i = 0; $i < $level + 1; $i++) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            echo "&nbsp;&nbsp;)<br />\n";
        }
    }
}
<?php

//input array
$clg1 = array(12, 11, 5, 2, 7, 5, 11);
$clg2 = array(5, 11, 5, 7, 11, 2, 11);
if (is_equal($clg1, $clg2)) {
    if (is_negative_value($clg1) && is_negative_value($clg2)) {
        if (check_array($clg1, $clg2)) {
            echo "Equal";
        } else {
            echo "Unequal";
        }
    } else {
        echo "Invalid";
    }
} else {
    echo "Unequal";
}
//if array are equal in length
function is_equal($array1, $array2)
{
    $len1 = count($array1);
    $len2 = count($array2);
    if ($len1 == $len2) {
        return 1;
    } else {
        return 0;
    }
}
//checking if array has any negative value
function is_negative_value($array1)
 protected static function parse_find_params(&$select_type, &$params = array())
 {
     $select_types = array('first', 'last', 'all');
     if (is_string($select_type) && in_array($select_type, $select_types)) {
         // if (!$params)
         // $params['select']
         // return false;
         $find_params['select_type'] = $select_type;
     } else {
         $params = $select_type;
         $find_params['select_type'] = 'first';
     }
     if (!is_array($params) && (is_int($params) || ctype_digit($params))) {
         # single ID.
         $params = array('conditions' => array(self::t() . '.id = ?', $params));
     } elseif (is_array($params) && is_indexed_arr($params)) {
         # array of IDs.
         $params = array('conditions' => array(self::t() . '.id IN (??)', $params));
     }
     check_array($params);
     if (!empty($params['return_array'])) {
         $find_params['return_array'] = true;
         unset($params['return_array']);
     } else {
         $find_params['return_array'] = false;
     }
     if (!empty($params['return_value'])) {
         $find_params['return_value'] = $params['return_value'];
         $find_params['return_array'] = true;
         unset($params['return_value']);
     } else {
         $find_params['return_value'] = false;
     }
     # for collection
     if (isset($params['model_name'])) {
         $find_params['model_name'] = $params['model_name'];
         unset($params['model_name']);
     } else {
         $find_params['model_name'] = self::cn();
     }
     if ($find_params['select_type'] == 'all' && isset($params['select'])) {
         $find_params['return_array'] = true;
     }
     if (!empty($params['return'])) {
         switch ($params['return']) {
             case 'model':
                 $find_params['return_array'] = false;
                 break;
         }
         unset($params['return']);
     }
     return $find_params;
 }
Example #10
0
 function calculate_related($tags)
 {
     if (!$tags) {
         return array();
     }
     check_array($tags);
     $from = array("posts_tags pt0");
     $cond = array("pt0.post_id = pt1.post_id");
     $sql = "";
     # Ignore deleted posts in pt0, so the count excludes them.
     $cond[] = "(SELECT TRUE FROM posts p0 WHERE p0.id = pt0.post_id AND p0.status <> 'deleted')";
     foreach (range(1, count($tags)) as $i) {
         $from[] = "posts_tags pt{$i}";
     }
     if (count($tags) > 1) {
         foreach (range(2, count($tags)) as $i) {
             $cond[] = "pt1.post_id = pt{$i}.post_id";
         }
     }
     foreach (range(1, count($tags)) as $i) {
         $cond[] = "pt{$i}.tag_id = (SELECT id FROM tags WHERE name = ?)";
     }
     $sql .= "(SELECT name FROM tags WHERE id = pt0.tag_id) AS tag, COUNT(*) AS tag_count";
     $sql .= " FROM " . implode(', ', $from);
     $sql .= " WHERE " . implode(' AND ', $cond);
     $sql .= " GROUP BY pt0.tag_id";
     $sql .= " ORDER BY tag_count DESC LIMIT 25";
     $params = array_merge(array($sql), $tags);
     $tags = call_user_func_array('DB::select', $params);
     if (!$tags) {
         return array();
     }
     $calc = array();
     foreach ($tags as $tag) {
         $calc[] = array($tag['tag'], $tag['tag_count']);
     }
     return $calc;
 }
Example #11
0
function validate_data($data, $callbacks, $return_messages = false)
{
    check_array($callbacks);
    foreach ($callbacks as $key => $val) {
        $func = $args = null;
        if (!is_int($key)) {
            if ($data === null) {
                if (empty($callbacks['accept_null'])) {
                    return false;
                } else {
                    unset($callbacks['accept_null']);
                }
            }
            if (is_array($val) && isset($val['message'])) {
                $message = $val['message'];
                unset($val['message']);
            }
            switch ($key) {
                case '%callback%':
                    $func = $val;
                    $args = array($data);
                    break;
                    # Checks a string's length.
                # Checks a string's length.
                case 'length':
                    check_array($val);
                    $compare_to = array_shift($val);
                    // $messages = $val; // future messages.
                    $r = compare_num(strlen($data), $compare_to, $return_messages, true);
                    return $r;
                    break;
                case 'format':
                    is_array($val) && ($val = array_shift($val));
                    if (!preg_match($val, $data)) {
                        return false;
                    }
                    break;
                    /**
                     * Number:
                     * ($data, array('number' => array(true, 'odd', 'even', '>=90')))
                     * Note: By default the param will be passed to compare_num.
                     */
                /**
                 * Number:
                 * ($data, array('number' => array(true, 'odd', 'even', '>=90')))
                 * Note: By default the param will be passed to compare_num.
                 */
                case 'number':
                    check_array($val);
                    foreach ($val as $param) {
                        switch ($param) {
                            case $param === true:
                                if (!ctype_digit((string) $data)) {
                                    return false;
                                }
                                break;
                            case 'odd':
                                if (!($data & 1)) {
                                    return false;
                                }
                                break;
                            case 'even':
                                if (!($data & 2)) {
                                    return false;
                                }
                                break;
                            default:
                                $r = compare_num(strlen($data), $compare_to, $return_messages, false);
                                return $r;
                                break;
                        }
                    }
                    break;
                case 'in':
                    if (!in_array($data, $val)) {
                        return false;
                    }
                    break;
            }
        } else {
            $func = $val;
            $args = array($data);
        }
        if (!empty($func)) {
            if (!call_user_func_array($func, $args)) {
                return false;
            }
        }
    }
    return true;
}
Example #12
0
function check_array($array, $arr_key)
{
    global $config, $new_config, $level;
    // расцветка для подсветки элементов
    $check_good = "#33AA33";
    $check_bad = "#FF3333";
    $check_empty = "#666666";
    $check_element = "#FF7F00";
    //GLOBAL $check_good,$check_bad,$check_empty,$check_element;
    $chcontent = "";
    foreach ($array as $key => $value) {
        $check_b = "<font color='{$check_empty}'>";
        $check_e = "</font>";
        if (!is_array($value)) {
            if (!empty($arr_key)) {
                if (in_array($value, $config[$arr_key])) {
                    $check_b = "<font color='{$check_good}'>";
                    $check_e = "</font>";
                }
            } else {
                if (isset($config[$key])) {
                    if ($config[$key] == $new_config[$key]) {
                        $check_b = "<font color='{$check_good}'>";
                        $check_e = "</font>";
                    } else {
                        $check_b = "<font color='{$check_bad}'>";
                        $check_e = "</font>";
                    }
                }
            }
            for ($i = 0; $i < $level + 1; $i++) {
                $chcontent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            if ($level) {
                $ccf = '';
            } else {
                $ccf = "<font color='{$check_element}'>\$config</font>";
            }
            $chcontent .= $ccf . "['<font color='{$check_element}'>" . $key . "</font>'] => " . $check_b . $value . "&nbsp" . $check_e . "<br />\n";
        } else {
            if (array_key_exists($key, $config)) {
                $check_b = "<font color='#00CC00'>";
                $check_e = "</font>";
            }
            for ($i = 0; $i < $level + 1; $i++) {
                $chcontent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $chcontent .= "<font color='{$check_element}'>\$config</font>['<font color='{$check_element}'>" . $key . "</font>'] => " . $check_b . "Array" . $check_e . "<br />\n";
            for ($i = 0; $i < $level + 1; $i++) {
                $chcontent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $chcontent .= "&nbsp;&nbsp;(<br />\n";
            $level++;
            $chcontent .= check_array($value, $key);
            $level--;
            for ($i = 0; $i < $level + 1; $i++) {
                $chcontent .= "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            $chcontent .= "&nbsp;&nbsp;)<br />\n";
        }
    }
    return $chcontent;
}