Test Failed
Push — main ( cb6120...71a77e )
by chief
07:27
created

video_helper::if_favorited()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @Auther: bhief
5
* @Version: 1.0
6
*
7
* Gets info from videos 
8
*
9
**/
10
class video_helper {
11
    public $__db;
12
13
    public function sh_exec(string $cmd, string $outputfile = "", string $pidfile = "", bool $mergestderror = true, bool $bg = false) {
14
        $fullcmd = $cmd;
15
        if(strlen($outputfile) > 0) $fullcmd .= " >> " . $outputfile;
16
        if($mergestderror) $fullcmd .= " 2>&1";
17
        
18
        if($bg) {
19
            $fullcmd = "nohup " . $fullcmd . " &";
20
            if(strlen($pidfile)) $fullcmd .= " echo $! > " . $pidfile;
21
        } else {
22
            if(strlen($pidfile) > 0) $fullcmd .= "; echo $$ > " . $pidfile;
23
        }
24
        shell_exec($fullcmd);
25
    }
26
27
    function file_get_contents_chunked($file, $chunk_size, $callback) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
        try {
29
            $handle = fopen($file, "r");
30
            $i = 0;
31
            while (!feof($handle)) {
32
                call_user_func_array($callback,array(fread($handle,$chunk_size),&$handle,$i));
33
                $i++;
34
            }
35
    
36
            fclose($handle);
37
        }
38
        catch(Exception $e) {
39
            trigger_error("file_get_contents_chunked::" . $e->getMessage(),E_USER_NOTICE);
40
            return false;
41
        }
42
43
        return true;
44
    }
45
46
	public function __construct($conn){
47
        $this->__db = $conn;
48
	}
49
50
    function fetch_video_views(string $id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
        $stmt = $this->__db->prepare("SELECT * FROM views WHERE videoid = :id");
52
        $stmt->bindParam(":id", $id);
53
        $stmt->execute();
54
55
        return $stmt->rowCount();
56
    }
57
58
    function get_comments_from_video($id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
        $stmt = $this->__db->prepare("SELECT * FROM comments WHERE toid = :id");
60
        $stmt->bindParam(":id", $id);
61
        $stmt->execute();
62
    
63
        return $stmt->rowCount();
64
    }
65
66
    function shorten_description(string $description, int $limit, bool $newlines = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
        $description = trim($description);
68
        if(strlen($description) >= $limit) {
69
            $description = substr($description, 0, $limit) . "...";
70
        } 
71
72
        $description = htmlspecialchars($description);
73
        if($newlines) { $description = str_replace(PHP_EOL, "<br>", $description); }
74
        return $description;
75
    }
76
77
    function get_video_responses($id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
        $stmt = $this->__db->prepare("SELECT * FROM video_response WHERE toid = :id");
79
        $stmt->bindParam(":id", $id);
80
        $stmt->execute();
81
    
82
        return $stmt->rowCount();
83
    }
84
85
    function get_video_likes($reciever, $liked) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86
        if($liked) {
87
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE reciever = :reciever AND type = 'l'");
88
            $stmt->bindParam(":reciever", $reciever);
89
            $stmt->execute();
90
91
            return $stmt->rowCount();
92
        } else {
93
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE reciever = :reciever AND type = 'd'");
94
            $stmt->bindParam(":reciever", $reciever);
95
            $stmt->execute();
96
97
            return $stmt->rowCount();
98
        }
99
    }
100
101
    function if_liked($user, $reciever, $liked) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
        if($liked) {
103
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE sender = :sender AND reciever = :reciever AND type = 'l'");
104
            $stmt->bindParam(":sender", $user);
105
            $stmt->bindParam(":reciever", $reciever);
106
            $stmt->execute();
107
108
            return $stmt->rowCount() === 1;
109
        } else {
110
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE sender = :sender AND reciever = :reciever AND type = 'd'");
111
            $stmt->bindParam(":sender", $user);
112
            $stmt->bindParam(":reciever", $reciever);
113
            $stmt->execute();
114
115
            return $stmt->rowCount() === 1;
116
        }
117
    }
118
119
    function fetch_video_rid(string $rid) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
120
        $stmt = $this->__db->prepare("SELECT * FROM videos WHERE rid = :rid");
121
        $stmt->bindParam(":rid", $rid);
122
        $stmt->execute();
123
124
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
125
    }
126
127
    function fetch_playlist_rid(string $rid) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
        $stmt = $this->__db->prepare("SELECT * FROM playlists WHERE rid = :rid");
129
        $stmt->bindParam(":rid", $rid);
130
        $stmt->execute();
131
132
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
133
    }
134
135
    function fetch_comment_id(string $id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136
        $stmt = $this->__db->prepare("SELECT * FROM comments WHERE id = :id");
137
        $stmt->bindParam(":id", $id);
138
        $stmt->execute();
139
140
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
141
    }
142
143
    function get_video_stars_level($id, $level) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
144
        $stmt = $this->__db->prepare("SELECT * FROM stars WHERE reciever = :id AND type = :lvl");
145
        $stmt->bindParam(":id", $id);
146
        $stmt->bindParam(":lvl", $level, PDO::PARAM_INT);
147
        $stmt->execute();
148
    
149
        return $stmt->rowCount();
150
    }
151
152
    function video_exists($video) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
        $stmt = $this->__db->prepare("SELECT rid FROM videos WHERE rid = :rid");
154
        $stmt->bindParam(":rid", $video);
155
        $stmt->execute();
156
        
157
        return $stmt->rowCount() === 1;
158
    }
159
160
    function if_favorited($user, $reciever) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
161
        $stmt = $this->__db->prepare("SELECT `reciever` FROM favorite_video WHERE sender = :user AND reciever = :reciever");
162
        $stmt->bindParam(":user", $user);
163
        $stmt->bindParam(":reciever", $reciever);
164
        $stmt->execute();
165
        
166
        return $stmt->rowCount() === 1;
167
    }
168
169
    function fetch_user_videos($v) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
170
        $stmt = $this->__db->prepare("SELECT rid FROM videos WHERE author = :v");
171
        $stmt->bindParam(":v", $v);
172
        $stmt->execute(); 
173
    
174
        return $stmt->rowCount();
175
    }
176
}
177
178
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...