Merge branch 'master' of ssh://jonik@koulukas/var/git/leiska
[irreco.git] / database / php / irreco_webdb_server.php
blob4460191f86c5d4bf026f68ce7e933e5b2b422dc5
1 <?php
3 /*
4 * irreco - Ir Remote Control
5 * Copyright (C) 2007,2008 Arto Karppinen (arto.karppinen@iki.fi),
6 * Joni Kokko (t5kojo01@students.oamk.fi),
7 * Sami Mäki (kasmra@xob.kapsi.fi),
8 * Harri Vattulainen (t5vaha01@students.oamk.fi)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 require_once 'irreco_webdb_log.php';
26 require_once 'XML/RPC2/Server.php';
28 class IrrecoWebdbServer
31 const ADD_USER_ERROR_MSG = "Adding user failed.";
32 const UPLOAD_CONFIGURATION_ERROR_MSG = "Uploading configuration\nfailed.";
33 const USERNAME_AND_PASSWORD_MISMATCH_MSG = "Username and password mismatch.";
34 const FILE_ALREADY_EXIST_MSG = "Database already contains identical device.";
35 const HASH_AND_DATA_MISMATCH_MSG = "File hash and file data don't match.";
36 const FAULTCODE_ERROR_MSG = "\nInvalid string faultCode.";
37 const LOGIN_ERROR_MSG = "Login failed.";
38 const EMPTY_DB_MSG = "Database is empty.";
39 const NO_THEMES_OF_REMOTE_MSG = "Can't find any theme of remote.";
40 const NO_CONFIGURATIONS_OF_REMOTE_MSG = "Can't find any configuration of remote.";
42 const ADD_USER_ERROR = 10001;
43 const UPLOAD_CONFIGURATION_ERROR = 10002;
44 const USERNAME_AND_PASSWORD_MISMATCH = 10003;
45 const FILE_ALREADY_EXIST = 10004;
46 const HASH_AND_DATA_MISMATCH = 10005;
47 const FAULTCODE_ERROR = 10006;
48 const LOGIN_ERROR = 10007;
49 const EMPTY_DB_ERROR = 10008;
50 const NO_THEMES_OF_REMOTE_ERROR = 10009;
51 const NO_CONFIGURATIONS_OF_REMOTE_ERROR = 10010;
53 private $database;
55 public function __construct($database)
57 $this->database = $database;
60 /**
61 * Sum two numbers.
63 * @param int num_a
64 * @param int num_b
65 * @return int Sum of integers
67 public function sum($num_a, $num_b)
69 IrrecoLog::$server->log('XML-RPC sum');
70 return $num_a + $num_b;
73 /**
74 * Add user to database
76 * Returns TRUE if user is added successfully to database.
78 * @param string name Username
79 * @param string email full email address
80 * @param string passwd sha1-hash of password
81 * @return boolean User added successfully
83 function addUser($name, $email, $passwd)
85 IrrecoLog::$server->log('XML-RPC addUser');
86 $table = 'user';
88 /* Check for faultCode */
89 if(strstr($name, "faultCode")) {
90 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
91 self::FAULTCODE_ERROR);
93 if(strstr($email, "faultCode")) {
94 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
95 self::FAULTCODE_ERROR);
98 /* Check parameters here */
99 if(strlen($name) < 6) {
100 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
101 self::ADD_USER_ERROR);
104 if(strlen($email) < 6) {
105 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
106 self::ADD_USER_ERROR);
109 if(strlen($passwd) < 6) {
110 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
111 self::ADD_USER_ERROR);
114 if(!strstr($email, "@")) {
115 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
116 self::ADD_USER_ERROR);
119 if(!strstr($email, ".")) {
120 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
121 self::ADD_USER_ERROR);
124 $data = $this->database->addUser($name, $email, $passwd, $table);
126 if ($data == FALSE) {
127 /* if failed */
128 throw new XML_RPC2_FaultException(self::ADD_USER_ERROR_MSG,
129 self::ADD_USER_ERROR);
131 return $data;
135 * Add configuration to database
137 * Returns error message or 'configuration added successfully' as string
140 * @param string backend Name for Backend. e.g. "IRTrans Transceiver"
141 * @param string category Name for Category. e.g. "Tv"
142 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
143 * @param string model Name for Model. e.g. "xyz"
144 * @param string user Username
145 * @param string password sha1-hash of password
146 * @param string file_hash sha1-hash of file_data
147 * @param string file_name Name for File
148 * @param string file_data Content of file
149 * @return string
151 function uploadConfiguration($backend,
152 $category,
153 $manufacturer,
154 $model,
155 $user,
156 $password,
157 $file_hash,
158 $file_name,
159 $file_data)
161 IrrecoLog::$server->log('XML-RPC uploadConfiguration');
163 /* Check for faultCode */
164 if(strstr($backend, "faultCode")) {
165 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
166 self::FAULTCODE_ERROR);
168 if(strstr($category, "faultCode")) {
169 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
170 self::FAULTCODE_ERROR);
172 if(strstr($manufacturer, "faultCode")) {
173 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
174 self::FAULTCODE_ERROR);
176 if(strstr($model, "faultCode")) {
177 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
178 self::FAULTCODE_ERROR);
180 if(strstr($user, "faultCode")) {
181 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
182 self::FAULTCODE_ERROR);
184 if(strstr($file_hash, "faultCode")) {
185 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
186 self::FAULTCODE_ERROR);
188 if(strstr($file_name, "faultCode")) {
189 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
190 self::FAULTCODE_ERROR);
192 if(strstr($file_data, "faultCode")) {
193 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
194 self::FAULTCODE_ERROR);
197 /* Check parameters here */
198 if(strlen($backend) == 0) {
199 throw new XML_RPC2_FaultException(
200 self::UPLOAD_CONFIGURATION_ERROR_MSG,
201 self::UPLOAD_CONFIGURATION_ERROR);
204 if(strlen($category) == 0) {
205 throw new XML_RPC2_FaultException(
206 self::UPLOAD_CONFIGURATION_ERROR_MSG,
207 self::UPLOAD_CONFIGURATION_ERROR);
210 if(strlen($manufacturer) == 0) {
211 throw new XML_RPC2_FaultException(
212 self::UPLOAD_CONFIGURATION_ERROR_MSG,
213 self::UPLOAD_CONFIGURATION_ERROR);
216 if(strlen($model) == 0) {
217 throw new XML_RPC2_FaultException(
218 self::UPLOAD_CONFIGURATION_ERROR_MSG,
219 self::UPLOAD_CONFIGURATION_ERROR);
222 if(strlen($user) < 6) {
223 throw new XML_RPC2_FaultException(
224 self::UPLOAD_CONFIGURATION_ERROR_MSG,
225 self::UPLOAD_CONFIGURATION_ERROR);
228 if(strlen($password) < 6) {
229 throw new XML_RPC2_FaultException(
230 self::UPLOAD_CONFIGURATION_ERROR_MSG,
231 self::UPLOAD_CONFIGURATION_ERROR);
234 if(strlen($file_hash) != 40) {
235 throw new XML_RPC2_FaultException(
236 self::UPLOAD_CONFIGURATION_ERROR_MSG,
237 self::UPLOAD_CONFIGURATION_ERROR);
240 /* try to add data to db */
241 $table = 'configuration';
242 $return = $this->database->uploadConfiguration($backend,
243 $category,
244 $manufacturer,
245 $model,
246 $user,
247 $password,
248 $file_hash,
249 $file_name,
250 $file_data);
252 if ($return == 'username and password mismatch.') {
253 throw new XML_RPC2_FaultException(
254 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
255 self::USERNAME_AND_PASSWORD_MISMATCH);
257 else if ($return == 'file already exist.') {
258 throw new XML_RPC2_FaultException(
259 self::FILE_ALREADY_EXIST_MSG,
260 self::FILE_ALREADY_EXIST);
262 else if ($return == 'file hash and file data mismatch.') {
263 throw new XML_RPC2_FaultException(
264 self::HASH_AND_DATA_MISMATCH_MSG,
265 self::HASH_AND_DATA_MISMATCH);
268 return $return;
272 * Get list of categories that have configurations in them.
274 * @return array
276 function getCategories()
278 IrrecoLog::$server->log('XML-RPC getCategories');
279 $data = $this->database->getCategoryList();
281 $array = array();
282 foreach ($data as $item) {
283 array_push($array, $item->name);
286 IrrecoLog::$database->log("Category list:\n" .
287 IrrecoLog::getVarDump($array));
288 return $array;
291 * Get list of all categories. Even those that don't have
292 * configurations in them.
294 * @return array
296 function getAllCategories()
298 IrrecoLog::$server->log('XML-RPC getAllCategories');
299 $data = $this->database->getWholeCategoryList();
301 $array = array();
302 foreach ($data as $item) {
303 array_push($array, $item->name);
306 IrrecoLog::$database->log("Category list:\n" .
307 IrrecoLog::getVarDump($array));
308 return $array;
312 * Get list of manufacturers for category.
314 * @param string category Name for Category. e.g. "Tv"
315 * @return array
317 function getManufacturers($category)
319 IrrecoLog::$server->log('XML-RPC getManufacturers');
320 $data = $this->database->getManufacturerList($category);
322 $array = array();
323 foreach ($data as $item) {
324 array_push($array, $item->name);
327 IrrecoLog::$database->log("Manufacturer list:\n" .
328 IrrecoLog::getVarDump($array));
329 return $array;
333 * Get list of all manufacturers. Even those that don't have
334 * configurations in them.
336 * @return array
338 function getAllManufacturers()
340 IrrecoLog::$server->log('XML-RPC getAllManufacturers');
341 $data = $this->database->getWholeManufacturerList();
343 $array = array();
344 foreach ($data as $item) {
345 array_push($array, $item->name);
348 IrrecoLog::$database->log("Manufacturer list:\n" .
349 IrrecoLog::getVarDump($array));
350 return $array;
354 * Get list of models by manufacturer in selected category.
356 * @param string category Name for Category. e.g. "Tv"
357 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
358 * @return array
360 function getModels($category,$manufacturer)
362 IrrecoLog::$server->log('XML-RPC getModels');
363 $data = $this->database->getModelList($category,$manufacturer);
365 $array = array();
366 foreach ($data as $item) {
367 array_push($array, $item->model);
370 IrrecoLog::$database->log("Model list:\n" .
371 IrrecoLog::getVarDump($array));
372 return $array;
377 * Get id list for configurations by model.
379 * This function is deprecated. Use the getConfigurations function
380 * instead.
381 * @param string model Name for Model. e.g. "xyz"
382 * @return array
384 function getConfigs($model)
386 IrrecoLog::$server->log('XML-RPC getConfigs');
387 $data = $this->database->getConfigs($model);
388 $array = array();
389 foreach ($data as $item) {
390 array_push($array, $item->id);
393 IrrecoLog::$database->log("Config id:\n" .
394 IrrecoLog::getVarDump($array));
395 return $array;
399 * Get id list for configurations by model and manufacturer
401 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
402 * @param string model Name for Model. e.g. "xyz"
403 * @return array
405 function getConfigurations($manufacturer, $model)
407 IrrecoLog::$server->log('XML-RPC getConfigurations');
408 $data = $this->database->getConfigurations($manufacturer, $model);
410 $array = array();
411 foreach ($data as $item) {
412 array_push($array, $item->id);
415 IrrecoLog::$database->log("Config id:\n" .
416 IrrecoLog::getVarDump($array));
417 return $array;
421 * Get all data from configuration
423 * Returns user, backend, category, manufacturer,
424 * model, file_hash, file_name, uploaded, download_count
426 * This function is deprecated. Use the getConfigurationById function
427 * instead.
428 * @param string id id-number for configuration.
429 * @return struct
431 function getConfiguration($id)
433 IrrecoLog::$server->log('XML-RPC getConfiguration');
434 $data = $this->database->getConfiguration($id);
435 $array = array();
437 $array['user'] = $data[0]->user;
438 $array['backend'] = $data[0]->backend;
439 $array['category'] = $data[0]->category;
440 $array['manufacturer'] = $data[0]->manufacturer;
441 $array['model'] = $data[0]->model;
442 $array['file_hash'] = $data[0]->file_hash;
443 $array['file_name'] = $data[0]->file_name;
444 $array['uploaded'] = $data[0]->uploaded;
445 $array['download_count'] = $data[0]->download_count;
447 IrrecoLog::$database->log("Configuration:\n" .
448 IrrecoLog::getVarDump($array));
449 return $array;
453 * Get all data from configuration
455 * Returns user, backend, category, manufacturer,
456 * model, file_hash, file_name, uploaded, download_count
457 * @param int id id-number for configuration.
458 * @return struct
460 function getConfigurationById($id)
462 IrrecoLog::$server->log('XML-RPC getConfigurationById');
463 $data = $this->database->getConfiguration($id);
464 $array = array();
466 $array['user'] = $data[0]->user;
467 $array['backend'] = $data[0]->backend;
468 $array['category'] = $data[0]->category;
469 $array['manufacturer'] = $data[0]->manufacturer;
470 $array['model'] = $data[0]->model;
471 $array['file_hash'] = $data[0]->file_hash;
472 $array['file_name'] = $data[0]->file_name;
473 $array['uploaded'] = $data[0]->uploaded;
474 $array['download_count'] = $data[0]->download_count;
476 IrrecoLog::$database->log("Configuration:\n" .
477 IrrecoLog::getVarDump($array));
478 return $array;
482 * Get configuration_id by name and date.
484 * Returns configuration_id or 0 if configuration does not exists.
485 * @param string file_hash sha1-hash of file_data
486 * @param string file_name Name for File
487 * @return int
489 function getConfigIdByFilehashAndFilename($file_hash, $file_name)
491 IrrecoLog::$server->log(
492 'XML-RPC getConfigIdByFilehashAndFilename');
493 $id = $this->database->getConfigIdByFilehashAndFilename(
494 $file_hash, $file_name);
496 IrrecoLog::$server->log("Configuration_id: ". $id);
497 return $id;
501 * Get File
504 * Returns content of file
505 * @param string hash sha1-hash of file_data
506 * @param string name Name for File
507 * @return struct
509 function getFile($hash,$name)
511 IrrecoLog::$server->log('XML-RPC getFile');
512 $data = $this->database->getFileData($hash,$name);
514 $array = array();
516 $array['data']= $data[0]->data;
518 IrrecoLog::$database->log("Data:\n" .
519 IrrecoLog::getVarDump($array));
520 return $array;
525 * Get whether user already exists
527 * Returns TRUE if given name is already in database.
528 * Used at irreco_webdb_register_dlg when adding new user.
530 * @param string name Username
531 * @return boolean User exists
533 function getUserExists($name)
535 IrrecoLog::$server->log('XML-RPC getUserExists');
536 $table = 'user';
537 $data = $this->database->getNameId($table, $name);
539 if ($data == "") {
540 return FALSE;
541 } else {
542 return TRUE;
547 * Create new Theme
549 * Returns theme_id
550 * @param string name Name of Theme
551 * @param string comment
552 * @param string preview_button name of preview button
553 * @param string folder name of themefolder
554 * @param string user Username
555 * @param string password sha1-hash of password
556 * @return int
558 function createNewTheme($name, $comment, $preview_button,
559 $folder, $user, $password)
561 IrrecoLog::$server->log('XML-RPC createNewTheme');
562 $theme_id = $this->database->createNewTheme($name, $comment,
563 $preview_button,
564 $folder, $user,
565 $password);
567 /* TODO when catching of exeptions is correctly handled */
568 /* it might be better to throw exeption, until then.. */
569 /*if ($theme_id == 'username and password mismatch.') {
570 throw new XML_RPC2_FaultException(
571 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
572 self::USERNAME_AND_PASSWORD_MISMATCH);
575 if ($theme_id == -1) {
576 IrrecoLog::$server->log('Username & password mismatch');
577 } elseif ($theme_id == -2) {
578 IrrecoLog::$server->log('No rights to themename');
581 IrrecoLog::$database->log("Theme_id: " . $theme_id . "\n");
582 return $theme_id;
586 * Set Theme downloadable
588 * @param int id id-number for theme.
589 * @param bool downloadable
590 * @param string user Username
591 * @param string password sha1-hash of password
592 * @return bool
594 function setThemeDownloadable($id, $downloadable, $user, $password)
596 IrrecoLog::$server->log('XML-RPC ThemeSetDownloadable');
597 $success = $this->database->setThemeDownloadable($id,
598 $downloadable,
599 $user,
600 $password);
602 if ($success == 'username and password mismatch.') {
603 throw new XML_RPC2_FaultException(
604 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
605 self::USERNAME_AND_PASSWORD_MISMATCH);
607 return TRUE;
611 * Get id list for themes
613 * @return array
615 function getThemes()
617 IrrecoLog::$server->log('XML-RPC getThemes');
618 $data = $this->database->getThemeList();
620 if ($data == 'Database is empty.') {
621 throw new XML_RPC2_FaultException(self::EMPTY_DB_MSG,
622 self::EMPTY_DB_ERROR);
625 $array = array();
626 foreach ($data as $item) {
627 array_push($array, $item->id);
630 IrrecoLog::$database->log("Theme id:\n" .
631 IrrecoLog::getVarDump($array));
632 return $array;
636 * Get all data from theme
638 * Returns name, user, comment, preview_button, folder,
639 * uploaded, modified, downloaded, download_count
640 * @param int id id-number for theme.
641 * @return struct
644 function getThemeById($id)
646 IrrecoLog::$server->log('XML-RPC getThemeById');
647 $data = $this->database->getTheme($id);
648 $array = array();
650 $array['name'] = $data[0]->name;
651 $array['user'] = $data[0]->user;
652 $array['comment'] = $data[0]->comment;
653 $array['preview_button'] = $data[0]->preview_button;
654 $array['folder'] = $data[0]->folder;
655 $array['uploaded'] = $data[0]->uploaded;
656 $array['modified'] = $data[0]->modified;
657 $array['downloaded'] = $data[0]->downloaded;
658 $array['download_count'] = intval($data[0]->download_count);
660 IrrecoLog::$database->log("Theme:\n" .
661 IrrecoLog::getVarDump($array));
662 return $array;
666 * Get theme versions by name
668 * Returns id-list
669 * @param string name name for theme.
670 * @return array
673 function getThemeVersionsByName($name)
675 IrrecoLog::$server->log('XML-RPC getThemeVersionsByName');
676 $data = $this->database->getThemeVersionsByName($name);
678 $array = array();
679 foreach ($data as $item) {
680 array_push($array, $item->id);
683 IrrecoLog::$database->log("Theme versions:\n".
684 IrrecoLog::getVarDump($array));
685 return $array;
689 * Get date for theme by id
691 * @param int id id-number for theme.
692 * @return string
695 function getThemeDateById($id)
697 IrrecoLog::$server->log('XML-RPC getThemeById');
698 $data = $this->database->getTheme($id);
700 IrrecoLog::$database->log("Theme-date: ".$data[0]->uploaded);
701 return $data[0]->uploaded;
705 * Get theme_id by name and date.
707 * Returns theme_id or 0 if theme does not exists.
708 * @param string name Name for theme.
709 * @param string date theme creation date.
710 * @return int
712 function getThemeIdByNameAndDate($name, $date)
714 IrrecoLog::$server->log('XML-RPC getThemeIdByNameAndDate');
715 $id = $this->database->getThemeIdByNameAndDate($name, $date);
717 IrrecoLog::$server->log("Theme_id: ". $id);
718 return $id;
722 * Add button to Theme
724 * Returns button_id
725 * @param string name Name of button
726 * @param bool allow_text
727 * @param string text_format_up
728 * @param string text_format_down
729 * @param int text_padding
730 * @param float text_h_align
731 * @param float text_v_align
732 * @param string image_up_hash
733 * @param string image_up_name
734 * @param string image_up
735 * @param string image_down_hash
736 * @param string image_down_name
737 * @param string image_down
738 * @param string folder
739 * @param int theme_id
740 * @param string user Username
741 * @param string password sha1-hash of password
742 * @return int
744 function addButtonToTheme($name, $allow_text, $text_format_up,
745 $text_format_down, $text_padding,
746 $text_h_align, $text_v_align,
747 $image_up_hash, $image_up_name, $image_up,
748 $image_down_hash, $image_down_name, $image_down,
749 $folder, $theme_id, $user, $password)
752 IrrecoLog::$server->log('XML-RPC addButtonToTheme');
754 $button_id = $this->database->addButtonToTheme(
755 $name, $allow_text, $text_format_up,
756 $text_format_down, $text_padding,
757 $text_h_align, $text_v_align,
758 $image_up_hash, $image_up_name,
759 base64_decode($image_up), $image_down_hash,
760 $image_down_name, base64_decode($image_down),
761 $folder, $theme_id, $user, $password);
763 if ($button_id == 'username and password mismatch.') {
764 throw new XML_RPC2_FaultException(
765 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
766 self::USERNAME_AND_PASSWORD_MISMATCH);
768 else if ($button_id == 'file hash and file data mismatch.') {
769 throw new XML_RPC2_FaultException(
770 self::HASH_AND_DATA_MISMATCH_MSG,
771 self::HASH_AND_DATA_MISMATCH);
774 IrrecoLog::$database->log("Button_id: " . $button_id . "\n");
775 return $button_id;
779 * Get id list of buttons for theme
781 * @param int theme_id id of theme
782 * @return array
784 function getButtons($theme_id)
786 IrrecoLog::$server->log('XML-RPC getButtons');
787 $data = $this->database->getButtonList($theme_id);
789 $array = array();
790 foreach ($data as $item) {
791 array_push($array, intval($item->id));
794 if (count($array) == 0) {
795 array_push($array, 0);
798 IrrecoLog::$database->log("Button id:\n".
799 IrrecoLog::getVarDump($array));
800 return $array;
804 * Get all data from Button
806 * Returns name, allow_text, text_format_up, text_format_down,
807 * text_padding, image_up_hash, image_up_name, base64 encoded image_up,
808 * image_down_hash, image_down_name, base64 encoded image_down, folder
809 * @param int id id-number for button.
810 * @return struct
812 function getButtonById($id)
814 IrrecoLog::$server->log('XML-RPC getButtonById');
815 $array = $this->database->getButton($id);
817 IrrecoLog::$database->log("Button:\n".
818 $array['name'].", ".
819 $array['allow_text'].", ".
820 $array['text_format_up'].", ".
821 $array['text_format_down'].", ".
822 $array['text_padding'].", ".
823 $array['text_h_align'].", ".
824 $array['text_v_align'].", ".
825 $array['image_up_hash'].", ".
826 $array['image_up_name'].", ".
827 "base64_image_data, ".
828 $array['image_down_hash'].", ".
829 $array['image_down_name'].", ".
830 "base64_image_data, ".
831 $array['folder']);
833 return $array;
837 * Get preview button
839 * Returns base64 encoded image-data string.
840 * @param int theme_id id for theme
841 * @return string
844 function getPreviewButton($theme_id)
846 IrrecoLog::$server->log('XML-RPC getPreviewButton');
847 $data = $this->database->getPreviewButton($theme_id);
848 return base64_encode($data);
852 * Add background to Theme
854 * Returns background_id
855 * @param string name Name of background
856 * @param string image_hash sha1-hash of image-data
857 * @param string image_name Name for image
858 * @param string image Content of image
859 * @param string folder name of button-folder
860 * @param int theme_id id of theme
861 * @param string user Username
862 * @param string password sha1-hash of password
863 * @return int
865 function addBgToTheme($name, $image_hash, $image_name, $image,
866 $folder, $theme_id, $user, $password)
868 IrrecoLog::$server->log('XML-RPC addBgToTheme');
869 $bg_id = $this->database->addBgToTheme($name, $image_hash,
870 $image_name,
871 base64_decode($image),
872 $folder, $theme_id,
873 $user, $password);
875 if ($bg_id == 'username and password mismatch.') {
876 throw new XML_RPC2_FaultException(
877 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
878 self::USERNAME_AND_PASSWORD_MISMATCH);
880 else if ($bg_id == 'file hash and file data mismatch.') {
881 throw new XML_RPC2_FaultException(
882 self::HASH_AND_DATA_MISMATCH_MSG,
883 self::HASH_AND_DATA_MISMATCH);
886 IrrecoLog::$database->log("Bg_id: " . $bg_id . "\n");
887 return $bg_id;
891 * Get id list of backgrounds for theme
893 * @param int theme_id id of theme
894 * @return array
896 function getBackgrounds($theme_id)
898 IrrecoLog::$server->log('XML-RPC getBackgrounds');
899 $data = $this->database->getBgList($theme_id);
901 $array = array();
902 foreach ($data as $item) {
903 array_push($array, intval($item->id));
906 if (count($array) == 0) {
907 array_push($array, 0);
910 IrrecoLog::$database->log("Bg id:\n" .
911 IrrecoLog::getVarDump($array));
912 return $array;
916 * Get all data from Background
918 * Returns name, image_hash, image_name,
919 * folder and base64 encoded image_data
920 * @param int id id-number for background.
921 * @return struct
923 function getBgById($id)
925 IrrecoLog::$server->log('XML-RPC getBgById');
926 $array = $this->database->getBg($id);
928 IrrecoLog::$database->log("Bg: ".
929 $array['name'].", ".
930 $array['image_hash'].", ".
931 $array['image_name'].", ".
932 "base64_image_data, ".
933 $array['folder']);
936 return $array;
940 * Login to database
942 * Returns TRUE if login successful, FALSE otherwise.
944 * @param string user Username
945 * @param string password sha1-hash of password
946 * @return boolean
948 function loginToDB($user, $password)
950 IrrecoLog::$server->log('XML-RPC loginToDB');
952 /* Check for faultCode */
953 if(strstr($user, "faultCode")) {
954 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
955 self::FAULTCODE_ERROR);
958 /* Check parameters here */
959 if(strlen($user) < 6) {
960 throw new XML_RPC2_FaultException(
961 self::LOGIN_ERROR_MSG,
962 self::LOGIN_ERROR);
965 if(strlen($password) < 6) {
966 throw new XML_RPC2_FaultException(
967 self::LOGIN_ERROR_MSG,
968 self::LOGIN_ERROR);
971 $return = $this->database->loginToDB($user, $password);
973 if ($return == FALSE) {
974 throw new XML_RPC2_FaultException(
975 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
976 self::USERNAME_AND_PASSWORD_MISMATCH);
979 return $return;
983 * Get max image-size in bytes
985 * Returns max image-size
986 * @return int
988 function getMaxImageSize()
990 IrrecoLog::$server->log('XML-RPC getMaxImageSize');
991 $val = trim(ini_get('post_max_size'));
992 $last = strtolower($val[strlen($val)-1]);
994 switch($last) {
995 // The 'G' modifier is available since PHP 5.1.0
996 case 'g':
997 $val *= 1024;
998 case 'm':
999 $val *= 1024;
1000 case 'k':
1001 $val *= 1024;
1004 $val = $val / 2 - (100 * 1024); //divided by 2 because of base64
1006 IrrecoLog::$server->log("Max image-size: " . $val . "\n");
1007 return $val;
1012 * Create new Remote
1014 * Returns remote_id
1015 * @param string comment
1016 * @param string category Name for Category. e.g. "Tv"
1017 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1018 * @param string model Name for Model. e.g. "xyz"
1019 * @param string file_hash sha1-hash of file_data
1020 * @param string file_name Name for File
1021 * @param string file_data Content of file
1022 * @param string user Username
1023 * @param string password sha1-hash of password
1024 * @return int
1026 function createNewRemote($comment, $category, $manufacturer,
1027 $model, $file_hash, $file_name, $file_data,
1028 $user, $password)
1030 IrrecoLog::$server->log('XML-RPC createNewRemote');
1031 $remote_id = $this->database->createNewRemote($comment,
1032 $category, $manufacturer, $model, $file_hash,
1033 $file_name, $file_data, $user, $password);
1035 if ($remote_id == 'username and password mismatch.') {
1036 throw new XML_RPC2_FaultException(
1037 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1038 self::USERNAME_AND_PASSWORD_MISMATCH);
1040 else if ($remote_id == 'file hash and file data mismatch.') {
1041 throw new XML_RPC2_FaultException(
1042 self::HASH_AND_DATA_MISMATCH_MSG,
1043 self::HASH_AND_DATA_MISMATCH);
1046 IrrecoLog::$database->log("Remote_id: " . $remote_id . "\n");
1047 return $remote_id;
1051 * Set Remote downloadable
1053 * @param int id id-number for remote.
1054 * @param bool downloadable
1055 * @param string user Username
1056 * @param string password sha1-hash of password
1057 * @return bool
1059 function setRemoteDownloadable($id, $downloadable, $user, $password)
1061 IrrecoLog::$server->log('XML-RPC RemoteSetDownloadable');
1062 $success = $this->database->setRemoteDownloadable($id,
1063 $downloadable,
1064 $user,
1065 $password);
1067 if ($success == 'username and password mismatch.') {
1068 throw new XML_RPC2_FaultException(
1069 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1070 self::USERNAME_AND_PASSWORD_MISMATCH);
1072 return TRUE;
1077 * Add Configuration to Remote
1079 * @param int remote_id id-number for remote.
1080 * @param int configuration_id id-number for configuration.
1081 * @param string user Username
1082 * @param string password sha1-hash of password
1083 * @return bool
1085 function addConfigurationToRemote($remote_id, $configuration_id,
1086 $user, $password)
1088 IrrecoLog::$server->log('XML-RPC addConfigurationToRemote');
1089 $success = $this->database->addConfigurationToRemote($remote_id,
1090 $configuration_id,
1091 $user,
1092 $password);
1094 if ($success == 'username and password mismatch.') {
1095 throw new XML_RPC2_FaultException(
1096 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1097 self::USERNAME_AND_PASSWORD_MISMATCH);
1099 return TRUE;
1103 * Add Theme to Remote
1105 * @param int remote_id id-number for remote.
1106 * @param int theme_id id-number for theme.
1107 * @param string user Username
1108 * @param string password sha1-hash of password
1109 * @return bool
1111 function addThemeToRemote($remote_id, $theme_id, $user, $password)
1113 IrrecoLog::$server->log('XML-RPC addThemeToRemote');
1114 $success = $this->database->addThemeToRemote($remote_id,
1115 $theme_id,
1116 $user,
1117 $password);
1119 if ($success == 'username and password mismatch.') {
1120 throw new XML_RPC2_FaultException(
1121 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1122 self::USERNAME_AND_PASSWORD_MISMATCH);
1124 return TRUE;
1128 * Get list of categories that have remotes in them.
1130 * @return array
1132 function getRemoteCategories()
1134 IrrecoLog::$server->log('XML-RPC getRemoteCategories');
1135 $data = $this->database->getRemoteCategories();
1137 $array = array();
1138 foreach ($data as $item) {
1139 array_push($array, $item->name);
1142 IrrecoLog::$database->log("Category list:\n" .
1143 IrrecoLog::getVarDump($array));
1144 return $array;
1148 * Get list of manufacturers for "remote category".
1150 * @param string category Name for Category. e.g. "Tv"
1151 * @return array
1153 function getRemoteManufacturers($category)
1155 IrrecoLog::$server->log('XML-RPC getRemoteManufacturers');
1156 $data = $this->database->getRemoteManufacturers($category);
1158 $array = array();
1159 foreach ($data as $item) {
1160 array_push($array, $item->name);
1163 IrrecoLog::$database->log("Manufacturer list:\n" .
1164 IrrecoLog::getVarDump($array));
1165 return $array;
1169 * Get list of models by manufacturer in selected category.
1171 * @param string category Name for Category. e.g. "Tv"
1172 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1173 * @return array
1175 function getRemoteModels($category,$manufacturer)
1177 IrrecoLog::$server->log('XML-RPC getRemoteModels');
1178 $data = $this->database->getRemoteModels($category,
1179 $manufacturer);
1181 $array = array();
1182 foreach ($data as $item) {
1183 array_push($array, $item->model);
1186 IrrecoLog::$database->log("Model list:\n" .
1187 IrrecoLog::getVarDump($array));
1188 return $array;
1192 * Get Creators for model.
1194 * @param string category Name for Category. e.g. "Tv"
1195 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1196 * @param string model Name for Model. e.g. "xyz"
1197 * @return array
1199 function getRemoteCreators($category, $manufacturer, $model)
1201 IrrecoLog::$server->log('XML-RPC getRemoteCreators');
1202 $data = $this->database->getRemoteCreators($category,
1203 $manufacturer,
1204 $model);
1206 $array = array();
1207 foreach ($data as $item) {
1208 array_push($array, $item->user);
1211 IrrecoLog::$database->log("Creator list:\n" .
1212 IrrecoLog::getVarDump($array));
1213 return $array;
1217 * Get Remotes
1219 * Returns list of remote_id
1220 * @param string category Name for Category. e.g. "Tv"
1221 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1222 * @param string model Name for Model. e.g. "xyz"
1223 * @param string user Username of Remote creator
1224 * @return array
1226 function getRemotes($category, $manufacturer, $model, $user)
1228 IrrecoLog::$server->log('XML-RPC getRemotes');
1229 $data = $this->database->getRemotes($category, $manufacturer,
1230 $model, $user);
1232 $array = array();
1233 foreach ($data as $item) {
1234 array_push($array, intval($item->id));
1237 IrrecoLog::$server->log("Remotes:\n" .
1238 IrrecoLog::getVarDump($array));
1239 return $array;
1243 * Get all data from remote
1245 * Returns name, user, comment, category, manufacturer,
1246 * model, file_hash, file_name, uploaded, modified, downloaded,
1247 * and download_count
1248 * @param int id id-number for remote.
1249 * @return struct
1251 function getRemoteById($id)
1253 IrrecoLog::$server->log('XML-RPC getRemoteById');
1254 $array = $this->database->getRemoteByid($id);
1256 IrrecoLog::$server->log("Remote:\n" .
1257 IrrecoLog::getVarDump($array));
1258 return $array;
1262 * Get themes of remote
1264 * @param int remote_id id-number for remote.
1265 * @return array
1267 function getThemesOfRemote($remote_id)
1269 IrrecoLog::$server->log('XML-RPC getThemesOfRemote');
1270 $data = $this->database->getThemesOfRemote($remote_id);
1272 $array = array();
1274 if ($data == "Can't find any theme of remote.") {
1275 /*throw new XML_RPC2_FaultException(
1276 self::NO_THEMES_OF_REMOTE_MSG,
1277 self::NO_THEMES_OF_REMOTE_ERROR);*/
1278 array_push($array, 0);
1279 } else {
1280 foreach ($data as $item) {
1281 array_push($array, intval($item->id));
1285 IrrecoLog::$server->log("Theme id:\n" .
1286 IrrecoLog::getVarDump($array));
1287 return $array;
1291 * Get configurations of remote
1293 * @param int remote_id id-number for remote.
1294 * @return array
1296 function getConfigurationsOfRemote($remote_id)
1298 IrrecoLog::$server->log('XML-RPC getConfigurationsOfRemote');
1299 $data = $this->database->getConfigurationsOfRemote($remote_id);
1301 $array = array();
1303 if ($data == "Can't find any configuration of remote.") {
1304 /*throw new XML_RPC2_FaultException(
1305 self::NO_CONFIGURATIONS_OF_REMOTE_MSG,
1306 self::NO_CONFIGURATIONS_OF_REMOTE_ERROR);*/
1307 array_push($array, 0);
1308 } else {
1309 foreach ($data as $item) {
1310 array_push($array, intval($item->id));
1314 IrrecoLog::$server->log("Configuration id:\n" .
1315 IrrecoLog::getVarDump($array));
1316 return $array;