Created function which returns configuration_id by file_hash and file_name
[irreco.git] / database / php / irreco_webdb_server.php
blob585b43901730c6413a3a946df1ad3e8b4229e0de
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 if ($theme_id == 'username and password mismatch.') {
568 throw new XML_RPC2_FaultException(
569 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
570 self::USERNAME_AND_PASSWORD_MISMATCH);
573 IrrecoLog::$database->log("Theme_id: " . $theme_id . "\n");
574 return $theme_id;
578 * Set Theme downloadable
580 * @param int id id-number for theme.
581 * @param bool downloadable
582 * @param string user Username
583 * @param string password sha1-hash of password
584 * @return bool
586 function setThemeDownloadable($id, $downloadable, $user, $password)
588 IrrecoLog::$server->log('XML-RPC ThemeSetDownloadable');
589 $success = $this->database->setThemeDownloadable($id,
590 $downloadable,
591 $user,
592 $password);
594 if ($success == 'username and password mismatch.') {
595 throw new XML_RPC2_FaultException(
596 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
597 self::USERNAME_AND_PASSWORD_MISMATCH);
599 return TRUE;
603 * Get id list for themes
605 * @return array
607 function getThemes()
609 IrrecoLog::$server->log('XML-RPC getThemes');
610 $data = $this->database->getThemeList();
612 if ($data == 'Database is empty.') {
613 throw new XML_RPC2_FaultException(self::EMPTY_DB_MSG,
614 self::EMPTY_DB_ERROR);
617 $array = array();
618 foreach ($data as $item) {
619 array_push($array, $item->id);
622 IrrecoLog::$database->log("Theme id:\n" .
623 IrrecoLog::getVarDump($array));
624 return $array;
628 * Get all data from theme
630 * Returns name, user, comment, preview_button, folder,
631 * uploaded, modified, downloaded, download_count
632 * @param int id id-number for theme.
633 * @return struct
636 function getThemeById($id)
638 IrrecoLog::$server->log('XML-RPC getThemeById');
639 $data = $this->database->getTheme($id);
640 $array = array();
642 $array['name'] = $data[0]->name;
643 $array['user'] = $data[0]->user;
644 $array['comment'] = $data[0]->comment;
645 $array['preview_button'] = $data[0]->preview_button;
646 $array['folder'] = $data[0]->folder;
647 $array['uploaded'] = $data[0]->uploaded;
648 $array['modified'] = $data[0]->modified;
649 $array['downloaded'] = $data[0]->downloaded;
650 $array['download_count'] = intval($data[0]->download_count);
652 IrrecoLog::$database->log("Theme:\n" .
653 IrrecoLog::getVarDump($array));
654 return $array;
658 * Get theme versions by name
660 * Returns id-list
661 * @param string name name for theme.
662 * @return array
665 function getThemeVersionsByName($name)
667 IrrecoLog::$server->log('XML-RPC getThemeVersionsByName');
668 $data = $this->database->getThemeVersionsByName($name);
670 $array = array();
671 foreach ($data as $item) {
672 array_push($array, $item->id);
675 IrrecoLog::$database->log("Theme versions:\n".
676 IrrecoLog::getVarDump($array));
677 return $array;
681 * Get date for theme by id
683 * @param int id id-number for theme.
684 * @return string
687 function getThemeDateById($id)
689 IrrecoLog::$server->log('XML-RPC getThemeById');
690 $data = $this->database->getTheme($id);
692 IrrecoLog::$database->log("Theme-date: ".$data[0]->uploaded);
693 return $data[0]->uploaded;
697 * Get theme_id by name and date.
699 * Returns theme_id or 0 if theme does not exists.
700 * @param string name Name for theme.
701 * @param string date theme creation date.
702 * @return int
704 function getThemeIdByNameAndDate($name, $date)
706 IrrecoLog::$server->log('XML-RPC getThemeIdByNameAndDate');
707 $id = $this->database->getThemeIdByNameAndDate($name, $date);
709 IrrecoLog::$server->log("Theme_id: ". $id);
710 return $id;
714 * Add button to Theme
716 * Returns button_id
717 * @param string name Name of button
718 * @param bool allow_text
719 * @param string text_format_up
720 * @param string text_format_down
721 * @param int text_padding
722 * @param float text_h_align
723 * @param float text_v_align
724 * @param string image_up_hash
725 * @param string image_up_name
726 * @param string image_up
727 * @param string image_down_hash
728 * @param string image_down_name
729 * @param string image_down
730 * @param string folder
731 * @param int theme_id
732 * @param string user Username
733 * @param string password sha1-hash of password
734 * @return int
736 function addButtonToTheme($name, $allow_text, $text_format_up,
737 $text_format_down, $text_padding,
738 $text_h_align, $text_v_align,
739 $image_up_hash, $image_up_name, $image_up,
740 $image_down_hash, $image_down_name, $image_down,
741 $folder, $theme_id, $user, $password)
744 IrrecoLog::$server->log('XML-RPC addButtonToTheme');
746 $button_id = $this->database->addButtonToTheme(
747 $name, $allow_text, $text_format_up,
748 $text_format_down, $text_padding,
749 $text_h_align, $text_v_align,
750 $image_up_hash, $image_up_name,
751 base64_decode($image_up), $image_down_hash,
752 $image_down_name, base64_decode($image_down),
753 $folder, $theme_id, $user, $password);
755 if ($button_id == 'username and password mismatch.') {
756 throw new XML_RPC2_FaultException(
757 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
758 self::USERNAME_AND_PASSWORD_MISMATCH);
760 else if ($button_id == 'file hash and file data mismatch.') {
761 throw new XML_RPC2_FaultException(
762 self::HASH_AND_DATA_MISMATCH_MSG,
763 self::HASH_AND_DATA_MISMATCH);
766 IrrecoLog::$database->log("Button_id: " . $button_id . "\n");
767 return $button_id;
771 * Get id list of buttons for theme
773 * @param int theme_id id of theme
774 * @return array
776 function getButtons($theme_id)
778 IrrecoLog::$server->log('XML-RPC getButtons');
779 $data = $this->database->getButtonList($theme_id);
781 $array = array();
782 foreach ($data as $item) {
783 array_push($array, intval($item->id));
786 if (count($array) == 0) {
787 array_push($array, 0);
790 IrrecoLog::$database->log("Button id:\n".
791 IrrecoLog::getVarDump($array));
792 return $array;
796 * Get all data from Button
798 * Returns name, allow_text, text_format_up, text_format_down,
799 * text_padding, image_up_hash, image_up_name, base64 encoded image_up,
800 * image_down_hash, image_down_name, base64 encoded image_down, folder
801 * @param int id id-number for button.
802 * @return struct
804 function getButtonById($id)
806 IrrecoLog::$server->log('XML-RPC getButtonById');
807 $array = $this->database->getButton($id);
809 IrrecoLog::$database->log("Button:\n".
810 $array['name'].", ".
811 $array['allow_text'].", ".
812 $array['text_format_up'].", ".
813 $array['text_format_down'].", ".
814 $array['text_padding'].", ".
815 $array['text_h_align'].", ".
816 $array['text_v_align'].", ".
817 $array['image_up_hash'].", ".
818 $array['image_up_name'].", ".
819 "base64_image_data, ".
820 $array['image_down_hash'].", ".
821 $array['image_down_name'].", ".
822 "base64_image_data, ".
823 $array['folder']);
825 return $array;
829 * Get preview button
831 * Returns base64 encoded image-data string.
832 * @param int theme_id id for theme
833 * @return string
836 function getPreviewButton($theme_id)
838 IrrecoLog::$server->log('XML-RPC getPreviewButton');
839 $data = $this->database->getPreviewButton($theme_id);
840 return base64_encode($data);
844 * Add background to Theme
846 * Returns background_id
847 * @param string name Name of background
848 * @param string image_hash sha1-hash of image-data
849 * @param string image_name Name for image
850 * @param string image Content of image
851 * @param string folder name of button-folder
852 * @param int theme_id id of theme
853 * @param string user Username
854 * @param string password sha1-hash of password
855 * @return int
857 function addBgToTheme($name, $image_hash, $image_name, $image,
858 $folder, $theme_id, $user, $password)
860 IrrecoLog::$server->log('XML-RPC addBgToTheme');
861 $bg_id = $this->database->addBgToTheme($name, $image_hash,
862 $image_name,
863 base64_decode($image),
864 $folder, $theme_id,
865 $user, $password);
867 if ($bg_id == 'username and password mismatch.') {
868 throw new XML_RPC2_FaultException(
869 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
870 self::USERNAME_AND_PASSWORD_MISMATCH);
872 else if ($bg_id == 'file hash and file data mismatch.') {
873 throw new XML_RPC2_FaultException(
874 self::HASH_AND_DATA_MISMATCH_MSG,
875 self::HASH_AND_DATA_MISMATCH);
878 IrrecoLog::$database->log("Bg_id: " . $bg_id . "\n");
879 return $bg_id;
883 * Get id list of backgrounds for theme
885 * @param int theme_id id of theme
886 * @return array
888 function getBackgrounds($theme_id)
890 IrrecoLog::$server->log('XML-RPC getBackgrounds');
891 $data = $this->database->getBgList($theme_id);
893 $array = array();
894 foreach ($data as $item) {
895 array_push($array, intval($item->id));
898 if (count($array) == 0) {
899 array_push($array, 0);
902 IrrecoLog::$database->log("Bg id:\n" .
903 IrrecoLog::getVarDump($array));
904 return $array;
908 * Get all data from Background
910 * Returns name, image_hash, image_name,
911 * folder and base64 encoded image_data
912 * @param int id id-number for background.
913 * @return struct
915 function getBgById($id)
917 IrrecoLog::$server->log('XML-RPC getBgById');
918 $array = $this->database->getBg($id);
920 IrrecoLog::$database->log("Bg: ".
921 $array['name'].", ".
922 $array['image_hash'].", ".
923 $array['image_name'].", ".
924 "base64_image_data, ".
925 $array['folder']);
928 return $array;
932 * Login to database
934 * Returns TRUE if login successful, FALSE otherwise.
936 * @param string user Username
937 * @param string password sha1-hash of password
938 * @return boolean
940 function loginToDB($user, $password)
942 IrrecoLog::$server->log('XML-RPC loginToDB');
944 /* Check for faultCode */
945 if(strstr($user, "faultCode")) {
946 throw new XML_RPC2_FaultException(self::FAULTCODE_ERROR_MSG,
947 self::FAULTCODE_ERROR);
950 /* Check parameters here */
951 if(strlen($user) < 6) {
952 throw new XML_RPC2_FaultException(
953 self::LOGIN_ERROR_MSG,
954 self::LOGIN_ERROR);
957 if(strlen($password) < 6) {
958 throw new XML_RPC2_FaultException(
959 self::LOGIN_ERROR_MSG,
960 self::LOGIN_ERROR);
963 $return = $this->database->loginToDB($user, $password);
965 if ($return == FALSE) {
966 throw new XML_RPC2_FaultException(
967 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
968 self::USERNAME_AND_PASSWORD_MISMATCH);
971 return $return;
975 * Get max image-size in bytes
977 * Returns max image-size
978 * @return int
980 function getMaxImageSize()
982 IrrecoLog::$server->log('XML-RPC getMaxImageSize');
983 $val = trim(ini_get('post_max_size'));
984 $last = strtolower($val[strlen($val)-1]);
986 switch($last) {
987 // The 'G' modifier is available since PHP 5.1.0
988 case 'g':
989 $val *= 1024;
990 case 'm':
991 $val *= 1024;
992 case 'k':
993 $val *= 1024;
996 $val = $val / 2 - (100 * 1024); //divided by 2 because of base64
998 IrrecoLog::$server->log("Max image-size: " . $val . "\n");
999 return $val;
1004 * Create new Remote
1006 * Returns remote_id
1007 * @param string comment
1008 * @param string category Name for Category. e.g. "Tv"
1009 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1010 * @param string model Name for Model. e.g. "xyz"
1011 * @param string file_hash sha1-hash of file_data
1012 * @param string file_name Name for File
1013 * @param string file_data Content of file
1014 * @param string user Username
1015 * @param string password sha1-hash of password
1016 * @return int
1018 function createNewRemote($comment, $category, $manufacturer,
1019 $model, $file_hash, $file_name, $file_data,
1020 $user, $password)
1022 IrrecoLog::$server->log('XML-RPC createNewRemote');
1023 $remote_id = $this->database->createNewRemote($comment,
1024 $category, $manufacturer, $model, $file_hash,
1025 $file_name, $file_data, $user, $password);
1027 if ($remote_id == 'username and password mismatch.') {
1028 throw new XML_RPC2_FaultException(
1029 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1030 self::USERNAME_AND_PASSWORD_MISMATCH);
1032 else if ($remote_id == 'file hash and file data mismatch.') {
1033 throw new XML_RPC2_FaultException(
1034 self::HASH_AND_DATA_MISMATCH_MSG,
1035 self::HASH_AND_DATA_MISMATCH);
1038 IrrecoLog::$database->log("Remote_id: " . $remote_id . "\n");
1039 return $remote_id;
1043 * Set Remote downloadable
1045 * @param int id id-number for remote.
1046 * @param bool downloadable
1047 * @param string user Username
1048 * @param string password sha1-hash of password
1049 * @return bool
1051 function setRemoteDownloadable($id, $downloadable, $user, $password)
1053 IrrecoLog::$server->log('XML-RPC RemoteSetDownloadable');
1054 $success = $this->database->setRemoteDownloadable($id,
1055 $downloadable,
1056 $user,
1057 $password);
1059 if ($success == 'username and password mismatch.') {
1060 throw new XML_RPC2_FaultException(
1061 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1062 self::USERNAME_AND_PASSWORD_MISMATCH);
1064 return TRUE;
1069 * Add Configuration to Remote
1071 * @param int remote_id id-number for remote.
1072 * @param int configuration_id id-number for configuration.
1073 * @param string user Username
1074 * @param string password sha1-hash of password
1075 * @return bool
1077 function addConfigurationToRemote($remote_id, $configuration_id,
1078 $user, $password)
1080 IrrecoLog::$server->log('XML-RPC addConfigurationToRemote');
1081 $success = $this->database->addConfigurationToRemote($remote_id,
1082 $configuration_id,
1083 $user,
1084 $password);
1086 if ($success == 'username and password mismatch.') {
1087 throw new XML_RPC2_FaultException(
1088 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1089 self::USERNAME_AND_PASSWORD_MISMATCH);
1091 return TRUE;
1095 * Add Theme to Remote
1097 * @param int remote_id id-number for remote.
1098 * @param int theme_id id-number for theme.
1099 * @param string user Username
1100 * @param string password sha1-hash of password
1101 * @return bool
1103 function addThemeToRemote($remote_id, $theme_id, $user, $password)
1105 IrrecoLog::$server->log('XML-RPC addThemeToRemote');
1106 $success = $this->database->addThemeToRemote($remote_id,
1107 $theme_id,
1108 $user,
1109 $password);
1111 if ($success == 'username and password mismatch.') {
1112 throw new XML_RPC2_FaultException(
1113 self::USERNAME_AND_PASSWORD_MISMATCH_MSG,
1114 self::USERNAME_AND_PASSWORD_MISMATCH);
1116 return TRUE;
1120 * Get list of categories that have remotes in them.
1122 * @return array
1124 function getRemoteCategories()
1126 IrrecoLog::$server->log('XML-RPC getRemoteCategories');
1127 $data = $this->database->getRemoteCategories();
1129 $array = array();
1130 foreach ($data as $item) {
1131 array_push($array, $item->name);
1134 IrrecoLog::$database->log("Category list:\n" .
1135 IrrecoLog::getVarDump($array));
1136 return $array;
1140 * Get list of manufacturers for "remote category".
1142 * @param string category Name for Category. e.g. "Tv"
1143 * @return array
1145 function getRemoteManufacturers($category)
1147 IrrecoLog::$server->log('XML-RPC getRemoteManufacturers');
1148 $data = $this->database->getRemoteManufacturers($category);
1150 $array = array();
1151 foreach ($data as $item) {
1152 array_push($array, $item->name);
1155 IrrecoLog::$database->log("Manufacturer list:\n" .
1156 IrrecoLog::getVarDump($array));
1157 return $array;
1161 * Get list of models by manufacturer in selected category.
1163 * @param string category Name for Category. e.g. "Tv"
1164 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1165 * @return array
1167 function getRemoteModels($category,$manufacturer)
1169 IrrecoLog::$server->log('XML-RPC getRemoteModels');
1170 $data = $this->database->getRemoteModels($category,
1171 $manufacturer);
1173 $array = array();
1174 foreach ($data as $item) {
1175 array_push($array, $item->model);
1178 IrrecoLog::$database->log("Model list:\n" .
1179 IrrecoLog::getVarDump($array));
1180 return $array;
1184 * Get Creators for model.
1186 * @param string category Name for Category. e.g. "Tv"
1187 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1188 * @param string model Name for Model. e.g. "xyz"
1189 * @return array
1191 function getRemoteCreators($category, $manufacturer, $model)
1193 IrrecoLog::$server->log('XML-RPC getRemoteCreators');
1194 $data = $this->database->getRemoteCreators($category,
1195 $manufacturer,
1196 $model);
1198 $array = array();
1199 foreach ($data as $item) {
1200 array_push($array, $item->user);
1203 IrrecoLog::$database->log("Creator list:\n" .
1204 IrrecoLog::getVarDump($array));
1205 return $array;
1209 * Get Remotes
1211 * Returns list of remote_id
1212 * @param string category Name for Category. e.g. "Tv"
1213 * @param string manufacturer Name for Manufacturer. e.g. "Sony"
1214 * @param string model Name for Model. e.g. "xyz"
1215 * @param string user Username of Remote creator
1216 * @return array
1218 function getRemotes($category, $manufacturer, $model, $user)
1220 IrrecoLog::$server->log('XML-RPC getRemotes');
1221 $data = $this->database->getRemotes($category, $manufacturer,
1222 $model, $user);
1224 $array = array();
1225 foreach ($data as $item) {
1226 array_push($array, intval($item->id));
1229 IrrecoLog::$server->log("Remotes:\n" .
1230 IrrecoLog::getVarDump($array));
1231 return $array;
1235 * Get all data from remote
1237 * Returns name, user, comment, category, manufacturer,
1238 * model, file_hash, file_name, uploaded, modified, downloaded,
1239 * and download_count
1240 * @param int id id-number for remote.
1241 * @return struct
1243 function getRemoteById($id)
1245 IrrecoLog::$server->log('XML-RPC getRemoteById');
1246 $array = $this->database->getRemoteByid($id);
1248 IrrecoLog::$server->log("Remote:\n" .
1249 IrrecoLog::getVarDump($array));
1250 return $array;
1254 * Get themes of remote
1256 * @param int remote_id id-number for remote.
1257 * @return array
1259 function getThemesOfRemote($remote_id)
1261 IrrecoLog::$server->log('XML-RPC getThemesOfRemote');
1262 $data = $this->database->getThemesOfRemote($remote_id);
1264 $array = array();
1266 if ($data == "Can't find any theme of remote.") {
1267 /*throw new XML_RPC2_FaultException(
1268 self::NO_THEMES_OF_REMOTE_MSG,
1269 self::NO_THEMES_OF_REMOTE_ERROR);*/
1270 array_push($array, 0);
1271 } else {
1272 foreach ($data as $item) {
1273 array_push($array, intval($item->id));
1277 IrrecoLog::$server->log("Theme id:\n" .
1278 IrrecoLog::getVarDump($array));
1279 return $array;
1283 * Get configurations of remote
1285 * @param int remote_id id-number for remote.
1286 * @return array
1288 function getConfigurationsOfRemote($remote_id)
1290 IrrecoLog::$server->log('XML-RPC getConfigurationsOfRemote');
1291 $data = $this->database->getConfigurationsOfRemote($remote_id);
1293 $array = array();
1295 if ($data == "Can't find any configuration of remote.") {
1296 /*throw new XML_RPC2_FaultException(
1297 self::NO_CONFIGURATIONS_OF_REMOTE_MSG,
1298 self::NO_CONFIGURATIONS_OF_REMOTE_ERROR);*/
1299 array_push($array, 0);
1300 } else {
1301 foreach ($data as $item) {
1302 array_push($array, intval($item->id));
1306 IrrecoLog::$server->log("Configuration id:\n" .
1307 IrrecoLog::getVarDump($array));
1308 return $array;