Completed
Push — master ( bb5c69...2e7c79 )
by Gino
06:03 queued 02:48
created

TDMCreatePhpCode::getPhpCodeIncludeDir()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
rs 8.7624
cc 5
eloc 14
nc 8
nop 6
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: TDMCreatePhpCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TDMCreatePhpCode.
27
 */
28
class TDMCreatePhpCode
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
    }
40
41
    /*
42
    *  @static function &getInstance
43
    *  @param null
44
    */
45
    /**
46
     * @return TDMCreatePhpCode
47
     */
48
    public static function &getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /*
59
    *  @public function getPhpCodeCommentLine
60
    *  @param $comment
61
    *  @param $var
62
    *  @return string
63
    */
64
    public function getPhpCodeCommentLine($comment = null, $var = null, $t = '')
65
    {
66
        $value = !empty($var) ? ' '.$var : '';
67
        $ret = "{$t}// {$comment}{$value}\n";
68
69
        return $ret;
70
    }
71
72
    /*
73
     * @public function getPhpCodeCommentMultiLine
74
     * @param $multiLine     
75
     *
76
     * @return string
77
     */
78
    public function getPhpCodeCommentMultiLine($multiLine = array(), $t = '')
79
    {
80
        $values = !empty($multiLine) ? $multiLine : array();
81
        $ret = "\n{$t}/**\n";
82
        foreach ($values as $string => $value) {
83
            if ($string === '' && $value === '') {
84
                $ret .= "{$t} *\n";
85
            } else {
86
                $ret .= "{$t} * {$string} {$value}\n";
87
            }
88
        }
89
        $ret .= "{$t} */\n";
90
91
        return $ret;
92
    }
93
94
    /*
95
    *  @public function getPhpCodeDefine
96
    *  @param $left
97
    *  @param $right
98
    *
99
    *  @return string
100
    */
101
    public function getPhpCodeDefine($left, $right)
102
    {
103
        return "define('{$left}', {$right});\n";
104
    }
105
106
    /*
107
    *  @public function getPhpCodeDefine
108
    *  @param $left
109
    *  @param $right
110
    *
111
    *  @return string
112
    */
113
    public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access')
114
    {
115
        return "defined('{$left}') || die('{$right}');\n";
116
    }
117
118
    /*
119
    *  @public function getPhpCodeGlobals
120
    *  @param $var
121
    *  @param $value
122
    *
123
    *  @return string
124
    */
125
    public function getPhpCodeGlobals($var, $value = '')
126
    {
127
        if ($value != '') {
128
            $ret = "\$GLOBALS['{$var}'] = \${$value};\n";
129
        } else {
130
            $ret = "\$GLOBALS['{$var}']";
131
        }
132
133
        return $ret;
134
    }
135
136
    /*
137
    *  @public function getPhpCodeGlobalsVariables    
138
    *  @param $var
139
    *  @param $type
140
    *
141
    *  @return string
142
    */
143
    public function getPhpCodeGlobalsVariables($var = null, $type = 'REQUEST')
144
    {
145
        $type = strtoupper($type);
146
        switch ($type) {
147
            case 'GET':
148
                $ret = "\$_GET['{$var}']";
149
                break;
150
            case 'POST':
151
                $ret = "\$_POST['{$var}']";
152
                break;
153
            case 'FILES':
154
                $ret = "\$_FILES['{$var}']";
155
                break;
156
            case 'COOKIE':
157
                $ret = "\$_COOKIE['{$var}']";
158
                break;
159
            case 'ENV':
160
                $ret = "\$_ENV['{$var}']";
161
                break;
162
            case 'SERVER':
163
                $ret = "\$_SERVER['{$var}']";
164
                break;
165
            default:
166
                $ret = "\$_REQUEST['{$var}']";
167
                break;
168
        }
169
170
        return $ret;
171
    }
172
173
    /*
174
     * @public function getPhpCodeRemoveCarriageReturn
175
     * @param $string     
176
     *
177
     * @return string
178
     */
179
    public function getPhpCodeRemoveCarriageReturn($string, $n = "\n", $t = "\r")
180
    {
181
        return str_replace(array("{$n}", "{$t}"), '', $string);
182
    }
183
184
    /*
185
    *  @public function getPhpCodeFileExists
186
    *  @param $filename
187
    *
188
    *  @return string
189
    */
190
    public function getPhpCodeFileExists($filename)
191
    {
192
        return "file_exists({$filename})";
193
    }
194
195
    /*
196
    *  @public function getPhpCodeIncludeDir
197
    *  @param $directory
198
    *  @param $filename
199
    *  @param $once
200
    *  @param $isPath
201
    *
202
    *  @return string
203
    */
204
    public function getPhpCodeIncludeDir($directory = null, $filename = null, $once = false, $isPath = false, $type = 'include', $t = '')
205
    {
206
        if ($type === '') {
207
            $type = 'include';
208
        }
209
        if ($once == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
210
            if (!$isPath) {
211
                $ret = "{$t}{$type} {$directory} .'/{$filename}.php';\n";
212
            } else {
213
                $ret = "{$t}{$type} {$directory};\n";
214
            }
215
        } else {
216
            if (!$isPath) {
217
                $ret = "{$t}{$type}_once {$directory} .'/{$filename}.php';\n";
218
            } else {
219
                $ret = "{$t}{$type}_once {$directory};\n";
220
            }
221
        }
222
223
        return $ret;
224
    }
225
226
    /*
227
    *  @public function getPhpCodeTernaryOperator
228
    *  @param $return
229
    *  @param $condition
230
    *  @param $one
231
    *  @param $two
232
    *  @param $t - Indentation 
233
    *
234
    *  @return string
235
    */
236
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '')
237
    {
238
        return "{$t}\${$return} = {$condition} ? {$one} : {$two};\n";
239
    }
240
241
    /*
242
    *  @public function getPhpCodeClass
243
    *  @param $name    
244
    *  @param $content
245
    *  @param $extends
246
    *  @param $type
247
    *
248
    *  @return string
249
    */
250
    public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null)
251
    {
252
        $typ = ($type != null) ? "{$type} " : '';
253
        $ext = ($extends != null) ? " extends {$extends}" : '';
254
        $ret = "{$typ}class {$name}{$ext}\n";
255
        $ret .= '{';
256
        $ret .= "{$content}";
257
        $ret .= "}\n";
258
259
        return $ret;
260
    }
261
262
    /*
263
    *  @public function getPhpCodeClass
264
    *  @param $type    
265
    *  @param $name
266
    *  @param $assign
267
    *  @param $t - Indentation 
268
    *
269
    *  @return string
270
    */
271
    public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '')
272
    {
273
        return "{$t}{$type} \${$name} = {$assign};\n";
274
    }
275
276
    /*
277
    *  @public function getPhpCodeFunction
278
    *  @param $name
279
    *  @param $params
280
    *  @param $content
281
    *  @param $method
282
    *  @param $t - Indentation 
283
    *
284
    *  @return string
285
    */
286
    public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '')
287
    {
288
        $inClass = ($method != null) ? $method : '';
289
        $ref = ($isRef != false) ? '&' : '';
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
290
        $ret = "{$t}{$inClass}function {$ref}{$name}({$params})\n";
291
        $ret .= "{$t}{\n";
292
        $ret .= "{$content}";
293
        $ret .= "{$t}}\n";
294
295
        return $ret;
296
    }
297
298
    /*
299
     * @public function getPhpCodeConditions
300
     * @param string $condition
301
     * @param string $operator
302
     * @param string $type
303
     * @param string $contentIf
304
     * @param mixed  $contentElse
305
     * @param $t - Indentation 
306
     *
307
     * @return string
308
     */
309
    public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '')
310
    {
311
        if (false === $contentElse) {
312
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
313
            $ret .= "{$contentIf}";
314
            $ret .= "{$t}}\n";
315
        } else {
316
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
317
            $ret .= "{$contentIf}";
318
            $ret .= "{$t}} else {\n";
319
            $ret .= "{$contentElse}";
320
            $ret .= "{$t}}\n";
321
        }
322
323
        return $ret;
324
    }
325
326
    /*
327
     * @public function getPhpCodeForeach
328
     * @param string $array
329
     * @param string $arrayKey     
330
     * @param string $key
331
     * @param string $value     
332
     * @param string $content
333
     *
334
     * @return string
335
     */
336
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '')
337
    {
338
        $vars = '';
339
        if ((false === $arrayKey) && (false === $key)) {
340
            $vars = "\${$array} as \${$value}";
341
        } elseif ((false === $arrayKey) && (false !== $key)) {
342
            $vars = "\${$array} as \${$key} => \${$value}";
343
        } elseif ((false !== $arrayKey) && (false === $key)) {
344
            $vars = "array_keys(\${$array}) as \${$value}";
345
        }
346
347
        $ret = "{$t}foreach({$vars}) {\n";
348
        $ret .= "{$t}{$content}";
349
        $ret .= "{$t}}\n";
350
351
        return $ret;
352
    }
353
354
    /*
355
     * @public function getPhpCodeFor
356
     * @param $var
357
     * @param $content
358
     * @param $value
359
     * @param $initVal
360
     * @param $operator
361
     *
362
     * @return string
363
     */
364
    public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '')
365
    {
366
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
367
        $ret .= "{$t}{$content}";
368
        $ret .= "{$t}}\n";
369
370
        return $ret;
371
    }
372
373
    /*
374
     * @public function getPhpCodeWhile
375
     * @param $var
376
     * @param $content
377
     * @param $value
378
     * @param $operator
379
     *  @param $t
380
     *
381
     * @return string
382
     */
383
    public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '')
384
    {
385
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
386
        $ret .= "{$t}{$content}";
387
        $ret .= "{$t}}\n";
388
389
        return $ret;
390
    }
391
392
    /**
393
     *  @public function getPhpCodeSwitch
394
     *
395
     *  @param $op
396
     *  @param $content
397
     *  @param $t
398
     *
399
     *  @return string
400
     */
401
    public function getPhpCodeSwitch($op = null, $content = null, $t = '')
402
    {
403
        $ret = "{$t}switch(\${$op}) {\n";
404
        $ret .= "{$t}{$content}";
405
        $ret .= "{$t}}\n";
406
407
        return $ret;
408
    }
409
410
    /**
411
     *  @public function getPhpCodeCaseSwitch     
412
     *
413
     *  @param $cases
414
     *  @param $defaultAfterCase
415
     *  @param $default
416
     *  @param $t
417
     *
418
     *  @return string
419
     */
420
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = '')
421
    {
422
        $ret = '';
423
        $def = "{$t}default:\n";
424
        foreach ($cases as $case => $value) {
425
            $case = is_string($case) ? "'{$case}'" : $case;
426
            if (!empty($case)) {
427
                $ret .= "{$t}case {$case}:\n";
428
                if ($defaultAfterCase != false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
429
                    $ret .= $def;
430
                }
431
                if (is_array($value)) {
432
                    foreach ($value as $content) {
433
                        $ret .= "{$t}{$t}{$content}\n";
434
                    }
435
                }
436
                $ret .= "{$t}break;\n";
437
            }
438
            $defaultAfterCase = false;
439
        }
440
        if ($default !== false) {
441
            $ret .= $def;
442
            $ret .= "{$t}{$default}\n";
443
            $ret .= "{$t}break;\n";
444
        }
445
446
        return $ret;
447
    }
448
449
    /*
450
    *  @public function getPhpCodeIsset
451
    *  @param $var
452
    *  @return string
453
    */
454
    public function getPhpCodeIsset($var)
455
    {
456
        return "isset(\${$var})";
457
    }
458
459
    /*
460
    *  @public function getPhpCodeUnset
461
    *  @param $var
462
    *  @return string
463
    */
464
    public function getPhpCodeUnset($var = '', $t = '')
465
    {
466
        return "{$t}unset(\${$var});\n";
467
    }
468
469
    /*
470
    *  @public function getPhpCodeIsDir
471
    *  @param $var
472
    *  @return string
473
    */
474
    public function getPhpCodeIsDir($var)
475
    {
476
        return "is_dir({$var})";
477
    }
478
479
    /*
480
    *  @public function getPhpCodeImplode
481
    *  @param $left
482
    *  @param $right
483
    *  @return string
484
    */
485
    public function getPhpCodeImplode($left, $right)
486
    {
487
        return "implode('{$left}', {$right})";
488
    }
489
490
    /*
491
    *  @public function getPhpCodeExplode
492
    *  @param $left
493
    *  @param $right
494
    *  @return string
495
    */
496
    public function getPhpCodeExplode($left, $right)
497
    {
498
        return "explode('{$left}', {$right})";
499
    }
500
501
    /*
502
    *  @public function getPhpCodeChmod
503
    *  @param $var
504
    *  @param $perm
505
    *  @return string
506
    */
507
    public function getPhpCodeChmod($var, $perm = '0777', $t = '')
508
    {
509
        return "{$t}chmod(\${$var}, {$perm});\n";
510
    }
511
512
    /*
513
    *  @public function getPhpCodeMkdir
514
    *  @param $var
515
    *  @param $perm
516
    *  @return string
517
    */
518
    public function getPhpCodeMkdir($var, $perm = '0777', $t = '')
519
    {
520
        return "{$t}mkdir(\${$var}, {$perm});\n";
521
    }
522
523
    /*
524
    *  @public function getPhpCodeCopy
525
    *  @param $file
526
    *  @param $newfile
527
    *  @return string
528
    */
529
    public function getPhpCodeCopy($file, $newfile = '', $t = '')
530
    {
531
        return "{$t}copy({$file}, {$newfile});\n";
532
    }
533
534
    /*
535
    *  @public function getPhpCodeArray
536
    *  @param $var
537
    *  @param $array
538
    *  @param $isParam
539
    *
540
    *  @return string
541
    */
542
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
543
    {
544
        $retArray = array();
545
        if (is_array($array) && !empty($array)) {
546
            foreach ($array as $k => $v) {
547
                if (is_numeric($k)) {
548
                    $retArray[] = $v;
549
                } else {
550
                    $retArray[] = "{$k} => {$v}";
551
                }
552
            }
553
            $arrayContent = implode(', ', $retArray);
554
        } else {
555
            $arrayContent = '';
556
        }
557
        unset($retArray);
558
559
        if (!$isParam) {
560
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
561
        } else {
562
            $ret = "array({$array})";
563
        }
564
565
        return $ret;
566
    }
567
568
    /*
569
    *  @public function getPhpCodeArrayType
570
    *  @param $var
571
    *  @param $type
572
    *  @param $left
573
    *  @param $right
574
    *  @param $isParam
575
    *
576
    *  @return string
577
    */
578
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
579
    {
580
        $vars = ($right != null) ? "\${$left}, {$right}" : "\${$left}";
581
        if (!$isParam) {
582
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
583
        } else {
584
            $ret = "array_{$type}({$vars})";
585
        }
586
587
        return $ret;
588
    }
589
590
    /*
591
    *  @public function getPhpCodeSprintf
592
    *  @param $left
593
    *  @param $right
594
    *  @return string
595
    */
596
    public function getPhpCodeSprintf($left, $right)
597
    {
598
        return "sprintf({$left}, {$right})";
599
    }
600
601
    /*
602
    *  @public function getPhpCodeEmpty
603
    *  @param $var
604
    *  @return string
605
    */
606
    public function getPhpCodeEmpty($var)
607
    {
608
        return "empty({$var})";
609
    }
610
611
    /*
612
    *  @public function getPhpCodeHeader
613
    *  @param $var
614
    *  @return string
615
    */
616
    public function getPhpCodeHeader($var)
617
    {
618
        return "header({$var})";
619
    }
620
621
    /*
622
    *  @public function getPhpCodeRawurlencode
623
    *  @param $var
624
    *  @return string
625
    */
626
    public function getPhpCodeRawurlencode($var)
627
    {
628
        return "rawurlencode({$var})";
629
    }
630
631
    /*
632
    *  @public function getPhpCodePregFunzions
633
    *  @param $var
634
    *  @param $exp
635
    *  @param $str
636
    *  @param $val
637
    *  @param $type
638
    *  @param $isParam
639
    *
640
    *  @return string
641
    */
642
    public function getPhpCodePregFunzions($var, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t")
643
    {
644
        $pregFunz = "preg_{$type}('";
645
        if (!$isParam) {
646
            $ret = "{$t}\${$var} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
647
        } else {
648
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
649
        }
650
651
        return $ret;
652
    }
653
654
    /*
655
    *  @public function getPhpCodeStrType
656
    *  @param $left
657
    *  @param $var
658
    *  @param $str
659
    *  @param $value
660
    *  @param $type
661
    *  @param $isParam
662
    *
663
    *  @return string
664
    */
665
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
666
    {
667
        $strType = "str_{$type}('";
668
        if (!$isParam) {
669
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
670
        } else {
671
            $ret = "{$strType}{$var}', '{$str}', {$value})";
672
        }
673
674
        return $ret;
675
    }
676
677
    /*
678
    *  @public function getPhpCodeStripTags
679
    *  @param $left
680
    *  @param $value
681
    *  @param $isParam
682
    *
683
    *  @return string
684
    */
685
    public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = '')
686
    {
687
        if (!$isParam) {
688
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
689
        } else {
690
            $ret = "strip_tags({$value})";
691
        }
692
693
        return $ret;
694
    }
695
696
    /*
697
    *  @public function getPhpCodeHtmlentities
698
    *  @param $entitiesVar
699
    *  @param $entitiesQuote
700
    *  @return string
701
    */
702
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
703
    {
704
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
705
        $entities = "htmlentities({$entitiesVar})";
706
707
        return $entities;
708
    }
709
710
    /*
711
    *  @public function getPhpCodeHtmlspecialchars
712
    *  @param $specialVar
713
    *  @param $specialQuote
714
    *  @return string
715
    */
716
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
717
    {
718
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
719
        $specialchars = "htmlspecialchars({$specialVar})";
720
721
        return $specialchars;
722
    }
723
}
724