| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | WPListCal Import/Export functions |
|---|
| 4 | |
|---|
| 5 | Permission is hereby granted, free of charge, to any person or organization |
|---|
| 6 | obtaining a copy of the software and accompanying documentation covered by |
|---|
| 7 | this license (the "Software") to use, reproduce, display, distribute, |
|---|
| 8 | execute, and transmit the Software, and to prepare derivative works of the |
|---|
| 9 | Software, and to permit third-parties to whom the Software is furnished to |
|---|
| 10 | do so, all subject to the following: |
|---|
| 11 | |
|---|
| 12 | The copyright notices in the Software and this entire statement, including |
|---|
| 13 | the above license grant and author attributions, this restriction, and the |
|---|
| 14 | following disclaimer, must be included in all copies of the Software, in |
|---|
| 15 | whole or in part, and all derivative works of the Software, unless such |
|---|
| 16 | copies or derivative works are solely in the form of machine-executable |
|---|
| 17 | object code generated by a source language processor. |
|---|
| 18 | |
|---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
|---|
| 22 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
|---|
| 23 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
|---|
| 24 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 25 | DEALINGS IN THE SOFTWARE. |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | // If this isn't PHP5.1, we don't need this anyway |
|---|
| 29 | if(version_compare(phpversion(), '5.1.0') == 1) { |
|---|
| 30 | function set_tz_by_offset($offset) { |
|---|
| 31 | $abbrarray = timezone_abbreviations_list(); |
|---|
| 32 | foreach ($abbrarray as $abbr) { |
|---|
| 33 | foreach ($abbr as $city) { |
|---|
| 34 | if ($city['offset'] == $offset) { // remember to multiply $offset by -1 if you're getting it from js |
|---|
| 35 | date_default_timezone_set($city['timezone_id']); |
|---|
| 36 | return true; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | date_default_timezone_set("ust"); |
|---|
| 41 | return false; |
|---|
| 42 | } |
|---|
| 43 | set_tz_by_offset(get_option("gmt_offset")); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | require_once("lib/iCalcreator.class.php"); |
|---|
| 47 | |
|---|
| 48 | function wplc_export_events($id=null) { |
|---|
| 49 | global $wpdb; |
|---|
| 50 | |
|---|
| 51 | $blog_title = get_bloginfo("name"); |
|---|
| 52 | |
|---|
| 53 | // Create calendar |
|---|
| 54 | $cal = new vcalendar(); |
|---|
| 55 | $cal->setConfig("unique_id", "wplc_".$blog_title); |
|---|
| 56 | $cal->setProperty("method", "PUBLISH"); |
|---|
| 57 | |
|---|
| 58 | // Get events |
|---|
| 59 | $tbl_name = $wpdb->escape(get_option("wplc_tbl_name")); |
|---|
| 60 | $sql = "SELECT e.*, u.display_name as event_author FROM $tbl_name e LEFT JOIN $wpdb->users u ON e.event_author=u.user_login"; |
|---|
| 61 | if(!is_null($id)) { |
|---|
| 62 | $sql .= " WHERE e.id=".$wpdb->escape($id); |
|---|
| 63 | } |
|---|
| 64 | $sql .= " ORDER BY event_start_time ASC, event_end_time ASC"; |
|---|
| 65 | $wpdb->show_errors(); |
|---|
| 66 | $events = $wpdb->get_results($sql, ARRAY_A); |
|---|
| 67 | |
|---|
| 68 | // Create events |
|---|
| 69 | for($i=0; $i<count($events); $i++) { |
|---|
| 70 | // Create start and end date arrays |
|---|
| 71 | $start_date = wplc_time_to_ical_array($events[$i]['event_start_time']); |
|---|
| 72 | $end_date = wplc_time_to_ical_array($events[$i]['event_end_time']); |
|---|
| 73 | $create_date = wplc_time_to_ical_array($events[$i]['event_create_time']); |
|---|
| 74 | $mod_date = wplc_time_to_ical_array($events[$i]['event_modified_time']); |
|---|
| 75 | |
|---|
| 76 | $ev = new vevent(); |
|---|
| 77 | $ev->setProperty("summary", str_replace(" & ", " & ", str_replace('"', """, stripslashes(stripslashes($events[$i]['event_name']))))); |
|---|
| 78 | $ev->setProperty("url", htmlspecialchars(stripslashes(stripslashes($events[$i]['event_link'])))); |
|---|
| 79 | $ev->setProperty("location", $events[$i]['event_loc']); |
|---|
| 80 | $ev->setProperty("dtstart", $start_date); |
|---|
| 81 | $ev->setProperty("dtend", $end_date); |
|---|
| 82 | $ev->setProperty("description", nl2br(htmlspecialchars_decode(str_replace(" & ", " & ", str_replace('"', """, stripslashes(stripslashes($events[$i]['event_desc']))))))); |
|---|
| 83 | $ev->setProperty("organizer", $events[$i]['event_author']); |
|---|
| 84 | $ev->setProperty("created", $create_date); |
|---|
| 85 | $ev->setProperty("last-modified", $mod_date); |
|---|
| 86 | |
|---|
| 87 | $cal->setComponent($ev); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | $dir = get_option("wplc_upload_dir"); |
|---|
| 91 | $url = get_option("wplc_upload_url"); |
|---|
| 92 | |
|---|
| 93 | if(empty($dir) || empty($url)) { |
|---|
| 94 | wplc_init_upload_dir_settings(); |
|---|
| 95 | $dir = get_option("wplc_upload_dir"); |
|---|
| 96 | $url = get_option("wplc_upload_url"); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | @unlink($dir."events.ics"); |
|---|
| 100 | $cal->saveCalendar($dir, "events.ics"); |
|---|
| 101 | |
|---|
| 102 | ?> |
|---|
| 103 | <script type="text/javascript" charset="utf-8"> |
|---|
| 104 | window.location = '<?php echo $url."events.ics"; ?>'; |
|---|
| 105 | </script> |
|---|
| 106 | <?php |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | function wplc_import_events($file) { |
|---|
| 110 | global $wpdb, $current_user; |
|---|
| 111 | get_currentuserinfo(); |
|---|
| 112 | |
|---|
| 113 | $blog_title = get_bloginfo("name"); |
|---|
| 114 | |
|---|
| 115 | // Create calendar |
|---|
| 116 | $cal = new vcalendar(); |
|---|
| 117 | $cal->setConfig("unique_id", "wplc_".$blog_title); |
|---|
| 118 | $cal->setProperty("method", "PUBLISH"); |
|---|
| 119 | |
|---|
| 120 | // Get file and parse it |
|---|
| 121 | $cal->setConfig("directory", dirname($file)); |
|---|
| 122 | $cal->setConfig("filename", basename($file)); |
|---|
| 123 | if($cal->parse() === false) |
|---|
| 124 | return false; |
|---|
| 125 | $cal->sort(); |
|---|
| 126 | while($ev = $cal->getComponent("vevent")) { |
|---|
| 127 | $title = $wpdb->escape(addslashes($ev->getProperty("summary"))); |
|---|
| 128 | $link = $wpdb->escape(addslashes($ev->getProperty("url"))); |
|---|
| 129 | $location = $wpdb->escape(addslashes($ev->getProperty("location"))); |
|---|
| 130 | $start = wplc_ical_array_to_time($ev->getProperty("dtstart")); |
|---|
| 131 | $end = wplc_ical_array_to_time($ev->getProperty("dtend")); |
|---|
| 132 | $description = $wpdb->escape(wplc_br2nl(addslashes($ev->getProperty("description")))); |
|---|
| 133 | $create = wplc_ical_array_to_time($ev->getProperty("created")); |
|---|
| 134 | $modified = wplc_time(); |
|---|
| 135 | |
|---|
| 136 | $author = $current_user->user_login; |
|---|
| 137 | |
|---|
| 138 | // Convert time to local |
|---|
| 139 | $offset = (int)get_option("gmt_offset"); |
|---|
| 140 | $unix_offset = $offset * 3600; // offset in seconds |
|---|
| 141 | $start += $unix_offset; |
|---|
| 142 | $end += $unix_offset; |
|---|
| 143 | $create += $unix_offset; |
|---|
| 144 | |
|---|
| 145 | $tbl_name = $wpdb->escape(get_option("wplc_tbl_name")); |
|---|
| 146 | $sql = "INSERT INTO $tbl_name (event_name, event_link, event_loc, event_desc, event_start_time, event_end_time, event_author, event_create_time, event_modified_time) VALUES('$title', '$link', '$location', $start, $end, '$description', '$author', $create, $modified)"; |
|---|
| 147 | $wpdb->query($sql); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | return true; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | function wplc_time_to_ical_array($time) { |
|---|
| 154 | return array( |
|---|
| 155 | "year" => (int)date('Y', $time), |
|---|
| 156 | "month" => (int)date('n', $time), |
|---|
| 157 | "day" => (int)date('j', $time), |
|---|
| 158 | "hour" => (int)date('G', $time), |
|---|
| 159 | "min" => (int)date('i', $time), |
|---|
| 160 | "sec" => 0); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | function wplc_ical_array_to_time($time) { |
|---|
| 164 | return mktime( |
|---|
| 165 | $time['hour'], |
|---|
| 166 | $time['min'], |
|---|
| 167 | $time['sec'], |
|---|
| 168 | $time['month'], |
|---|
| 169 | $time['day'], |
|---|
| 170 | $time['year']); |
|---|
| 171 | } |
|---|
| 172 | ?> |
|---|