Completed
Push — master ( 98eeb4...22887c )
by Michael
11s
created

class/language.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * xLanguage module (eXtensible Language Management For XOOPS)
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright    XOOPS Project (https://xoops.org)
13
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
14
 * @package      xlanguage
15
 * @since        2.0
16
 * @author       D.J.(phppp) [email protected]
17
 **/
18
19
//include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
20
//include_once(XOOPS_ROOT_PATH.'/modules/xlanguage/include/vars.php');
21
//include_once(XOOPS_ROOT_PATH.'/modules/xlanguage/include/functions.php');
22
23
/**
24
 * Class Blanguage
25
 */
26
class Blanguage extends XoopsObject
27
{
28
    public $isBase;
29
30
    /**
31
     * Blanguage constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
36
        $this->table = $this->db->prefix('xlanguage_base');
37
        $this->initVar('lang_id', XOBJ_DTYPE_INT);
38
        $this->initVar('weight', XOBJ_DTYPE_INT);
39
        $this->initVar('lang_name', XOBJ_DTYPE_TXTBOX);
40
        $this->initVar('lang_desc', XOBJ_DTYPE_TXTBOX);
41
        $this->initVar('lang_code', XOBJ_DTYPE_TXTBOX);
42
        $this->initVar('lang_charset', XOBJ_DTYPE_TXTBOX);
43
        $this->initVar('lang_image', XOBJ_DTYPE_TXTBOX);
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function prepareVars()
50
    {
51
        foreach ($this->vars as $k => $v) {
52
            $cleanv = $this->cleanVars[$k];
53
            switch ($v['data_type']) {
54
                case XOBJ_DTYPE_TXTBOX:
55
                case XOBJ_DTYPE_TXTAREA:
56
                case XOBJ_DTYPE_SOURCE:
57
                case XOBJ_DTYPE_EMAIL:
58
                    $cleanv = $v['changed'] ? $cleanv : '';
59
                    if (!isset($v['not_gpc']) || !$v['not_gpc']) {
60
                        $cleanv = $this->db->quoteString($cleanv);
61
                    }
62
                    break;
63
                case XOBJ_DTYPE_INT:
64
                    $cleanv = $v['changed'] ? (int)$cleanv : 0;
65
                    break;
66
                case XOBJ_DTYPE_ARRAY:
67
                    $cleanv = $v['changed'] ? $cleanv : serialize(array());
68
                    break;
69
                case XOBJ_DTYPE_STIME:
70
                case XOBJ_DTYPE_MTIME:
71
                case XOBJ_DTYPE_LTIME:
72
                    $cleanv = $v['changed'] ? $cleanv : 0;
73
                    break;
74
75
                default:
76
                    break;
77
            }
78
            $this->cleanVars[$k] =& $cleanv;
79
            unset($cleanv);
80
        }
81
82
        return true;
83
    }
84
85
    public function setBase()
86
    {
87
        $this->isBase = true;
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function hasBase()
94
    {
95
        return $this->isBase;
96
    }
97
}
98
99
/**
100
 * Class Xlanguage
101
 */
102
class Xlanguage extends Blanguage
103
{
104
    /**
105
     * Xlanguage constructor.
106
     */
107
    public function __construct()
108
    {
109
        parent::__construct();
110
        $this->table = $this->db->prefix('xlanguage_ext');
111
        $this->initVar('lang_base', XOBJ_DTYPE_TXTBOX);
112
        $this->isBase = false;
113
    }
114
}
115
116
/**
117
 * Class XlanguageLanguageHandler
118
 */
119
class XlanguageLanguageHandler extends XoopsObjectHandler
120
{
121
    public $cached_config;
122
123
    public function loadConfig()
124
    {
125
        $this->cached_config = $this->loadFileConfig();
126
    }
127
128
    /**
129
     * @param int  $id
130
     * @param bool $isBase
131
     *
132
     * @return Blanguage|null|Xlanguage
133
     */
134
    public function get($id, $isBase = true)
135
    {
136
        $lang = null;
137
        $id   = (int)$id;
138
        if (!$id) {
139
            return $lang;
140
        }
141
        $prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext';
142
        if (isset($this->cached_config[$prefix][$id])) {
143
            $array = $this->cached_config[$prefix][$id];
144
        } else {
145
            $sql   = 'SELECT * FROM ' . $this->db->prefix($prefix) . ' WHERE lang_id=' . $id;
146
            $array = $this->db->fetchArray($this->db->query($sql));
147
        }
148
        if (!is_array($array) || count($array) == 0) {
149
            return $lang;
150
        }
151
        $lang = $this->create(false, $isBase);
152
        $lang->assignVars($array);
153
        if ($isBase) {
154
            $lang->isBase = true;
155
        }
156
157
        return $lang;
158
    }
159
160
    /**
161
     * @param $name
162
     *
163
     * @return Blanguage|null|Xlanguage
164
     */
165
    public function getByName($name)
166
    {
167
        $lang = null;
168
        if (empty($name) || preg_match("/[^a-zA-Z0-9\_\-]/", $name)) {
169
            return $lang;
170
        }
171
        $isBase = false;
172
        if (isset($this->cached_config['xlanguage_base'][$name])) {
173
            $array  = $this->cached_config['xlanguage_base'][$name];
174
            $isBase = true;
175
        } elseif (isset($this->cached_config['xlanguage_ext'][$name])) {
176
            $array = $this->cached_config['xlanguage_ext'][$name];
177
        } elseif (!isset($this->cached_config)) {
178
            $sql    = 'SELECT * FROM ' . $this->db->prefix('xlanguage_base') . ' WHERE lang_name=\'' . $name . '\'';
179
            $result = $this->db->query($sql);
180
            $array  = $this->db->fetchArray($result);
181
            if (!is_array($array) || count($array) == 0) {
182
                $sql    = 'SELECT * FROM ' . $this->db->prefix('xlanguage_ext') . ' WHERE lang_name=\'' . $name . '\'';
183
                $result = $this->db->query($sql);
184
                $array  = $this->db->fetchArray($result);
185
                if (!is_array($array) || count($array) == 0) {
186
                    return $lang;
187
                }
188
            } else {
189
                $isBase = true;
190
            }
191
        }
192
        if (empty($array)) {
193
            return $lang;
194
        }
195
        $lang = $this->create(false, $isBase);
196
        $lang->assignVars($array);
197
        if (!isset($array['lang_base'])) {
198
            $lang->isBase = true;
199
        }
200
201
        return $lang;
202
    }
203
204
    /**
205
     * @param bool $isBase
206
     *
207
     * @return array
208
     */
209
    public function getAll($isBase = true)
210
    {
211
        $prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext';
212
        $ret    = array();
213
        if (isset($this->cached_config[$prefix])) {
214
            $array = $this->cached_config[$prefix];
215
            foreach ($array as $lang_name => $myrow) {
216
                $lang = $this->create(false, $isBase);
217
                $lang->assignVars($myrow);
218
                $ret[$myrow['lang_name']] =& $lang;
219
                unset($lang);
220
            }
221
        } elseif (!isset($this->cached_config)) {
222
            $sql    = 'SELECT * FROM ' . $this->db->prefix($prefix);
223
            $result = $this->db->query($sql);
224
            while ($myrow = $this->db->fetchArray($result)) {
225
                $lang = $this->create(false, $isBase);
226
                $lang->assignVars($myrow);
227
                $ret[$myrow['lang_name']] =& $lang;
228
                unset($lang);
229
            }
230
        }
231
232
        return $ret;
233
    }
234
235
    /**
236
     * @return array
237
     */
238
    public function getAllList()
239
    {
240
        $baseArray = $this->getAll();
241
242
        $extArray = $this->getAll(false);
243
        $ret      = array();
244
        if (is_array($baseArray) && count($baseArray) > 0) {
245
            foreach ($baseArray as $base) {
246
                $ret[$base->getVar('lang_name')]['base'] = $base;
247
                unset($base);
248
            }
249
        }
250
        if (is_array($extArray) && count($extArray) > 0) {
251
            foreach ($extArray as $ext) {
252
                $ret[$ext->getVar('lang_base')]['ext'][] = $ext;
253
                unset($ext);
254
            }
255
        }
256
257
        return $ret;
258
    }
259
260
    /**
261
     * @param bool $isNew
262
     * @param bool $isBase
263
     *
264
     * @return Blanguage|Xlanguage
265
     */
266
    public function create($isNew = true, $isBase = true)
267
    {
268
        if ($isBase) {
269
            $lang         = new Blanguage();
270
            $lang->isBase = true;
271
        } else {
272
            $lang = new Xlanguage();
273
        }
274
        if ($isNew) {
275
            $lang->setNew();
276
        }
277
278
        return $lang;
279
    }
280
281
    /**
282
     * @param  XoopsObject $object
283
     * @return bool
284
     * @internal param object $lang
285
     *
286
     */
287
    public function insert(XoopsObject $object)//insert(&$lang)
288
    {
289
        $lang = $object;
290
        if (!$lang->isDirty()) {
291
            return true;
292
        }
293
        if (!$lang->cleanVars()) {
294
            return false;
295
        }
296
        $lang->prepareVars();
297
        foreach ($lang->cleanVars as $k => $v) {
298
            ${$k} = $v;
299
        }
300
301
        if ($lang->isNew()) {
302
            $var_array = array(
303
                'lang_id',
304
                'weight',
305
                'lang_name',
306
                'lang_desc',
307
                'lang_code',
308
                'lang_charset',
309
                'lang_image',
310
                'lang_base'
311
            );
312 View Code Duplication
            if ($lang->isBase) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
                $var_array = array(
314
                    'lang_id',
315
                    'weight',
316
                    'lang_name',
317
                    'lang_desc',
318
                    'lang_code',
319
                    'lang_charset',
320
                    'lang_image'
321
                );
322
            }
323
            $lang_id = $this->db->genId($lang->table . '_lang_id_seq');
324
            foreach ($var_array as $var) {
325
                $val_array[] = ${$var};
326
            }
327
            $sql = 'INSERT INTO ' . $lang->table . ' (' . implode(',', $var_array) . ') VALUES (' . implode(',', $val_array) . ')';
328 View Code Duplication
            if (!$result = $this->db->queryF($sql)) {
329
                xoops_error('Insert language error:' . $sql);
330
331
                return false;
332
            }
333
            if ($lang_id == 0) {
334
                $lang_id = $this->db->getInsertId();
335
            }
336
            $lang->setVar('lang_id', $lang_id);
337
        } else {
338
            $var_array = array(
339
                'weight',
340
                'lang_name',
341
                'lang_desc',
342
                'lang_code',
343
                'lang_charset',
344
                'lang_image',
345
                'lang_base'
346
            );
347 View Code Duplication
            if ($lang->isBase) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
348
                $var_array = array('weight', 'lang_name', 'lang_desc', 'lang_code', 'lang_charset', 'lang_image');
349
            }
350
            $set_array = array();
351
            foreach ($var_array as $var) {
352
                $set_array[] = "$var = " . ${$var};
353
            }
354
            $set_string = implode(',', $set_array);
355
            $sql        = 'UPDATE ' . $lang->table . ' SET ' . $set_string . ' WHERE lang_id = ' . $lang->getVar('lang_id');
356 View Code Duplication
            if (!$result = $this->db->queryF($sql)) {
357
                xoops_error('update language error:' . $sql);
358
359
                return false;
360
            }
361
        }
362
        $this->createConfig();
363
364
        return $lang->getVar('lang_id');
365
    }
366
367
    /**
368
     * @param  XoopsObject $lang
369
     * @return bool
370
     * @internal param object $lang
371
     *
372
     */
373
    public function delete(XoopsObject $lang)//delete(&$lang)
374
    {
375
        if (!is_object($lang) || !$lang->getVar('lang_id')) {
376
            return true;
377
        }
378
        $sql = 'DELETE FROM ' . $lang->table . ' WHERE lang_id= ' . $lang->getVar('lang_id');
379
        if (!$result = $this->db->query($sql)) {
380
            return false;
381
        }
382
        $this->createConfig();
383
384
        return true;
385
    }
386
387
    /**
388
     * @return array
389
     */
390
    public function getXoopsLangList()
391
    {
392
        return XoopsLists::getLangList();
393
    }
394
395
    /**
396
     * @return bool
397
     */
398
    public function createConfig()
399
    {
400
        $file_config = XLANGUAGE_CONFIG_FILE;
401
        @unlink($file_config);
402
        if (!$fp = fopen($file_config, 'w')) {
403
            echo '<br> the config file can not be created: ' . $file_config;
404
405
            return false;
406
        }
407
408
        $file_content = '<?php';
409
        unset($this->cached_config);
410
        $baseArray = $this->getAll();
411
        if (is_array($baseArray) && count($baseArray) > 0) {
412
            $file_content .= "\n    \$" . XLANGUAGE_CONFIG_VAR . "['xlanguage_base'] = array(";
413
            foreach ($baseArray as $lang) {
414
                $file_content .= "\n        \"" . $lang->getVar('lang_name') . '"=>array(';
415
                $file_content .= "\n            \"lang_id\"=>" . $lang->getVar('lang_id') . ',';
416
                $file_content .= "\n            \"weight\"=>" . $lang->getVar('weight') . ',';
417
                $file_content .= "\n            \"lang_name\"=>\"" . $lang->getVar('lang_name') . '",';
418
                $file_content .= "\n            \"lang_desc\"=>\"" . $lang->getVar('lang_desc') . '",';
419
                $file_content .= "\n            \"lang_code\"=>\"" . $lang->getVar('lang_code') . '",';
420
                $file_content .= "\n            \"lang_charset\"=>\"" . $lang->getVar('lang_charset') . '",';
421
                $file_content .= "\n            \"lang_image\"=>\"" . $lang->getVar('lang_image') . '"';
422
                $file_content .= "\n        ),";
423
            }
424
            $file_content .= "\n    );";
425
        }
426
427
        $extArray = $this->getAll(false);
428
        if (is_array($extArray) && count($extArray) > 0) {
429
            $file_content .= "\n    \$" . XLANGUAGE_CONFIG_VAR . "['xlanguage_ext'] = array(";
430
            foreach ($extArray as $lang) {
431
                $file_content .= "\n        \"" . $lang->getVar('lang_name') . '"=>array(';
432
                $file_content .= "\n            \"lang_id\"=>" . $lang->getVar('lang_id') . ',';
433
                $file_content .= "\n            \"weight\"=>" . $lang->getVar('weight') . ',';
434
                $file_content .= "\n            \"lang_name\"=>\"" . $lang->getVar('lang_name') . '",';
435
                $file_content .= "\n            \"lang_desc\"=>\"" . $lang->getVar('lang_desc') . '",';
436
                $file_content .= "\n            \"lang_code\"=>\"" . $lang->getVar('lang_code') . '",';
437
                $file_content .= "\n            \"lang_charset\"=>\"" . $lang->getVar('lang_charset') . '",';
438
                $file_content .= "\n            \"lang_image\"=>\"" . $lang->getVar('lang_image') . '",';
439
                $file_content .= "\n            \"lang_base\"=>\"" . $lang->getVar('lang_base') . '"';
440
                $file_content .= "\n        ),";
441
            }
442
            $file_content .= "\n    );";
443
        }
444
445
        $file_content .= "\n?>";
446
        fwrite($fp, $file_content);
447
        fclose($fp);
448
449
        return true;
450
    }
451
452
    /**
453
     * @return null
454
     */
455
    public function loadFileConfig()
456
    {
457
        $file_config = XLANGUAGE_CONFIG_FILE;
458
        if (!file_exists($file_config)) {
459
            $this->createConfig();
460
        }
461
        if (!is_readable($file_config)) {
462
            $config = null;
463
464
            return $config;
465
        } else {
466
            include $file_config;
467
            if (isset(${XLANGUAGE_CONFIG_VAR})) {
468
                return ${XLANGUAGE_CONFIG_VAR};
469
            } else {
470
                return false;
471
            }
472
        }
473
    }
474
}
475