<?xml version="1.0" encoding="utf-8"?>
<!---->
<feed xmlns="http://purl.org/atom/ns#">
  <id>urn:lj:klab.lv:atom1:kruzulis</id>
  <title>He&amp;apos;s off the map, he&amp;apos;s off the map!</title>
  <subtitle>kruzulis</subtitle>
  <tagline>kruzulis</tagline>
  <author>
    <email>kruzulis@gmail.com</email>
    <name>kruzulis</name>
  </author>
  <link rel="alternate" type="text/html" href="http://klab.lv/users/kruzulis/"/>
  <link rel="self" type="text/xml" href="http://klab.lv/users/kruzulis/data/atom"/>
  <updated>2022-05-09T11:10:22Z</updated>
  <modified>2022-05-09T11:10:22Z</modified>
  <link rel="service.feed" type="application/x.atom+xml" href="http://klab.lv/users/kruzulis/data/atom" title="He&amp;apos;s off the map, he&amp;apos;s off the map!"/>
  <entry>
    <title></title>
    <id>urn:lj:klab.lv:atom1:kruzulis:225524</id>
    <link rel="alternate" type="text/html" href="http://klab.lv/users/kruzulis/225524.html"/>
    <published>2022-05-09T13:12:00</published>
    <issued>2022-05-09T13:12:00</issued>
    <updated>2022-05-09T11:10:22Z</updated>
    <modified>2022-05-09T11:10:22Z</modified>
    <content type="html">Nokārtots&amp;lt;br /&amp;gt;&amp;lt;a name=&amp;quot;cutid1&amp;quot;&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;pre&amp;gt;
class LiveJournalArchive
{
	private $user;
	private $pass;

	/**
	 * @param mixed $user
	 * @param mixed $pass
	 */
	public function __construct($user, $pass)
	{
		$this-&amp;amp;gt;user = $user;
		$this-&amp;amp;gt;pass = md5($pass);
		$this-&amp;amp;gt;api = &amp;apos;http://www.klab.lv/interface/flat&amp;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;amp;gt;modifyEvents($year, $month, $day);
				}
			}
		}
	}

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

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

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

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

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

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

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

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

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

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

	private function parseResponse(string $responseText): array
	{
		$response = explode(&amp;quot;\n&amp;quot;, rtrim($responseText));
		$responseParsed = [];
		foreach ($response as $i =&amp;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(&amp;apos;username&amp;apos;, &amp;apos;pass&amp;apos;);
$kl-&amp;amp;gt;archive(2004, 2022);
&amp;lt;/pre&amp;gt;&amp;lt;br /&amp;gt;</content>
  </entry>
</feed>
