/gui/tools/webmail/plugins/calendar_sql_backend/calendar_functions.php

https://github.com/BenBE/ispCP · PHP · 1051 lines · 449 code · 313 blank · 289 comment · 46 complexity · 5c26ecb5c3701acc4585845bf429a658 MD5 · raw file

  1. <?php
  2. /**
  3. * SquirrelMail Calendar Plugin SQL Backend
  4. * Copyright (C) 2005 Paul Lesneiwski <pdontthink@angrynerds.com>
  5. * This program is licensed under GPL. See COPYING for details
  6. *
  7. */
  8. include_once(SM_PATH . 'plugins/calendar_sql_backend/functions.php');
  9. /**
  10. * Returns a listing of all calendars that the given user
  11. * owns. If the user is a superuser, all calendars are
  12. * returned (regardless of actual ownership); if the user
  13. * is a limited administrator, the calendars owned by that
  14. * user are returned; and if the user is an unpriveleged
  15. * user, just that user's private calendars are returned.
  16. *
  17. * @param string $user The user for which to retrieve calendars
  18. * @param string $userType The type of user this is, which
  19. * corresponds to the constants defined
  20. * in the {@link calendar/calendar_constants.php}
  21. * file
  22. *
  23. * @return array An array of calendar objects
  24. *
  25. */
  26. function cal_sql_get_all_owned_calendars_do($user, $userType)
  27. {
  28. global $all_calendars_query, $owned_calendars_query, $all_calendars_of_type_query,
  29. $wildcard_calendar_owners_query, $color;
  30. // get database connection
  31. //
  32. $db = cal_get_database_connection();
  33. // superusers get it all
  34. //
  35. if ($userType == SM_CAL_SUPERUSER)
  36. {
  37. $sql = $all_calendars_query;
  38. $IDs = $db->getAll($sql);
  39. // check for database errors
  40. //
  41. if (DB::isError($IDs))
  42. {
  43. $msg = $IDs->getMessage();
  44. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_owned_calendars_do): could not query all calendar IDs - ' . $msg, $color);
  45. exit;
  46. }
  47. // reduce to single list of IDs
  48. //
  49. $calIDs = array();
  50. foreach ($IDs as $ID) $calIDs[] = $ID[0];
  51. }
  52. else
  53. {
  54. // get owned calendars
  55. //
  56. $sql = $owned_calendars_query;
  57. $sql = str_replace('%1', $user, $sql);
  58. $ownedIDs = $db->getAll($sql);
  59. // check for database errors
  60. //
  61. if (DB::isError($ownedIDs))
  62. {
  63. $msg = $ownedIDs->getMessage();
  64. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_owned_calendars_do): could not query owned calendar IDs - ' . $msg, $color);
  65. exit;
  66. }
  67. // reduce to single list of IDs
  68. //
  69. $ownedCalIDs = array();
  70. foreach ($ownedIDs as $ID) $ownedCalIDs[] = $ID[0];
  71. // get calendars with owner names that have wildcards in them
  72. //
  73. $sql = $wildcard_calendar_owners_query;
  74. $wildcardOwners = $db->getAll($sql);
  75. // check for database errors
  76. //
  77. if (DB::isError($wildcardOwners))
  78. {
  79. $msg = $wildcardOwners->getMessage();
  80. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_owned_calendars_do): could not query owners with wildcards - ' . $msg, $color);
  81. exit;
  82. }
  83. // figure out which ones match current user
  84. //
  85. foreach ($wildcardOwners as $owner)
  86. {
  87. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  88. strtoupper($owner[0])) . '$/', strtoupper($user)))
  89. $ownedCalIDs[] = $owner[1];
  90. }
  91. // unpriveleged users just get their private/personal
  92. // calendars... they should never own anything but,
  93. // except we'll make sure... just in case
  94. //
  95. if ($userType == SM_CAL_REGULAR_USER)
  96. {
  97. $sql = $all_calendars_of_type_query;
  98. $sql = str_replace('%1', SM_CAL_TYPE_PERSONAL, $sql);
  99. $allPrivateIDs = $db->getAll($sql);
  100. // check for database errors
  101. //
  102. if (DB::isError($allPrivateIDs))
  103. {
  104. $msg = $allPrivateIDs->getMessage();
  105. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_owned_calendars_do): could not query private calendar IDs - ' . $msg, $color);
  106. exit;
  107. }
  108. // reduce to single list of IDs
  109. //
  110. $allPrivateCalIDs = array();
  111. foreach ($allPrivateIDs as $ID) $allPrivateCalIDs[] = $ID[0];
  112. $calIDs = array_intersect($ownedCalIDs, $allPrivateCalIDs);
  113. }
  114. // limited admins get all owned calendars
  115. //
  116. else if ($userType == SM_CAL_LIMITED_ADMIN)
  117. $calIDs = $ownedCalIDs;
  118. }
  119. $calList = array();
  120. foreach ($calIDs as $calID)
  121. $calList[] = cal_sql_get_calendar_do($calID);
  122. return $calList;
  123. }
  124. /**
  125. * Returns a listing of all public calendars.
  126. *
  127. * @return array An array of calendar objects
  128. *
  129. */
  130. function cal_sql_get_all_public_calendars_do()
  131. {
  132. global $all_calendars_of_type_query, $color;
  133. // get database connection
  134. //
  135. $db = cal_get_database_connection();
  136. $sql = $all_calendars_of_type_query;
  137. $sql = str_replace('%1', SM_CAL_TYPE_PUBLIC, $sql);
  138. $IDs = $db->getAll($sql);
  139. // check for database errors
  140. //
  141. if (DB::isError($IDs))
  142. {
  143. $msg = $IDs->getMessage();
  144. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_public_calendars_do): could not query public calendar IDs - ' . $msg, $color);
  145. exit;
  146. }
  147. // reduce to single list of IDs
  148. //
  149. $calIDs = array();
  150. foreach ($IDs as $ID) $calIDs[] = $ID[0];
  151. $calList = array();
  152. foreach ($calIDs as $calID)
  153. $calList[] = cal_sql_get_calendar_do($calID);
  154. return $calList;
  155. }
  156. /**
  157. * Returns a listing of all shared and private (but not public)
  158. * calendars that the given user has read or write access to
  159. * EXCEPT the user's default private calendar.
  160. *
  161. * @param string $user The user for which to retrieve calendars
  162. * @param string $domain The user's domain
  163. *
  164. * @return array An array of calendar objects
  165. *
  166. */
  167. function cal_sql_get_all_accessible_calendars_do($user, $domain)
  168. {
  169. global $all_owned_calendars_of_type_query, $all_readable_calendars_of_type_query,
  170. $all_writeable_calendars_of_type_query,
  171. $wildcard_calendar_owners_of_type_query,
  172. $wildcard_calendar_readers_of_type_query,
  173. $wildcard_calendar_writers_of_type_query, $color;
  174. // get database connection
  175. //
  176. $db = cal_get_database_connection();
  177. // -------------------------------
  178. // get owned private cals
  179. //
  180. $sql = $all_owned_calendars_of_type_query;
  181. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_PERSONAL), $sql);
  182. $ownedPrivateIDs = $db->getAll($sql);
  183. // check for database errors
  184. //
  185. if (DB::isError($ownedPrivateIDs))
  186. {
  187. $msg = $ownedPrivateIDs->getMessage();
  188. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query owned private calendar IDs - ' . $msg, $color);
  189. exit;
  190. }
  191. // reduce to single list of IDs
  192. //
  193. $ownedPrivateCalIDs = array();
  194. foreach ($ownedPrivateIDs as $ID) $ownedPrivateCalIDs[] = $ID[0];
  195. // -------------------------------
  196. // get private calendars with owner names that have wildcards in them
  197. //
  198. $sql = $wildcard_calendar_owners_of_type_query;
  199. $sql = str_replace('%1', SM_CAL_TYPE_PERSONAL, $sql);
  200. $wildcardOwners = $db->getAll($sql);
  201. // check for database errors
  202. //
  203. if (DB::isError($wildcardOwners))
  204. {
  205. $msg = $wildcardOwners->getMessage();
  206. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query owners with wildcards - ' . $msg, $color);
  207. exit;
  208. }
  209. // figure out which ones match current user
  210. //
  211. foreach ($wildcardOwners as $owner)
  212. {
  213. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  214. strtoupper($owner[0])) . '$/', strtoupper($user)))
  215. $ownedPrivateCalIDs[] = $owner[1];
  216. }
  217. // -------------------------------
  218. // get readable private cals
  219. //
  220. $sql = $all_readable_calendars_of_type_query;
  221. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_PERSONAL), $sql);
  222. $readablePrivateIDs = $db->getAll($sql);
  223. // check for database errors
  224. //
  225. if (DB::isError($readablePrivateIDs))
  226. {
  227. $msg = $readablePrivateIDs->getMessage();
  228. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query readable private calendar IDs - ' . $msg, $color);
  229. exit;
  230. }
  231. // reduce to single list of IDs
  232. //
  233. $readablePrivateCalIDs = array();
  234. foreach ($readablePrivateIDs as $ID) $readablePrivateCalIDs[] = $ID[0];
  235. // -------------------------------
  236. // get private calendars with reader names that have wildcards in them
  237. //
  238. $sql = $wildcard_calendar_readers_of_type_query;
  239. $sql = str_replace('%1', SM_CAL_TYPE_PERSONAL, $sql);
  240. $wildcardReaders = $db->getAll($sql);
  241. // check for database errors
  242. //
  243. if (DB::isError($wildcardReaders))
  244. {
  245. $msg = $wildcardReaders->getMessage();
  246. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query readers with wildcards - ' . $msg, $color);
  247. exit;
  248. }
  249. // figure out which ones match current user
  250. //
  251. foreach ($wildcardReaders as $reader)
  252. {
  253. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  254. strtoupper($reader[0])) . '$/', strtoupper($user)))
  255. $readablePrivateCalIDs[] = $reader[1];
  256. }
  257. // -------------------------------
  258. // get writeable private cals
  259. //
  260. $sql = $all_writeable_calendars_of_type_query;
  261. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_PERSONAL), $sql);
  262. $writeablePrivateIDs = $db->getAll($sql);
  263. // check for database errors
  264. //
  265. if (DB::isError($writeablePrivateIDs))
  266. {
  267. $msg = $writeablePrivateIDs->getMessage();
  268. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query writeable private calendar IDs - ' . $msg, $color);
  269. exit;
  270. }
  271. // reduce to single list of IDs
  272. //
  273. $writeablePrivateCalIDs = array();
  274. foreach ($writeablePrivateIDs as $ID) $writeablePrivateCalIDs[] = $ID[0];
  275. // -------------------------------
  276. // get private calendars with writer names that have wildcards in them
  277. //
  278. $sql = $wildcard_calendar_writers_of_type_query;
  279. $sql = str_replace('%1', SM_CAL_TYPE_PERSONAL, $sql);
  280. $wildcardWriters = $db->getAll($sql);
  281. // check for database errors
  282. //
  283. if (DB::isError($wildcardWriters))
  284. {
  285. $msg = $wildcardWriters->getMessage();
  286. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query writers with wildcards - ' . $msg, $color);
  287. exit;
  288. }
  289. // figure out which ones match current user
  290. //
  291. foreach ($wildcardWriters as $writer)
  292. {
  293. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  294. strtoupper($writer[0])) . '$/', strtoupper($user)))
  295. $writeablePrivateCalIDs[] = $writer[1];
  296. }
  297. // -------------------------------
  298. // wrap up private calendars
  299. //
  300. $privateCalIDs = array_unique(array_merge($writeablePrivateCalIDs,
  301. $readablePrivateCalIDs,
  302. $ownedPrivateCalIDs));
  303. // remove default personal calendar from private cal list
  304. //
  305. $privateCalID = get_personal_cal_id($user, $domain);
  306. $privateCalIDs = array_diff($privateCalIDs, array($privateCalID));
  307. // -------------------------------
  308. // get owned shared cals
  309. //
  310. $sql = $all_owned_calendars_of_type_query;
  311. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_SHARED), $sql);
  312. $ownedSharedIDs = $db->getAll($sql);
  313. // check for database errors
  314. //
  315. if (DB::isError($ownedSharedIDs))
  316. {
  317. $msg = $ownedSharedIDs->getMessage();
  318. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query owned shared calendar IDs - ' . $msg, $color);
  319. exit;
  320. }
  321. // reduce to single list of IDs
  322. //
  323. $ownedSharedCalIDs = array();
  324. foreach ($ownedSharedIDs as $ID) $ownedSharedCalIDs[] = $ID[0];
  325. // -------------------------------
  326. // get shared calendars with owner names that have wildcards in them
  327. //
  328. $sql = $wildcard_calendar_owners_of_type_query;
  329. $sql = str_replace('%1', SM_CAL_TYPE_SHARED, $sql);
  330. $wildcardOwners = $db->getAll($sql);
  331. // check for database errors
  332. //
  333. if (DB::isError($wildcardOwners))
  334. {
  335. $msg = $wildcardOwners->getMessage();
  336. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query owners with wildcards - ' . $msg, $color);
  337. exit;
  338. }
  339. // figure out which ones match current user
  340. //
  341. foreach ($wildcardOwners as $owner)
  342. {
  343. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  344. strtoupper($owner[0])) . '$/', strtoupper($user)))
  345. $ownedSharedCalIDs[] = $owner[1];
  346. }
  347. // -------------------------------
  348. // get readable shared cals
  349. //
  350. $sql = $all_readable_calendars_of_type_query;
  351. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_SHARED), $sql);
  352. $readableSharedIDs = $db->getAll($sql);
  353. // check for database errors
  354. //
  355. if (DB::isError($readableSharedIDs))
  356. {
  357. $msg = $readableSharedIDs->getMessage();
  358. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query readable shared calendar IDs - ' . $msg, $color);
  359. exit;
  360. }
  361. // reduce to single list of IDs
  362. //
  363. $readableSharedCalIDs = array();
  364. foreach ($readableSharedIDs as $ID) $readableSharedCalIDs[] = $ID[0];
  365. // -------------------------------
  366. // get shared calendars with reader names that have wildcards in them
  367. //
  368. $sql = $wildcard_calendar_readers_of_type_query;
  369. $sql = str_replace('%1', SM_CAL_TYPE_SHARED, $sql);
  370. $wildcardReaders = $db->getAll($sql);
  371. // check for database errors
  372. //
  373. if (DB::isError($wildcardReaders))
  374. {
  375. $msg = $wildcardReaders->getMessage();
  376. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query readers with wildcards - ' . $msg, $color);
  377. exit;
  378. }
  379. // figure out which ones match current user
  380. //
  381. foreach ($wildcardReaders as $reader)
  382. {
  383. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  384. strtoupper($reader[0])) . '$/', strtoupper($user)))
  385. $readableSharedCalIDs[] = $reader[1];
  386. }
  387. // -------------------------------
  388. // get writeable shared cals
  389. //
  390. $sql = $all_writeable_calendars_of_type_query;
  391. $sql = str_replace(array('%1', '%2'), array($user, SM_CAL_TYPE_SHARED), $sql);
  392. $writeableSharedIDs = $db->getAll($sql);
  393. // check for database errors
  394. //
  395. if (DB::isError($writeableSharedIDs))
  396. {
  397. $msg = $writeableSharedIDs->getMessage();
  398. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query writeable shared calendar IDs - ' . $msg, $color);
  399. exit;
  400. }
  401. // reduce to single list of IDs
  402. //
  403. $writeableSharedCalIDs = array();
  404. foreach ($writeableSharedIDs as $ID) $writeableSharedCalIDs[] = $ID[0];
  405. // -------------------------------
  406. // get shared calendars with writer names that have wildcards in them
  407. //
  408. $sql = $wildcard_calendar_writers_of_type_query;
  409. $sql = str_replace('%1', SM_CAL_TYPE_SHARED, $sql);
  410. $wildcardWriters = $db->getAll($sql);
  411. // check for database errors
  412. //
  413. if (DB::isError($wildcardWriters))
  414. {
  415. $msg = $wildcardWriters->getMessage();
  416. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_all_accessible_calendars_do): could not query writers with wildcards - ' . $msg, $color);
  417. exit;
  418. }
  419. // figure out which ones match current user
  420. //
  421. foreach ($wildcardWriters as $writer)
  422. {
  423. if (preg_match('/^' . str_replace(array('?', '*'), array('\w{1}', '.*?'),
  424. strtoupper($writer[0])) . '$/', strtoupper($user)))
  425. $writeableSharedCalIDs[] = $writer[1];
  426. }
  427. // -------------------------------
  428. // wrap up shared calendars
  429. //
  430. $calIDs = array_unique(array_merge($writeableSharedCalIDs, $readableSharedCalIDs,
  431. $ownedSharedCalIDs, $privateCalIDs));
  432. $calList = array();
  433. foreach ($calIDs as $calID)
  434. $calList[] = cal_sql_get_calendar_do($calID);
  435. return $calList;
  436. }
  437. /**
  438. * Get calendar
  439. *
  440. * Retrieves the given calendar from the backend
  441. *
  442. * @param string $calendarID The ID of the calendar to be retrieved
  443. * @param boolean $quiet When FALSE, if the requested calendar isn't
  444. * found, an error is shown and execution halts;
  445. * otherwise, FALSE is just returned quietly
  446. * (optional; default = FALSE)
  447. *
  448. * @return mixed A Calendar object corresponding to the desired
  449. * calendar, or FALSE if the calendar is not found
  450. * and the $quiet parameter is TRUE
  451. *
  452. */
  453. function cal_sql_get_calendar_do($calendarID, $quiet=FALSE)
  454. {
  455. global $calendar_ical_query, $color;
  456. // get database connection
  457. //
  458. $db = cal_get_database_connection();
  459. $sql = $calendar_ical_query;
  460. $sql = str_replace('%1', $calendarID, $sql);
  461. $iCal = $db->getAll($sql);
  462. // check for database errors
  463. //
  464. if (DB::isError($iCal))
  465. {
  466. $msg = $iCal->getMessage();
  467. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_calendar_do): could not query calendar - ' . $msg, $color);
  468. exit;
  469. }
  470. if (sizeof($iCal))
  471. return Calendar::getCalendarFromICal(preg_split("/(\r\n)|(\n)|(\r)/",
  472. $iCal[0][0]));
  473. if (!$quiet)
  474. {
  475. global $color;
  476. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_get_calendar_do): cannot find calendar for calendar ID ' . $calendarID, $color);
  477. exit;
  478. }
  479. return FALSE;
  480. }
  481. /**
  482. * Creates a new calendar
  483. *
  484. * Takes the given calendar object and inserts it into the
  485. * backend as a new calendar. The ID contained in the given
  486. * calendar is expected to be a correct unique ID value.
  487. *
  488. * @param object $calendar The new calendar object
  489. *
  490. */
  491. function cal_sql_create_calendar_do($calendar)
  492. {
  493. global $insert_cal_query, $insert_cal_owner_query, $insert_cal_reader_query,
  494. $insert_cal_writer_query, $color;
  495. // make sure the calendar doesn't already exist
  496. //
  497. $cal = cal_sql_get_calendar_do($calendar->getID(), TRUE);
  498. if ($cal !== FALSE)
  499. {
  500. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_create_calendar_do): calendar ' . $calendar->getID() . ' already exists!', $color);
  501. exit;
  502. }
  503. // get database connection
  504. //
  505. $db = cal_get_database_connection();
  506. // write calendar out to database
  507. //
  508. $sql = $insert_cal_query;
  509. $sql = str_replace(array('%1', '%2', '%3', '%4', '%5', '%6', '%7'),
  510. array($calendar->getID(), $calendar->getCalendarType(),
  511. $calendar->getName(), $calendar->getDomain(),
  512. date('Y-m-d H:i:s', $calendar->createdOn()),
  513. date('Y-m-d H:i:s', $calendar->lastUpdatedOn()),
  514. $calendar->getICal(TRUE)), $sql);
  515. // send query to database
  516. //
  517. $result = $db->query($sql);
  518. // check for database errors
  519. //
  520. if (DB::isError($result))
  521. {
  522. $msg = $result->getMessage();
  523. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_create_calendar_do): cannot write calendar to database - ' . $msg, $color);
  524. exit;
  525. }
  526. // write calendar owners out to database
  527. //
  528. foreach ($calendar->getOwners() as $owner)
  529. {
  530. $sql = $insert_cal_owner_query;
  531. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $owner), $sql);
  532. // send query to database
  533. //
  534. $result = $db->query($sql);
  535. // check for database errors
  536. //
  537. if (DB::isError($result))
  538. {
  539. $msg = $result->getMessage();
  540. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_create_calendar_do): cannot write calendar owner to database - ' . $msg, $color);
  541. exit;
  542. }
  543. }
  544. // write calendar readers out to database
  545. //
  546. foreach ($calendar->getReadableUsers() as $reader)
  547. {
  548. $sql = $insert_cal_reader_query;
  549. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $reader), $sql);
  550. // send query to database
  551. //
  552. $result = $db->query($sql);
  553. // check for database errors
  554. //
  555. if (DB::isError($result))
  556. {
  557. $msg = $result->getMessage();
  558. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_create_calendar_do): cannot write calendar reader to database - ' . $msg, $color);
  559. exit;
  560. }
  561. }
  562. // write calendar writers out to database
  563. //
  564. foreach ($calendar->getWriteableUsers() as $writer)
  565. {
  566. $sql = $insert_cal_writer_query;
  567. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $writer), $sql);
  568. // send query to database
  569. //
  570. $result = $db->query($sql);
  571. // check for database errors
  572. //
  573. if (DB::isError($result))
  574. {
  575. $msg = $result->getMessage();
  576. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_create_calendar_do): cannot write calendar writer to database - ' . $msg, $color);
  577. exit;
  578. }
  579. }
  580. }
  581. /**
  582. * Updates a calendar
  583. *
  584. * Updates the given calendar by replacing it in the backend
  585. * with the given calendar object.
  586. *
  587. * @param object $calendar The updated calendar object
  588. *
  589. */
  590. function cal_sql_update_calendar_do($calendar)
  591. {
  592. global $update_cal_query, $delete_cal_owners_readers_writers_queries,
  593. $insert_cal_owner_query, $insert_cal_reader_query, $insert_cal_writer_query,
  594. $color;
  595. // make sure the calendar exists
  596. //
  597. $cal = cal_sql_get_calendar_do($calendar->getID(), TRUE);
  598. if ($cal === FALSE)
  599. {
  600. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): calendar ' . $calendar->getID() . ' does not exist', $color);
  601. exit;
  602. }
  603. // get database connection
  604. //
  605. $db = cal_get_database_connection();
  606. // write calendar out to database
  607. //
  608. $sql = $update_cal_query;
  609. $sql = str_replace(array('%1', '%2', '%3', '%4', '%5', '%6', '%7'),
  610. array($calendar->getID(), $calendar->getCalendarType(),
  611. $calendar->getName(), $calendar->getDomain(),
  612. date('Y-m-d H:i:s', $calendar->createdOn()),
  613. date('Y-m-d H:i:s', $calendar->lastUpdatedOn()),
  614. $calendar->getICal(TRUE)), $sql);
  615. // send query to database
  616. //
  617. $result = $db->query($sql);
  618. // check for database errors
  619. //
  620. if (DB::isError($result))
  621. {
  622. $msg = $result->getMessage();
  623. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): cannot update calendar in database - ' . $msg, $color);
  624. exit;
  625. }
  626. // remove calendar owners, readers and writers
  627. //
  628. foreach ($delete_cal_owners_readers_writers_queries as $query)
  629. {
  630. $sql = $query;
  631. $sql = str_replace('%1', $calendar->getID(), $sql);
  632. // send query to database
  633. //
  634. $result = $db->query($sql);
  635. // check for database errors
  636. //
  637. if (DB::isError($result))
  638. {
  639. $msg = $result->getMessage();
  640. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): cannot delete calendar owner/reader/writer - ' . $msg, $color);
  641. exit;
  642. }
  643. }
  644. // write calendar owners out to database
  645. //
  646. foreach ($calendar->getOwners() as $owner)
  647. {
  648. $sql = $insert_cal_owner_query;
  649. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $owner), $sql);
  650. // send query to database
  651. //
  652. $result = $db->query($sql);
  653. // check for database errors
  654. //
  655. if (DB::isError($result))
  656. {
  657. $msg = $result->getMessage();
  658. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): cannot write calendar owner to database - ' . $msg, $color);
  659. exit;
  660. }
  661. }
  662. // write calendar readers out to database
  663. //
  664. foreach ($calendar->getReadableUsers() as $reader)
  665. {
  666. $sql = $insert_cal_reader_query;
  667. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $reader), $sql);
  668. // send query to database
  669. //
  670. $result = $db->query($sql);
  671. // check for database errors
  672. //
  673. if (DB::isError($result))
  674. {
  675. $msg = $result->getMessage();
  676. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): cannot write calendar reader to database - ' . $msg, $color);
  677. exit;
  678. }
  679. }
  680. // write calendar writers out to database
  681. //
  682. foreach ($calendar->getWriteableUsers() as $writer)
  683. {
  684. $sql = $insert_cal_writer_query;
  685. $sql = str_replace(array('%1', '%2'), array($calendar->getID(), $writer), $sql);
  686. // send query to database
  687. //
  688. $result = $db->query($sql);
  689. // check for database errors
  690. //
  691. if (DB::isError($result))
  692. {
  693. $msg = $result->getMessage();
  694. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_update_calendar_do): cannot write calendar writer to database - ' . $msg, $color);
  695. exit;
  696. }
  697. }
  698. }
  699. /**
  700. * Delete calendar
  701. *
  702. * Removes the given calendar from the system.
  703. *
  704. * @param string $calendarID The ID of the calendar to be removed
  705. *
  706. */
  707. function cal_sql_delete_calendar_do($calendarID)
  708. {
  709. global $delete_cal_queries, $username, $color;
  710. include_once(SM_PATH . 'plugins/calendar_sql_backend/event_functions.php');
  711. // remove all events first
  712. //
  713. $events = cal_sql_get_all_events_do($calendarID, $username);
  714. foreach ($events as $event)
  715. cal_sql_delete_event_do($calendarID, $event->getID());
  716. // get database connection
  717. //
  718. $db = cal_get_database_connection();
  719. // remove calendar
  720. //
  721. foreach ($delete_cal_queries as $query)
  722. {
  723. $sql = $query;
  724. $sql = str_replace('%1', $calendarID, $sql);
  725. // send query to database
  726. //
  727. $result = $db->query($sql);
  728. // check for database errors
  729. //
  730. if (DB::isError($result))
  731. {
  732. $msg = $result->getMessage();
  733. plain_error_message('ERROR IN CALENDAR SQL BACKEND (cal_sql_delete_calendar_do): cannot delete calendar - ' . $msg, $color);
  734. exit;
  735. }
  736. }
  737. }
  738. ?>