Test Failed
Push — main ( 433cdf...8ba42a )
by chief
02:59
created

video_helper::add_view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
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 fetch_views_from_user($user) {
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 `rid` FROM videos WHERE author = :username");
60
        $stmt->bindParam(":username", $user);
61
        $stmt->execute();
62
        
63
        $views = 0;
64
        while($video = $stmt->fetch(PDO::FETCH_ASSOC)) { 
65
            $views = $views + $this->fetch_video_views($video['rid']);
66
        }
67
        return $views;
68
        $stmt->close();
0 ignored issues
show
Unused Code introduced by
$stmt->close() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
69
    }
70
71
    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...
72
        $stmt = $this->__db->prepare("SELECT * FROM comments WHERE toid = :id");
73
        $stmt->bindParam(":id", $id);
74
        $stmt->execute();
75
    
76
        return $stmt->rowCount();
77
    }
78
79
    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...
80
        $description = trim($description);
81
        if(strlen($description) >= $limit) {
82
            $description = substr($description, 0, $limit) . "...";
83
        } 
84
85
        $description = htmlspecialchars($description);
86
        if($newlines) { $description = str_replace(PHP_EOL, "<br>", $description); }
87
        return $description;
88
    }
89
90
    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...
91
        $stmt = $this->__db->prepare("SELECT * FROM video_response WHERE toid = :id");
92
        $stmt->bindParam(":id", $id);
93
        $stmt->execute();
94
    
95
        return $stmt->rowCount();
96
    }
97
98
    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...
99
        if($liked) {
100
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE reciever = :reciever AND type = 'l'");
101
            $stmt->bindParam(":reciever", $reciever);
102
            $stmt->execute();
103
104
            return $stmt->rowCount();
105
        } else {
106
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE reciever = :reciever AND type = 'd'");
107
            $stmt->bindParam(":reciever", $reciever);
108
            $stmt->execute();
109
110
            return $stmt->rowCount();
111
        }
112
    }
113
114
    function check_view($vidid, $user) {
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...
115
        $stmt = $this->__db->prepare("SELECT * FROM views WHERE viewer = :viewer AND videoid = :rid");
116
        $stmt->bindParam(":viewer", $user);
117
        $stmt->bindParam(":rid", $vidid);
118
        $stmt->execute();
119
        if($stmt->rowCount() === 0) {
120
            $this->add_view($vidid, $user);
121
        }
122
    }
123
124
    function add_view($vidid, $user) {
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...
125
        $stmt = $this->__db->prepare("INSERT INTO views (viewer, videoid) VALUES (:user, :vidid)");
126
        $stmt->bindParam(":user", $user);
127
        $stmt->bindParam(":vidid", $vidid);
128
        $stmt->execute();
129
    }
130
131
    function get_comment_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...
132
        if($liked) {
133
            $stmt = $this->__db->prepare("SELECT `sender` FROM comment_likes WHERE reciever = :reciever AND type = 'l'");
134
            $stmt->bindParam(":reciever", $reciever);
135
            $stmt->execute();
136
137
            return $stmt->rowCount();
138
        } else {
139
            $stmt = $this->__db->prepare("SELECT `sender` FROM comment_likes WHERE reciever = :reciever AND type = 'd'");
140
            $stmt->bindParam(":reciever", $reciever);
141
            $stmt->execute();
142
143
            return $stmt->rowCount();
144
        }
145
    }
146
147
    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...
148
        if($liked) {
149
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE sender = :sender AND reciever = :reciever AND type = 'l'");
150
            $stmt->bindParam(":sender", $user);
151
            $stmt->bindParam(":reciever", $reciever);
152
            $stmt->execute();
153
154
            return $stmt->rowCount() === 1;
155
        } else {
156
            $stmt = $this->__db->prepare("SELECT `sender` FROM likes WHERE sender = :sender AND reciever = :reciever AND type = 'd'");
157
            $stmt->bindParam(":sender", $user);
158
            $stmt->bindParam(":reciever", $reciever);
159
            $stmt->execute();
160
161
            return $stmt->rowCount() === 1;
162
        }
163
    }
164
165
    function if_comment_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...
166
        if($liked) {
167
            $stmt = $this->__db->prepare("SELECT `sender` FROM comment_likes WHERE sender = :sender AND reciever = :reciever AND type = 'l'");
168
            $stmt->bindParam(":sender", $user);
169
            $stmt->bindParam(":reciever", $reciever);
170
            $stmt->execute();
171
172
            return $stmt->rowCount() === 1;
173
        } else {
174
            $stmt = $this->__db->prepare("SELECT `sender` FROM comment_likes WHERE sender = :sender AND reciever = :reciever AND type = 'd'");
175
            $stmt->bindParam(":sender", $user);
176
            $stmt->bindParam(":reciever", $reciever);
177
            $stmt->execute();
178
179
            return $stmt->rowCount() === 1;
180
        }
181
    }
182
183
    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...
184
        $stmt = $this->__db->prepare("SELECT * FROM videos WHERE rid = :rid");
185
        $stmt->bindParam(":rid", $rid);
186
        $stmt->execute();
187
188
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
189
    }
190
191
    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...
192
        $stmt = $this->__db->prepare("SELECT * FROM playlists WHERE rid = :rid");
193
        $stmt->bindParam(":rid", $rid);
194
        $stmt->execute();
195
196
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
197
    }
198
199
    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...
200
        $stmt = $this->__db->prepare("SELECT * FROM comments WHERE id = :id");
201
        $stmt->bindParam(":id", $id);
202
        $stmt->execute();
203
204
        return ($stmt->rowCount() === 0 ? 0 : $stmt->fetch(PDO::FETCH_ASSOC));
205
    }
206
207
    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...
208
        $stmt = $this->__db->prepare("SELECT * FROM stars WHERE reciever = :id AND type = :lvl");
209
        $stmt->bindParam(":id", $id);
210
        $stmt->bindParam(":lvl", $level, PDO::PARAM_INT);
211
        $stmt->execute();
212
    
213
        return $stmt->rowCount();
214
    }
215
216
    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...
217
        $stmt = $this->__db->prepare("SELECT rid FROM videos WHERE rid = :rid");
218
        $stmt->bindParam(":rid", $video);
219
        $stmt->execute();
220
        
221
        return $stmt->rowCount() === 1;
222
    }
223
224
    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...
225
        $stmt = $this->__db->prepare("SELECT `reciever` FROM favorite_video WHERE sender = :user AND reciever = :reciever");
226
        $stmt->bindParam(":user", $user);
227
        $stmt->bindParam(":reciever", $reciever);
228
        $stmt->execute();
229
        
230
        return $stmt->rowCount() === 1;
231
    }
232
233
    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...
234
        $stmt = $this->__db->prepare("SELECT rid FROM videos WHERE author = :v");
235
        $stmt->bindParam(":v", $v);
236
        $stmt->execute(); 
237
    
238
        return $stmt->rowCount();
239
    }
240
}
241
242
243
/* SHITTY FIX INCOMING FOR EMBEDS - MOVE SO IT'S IN EVERY PAGE WHEN POSSIBLE AND NOT LAZY */
244
$__server->page_embeds->page_title = "SubRocks - " . $__server->page_title;
245
$__server->page_embeds->page_description = "Welcome to a Youtube recreation dedicated to replecating 2012's YouTube layout.";
246
$__server->page_embeds->page_url = "https://subrock.rocks/";
247
?>
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...