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