Completed
Push — master ( a8ea98...cecb98 )
by Gino
06:23 queued 03:11
created

IncludeFunctions::render()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 8.439
cc 5
eloc 31
nc 16
nop 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       The XOOPS Project http://sourceforge.net/projects/xoops/
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
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreatePhpCode::getInstance() of type object<TDMCreatePhpCode> is incompatible with the declared type string of property $phpcode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /*
44
    *  @static function &getInstance
45
    *  @param null
46
    */
47
    /**
48
     * @return IncludeFunctions
49
     */
50
    public static function &getInstance()
51
    {
52
        static $instance = false;
53
        if (!$instance) {
54
            $instance = new self();
55
        }
56
57
        return $instance;
58
    }
59
60
    /**
61
     * @public function write
62
     *
63
     * @param $module
64
     * @param $filename
65
     */
66
    public function write($module, $filename)
67
    {
68
        $this->setModule($module);
69
        $this->setFileName($filename);
70
    }
71
72
    /*
73
    *  @private function getFunctionBlock
74
    *  @param string $moduleDirname
75
    */
76
    /**
77
     * @param $moduleDirname
78
     *
79
     * @return string
80
     */
81
    private function getFunctionBlock($moduleDirname)
82
    {
83
        $ret = <<<EOT
84
\n/***************Blocks***************/
85
function {$moduleDirname}_block_addCatSelect(\$cats) {
86
    if(is_array(\$cats))
87
    {
88
        \$cat_sql = '('.current(\$cats);
89
        array_shift(\$cats);
90
        foreach(\$cats as \$cat)
91
        {
92
            \$cat_sql .= ','.\$cat;
93
        }
94
        \$cat_sql .= ')';
95
    }
96
    return \$cat_sql;
97
}\n
98
EOT;
99
100
        return $ret;
101
    }
102
103
    /*
104
    *  @private function getFunctionGetMyItemIds
105
    *  @param string $moduleDirname
106
    */
107
    /**
108
     * @param $moduleDirname
109
     * @param $tableName
110
     *
111
     * @return string
112
     */
113
    private function getFunctionGetMyItemIds($moduleDirname, $tableName)
114
    {
115
        $ret = <<<EOT
116
\n/**
117
 *  Get the permissions ids
118
 */
119
function {$moduleDirname}GetMyItemIds(\$permtype, \$dirname)
120
{
121
    global \$xoopsUser;
122
    static \$permissions = array();
123
    if(is_array(\$permissions) && array_key_exists(\$permtype, \$permissions)) {
124
        return \$permissions[\$permtype];
125
    }
126
	\$moduleHandler =& xoops_gethandler('module');
127
	\${$moduleDirname}Module =& \$moduleHandler->getByDirname(\$dirname);
128
	\$groups = is_object(\$xoopsUser) ? \$xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
129
	\$gpermHandler =& xoops_gethandler('groupperm');
130
	\${$tableName} = \$gpermHandler->getItemIds(\$permtype, \$groups, \${$moduleDirname}Module->getVar('mid'));
131
    return \${$tableName};
132
}\n
133
EOT;
134
135
        return $ret;
136
    }
137
138
    /*
139
    *  @private function getFunctionNumbersOfEntries
140
    *  @param string $moduleDirname
141
    */
142
    /**
143
     * @param $moduleDirname
144
     * @param $tableMid
145
     * @param $tableMid
146
     * @param $tableName
147
     *
148
     * @return string
149
     */
150
    private function getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName)
151
    {
152
        $fields = $this->getTableFields($tableMid, $tableId);
153
        foreach (array_keys($fields) as $f) {
154
            $fieldName = $fields[$f]->getVar('field_name');
155
            if (0 == $f) {
156
                $fieldId = $fieldName; // fieldMain = fields parameters main field
157
            }
158
        }
159
        $ret = <<<EOT
160
\n/**
161
 *  Get the number of {$tableName} from the sub categories of a category or sub topics of or topic
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
    *  @param string $moduleDirname
189
    */
190
    /**
191
     * @param $moduleDirname
192
     *
193
     * @return string
194
     */
195
    private function getFunctionMetaKeywords($moduleDirname)
196
    {
197
        $ret = <<<EOT
198
\nfunction {$moduleDirname}MetaKeywords(\$content)
199
{
200
    global \$xoopsTpl, \$xoTheme;
201
    \$myts =& MyTextSanitizer::getInstance();
202
    \$content= \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
203
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
204
        \$xoTheme->addMeta( 'meta', 'keywords', strip_tags(\$content));
205
    } else {    // Compatibility for old Xoops versions
206
        \$xoopsTpl->assign('xoops_meta_keywords', strip_tags(\$content));
207
    }
208
}\n
209
EOT;
210
211
        return $ret;
212
    }
213
214
    /*
215
    *  @private function getFunctionDescription
216
    *  @param string $moduleDirname
217
    */
218
    /**
219
     * @param $moduleDirname
220
     *
221
     * @return string
222
     */
223
    private function getFunctionMetaDescription($moduleDirname)
224
    {
225
        $ret = <<<EOT
226
\nfunction {$moduleDirname}MetaDescription(\$content)
227
{
228
    global \$xoopsTpl, \$xoTheme;
229
    \$myts =& MyTextSanitizer::getInstance();
230
    \$content = \$myts->undoHtmlSpecialChars(\$myts->displayTarea(\$content));
231
    if(isset(\$xoTheme) && is_object(\$xoTheme)) {
232
        \$xoTheme->addMeta( 'meta', 'description', strip_tags(\$content));
233
    } else {    // Compatibility for old Xoops versions
234
        \$xoopsTpl->assign('xoops_meta_description', strip_tags(\$content));
235
    }
236
}\n
237
EOT;
238
239
        return $ret;
240
    }
241
242
    /*
243
    *  @private function getRewriteUrl
244
    *  @param string $moduleDirname
245
    *  @param string $tableName
246
    */
247
    /**
248
     * @param $moduleDirname
249
     * @param $tableName
250
     *
251
     * @return string
252
     */
253
    private function getRewriteUrl($moduleDirname, $tableName)
254
    {
255
        $ucfModuleDirname = ucfirst($moduleDirname);
256
        $ret = <<<EOT
257
\n/**
258
 * Rewrite all url
259
 *
260
 * @String  \$module  module name
261
 * @String  \$array   array
262
 * @return  \$type    string replacement for any blank case
263
 */
264
function {$moduleDirname}_RewriteUrl(\$module, \$array, \$type = 'content')
265
{
266
    \$comment = '';
267
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
268
    \${$tableName} = \${$moduleDirname}->getHandler('{$tableName}');
269
    \$lenght_id = \${$moduleDirname}->getConfig('lenght_id');
270
    \$rewrite_url = \${$moduleDirname}->getConfig('rewrite_url');
271
272
    if (\$lenght_id != 0) {
273
        \$id = \$array['content_id'];
274
        while (strlen(\$id) < \$lenght_id)
275
            \$id = "0" . \$id;
276
    } else {
277
        \$id = \$array['content_id'];
278
    }
279
280
    if (isset(\$array['topic_alias']) && \$array['topic_alias']) {
281
        \$topic_name = \$array['topic_alias'];
282
    } else {
283
        \$topic_name = {$moduleDirname}_Filter(xoops_getModuleOption('static_name', \$module));
284
    }
285
286
    switch (\$rewrite_url) {
287
288
        case 'none':
289
            if(\$topic_name) {
290
                 \$topic_name = 'topic=' . \$topic_name . '&amp;';
291
            }
292
            \$rewrite_base = '/modules/';
293
            \$page = 'page=' . \$array['content_alias'];
294
            return XOOPS_URL . \$rewrite_base . \$module . '/' . \$type . '.php?' . \$topic_name . 'id=' . \$id . '&amp;' . \$page . \$comment;
295
            break;
296
297
        case 'rewrite':
298
            if(\$topic_name) {
299
                \$topic_name = \$topic_name . '/';
300
            }
301
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
302
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
303
            \$module_name = '';
304
            if(xoops_getModuleOption('rewrite_name', \$module)) {
305
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
306
            }
307
            \$page = \$array['content_alias'];
308
            \$type = \$type . '/';
309
            \$id = \$id . '/';
310
            if (\$type == 'content/') \$type = '';
311
312
            if (\$type == 'comment-edit/' || \$type == 'comment-reply/' || \$type == 'comment-delete/') {
313
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
314
            }
315
316
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name  . \$id . \$page . \$rewrite_ext;
317
            break;
318
319
         case 'short':
320
            if(\$topic_name) {
321
                \$topic_name = \$topic_name . '/';
322
            }
323
            \$rewrite_base = xoops_getModuleOption('rewrite_mode', \$module);
324
            \$rewrite_ext = xoops_getModuleOption('rewrite_ext', \$module);
325
            \$module_name = '';
326
            if(xoops_getModuleOption('rewrite_name', \$module)) {
327
                \$module_name = xoops_getModuleOption('rewrite_name', \$module) . '/';
328
            }
329
            \$page = \$array['content_alias'];
330
            \$type = \$type . '/';
331
            if (\$type == 'content/') \$type = '';
332
333
            if (\$type == 'comment-edit/' || \$type == 'comment-reply/' || \$type == 'comment-delete/') {
334
                return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$id . '/';
335
            }
336
337
            return XOOPS_URL . \$rewrite_base . \$module_name . \$type . \$topic_name . \$page . \$rewrite_ext;
338
            break;
339
    }
340
}
341
EOT;
342
343
        return $ret;
344
    }
345
346
    /*
347
    *  @private function getRewriteFilter
348
    *  @param string $moduleDirname
349
    *  @param string $tableName
350
    */
351
    /**
352
     * @param $moduleDirname
353
     * @param $tableName
354
     *
355
     * @return string
356
     */
357
    private function getRewriteFilter($moduleDirname, $tableName)
358
    {
359
        $ucfModuleDirname = ucfirst($moduleDirname);
360
        $ret = <<<EOT
361
\n/**
362
 * Replace all escape, character, ... for display a correct url
363
 *
364
 * @String  \$url    string to transform
365
 * @String  \$type   string replacement for any blank case
366
 * @return  \$url
367
 */
368
function {$moduleDirname}_Filter(\$url, \$type = '', \$module = '{$moduleDirname}') {
369
370
    // Get regular expression from module setting. default setting is : `[^a-z0-9]`i
371
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
372
    \${$tableName} = \${$moduleDirname}->getHandler('{$tableName}');
373
    \$regular_expression = \${$moduleDirname}->getConfig('regular_expression');
374
375
    \$url = strip_tags(\$url);
376
    \$url = preg_replace("`\[.*\]`U", "", \$url);
377
    \$url = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', \$url);
378
    \$url = htmlentities(\$url, ENT_COMPAT, 'utf-8');
379
    \$url = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i", "\\1", \$url);
380
    \$url = preg_replace(array(\$regular_expression, "`[-]+`"), "-", \$url);
381
    \$url = (\$url == "") ? \$type : strtolower(trim(\$url, '-'));
382
    return \$url;
383
}
384
EOT;
385
386
        return $ret;
387
    }
388
389
    /*
390
    *  @public function render
391
    *  @param null
392
    */
393
    /**
394
     * @return bool|string
395
     */
396
    public function render()
397
    {
398
        $module = $this->getModule();
399
        $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
400
        $tableId = null;
401
        $tableMid = null;
402
        $tableName = null;
403
        $tableBlocks = null;
404
        $tablePermissions = null;
405
        $tableCategory = null;
406
        foreach (array_keys($tables) as $i) {
407
            $tableId = $tables[$i]->getVar('table_id');
408
            $tableMid = $tables[$i]->getVar('table_mid');
409
            $tableName = $tables[$i]->getVar('table_name');
410
            $tableBlocks[] = $tables[$i]->getVar('table_blocks');
411
            $tablePermissions[] = $tables[$i]->getVar('table_permissions');
412
            $tableCategory[] = $tables[$i]->getVar('table_category');
413
        }
414
        $filename = $this->getFileName();
415
        $moduleDirname = $module->getVar('mod_dirname');
416
        $content = $this->getHeaderFilesComments($module, $filename);
417
        if (in_array(1, $tableBlocks)) {
418
            $content .= $this->getFunctionBlock($moduleDirname);
419
        }
420
        if (in_array(1, $tablePermissions)) {
421
            $content .= $this->getFunctionGetMyItemIds($moduleDirname, $tableName);
422
        }
423
        if (in_array(1, $tableCategory)) {
424
            $content .= $this->getFunctionNumbersOfEntries($moduleDirname, $tableMid, $tableId, $tableName);
425
        }
426
        $content .= $this->getFunctionMetaKeywords($moduleDirname);
427
        $content .= $this->getFunctionMetaDescription($moduleDirname);
428
        $content .= $this->getRewriteUrl($moduleDirname, $tableName);
429
        $content .= $this->getRewriteFilter($moduleDirname, $tableName);
430
        //
431
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
432
433
        return $this->renderFile();
434
    }
435
}
436