i_cal_object.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. //TODO: make a helper
  3. class ICalObject {
  4. var $config = array('database' => '');
  5. var $__count = 0;
  6. var $__lastInsertId = null;
  7. var $__data = null;
  8. var $__timezones = array('US-Eastern');
  9. var $columns = array('primary_key' => array('name' => 'uid'),
  10. 'string' => array('name' => 'string'),
  11. 'timestamp' => array('name' => 'timestamp', 'format' => 'Ymd/T/His'),
  12. 'datetime' => array('name' => 'timestamp', 'format' => 'Ymd/T/His')//,
  13. );
  14. var $__keyMap = array(
  15. 'id' => 'uid',
  16. 'end_date' => 'dtend',
  17. 'start_date' => 'dtstart',
  18. 'date_stamp' => 'dtstamp'
  19. );
  20. var $__textMap = array(
  21. '"' => 'DQUOTE',
  22. ',' => '\,',
  23. //':' => '":"', // Not sure about this one
  24. ';' => '\;',
  25. '\\' => '\\\\',
  26. '\n' => '\\n'
  27. );
  28. function listSources() {
  29. return array('calendars', 'events', 'todos', 'alarms', 'journals');
  30. }
  31. function create($data) {
  32. return $this->__output($data);
  33. }
  34. function read($filename = null) {
  35. if ($filename != null) {
  36. $this->__count = 0;
  37. $this->config['database'] = $filename;
  38. }
  39. if ($this->__data == null) {
  40. $this->__data = $this->__parse(file_get_contents($this->config['database']));
  41. }
  42. return $this->__data;
  43. }
  44. function update($data) { }
  45. function delete($data) { }
  46. function __output($data) {
  47. $out = '';
  48. foreach($data as $key => $val) {
  49. $keyAppend = '';
  50. if (in_array($key, array('Calendar', 'Event', 'Timezone', 'Todo', 'Alarm', 'Journals'))) {
  51. $key = 'v' . $key;
  52. }
  53. if (is_array($val) && strtolower($key) != $key) {
  54. if (countdim($val) > 1) {
  55. foreach ($val as $val2) {
  56. $out .= strtoupper("begin:{$key}\n");
  57. $out .= $this->__output($val2);
  58. $out .= strtoupper("end:{$key}\n");
  59. }
  60. } else {
  61. $out .= strtoupper("begin:{$key}\n");
  62. $out .= $this->__output($val);
  63. $out .= strtoupper("end:{$key}\n");
  64. }
  65. } else {
  66. if (is_array($val)) {
  67. $tmp = array();
  68. foreach ($val as $key2 => $val2) {
  69. if ($key2 !== 0) {
  70. $tmp[] = strtoupper($key2) . '=' . $val2;
  71. }
  72. }
  73. if (!empty($tmp)) {
  74. $keyAppend = ';' . join(';', $tmp);
  75. }
  76. $_val = $val[0];
  77. } else {
  78. $_val = $val;
  79. }
  80. switch ($key) {
  81. case 'end_date':
  82. case 'start_date':
  83. case 'date_stamp':
  84. case 'last_modified':
  85. case 'trigger':
  86. if (strpos($_val, ' weeks') === false && strpos($_val, ' days') === false && strpos($_val, ' hours') === false && strpos($_val, ' minutes') === false && strpos($_val, ' seconds') === false) {
  87. $utc = false;
  88. if (strpos($_val, 'UTC')) {
  89. $utc = true;
  90. }
  91. if (strpos($_val, ' ') === false && strpos($_val, ':') === false) {
  92. $val = date('Ymd', strtotime($_val));
  93. } else {
  94. $_val = trim(r('UTC', '', $_val));
  95. $tmp = date('Ymd', strtotime($_val)).'T'.date('His', strtotime($_val));
  96. if ($utc) {
  97. $tmp .= 'Z';
  98. }
  99. $val = $tmp;
  100. }
  101. } else {
  102. $val = $this->__putDuration($val);
  103. }
  104. break;
  105. case 'duration':
  106. $val = $this->__putDuration($val);
  107. break;
  108. case 'contact':
  109. case 'comment':
  110. case 'description':
  111. case 'location':
  112. case 'prodid':
  113. case 'resources':
  114. case 'status':
  115. case 'summary':
  116. $s = array_keys($this->__textMap);
  117. $r = array_values($this->__textMap);
  118. $val = str_replace($s, $r, $val);
  119. $val = str_replace('\\\\', '\\', $val);
  120. break;
  121. default:
  122. if ($val === true) {
  123. $val = 'TRUE';
  124. } elseif ($val === false) {
  125. $val = 'FALSE';
  126. }
  127. break;
  128. }
  129. if (in_array($key, array_keys($this->__keyMap))) {
  130. $key = $this->__keyMap[$key];
  131. }
  132. if (is_array($val) && isset($val[0])) {
  133. $val = $val[0];
  134. }
  135. $out .= strtoupper(str_replace('_', '-', $key)) . $keyAppend . ':' . $val . "\n";
  136. }
  137. }
  138. return $out;
  139. }
  140. function __parse(&$lines) {
  141. if (is_string($lines)) {
  142. $lines = str_replace("\r", '', $lines);
  143. $lines = explode("\n", $lines);
  144. $lines1 = ($lines);
  145. for ($i = 0; $i < count($lines); $i++) {
  146. if (substr($lines[$i], 0, 1) == ' ') {
  147. $lines[$i - 1] .= substr($lines[$i], 1);
  148. array_splice($lines, $i, 1);
  149. } elseif ($lines[$i] == '') {
  150. array_splice($lines, $i, 1);
  151. }
  152. }
  153. }
  154. $data = array();
  155. for ($i = $this->__count; $i < count($lines); $i++) {
  156. $idx = strpos($lines[$i], ':');
  157. $key = str_replace('-', '_', substr($lines[$i], 0, $idx));
  158. $value = substr($lines[$i], $idx + 1);
  159. if (strtolower($key) == 'end') {
  160. $this->__count = $i++;
  161. return $data;
  162. } elseif (strtolower($key) == 'begin') {
  163. $key = ucwords(strtolower($value));
  164. if ($key{0} == 'V') {
  165. $key = ucwords(substr($key, 1));
  166. }
  167. $this->__count = ++$i;
  168. $value = $this->__parse($lines);
  169. $i = $this->__count;
  170. } else {
  171. if (strpos($key, ';')) {
  172. $key = explode(';', $key);
  173. $props = $key;
  174. $key = $key[0];
  175. array_shift($props);
  176. $value = array($value);
  177. foreach ($props as $v) {
  178. $tmp = explode('=', $v);
  179. if (isset($tmp[1])) {
  180. $value[strtolower($tmp[0])] = $tmp[1];
  181. }
  182. }
  183. }
  184. $key = strtolower($key);
  185. }
  186. if (in_array($key, $this->__keyMap)) {
  187. $reverse = array_combine(array_values($this->__keyMap), array_keys($this->__keyMap));
  188. $key = $reverse[$key];
  189. }
  190. // Format the data types
  191. switch ($key) {
  192. case 'end_date':
  193. case 'start_date':
  194. case 'date_stamp':
  195. case 'last_modified':
  196. case 'trigger':
  197. if (is_array($value)) {
  198. $value[0] = $this->__timestamp($value[0]);
  199. } elseif (strpos(strtolower($value), '-p') !== false || strpos(strtolower($value), 'p') !== false) {
  200. $value = $this->__duration($value);
  201. } else {
  202. $value = $this->__timestamp($value);
  203. }
  204. break;
  205. case 'duration':
  206. $value = $this->__duration($value);
  207. break;
  208. case 'contact':
  209. case 'comment':
  210. case 'description':
  211. case 'location':
  212. case 'prodid':
  213. case 'resources':
  214. case 'status':
  215. case 'summary':
  216. $r = array_keys($this->__textMap);
  217. $s = array_values($this->__textMap);
  218. $value = str_replace($s, $r, $value);
  219. break;
  220. default:
  221. if ($value == 'TRUE') {
  222. $value = true;
  223. } elseif ($value == 'FALSE') {
  224. $value = false;
  225. }
  226. break;
  227. }
  228. if (isset($data[$key])) {
  229. if (!isset($data[$key][0])) {
  230. $data[$key] = array($data[$key]);
  231. $data[$key][] = $value;
  232. } elseif (isset($data[$key][0]) && is_array($data[$key])) {
  233. $data[$key][] = $value;
  234. }
  235. } else {
  236. $data[$key] = $value;
  237. }
  238. }
  239. return $data;
  240. }
  241. // Event UID generator
  242. function __insertID() {
  243. $chunk = array();
  244. $hash = strtoupper(md5(intval(str_replace('.', '', env('SERVER_ADDR'))).''.intval(rand() * 1000).time()));
  245. $chunk[] = substr($hash, 0, 8);
  246. $chunk[] = substr($hash, 8, 4);
  247. $chunk[] = substr($hash, 12, 4);
  248. $chunk[] = substr($hash, 16, 4);
  249. $chunk[] = substr($hash, 20);
  250. $this->__lastInsertId = join('-', $chunk);
  251. return $this->__lastInsertId;
  252. }
  253. function lastInsertId() {
  254. return $this->__lastInsertId;
  255. }
  256. function __timestamp($time) {
  257. if (strpos(strtolower($time), 'p') === 0 || strpos(strtolower($time), 'p') === 1) {
  258. return $this->__duration($time);
  259. }
  260. $utc = false;
  261. if (strpos(strtolower($time), 'z')) {
  262. $utc = true;
  263. }
  264. if (strpos(strtolower($time), 't') === false) {
  265. return date('Y-m-d', strtotime($time));
  266. }
  267. $time = explode('t', str_replace('z', '', strtolower($time)));
  268. $time[1] = substr($time[1], 0, 2).':'.substr($time[1], 2, 2).':'.substr($time[1], 4, 2);
  269. return date('Y-m-d', strtotime($time[0])).' '.$time[1] . ($utc ? ' UTC' : '');
  270. }
  271. function __putDuration($time, $date = null) {
  272. $negative = false;
  273. if (is_string($time)) {
  274. if (strpos($time, '-') === 0) {
  275. $time = substr($time, 1);
  276. $negative = true;
  277. }
  278. $time = strtotime('+' . $time) - strtotime('now');;
  279. }
  280. if ($time < 0) {
  281. $time = abs($time);
  282. $negative = true;
  283. }
  284. $out = 'P';
  285. $t = false;
  286. $offset = array('W' => 604800, 'D' => 86400, 'H' => 3600, 'M' => 60, 'S' => 1);
  287. if ($date != null) {
  288. $time = strtotime($date) - strtotime($time);
  289. }
  290. foreach ($offset as $key => $val) {
  291. $tmp = 0;
  292. if ($time >= $val) {
  293. $tmp = $time / $val;
  294. }
  295. if ($tmp >= 1) {
  296. if (in_array($key, array('H', 'M', 'S')) && $t == false) {
  297. $t = true;
  298. $out .= 'T';
  299. }
  300. $out .= $tmp.$key;
  301. $time -= $tmp * $val;
  302. }
  303. }
  304. return ($negative ? '-' : '') . $out;
  305. }
  306. function __duration($dur) {
  307. $tmp = '';
  308. $out = '';
  309. for ($i = 0; $i < strlen($dur); $i++) {
  310. switch(strtolower($dur{$i})) {
  311. case 't':
  312. case 'p':
  313. // do nothing
  314. break;
  315. case 'w':
  316. $out .= $tmp . ' week' . (intval($tmp) != 1 ? 's' : '') . ' ';
  317. $tmp = '';
  318. break;
  319. case 'd':
  320. $out .= $tmp . ' day' . (intval($tmp) != 1 ? 's' : '') . ' ';
  321. $tmp = '';
  322. break;
  323. case 'h':
  324. $out .= $tmp . ' hour' . (intval($tmp) != 1 ? 's' : '') . ' ';
  325. $tmp = '';
  326. break;
  327. case 'm':
  328. $out .= $tmp . ' minute' . (intval($tmp) != 1 ? 's' : '') . ' ';
  329. $tmp = '';
  330. break;
  331. case 's':
  332. $out .= $tmp . ' second' . (intval($tmp) != 1 ? 's' : '') . ' ';
  333. $tmp = '';
  334. break;
  335. default:
  336. $tmp .= $dur{$i};
  337. break;
  338. }
  339. }
  340. return trim($out);
  341. }
  342. function debug_compare($lines1) {
  343. $data = $this->__parse($lines1);
  344. pr($data);
  345. $diff = 0;
  346. $lines2 = explode("\n", $this->__output($data));
  347. pr($lines2);
  348. e('<table border=1>');
  349. foreach ($lines1 as $i => $val) {
  350. e('<tr><td>');
  351. pr($i . ' : ' . $val);
  352. e('</td><td>');
  353. if ($lines2[$i] != $val) {
  354. pr($lines2[$i]);
  355. $diff++;
  356. }
  357. e('</td></tr>');
  358. }
  359. e('</table>');
  360. pr('Diffs : '.$diff);
  361. die();
  362. }
  363. }