Skip to content

Instantly share code, notes, and snippets.

@vlastv
Last active December 17, 2015 08:49
Show Gist options
  • Save vlastv/5583080 to your computer and use it in GitHub Desktop.
Save vlastv/5583080 to your computer and use it in GitHub Desktop.
NetCat CMS extendsRequirments: PHP >= 5.3
<?php
function jm_cc_link($cc, $action = 'index', $absolute = false)
{
static $storage;
global $db;
$storageKey = serialize(func_get_args());
if (!isset($storage[$storageKey])) {
$sql = sprintf('
SELECT sub.Hidden_URL, cc.EnglishName FROM Sub_Class cc
JOIN Subdivision sub ON cc.Subdivision_ID = sub.Subdivision_ID
WHERE cc.Sub_Class_ID = %d
', $cc);
list($path, $name) = $db->get_row($sql, ARRAY_N);
$storage[$storageKey] = 'index' === $action ? sprintf('%s%s.html', $path, $name) : sprintf('%s%s_%s.html', $path, $action, $name);
}
return $storage[$storageKey];
}
function jm_browse_array(array $data, array $template, $current = null, $default = null, $property = null)
{
if (empty($data) || empty($template)) {
return;
}
$result = null;
$result .= isset($template['prefix']) ? $template['prefix'] : null;
if (null === $current && null !== $default) {
$current = $default;
}
$index = 0; $index0 = 0;
foreach ($data as $key => $value) {
++$index;
if (isset($template['divider']) && $index0 > 0) {
$result .= $template['divider'];
}
if ($current === (null === $property && !is_array($value) ? $key : $value[$property])) {
$row = $template['active'];
} else {
$row = $template['unactive'];
}
$search = array('KEY', 'INDEX', 'INDEX0');
$replace = array($key, $index, $index0);
if (is_array($value)) {
$search = array_merge($search, array_keys($value));
$replace = array_merge($replace, array_values($value));
} else {
$search[] = 'ELEMENT';
$replace[] = $value;
}
$search = array_map(function ($value) { return '%' . $value; }, $search);
$result .= str_replace($search, $replace, $row);
++$index0;
}
$result .= isset($template['suffix']) ? $template['suffix'] : null;
return $result;
}
function jm_is_human($set = null)
{
if(null !== $set) {
$_SESSION['_HUMAN'] = (bool) $set;
}
return isset($_SESSION['_HUMAN']) && $_SESSION['_HUMAN'];
}
function jm_is_ajax()
{
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function jm_response_im($content, $code = 200, $contentType = 'text/html')
{
$map = array(
200 => 'OK',
204 => 'No Content',
400 => 'Bad Request',
);
header('Content-Type: ' . $contentType);
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
$text = $map[$code];
header($protocol . ' ' . $code . ' ' . $text);
die($content);
}
function jm_json_response_im($data, $code)
{
return jm_response_im(json_encode($data), $code, 'application/javascript');
}
function jm_helper_ajax_handler($error)
{
if(jm_is_ajax()) {
$content = array(
'errors' => strip_tags($error),
);
if(!jm_is_human()) {
$content['captcha'] = nc_captcha_formfield();
}
jm_json_response_im($content, 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment