1 | <?php |
||||
2 | /* For licensing terms, see /license.txt */ |
||||
3 | |||||
4 | /* To show the plugin course icons you need to add these icons in the main/img Chamilo platform |
||||
5 | * main/img/icons/22/plugin_name.png |
||||
6 | * main/img/icons/64/plugin_name.png |
||||
7 | * main/img/icons/64/plugin_name_na.png |
||||
8 | */ |
||||
9 | |||||
10 | /** |
||||
11 | * Class OLPC_Peru_FilterPlugin. |
||||
12 | */ |
||||
13 | class OLPC_Peru_FilterPlugin extends Plugin |
||||
14 | { |
||||
15 | public $blacklist_enabled_file = '/var/sqg/blacklists'; |
||||
16 | public $blacklists_dir = '/var/squidGuard/blacklists'; |
||||
17 | public $isCoursePlugin = true; |
||||
18 | // When creating a new course, these settings are added to the course |
||||
19 | public $course_settings = []; |
||||
20 | public $course_settings_callback = true; |
||||
21 | public $error = ''; |
||||
22 | |||||
23 | protected function __construct() |
||||
24 | { |
||||
25 | parent::__construct('0.1', 'Yannick Warnier, Aliosh Neira', ['tool_enable' => 'boolean']); |
||||
26 | |||||
27 | $this->course_settings = []; |
||||
28 | $list = $this->get_blacklist_options(); |
||||
29 | foreach ($list as $k => $v) { |
||||
30 | $this->course_settings[] = |
||||
31 | ['group' => 'olpc_peru_filter_filter', 'name' => $k, 'type' => 'checkbox', 'init_value' => $v]; |
||||
32 | } |
||||
33 | require_once __DIR__.'/../config.php'; |
||||
34 | if (!empty($blacklist_enabled_file)) { |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||||
35 | $this->blacklist_enabled_file = $blacklist_enabled_file; |
||||
36 | } |
||||
37 | if (!empty($blacklists_dir)) { |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
38 | $this->blacklists_dir = $blacklists_dir; |
||||
39 | } |
||||
40 | } |
||||
41 | |||||
42 | public static function create() |
||||
43 | { |
||||
44 | static $result = null; |
||||
45 | |||||
46 | return $result ? $result : $result = new self(); |
||||
47 | } |
||||
48 | |||||
49 | public function install() |
||||
50 | { |
||||
51 | //Installing course settings |
||||
52 | $this->install_course_fields_in_all_courses(false); |
||||
53 | } |
||||
54 | |||||
55 | public function uninstall() |
||||
56 | { |
||||
57 | //Deleting course settings |
||||
58 | $this->uninstall_course_fields_in_all_courses(); |
||||
59 | } |
||||
60 | |||||
61 | /** |
||||
62 | * Caller for the install_course_fields() function. |
||||
63 | * |
||||
64 | * @param int The course's integer ID |
||||
65 | * @param bool Whether to add a tool link on the course homepage |
||||
66 | */ |
||||
67 | public function course_install($course_id, $add_tool_link = true) |
||||
68 | { |
||||
69 | //force ignoring the tools table insertion for this plugin |
||||
70 | $this->install_course_fields($course_id, false); |
||||
71 | } |
||||
72 | |||||
73 | public function course_settings_updated($values = []) |
||||
74 | { |
||||
75 | if (!is_array($values) or count($values) == 0) { |
||||
76 | return false; |
||||
77 | } |
||||
78 | $this->set_blacklist_options($values['olpc_peru_filter_filter']); |
||||
79 | } |
||||
80 | |||||
81 | /** |
||||
82 | * Get a list of options (checked and unchecked) for blacklists as coming |
||||
83 | * from the Squid files. |
||||
84 | */ |
||||
85 | public function get_blacklist_options() |
||||
86 | { |
||||
87 | $categories = $blacklists = []; |
||||
88 | if (!is_dir($this->blacklists_dir)) { |
||||
89 | $this->error = 'Could not find blacklists dir '.$this->blacklists_dir; |
||||
0 ignored issues
–
show
Are you sure
$this->blacklists_dir of type mixed can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
90 | |||||
91 | return $blacklists; |
||||
92 | } |
||||
93 | if (!is_file($this->blacklist_enabled_file)) { |
||||
94 | $this->error = 'Could not find blacklists dir '.$this->blacklists_dir; |
||||
95 | |||||
96 | return $blacklists; |
||||
97 | } |
||||
98 | $list = scandir($this->blacklists_dir); |
||||
99 | foreach ($list as $file) { |
||||
100 | if (substr($file, 0, 1) == '.' or $file == 'custom_blacklist' or is_dir($this->blacklists_dir.'/'.$file)) { |
||||
101 | continue; |
||||
102 | } |
||||
103 | $categories[] = $file; |
||||
104 | } |
||||
105 | sort($categories); |
||||
106 | $current_blacklist = file($this->blacklist_enabled_file); |
||||
107 | $current_blacklist = array_map('trim', $current_blacklist); |
||||
108 | foreach ($categories as $category) { |
||||
109 | foreach ($current_blacklist as $blacklisted) { |
||||
110 | $checked = 0; |
||||
111 | if ($category == trim($blacklisted)) { |
||||
112 | $checked = 1; |
||||
113 | $blacklists[$category] = $checked; |
||||
114 | break; |
||||
115 | } |
||||
116 | $blacklists[$category] = $checked; |
||||
117 | } |
||||
118 | } |
||||
119 | |||||
120 | return $blacklists; |
||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * Given an array of blacklist => 0/1, save the new blacklist file to disk. |
||||
125 | * |
||||
126 | * @param array Array of blacklists names |
||||
127 | * |
||||
128 | * @return bool False on error, True on success |
||||
129 | */ |
||||
130 | public function set_blacklist_options($values) |
||||
131 | { |
||||
132 | if (!is_array($values)) { |
||||
133 | return false; |
||||
134 | } |
||||
135 | if (!is_writeable($this->blacklist_enabled_file)) { |
||||
136 | return false; |
||||
137 | } |
||||
138 | $new_blacklist = ''; |
||||
139 | foreach ($values as $k => $v) { |
||||
140 | if ($v) { |
||||
141 | $new_blacklist .= $k."\n"; |
||||
142 | } |
||||
143 | } |
||||
144 | $r = @file_put_contents($this->blacklist_enabled_file, $new_blacklist); |
||||
145 | //todo check the value or $r in $php_errormsg |
||||
146 | return true; |
||||
147 | } |
||||
148 | } |
||||
149 |