Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

427 righe
12 KiB

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