Completed
Pull Request — master (#115)
by Gino
03:12
created

TDMCreateFile::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
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
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
30
    /**
31
     * "fileName" attribute of the files.
32
     *
33
     * @var mixed
34
     */
35
    private $fileName = null;
36
37
    /**
38
     * "subdir" attribute of the directories.
39
     *
40
     * @var string
41
     */
42
    private $subdir = null;
43
44
    /**
45
     * "uploadPath" attribute of the files.
46
     *
47
     * @var string
48
     */
49
    private $uploadPath = null;
50
51
    /*
52
    * @var string
53
    */
54
    private $content = null;
55
56
    /*
57
    * @var mixed
58
    */
59
    private $created = null;
60
61
    /*
62
    * @var mixed
63
    */
64
    private $notCreated = null;
65
66
    /*
67
    * @var string
68
    */
69
    private $mode = null;
70
71
    /*
72
    * @var string
73
    */
74
    protected $phpcode = null;
75
76
    /*
77
    * @var string
78
    */
79
    protected $htmlcode;
80
81
    /*
82
    *  @public function constructor
83
    *  @param null
84
    */
85
    /**
86
     *
87
     */
88
    public function __construct()
89
    {
90
        parent::__construct();
91
    }
92
93
    /*
94
    *  @public static function &getInstance
95
    *  @param null
96
    */
97
    /**
98
     * @return TDMCreateFile
99
     */
100
    public static function &getInstance()
101
    {
102
        static $instance = false;
103
        if (!$instance) {
104
            $instance = new self();
105
        }
106
107
        return $instance;
108
    }
109
110
    /**
111
     * @public function create
112
     *
113
     * @param $moduleDirname
114
     * @param $subdir
115
     * @param $fileName
116
     * @param $content
117
     * @param $created
118
     * @param $notCreated
119
     * @param $mode
120
     */
121
    public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+')
122
    {
123
        $this->setFileName($fileName);
124
        $this->created = $created;
125
        $this->notCreated = $notCreated;
126
        $this->setMode($mode);
127
        $this->setRepositoryPath($moduleDirname);
128
        if (!empty($content) && is_string($content)) {
129
            $this->setContent($content);
130
        }
131
        if (isset($subdir) && is_string($subdir)) {
132
            $this->setSubDir($subdir);
133
        }
134
    }
135
136
    /*
137
    *  @public function write
138
    *  @param string $module
139
    *  @param string $fileName
140
    */
141
    /**
142
     * @param $module
143
     * @param $fileName
144
     */
145
    /*public function write($module, $fileName)
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
    {
147
        $this->setModule($module);
148
        $this->setFileName($fileName);
149
    }*/
150
151
    /*
152
    *  @private function setRepositoryPath
153
    *  @param string $moduleDirname
154
    */
155
    /**
156
     * @param $moduleDirname
157
     */
158
    private function setRepositoryPath($moduleDirname)
159
    {
160
        $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH.'/'.$moduleDirname;
161
    }
162
163
    /*
164
    *  @private function getRepositoryPath
165
    *  @param null
166
    */
167
    /**
168
     * @return string
169
     */
170
    private function getRepositoryPath()
171
    {
172
        return $this->uploadPath;
173
    }
174
175
    /*
176
    *  @private function setSubDir
177
    *  @param string $subdir
178
    */
179
    /**
180
     * @param $subdir
181
     */
182
    private function setSubDir($subdir)
183
    {
184
        $this->subdir = $subdir;
185
    }
186
187
    /*
188
    *  @private function getSubDir
189
    *  @param null
190
    */
191
    private function getSubDir()
192
    {
193
        return $this->subdir;
194
    }
195
196
    /**
197
     * public function setFileName.
198
     *
199
     * @param $fileName
200
     *
201
     * @internal param mixed $file_name
202
     */
203
    public function setFileName($fileName)
204
    {
205
        $this->fileName = $fileName;
206
    }
207
208
    /*
209
    *  @public function getFileName
210
    *  @param null
211
    */
212
    /**
213
     * @return mixed
214
     */
215
    public function getFileName()
216
    {
217
        return $this->fileName;
218
    }
219
220
    /*
221
    *  @private function setContent
222
    *  @param string $content
223
    */
224
    /**
225
     * @param $content
226
     */
227
    private function setContent($content)
228
    {
229
        $this->content = $content;
230
    }
231
232
    /*
233
    *  @private function setContent
234
    *  @param null
235
    */
236
    /**
237
     */
238
    private function getContent()
239
    {
240
        return $this->content;
241
    }
242
243
    /*
244
    *  @private function getFolderName
245
    *  @param null
246
    */
247
    /**
248
     * @return string
249
     */
250
    private function getFolderName()
251
    {
252
        $path = $this->getUploadPath();
253
        if (strrpos($path, '\\')) {
254
            $str = strrpos($path, '\\');
255
            if ($str !== false) {
256
                return substr($path, $str + 1, strlen($path));
257
            } else {
258
                return substr($path, $str, strlen($path));
259
            }
260
        }
261
        //return 'root module';
262
        return;
263
    }
264
265
    /*
266
    *  @public function getUploadPath
267
    *  @param null
268
    */
269
    /**
270
     * @return string
271
     */
272
    private function getUploadPath()
273
    {
274
        if ($this->getSubDir() != null) {
275
            $ret = $this->getRepositoryPath().'/'.$this->getSubDir();
276
        } else {
277
            $ret = $this->getRepositoryPath();
278
        }
279
280
        return $ret;
281
    }
282
283
    /*
284
    *  @private function getCreated
285
    *  @param null
286
    */
287
    /**
288
     * @return bool
289
     */
290
    private function getCreated()
291
    {
292
        return $this->created;
293
    }
294
295
    /*
296
    *  @private function getNotCreated
297
    *  @param null
298
    */
299
    /**
300
     * @return bool
301
     */
302
    private function getNotCreated()
303
    {
304
        return $this->notCreated;
305
    }
306
307
    /*
308
    *  @private function setMode
309
    *  @param string $mode
310
    */
311
    /**
312
     * @param $mode
313
     */
314
    private function setMode($mode)
315
    {
316
        $this->mode = $mode;
317
    }
318
319
    /*
320
    *  @private function getMode
321
    *  @param null
322
    */
323
    /**
324
     */
325
    private function getMode()
326
    {
327
        return $this->mode;
328
    }
329
330
    /*
331
    *  @public function getLanguage
332
    *  @param string $moduleDirname
333
    *  @param string $prefix
334
    *  @param string $suffix
335
    */
336
    /**
337
     * @param        $moduleDirname
338
     * @param string $prefix
339
     * @param string $suffix
340
     *
341
     * @return string
342
     */
343
    public function getLanguage($moduleDirname, $prefix = '', $suffix = '')
344
    {
345
        $lang = '_'.$prefix.'_'.strtoupper($moduleDirname);
346
        $ret = $lang;
347
        if (!empty($suffix) || $suffix != '_') {
348
            $ret = $lang.'_'.$suffix;
349
        } elseif ($suffix == '_') {
350
            $ret = $lang.'_';
351
        }
352
353
        return $ret;
354
    }
355
356
    /*
357
    *  @public function getLeftString
358
    *  @param string $string
359
    */
360
    /**
361
     * @param $string
362
     *
363
     * @return string
364
     */
365
    public function getLeftString($string)
366
    {
367
        return substr($string, 0, strpos($string, '_'));
368
    }
369
370
    /*
371
     *  @public function getRightString
372
     *  @param $string
373
     *
374
     * @return string
375
     */
376
    public function getRightString($string = null)
377
    {
378
        if (strpos($string, '_')) {
379
            $str = strpos($string, '_');
380
            if ($str !== false) {
381
                $ret = substr($string, $str + 1, strlen($string));
382
383
                return $ret;
384
            }
385
        }
386
387
        return $string;
388
    }
389
390
    /*
391
     *  @public function getCamelCase
392
     *  @param $string
393
     */
394
    /**
395
     * @param $string
396
     * @param $ucfirst
397
     * @param $lcfirst
398
     *
399
     * @return string
400
     */
401
    public function getCamelCase($string, $ucfirst = false, $lcfirst = false)
402
    {
403
        $rightString = $this->getRightString($string);
404
        $leftString = $this->getLeftString($string);
405
        if ($ucfirst) {
406
            return $this->getUcfirst($leftString).$this->getUcfirst($rightString);
407
        }
408
        if ($lcfirst) {
409
            return $this->getlcfirst($leftString).$this->getUcfirst($rightString);
410
        }
411
412
        return $string;
413
    }
414
415
    /*
416
     *  @public function getUcfirst
417
     *  @param $string
418
     */
419
    /**
420
     * @param $string
421
     *
422
     * @return string
423
     */
424
    public function getUcfirst($string)
425
    {
426
        return ucfirst($string);
427
    }
428
429
    /*
430
     *  @public function getLcfirst
431
     *  @param $string
432
     */
433
    /**
434
     * @param $string
435
     *
436
     * @return string
437
     */
438
    public function getLcfirst($string)
439
    {
440
        return lcfirst($string);
441
    }
442
443
    /*
444
     *  @public function getStrToUpper
445
     *  @param $string
446
     */
447
    /**
448
     * @param $string
449
     *
450
     * @return string
451
     */
452
    public function getStrToUpper($string)
453
    {
454
        return strtoupper($string);
455
    }
456
457
    /*
458
     *  @public function getStrToLower
459
     *  @param $string
460
     */
461
    /**
462
     * @param $string
463
     *
464
     * @return string
465
     */
466
    public function getStrToLower($string)
467
    {
468
        return strtolower($string);
469
    }
470
471
    /*
472
    *  @public function getInclude
473
    *  @param $filename
474
    */
475
    /**
476
     * @return string
477
     */
478
    public function getInclude($filename = 'header')
479
    {
480
        return "include __DIR__ . '/{$filename}.php';\n";
481
    }
482
483
    /*
484
    *  @public function getIncludeOnce
485
    *  @param $filename
486
    */
487
    /**
488
     * @return string
489
     */
490
    public function getIncludeOnce($filename = 'header')
491
    {
492
        return "include_once __DIR__ . '/{$filename}.php';\n";
493
    }
494
495
    /**
496
     * @private function getDashComment
497
     *
498
     * @param $comment
499
     *
500
     * @return string
501
     */
502
    public function getDashComment($comment)
503
    {
504
        return "// ------------------- {$comment} ------------------- //\n";
505
    }
506
507
    /*
508
     * @public function getSimpleString
509
     * @param $string     
510
     *
511
     * @return string
512
     */
513
    public function getSimpleString($string, $t = '')
514
    {
515
        return "{$t}{$string}\n";
516
    }
517
518
    /*
519
    *  @public function getHeaderFilesComments
520
    *  @param string $module
521
    *  @param string $fileName
522
    */
523
    /**
524
     * @param $module
525
     * @param $fileName
526
     * @param $noPhpFile
527
     *
528
     * @return string
529
     */
530
    public function getHeaderFilesComments($module, $fileName, $noPhpFile = null)
531
    {
532
        $name = $module->getVar('mod_name');
533
        $dirname = $module->getVar('mod_dirname');
534
        $version = $module->getVar('mod_version');
535
        $since = $module->getVar('mod_since');
536
        $minXoops = $module->getVar('mod_min_xoops');
537
        $author = $module->getVar('mod_author');
538
        $credits = $module->getVar('mod_credits');
539
        $authorMail = $module->getVar('mod_author_mail');
540
        $authorWebsiteUrl = $module->getVar('mod_author_website_url');
541
        $license = $module->getVar('mod_license');
542
        $subversion = $module->getVar('mod_subversion');
543
        $date = date('D Y-m-d H:i:s');
544
        if (is_null($noPhpFile)) {
545
            $ret = "<?php\n";
546
            $ret .= "/*\n";
547
        } elseif (is_string($noPhpFile)) {
548
            $ret = $noPhpFile;
549
            $ret .= "/*\n";
550
        } else {
551
            $ret .= "/*\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...
552
        }
553
        $filename = TDMC_CLASS_PATH.'/files/docs/license.txt';
554
        $handle = fopen($filename, 'rb');
555
        $data = fread($handle, filesize($filename));
556
        fclose($handle);
557
        $ret .= $data."\n";
558
        $ret .= "*/\n";
559
        $copyright = array($name => 'module for xoops', '' => '', '@copyright  ' => '   module for xoops', '@license   ' => "    {$license}", '@package   ' => "    {$dirname}",
560
                            '@since    ' => "     {$since}", '@min_xoops   ' => "  {$minXoops}", '@author    ' => "    {$author} - Email:<{$authorMail}> - Website:<{$authorWebsiteUrl}>",
561
                            '@version    ' => "   \$Id: {$version} {$fileName} {$subversion} {$date}Z {$credits} \$", );
562
        $ret .= TDMCreatePhpCode::getInstance()->getPhpCodeCommentMultiLine($copyright);
563
564
        return $ret;
565
    }
566
567
    /*
568
    *  @public function renderFile
569
    *  @param null
570
    */
571
    /**
572
     * @return string
573
     */
574
    public function renderFile()
575
    {
576
        $fileName = $this->getFileName();
577
        $path = $this->getUploadPath().'/'.$fileName;
578
        $created = $this->getCreated();
579
        $notCreated = $this->getNotCreated();
580
        $folderName = $this->getFolderName();
581
        $mode = $this->getMode();
582
        $ret = '';
583
        $xf = XoopsFile::getHandler();
584
		if (!$xf->__construct($path, true)) {
585
            // Force to create file if not exist
586
            if ($xf->open($mode, true)) {
587
                if ($xf->writable()) {
588
                    // Integration of the content in the file
589
                    if (!$xf->write($this->getContent(), $mode, true)) {
590
                        $ret .= sprintf($notCreated, $fileName, $folderName);
591
                        $GLOBALS['xoopsTpl']->assign('created', false);
592
                        exit();
593
                    }
594
                    // Created
595
                    $ret .= sprintf($created, $fileName, $folderName);
596
                    $GLOBALS['xoopsTpl']->assign('created', true);
597
                    $xf->close();
598
                } else {
599
                    $ret .= sprintf($notCreated, $fileName, $folderName);
600
                    $GLOBALS['xoopsTpl']->assign('created', false);
601
                }
602
            } else {
603
                $ret .= sprintf($notCreated, $fileName, $folderName);
604
                $GLOBALS['xoopsTpl']->assign('created', false);
605
            }
606
        }
607
608
        return $ret;
609
    }
610
}
611