Completed
Push — master ( 2ee26c...778493 )
by Gino
06:21 queued 02:34
created

TDMCreatePhpCode::getPhpCodeCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
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 ($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...
207
            if (!$isPath) {
208
                $ret = "{$t}{$type} {$directory} .'/{$filename}.php';\n";
209
            } else {
210
                $ret = "{$t}{$type} {$directory};\n";
211
            }
212
        } else {
213
            if (!$isPath) {
214
                $ret = "{$t}{$type}_once {$directory} .'/{$filename}.php';\n";
215
            } else {
216
                $ret = "{$t}{$type}_once {$directory};\n";
217
            }
218
        }
219
220
        return $ret;
221
    }
222
223
    /*
224
    *  @public function getPhpCodeTernaryOperator
225
    *  @param $return
226
    *  @param $condition
227
    *  @param $one
228
    *  @param $two
229
    *  @param $t - Indentation 
230
    *
231
    *  @return string
232
    */
233
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '')
234
    {
235
        return "{$t}\${$return} = {$condition} ? {$one} : {$two};\n";
236
    }
237
238
    /*
239
    *  @public function getPhpCodeClass
240
    *  @param $name    
241
    *  @param $content
242
    *  @param $extends
243
    *  @param $type
244
    *
245
    *  @return string
246
    */
247
    public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null)
248
    {
249
        $typ = ($type != null) ? "{$type} " : '';
250
        $ext = ($extends != null) ? " extends {$extends}" : '';
251
        $ret = "{$typ}class {$name}{$ext}\n";
252
        $ret .= '{';
253
        $ret .= "{$content}";
254
        $ret .= "}\n";
255
256
        return $ret;
257
    }
258
259
    /*
260
    *  @public function getPhpCodeClass
261
    *  @param $type    
262
    *  @param $name
263
    *  @param $assign
264
    *  @param $t - Indentation 
265
    *
266
    *  @return string
267
    */
268
    public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '')
269
    {
270
        return "{$t}{$type} \${$name} = {$assign};\n";
271
    }
272
273
    /*
274
    *  @public function getPhpCodeFunction
275
    *  @param $name
276
    *  @param $params
277
    *  @param $content
278
    *  @param $method
279
    *  @param $t - Indentation 
280
    *
281
    *  @return string
282
    */
283
    public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '')
284
    {
285
        $inClass = ($method != null) ? $method : '';
286
        $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...
287
        $ret = "{$t}{$inClass}function {$ref}{$name}({$params})\n";
288
        $ret .= "{$t}{\n";
289
        $ret .= "{$content}";
290
        $ret .= "{$t}}\n";
291
292
        return $ret;
293
    }
294
295
    /*
296
     * @public function getPhpCodeConditions
297
     * @param string $condition
298
     * @param string $operator
299
     * @param string $type
300
     * @param string $contentIf
301
     * @param mixed  $contentElse
302
     * @param $t - Indentation 
303
     *
304
     * @return string
305
     */
306
    public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '')
307
    {
308
        if (false === $contentElse) {
309
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
310
            $ret .= "{$contentIf}";
311
            $ret .= "{$t}}\n";
312
        } else {
313
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
314
            $ret .= "{$contentIf}";
315
            $ret .= "{$t}} else {\n";
316
            $ret .= "{$contentElse}";
317
            $ret .= "{$t}}\n";
318
        }
319
320
        return $ret;
321
    }
322
323
    /*
324
     * @public function getPhpCodeForeach
325
     * @param string $array
326
     * @param string $arrayKey     
327
     * @param string $key
328
     * @param string $value     
329
     * @param string $content
330
     *
331
     * @return string
332
     */
333
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '')
334
    {
335
        $vars = '';
336
        if ((false === $arrayKey) && (false === $key)) {
337
            $vars = "\${$array} as \${$value}";
338
        } elseif ((false === $arrayKey) && (false !== $key)) {
339
            $vars = "\${$array} as \${$key} => \${$value}";
340
        } elseif ((false !== $arrayKey) && (false === $key)) {
341
            $vars = "array_keys(\${$array}) as \${$value}";
342
        }
343
344
        $ret = "{$t}foreach({$vars}) {\n";
345
        $ret .= "{$t}{$content}";
346
        $ret .= "{$t}}\n";
347
348
        return $ret;
349
    }
350
351
    /*
352
     * @public function getPhpCodeFor
353
     * @param $var
354
     * @param $content
355
     * @param $value
356
     * @param $initVal
357
     * @param $operator
358
     *
359
     * @return string
360
     */
361
    public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '')
362
    {
363
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
364
        $ret .= "{$t}{$content}";
365
        $ret .= "{$t}}\n";
366
367
        return $ret;
368
    }
369
370
    /*
371
     * @public function getPhpCodeWhile
372
     * @param $var
373
     * @param $content
374
     * @param $value
375
     * @param $operator
376
     *  @param $t
377
     *
378
     * @return string
379
     */
380
    public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '')
381
    {
382
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
383
        $ret .= "{$t}{$content}";
384
        $ret .= "{$t}}\n";
385
386
        return $ret;
387
    }
388
389
    /**
390
     *  @public function getPhpCodeSwitch
391
     *
392
     *  @param $op
393
     *  @param $content
394
     *  @param $t
395
     *
396
     *  @return string
397
     */
398
    public function getPhpCodeSwitch($op = null, $content = null, $t = '')
399
    {
400
        $ret = "{$t}switch(\${$op}) {\n";
401
        $ret .= "{$t}{$content}";
402
        $ret .= "{$t}}\n";
403
404
        return $ret;
405
    }
406
407
    /**
408
     *  @public function getPhpCodeCaseSwitch     
409
     *
410
     *  @param $cases
411
     *  @param $defaultAfterCase
412
     *  @param $default
413
     *  @param $t
414
     *
415
     *  @return string
416
     */
417
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = '')
418
    {
419
        $ret = '';
420
        $def = "{$t}default:\n";
421
        foreach ($cases as $case => $value) {
422
            $case = is_string($case) ? "'{$case}'" : $case;
423
            if (!empty($case)) {
424
                $ret .= "{$t}case {$case}:\n";
425
                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...
426
                    $ret .= $def;
427
                }
428
                if (is_array($value)) {
429
                    foreach ($value as $content) {
430
                        $ret .= "{$t}{$t}{$content}\n";
431
                    }
432
                }
433
                $ret .= "{$t}break;\n";
434
            }
435
            $defaultAfterCase = false;
436
        }
437
        if ($default !== false) {
438
            $ret .= $def;
439
            $ret .= "{$t}{$default}\n";
440
            $ret .= "{$t}break;\n";
441
        }
442
443
        return $ret;
444
    }
445
446
    /*
447
    *  @public function getPhpCodeIsset
448
    *  @param $var
449
    *  @return string
450
    */
451
    public function getPhpCodeIsset($var)
452
    {
453
        return "isset(\${$var})";
454
    }
455
456
    /*
457
    *  @public function getPhpCodeUnset
458
    *  @param $var
459
    *  @return string
460
    */
461
    public function getPhpCodeUnset($var = '', $t = '')
462
    {
463
        return "{$t}unset(\${$var});\n";
464
    }
465
466
    /*
467
    *  @public function getPhpCodeImplode
468
    *  @param $left
469
    *  @param $right
470
    *  @return string
471
    */
472
    public function getPhpCodeImplode($left, $right)
473
    {
474
        return "implode('{$left}', {$right})";
475
    }
476
477
    /*
478
    *  @public function getPhpCodeExplode
479
    *  @param $left
480
    *  @param $right
481
    *  @return string
482
    */
483
    public function getPhpCodeExplode($left, $right)
484
    {
485
        return "explode('{$left}', {$right})";
486
    }
487
488
    /*
489
    *  @public function getPhpCodeChmod
490
    *  @param $var
491
    *  @param $perm
492
    *  @return string
493
    */
494
    public function getPhpCodeChmod($var, $perm = '0777', $t = '')
495
    {
496
        return "{$t}chmod(\${$var}, {$perm});\n";
497
    }
498
499
    /*
500
    *  @public function getPhpCodeMkdir
501
    *  @param $var
502
    *  @param $perm
503
    *  @return string
504
    */
505
    public function getPhpCodeMkdir($var, $perm = '0777', $t = '')
506
    {
507
        return "{$t}mkdir(\${$var}, {$perm});\n";
508
    }
509
510
    /*
511
    *  @public function getPhpCodeCopy
512
    *  @param $file
513
    *  @param $newfile
514
    *  @return string
515
    */
516
    public function getPhpCodeCopy($file, $newfile = '', $t = '')
517
    {
518
        return "{$t}copy({$file}, {$newfile});\n";
519
    }
520
521
    /*
522
    *  @public function getPhpCodeArray
523
    *  @param $var
524
    *  @param $array
525
    *  @param $isParam
526
    *
527
    *  @return string
528
    */
529
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
530
    {
531
        $retArray = array();
532
        if (is_array($array) && !empty($array)) {
533
            foreach ($array as $k => $v) {
534
                if (is_numeric($k)) {
535
                    $retArray[] = $v;
536
                } else {
537
                    $retArray[] = "{$k} => {$v}";
538
                }
539
            }
540
            $arrayContent = implode(', ', $retArray);
541
        } else {
542
            $arrayContent = '';
543
        }
544
        unset($retArray);
545
546
        if (!$isParam) {
547
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
548
        } else {
549
            $ret = "array({$array})";
550
        }
551
552
        return $ret;
553
    }
554
555
    /*
556
    *  @public function getPhpCodeArrayType
557
    *  @param $var
558
    *  @param $type
559
    *  @param $left
560
    *  @param $right
561
    *  @param $isParam
562
    *
563
    *  @return string
564
    */
565
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
566
    {
567
        $vars = ($right != null) ? "\${$left}, {$right}" : "\${$left}";
568
        if (!$isParam) {
569
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
570
        } else {
571
            $ret = "array_{$type}({$vars})";
572
        }
573
574
        return $ret;
575
    }
576
577
    /*
578
    *  @public function getPhpCodeSprintf
579
    *  @param $left
580
    *  @param $right
581
    *  @return string
582
    */
583
    public function getPhpCodeSprintf($left, $right)
584
    {
585
        return "sprintf({$left}, {$right})";
586
    }
587
588
    /*
589
    *  @public function getPhpCodeEmpty
590
    *  @param $var
591
    *  @return string
592
    */
593
    public function getPhpCodeEmpty($var)
594
    {
595
        return "empty({$var})";
596
    }
597
598
    /*
599
    *  @public function getPhpCodeHeader
600
    *  @param $var
601
    *  @return string
602
    */
603
    public function getPhpCodeHeader($var)
604
    {
605
        return "header({$var})";
606
    }
607
608
    /*
609
    *  @public function getPhpCodeRawurlencode
610
    *  @param $var
611
    *  @return string
612
    */
613
    public function getPhpCodeRawurlencode($var)
614
    {
615
        return "rawurlencode({$var})";
616
    }
617
618
    /*
619
    *  @public function getPhpCodePregFunzions
620
    *  @param $return
621
    *  @param $exp
622
    *  @param $str
623
    *  @param $val
624
    *  @param $type
625
    *  @param $isParam
626
    *
627
    *  @return string
628
    */
629
    public function getPhpCodePregFunzions($return, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t")
630
    {
631
        $pregFunz = "preg_{$type}( '";
632
        if (!$isParam) {
633
            $ret = "{$t}\${$return} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
634
        } else {
635
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
636
        }
637
638
        return $ret;
639
    }
640
641
    /*
642
    *  @public function getPhpCodeStrType
643
    *  @param $left
644
    *  @param $var
645
    *  @param $str
646
    *  @param $value
647
    *  @param $type
648
    *  @param $isParam
649
    *
650
    *  @return string
651
    */
652
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
653
    {
654
        $strType = "str_{$type}( '";
655
        if (!$isParam) {
656
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
657
        } else {
658
            $ret = "{$strType}{$var}', '{$str}', {$value})";
659
        }
660
661
        return $ret;
662
    }
663
664
    /*
665
    *  @public function getPhpCodeStripTags
666
    *  @param $left
667
    *  @param $value
668
    *  @param $isParam
669
    *
670
    *  @return string
671
    */
672
    public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = '')
673
    {
674
        if (!$isParam) {
675
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
676
        } else {
677
            $ret = "strip_tags({$value})";
678
        }
679
680
        return $ret;
681
    }
682
683
    /*
684
    *  @public function getPhpCodeHtmlentities
685
    *  @param $entitiesVar
686
    *  @param $entitiesQuote
687
    *  @return string
688
    */
689
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
690
    {
691
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
692
        $entities = "htmlentities({$entitiesVar})";
693
694
        return $entities;
695
    }
696
697
    /*
698
    *  @public function getPhpCodeHtmlspecialchars
699
    *  @param $specialVar
700
    *  @param $specialQuote
701
    *  @return string
702
    */
703
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
704
    {
705
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
706
        $specialchars = "htmlspecialchars({$specialVar})";
707
708
        return $specialchars;
709
    }
710
}
711