コード例 #1
0
 /**
  * Handy MYSQL update operation
  *
  * @param string $table The table name without prefix
  * @param array $data The array of data field names and values
  *   The first field/value pair will be used as condition if you did not provide the fourth argument
  *
  *     array(
  *       'conditionField'  => $conditionFieldValue, <===
  *       'fieldNameToSlug' => $valueToSlug,  <=== if $lc_useDBAutoFields is enabled
  *       'fieldName1'      => $value1,
  *       'fieldName2'      => $value2
  *     )
  *
  * @param boolean $useSlug TRUE to include the slug field or FALSE to not exclude it
  *   The fourth argument can be provided here if you want to omit this.
  * @param array|string $condition The condition for the UPDATE query. If you provide this,
  *   the first field of `$data` will not be built for condition
  *
  * ### Example
  *
  *     array(
  *       'fieldName1' => $value1,
  *       'fieldName2' => $value2
  *     )
  *
  * OR
  *
  *     db_or(array(
  *       'fieldName1' => $value1,
  *       'fieldName2' => $value2
  *     ))
  *
  * @return boolean Returns TRUE on success or FALSE on failure
  */
 function db_update($table, $data = array(), $useSlug = true, $condition = null)
 {
     if (count($data) == 0) {
         return;
     }
     global $_DB;
     global $_conn;
     global $lc_useDBAutoFields;
     if (func_num_args() === 3 && (gettype($useSlug) === 'string' || is_array($useSlug))) {
         $condition = $useSlug;
         $useSlug = true;
     }
     $table = ltrim($table, db_prefix());
     $table = db_prefix() . $table;
     # Invoke the hook db_update_[table_name] if any
     $hook = 'db_update_' . strtolower($table);
     if (function_exists($hook)) {
         return call_user_func_array($hook, array($table, $data, $useSlug, $condition));
     }
     # if slug is already provided in the data array, use it
     if (array_key_exists('slug', $data)) {
         $slug = db_escapeString($data['slug']);
         $slug = _slug($slug);
         $data['slug'] = $slug;
         session_set('lastInsertSlug', $slug);
         $useSlug = false;
     }
     $fields = array();
     $slug = '';
     $cond = '';
     $notCond = '';
     $i = 0;
     $slugIndex = 1;
     if ($condition) {
         $slugIndex = 0;
     }
     $dsm = $_DB->schemaManager;
     foreach ($data as $field => $value) {
         if ($i === 0 && !$condition) {
             # $data[0] is for PK condition, but only if $condition is not provided
             $cond = array($field => db_escapeString($value));
             # for PK condition
             $i++;
             continue;
         }
         if (is_object($dsm) && $dsm->isLoaded()) {
             $fieldType = $dsm->getFieldType($table, $field);
             if (is_array($value) && $fieldType == 'array') {
                 $value = serialize($value);
             } elseif (is_array($value) && $fieldType == 'json') {
                 $jsonValue = json_encode($value);
                 $value = $jsonValue ? $jsonValue : null;
             } elseif ($fieldType == 'boolean') {
                 $value = $value ? 1 : 0;
             }
         }
         if (is_null($value)) {
             $fields[] = QueryBuilder::quote($field) . ' = NULL';
         } else {
             $fields[] = QueryBuilder::quote($field) . ' = "' . db_escapeString($value) . '"';
         }
         if ($i === $slugIndex && $useSlug === true) {
             # $data[1] is slug
             $slug = db_escapeString($value);
         }
         $i++;
     }
     # must have condition
     # this prevents unexpected update happened to all records
     if ($cond || $condition) {
         if ($cond && is_array($cond)) {
             $cond = db_condition($cond);
         } elseif ($condition && is_array($condition)) {
             $cond = db_condition($condition);
         } elseif ($condition && is_string($condition)) {
             $cond = $condition;
         }
         if (empty($cond)) {
             return false;
         }
         $notCond = 'NOT ( ' . $cond . ' )';
         if (db_tableHasSlug($table, $useSlug)) {
             $slug = _slug($slug, $table, $notCond);
             session_set('lastInsertSlug', $slug);
             $fields[] = '`slug` = "' . $slug . '"';
         }
         if (db_tableHasTimestamps($table)) {
             $fields[] = '`updated` = "' . date('Y-m-d H:i:s') . '"';
         }
         $fields = implode(', ', $fields);
         $sql = 'UPDATE ' . QueryBuilder::quote($table) . '
                 SET ' . $fields . ' WHERE ' . $cond;
         return db_query($sql);
     } else {
         return false;
     }
 }
コード例 #2
0
    $_fn = $_SERVER['HTTP_X_REQUESTED_FILENAME'];
} else {
    die('no file');
}
if (isset($_SERVER['HTTP_X_FILENAME_UP_DIR'])) {
    $_dr = $_SERVER['HTTP_X_FILENAME_UP_DIR'];
} elseif (isset($_SERVER['HTTP_X_REQUESTED_FILEUPDIR'])) {
    $_dr = $_SERVER['HTTP_X_REQUESTED_FILEUPDIR'];
} else {
    die('no updir');
}
if (!is_dir($_dr)) {
    die('no exist updir');
}
// файл
$fn = _slug($_fn);
// каталог
$up_dir = getinfo('FCPATH') . $_dr;
// file_put_contents(FCPATH . 'log.txt', $up_dir . $fn); // лог для отладки
// загрузка
file_put_contents($up_dir . $fn, file_get_contents('php://input'));
if (file_exists($up_dir . $fn)) {
    require_once __DIR__ . '/lib/add-new-page.php';
    add_new_page($up_dir . $fn, $up_dir);
}
function _slug($slug)
{
    $repl = array("А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d", "Е" => "e", "Ё" => "jo", "Ж" => "zh", "З" => "z", "И" => "i", "Й" => "j", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n", "О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t", "У" => "u", "Ф" => "f", "Х" => "h", "Ц" => "c", "Ч" => "ch", "Ш" => "sh", "Щ" => "shh", "Ъ" => "", "Ы" => "y", "Ь" => "", "Э" => "e", "Ю" => "ju", "Я" => "ja", "а" => "a", "б" => "b", "в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "jo", "ж" => "zh", "з" => "z", "и" => "i", "й" => "j", "к" => "k", "л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p", "р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f", "х" => "h", "ц" => "c", "ч" => "ch", "ш" => "sh", "щ" => "shh", "ъ" => "", "ы" => "y", "ь" => "", "э" => "e", "ю" => "ju", "я" => "ja", "Є" => "ye", "є" => "ye", "І" => "i", "і" => "i", "Ї" => "yi", "ї" => "yi", "Ґ" => "g", "ґ" => "g", "Ў" => "u", "ў" => "u", "'" => "", "ă" => 'a', "î" => 'i', "ş" => 'sh', "ţ" => 'ts', "â" => 'a', "«" => "", "»" => "", "—" => "-", "`" => "", " " => "-", "[" => "", "]" => "", "{" => "", "}" => "", "<" => "", ">" => "", "?" => "", "," => "", "*" => "", "%" => "", "\$" => "", "@" => "", "!" => "", ";" => "", ":" => "", "^" => "", "\"" => "", "&" => "", "=" => "", "№" => "", "\\" => "", "/" => "", "#" => "", "(" => "", ")" => "", "~" => "", "|" => "", "+" => "", "”" => "", "“" => "", "'" => "", "’" => "", "—" => "-", "–" => "-", "™" => "tm", "©" => "c", "®" => "r", "…" => "", "“" => "", "”" => "", "„" => "", " " => "-");
    $slug = strtr(trim($slug), $repl);
    $slug = htmlentities($slug);
    // если есть что-то из юникода
コード例 #3
0
function _upload($up_dir, $fn, $r = array())
{
    $fn = _slug($fn);
    $ext = substr(strrchr($fn, '.'), 1);
    $name = substr($fn, 0, strlen($fn) - strlen($ext) - 1);
    // Если имя файла пустое, только расширение.
    if ($fn == '.' . $ext) {
        $fn = '1' . $fn;
    }
    // Если файл уже существует.
    if (file_exists($up_dir . $fn)) {
        for ($i = 1; $i < 100; $i++) {
            $fn = $name . '-' . $i . '.' . $ext;
            if (!file_exists($up_dir . $fn)) {
                break;
            }
        }
    }
    file_put_contents($up_dir . $fn, file_get_contents('php://input'));
    if (!in_array($ext, array('jpg', 'jpeg', 'png', 'gif'))) {
        // Не картинка, загрузили, больше ничего не надо.
        return;
    }
    // С какими дефолтными параметрами ресайзим и делаем миниатюрки.
    $def = array('resize_images' => mso_get_option('resize_images', 'general', '600'), 'size_image_mini' => mso_get_option('size_image_mini', 'general', '150'), 'image_mini_type' => mso_get_option('image_mini_type', 'general', '1'), 'use_watermark' => mso_get_option('use_watermark', 'general', '0'), 'watermark_type' => mso_get_option('watermark_type', 'general', '1'));
    //Можем передать свои параметры в эту функцию.
    $r = array_merge($def, $r);
    require getinfo('shared_dir') . 'stock/thumb/thumb.php';
    // У нас есть uploads_dir, а нужен url
    $url = str_replace(getinfo('uploads_dir'), getinfo('uploads_url'), $up_dir);
    // Если картинка больше, чем нужно, то делаем ресайз, иначе ничего не делаем.
    // $new_size пригодится, когда из новой картинки будем миниатюру делать.
    $size = $new_size = getimagesize($up_dir . $fn);
    if ($size[0] > $r['resize_images'] || $size[1] > $r['resize_images']) {
        if ($size[0] > $size[1]) {
            $new_size[0] = $r['resize_images'];
            $new_size[1] = round($size[1] / ($size[0] / $new_size[0]));
        } else {
            $new_size[1] = $r['resize_images'];
            $new_size[0] = round($size[0] / ($size[1] / $new_size[1]));
        }
        //pr($new_size);
        thumb_generate($url . $fn, $new_size[0], $new_size[1], false, 'resize', true, '', false);
    }
    // Создание ватермарки, если такая опция и есть нужный файл.
    if ($r['use_watermark'] and file_exists(getinfo('uploads_dir') . 'watermark.png')) {
        $water_type = $r['watermark_type'];
        // Расположение ватермарка на картинке
        $hor = 'right';
        //Инитим дефолтом.
        $vrt = 'bottom';
        //Инитим дефолтом.
        if ($water_type == 2 or $water_type == 4) {
            $hor = 'left';
        }
        if ($water_type == 2 or $water_type == 3) {
            $vrt = 'top';
        }
        if ($water_type == 1) {
            $hor = 'center';
            $vrt = 'middle';
        }
        $r_conf = array('image_library' => 'gd2', 'source_image' => $up_dir . $fn, 'new_image' => $up_dir . $fn, 'wm_type' => 'overlay', 'wm_vrt_alignment' => $vrt, 'wm_hor_alignment' => $hor, 'wm_overlay_path' => getinfo('uploads_dir') . 'watermark.png');
        $CI =& get_instance();
        $CI->load->library('image_lib');
        $CI->image_lib->clear();
        $CI->image_lib->initialize($r_conf);
        if (!$CI->image_lib->watermark()) {
            echo '<div class="error">' . t('Водяной знак:') . ' ' . $CI->image_lib->display_errors() . '</div>';
        }
    }
    // $r['image_mini_type'] = 6;
    switch ($r['image_mini_type']) {
        case 1:
            // Пропорциональное уменьшение
            if ($size[0] > $size[1]) {
                $new_size[0] = $r['size_image_mini'];
                $new_size[1] = round($size[1] / ($size[0] / $new_size[0]));
                thumb_generate($url . $fn, $new_size[0], $new_size[1], false, 'resize', true, 'mini', false);
            } else {
                $new_size[1] = $r['size_image_mini'];
                $new_size[0] = round($size[0] / ($size[1] / $new_size[1]));
                thumb_generate($url . $fn, $new_size[0], $new_size[1], false, 'resize', true, 'mini', false);
            }
            break;
        case 2:
            // Обрезки (crop) по центру
            thumb_generate($url . $fn, $r['size_image_mini'], $r['size_image_mini'], false, 'resize_full_crop_center', true, 'mini', false);
            break;
        case 3:
            // Обрезки (crop) с левого верхнего края
            thumb_generate($url . $fn, $r['size_image_mini'], $r['size_image_mini'], false, 'crop', true, 'mini', false);
            break;
        case 4:
            // Обрезки (crop) с левого нижнего края
            $thumb = new Thumb($url . $fn, $postfix = '', $replace_file = true, $subdir = 'mini');
            $thumb->crop($r['size_image_mini'], $r['size_image_mini'], 0, $new_size[1] - $r['size_image_mini']);
            //thumb_generate($url . $fn, $width, $height, false, $type_resize = 'resize_full_crop_center', true, 'mini', false);
            break;
        case 5:
            // Обрезки (crop) с правого верхнего края
            $thumb = new Thumb($url . $fn, $postfix = '', $replace_file = true, $subdir = 'mini');
            $thumb->crop($r['size_image_mini'], $r['size_image_mini'], $new_size[0] - $r['size_image_mini'], 0);
            //thumb_generate($url . $fn, $width, $height, false, $type_resize = 'resize_full_crop_center', true, 'mini', false);
            break;
        case 6:
            // Обрезки (crop) с правого нижнего края
            $thumb = new Thumb($url . $fn, $postfix = '', $replace_file = true, $subdir = 'mini');
            $thumb->crop($r['size_image_mini'], $r['size_image_mini'], $new_size[0] - $r['size_image_mini'], $new_size[1] - $r['size_image_mini']);
            //thumb_generate($url . $fn, $width, $height, false, $type_resize = 'resize_full_crop_center', true, 'mini', false);
            break;
        case 7:
            // Уменьшения и обрезки (crop) в квадрат
            if ($size[0] < $size[1]) {
                thumb_generate($url . $fn, $r['size_image_mini'], $r['size_image_mini'], false, 'resize_crop_center', true, 'mini', false);
            } else {
                thumb_generate($url . $fn, $r['size_image_mini'], $r['size_image_mini'], false, 'resize_h_crop_center', true, 'mini', false);
            }
            break;
    }
    thumb_generate($url . $fn, 100, 100, false, 'resize_full_crop_center', true, '_mso_i', false);
}