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.
 
 
 
 
 
 

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