Passed
Push — master ( dc1f79...43726e )
by Goffy
02:26
created

IncludeFunctions::render()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 16
nop 0
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: 1.91 IncludeFunctions.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class IncludeFunctions.
27
 */
28
class IncludeFunctions extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /**
31
    *  @public function constructor
32
    *  @param null
33
    */
34
35
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /**
41
    *  @static function getInstance
42
    *  @param null
43
     * @return IncludeFunctions
44
     */
45
    public static function getInstance()
46
    {
47
        static $instance = false;
48
        if (!$instance) {
49
            $instance = new self();
50
        }
51
52
        return $instance;
53
    }
54
55
    /**
56
     * @public function write
57
     *
58
     * @param $module
59
     * @param $filename
60
     */
61
    public function write($module, $filename)
62
    {
63
        $this->setModule($module);
64
        $this->setFileName($filename);
65
    }
66
67
    /**
68
    *  @private function getFunctionBlock
69
    *  @param string $moduleDirname
70
     *
71
     * @return string
72
     */
73
    private function getFunctionBlock($moduleDirname)
74
    {
75
        $ret = <<<EOT
76
\n/***************Blocks***************/
77
\n/**
78
 * add selected cats
79
 * @param \$cats
80
 * @return string
81
 */
82
function {$moduleDirname}_block_addCatSelect(\$cats) {
83
    if(is_array(\$cats))
84
    {
85
        \$cat_sql = '('.current(\$cats);
86
        array_shift(\$cats);
87
        foreach(\$cats as \$cat)
88
        {
89
            \$cat_sql .= ','.\$cat;
90
        }
91
        \$cat_sql .= ')';
92
    }
93
    return \$cat_sql;
94
}\n
95
EOT;
96
97
        return $ret;
98
    }
99
100
    /**
101
    *  @private function getFunctionGetMyItemIds
102
    *  @param string $moduleDirname
103
     * @param $tableName
104
     *
105
     * @return string
106
     */
107
    private function getFunctionGetMyItemIds($moduleDirname, $tableName)
108
    {
109
        $ret = <<<EOT
110
\n/**
111
 * Get the permissions ids
112
 * @param \$permtype
113
 * @param \$dirname
114
 * @return mixed \${$tableName}
115
 */
116
function {$moduleDirname}GetMyItemIds(\$permtype, \$dirname)
117
{
118
    global \$xoopsUser;
119
    static \$permissions = array();
120
    if(is_array(\$permissions) && array_key_exists(\$permtype, \$permissions)) {
121
        return \$permissions[\$permtype];
122
    }
123
	\$moduleHandler = xoops_getHandler('module');
124
	\${$moduleDirname}Module = \$moduleHandler->getByDirname(\$dirname);
125
	\$groups = is_object(\$xoopsUser) ? \$xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
126
	\$gpermHandler = xoops_getHandler('groupperm');
127
	\${$tableName} = \$gpermHandler->getItemIds(\$permtype, \$groups, \${$moduleDirname}Module->getVar('mid'));
128
    return \${$tableName};
129
}\n
130
EOT;
131
132
        return $ret;
133
    }
134
135
    /**
136
     *  @private function getFunctionNumbersOfEntries
137
     *
138
     *  @param string $moduleDirname
139
     * @param $tableMid
140
     * @param $tableId
141
     * @param $tableName
142
     *
143
     * @return string
144
     */
145
    private function getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName)
146
    {
147
        $fields = $this->getTableFields($tableMid, $tableId);
148
        foreach (array_keys($fields) as $f) {
149
            $fieldName = $fields[$f]->getVar('field_name');
150
            if (0 == $f) {
151
                $fieldId = $fieldName; // fieldMain = fields parameters main field
152
            }
153
        }
154
        $ret = <<<EOT
155
\n/**
156
 * Get the number of {$tableName} from the sub categories of a category or sub topics of or topic
157
 * @param \$mytree
158
 * @param \${$tableName}
159
 * @param \$entries
160
 * @param \$cid
161
 * @return int
162
 */
163
function {$moduleDirname}NumbersOfEntries(\$mytree, \${$tableName}, \$entries, \$cid)
164
{
165
    \$count = 0;
166
    if(in_array(\$cid, \${$tableName})) {
167
        \$child = \$mytree->getAllChild(\$cid);
168
        foreach (array_keys(\$entries) as \$i) {
169
            if (\$entries[\$i]->getVar('{$fieldId}') == \$cid){
0 ignored issues
show
Bug introduced by
The variable $fieldId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
170
                \$count++;
171
            }
172
            foreach (array_keys(\$child) as \$j) {
173
                if (\$entries[\$i]->getVar('{$fieldId}') == \$j){
174
                    \$count++;
175
                }
176
            }
177
        }
178
    }
179
    return \$count;
180
}\n
181
EOT;
182
183
        return $ret;
184
    }
185
186
    /**
187
     *  @private function getFunctionMetaKeywords
188
     *
189
     *  @param string $moduleDirname
190
     *
191
     * @return string
192
     */
193
    private function getFunctionMetaKeywords($moduleDirname)
194
    {
195
        $ret = <<<EOT
196
\n/**
197
 * Add content as meta tag to template
198
 * @param \$content
199
 * @return void
200
 */
201
\nfunction {$moduleDirname}MetaKeywords(\$content)
202
{
203
    global \$xoopsTpl, \$xoTheme;
204
    \$myts = MyTextSanitizer::getInstance();
205
    \$content= \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
206
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
207
        \$xoTheme->addMeta( 'meta', 'keywords', strip_tags(\$content));
208
    } else {    // Compatibility for old Xoops versions
209
        \$xoopsTpl->assign('xoops_meta_keywords', strip_tags(\$content));
210
    }
211
}\n
212
EOT;
213
214
        return $ret;
215
    }
216
217
    /**
218
     *  @private function getFunctionDescription
219
     *
220
     *  @param string $moduleDirname
221
     *
222
     * @return string
223
     */
224
    private function getFunctionMetaDescription($moduleDirname)
225
    {
226
        $ret = <<<EOT
227
\n/**
228
 * Add content as meta description to template
229
 * @param \$content
230
 * @return void
231
 */
232
 \nfunction {$moduleDirname}MetaDescription(\$content)
233
{
234
    global \$xoopsTpl, \$xoTheme;
235
    \$myts = MyTextSanitizer::getInstance();
236
    \$content = \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
237
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
238
        \$xoTheme->addMeta( 'meta', 'description', strip_tags(\$content));
239
    } else {    // Compatibility for old Xoops versions
240
        \$xoopsTpl->assign('xoops_meta_description', strip_tags(\$content));
241
    }
242
}\n
243
EOT;
244
245
        return $ret;
246
    }
247
248
    /**
249
     *  @private function getRewriteUrl
250
     *
251
     *  @param string $moduleDirname
252
     *  @param string $tableName
253
     *
254
     * @return string
255
     */
256
    private function getRewriteUrl($moduleDirname, $tableName)
257
    {
258
        $ucfModuleDirname = ucfirst($moduleDirname);
259
        $ret = <<<EOT
260
\n/**
261
 * Rewrite all url
262
 *
263
 * @param string  \$module  module name
264
 * @param array   \$array   array
265
 * @param string  \$type    type
266
 * @return string \$type    string replacement for any blank case
267
 */
268
function {$moduleDirname}_RewriteUrl(\$module, \$array, \$type = 'content')
269
{
270
    \$comment = '';
271
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
272
    \${$tableName} = \${$moduleDirname}->getHandler('{$tableName}');
273
    \$lenght_id = \${$moduleDirname}->getConfig('lenght_id');
274
    \$rewrite_url = \${$moduleDirname}->getConfig('rewrite_url');
275
276
    if (\$lenght_id != 0) {
277
        \$id = \$array['content_id'];
278
        while (strlen(\$id) < \$lenght_id) {
279
            \$id = '0' . \$id;
280
        }
281
    } else {
282
        \$id = \$array['content_id'];
283
    }
284
285
    if (isset(\$array['topic_alias']) && \$array['topic_alias']) {
286
        \$topic_name = \$array['topic_alias'];
287
    } else {
288
        \$topic_name = {$moduleDirname}_Filter(xoops_getModuleOption('static_name', \$module));
289
    }
290
291
    switch (\$rewrite_url) {
292
293
        case 'none':
294
            if(\$topic_name) {
295
                 \$topic_name = 'topic=' . \$topic_name . '&amp;';
296
            }
297
            \$rewrite_base = '/modules/';
298
            \$page = 'page=' . \$array['content_alias'];
299
            return XOOPS_URL . \$rewrite_base . \$module . '/' . \$type . '.php?' . \$topic_name . 'id=' . \$id . '&amp;' . \$page . \$comment;
300
            break;
301
302
        case 'rewrite':
303
            if(\$topic_name) {
304
                \$topic_name .= '/';
305
            }
306
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
307
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
308
            \$module_name = '';
309
            if(xoops_getModuleOption('rewrite_name', \$module)) {
310
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
311
            }
312
            \$page = \$array['content_alias'];
313
            \$type .= '/';
314
            \$id .= '/';
315
            if (\$type === 'content/') {
316
                \$type = '';
317
            }
318
            if (\$type === 'comment-edit/' || \$type === 'comment-reply/' || \$type === 'comment-delete/') {
319
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
320
            }
321
322
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name  . \$id . \$page . \$rewrite_ext;
323
            break;
324
325
         case 'short':
326
            if(\$topic_name) {
327
                \$topic_name .= '/';
328
            }
329
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
330
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
331
            \$module_name = '';
332
            if(xoops_getModuleOption('rewrite_name', \$module)) {
333
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
334
            }
335
            \$page = \$array['content_alias'];
336
            \$type .= '/';
337
            if (\$type === 'content/') {
338
                \$type = '';
339
            }
340
            if (\$type === 'comment-edit/' || \$type === 'comment-reply/' || \$type === 'comment-delete/') {
341
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
342
            }
343
344
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name . \$page . \$rewrite_ext;
345
            break;
346
    }
347
    return '';
348
}
349
EOT;
350
351
        return $ret;
352
    }
353
354
    /**
355
     *  @private function getRewriteFilter
356
     *
357
     *  @param string $moduleDirname
358
     *  @param string $tableName
359
     *
360
     * @return string
361
     */
362
    private function getRewriteFilter($moduleDirname, $tableName)
363
    {
364
        $ucfModuleDirname = ucfirst($moduleDirname);
365
        $ret = <<<EOT
366
\n/**
367
 * Replace all escape, character, ... for display a correct url
368
 *
369
 * @param string \$url      string to transform
370
 * @param string \$type     string replacement for any blank case
371
 * @param string \$module
372
 * @return string \$url
373
 */
374
function {$moduleDirname}_Filter(\$url, \$type = '', \$module = '{$moduleDirname}') {
375
376
    // Get regular expression from module setting. default setting is : `[^a-z0-9]`i
377
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
378
    \${$tableName} = \${$moduleDirname}->getHandler('{$tableName}');
379
    \$regular_expression = \${$moduleDirname}->getConfig('regular_expression');
380
381
    \$url = strip_tags(\$url);
382
    \$url = preg_replace("`\[.*\]`U", "", \$url);
383
    \$url = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', \$url);
384
    \$url = htmlentities(\$url, ENT_COMPAT, 'utf-8');
385
    \$url = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i", "\\1", \$url);
386
    \$url = preg_replace(array(\$regular_expression, "`[-]+`"), "-", \$url);
387
    \$url = (\$url == '') ? \$type : strtolower(trim(\$url, '-'));
388
    return \$url;
389
}
390
EOT;
391
392
        return $ret;
393
    }
394
395
    /**
396
     *  @public function render
397
     *
398
     *  @param null
399
     *
400
     * @return bool|string
401
     */
402
    public function render()
403
    {
404
        $module = $this->getModule();
405
        $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
406
        $tableId = null;
407
        $tableMid = null;
408
        $tableName = null;
409
        $tableBlocks = null;
410
        $tablePermissions = null;
411
        $tableCategory = null;
412
        foreach (array_keys($tables) as $i) {
413
            $tableId = $tables[$i]->getVar('table_id');
414
            $tableMid = $tables[$i]->getVar('table_mid');
415
            $tableName = $tables[$i]->getVar('table_name');
416
            $tableBlocks[] = $tables[$i]->getVar('table_blocks');
417
            $tablePermissions[] = $tables[$i]->getVar('table_permissions');
418
            $tableCategory[] = $tables[$i]->getVar('table_category');
419
        }
420
        $filename = $this->getFileName();
421
        $moduleDirname = $module->getVar('mod_dirname');
422
        $content = $this->getHeaderFilesComments($module, $filename);
423
        if (in_array(1, $tableBlocks)) {
424
            $content .= $this->getFunctionBlock($moduleDirname);
425
        }
426
        if (in_array(1, $tablePermissions)) {
427
            $content .= $this->getFunctionGetMyItemIds($moduleDirname, $tableName);
428
        }
429
        if (in_array(1, $tableCategory)) {
430
            $content .= $this->getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName);
431
        }
432
        $content .= $this->getFunctionMetaKeywords($moduleDirname);
433
        $content .= $this->getFunctionMetaDescription($moduleDirname);
434
        $content .= $this->getRewriteUrl($moduleDirname, $tableName);
435
        $content .= $this->getRewriteFilter($moduleDirname, $tableName);
436
437
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
438
439
        return $this->renderFile();
440
    }
441
}
442