Passed
Push — master ( 0f0f99...fb91d6 )
by Michael
17s
created

TDMCreateFile::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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: TDMCreateFile.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
xoops_load('XoopsFile');
25
/**
26
 * Class TDMCreateFile.
27
 */
28
class TDMCreateFile extends TDMCreateTableFields
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
    * @var string
32
    */
33
    private $xf = null;
34
35
    /**
36
     * "fileName" attribute of the files.
37
     *
38
     * @var mixed
39
     */
40
    private $fileName = null;
41
42
    /**
43
     * "subdir" attribute of the directories.
44
     *
45
     * @var string
46
     */
47
    private $subdir = null;
48
49
    /**
50
     * "uploadPath" attribute of the files.
51
     *
52
     * @var string
53
     */
54
    private $uploadPath = null;
55
56
    /*
57
    * @var string
58
    */
59
    private $content = null;
60
61
    /*
62
    * @var mixed
63
    */
64
    private $created = null;
65
66
    /*
67
    * @var mixed
68
    */
69
    private $notCreated = null;
70
71
    /*
72
    * @var string
73
    */
74
    private $mode = null;
75
76
    /*
77
    * @var string
78
    */
79
    protected $phpcode = null;
80
81
    /*
82
    * @var string
83
    */
84
    protected $htmlcode;
85
86
    /*
87
    *  @public function constructor
88
    *  @param null
89
    */
90
    /**
91
     *
92
     */
93
    public function __construct()
94
    {
95
        parent::__construct();
96
        $this->xf = XoopsFile::getHandler();
97
    }
98
99
    /*
100
    *  @public static function &getInstance
101
    *  @param null
102
    */
103
    /**
104
     * @return TDMCreateFile
105
     */
106
    public static function &getInstance()
107
    {
108
        static $instance = false;
109
        if (!$instance) {
110
            $instance = new self();
111
        }
112
113
        return $instance;
114
    }
115
116
    /**
117
     * @public function create
118
     *
119
     * @param $moduleDirname
120
     * @param $subdir
121
     * @param $fileName
122
     * @param $content
123
     * @param $created
124
     * @param $notCreated
125
     * @param $mode
126
     */
127
    public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+')
128
    {
129
        $this->setFileName($fileName);
130
        $this->created = $created;
131
        $this->notCreated = $notCreated;
132
        $this->setMode($mode);
133
        $this->setRepositoryPath($moduleDirname);
134
        if (!empty($content) && is_string($content)) {
135
            $this->setContent($content);
136
        }
137
        if (isset($subdir) && is_string($subdir)) {
138
            $this->setSubDir($subdir);
139
        }
140
    }
141
142
    /*
143
    *  @public function write
144
    *  @param string $module
145
    *  @param string $fileName
146
    */
147
    /**
148
     * @param $module
149
     * @param $fileName
150
     */
151
    public function write($module, $fileName)
152
    {
153
        $this->setModule($module);
154
        $this->setFileName($fileName);
155
    }
156
157
    /*
158
    *  @private function setRepositoryPath
159
    *  @param string $moduleDirname
160
    */
161
    /**
162
     * @param $moduleDirname
163
     */
164
    private function setRepositoryPath($moduleDirname)
165
    {
166
        $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH.'/'.$moduleDirname;
167
    }
168
169
    /*
170
    *  @private function getRepositoryPath
171
    *  @param null
172
    */
173
    /**
174
     * @return string
175
     */
176
    private function getRepositoryPath()
177
    {
178
        return $this->uploadPath;
179
    }
180
181
    /*
182
    *  @private function setSubDir
183
    *  @param string $subdir
184
    */
185
    /**
186
     * @param $subdir
187
     */
188
    private function setSubDir($subdir)
189
    {
190
        $this->subdir = $subdir;
191
    }
192
193
    /*
194
    *  @private function getSubDir
195
    *  @param null
196
    */
197
    private function getSubDir()
198
    {
199
        return $this->subdir;
200
    }
201
202
    /**
203
     * public function setFileName.
204
     *
205
     * @param $fileName
206
     *
207
     * @internal param mixed $file_name
208
     */
209
    public function setFileName($fileName)
210
    {
211
        $this->fileName = $fileName;
212
    }
213
214
    /*
215
    *  @public function getFileName
216
    *  @param null
217
    */
218
    /**
219
     * @return mixed
220
     */
221
    public function getFileName()
222
    {
223
        return $this->fileName;
224
    }
225
226
    /*
227
    *  @private function setContent
228
    *  @param string $content
229
    */
230
    /**
231
     * @param $content
232
     */
233
    private function setContent($content)
234
    {
235
        $this->content = $content;
236
    }
237
238
    /*
239
    *  @private function setContent
240
    *  @param null
241
    */
242
    /**
243
     */
244
    private function getContent()
245
    {
246
        return $this->content;
247
    }
248
249
    /*
250
    *  @private function getFolderName
251
    *  @param null
252
    */
253
    /**
254
     * @return string
255
     */
256
    private function getFolderName()
257
    {
258
        $path = $this->getUploadPath();
259
        if (strrpos($path, '\\')) {
260
            $str = strrpos($path, '\\');
261
            if ($str !== false) {
262
                return substr($path, $str + 1, strlen($path));
263
            } else {
264
                return substr($path, $str, strlen($path));
265
            }
266
        }
267
        //return 'root module';
268
        return;
269
    }
270
271
    /*
272
    *  @public function getUploadPath
273
    *  @param null
274
    */
275
    /**
276
     * @return string
277
     */
278
    private function getUploadPath()
279
    {
280
        if ($this->getSubDir() != null) {
281
            $ret = $this->getRepositoryPath().'/'.$this->getSubDir();
282
        } else {
283
            $ret = $this->getRepositoryPath();
284
        }
285
286
        return $ret;
287
    }
288
289
    /*
290
    *  @private function getCreated
291
    *  @param null
292
    */
293
    /**
294
     * @return bool
295
     */
296
    private function getCreated()
297
    {
298
        return $this->created;
299
    }
300
301
    /*
302
    *  @private function getNotCreated
303
    *  @param null
304
    */
305
    /**
306
     * @return bool
307
     */
308
    private function getNotCreated()
309
    {
310
        return $this->notCreated;
311
    }
312
313
    /*
314
    *  @private function setMode
315
    *  @param string $mode
316
    */
317
    /**
318
     * @param $mode
319
     */
320
    private function setMode($mode)
321
    {
322
        $this->mode = $mode;
323
    }
324
325
    /*
326
    *  @private function getMode
327
    *  @param null
328
    */
329
    /**
330
     */
331
    private function getMode()
332
    {
333
        return $this->mode;
334
    }
335
336
    /*
337
    *  @public function getLanguage
338
    *  @param string $moduleDirname
339
    *  @param string $prefix
340
    *  @param string $suffix
341
    */
342
    /**
343
     * @param        $moduleDirname
344
     * @param string $prefix
345
     * @param string $suffix
346
     *
347
     * @return string
348
     */
349
    public function getLanguage($moduleDirname, $prefix = '', $suffix = '')
350
    {
351
        $lang = '_'.$prefix.'_'.strtoupper($moduleDirname);
352
        $ret = $lang;
353
        if (!empty($suffix) || $suffix != '_') {
354
            $ret = $lang.'_'.$suffix;
355
        } elseif ($suffix == '_') {
356
            $ret = $lang.'_';
357
        }
358
359
        return $ret;
360
    }
361
362
    /*
363
    *  @public function getLeftString
364
    *  @param string $string
365
    */
366
    /**
367
     * @param $string
368
     *
369
     * @return string
370
     */
371
    public function getLeftString($string)
372
    {
373
        return substr($string, 0, strpos($string, '_'));
374
    }
375
376
    /*
377
     *  @public function getRightString
378
     *  @param $string
379
     *
380
     * @return string
381
     */
382
    public function getRightString($string = null)
383
    {
384
        if (strpos($string, '_')) {
385
            $str = strpos($string, '_');
386
            if ($str !== false) {
387
                $ret = substr($string, $str + 1, strlen($string));
388
389
                return $ret;
390
            }
391
        }
392
393
        return $string;
394
    }
395
396
    /*
397
     *  @public function getCamelCase
398
     *  @param $string
399
     */
400
    /**
401
     * @param $string
402
     * @param $ucfirst
403
     * @param $lcfirst
404
     *
405
     * @return string
406
     */
407
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
408
    {
409
        $rightString = $this->getRightString($string);
410
        $leftString = $this->getLeftString($string);
411
        if ($ucfirst) {
412
            return $this->getUcfirst($leftString).$this->getUcfirst($rightString);
413
        }
414
        if ($lcfirst) {
415
            return $this->getlcfirst($leftString).$this->getUcfirst($rightString);
416
        }
417
418
        return $string;
419
    }
420
421
    /*
422
     *  @public function getUcfirst
423
     *  @param $string
424
     */
425
    /**
426
     * @param $string
427
     *
428
     * @return string
429
     */
430
    public function getUcfirst($string)
431
    {
432
        return ucfirst($string);
433
    }
434
435
    /*
436
     *  @public function getLcfirst
437
     *  @param $string
438
     */
439
    /**
440
     * @param $string
441
     *
442
     * @return string
443
     */
444
    public function getLcfirst($string)
445
    {
446
        return lcfirst($string);
447
    }
448
449
    /*
450
     *  @public function getStrToUpper
451
     *  @param $string
452
     */
453
    /**
454
     * @param $string
455
     *
456
     * @return string
457
     */
458
    public function getStrToUpper($string)
459
    {
460
        return strtoupper($string);
461
    }
462
463
    /*
464
     *  @public function getStrToLower
465
     *  @param $string
466
     */
467
    /**
468
     * @param $string
469
     *
470
     * @return string
471
     */
472
    public function getStrToLower($string)
473
    {
474
        return strtolower($string);
475
    }
476
477
    /*
478
    *  @public function getInclude
479
    *  @param $filename
480
    */
481
    /**
482
     * @return string
483
     */
484
    public function getInclude($filename = 'header')
485
    {
486
        return "include __DIR__ . '/{$filename}.php';\n";
487
    }
488
489
    /*
490
    *  @public function getIncludeOnce
491
    *  @param $filename
492
    */
493
    /**
494
     * @return string
495
     */
496
    public function getIncludeOnce($filename = 'header')
497
    {
498
        return "include_once __DIR__ . '/{$filename}.php';\n";
499
    }
500
501
    /**
502
     * @private function getDashComment
503
     *
504
     * @param $comment
505
     *
506
     * @return string
507
     */
508
    public function getDashComment($comment)
509
    {
510
        return "// ------------------- {$comment} ------------------- //\n";
511
    }
512
513
    /*
514
     * @public function getSimpleString
515
     * @param $string     
516
     *
517
     * @return string
518
     */
519
    public function getSimpleString($string, $t = '')
520
    {
521
        return "{$t}{$string}\n";
522
    }
523
524
    /*
525
    *  @public function getHeaderFilesComments
526
    *  @param string $module
527
    *  @param string $fileName
528
    */
529
    /**
530
     * @param $module
531
     * @param $fileName
532
     * @param $noPhpFile
533
     *
534
     * @return string
535
     */
536
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
537
    {
538
        $name = $module->getVar('mod_name');
539
        $dirname = $module->getVar('mod_dirname');
540
        $version = $module->getVar('mod_version');
541
        $since = $module->getVar('mod_since');
542
        $minXoops = $module->getVar('mod_min_xoops');
543
        $author = $module->getVar('mod_author');
544
        $credits = $module->getVar('mod_credits');
545
        $authorMail = $module->getVar('mod_author_mail');
546
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
547
        $license = $module->getVar('mod_license');
548
        $subversion = $module->getVar('mod_subversion');
549
        $date = date('D Y-m-d H:i:s');
550
        if (is_null($noPhpFile)) {
551
            $ret = "<?php\n";
552
            $ret .= "/*\n";
553
        } elseif (is_string($noPhpFile)) {
554
            $ret = $noPhpFile;
555
            $ret .= "\n/*\n";
556
        } else {
557
            $ret .= "\n/*\n";
0 ignored issues
show
Bug introduced by
The variable $ret seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
558
        }
559
        $filename = TDMC_CLASS_PATH.'/files/docs/license.txt';
560
        $handle = fopen($filename, 'rb');
561
        $data = fread($handle, filesize($filename));
562
        fclose($handle);
563
        $ret .= $data."\n";
564
        $ret .= "*/\n";
565
        $copyright = array($name => 'module for xoops', '' => '', '@copyright  ' => '   module for xoops', '@license   ' => "    {$license}", '@package   ' => "    {$dirname}",
566
                            '@since    ' => "     {$since}", '@min_xoops   ' => "  {$minXoops}", '@author    ' => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
567
                            '@version    ' => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$", );
568
        $ret .= TDMCreatePhpCode::getInstance()->getPhpCodeCommentMultiLine($copyright);
569
570
        return $ret;
571
    }
572
573
    /*
574
    *  @public function renderFile
575
    *  @param null
576
    */
577
    /**
578
     * @return string
579
     */
580
    public function renderFile()
581
    {
582
        $fileName = $this->getFileName();
583
        $path = $this->getUploadPath().'/'.$fileName;
584
        $created = $this->getCreated();
585
        $notCreated = $this->getNotCreated();
586
        $folderName = $this->getFolderName();
587
        $mode = $this->getMode();
588
        $ret = '';
589
        if (!$this->xf->__construct($path, true)) {
0 ignored issues
show
Bug introduced by
The method __construct cannot be called on $this->xf (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
590
            // Force to create file if not exist
591
            if ($this->xf->open($mode, true)) {
0 ignored issues
show
Bug introduced by
The method open cannot be called on $this->xf (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
592
                if ($this->xf->writable()) {
0 ignored issues
show
Bug introduced by
The method writable cannot be called on $this->xf (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
593
                    // Integration of the content in the file
594
                    if (!$this->xf->write($this->getContent(), $mode, true)) {
0 ignored issues
show
Bug introduced by
The method write cannot be called on $this->xf (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
595
                        $ret .= sprintf($notCreated, $fileName, $folderName);
596
                        $GLOBALS['xoopsTpl']->assign('created', false);
597
                        exit();
598
                    }
599
                    // Created
600
                    $ret .= sprintf($created, $fileName, $folderName);
601
                    $GLOBALS['xoopsTpl']->assign('created', true);
602
                    $this->xf->close();
0 ignored issues
show
Bug introduced by
The method close cannot be called on $this->xf (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
603
                } else {
604
                    $ret .= sprintf($notCreated, $fileName, $folderName);
605
                    $GLOBALS['xoopsTpl']->assign('created', false);
606
                }
607
            } else {
608
                $ret .= sprintf($notCreated, $fileName, $folderName);
609
                $GLOBALS['xoopsTpl']->assign('created', false);
610
            }
611
        }
612
613
        return $ret;
614
    }
615
}
616