Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

389 lines
11 KiB

  1. <?php
  2. function format_event_html($ev) {
  3. require_once('include/bbcode.php');
  4. if(! ((is_array($ev)) && count($ev)))
  5. return '';
  6. $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
  7. $o = '<div class="vevent">' . "\r\n";
  8. $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
  9. $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
  10. . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
  11. . '" >'
  12. . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
  13. $ev['start'] , $bd_format ))
  14. : day_translate(datetime_convert('UTC', 'UTC',
  15. $ev['start'] , $bd_format)))
  16. . '</abbr></p>' . "\r\n";
  17. if(! $ev['nofinish'])
  18. $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
  19. . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
  20. . '" >'
  21. . (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
  22. $ev['finish'] , $bd_format ))
  23. : day_translate(datetime_convert('UTC', 'UTC',
  24. $ev['finish'] , $bd_format )))
  25. . '</abbr></p>' . "\r\n";
  26. if(strlen($ev['location']))
  27. $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
  28. . bbcode($ev['location'])
  29. . '</span></p>' . "\r\n";
  30. $o .= '</div>' . "\r\n";
  31. return $o;
  32. }
  33. function parse_event($h) {
  34. require_once('include/Scrape.php');
  35. require_once('library/HTMLPurifier.auto.php');
  36. require_once('include/html2bbcode');
  37. $h = '<html><body>' . $h . '</body></html>';
  38. $ret = array();
  39. try {
  40. $dom = HTML5_Parser::parse($h);
  41. } catch (DOMException $e) {
  42. logger('parse_event: parse error: ' . $e);
  43. }
  44. if(! $dom)
  45. return $ret;
  46. $items = $dom->getElementsByTagName('*');
  47. foreach($items as $item) {
  48. if(attribute_contains($item->getAttribute('class'), 'vevent')) {
  49. $level2 = $item->getElementsByTagName('*');
  50. foreach($level2 as $x) {
  51. if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
  52. $ret['start'] = $x->getAttribute('title');
  53. if(! strpos($ret['start'],'Z'))
  54. $ret['adjust'] = true;
  55. }
  56. if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
  57. $ret['finish'] = $x->getAttribute('title');
  58. if(attribute_contains($x->getAttribute('class'),'description'))
  59. $ret['desc'] = $x->textContent;
  60. if(attribute_contains($x->getAttribute('class'),'location'))
  61. $ret['location'] = $x->textContent;
  62. }
  63. }
  64. }
  65. // sanitise
  66. if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
  67. $config = HTMLPurifier_Config::createDefault();
  68. $config->set('Cache.DefinitionImpl', null);
  69. $purifier = new HTMLPurifier($config);
  70. $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
  71. }
  72. if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
  73. $config = HTMLPurifier_Config::createDefault();
  74. $config->set('Cache.DefinitionImpl', null);
  75. $purifier = new HTMLPurifier($config);
  76. $ret['location'] = html2bbcode($purifier->purify($ret['location']));
  77. }
  78. if(x($ret,'start'))
  79. $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
  80. if(x($ret,'finish'))
  81. $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
  82. return $ret;
  83. }
  84. function format_event_bbcode($ev) {
  85. $o = '';
  86. if($ev['desc'])
  87. $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
  88. if($ev['start'])
  89. $o .= '[event-start]' . $ev['start'] . '[/event-start]';
  90. if(($ev['finish']) && (! $ev['nofinish']))
  91. $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
  92. if($ev['location'])
  93. $o .= '[event-location]' . $ev['location'] . '[/event-location]';
  94. if($ev['adjust'])
  95. $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
  96. return $o;
  97. }
  98. function bbtovcal($s) {
  99. $o = '';
  100. $ev = bbtoevent($s);
  101. if($ev['desc'])
  102. $o = format_event_html($ev);
  103. return $o;
  104. }
  105. function bbtoevent($s) {
  106. $ev = array();
  107. $match = '';
  108. if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
  109. $ev['desc'] = $match[1];
  110. $match = '';
  111. if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
  112. $ev['start'] = $match[1];
  113. $match = '';
  114. if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
  115. $ev['finish'] = $match[1];
  116. $match = '';
  117. if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
  118. $ev['location'] = $match[1];
  119. $match = '';
  120. if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
  121. $ev['adjust'] = $match[1];
  122. $match = '';
  123. $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
  124. return $ev;
  125. }
  126. function sort_by_date($a) {
  127. usort($a,'ev_compare');
  128. return $a;
  129. }
  130. function ev_compare($a,$b) {
  131. $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
  132. $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
  133. if($date_a === $date_b)
  134. return strcasecmp($a['desc'],$b['desc']);
  135. return strcmp($date_a,$date_b);
  136. }
  137. function event_store($arr) {
  138. require_once('include/datetime.php');
  139. require_once('include/items.php');
  140. require_once('include/bbcode.php');
  141. $a = get_app();
  142. $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
  143. $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
  144. $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
  145. $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
  146. $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
  147. $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
  148. if($arr['cid'])
  149. $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  150. intval($arr['cid']),
  151. intval($arr['uid'])
  152. );
  153. else
  154. $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
  155. intval($arr['uid'])
  156. );
  157. if(count($c))
  158. $contact = $c[0];
  159. // Existing event being modified
  160. if($arr['id']) {
  161. // has the event actually changed?
  162. $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  163. intval($arr['id']),
  164. intval($arr['uid'])
  165. );
  166. if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
  167. // Nothing has changed. Grab the item id to return.
  168. $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
  169. intval($arr['id']),
  170. intval($arr['uid'])
  171. );
  172. return((count($r)) ? $r[0]['id'] : 0);
  173. }
  174. // The event changed. Update it.
  175. $r = q("UPDATE `event` SET
  176. `edited` = '%s',
  177. `start` = '%s',
  178. `finish` = '%s',
  179. `desc` = '%s',
  180. `location` = '%s',
  181. `type` = '%s',
  182. `adjust` = %d,
  183. `nofinish` = %d,
  184. `allow_cid` = '%s',
  185. `allow_gid` = '%s',
  186. `deny_cid` = '%s',
  187. `deny_gid` = '%s'
  188. WHERE `id` = %d AND `uid` = %d LIMIT 1",
  189. dbesc($arr['edited']),
  190. dbesc($arr['start']),
  191. dbesc($arr['finish']),
  192. dbesc($arr['desc']),
  193. dbesc($arr['location']),
  194. dbesc($arr['type']),
  195. intval($arr['adjust']),
  196. intval($arr['nofinish']),
  197. dbesc($arr['allow_cid']),
  198. dbesc($arr['allow_gid']),
  199. dbesc($arr['deny_cid']),
  200. dbesc($arr['deny_gid']),
  201. intval($arr['id']),
  202. intval($arr['uid'])
  203. );
  204. $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
  205. intval($arr['id']),
  206. intval($arr['uid'])
  207. );
  208. if(count($r)) {
  209. $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
  210. $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
  211. $object .= '</object>' . "\n";
  212. q("UPDATE `item` SET `body` = '%s', `object` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `edited` = '%s', `private` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
  213. dbesc(format_event_bbcode($arr)),
  214. dbesc($object),
  215. dbesc($arr['allow_cid']),
  216. dbesc($arr['allow_gid']),
  217. dbesc($arr['deny_cid']),
  218. dbesc($arr['deny_gid']),
  219. dbesc($arr['edited']),
  220. intval($arr['private']),
  221. intval($r[0]['id']),
  222. intval($arr['uid'])
  223. );
  224. return $r[0]['id'];
  225. }
  226. else
  227. return 0;
  228. }
  229. else {
  230. // New event. Store it.
  231. $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`desc`,`location`,`type`,
  232. `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
  233. VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
  234. intval($arr['uid']),
  235. intval($arr['cid']),
  236. dbesc($arr['uri']),
  237. dbesc($arr['created']),
  238. dbesc($arr['edited']),
  239. dbesc($arr['start']),
  240. dbesc($arr['finish']),
  241. dbesc($arr['desc']),
  242. dbesc($arr['location']),
  243. dbesc($arr['type']),
  244. intval($arr['adjust']),
  245. intval($arr['nofinish']),
  246. dbesc($arr['allow_cid']),
  247. dbesc($arr['allow_gid']),
  248. dbesc($arr['deny_cid']),
  249. dbesc($arr['deny_gid'])
  250. );
  251. $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  252. dbesc($arr['uri']),
  253. intval($arr['uid'])
  254. );
  255. if(count($r))
  256. $event = $r[0];
  257. $item_arr = array();
  258. $item_arr['uid'] = $arr['uid'];
  259. $item_arr['contact-id'] = $arr['cid'];
  260. $item_arr['uri'] = $arr['uri'];
  261. $item_arr['parent-uri'] = $arr['uri'];
  262. $item_arr['type'] = 'activity';
  263. $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
  264. $item_arr['contact-id'] = $contact['id'];
  265. $item_arr['owner-name'] = $contact['name'];
  266. $item_arr['owner-link'] = $contact['url'];
  267. $item_arr['owner-avatar'] = $contact['thumb'];
  268. $item_arr['author-name'] = $contact['name'];
  269. $item_arr['author-link'] = $contact['url'];
  270. $item_arr['author-avatar'] = $contact['thumb'];
  271. $item_arr['title'] = '';
  272. $item_arr['allow_cid'] = $arr['allow_cid'];
  273. $item_arr['allow_gid'] = $arr['allow_gid'];
  274. $item_arr['deny_cid'] = $arr['deny_cid'];
  275. $item_arr['deny_gid'] = $arr['deny_gid'];
  276. $item_arr['private'] = $arr['private'];
  277. $item_arr['last-child'] = 1;
  278. $item_arr['visible'] = 1;
  279. $item_arr['verb'] = ACTIVITY_POST;
  280. $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
  281. $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
  282. $item_arr['body'] = format_event_bbcode($event);
  283. $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($uri) . '</id>';
  284. $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
  285. $item_arr['object'] .= '</object>' . "\n";
  286. $item_id = item_store($item_arr);
  287. $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
  288. intval($arr['uid'])
  289. );
  290. if(count($r))
  291. $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
  292. if($item_id) {
  293. q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d LIMIT 1",
  294. dbesc($plink),
  295. intval($event['id']),
  296. intval($arr['uid']),
  297. intval($item_id)
  298. );
  299. }
  300. return $item_id;
  301. }
  302. }