ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatSmilies.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35  private static function _setupDatabase() {
36  global $ilDB;
37  $fields = array(
38  'smiley_id' => array(
39  'type' => 'integer',
40  'length' => 4,
41  ),
42  'smiley_keywords' => array(
43  'type' => 'text',
44  'length' => 100,
45  ),
46  'smiley_path' => array(
47  'type' => 'text',
48  'length' => 200,
49  )
50  );
51 
52  $ilDB->dropTable("chat_smilies");
53  $ilDB->dropTable("chat_smilies_seq");
54 
55  $ilDB->createTable('chat_smilies', $fields);
56  $ilDB->addPrimaryKey('chat_smilies', array('smiley_id'));
57  $ilDB->createSequence('chat_smilies');
58  }
59 
63  private static function _insertDefaultValues() {
64  global $ilDB;
65  $values = array (
66  array("icon_smile.gif", ":)\n:-)\n:smile:"),
67  array("icon_wink.gif", ";)\n;-)\n:wink:"),
68  array("icon_laugh.gif", ":D\n:-D\n:laugh:\n:grin:\n:biggrin:"),
69  array("icon_sad.gif", ":(\n:-(\n:sad:"),
70  array("icon_shocked.gif", ":o\n:-o\n:shocked:"),
71  array("icon_tongue.gif", ":p\n:-p\n:tongue:"),
72  array("icon_cool.gif", ":cool:"),
73  array("icon_eek.gif", ":eek:"),
74  array("icon_angry.gif", ":||\n:-||\n:angry:"),
75  array("icon_flush.gif", ":flush:"),
76  array("icon_idea.gif", ":idea:"),
77  array("icon_thumbup.gif", ":thumbup:"),
78  array("icon_thumbdown.gif", ":thumbdown:"),
79  );
80 
81  $stmt = $ilDB->prepare("
82  INSERT INTO chat_smilies (smiley_id, smiley_keywords, smiley_path)
83  VALUES (?, ?, ?)",
84  array(
85  "integer", "text", "text"
86  )
87  );
88 
89  foreach($values as $val) {
90  $row = array(
91  $ilDB->nextID("chat_smilies"),
92  $val[1],
93  $val[0]
94  );
95  $stmt->execute($row);
96  }
97  }
98 
102  private static function _setupFolder() {
103 
104  $path = ilUtil::getWebspaceDir().'/chat/smilies';
105 
106  if (!is_dir($path)) {
107  mkdir($path, 0755, true);
108  }
109  }
110 
114  public static function _getSmileyDir() {
115  return ilUtil::getWebspaceDir().'/chat/smilies';
116  }
117 
121  public static function _initial() {
125  }
126 
132  public static function _checkSetup()
133  {
134  global $lng;
136  if (!is_dir($path))
137  {
138  ilUtil::sendInfo($lng->txt('chat_smilies_dir_not_exists'));
140  if (!is_dir($path))
141  {
142  ilUtil::sendFailure($lng->txt('chat_smilies_dir_not_available'));
143  return false;
144  }
145  else
146  {
147  $smilies = array
148  (
149  "icon_smile.gif",
150  "icon_wink.gif",
151  "icon_laugh.gif",
152  "icon_sad.gif",
153  "icon_shocked.gif",
154  "icon_tongue.gif",
155  "icon_cool.gif",
156  "icon_eek.gif",
157  "icon_angry.gif",
158  "icon_flush.gif",
159  "icon_idea.gif",
160  "icon_thumbup.gif",
161  "icon_thumbdown.gif",
162  );
163 
164  foreach($smilies as $smiley)
165  {
166  copy("templates/default/images/emoticons/$smiley", $path . "/$smiley");
167  }
168 
170 
171  ilUtil::sendSuccess($lng->txt('chat_smilies_initialized'));
172  }
173 
174  }
175 
176  if (!is_writable($path))
177  {
178  ilUtil::sendInfo($lng->txt('chat_smilies_dir_not_writable'));
179  }
180 
181  return true;
182  }
183 
187  public static function _getSmilies() {
188  global $ilDB;
189 
190  $res = $ilDB->query("SELECT smiley_id, smiley_keywords, smiley_path FROM chat_smilies");
191  $result = array();
192 
193  for ($i = 0; $i < $res->numRows(); $i++) {
194  $tmp = $res->fetchRow();
195  $result[] = array(
196  "smiley_id" => $tmp[0],
197  "smiley_keywords" => $tmp[1],
198  "smiley_path" => $tmp[2],
199  "smiley_fullpath" => ilUtil::getWebspaceDir().'/chat/smilies/'.$tmp[2]
200  );
201  }
202  return $result;
203  }
204 
211  public static function _getSmiley($a_id) {
212  global $ilDB;
213 
214  $res = $ilDB->queryF("
215  SELECT smiley_id, smiley_keywords, smiley_path
216  FROM chat_smilies
217  WHERE smiley_id = %s
218  ", array('integer'), array($a_id));
219 
220  if ($res->numRows()) {
221  $tmp = $res->fetchRow();
222  $result = array(
223  "smiley_id" => $tmp[0],
224  "smiley_keywords" => $tmp[1],
225  "smiley_path" => $tmp[2],
226  "smiley_fullpath" => ilUtil::getWebspaceDir().'/chat/smilies/'.$tmp[2]
227  );
228  return $result;
229  }
230  throw new Exception('smiley with id $a_id not found');
231  }
232 
233  public static function _getSmiliesById($ids = array()) {
234  global $ilDB;
235 
236  if (!count($ids)) return;
237 
238  $sql = "SELECT smiley_id, smiley_keywords, smiley_path FROM chat_smilies WHERE ";
239 
240  $sql_parts = array();
241  foreach($ids as $id) {
242  $sql_parts[] .= "smiley_id = " . $ilDB->quote($id, "integer");
243  }
244 
245  $sql .= join(" OR ", $sql_parts);
246 
247  $res = $ilDB->query($sql);
248  $result = array();
249  for($i = 0; $i < $res->numRows(); $i++) {
250  $tmp = $res->fetchRow();
251  $result[] = array(
252  "smiley_id" => $tmp[0],
253  "smiley_keywords" => $tmp[1],
254  "smiley_path" => $tmp[2],
255  "smiley_fullpath" => ilUtil::getWebspaceDir().'/chat/smilies/'.$tmp[2]
256  );
257  }
258  return $result;
259  }
260 
261  public static function _deleteMultipleSmilies($ids = array()) {
262  global $ilDB;
263  $smilies = self::_getSmiliesById($ids);
264  if (count($smilies) <= 0)
265  return;
266 
267  $sql_parts = array();
268 
269  foreach($smilies as $s) {
270  unlink($s["smiley_fullpath"]);
271  $sql_parts[] = "smiley_id = " . $ilDB->quote($s["smiley_id"],'integer');
272  }
273 
274  $ilDB->manipulate("DELETE FROM chat_smilies WHERE " . join(" OR ", $sql_parts) );
275  }
276 
277  public static function _updateSmiley($data) {
278  global $ilDB;
279 
280  $ilDB->manipulateF(
281  "UPDATE chat_smilies
282  SET smiley_keywords = %s
283  WHERE
284  smiley_id = %s",
285  array('text', 'integer'),
286  array($data["smiley_keywords"], $data["smiley_id"])
287  );
288 
289  if ($data["smiley_path"]) {
290  $sm = self::_getSmiley($data["smiley_id"]);
291  unlink($sm["smiley_fullpath"]);
292  $ilDB->manipulateF(
293  "UPDATE chat_smilies
294  SET smiley_path = %s
295  WHERE
296  smiley_id = %s",
297  array('text', 'integer'),
298  array($data["smiley_path"], $data["smiley_id"])
299  );
300  }
301 
302  }
303 
304  public static function _getSmiliesBasePath() {
305  return ilUtil::getWebspaceDir().'/chat/smilies/';
306  }
307 
308  public static function _deleteSmiley($a_id) {
309  global $ilDB;
310 
311  try {
312  $smiley = self::_getSmiley($a_id);
313  $path = ilUtil::getWebspaceDir().'/chat/smilies/'.$smiley["smiley_path"];
314  if (is_file($path))
315  unlink($path);
316 
317  $ilDB->manipulateF(
318  "DELETE FROM chat_smilies
319  WHERE
320  smiley_id = %s",
321  array('integer'),
322  array($a_id)
323  );
324  }
325  catch(Exception $e) {
326 
327  }
328  }
329 
330  public static function _storeSmiley($keywords, $path) {
331  global $ilDB;
332 
333  $stmt = $ilDB->prepare("
334  INSERT INTO chat_smilies (smiley_id, smiley_keywords, smiley_path)
335  VALUES (?, ?, ?)",
336  array(
337  "integer", "text", "text"
338  )
339  );
340  $row = array(
341  $ilDB->nextID("chat_smilies"),
342  $keywords,
343  $path
344  );
345  $stmt->execute($row);
346  }
347 
348  public static function _prepareKeywords($words) {
349  $keywordscheck = true;
350 
351  // check keywords
352  $keywords_unchecked = explode("\n", $words);
353  if (count($keywords_unchecked) <= 0)
354  $keywordscheck = false;
355 
356  if ($keywordscheck) {
357  $keywords = array();
358  foreach($keywords_unchecked as $word) {
359  if (trim($word))
360  $keywords[] = trim($word);
361  }
362  }
363 
364  if ($keywordscheck && count($keywords) <= 0)
365  $keywordscheck = false;
366 
367  if ($keywordscheck)
368  return $keywords;
369  else
370  return array();
371  }
372 
373  public static function _parseString($str) {
374  global $ilDB;
375 
376  $q = $ilDB->query(
377  "SELECT smiley_keywords, smiley_path
378  FROM chat_smilies"
379  );
380 
381  $ar_search = array();
382  $ar_replace = array();
383 
384  $ostr = "";
385  for($i = 0; $i < $q->numRows(); $i++) {
386  $row = $q->fetchRow();
387  $keywords = explode("\n", $row[0]);
388 
389  for ($x = 0; $x < count($keywords); $x++) {
390  $ar_search[] = $keywords[$x];
391 
392  $tpl = new ilTemplate("tpl.chat_smiley_line.html", true, true, "Modules/Chat");
393  $tpl->setVariable("SMILEY_PATH", ilUtil::getHtmlPath(self::_getSmiliesBasePath().$row[1]));
394  $tpl->setVariable("SMILEY_ALT", urlencode($keywords[$x]));
395  $tpl->parseCurrentBlock();
396 
397  $ar_replace[] = $tpl->get();
398 
399  }
400  }
401 
402  $str = str_replace($ar_search, $ar_replace, $str);
403  return $str;
404  }
405 }
406 
407 ?>