<?xml version='1.0' encoding='utf-8' ?>

<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/'>
<channel>
  <title>He&apos;s off the map, he&apos;s off the map!</title>
  <link>http://klab.lv/users/kruzulis/</link>
  <description>He&apos;s off the map, he&apos;s off the map! - Sviesta Ciba</description>
  <lastBuildDate>Mon, 09 May 2022 10:15:24 GMT</lastBuildDate>
  <generator>LiveJournal / Sviesta Ciba</generator>
  <image>
    <url>http://klab.lv/userpic/171812/6288</url>
    <title>He&apos;s off the map, he&apos;s off the map!</title>
    <link>http://klab.lv/users/kruzulis/</link>
    <width>100</width>
    <height>100</height>
  </image>

<item>
  <guid isPermaLink='true'>http://klab.lv/users/kruzulis/225524.html</guid>
  <pubDate>Mon, 09 May 2022 10:15:24 GMT</pubDate>
  <link>http://klab.lv/users/kruzulis/225524.html</link>
  <description>Nokārtots&lt;br /&gt;&lt;a name=&quot;cutid1&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;
class LiveJournalArchive
{
	private $user;
	private $pass;

	/**
	 * @param mixed $user
	 * @param mixed $pass
	 */
	public function __construct($user, $pass)
	{
		$this-&amp;gt;user = $user;
		$this-&amp;gt;pass = md5($pass);
		$this-&amp;gt;api = &apos;http://www.klab.lv/interface/flat&apos;;
	}

	public function archive($yearFrom, $yearTo)
	{
		foreach (range($yearFrom, $yearTo) as $year) {
			foreach (range(1, 12) as $month) {
				foreach (range(1, 31) as $day) {
					$this-&amp;gt;modifyEvents($year, $month, $day);
				}
			}
		}
	}

	private function modifyEvents($year, $month, $day)
	{
		$vars = [
			&apos;mode&apos; =&amp;gt; &apos;getevents&apos;,
			&apos;user&apos; =&amp;gt; &quot;{$this-&amp;gt;user}&quot;,
			&apos;ver&apos; =&amp;gt; 1,
			&apos;hpassword&apos; =&amp;gt; $this-&amp;gt;pass,
			&apos;prefersubject&apos; =&amp;gt; 0,
			&apos;noprops&apos; =&amp;gt; 0,
			&apos;selecttype&apos; =&amp;gt; &apos;day&apos;,
			&apos;year&apos; =&amp;gt; $year,
			&apos;month&apos; =&amp;gt; $month,
			&apos;day&apos; =&amp;gt; $day,
			&apos;lineendings&apos; =&amp;gt; &apos;pc&apos;,
		];

		$response = $this-&amp;gt;httpPost($this-&amp;gt;api, $vars);

		$events = $this-&amp;gt;parseResponse($response);

		$count = $events[&apos;events_count&apos;];
		if ($count &amp;gt; 0) {
			foreach (range(1, $count) as $i) {
				$event = $this-&amp;gt;decodeString($events[&apos;events_&apos;.$i.&apos;_event&apos;]);
				$subject = $events[&apos;events_&apos;.$i.&apos;_subject&apos;] ?? null;
				$itemId = $events[&apos;events_&apos;.$i.&apos;_itemid&apos;];
				$this-&amp;gt;editEvent($itemId, $subject, $event);
			}
		}
	}

	private function decodeString($who): array|string
	{
		$who = str_replace(&apos;+&apos;, &apos; &apos;, $who);
		$who = preg_replace_callback(
			&apos;/%([a-fA-F0-9]{2,2})/&apos;,
			static function ($matches) {
				$hexStr = preg_replace(&apos;/[^0-9A-Fa-f]/&apos;, &apos;&apos;, $matches[0]); // Gets a proper hex string

				return chr(hexdec($hexStr));
			},
			$who
		);

		return preg_replace(&apos;//&apos;, &apos;&apos;, $who);
	}

	private function editEvent($itemId, $subject, $event)
	{
		$security = &apos;private&apos;; //usemask|private|public
		$vars = [
			&apos;mode&apos; =&amp;gt; &apos;editevent&apos;,
			&apos;user&apos; =&amp;gt; $this-&amp;gt;user,
			&apos;ver&apos; =&amp;gt; 1,
			&apos;hpassword&apos; =&amp;gt; $this-&amp;gt;pass,
			&apos;itemid&apos; =&amp;gt; $itemId,
			&apos;subject&apos; =&amp;gt; $subject,
			&apos;event&apos; =&amp;gt; $event,
			&apos;lineendings&apos; =&amp;gt; &apos;unix&apos;,
			&apos;security&apos; =&amp;gt; $security,
			&apos;allowmask&apos; =&amp;gt; 0,
		];

		$response = $this-&amp;gt;httpPost($this-&amp;gt;api, $vars);
		$result = $this-&amp;gt;parseResponse($response);

		if ($result[&apos;success&apos;] === &apos;OK&apos;) {
			print &apos;itemId &apos;.$itemId.&quot; modified.\n&quot;;
		} else {
			print $result[&apos;errmsg&apos;];
		}
	}

	private function parseResponse(string $responseText): array
	{
		$response = explode(&quot;\n&quot;, rtrim($responseText));
		$responseParsed = [];
		foreach ($response as $i =&amp;gt; $text) {
			if ($i % 2 === 0) {
				$responseParsed[$text] = $response[$i + 1];
			}
		}

		return $responseParsed;
	}

	private function httpPost($url, $data)
	{
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($curl);
		curl_close($curl);

		return $response;
	}
}

$kl = new LiveJournalArchive(&apos;username&apos;, &apos;pass&apos;);
$kl-&amp;gt;archive(2004, 2022);
&lt;/pre&gt;&lt;br /&gt;</description>
</item>
</channel>
</rss>
