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.
 
 
 
 
 
 

445 lines
12 KiB

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